blob: 13c40f7ca1dc01678d24e08946ff51bce810b2f9 [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerddc135e2006-11-10 06:34:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyck8c89d592009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff67391b82007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
John McCall87fe5d52010-05-20 01:18:31 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000023#include "clang/AST/ASTMutationListener.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000025#include "clang/AST/Mangle.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000027#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000028#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000029#include "llvm/ADT/SmallString.h"
Anders Carlssond8499822007-10-29 05:01:08 +000030#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000031#include "llvm/Support/MathExtras.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000032#include "llvm/Support/raw_ostream.h"
Charles Davis53c59df2010-08-16 03:33:14 +000033#include "CXXABI.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000034
Chris Lattnerddc135e2006-11-10 06:34:16 +000035using namespace clang;
36
Douglas Gregor9672f922010-07-03 00:47:00 +000037unsigned ASTContext::NumImplicitDefaultConstructors;
38unsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
Douglas Gregora6d69502010-07-02 23:41:54 +000039unsigned ASTContext::NumImplicitCopyConstructors;
40unsigned ASTContext::NumImplicitCopyConstructorsDeclared;
Douglas Gregor330b9cf2010-07-02 21:50:04 +000041unsigned ASTContext::NumImplicitCopyAssignmentOperators;
42unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
Douglas Gregor7454c562010-07-02 20:37:36 +000043unsigned ASTContext::NumImplicitDestructors;
44unsigned ASTContext::NumImplicitDestructorsDeclared;
45
Steve Naroff0af91202007-04-27 21:51:21 +000046enum FloatingRank {
47 FloatRank, DoubleRank, LongDoubleRank
48};
49
Douglas Gregor7dbfb462010-06-16 21:09:37 +000050void
51ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
52 TemplateTemplateParmDecl *Parm) {
53 ID.AddInteger(Parm->getDepth());
54 ID.AddInteger(Parm->getPosition());
Douglas Gregorf5500772011-01-05 15:48:55 +000055 ID.AddBoolean(Parm->isParameterPack());
Douglas Gregor7dbfb462010-06-16 21:09:37 +000056
57 TemplateParameterList *Params = Parm->getTemplateParameters();
58 ID.AddInteger(Params->size());
59 for (TemplateParameterList::const_iterator P = Params->begin(),
60 PEnd = Params->end();
61 P != PEnd; ++P) {
62 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
63 ID.AddInteger(0);
64 ID.AddBoolean(TTP->isParameterPack());
65 continue;
66 }
67
68 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
69 ID.AddInteger(1);
Douglas Gregorf5500772011-01-05 15:48:55 +000070 ID.AddBoolean(NTTP->isParameterPack());
Douglas Gregor7dbfb462010-06-16 21:09:37 +000071 ID.AddPointer(NTTP->getType().getAsOpaquePtr());
Douglas Gregor0231d8d2011-01-19 20:10:05 +000072 if (NTTP->isExpandedParameterPack()) {
73 ID.AddBoolean(true);
74 ID.AddInteger(NTTP->getNumExpansionTypes());
75 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I)
76 ID.AddPointer(NTTP->getExpansionType(I).getAsOpaquePtr());
77 } else
78 ID.AddBoolean(false);
Douglas Gregor7dbfb462010-06-16 21:09:37 +000079 continue;
80 }
81
82 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
83 ID.AddInteger(2);
84 Profile(ID, TTP);
85 }
86}
87
88TemplateTemplateParmDecl *
89ASTContext::getCanonicalTemplateTemplateParmDecl(
Jay Foad39c79802011-01-12 09:06:06 +000090 TemplateTemplateParmDecl *TTP) const {
Douglas Gregor7dbfb462010-06-16 21:09:37 +000091 // Check if we already have a canonical template template parameter.
92 llvm::FoldingSetNodeID ID;
93 CanonicalTemplateTemplateParm::Profile(ID, TTP);
94 void *InsertPos = 0;
95 CanonicalTemplateTemplateParm *Canonical
96 = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
97 if (Canonical)
98 return Canonical->getParam();
99
100 // Build a canonical template parameter list.
101 TemplateParameterList *Params = TTP->getTemplateParameters();
102 llvm::SmallVector<NamedDecl *, 4> CanonParams;
103 CanonParams.reserve(Params->size());
104 for (TemplateParameterList::const_iterator P = Params->begin(),
105 PEnd = Params->end();
106 P != PEnd; ++P) {
107 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
108 CanonParams.push_back(
109 TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000110 SourceLocation(),
111 SourceLocation(),
112 TTP->getDepth(),
Douglas Gregor7dbfb462010-06-16 21:09:37 +0000113 TTP->getIndex(), 0, false,
114 TTP->isParameterPack()));
115 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000116 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
117 QualType T = getCanonicalType(NTTP->getType());
118 TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
119 NonTypeTemplateParmDecl *Param;
120 if (NTTP->isExpandedParameterPack()) {
121 llvm::SmallVector<QualType, 2> ExpandedTypes;
122 llvm::SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
123 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
124 ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
125 ExpandedTInfos.push_back(
126 getTrivialTypeSourceInfo(ExpandedTypes.back()));
127 }
128
129 Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +0000130 SourceLocation(),
131 SourceLocation(),
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000132 NTTP->getDepth(),
133 NTTP->getPosition(), 0,
134 T,
135 TInfo,
136 ExpandedTypes.data(),
137 ExpandedTypes.size(),
138 ExpandedTInfos.data());
139 } else {
140 Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +0000141 SourceLocation(),
142 SourceLocation(),
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000143 NTTP->getDepth(),
144 NTTP->getPosition(), 0,
145 T,
146 NTTP->isParameterPack(),
147 TInfo);
148 }
149 CanonParams.push_back(Param);
150
151 } else
Douglas Gregor7dbfb462010-06-16 21:09:37 +0000152 CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
153 cast<TemplateTemplateParmDecl>(*P)));
154 }
155
156 TemplateTemplateParmDecl *CanonTTP
157 = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
158 SourceLocation(), TTP->getDepth(),
Douglas Gregorf5500772011-01-05 15:48:55 +0000159 TTP->getPosition(),
160 TTP->isParameterPack(),
161 0,
Douglas Gregor7dbfb462010-06-16 21:09:37 +0000162 TemplateParameterList::Create(*this, SourceLocation(),
163 SourceLocation(),
164 CanonParams.data(),
165 CanonParams.size(),
166 SourceLocation()));
167
168 // Get the new insert position for the node we care about.
169 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
170 assert(Canonical == 0 && "Shouldn't be in the map!");
171 (void)Canonical;
172
173 // Create the canonical template template parameter entry.
174 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
175 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
176 return CanonTTP;
177}
178
Charles Davis53c59df2010-08-16 03:33:14 +0000179CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
John McCall86353412010-08-21 22:46:04 +0000180 if (!LangOpts.CPlusPlus) return 0;
181
Charles Davis6bcb07a2010-08-19 02:18:14 +0000182 switch (T.getCXXABI()) {
John McCall86353412010-08-21 22:46:04 +0000183 case CXXABI_ARM:
184 return CreateARMCXXABI(*this);
185 case CXXABI_Itanium:
Charles Davis53c59df2010-08-16 03:33:14 +0000186 return CreateItaniumCXXABI(*this);
Charles Davis6bcb07a2010-08-19 02:18:14 +0000187 case CXXABI_Microsoft:
188 return CreateMicrosoftCXXABI(*this);
189 }
John McCall86353412010-08-21 22:46:04 +0000190 return 0;
Charles Davis53c59df2010-08-16 03:33:14 +0000191}
192
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000193static const LangAS::Map &getAddressSpaceMap(const TargetInfo &T,
194 const LangOptions &LOpts) {
195 if (LOpts.FakeAddressSpaceMap) {
196 // The fake address space map must have a distinct entry for each
197 // language-specific address space.
198 static const unsigned FakeAddrSpaceMap[] = {
199 1, // opencl_global
200 2, // opencl_local
201 3 // opencl_constant
202 };
203 return FakeAddrSpaceMap;
204 } else {
205 return T.getAddressSpaceMap();
206 }
207}
208
Chris Lattner465fa322008-10-05 17:34:18 +0000209ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar1b444192009-11-13 05:51:54 +0000210 const TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +0000211 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +0000212 Builtin::Context &builtins,
Douglas Gregor5b11d492010-07-25 17:53:33 +0000213 unsigned size_reserve) :
Sebastian Redl31ad7542011-03-13 17:09:40 +0000214 FunctionProtoTypes(this_()),
John McCall773cc982010-06-11 11:07:21 +0000215 TemplateSpecializationTypes(this_()),
216 DependentTemplateSpecializationTypes(this_()),
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +0000217 GlobalNestedNameSpecifier(0), IsInt128Installed(false),
218 CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +0000219 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +0000220 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +0000221 cudaConfigureCallDecl(0),
John McCall8cb7bdf2010-06-04 23:28:52 +0000222 NullTypeSourceInfo(QualType()),
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000223 SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)),
224 AddrSpaceMap(getAddressSpaceMap(t, LOpts)), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000225 Idents(idents), Selectors(sels),
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000226 BuiltinInfo(builtins),
227 DeclarationNames(*this),
Argyrios Kyrtzidis440ea322010-10-28 09:29:35 +0000228 ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
Ted Kremenek520f47b2010-06-18 00:31:04 +0000229 LastSDM(0, 0),
John McCall147d0212011-02-22 22:38:33 +0000230 UniqueBlockByRefTypeID(0) {
David Chisnall9f57c292009-08-17 16:35:33 +0000231 ObjCIdRedefinitionType = QualType();
232 ObjCClassRedefinitionType = QualType();
Douglas Gregor5b11d492010-07-25 17:53:33 +0000233 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000234 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +0000235 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000236 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +0000237}
238
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000239ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000240 // Release the DenseMaps associated with DeclContext objects.
241 // FIXME: Is this the ideal solution?
242 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +0000243
Douglas Gregor5b11d492010-07-25 17:53:33 +0000244 // Call all of the deallocation functions.
245 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
246 Deallocations[I].first(Deallocations[I].second);
Douglas Gregor1a809332010-05-23 18:26:36 +0000247
Douglas Gregor832940b2010-03-02 23:58:15 +0000248 // Release all of the memory associated with overridden C++ methods.
249 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
250 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
251 OM != OMEnd; ++OM)
252 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000253
Ted Kremenek076baeb2010-06-08 23:00:58 +0000254 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
Douglas Gregor5b11d492010-07-25 17:53:33 +0000255 // because they can contain DenseMaps.
256 for (llvm::DenseMap<const ObjCContainerDecl*,
257 const ASTRecordLayout*>::iterator
258 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
259 // Increment in loop to prevent using deallocated memory.
260 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
261 R->Destroy(*this);
262
Ted Kremenek076baeb2010-06-08 23:00:58 +0000263 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
264 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
265 // Increment in loop to prevent using deallocated memory.
266 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
267 R->Destroy(*this);
268 }
Douglas Gregor561eceb2010-08-30 16:49:28 +0000269
270 for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
271 AEnd = DeclAttrs.end();
272 A != AEnd; ++A)
273 A->second->~AttrVec();
274}
Douglas Gregorf21eb492009-03-26 23:50:42 +0000275
Douglas Gregor1a809332010-05-23 18:26:36 +0000276void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
277 Deallocations.push_back(std::make_pair(Callback, Data));
278}
279
Mike Stump11289f42009-09-09 15:08:12 +0000280void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000281ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
282 ExternalSource.reset(Source.take());
283}
284
Chris Lattner4eb445d2007-01-26 01:27:23 +0000285void ASTContext::PrintStats() const {
286 fprintf(stderr, "*** AST Context Stats:\n");
287 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000288
Douglas Gregora30d0462009-05-26 14:40:08 +0000289 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000290#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000291#define ABSTRACT_TYPE(Name, Parent)
292#include "clang/AST/TypeNodes.def"
293 0 // Extra
294 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000295
Chris Lattner4eb445d2007-01-26 01:27:23 +0000296 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
297 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000298 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000299 }
300
Douglas Gregora30d0462009-05-26 14:40:08 +0000301 unsigned Idx = 0;
302 unsigned TotalBytes = 0;
303#define TYPE(Name, Parent) \
304 if (counts[Idx]) \
305 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
306 TotalBytes += counts[Idx] * sizeof(Name##Type); \
307 ++Idx;
308#define ABSTRACT_TYPE(Name, Parent)
309#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000310
Douglas Gregora30d0462009-05-26 14:40:08 +0000311 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor747eb782010-07-08 06:14:04 +0000312
Douglas Gregor7454c562010-07-02 20:37:36 +0000313 // Implicit special member functions.
Douglas Gregor9672f922010-07-03 00:47:00 +0000314 fprintf(stderr, " %u/%u implicit default constructors created\n",
315 NumImplicitDefaultConstructorsDeclared,
316 NumImplicitDefaultConstructors);
Douglas Gregora6d69502010-07-02 23:41:54 +0000317 fprintf(stderr, " %u/%u implicit copy constructors created\n",
318 NumImplicitCopyConstructorsDeclared,
319 NumImplicitCopyConstructors);
Douglas Gregor330b9cf2010-07-02 21:50:04 +0000320 fprintf(stderr, " %u/%u implicit copy assignment operators created\n",
321 NumImplicitCopyAssignmentOperatorsDeclared,
322 NumImplicitCopyAssignmentOperators);
Douglas Gregor7454c562010-07-02 20:37:36 +0000323 fprintf(stderr, " %u/%u implicit destructors created\n",
324 NumImplicitDestructorsDeclared, NumImplicitDestructors);
325
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000326 if (ExternalSource.get()) {
327 fprintf(stderr, "\n");
328 ExternalSource->PrintStats();
329 }
Douglas Gregor747eb782010-07-08 06:14:04 +0000330
Douglas Gregor5b11d492010-07-25 17:53:33 +0000331 BumpAlloc.PrintStats();
Chris Lattner4eb445d2007-01-26 01:27:23 +0000332}
333
334
John McCall48f2d582009-10-23 23:03:21 +0000335void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000336 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000337 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000338 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000339}
340
Chris Lattner970e54e2006-11-12 00:37:36 +0000341void ASTContext::InitBuiltinTypes() {
342 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattner970e54e2006-11-12 00:37:36 +0000344 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000345 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner970e54e2006-11-12 00:37:36 +0000347 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000348 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000349 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000350 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000351 InitBuiltinType(CharTy, BuiltinType::Char_S);
352 else
353 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000354 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000355 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
356 InitBuiltinType(ShortTy, BuiltinType::Short);
357 InitBuiltinType(IntTy, BuiltinType::Int);
358 InitBuiltinType(LongTy, BuiltinType::Long);
359 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattner970e54e2006-11-12 00:37:36 +0000361 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000362 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
363 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
364 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
365 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
366 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattner970e54e2006-11-12 00:37:36 +0000368 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000369 InitBuiltinType(FloatTy, BuiltinType::Float);
370 InitBuiltinType(DoubleTy, BuiltinType::Double);
371 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000372
Chris Lattnerf122cef2009-04-30 02:43:43 +0000373 // GNU extension, 128-bit integers.
374 InitBuiltinType(Int128Ty, BuiltinType::Int128);
375 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
376
Chris Lattnerad3467e2010-12-25 23:25:43 +0000377 if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
378 if (!LangOpts.ShortWChar)
379 InitBuiltinType(WCharTy, BuiltinType::WChar_S);
380 else // -fshort-wchar makes wchar_t be unsigned.
381 InitBuiltinType(WCharTy, BuiltinType::WChar_U);
382 } else // C99
Chris Lattner007cb022009-02-26 23:43:47 +0000383 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000384
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000385 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
386 InitBuiltinType(Char16Ty, BuiltinType::Char16);
387 else // C99
388 Char16Ty = getFromTargetType(Target.getChar16Type());
389
390 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
391 InitBuiltinType(Char32Ty, BuiltinType::Char32);
392 else // C99
393 Char32Ty = getFromTargetType(Target.getChar32Type());
394
Douglas Gregor4619e432008-12-05 23:32:09 +0000395 // Placeholder type for type-dependent expressions whose type is
396 // completely unknown. No code should ever check a type against
397 // DependentTy and users should never see it; however, it is here to
398 // help diagnose failures to properly check for type-dependent
399 // expressions.
400 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000401
John McCall36e7fe32010-10-12 00:20:44 +0000402 // Placeholder type for functions.
403 InitBuiltinType(OverloadTy, BuiltinType::Overload);
404
John McCall0009fcc2011-04-26 20:42:42 +0000405 // Placeholder type for bound members.
406 InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember);
407
John McCall31996342011-04-07 08:22:57 +0000408 // "any" type; useful for debugger-like clients.
409 InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny);
410
Chris Lattner970e54e2006-11-12 00:37:36 +0000411 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000412 FloatComplexTy = getComplexType(FloatTy);
413 DoubleComplexTy = getComplexType(DoubleTy);
414 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000415
Steve Naroff66e9f332007-10-15 14:41:52 +0000416 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000417
Steve Naroff1329fa02009-07-15 18:40:39 +0000418 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
419 ObjCIdTypedefType = QualType();
420 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000421 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000422
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000423 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000424 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
425 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000426 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000427
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000428 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000429
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000430 // void * type
431 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000432
433 // nullptr type (C++0x 2.14.7)
434 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000435}
436
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000437Diagnostic &ASTContext::getDiagnostics() const {
438 return SourceMgr.getDiagnostics();
439}
440
Douglas Gregor561eceb2010-08-30 16:49:28 +0000441AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
442 AttrVec *&Result = DeclAttrs[D];
443 if (!Result) {
444 void *Mem = Allocate(sizeof(AttrVec));
445 Result = new (Mem) AttrVec;
446 }
447
448 return *Result;
449}
450
451/// \brief Erase the attributes corresponding to the given declaration.
452void ASTContext::eraseDeclAttrs(const Decl *D) {
453 llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
454 if (Pos != DeclAttrs.end()) {
455 Pos->second->~AttrVec();
456 DeclAttrs.erase(Pos);
457 }
458}
459
Douglas Gregor86d142a2009-10-08 07:24:58 +0000460MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000461ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000462 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000463 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000464 = InstantiatedFromStaticDataMember.find(Var);
465 if (Pos == InstantiatedFromStaticDataMember.end())
466 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000468 return Pos->second;
469}
470
Mike Stump11289f42009-09-09 15:08:12 +0000471void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000472ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000473 TemplateSpecializationKind TSK,
474 SourceLocation PointOfInstantiation) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000475 assert(Inst->isStaticDataMember() && "Not a static data member");
476 assert(Tmpl->isStaticDataMember() && "Not a static data member");
477 assert(!InstantiatedFromStaticDataMember[Inst] &&
478 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000479 InstantiatedFromStaticDataMember[Inst]
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000480 = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000481}
482
John McCalle61f2ba2009-11-18 02:36:19 +0000483NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000484ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000485 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000486 = InstantiatedFromUsingDecl.find(UUD);
487 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000488 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000489
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000490 return Pos->second;
491}
492
493void
John McCallb96ec562009-12-04 22:46:56 +0000494ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
495 assert((isa<UsingDecl>(Pattern) ||
496 isa<UnresolvedUsingValueDecl>(Pattern) ||
497 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
498 "pattern decl is not a using decl");
499 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
500 InstantiatedFromUsingDecl[Inst] = Pattern;
501}
502
503UsingShadowDecl *
504ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
505 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
506 = InstantiatedFromUsingShadowDecl.find(Inst);
507 if (Pos == InstantiatedFromUsingShadowDecl.end())
508 return 0;
509
510 return Pos->second;
511}
512
513void
514ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
515 UsingShadowDecl *Pattern) {
516 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
517 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000518}
519
Anders Carlsson5da84842009-09-01 04:26:58 +0000520FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
521 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
522 = InstantiatedFromUnnamedFieldDecl.find(Field);
523 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
524 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000525
Anders Carlsson5da84842009-09-01 04:26:58 +0000526 return Pos->second;
527}
528
529void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
530 FieldDecl *Tmpl) {
531 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
532 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
533 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
534 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000535
Anders Carlsson5da84842009-09-01 04:26:58 +0000536 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
537}
538
Fariborz Jahanian595ec5d2011-04-27 17:14:21 +0000539bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
540 const FieldDecl *LastFD) const {
541 return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
542 FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() == 0);
543
544}
545
Douglas Gregor832940b2010-03-02 23:58:15 +0000546ASTContext::overridden_cxx_method_iterator
547ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
548 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
549 = OverriddenMethods.find(Method);
550 if (Pos == OverriddenMethods.end())
551 return 0;
552
553 return Pos->second.begin();
554}
555
556ASTContext::overridden_cxx_method_iterator
557ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
558 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
559 = OverriddenMethods.find(Method);
560 if (Pos == OverriddenMethods.end())
561 return 0;
562
563 return Pos->second.end();
564}
565
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000566unsigned
567ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
568 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
569 = OverriddenMethods.find(Method);
570 if (Pos == OverriddenMethods.end())
571 return 0;
572
573 return Pos->second.size();
574}
575
Douglas Gregor832940b2010-03-02 23:58:15 +0000576void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
577 const CXXMethodDecl *Overridden) {
578 OverriddenMethods[Method].push_back(Overridden);
579}
580
Chris Lattner53cfe802007-07-18 17:52:12 +0000581//===----------------------------------------------------------------------===//
582// Type Sizing and Analysis
583//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000584
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000585/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
586/// scalar floating point type.
587const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000588 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000589 assert(BT && "Not a floating point type!");
590 switch (BT->getKind()) {
591 default: assert(0 && "Not a floating point type!");
592 case BuiltinType::Float: return Target.getFloatFormat();
593 case BuiltinType::Double: return Target.getDoubleFormat();
594 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
595 }
596}
597
Ken Dyck160146e2010-01-27 17:10:57 +0000598/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000599/// specified decl. Note that bitfields do not have a valid alignment, so
600/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000601/// If @p RefAsPointee, references are treated like their underlying type
602/// (for alignof), else they're treated like pointers (for CodeGen).
Jay Foad39c79802011-01-12 09:06:06 +0000603CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
Eli Friedman19a546c2009-02-22 02:56:25 +0000604 unsigned Align = Target.getCharWidth();
605
John McCall94268702010-10-08 18:24:19 +0000606 bool UseAlignAttrOnly = false;
607 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
608 Align = AlignFromAttr;
Eli Friedman19a546c2009-02-22 02:56:25 +0000609
John McCall94268702010-10-08 18:24:19 +0000610 // __attribute__((aligned)) can increase or decrease alignment
611 // *except* on a struct or struct member, where it only increases
612 // alignment unless 'packed' is also specified.
613 //
614 // It is an error for [[align]] to decrease alignment, so we can
615 // ignore that possibility; Sema should diagnose it.
616 if (isa<FieldDecl>(D)) {
617 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
618 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
619 } else {
620 UseAlignAttrOnly = true;
621 }
622 }
623
John McCall4e819612011-01-20 07:57:12 +0000624 // If we're using the align attribute only, just ignore everything
625 // else about the declaration and its type.
John McCall94268702010-10-08 18:24:19 +0000626 if (UseAlignAttrOnly) {
John McCall4e819612011-01-20 07:57:12 +0000627 // do nothing
628
John McCall94268702010-10-08 18:24:19 +0000629 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner68061312009-01-24 21:53:27 +0000630 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000631 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000632 if (RefAsPointee)
633 T = RT->getPointeeType();
634 else
635 T = getPointerType(RT->getPointeeType());
636 }
637 if (!T->isIncompleteType() && !T->isFunctionType()) {
John McCall33ddac02011-01-19 10:06:00 +0000638 // Adjust alignments of declarations with array type by the
639 // large-array alignment on the target.
Rafael Espindolae971b9a2010-06-04 23:15:27 +0000640 unsigned MinWidth = Target.getLargeArrayMinWidth();
John McCall33ddac02011-01-19 10:06:00 +0000641 const ArrayType *arrayType;
642 if (MinWidth && (arrayType = getAsArrayType(T))) {
643 if (isa<VariableArrayType>(arrayType))
644 Align = std::max(Align, Target.getLargeArrayAlign());
645 else if (isa<ConstantArrayType>(arrayType) &&
646 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
647 Align = std::max(Align, Target.getLargeArrayAlign());
Eli Friedman19a546c2009-02-22 02:56:25 +0000648
John McCall33ddac02011-01-19 10:06:00 +0000649 // Walk through any array types while we're at it.
650 T = getBaseElementType(arrayType);
651 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000652 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
653 }
John McCall4e819612011-01-20 07:57:12 +0000654
655 // Fields can be subject to extra alignment constraints, like if
656 // the field is packed, the struct is packed, or the struct has a
657 // a max-field-alignment constraint (#pragma pack). So calculate
658 // the actual alignment of the field within the struct, and then
659 // (as we're expected to) constrain that by the alignment of the type.
660 if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
661 // So calculate the alignment of the field.
662 const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
663
664 // Start with the record's overall alignment.
Ken Dyck7ad11e72011-02-15 02:32:40 +0000665 unsigned fieldAlign = toBits(layout.getAlignment());
John McCall4e819612011-01-20 07:57:12 +0000666
667 // Use the GCD of that and the offset within the record.
668 uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
669 if (offset > 0) {
670 // Alignment is always a power of 2, so the GCD will be a power of 2,
671 // which means we get to do this crazy thing instead of Euclid's.
672 uint64_t lowBitOfOffset = offset & (~offset + 1);
673 if (lowBitOfOffset < fieldAlign)
674 fieldAlign = static_cast<unsigned>(lowBitOfOffset);
675 }
676
677 Align = std::min(Align, fieldAlign);
Charles Davis3fc51072010-02-23 04:52:00 +0000678 }
Chris Lattner68061312009-01-24 21:53:27 +0000679 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000680
Ken Dyckcc56c542011-01-15 18:38:59 +0000681 return toCharUnitsFromBits(Align);
Chris Lattner68061312009-01-24 21:53:27 +0000682}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000683
John McCall87fe5d52010-05-20 01:18:31 +0000684std::pair<CharUnits, CharUnits>
Ken Dyckd24099d2011-02-20 01:55:18 +0000685ASTContext::getTypeInfoInChars(const Type *T) const {
John McCall87fe5d52010-05-20 01:18:31 +0000686 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
Ken Dyckcc56c542011-01-15 18:38:59 +0000687 return std::make_pair(toCharUnitsFromBits(Info.first),
688 toCharUnitsFromBits(Info.second));
John McCall87fe5d52010-05-20 01:18:31 +0000689}
690
691std::pair<CharUnits, CharUnits>
Ken Dyckd24099d2011-02-20 01:55:18 +0000692ASTContext::getTypeInfoInChars(QualType T) const {
John McCall87fe5d52010-05-20 01:18:31 +0000693 return getTypeInfoInChars(T.getTypePtr());
694}
695
Chris Lattner983a8bb2007-07-13 22:13:22 +0000696/// getTypeSize - Return the size of the specified type, in bits. This method
697/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000698///
699/// FIXME: Pointers into different addr spaces could have different sizes and
700/// alignment requirements: getPointerInfo should take an AddrSpace, this
701/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000702std::pair<uint64_t, unsigned>
Jay Foad39c79802011-01-12 09:06:06 +0000703ASTContext::getTypeInfo(const Type *T) const {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000704 uint64_t Width=0;
705 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000706 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000707#define TYPE(Class, Base)
708#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000709#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000710#define DEPENDENT_TYPE(Class, Base) case Type::Class:
711#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000712 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000713 break;
714
Chris Lattner355332d2007-07-13 22:27:08 +0000715 case Type::FunctionNoProto:
716 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000717 // GCC extension: alignof(function) = 32 bits
718 Width = 0;
719 Align = 32;
720 break;
721
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000722 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000723 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000724 Width = 0;
725 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
726 break;
727
Steve Naroff5c131802007-08-30 01:06:46 +0000728 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000729 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000730
Chris Lattner37e05872008-03-05 18:54:05 +0000731 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000732 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000733 Align = EltInfo.second;
Argyrios Kyrtzidisae40e4e2011-04-26 21:05:39 +0000734 Width = llvm::RoundUpToAlignment(Width, Align);
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000735 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000736 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000737 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000738 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000739 const VectorType *VT = cast<VectorType>(T);
740 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
741 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000742 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000743 // If the alignment is not a power of 2, round up to the next power of 2.
744 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000745 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000746 Align = llvm::NextPowerOf2(Align);
747 Width = llvm::RoundUpToAlignment(Width, Align);
748 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000749 break;
750 }
Chris Lattner647fb222007-07-18 18:26:58 +0000751
Chris Lattner7570e9c2008-03-08 08:52:55 +0000752 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000753 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000754 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000755 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000756 // GCC extension: alignof(void) = 8 bits.
757 Width = 0;
758 Align = 8;
759 break;
760
Chris Lattner6a4f7452007-12-19 19:23:28 +0000761 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000762 Width = Target.getBoolWidth();
763 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000764 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000765 case BuiltinType::Char_S:
766 case BuiltinType::Char_U:
767 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000768 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000769 Width = Target.getCharWidth();
770 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000771 break;
Chris Lattnerad3467e2010-12-25 23:25:43 +0000772 case BuiltinType::WChar_S:
773 case BuiltinType::WChar_U:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000774 Width = Target.getWCharWidth();
775 Align = Target.getWCharAlign();
776 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000777 case BuiltinType::Char16:
778 Width = Target.getChar16Width();
779 Align = Target.getChar16Align();
780 break;
781 case BuiltinType::Char32:
782 Width = Target.getChar32Width();
783 Align = Target.getChar32Align();
784 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000785 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000786 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000787 Width = Target.getShortWidth();
788 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000789 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000790 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000791 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000792 Width = Target.getIntWidth();
793 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000794 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000795 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000796 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000797 Width = Target.getLongWidth();
798 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000799 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000800 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000801 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000802 Width = Target.getLongLongWidth();
803 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000804 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000805 case BuiltinType::Int128:
806 case BuiltinType::UInt128:
807 Width = 128;
808 Align = 128; // int128_t is 128-bit aligned on all targets.
809 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000810 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000811 Width = Target.getFloatWidth();
812 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000813 break;
814 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000815 Width = Target.getDoubleWidth();
816 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000817 break;
818 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000819 Width = Target.getLongDoubleWidth();
820 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000821 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000822 case BuiltinType::NullPtr:
823 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
824 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000825 break;
Fariborz Jahanian9c6a39e2010-08-02 18:03:20 +0000826 case BuiltinType::ObjCId:
827 case BuiltinType::ObjCClass:
828 case BuiltinType::ObjCSel:
829 Width = Target.getPointerWidth(0);
830 Align = Target.getPointerAlign(0);
831 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000832 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000833 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000834 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000835 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000836 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000837 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000838 case Type::BlockPointer: {
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000839 unsigned AS = getTargetAddressSpace(
840 cast<BlockPointerType>(T)->getPointeeType());
Steve Naroff921a45c2008-09-24 15:05:44 +0000841 Width = Target.getPointerWidth(AS);
842 Align = Target.getPointerAlign(AS);
843 break;
844 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000845 case Type::LValueReference:
846 case Type::RValueReference: {
847 // alignof and sizeof should never enter this code path here, so we go
848 // the pointer route.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000849 unsigned AS = getTargetAddressSpace(
850 cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000851 Width = Target.getPointerWidth(AS);
852 Align = Target.getPointerAlign(AS);
853 break;
854 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000855 case Type::Pointer: {
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000856 unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000857 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000858 Align = Target.getPointerAlign(AS);
859 break;
860 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000861 case Type::MemberPointer: {
Charles Davis53c59df2010-08-16 03:33:14 +0000862 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000863 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000864 getTypeInfo(getPointerDiffType());
Charles Davis53c59df2010-08-16 03:33:14 +0000865 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson32440a02009-05-17 02:06:04 +0000866 Align = PtrDiffInfo.second;
867 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000868 }
Chris Lattner647fb222007-07-18 18:26:58 +0000869 case Type::Complex: {
870 // Complex types have the same alignment as their elements, but twice the
871 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000872 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000873 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000874 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000875 Align = EltInfo.second;
876 break;
877 }
John McCall8b07ec22010-05-15 11:32:37 +0000878 case Type::ObjCObject:
879 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Pateldbb72632008-06-04 21:54:36 +0000880 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000881 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000882 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
Ken Dyckb0fcc592011-02-11 01:54:29 +0000883 Width = toBits(Layout.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000884 Align = toBits(Layout.getAlignment());
Devang Pateldbb72632008-06-04 21:54:36 +0000885 break;
886 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000887 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000888 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000889 const TagType *TT = cast<TagType>(T);
890
891 if (TT->getDecl()->isInvalidDecl()) {
Douglas Gregor7f971892011-04-20 17:29:44 +0000892 Width = 8;
893 Align = 8;
Chris Lattner572100b2008-08-09 21:35:13 +0000894 break;
895 }
Mike Stump11289f42009-09-09 15:08:12 +0000896
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000897 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000898 return getTypeInfo(ET->getDecl()->getIntegerType());
899
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000900 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000901 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
Ken Dyckb0fcc592011-02-11 01:54:29 +0000902 Width = toBits(Layout.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000903 Align = toBits(Layout.getAlignment());
Chris Lattner49a953a2007-07-23 22:46:22 +0000904 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000905 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000906
Chris Lattner63d2b362009-10-22 05:17:15 +0000907 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000908 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
909 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000910
Richard Smith30482bc2011-02-20 03:19:35 +0000911 case Type::Auto: {
912 const AutoType *A = cast<AutoType>(T);
913 assert(A->isDeduced() && "Cannot request the size of a dependent type");
Matt Beaumont-Gay7a24210e2011-02-22 20:00:16 +0000914 return getTypeInfo(A->getDeducedType().getTypePtr());
Richard Smith30482bc2011-02-20 03:19:35 +0000915 }
916
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000917 case Type::Paren:
918 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
919
Douglas Gregoref462e62009-04-30 17:32:17 +0000920 case Type::Typedef: {
Richard Smithdda56e42011-04-15 14:24:37 +0000921 const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregorf5bae222010-08-27 00:11:28 +0000922 std::pair<uint64_t, unsigned> Info
923 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Chris Lattner29eb47b2011-02-19 22:55:41 +0000924 // If the typedef has an aligned attribute on it, it overrides any computed
925 // alignment we have. This violates the GCC documentation (which says that
926 // attribute(aligned) can only round up) but matches its implementation.
927 if (unsigned AttrAlign = Typedef->getMaxAlignment())
928 Align = AttrAlign;
929 else
930 Align = Info.second;
Douglas Gregorf5bae222010-08-27 00:11:28 +0000931 Width = Info.first;
Douglas Gregordc572a32009-03-30 22:58:21 +0000932 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000933 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000934
935 case Type::TypeOfExpr:
936 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
937 .getTypePtr());
938
939 case Type::TypeOf:
940 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
941
Anders Carlsson81df7b82009-06-24 19:06:50 +0000942 case Type::Decltype:
943 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
944 .getTypePtr());
945
Abramo Bagnara6150c882010-05-11 21:36:43 +0000946 case Type::Elaborated:
947 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000948
John McCall81904512011-01-06 01:58:22 +0000949 case Type::Attributed:
950 return getTypeInfo(
951 cast<AttributedType>(T)->getEquivalentType().getTypePtr());
952
Douglas Gregoref462e62009-04-30 17:32:17 +0000953 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000954 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000955 "Cannot request the size of a dependent type");
956 // FIXME: this is likely to be wrong once we support template
957 // aliases, since a template alias could refer to a typedef that
958 // has an __aligned__ attribute on it.
959 return getTypeInfo(getCanonicalType(T));
960 }
Mike Stump11289f42009-09-09 15:08:12 +0000961
Chris Lattner53cfe802007-07-18 17:52:12 +0000962 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000963 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000964}
965
Ken Dyckcc56c542011-01-15 18:38:59 +0000966/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
967CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
968 return CharUnits::fromQuantity(BitSize / getCharWidth());
969}
970
Ken Dyckb0fcc592011-02-11 01:54:29 +0000971/// toBits - Convert a size in characters to a size in characters.
972int64_t ASTContext::toBits(CharUnits CharSize) const {
973 return CharSize.getQuantity() * getCharWidth();
974}
975
Ken Dyck8c89d592009-12-22 14:23:30 +0000976/// getTypeSizeInChars - Return the size of the specified type, in characters.
977/// This method does not work on incomplete types.
Jay Foad39c79802011-01-12 09:06:06 +0000978CharUnits ASTContext::getTypeSizeInChars(QualType T) const {
Ken Dyckcc56c542011-01-15 18:38:59 +0000979 return toCharUnitsFromBits(getTypeSize(T));
Ken Dyck8c89d592009-12-22 14:23:30 +0000980}
Jay Foad39c79802011-01-12 09:06:06 +0000981CharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
Ken Dyckcc56c542011-01-15 18:38:59 +0000982 return toCharUnitsFromBits(getTypeSize(T));
Ken Dyck8c89d592009-12-22 14:23:30 +0000983}
984
Ken Dycka6046ab2010-01-26 17:25:18 +0000985/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000986/// characters. This method does not work on incomplete types.
Jay Foad39c79802011-01-12 09:06:06 +0000987CharUnits ASTContext::getTypeAlignInChars(QualType T) const {
Ken Dyckcc56c542011-01-15 18:38:59 +0000988 return toCharUnitsFromBits(getTypeAlign(T));
Ken Dyck24d28d62010-01-26 17:22:55 +0000989}
Jay Foad39c79802011-01-12 09:06:06 +0000990CharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
Ken Dyckcc56c542011-01-15 18:38:59 +0000991 return toCharUnitsFromBits(getTypeAlign(T));
Ken Dyck24d28d62010-01-26 17:22:55 +0000992}
993
Chris Lattnera3402cd2009-01-27 18:08:34 +0000994/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
995/// type for the current target in bits. This can be different than the ABI
996/// alignment in cases where it is beneficial for performance to overalign
997/// a data type.
Jay Foad39c79802011-01-12 09:06:06 +0000998unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
Chris Lattnera3402cd2009-01-27 18:08:34 +0000999 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +00001000
1001 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +00001002 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +00001003 T = CT->getElementType().getTypePtr();
1004 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
1005 T->isSpecificBuiltinType(BuiltinType::LongLong))
1006 return std::max(ABIAlign, (unsigned)getTypeSize(T));
1007
Chris Lattnera3402cd2009-01-27 18:08:34 +00001008 return ABIAlign;
1009}
1010
Fariborz Jahanian7c809592009-06-04 01:19:09 +00001011/// ShallowCollectObjCIvars -
1012/// Collect all ivars, including those synthesized, in the current class.
1013///
1014void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Jay Foad39c79802011-01-12 09:06:06 +00001015 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001016 // FIXME. This need be removed but there are two many places which
1017 // assume const-ness of ObjCInterfaceDecl
1018 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1019 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1020 Iv= Iv->getNextIvar())
1021 Ivars.push_back(Iv);
Fariborz Jahanian7c809592009-06-04 01:19:09 +00001022}
1023
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001024/// DeepCollectObjCIvars -
1025/// This routine first collects all declared, but not synthesized, ivars in
1026/// super class and then collects all ivars, including those synthesized for
1027/// current class. This routine is used for implementation of current class
1028/// when all ivars, declared and synthesized are known.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00001029///
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001030void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
1031 bool leafClass,
Jay Foad39c79802011-01-12 09:06:06 +00001032 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001033 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
1034 DeepCollectObjCIvars(SuperClass, false, Ivars);
1035 if (!leafClass) {
1036 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1037 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanianaef66222010-02-19 00:31:17 +00001038 Ivars.push_back(*I);
1039 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001040 else
1041 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00001042}
1043
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001044/// CollectInheritedProtocols - Collect all protocols in current class and
1045/// those inherited by it.
1046void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00001047 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001048 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001049 // We can use protocol_iterator here instead of
1050 // all_referenced_protocol_iterator since we are walking all categories.
1051 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
1052 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001053 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00001054 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001055 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +00001056 PE = Proto->protocol_end(); P != PE; ++P) {
1057 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001058 CollectInheritedProtocols(*P, Protocols);
1059 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +00001060 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001061
1062 // Categories of this Interface.
1063 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1064 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1065 CollectInheritedProtocols(CDeclChain, Protocols);
1066 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1067 while (SD) {
1068 CollectInheritedProtocols(SD, Protocols);
1069 SD = SD->getSuperClass();
1070 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +00001071 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001072 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001073 PE = OC->protocol_end(); P != PE; ++P) {
1074 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00001075 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001076 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1077 PE = Proto->protocol_end(); P != PE; ++P)
1078 CollectInheritedProtocols(*P, Protocols);
1079 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +00001080 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001081 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1082 PE = OP->protocol_end(); P != PE; ++P) {
1083 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00001084 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001085 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1086 PE = Proto->protocol_end(); P != PE; ++P)
1087 CollectInheritedProtocols(*P, Protocols);
1088 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001089 }
1090}
1091
Jay Foad39c79802011-01-12 09:06:06 +00001092unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +00001093 unsigned count = 0;
1094 // Count ivars declared in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001095 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
1096 CDecl = CDecl->getNextClassExtension())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +00001097 count += CDecl->ivar_size();
1098
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +00001099 // Count ivar defined in this class's implementation. This
1100 // includes synthesized ivars.
1101 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +00001102 count += ImplDecl->ivar_size();
1103
Fariborz Jahanian7c809592009-06-04 01:19:09 +00001104 return count;
1105}
1106
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001107/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
1108ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
1109 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1110 I = ObjCImpls.find(D);
1111 if (I != ObjCImpls.end())
1112 return cast<ObjCImplementationDecl>(I->second);
1113 return 0;
1114}
1115/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1116ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
1117 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1118 I = ObjCImpls.find(D);
1119 if (I != ObjCImpls.end())
1120 return cast<ObjCCategoryImplDecl>(I->second);
1121 return 0;
1122}
1123
1124/// \brief Set the implementation of ObjCInterfaceDecl.
1125void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1126 ObjCImplementationDecl *ImplD) {
1127 assert(IFaceD && ImplD && "Passed null params");
1128 ObjCImpls[IFaceD] = ImplD;
1129}
1130/// \brief Set the implementation of ObjCCategoryDecl.
1131void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1132 ObjCCategoryImplDecl *ImplD) {
1133 assert(CatD && ImplD && "Passed null params");
1134 ObjCImpls[CatD] = ImplD;
1135}
1136
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001137/// \brief Get the copy initialization expression of VarDecl,or NULL if
1138/// none exists.
Fariborz Jahanian50198092010-12-02 17:02:11 +00001139Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001140 assert(VD && "Passed null params");
1141 assert(VD->hasAttr<BlocksAttr>() &&
1142 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian50198092010-12-02 17:02:11 +00001143 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001144 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001145 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1146}
1147
1148/// \brief Set the copy inialization expression of a block var decl.
1149void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1150 assert(VD && Init && "Passed null params");
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001151 assert(VD->hasAttr<BlocksAttr>() &&
1152 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001153 BlockVarCopyInits[VD] = Init;
1154}
1155
John McCallbcd03502009-12-07 02:54:59 +00001156/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001157///
John McCallbcd03502009-12-07 02:54:59 +00001158/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001159/// the TypeLoc wrappers.
1160///
1161/// \param T the type that will be the basis for type source info. This type
1162/// should refer to how the declarator was written in source code, not to
1163/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +00001164TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
Jay Foad39c79802011-01-12 09:06:06 +00001165 unsigned DataSize) const {
John McCall26fe7e02009-10-21 00:23:54 +00001166 if (!DataSize)
1167 DataSize = TypeLoc::getFullDataSizeForType(T);
1168 else
1169 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +00001170 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +00001171
John McCallbcd03502009-12-07 02:54:59 +00001172 TypeSourceInfo *TInfo =
1173 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1174 new (TInfo) TypeSourceInfo(T);
1175 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001176}
1177
John McCallbcd03502009-12-07 02:54:59 +00001178TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001179 SourceLocation L) const {
John McCallbcd03502009-12-07 02:54:59 +00001180 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
Douglas Gregor2d525f02011-01-25 19:13:18 +00001181 DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
John McCall3665e002009-10-23 21:14:09 +00001182 return DI;
1183}
1184
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001185const ASTRecordLayout &
Jay Foad39c79802011-01-12 09:06:06 +00001186ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001187 return getObjCLayout(D, 0);
1188}
1189
1190const ASTRecordLayout &
Jay Foad39c79802011-01-12 09:06:06 +00001191ASTContext::getASTObjCImplementationLayout(
1192 const ObjCImplementationDecl *D) const {
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001193 return getObjCLayout(D->getClassInterface(), D);
1194}
1195
Chris Lattner983a8bb2007-07-13 22:13:22 +00001196//===----------------------------------------------------------------------===//
1197// Type creation/memoization methods
1198//===----------------------------------------------------------------------===//
1199
Jay Foad39c79802011-01-12 09:06:06 +00001200QualType
John McCall33ddac02011-01-19 10:06:00 +00001201ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
1202 unsigned fastQuals = quals.getFastQualifiers();
1203 quals.removeFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00001204
1205 // Check if we've already instantiated this type.
1206 llvm::FoldingSetNodeID ID;
John McCall33ddac02011-01-19 10:06:00 +00001207 ExtQuals::Profile(ID, baseType, quals);
1208 void *insertPos = 0;
1209 if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
1210 assert(eq->getQualifiers() == quals);
1211 return QualType(eq, fastQuals);
John McCall8ccfcb52009-09-24 19:53:00 +00001212 }
1213
John McCall33ddac02011-01-19 10:06:00 +00001214 // If the base type is not canonical, make the appropriate canonical type.
1215 QualType canon;
1216 if (!baseType->isCanonicalUnqualified()) {
1217 SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
1218 canonSplit.second.addConsistentQualifiers(quals);
1219 canon = getExtQualType(canonSplit.first, canonSplit.second);
1220
1221 // Re-find the insert position.
1222 (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
1223 }
1224
1225 ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
1226 ExtQualNodes.InsertNode(eq, insertPos);
1227 return QualType(eq, fastQuals);
John McCall8ccfcb52009-09-24 19:53:00 +00001228}
1229
Jay Foad39c79802011-01-12 09:06:06 +00001230QualType
1231ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001232 QualType CanT = getCanonicalType(T);
1233 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001234 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001235
John McCall8ccfcb52009-09-24 19:53:00 +00001236 // If we are composing extended qualifiers together, merge together
1237 // into one ExtQuals node.
1238 QualifierCollector Quals;
1239 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001240
John McCall8ccfcb52009-09-24 19:53:00 +00001241 // If this type already has an address space specified, it cannot get
1242 // another one.
1243 assert(!Quals.hasAddressSpace() &&
1244 "Type cannot be in multiple addr spaces!");
1245 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001246
John McCall8ccfcb52009-09-24 19:53:00 +00001247 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001248}
1249
Chris Lattnerd60183d2009-02-18 22:53:11 +00001250QualType ASTContext::getObjCGCQualType(QualType T,
Jay Foad39c79802011-01-12 09:06:06 +00001251 Qualifiers::GC GCAttr) const {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001252 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001253 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001254 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001255
John McCall53fa7142010-12-24 02:08:15 +00001256 if (const PointerType *ptr = T->getAs<PointerType>()) {
1257 QualType Pointee = ptr->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001258 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001259 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1260 return getPointerType(ResultType);
1261 }
1262 }
Mike Stump11289f42009-09-09 15:08:12 +00001263
John McCall8ccfcb52009-09-24 19:53:00 +00001264 // If we are composing extended qualifiers together, merge together
1265 // into one ExtQuals node.
1266 QualifierCollector Quals;
1267 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001268
John McCall8ccfcb52009-09-24 19:53:00 +00001269 // If this type already has an ObjCGC specified, it cannot get
1270 // another one.
1271 assert(!Quals.hasObjCGCAttr() &&
1272 "Type cannot have multiple ObjCGCs!");
1273 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001274
John McCall8ccfcb52009-09-24 19:53:00 +00001275 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001276}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001277
John McCall4f5019e2010-12-19 02:44:49 +00001278const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1279 FunctionType::ExtInfo Info) {
1280 if (T->getExtInfo() == Info)
1281 return T;
1282
1283 QualType Result;
1284 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1285 Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1286 } else {
1287 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1288 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1289 EPI.ExtInfo = Info;
1290 Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1291 FPT->getNumArgs(), EPI);
1292 }
1293
1294 return cast<FunctionType>(Result.getTypePtr());
1295}
1296
Chris Lattnerc6395932007-06-22 20:56:16 +00001297/// getComplexType - Return the uniqued reference to the type for a complex
1298/// number with the specified element type.
Jay Foad39c79802011-01-12 09:06:06 +00001299QualType ASTContext::getComplexType(QualType T) const {
Chris Lattnerc6395932007-06-22 20:56:16 +00001300 // Unique pointers, to guarantee there is only one pointer of a particular
1301 // structure.
1302 llvm::FoldingSetNodeID ID;
1303 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001304
Chris Lattnerc6395932007-06-22 20:56:16 +00001305 void *InsertPos = 0;
1306 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1307 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001308
Chris Lattnerc6395932007-06-22 20:56:16 +00001309 // If the pointee type isn't canonical, this won't be a canonical type either,
1310 // so fill in the canonical type field.
1311 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001312 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001313 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001314
Chris Lattnerc6395932007-06-22 20:56:16 +00001315 // Get the new insert position for the node we care about.
1316 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001317 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001318 }
John McCall90d1c2d2009-09-24 23:30:46 +00001319 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001320 Types.push_back(New);
1321 ComplexTypes.InsertNode(New, InsertPos);
1322 return QualType(New, 0);
1323}
1324
Chris Lattner970e54e2006-11-12 00:37:36 +00001325/// getPointerType - Return the uniqued reference to the type for a pointer to
1326/// the specified type.
Jay Foad39c79802011-01-12 09:06:06 +00001327QualType ASTContext::getPointerType(QualType T) const {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001328 // Unique pointers, to guarantee there is only one pointer of a particular
1329 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001330 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001331 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001332
Chris Lattner67521df2007-01-27 01:29:36 +00001333 void *InsertPos = 0;
1334 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001335 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001336
Chris Lattner7ccecb92006-11-12 08:50:50 +00001337 // If the pointee type isn't canonical, this won't be a canonical type either,
1338 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001339 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001340 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001341 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001342
Chris Lattner67521df2007-01-27 01:29:36 +00001343 // Get the new insert position for the node we care about.
1344 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001345 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001346 }
John McCall90d1c2d2009-09-24 23:30:46 +00001347 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001348 Types.push_back(New);
1349 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001350 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001351}
1352
Mike Stump11289f42009-09-09 15:08:12 +00001353/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001354/// a pointer to the specified block.
Jay Foad39c79802011-01-12 09:06:06 +00001355QualType ASTContext::getBlockPointerType(QualType T) const {
Steve Naroff0ac012832008-08-28 19:20:44 +00001356 assert(T->isFunctionType() && "block of function types only");
1357 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001358 // structure.
1359 llvm::FoldingSetNodeID ID;
1360 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001361
Steve Naroffec33ed92008-08-27 16:04:49 +00001362 void *InsertPos = 0;
1363 if (BlockPointerType *PT =
1364 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1365 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001366
1367 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001368 // type either so fill in the canonical type field.
1369 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001370 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001371 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001372
Steve Naroffec33ed92008-08-27 16:04:49 +00001373 // Get the new insert position for the node we care about.
1374 BlockPointerType *NewIP =
1375 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001376 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001377 }
John McCall90d1c2d2009-09-24 23:30:46 +00001378 BlockPointerType *New
1379 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001380 Types.push_back(New);
1381 BlockPointerTypes.InsertNode(New, InsertPos);
1382 return QualType(New, 0);
1383}
1384
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001385/// getLValueReferenceType - Return the uniqued reference to the type for an
1386/// lvalue reference to the specified type.
Jay Foad39c79802011-01-12 09:06:06 +00001387QualType
1388ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
Bill Wendling3708c182007-05-27 10:15:43 +00001389 // Unique pointers, to guarantee there is only one pointer of a particular
1390 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001391 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001392 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001393
1394 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001395 if (LValueReferenceType *RT =
1396 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001397 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001398
John McCallfc93cf92009-10-22 22:37:11 +00001399 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1400
Bill Wendling3708c182007-05-27 10:15:43 +00001401 // If the referencee type isn't canonical, this won't be a canonical type
1402 // either, so fill in the canonical type field.
1403 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001404 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1405 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1406 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001407
Bill Wendling3708c182007-05-27 10:15:43 +00001408 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001409 LValueReferenceType *NewIP =
1410 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001411 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001412 }
1413
John McCall90d1c2d2009-09-24 23:30:46 +00001414 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001415 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1416 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001417 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001418 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001419
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001420 return QualType(New, 0);
1421}
1422
1423/// getRValueReferenceType - Return the uniqued reference to the type for an
1424/// rvalue reference to the specified type.
Jay Foad39c79802011-01-12 09:06:06 +00001425QualType ASTContext::getRValueReferenceType(QualType T) const {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001426 // Unique pointers, to guarantee there is only one pointer of a particular
1427 // structure.
1428 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001429 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001430
1431 void *InsertPos = 0;
1432 if (RValueReferenceType *RT =
1433 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1434 return QualType(RT, 0);
1435
John McCallfc93cf92009-10-22 22:37:11 +00001436 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1437
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001438 // If the referencee type isn't canonical, this won't be a canonical type
1439 // either, so fill in the canonical type field.
1440 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001441 if (InnerRef || !T.isCanonical()) {
1442 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1443 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001444
1445 // Get the new insert position for the node we care about.
1446 RValueReferenceType *NewIP =
1447 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001448 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001449 }
1450
John McCall90d1c2d2009-09-24 23:30:46 +00001451 RValueReferenceType *New
1452 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001453 Types.push_back(New);
1454 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001455 return QualType(New, 0);
1456}
1457
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001458/// getMemberPointerType - Return the uniqued reference to the type for a
1459/// member pointer to the specified type, in the specified class.
Jay Foad39c79802011-01-12 09:06:06 +00001460QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001461 // Unique pointers, to guarantee there is only one pointer of a particular
1462 // structure.
1463 llvm::FoldingSetNodeID ID;
1464 MemberPointerType::Profile(ID, T, Cls);
1465
1466 void *InsertPos = 0;
1467 if (MemberPointerType *PT =
1468 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1469 return QualType(PT, 0);
1470
1471 // If the pointee or class type isn't canonical, this won't be a canonical
1472 // type either, so fill in the canonical type field.
1473 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001474 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001475 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1476
1477 // Get the new insert position for the node we care about.
1478 MemberPointerType *NewIP =
1479 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001480 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001481 }
John McCall90d1c2d2009-09-24 23:30:46 +00001482 MemberPointerType *New
1483 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001484 Types.push_back(New);
1485 MemberPointerTypes.InsertNode(New, InsertPos);
1486 return QualType(New, 0);
1487}
1488
Mike Stump11289f42009-09-09 15:08:12 +00001489/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001490/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001491QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001492 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001493 ArrayType::ArraySizeModifier ASM,
Abramo Bagnara92141d22011-01-27 19:55:10 +00001494 unsigned IndexTypeQuals) const {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001495 assert((EltTy->isDependentType() ||
1496 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001497 "Constant array of VLAs is illegal!");
1498
Chris Lattnere2df3f92009-05-13 04:12:56 +00001499 // Convert the array size into a canonical width matching the pointer size for
1500 // the target.
1501 llvm::APInt ArySize(ArySizeIn);
Jay Foad6d4db0c2010-12-07 08:25:34 +00001502 ArySize =
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001503 ArySize.zextOrTrunc(Target.getPointerWidth(getTargetAddressSpace(EltTy)));
Mike Stump11289f42009-09-09 15:08:12 +00001504
Chris Lattner23b7eb62007-06-15 23:05:46 +00001505 llvm::FoldingSetNodeID ID;
Abramo Bagnara92141d22011-01-27 19:55:10 +00001506 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001507
Chris Lattner36f8e652007-01-27 08:31:04 +00001508 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001509 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001510 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001511 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001512
John McCall33ddac02011-01-19 10:06:00 +00001513 // If the element type isn't canonical or has qualifiers, this won't
1514 // be a canonical type either, so fill in the canonical type field.
1515 QualType Canon;
1516 if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
1517 SplitQualType canonSplit = getCanonicalType(EltTy).split();
1518 Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize,
Abramo Bagnara92141d22011-01-27 19:55:10 +00001519 ASM, IndexTypeQuals);
John McCall33ddac02011-01-19 10:06:00 +00001520 Canon = getQualifiedType(Canon, canonSplit.second);
1521
Chris Lattner36f8e652007-01-27 08:31:04 +00001522 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001523 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001524 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001525 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001526 }
Mike Stump11289f42009-09-09 15:08:12 +00001527
John McCall90d1c2d2009-09-24 23:30:46 +00001528 ConstantArrayType *New = new(*this,TypeAlignment)
Abramo Bagnara92141d22011-01-27 19:55:10 +00001529 ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001530 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001531 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001532 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001533}
1534
John McCall06549462011-01-18 08:40:38 +00001535/// getVariableArrayDecayedType - Turns the given type, which may be
1536/// variably-modified, into the corresponding type with all the known
1537/// sizes replaced with [*].
1538QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1539 // Vastly most common case.
1540 if (!type->isVariablyModifiedType()) return type;
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001541
John McCall06549462011-01-18 08:40:38 +00001542 QualType result;
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001543
John McCall06549462011-01-18 08:40:38 +00001544 SplitQualType split = type.getSplitDesugaredType();
1545 const Type *ty = split.first;
1546 switch (ty->getTypeClass()) {
1547#define TYPE(Class, Base)
1548#define ABSTRACT_TYPE(Class, Base)
1549#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1550#include "clang/AST/TypeNodes.def"
1551 llvm_unreachable("didn't desugar past all non-canonical types?");
1552
1553 // These types should never be variably-modified.
1554 case Type::Builtin:
1555 case Type::Complex:
1556 case Type::Vector:
1557 case Type::ExtVector:
1558 case Type::DependentSizedExtVector:
1559 case Type::ObjCObject:
1560 case Type::ObjCInterface:
1561 case Type::ObjCObjectPointer:
1562 case Type::Record:
1563 case Type::Enum:
1564 case Type::UnresolvedUsing:
1565 case Type::TypeOfExpr:
1566 case Type::TypeOf:
1567 case Type::Decltype:
1568 case Type::DependentName:
1569 case Type::InjectedClassName:
1570 case Type::TemplateSpecialization:
1571 case Type::DependentTemplateSpecialization:
1572 case Type::TemplateTypeParm:
1573 case Type::SubstTemplateTypeParmPack:
Richard Smith30482bc2011-02-20 03:19:35 +00001574 case Type::Auto:
John McCall06549462011-01-18 08:40:38 +00001575 case Type::PackExpansion:
1576 llvm_unreachable("type should never be variably-modified");
1577
1578 // These types can be variably-modified but should never need to
1579 // further decay.
1580 case Type::FunctionNoProto:
1581 case Type::FunctionProto:
1582 case Type::BlockPointer:
1583 case Type::MemberPointer:
1584 return type;
1585
1586 // These types can be variably-modified. All these modifications
1587 // preserve structure except as noted by comments.
1588 // TODO: if we ever care about optimizing VLAs, there are no-op
1589 // optimizations available here.
1590 case Type::Pointer:
1591 result = getPointerType(getVariableArrayDecayedType(
1592 cast<PointerType>(ty)->getPointeeType()));
1593 break;
1594
1595 case Type::LValueReference: {
1596 const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
1597 result = getLValueReferenceType(
1598 getVariableArrayDecayedType(lv->getPointeeType()),
1599 lv->isSpelledAsLValue());
1600 break;
1601 }
1602
1603 case Type::RValueReference: {
1604 const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
1605 result = getRValueReferenceType(
1606 getVariableArrayDecayedType(lv->getPointeeType()));
1607 break;
1608 }
1609
1610 case Type::ConstantArray: {
1611 const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
1612 result = getConstantArrayType(
1613 getVariableArrayDecayedType(cat->getElementType()),
1614 cat->getSize(),
1615 cat->getSizeModifier(),
1616 cat->getIndexTypeCVRQualifiers());
1617 break;
1618 }
1619
1620 case Type::DependentSizedArray: {
1621 const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
1622 result = getDependentSizedArrayType(
1623 getVariableArrayDecayedType(dat->getElementType()),
1624 dat->getSizeExpr(),
1625 dat->getSizeModifier(),
1626 dat->getIndexTypeCVRQualifiers(),
1627 dat->getBracketsRange());
1628 break;
1629 }
1630
1631 // Turn incomplete types into [*] types.
1632 case Type::IncompleteArray: {
1633 const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
1634 result = getVariableArrayType(
1635 getVariableArrayDecayedType(iat->getElementType()),
1636 /*size*/ 0,
1637 ArrayType::Normal,
1638 iat->getIndexTypeCVRQualifiers(),
1639 SourceRange());
1640 break;
1641 }
1642
1643 // Turn VLA types into [*] types.
1644 case Type::VariableArray: {
1645 const VariableArrayType *vat = cast<VariableArrayType>(ty);
1646 result = getVariableArrayType(
1647 getVariableArrayDecayedType(vat->getElementType()),
1648 /*size*/ 0,
1649 ArrayType::Star,
1650 vat->getIndexTypeCVRQualifiers(),
1651 vat->getBracketsRange());
1652 break;
1653 }
1654 }
1655
1656 // Apply the top-level qualifiers from the original.
1657 return getQualifiedType(result, split.second);
1658}
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001659
Steve Naroffcadebd02007-08-30 18:14:25 +00001660/// getVariableArrayType - Returns a non-unique reference to the type for a
1661/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001662QualType ASTContext::getVariableArrayType(QualType EltTy,
1663 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001664 ArrayType::ArraySizeModifier ASM,
Abramo Bagnara92141d22011-01-27 19:55:10 +00001665 unsigned IndexTypeQuals,
Jay Foad39c79802011-01-12 09:06:06 +00001666 SourceRange Brackets) const {
Eli Friedmanbd258282008-02-15 18:16:39 +00001667 // Since we don't unique expressions, it isn't possible to unique VLA's
1668 // that have an expression provided for their size.
John McCall33ddac02011-01-19 10:06:00 +00001669 QualType Canon;
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001670
John McCall33ddac02011-01-19 10:06:00 +00001671 // Be sure to pull qualifiers off the element type.
1672 if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
1673 SplitQualType canonSplit = getCanonicalType(EltTy).split();
1674 Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM,
Abramo Bagnara92141d22011-01-27 19:55:10 +00001675 IndexTypeQuals, Brackets);
John McCall33ddac02011-01-19 10:06:00 +00001676 Canon = getQualifiedType(Canon, canonSplit.second);
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001677 }
1678
John McCall90d1c2d2009-09-24 23:30:46 +00001679 VariableArrayType *New = new(*this, TypeAlignment)
Abramo Bagnara92141d22011-01-27 19:55:10 +00001680 VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001681
1682 VariableArrayTypes.push_back(New);
1683 Types.push_back(New);
1684 return QualType(New, 0);
1685}
1686
Douglas Gregor4619e432008-12-05 23:32:09 +00001687/// getDependentSizedArrayType - Returns a non-unique reference to
1688/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001689/// type.
John McCall33ddac02011-01-19 10:06:00 +00001690QualType ASTContext::getDependentSizedArrayType(QualType elementType,
1691 Expr *numElements,
Douglas Gregor4619e432008-12-05 23:32:09 +00001692 ArrayType::ArraySizeModifier ASM,
John McCall33ddac02011-01-19 10:06:00 +00001693 unsigned elementTypeQuals,
1694 SourceRange brackets) const {
1695 assert((!numElements || numElements->isTypeDependent() ||
1696 numElements->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001697 "Size must be type- or value-dependent!");
1698
John McCall33ddac02011-01-19 10:06:00 +00001699 // Dependently-sized array types that do not have a specified number
1700 // of elements will have their sizes deduced from a dependent
1701 // initializer. We do no canonicalization here at all, which is okay
1702 // because they can't be used in most locations.
1703 if (!numElements) {
1704 DependentSizedArrayType *newType
1705 = new (*this, TypeAlignment)
1706 DependentSizedArrayType(*this, elementType, QualType(),
1707 numElements, ASM, elementTypeQuals,
1708 brackets);
1709 Types.push_back(newType);
1710 return QualType(newType, 0);
1711 }
1712
1713 // Otherwise, we actually build a new type every time, but we
1714 // also build a canonical type.
1715
1716 SplitQualType canonElementType = getCanonicalType(elementType).split();
1717
1718 void *insertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001719 llvm::FoldingSetNodeID ID;
John McCall33ddac02011-01-19 10:06:00 +00001720 DependentSizedArrayType::Profile(ID, *this,
1721 QualType(canonElementType.first, 0),
1722 ASM, elementTypeQuals, numElements);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001723
John McCall33ddac02011-01-19 10:06:00 +00001724 // Look for an existing type with these properties.
1725 DependentSizedArrayType *canonTy =
1726 DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001727
John McCall33ddac02011-01-19 10:06:00 +00001728 // If we don't have one, build one.
1729 if (!canonTy) {
1730 canonTy = new (*this, TypeAlignment)
1731 DependentSizedArrayType(*this, QualType(canonElementType.first, 0),
1732 QualType(), numElements, ASM, elementTypeQuals,
1733 brackets);
1734 DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
1735 Types.push_back(canonTy);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001736 }
1737
John McCall33ddac02011-01-19 10:06:00 +00001738 // Apply qualifiers from the element type to the array.
1739 QualType canon = getQualifiedType(QualType(canonTy,0),
1740 canonElementType.second);
Mike Stump11289f42009-09-09 15:08:12 +00001741
John McCall33ddac02011-01-19 10:06:00 +00001742 // If we didn't need extra canonicalization for the element type,
1743 // then just use that as our result.
1744 if (QualType(canonElementType.first, 0) == elementType)
1745 return canon;
1746
1747 // Otherwise, we need to build a type which follows the spelling
1748 // of the element type.
1749 DependentSizedArrayType *sugaredType
1750 = new (*this, TypeAlignment)
1751 DependentSizedArrayType(*this, elementType, canon, numElements,
1752 ASM, elementTypeQuals, brackets);
1753 Types.push_back(sugaredType);
1754 return QualType(sugaredType, 0);
Douglas Gregor4619e432008-12-05 23:32:09 +00001755}
1756
John McCall33ddac02011-01-19 10:06:00 +00001757QualType ASTContext::getIncompleteArrayType(QualType elementType,
Eli Friedmanbd258282008-02-15 18:16:39 +00001758 ArrayType::ArraySizeModifier ASM,
John McCall33ddac02011-01-19 10:06:00 +00001759 unsigned elementTypeQuals) const {
Eli Friedmanbd258282008-02-15 18:16:39 +00001760 llvm::FoldingSetNodeID ID;
John McCall33ddac02011-01-19 10:06:00 +00001761 IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001762
John McCall33ddac02011-01-19 10:06:00 +00001763 void *insertPos = 0;
1764 if (IncompleteArrayType *iat =
1765 IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
1766 return QualType(iat, 0);
Eli Friedmanbd258282008-02-15 18:16:39 +00001767
1768 // If the element type isn't canonical, this won't be a canonical type
John McCall33ddac02011-01-19 10:06:00 +00001769 // either, so fill in the canonical type field. We also have to pull
1770 // qualifiers off the element type.
1771 QualType canon;
Eli Friedmanbd258282008-02-15 18:16:39 +00001772
John McCall33ddac02011-01-19 10:06:00 +00001773 if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
1774 SplitQualType canonSplit = getCanonicalType(elementType).split();
1775 canon = getIncompleteArrayType(QualType(canonSplit.first, 0),
1776 ASM, elementTypeQuals);
1777 canon = getQualifiedType(canon, canonSplit.second);
Eli Friedmanbd258282008-02-15 18:16:39 +00001778
1779 // Get the new insert position for the node we care about.
John McCall33ddac02011-01-19 10:06:00 +00001780 IncompleteArrayType *existing =
1781 IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
1782 assert(!existing && "Shouldn't be in the map!"); (void) existing;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001783 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001784
John McCall33ddac02011-01-19 10:06:00 +00001785 IncompleteArrayType *newType = new (*this, TypeAlignment)
1786 IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001787
John McCall33ddac02011-01-19 10:06:00 +00001788 IncompleteArrayTypes.InsertNode(newType, insertPos);
1789 Types.push_back(newType);
1790 return QualType(newType, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001791}
1792
Steve Naroff91fcddb2007-07-18 18:00:27 +00001793/// getVectorType - Return the unique reference to a vector type of
1794/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001795QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Jay Foad39c79802011-01-12 09:06:06 +00001796 VectorType::VectorKind VecKind) const {
John McCall33ddac02011-01-19 10:06:00 +00001797 assert(vecType->isBuiltinType());
Mike Stump11289f42009-09-09 15:08:12 +00001798
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001799 // Check if we've already instantiated a vector of this type.
1800 llvm::FoldingSetNodeID ID;
Bob Wilsonaeb56442010-11-10 21:56:12 +00001801 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner37141f42010-06-23 06:00:24 +00001802
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001803 void *InsertPos = 0;
1804 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1805 return QualType(VTP, 0);
1806
1807 // If the element type isn't canonical, this won't be a canonical type either,
1808 // so fill in the canonical type field.
1809 QualType Canonical;
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00001810 if (!vecType.isCanonical()) {
Bob Wilson77954802010-11-16 00:32:20 +00001811 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump11289f42009-09-09 15:08:12 +00001812
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001813 // Get the new insert position for the node we care about.
1814 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001815 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001816 }
John McCall90d1c2d2009-09-24 23:30:46 +00001817 VectorType *New = new (*this, TypeAlignment)
Bob Wilsonaeb56442010-11-10 21:56:12 +00001818 VectorType(vecType, NumElts, Canonical, VecKind);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001819 VectorTypes.InsertNode(New, InsertPos);
1820 Types.push_back(New);
1821 return QualType(New, 0);
1822}
1823
Nate Begemance4d7fc2008-04-18 23:10:10 +00001824/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001825/// the specified element type and size. VectorType must be a built-in type.
Jay Foad39c79802011-01-12 09:06:06 +00001826QualType
1827ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
John McCall33ddac02011-01-19 10:06:00 +00001828 assert(vecType->isBuiltinType());
Mike Stump11289f42009-09-09 15:08:12 +00001829
Steve Naroff91fcddb2007-07-18 18:00:27 +00001830 // Check if we've already instantiated a vector of this type.
1831 llvm::FoldingSetNodeID ID;
Chris Lattner37141f42010-06-23 06:00:24 +00001832 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001833 VectorType::GenericVector);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001834 void *InsertPos = 0;
1835 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1836 return QualType(VTP, 0);
1837
1838 // If the element type isn't canonical, this won't be a canonical type either,
1839 // so fill in the canonical type field.
1840 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001841 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001842 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001843
Steve Naroff91fcddb2007-07-18 18:00:27 +00001844 // Get the new insert position for the node we care about.
1845 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001846 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001847 }
John McCall90d1c2d2009-09-24 23:30:46 +00001848 ExtVectorType *New = new (*this, TypeAlignment)
1849 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001850 VectorTypes.InsertNode(New, InsertPos);
1851 Types.push_back(New);
1852 return QualType(New, 0);
1853}
1854
Jay Foad39c79802011-01-12 09:06:06 +00001855QualType
1856ASTContext::getDependentSizedExtVectorType(QualType vecType,
1857 Expr *SizeExpr,
1858 SourceLocation AttrLoc) const {
Douglas Gregor352169a2009-07-31 03:54:25 +00001859 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001860 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001861 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001862
Douglas Gregor352169a2009-07-31 03:54:25 +00001863 void *InsertPos = 0;
1864 DependentSizedExtVectorType *Canon
1865 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1866 DependentSizedExtVectorType *New;
1867 if (Canon) {
1868 // We already have a canonical version of this array type; use it as
1869 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001870 New = new (*this, TypeAlignment)
1871 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1872 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001873 } else {
1874 QualType CanonVecTy = getCanonicalType(vecType);
1875 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001876 New = new (*this, TypeAlignment)
1877 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1878 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001879
1880 DependentSizedExtVectorType *CanonCheck
1881 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1882 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1883 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001884 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1885 } else {
1886 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1887 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001888 New = new (*this, TypeAlignment)
1889 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001890 }
1891 }
Mike Stump11289f42009-09-09 15:08:12 +00001892
Douglas Gregor758a8692009-06-17 21:51:59 +00001893 Types.push_back(New);
1894 return QualType(New, 0);
1895}
1896
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001897/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001898///
Jay Foad39c79802011-01-12 09:06:06 +00001899QualType
1900ASTContext::getFunctionNoProtoType(QualType ResultTy,
1901 const FunctionType::ExtInfo &Info) const {
Roman Divacky65b88cd2011-03-01 17:40:53 +00001902 const CallingConv DefaultCC = Info.getCC();
1903 const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
1904 CC_X86StdCall : DefaultCC;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001905 // Unique functions, to guarantee there is only one function of a particular
1906 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001907 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001908 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001909
Chris Lattner47955de2007-01-27 08:37:20 +00001910 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001911 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001912 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001913 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001914
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001915 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001916 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001917 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001918 Canonical =
1919 getFunctionNoProtoType(getCanonicalType(ResultTy),
1920 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001921
Chris Lattner47955de2007-01-27 08:37:20 +00001922 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001923 FunctionNoProtoType *NewIP =
1924 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001925 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001926 }
Mike Stump11289f42009-09-09 15:08:12 +00001927
Roman Divacky65b88cd2011-03-01 17:40:53 +00001928 FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
John McCall90d1c2d2009-09-24 23:30:46 +00001929 FunctionNoProtoType *New = new (*this, TypeAlignment)
Roman Divacky65b88cd2011-03-01 17:40:53 +00001930 FunctionNoProtoType(ResultTy, Canonical, newInfo);
Chris Lattner47955de2007-01-27 08:37:20 +00001931 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001932 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001933 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001934}
1935
1936/// getFunctionType - Return a normal function type with a typed argument
1937/// list. isVariadic indicates whether the argument list includes '...'.
Jay Foad39c79802011-01-12 09:06:06 +00001938QualType
1939ASTContext::getFunctionType(QualType ResultTy,
1940 const QualType *ArgArray, unsigned NumArgs,
1941 const FunctionProtoType::ExtProtoInfo &EPI) const {
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001942 // Unique functions, to guarantee there is only one function of a particular
1943 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001944 llvm::FoldingSetNodeID ID;
Sebastian Redl31ad7542011-03-13 17:09:40 +00001945 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001946
1947 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001948 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001949 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001950 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001951
1952 // Determine whether the type being created is already canonical or not.
Sebastian Redl7c6c9e92011-03-06 10:52:04 +00001953 bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001954 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001955 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001956 isCanonical = false;
1957
Roman Divacky65b88cd2011-03-01 17:40:53 +00001958 const CallingConv DefaultCC = EPI.ExtInfo.getCC();
1959 const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
1960 CC_X86StdCall : DefaultCC;
John McCalldb40c7f2010-12-14 08:05:40 +00001961
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001962 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001963 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001964 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001965 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001966 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001967 CanonicalArgs.reserve(NumArgs);
1968 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001969 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001970
John McCalldb40c7f2010-12-14 08:05:40 +00001971 FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
Sebastian Redl7c6c9e92011-03-06 10:52:04 +00001972 CanonicalEPI.ExceptionSpecType = EST_None;
1973 CanonicalEPI.NumExceptions = 0;
John McCalldb40c7f2010-12-14 08:05:40 +00001974 CanonicalEPI.ExtInfo
1975 = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
1976
Chris Lattner76a00cf2008-04-06 22:59:24 +00001977 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001978 CanonicalArgs.data(), NumArgs,
John McCalldb40c7f2010-12-14 08:05:40 +00001979 CanonicalEPI);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001980
Chris Lattnerfd4de792007-01-27 01:15:32 +00001981 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001982 FunctionProtoType *NewIP =
1983 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001984 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001985 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001986
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001987 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001988 // for two variable size arrays (for parameter and exception types) at the
Sebastian Redlfa453cf2011-03-12 11:50:43 +00001989 // end of them. Instead of the exception types, there could be a noexcept
1990 // expression and a context pointer.
John McCalldb40c7f2010-12-14 08:05:40 +00001991 size_t Size = sizeof(FunctionProtoType) +
Sebastian Redlfa453cf2011-03-12 11:50:43 +00001992 NumArgs * sizeof(QualType);
1993 if (EPI.ExceptionSpecType == EST_Dynamic)
1994 Size += EPI.NumExceptions * sizeof(QualType);
1995 else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001996 Size += sizeof(Expr*);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00001997 }
John McCalldb40c7f2010-12-14 08:05:40 +00001998 FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001999 FunctionProtoType::ExtProtoInfo newEPI = EPI;
2000 newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
Sebastian Redl31ad7542011-03-13 17:09:40 +00002001 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00002002 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002003 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00002004 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00002005}
Chris Lattneref51c202006-11-10 07:17:23 +00002006
John McCalle78aac42010-03-10 03:28:59 +00002007#ifndef NDEBUG
2008static bool NeedsInjectedClassNameType(const RecordDecl *D) {
2009 if (!isa<CXXRecordDecl>(D)) return false;
2010 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
2011 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
2012 return true;
2013 if (RD->getDescribedClassTemplate() &&
2014 !isa<ClassTemplateSpecializationDecl>(RD))
2015 return true;
2016 return false;
2017}
2018#endif
2019
2020/// getInjectedClassNameType - Return the unique reference to the
2021/// injected class name type for the specified templated declaration.
2022QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
Jay Foad39c79802011-01-12 09:06:06 +00002023 QualType TST) const {
John McCalle78aac42010-03-10 03:28:59 +00002024 assert(NeedsInjectedClassNameType(Decl));
2025 if (Decl->TypeForDecl) {
2026 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00002027 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCalle78aac42010-03-10 03:28:59 +00002028 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
2029 Decl->TypeForDecl = PrevDecl->TypeForDecl;
2030 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2031 } else {
John McCall424cec92011-01-19 06:33:43 +00002032 Type *newType =
John McCall2408e322010-04-27 00:57:59 +00002033 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCall424cec92011-01-19 06:33:43 +00002034 Decl->TypeForDecl = newType;
2035 Types.push_back(newType);
John McCalle78aac42010-03-10 03:28:59 +00002036 }
2037 return QualType(Decl->TypeForDecl, 0);
2038}
2039
Douglas Gregor83a586e2008-04-13 21:07:44 +00002040/// getTypeDeclType - Return the unique reference to the type for the
2041/// specified type declaration.
Jay Foad39c79802011-01-12 09:06:06 +00002042QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00002043 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00002044 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00002045
Richard Smithdda56e42011-04-15 14:24:37 +00002046 if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00002047 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00002048
John McCall96f0b5f2010-03-10 06:48:02 +00002049 assert(!isa<TemplateTypeParmDecl>(Decl) &&
2050 "Template type parameter types are always available.");
2051
John McCall81e38502010-02-16 03:57:14 +00002052 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00002053 assert(!Record->getPreviousDeclaration() &&
2054 "struct/union has previous declaration");
2055 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002056 return getRecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00002057 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00002058 assert(!Enum->getPreviousDeclaration() &&
2059 "enum has previous declaration");
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002060 return getEnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00002061 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00002062 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
John McCall424cec92011-01-19 06:33:43 +00002063 Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2064 Decl->TypeForDecl = newType;
2065 Types.push_back(newType);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00002066 } else
John McCall96f0b5f2010-03-10 06:48:02 +00002067 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00002068
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00002069 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00002070}
2071
Chris Lattner32d920b2007-01-26 02:01:53 +00002072/// getTypedefType - Return the unique reference to the type for the
Richard Smithdda56e42011-04-15 14:24:37 +00002073/// specified typedef name decl.
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002074QualType
Richard Smithdda56e42011-04-15 14:24:37 +00002075ASTContext::getTypedefType(const TypedefNameDecl *Decl,
2076 QualType Canonical) const {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00002077 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002078
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002079 if (Canonical.isNull())
2080 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall424cec92011-01-19 06:33:43 +00002081 TypedefType *newType = new(*this, TypeAlignment)
John McCall90d1c2d2009-09-24 23:30:46 +00002082 TypedefType(Type::Typedef, Decl, Canonical);
John McCall424cec92011-01-19 06:33:43 +00002083 Decl->TypeForDecl = newType;
2084 Types.push_back(newType);
2085 return QualType(newType, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00002086}
2087
Jay Foad39c79802011-01-12 09:06:06 +00002088QualType ASTContext::getRecordType(const RecordDecl *Decl) const {
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002089 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2090
2091 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
2092 if (PrevDecl->TypeForDecl)
2093 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2094
John McCall424cec92011-01-19 06:33:43 +00002095 RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2096 Decl->TypeForDecl = newType;
2097 Types.push_back(newType);
2098 return QualType(newType, 0);
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002099}
2100
Jay Foad39c79802011-01-12 09:06:06 +00002101QualType ASTContext::getEnumType(const EnumDecl *Decl) const {
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002102 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2103
2104 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
2105 if (PrevDecl->TypeForDecl)
2106 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2107
John McCall424cec92011-01-19 06:33:43 +00002108 EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2109 Decl->TypeForDecl = newType;
2110 Types.push_back(newType);
2111 return QualType(newType, 0);
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002112}
2113
John McCall81904512011-01-06 01:58:22 +00002114QualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
2115 QualType modifiedType,
2116 QualType equivalentType) {
2117 llvm::FoldingSetNodeID id;
2118 AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
2119
2120 void *insertPos = 0;
2121 AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
2122 if (type) return QualType(type, 0);
2123
2124 QualType canon = getCanonicalType(equivalentType);
2125 type = new (*this, TypeAlignment)
2126 AttributedType(canon, attrKind, modifiedType, equivalentType);
2127
2128 Types.push_back(type);
2129 AttributedTypes.InsertNode(type, insertPos);
2130
2131 return QualType(type, 0);
2132}
2133
2134
John McCallcebee162009-10-18 09:09:24 +00002135/// \brief Retrieve a substitution-result type.
2136QualType
2137ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
Jay Foad39c79802011-01-12 09:06:06 +00002138 QualType Replacement) const {
John McCallb692a092009-10-22 20:10:53 +00002139 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00002140 && "replacement types must always be canonical");
2141
2142 llvm::FoldingSetNodeID ID;
2143 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
2144 void *InsertPos = 0;
2145 SubstTemplateTypeParmType *SubstParm
2146 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2147
2148 if (!SubstParm) {
2149 SubstParm = new (*this, TypeAlignment)
2150 SubstTemplateTypeParmType(Parm, Replacement);
2151 Types.push_back(SubstParm);
2152 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2153 }
2154
2155 return QualType(SubstParm, 0);
2156}
2157
Douglas Gregorada4b792011-01-14 02:55:32 +00002158/// \brief Retrieve a
2159QualType ASTContext::getSubstTemplateTypeParmPackType(
2160 const TemplateTypeParmType *Parm,
2161 const TemplateArgument &ArgPack) {
2162#ifndef NDEBUG
2163 for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2164 PEnd = ArgPack.pack_end();
2165 P != PEnd; ++P) {
2166 assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2167 assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2168 }
2169#endif
2170
2171 llvm::FoldingSetNodeID ID;
2172 SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2173 void *InsertPos = 0;
2174 if (SubstTemplateTypeParmPackType *SubstParm
2175 = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2176 return QualType(SubstParm, 0);
2177
2178 QualType Canon;
2179 if (!Parm->isCanonicalUnqualified()) {
2180 Canon = getCanonicalType(QualType(Parm, 0));
2181 Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2182 ArgPack);
2183 SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2184 }
2185
2186 SubstTemplateTypeParmPackType *SubstParm
2187 = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2188 ArgPack);
2189 Types.push_back(SubstParm);
2190 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2191 return QualType(SubstParm, 0);
2192}
2193
Douglas Gregoreff93e02009-02-05 23:33:38 +00002194/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00002195/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00002196/// name.
Mike Stump11289f42009-09-09 15:08:12 +00002197QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00002198 bool ParameterPack,
Jay Foad39c79802011-01-12 09:06:06 +00002199 IdentifierInfo *Name) const {
Douglas Gregoreff93e02009-02-05 23:33:38 +00002200 llvm::FoldingSetNodeID ID;
Douglas Gregor2ebcae12010-06-16 15:23:05 +00002201 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00002202 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002203 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00002204 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2205
2206 if (TypeParm)
2207 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002208
Douglas Gregor2ebcae12010-06-16 15:23:05 +00002209 if (Name) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00002210 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregor2ebcae12010-06-16 15:23:05 +00002211 TypeParm = new (*this, TypeAlignment)
2212 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002213
2214 TemplateTypeParmType *TypeCheck
2215 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2216 assert(!TypeCheck && "Template type parameter canonical type broken");
2217 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00002218 } else
John McCall90d1c2d2009-09-24 23:30:46 +00002219 TypeParm = new (*this, TypeAlignment)
2220 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00002221
2222 Types.push_back(TypeParm);
2223 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2224
2225 return QualType(TypeParm, 0);
2226}
2227
John McCalle78aac42010-03-10 03:28:59 +00002228TypeSourceInfo *
2229ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
2230 SourceLocation NameLoc,
2231 const TemplateArgumentListInfo &Args,
Jay Foad39c79802011-01-12 09:06:06 +00002232 QualType CanonType) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002233 assert(!Name.getAsDependentTemplateName() &&
2234 "No dependent template names here!");
John McCalle78aac42010-03-10 03:28:59 +00002235 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
2236
2237 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
2238 TemplateSpecializationTypeLoc TL
2239 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
2240 TL.setTemplateNameLoc(NameLoc);
2241 TL.setLAngleLoc(Args.getLAngleLoc());
2242 TL.setRAngleLoc(Args.getRAngleLoc());
2243 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2244 TL.setArgLocInfo(i, Args[i].getLocInfo());
2245 return DI;
2246}
2247
Mike Stump11289f42009-09-09 15:08:12 +00002248QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00002249ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00002250 const TemplateArgumentListInfo &Args,
Jay Foad39c79802011-01-12 09:06:06 +00002251 QualType Canon) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002252 assert(!Template.getAsDependentTemplateName() &&
2253 "No dependent template names here!");
2254
John McCall6b51f282009-11-23 01:53:49 +00002255 unsigned NumArgs = Args.size();
2256
John McCall0ad16662009-10-29 08:12:44 +00002257 llvm::SmallVector<TemplateArgument, 4> ArgVec;
2258 ArgVec.reserve(NumArgs);
2259 for (unsigned i = 0; i != NumArgs; ++i)
2260 ArgVec.push_back(Args[i].getArgument());
2261
John McCall2408e322010-04-27 00:57:59 +00002262 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00002263 Canon);
John McCall0ad16662009-10-29 08:12:44 +00002264}
2265
2266QualType
2267ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00002268 const TemplateArgument *Args,
2269 unsigned NumArgs,
Jay Foad39c79802011-01-12 09:06:06 +00002270 QualType Canon) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002271 assert(!Template.getAsDependentTemplateName() &&
2272 "No dependent template names here!");
Douglas Gregore29139c2011-03-03 17:04:51 +00002273 // Look through qualified template names.
2274 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2275 Template = TemplateName(QTN->getTemplateDecl());
Douglas Gregore16af532011-02-28 18:50:33 +00002276
Douglas Gregor15301382009-07-30 17:40:51 +00002277 if (!Canon.isNull())
2278 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002279 else
2280 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregord56a91e2009-02-26 22:19:44 +00002281
Douglas Gregora8e02e72009-07-28 23:00:59 +00002282 // Allocate the (non-canonical) template specialization type, but don't
2283 // try to unique it: these types typically have location information that
2284 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00002285 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00002286 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00002287 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00002288 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00002289 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00002290 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00002291 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00002292
Douglas Gregor8bf42052009-02-09 18:46:07 +00002293 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00002294 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002295}
2296
Mike Stump11289f42009-09-09 15:08:12 +00002297QualType
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002298ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2299 const TemplateArgument *Args,
Jay Foad39c79802011-01-12 09:06:06 +00002300 unsigned NumArgs) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002301 assert(!Template.getAsDependentTemplateName() &&
2302 "No dependent template names here!");
Douglas Gregore29139c2011-03-03 17:04:51 +00002303 // Look through qualified template names.
2304 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2305 Template = TemplateName(QTN->getTemplateDecl());
Douglas Gregore16af532011-02-28 18:50:33 +00002306
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002307 // Build the canonical template specialization type.
2308 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2309 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2310 CanonArgs.reserve(NumArgs);
2311 for (unsigned I = 0; I != NumArgs; ++I)
2312 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2313
2314 // Determine whether this canonical template specialization type already
2315 // exists.
2316 llvm::FoldingSetNodeID ID;
2317 TemplateSpecializationType::Profile(ID, CanonTemplate,
2318 CanonArgs.data(), NumArgs, *this);
2319
2320 void *InsertPos = 0;
2321 TemplateSpecializationType *Spec
2322 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2323
2324 if (!Spec) {
2325 // Allocate a new canonical template specialization type.
2326 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2327 sizeof(TemplateArgument) * NumArgs),
2328 TypeAlignment);
2329 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2330 CanonArgs.data(), NumArgs,
2331 QualType());
2332 Types.push_back(Spec);
2333 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2334 }
2335
2336 assert(Spec->isDependentType() &&
2337 "Non-dependent template-id type must have a canonical type");
2338 return QualType(Spec, 0);
2339}
2340
2341QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00002342ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2343 NestedNameSpecifier *NNS,
Jay Foad39c79802011-01-12 09:06:06 +00002344 QualType NamedType) const {
Douglas Gregor52537682009-03-19 00:18:19 +00002345 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002346 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00002347
2348 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002349 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002350 if (T)
2351 return QualType(T, 0);
2352
Douglas Gregorc42075a2010-02-04 18:10:26 +00002353 QualType Canon = NamedType;
2354 if (!Canon.isCanonical()) {
2355 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002356 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2357 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00002358 (void)CheckT;
2359 }
2360
Abramo Bagnara6150c882010-05-11 21:36:43 +00002361 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00002362 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002363 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002364 return QualType(T, 0);
2365}
2366
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002367QualType
Jay Foad39c79802011-01-12 09:06:06 +00002368ASTContext::getParenType(QualType InnerType) const {
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002369 llvm::FoldingSetNodeID ID;
2370 ParenType::Profile(ID, InnerType);
2371
2372 void *InsertPos = 0;
2373 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2374 if (T)
2375 return QualType(T, 0);
2376
2377 QualType Canon = InnerType;
2378 if (!Canon.isCanonical()) {
2379 Canon = getCanonicalType(InnerType);
2380 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2381 assert(!CheckT && "Paren canonical type broken");
2382 (void)CheckT;
2383 }
2384
2385 T = new (*this) ParenType(InnerType, Canon);
2386 Types.push_back(T);
2387 ParenTypes.InsertNode(T, InsertPos);
2388 return QualType(T, 0);
2389}
2390
Douglas Gregor02085352010-03-31 20:19:30 +00002391QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2392 NestedNameSpecifier *NNS,
2393 const IdentifierInfo *Name,
Jay Foad39c79802011-01-12 09:06:06 +00002394 QualType Canon) const {
Douglas Gregor333489b2009-03-27 23:10:48 +00002395 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2396
2397 if (Canon.isNull()) {
2398 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00002399 ElaboratedTypeKeyword CanonKeyword = Keyword;
2400 if (Keyword == ETK_None)
2401 CanonKeyword = ETK_Typename;
2402
2403 if (CanonNNS != NNS || CanonKeyword != Keyword)
2404 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002405 }
2406
2407 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002408 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002409
2410 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002411 DependentNameType *T
2412 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00002413 if (T)
2414 return QualType(T, 0);
2415
Douglas Gregor02085352010-03-31 20:19:30 +00002416 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002417 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002418 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002419 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002420}
2421
Mike Stump11289f42009-09-09 15:08:12 +00002422QualType
John McCallc392f372010-06-11 00:33:02 +00002423ASTContext::getDependentTemplateSpecializationType(
2424 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002425 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002426 const IdentifierInfo *Name,
Jay Foad39c79802011-01-12 09:06:06 +00002427 const TemplateArgumentListInfo &Args) const {
John McCallc392f372010-06-11 00:33:02 +00002428 // TODO: avoid this copy
2429 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2430 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2431 ArgCopy.push_back(Args[I].getArgument());
2432 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2433 ArgCopy.size(),
2434 ArgCopy.data());
2435}
2436
2437QualType
2438ASTContext::getDependentTemplateSpecializationType(
2439 ElaboratedTypeKeyword Keyword,
2440 NestedNameSpecifier *NNS,
2441 const IdentifierInfo *Name,
2442 unsigned NumArgs,
Jay Foad39c79802011-01-12 09:06:06 +00002443 const TemplateArgument *Args) const {
Douglas Gregor6e068012011-02-28 00:04:36 +00002444 assert((!NNS || NNS->isDependent()) &&
2445 "nested-name-specifier must be dependent");
Douglas Gregordce2b622009-04-01 00:28:59 +00002446
Douglas Gregorc42075a2010-02-04 18:10:26 +00002447 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002448 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2449 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002450
2451 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002452 DependentTemplateSpecializationType *T
2453 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002454 if (T)
2455 return QualType(T, 0);
2456
John McCallc392f372010-06-11 00:33:02 +00002457 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002458
John McCallc392f372010-06-11 00:33:02 +00002459 ElaboratedTypeKeyword CanonKeyword = Keyword;
2460 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2461
2462 bool AnyNonCanonArgs = false;
2463 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2464 for (unsigned I = 0; I != NumArgs; ++I) {
2465 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2466 if (!CanonArgs[I].structurallyEquals(Args[I]))
2467 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002468 }
2469
John McCallc392f372010-06-11 00:33:02 +00002470 QualType Canon;
2471 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2472 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2473 Name, NumArgs,
2474 CanonArgs.data());
2475
2476 // Find the insert position again.
2477 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2478 }
2479
2480 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2481 sizeof(TemplateArgument) * NumArgs),
2482 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002483 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002484 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002485 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002486 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002487 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002488}
2489
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002490QualType ASTContext::getPackExpansionType(QualType Pattern,
2491 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00002492 llvm::FoldingSetNodeID ID;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002493 PackExpansionType::Profile(ID, Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002494
2495 assert(Pattern->containsUnexpandedParameterPack() &&
2496 "Pack expansions must expand one or more parameter packs");
2497 void *InsertPos = 0;
2498 PackExpansionType *T
2499 = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2500 if (T)
2501 return QualType(T, 0);
2502
2503 QualType Canon;
2504 if (!Pattern.isCanonical()) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002505 Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002506
2507 // Find the insert position again.
2508 PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2509 }
2510
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002511 T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002512 Types.push_back(T);
2513 PackExpansionTypes.InsertNode(T, InsertPos);
2514 return QualType(T, 0);
2515}
2516
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002517/// CmpProtocolNames - Comparison predicate for sorting protocols
2518/// alphabetically.
2519static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2520 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002521 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002522}
2523
John McCall8b07ec22010-05-15 11:32:37 +00002524static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002525 unsigned NumProtocols) {
2526 if (NumProtocols == 0) return true;
2527
2528 for (unsigned i = 1; i != NumProtocols; ++i)
2529 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2530 return false;
2531 return true;
2532}
2533
2534static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002535 unsigned &NumProtocols) {
2536 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002537
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002538 // Sort protocols, keyed by name.
2539 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2540
2541 // Remove duplicates.
2542 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2543 NumProtocols = ProtocolsEnd-Protocols;
2544}
2545
John McCall8b07ec22010-05-15 11:32:37 +00002546QualType ASTContext::getObjCObjectType(QualType BaseType,
2547 ObjCProtocolDecl * const *Protocols,
Jay Foad39c79802011-01-12 09:06:06 +00002548 unsigned NumProtocols) const {
John McCall8b07ec22010-05-15 11:32:37 +00002549 // If the base type is an interface and there aren't any protocols
2550 // to add, then the interface type will do just fine.
2551 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2552 return BaseType;
2553
2554 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002555 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002556 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002557 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002558 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2559 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002560
John McCall8b07ec22010-05-15 11:32:37 +00002561 // Build the canonical type, which has the canonical base type and
2562 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002563 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002564 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2565 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2566 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002567 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2568 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002569 unsigned UniqueCount = NumProtocols;
2570
John McCallfc93cf92009-10-22 22:37:11 +00002571 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002572 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2573 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002574 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002575 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2576 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002577 }
2578
2579 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002580 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2581 }
2582
2583 unsigned Size = sizeof(ObjCObjectTypeImpl);
2584 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2585 void *Mem = Allocate(Size, TypeAlignment);
2586 ObjCObjectTypeImpl *T =
2587 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2588
2589 Types.push_back(T);
2590 ObjCObjectTypes.InsertNode(T, InsertPos);
2591 return QualType(T, 0);
2592}
2593
2594/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2595/// the given object type.
Jay Foad39c79802011-01-12 09:06:06 +00002596QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
John McCall8b07ec22010-05-15 11:32:37 +00002597 llvm::FoldingSetNodeID ID;
2598 ObjCObjectPointerType::Profile(ID, ObjectT);
2599
2600 void *InsertPos = 0;
2601 if (ObjCObjectPointerType *QT =
2602 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2603 return QualType(QT, 0);
2604
2605 // Find the canonical object type.
2606 QualType Canonical;
2607 if (!ObjectT.isCanonical()) {
2608 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2609
2610 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002611 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2612 }
2613
Douglas Gregorf85bee62010-02-08 22:59:26 +00002614 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002615 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2616 ObjCObjectPointerType *QType =
2617 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002618
Steve Narofffb4330f2009-06-17 22:40:22 +00002619 Types.push_back(QType);
2620 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002621 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002622}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002623
Douglas Gregor1c283312010-08-11 12:19:30 +00002624/// getObjCInterfaceType - Return the unique reference to the type for the
2625/// specified ObjC interface decl. The list of protocols is optional.
Jay Foad39c79802011-01-12 09:06:06 +00002626QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const {
Douglas Gregor1c283312010-08-11 12:19:30 +00002627 if (Decl->TypeForDecl)
2628 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002629
Douglas Gregor1c283312010-08-11 12:19:30 +00002630 // FIXME: redeclarations?
2631 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2632 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2633 Decl->TypeForDecl = T;
2634 Types.push_back(T);
2635 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002636}
2637
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002638/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2639/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002640/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002641/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002642/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002643QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
Douglas Gregorabd68132009-07-08 00:03:05 +00002644 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002645 if (tofExpr->isTypeDependent()) {
2646 llvm::FoldingSetNodeID ID;
2647 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002648
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002649 void *InsertPos = 0;
2650 DependentTypeOfExprType *Canon
2651 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2652 if (Canon) {
2653 // We already have a "canonical" version of an identical, dependent
2654 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002655 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002656 QualType((TypeOfExprType*)Canon, 0));
2657 }
2658 else {
2659 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002660 Canon
2661 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002662 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2663 toe = Canon;
2664 }
2665 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002666 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002667 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002668 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002669 Types.push_back(toe);
2670 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002671}
2672
Steve Naroffa773cd52007-08-01 18:02:17 +00002673/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2674/// TypeOfType AST's. The only motivation to unique these nodes would be
2675/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002676/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002677/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002678QualType ASTContext::getTypeOfType(QualType tofType) const {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002679 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002680 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002681 Types.push_back(tot);
2682 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002683}
2684
Anders Carlssonad6bd352009-06-24 21:24:56 +00002685/// getDecltypeForExpr - Given an expr, will return the decltype for that
2686/// expression, according to the rules in C++0x [dcl.type.simple]p4
Jay Foad39c79802011-01-12 09:06:06 +00002687static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002688 if (e->isTypeDependent())
2689 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002690
Anders Carlssonad6bd352009-06-24 21:24:56 +00002691 // If e is an id expression or a class member access, decltype(e) is defined
2692 // as the type of the entity named by e.
2693 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2694 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2695 return VD->getType();
2696 }
2697 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2698 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2699 return FD->getType();
2700 }
2701 // If e is a function call or an invocation of an overloaded operator,
2702 // (parentheses around e are ignored), decltype(e) is defined as the
2703 // return type of that function.
2704 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2705 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002706
Anders Carlssonad6bd352009-06-24 21:24:56 +00002707 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002708
2709 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002710 // defined as T&, otherwise decltype(e) is defined as T.
John McCall086a4642010-11-24 05:12:34 +00002711 if (e->isLValue())
Anders Carlssonad6bd352009-06-24 21:24:56 +00002712 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002713
Anders Carlssonad6bd352009-06-24 21:24:56 +00002714 return T;
2715}
2716
Anders Carlsson81df7b82009-06-24 19:06:50 +00002717/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2718/// DecltypeType AST's. The only motivation to unique these nodes would be
2719/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002720/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002721/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002722QualType ASTContext::getDecltypeType(Expr *e) const {
Douglas Gregorabd68132009-07-08 00:03:05 +00002723 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002724 if (e->isTypeDependent()) {
2725 llvm::FoldingSetNodeID ID;
2726 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002727
Douglas Gregora21f6c32009-07-30 23:36:40 +00002728 void *InsertPos = 0;
2729 DependentDecltypeType *Canon
2730 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2731 if (Canon) {
2732 // We already have a "canonical" version of an equivalent, dependent
2733 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002734 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002735 QualType((DecltypeType*)Canon, 0));
2736 }
2737 else {
2738 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002739 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002740 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2741 dt = Canon;
2742 }
2743 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002744 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002745 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002746 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002747 Types.push_back(dt);
2748 return QualType(dt, 0);
2749}
2750
Richard Smithb2bc2e62011-02-21 20:05:19 +00002751/// getAutoType - We only unique auto types after they've been deduced.
Richard Smith30482bc2011-02-20 03:19:35 +00002752QualType ASTContext::getAutoType(QualType DeducedType) const {
Richard Smithb2bc2e62011-02-21 20:05:19 +00002753 void *InsertPos = 0;
2754 if (!DeducedType.isNull()) {
2755 // Look in the folding set for an existing type.
2756 llvm::FoldingSetNodeID ID;
2757 AutoType::Profile(ID, DeducedType);
2758 if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
2759 return QualType(AT, 0);
2760 }
2761
2762 AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
2763 Types.push_back(AT);
2764 if (InsertPos)
2765 AutoTypes.InsertNode(AT, InsertPos);
2766 return QualType(AT, 0);
Richard Smith30482bc2011-02-20 03:19:35 +00002767}
2768
Richard Smith02e85f32011-04-14 22:09:26 +00002769/// getAutoDeductType - Get type pattern for deducing against 'auto'.
2770QualType ASTContext::getAutoDeductType() const {
2771 if (AutoDeductTy.isNull())
2772 AutoDeductTy = getAutoType(QualType());
2773 assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
2774 return AutoDeductTy;
2775}
2776
2777/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
2778QualType ASTContext::getAutoRRefDeductType() const {
2779 if (AutoRRefDeductTy.isNull())
2780 AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
2781 assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
2782 return AutoRRefDeductTy;
2783}
2784
Chris Lattnerfb072462007-01-23 05:45:31 +00002785/// getTagDeclType - Return the unique reference to the type for the
2786/// specified TagDecl (struct/union/class/enum) decl.
Jay Foad39c79802011-01-12 09:06:06 +00002787QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002788 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002789 // FIXME: What is the design on getTagDeclType when it requires casting
2790 // away const? mutable?
2791 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002792}
2793
Mike Stump11289f42009-09-09 15:08:12 +00002794/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2795/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2796/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002797CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002798 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002799}
Chris Lattnerfb072462007-01-23 05:45:31 +00002800
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002801/// getSignedWCharType - Return the type of "signed wchar_t".
2802/// Used when in C++, as a GCC extension.
2803QualType ASTContext::getSignedWCharType() const {
2804 // FIXME: derive from "Target" ?
2805 return WCharTy;
2806}
2807
2808/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2809/// Used when in C++, as a GCC extension.
2810QualType ASTContext::getUnsignedWCharType() const {
2811 // FIXME: derive from "Target" ?
2812 return UnsignedIntTy;
2813}
2814
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002815/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2816/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2817QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002818 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002819}
2820
Chris Lattnera21ad802008-04-02 05:18:44 +00002821//===----------------------------------------------------------------------===//
2822// Type Operators
2823//===----------------------------------------------------------------------===//
2824
Jay Foad39c79802011-01-12 09:06:06 +00002825CanQualType ASTContext::getCanonicalParamType(QualType T) const {
John McCallfc93cf92009-10-22 22:37:11 +00002826 // Push qualifiers into arrays, and then discard any remaining
2827 // qualifiers.
2828 T = getCanonicalType(T);
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00002829 T = getVariableArrayDecayedType(T);
John McCallfc93cf92009-10-22 22:37:11 +00002830 const Type *Ty = T.getTypePtr();
John McCallfc93cf92009-10-22 22:37:11 +00002831 QualType Result;
2832 if (isa<ArrayType>(Ty)) {
2833 Result = getArrayDecayedType(QualType(Ty,0));
2834 } else if (isa<FunctionType>(Ty)) {
2835 Result = getPointerType(QualType(Ty, 0));
2836 } else {
2837 Result = QualType(Ty, 0);
2838 }
2839
2840 return CanQualType::CreateUnsafe(Result);
2841}
2842
Chris Lattner7adf0762008-08-04 07:31:14 +00002843
John McCall6c9dd522011-01-18 07:41:22 +00002844QualType ASTContext::getUnqualifiedArrayType(QualType type,
2845 Qualifiers &quals) {
2846 SplitQualType splitType = type.getSplitUnqualifiedType();
2847
2848 // FIXME: getSplitUnqualifiedType() actually walks all the way to
2849 // the unqualified desugared type and then drops it on the floor.
2850 // We then have to strip that sugar back off with
2851 // getUnqualifiedDesugaredType(), which is silly.
2852 const ArrayType *AT =
2853 dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType());
2854
2855 // If we don't have an array, just use the results in splitType.
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002856 if (!AT) {
John McCall6c9dd522011-01-18 07:41:22 +00002857 quals = splitType.second;
2858 return QualType(splitType.first, 0);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002859 }
2860
John McCall6c9dd522011-01-18 07:41:22 +00002861 // Otherwise, recurse on the array's element type.
2862 QualType elementType = AT->getElementType();
2863 QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
2864
2865 // If that didn't change the element type, AT has no qualifiers, so we
2866 // can just use the results in splitType.
2867 if (elementType == unqualElementType) {
2868 assert(quals.empty()); // from the recursive call
2869 quals = splitType.second;
2870 return QualType(splitType.first, 0);
2871 }
2872
2873 // Otherwise, add in the qualifiers from the outermost type, then
2874 // build the type back up.
2875 quals.addConsistentQualifiers(splitType.second);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002876
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002877 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002878 return getConstantArrayType(unqualElementType, CAT->getSize(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002879 CAT->getSizeModifier(), 0);
2880 }
2881
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002882 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002883 return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002884 }
2885
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002886 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002887 return getVariableArrayType(unqualElementType,
John McCallc3007a22010-10-26 07:05:15 +00002888 VAT->getSizeExpr(),
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002889 VAT->getSizeModifier(),
2890 VAT->getIndexTypeCVRQualifiers(),
2891 VAT->getBracketsRange());
2892 }
2893
2894 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCall6c9dd522011-01-18 07:41:22 +00002895 return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002896 DSAT->getSizeModifier(), 0,
2897 SourceRange());
2898}
2899
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002900/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2901/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2902/// they point to and return true. If T1 and T2 aren't pointer types
2903/// or pointer-to-member types, or if they are not similar at this
2904/// level, returns false and leaves T1 and T2 unchanged. Top-level
2905/// qualifiers on T1 and T2 are ignored. This function will typically
2906/// be called in a loop that successively "unwraps" pointer and
2907/// pointer-to-member types to compare them at each level.
2908bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2909 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2910 *T2PtrType = T2->getAs<PointerType>();
2911 if (T1PtrType && T2PtrType) {
2912 T1 = T1PtrType->getPointeeType();
2913 T2 = T2PtrType->getPointeeType();
2914 return true;
2915 }
2916
2917 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2918 *T2MPType = T2->getAs<MemberPointerType>();
2919 if (T1MPType && T2MPType &&
2920 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2921 QualType(T2MPType->getClass(), 0))) {
2922 T1 = T1MPType->getPointeeType();
2923 T2 = T2MPType->getPointeeType();
2924 return true;
2925 }
2926
2927 if (getLangOptions().ObjC1) {
2928 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2929 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2930 if (T1OPType && T2OPType) {
2931 T1 = T1OPType->getPointeeType();
2932 T2 = T2OPType->getPointeeType();
2933 return true;
2934 }
2935 }
2936
2937 // FIXME: Block pointers, too?
2938
2939 return false;
2940}
2941
Jay Foad39c79802011-01-12 09:06:06 +00002942DeclarationNameInfo
2943ASTContext::getNameForTemplate(TemplateName Name,
2944 SourceLocation NameLoc) const {
John McCall847e2a12009-11-24 18:42:40 +00002945 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002946 // DNInfo work in progress: CHECKME: what about DNLoc?
2947 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2948
John McCall847e2a12009-11-24 18:42:40 +00002949 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002950 DeclarationName DName;
John McCall847e2a12009-11-24 18:42:40 +00002951 if (DTN->isIdentifier()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002952 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2953 return DeclarationNameInfo(DName, NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002954 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002955 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2956 // DNInfo work in progress: FIXME: source locations?
2957 DeclarationNameLoc DNLoc;
2958 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2959 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2960 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall847e2a12009-11-24 18:42:40 +00002961 }
2962 }
2963
John McCalld28ae272009-12-02 08:04:21 +00002964 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2965 assert(Storage);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002966 // DNInfo work in progress: CHECKME: what about DNLoc?
2967 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002968}
2969
Jay Foad39c79802011-01-12 09:06:06 +00002970TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002971 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2972 if (TemplateTemplateParmDecl *TTP
2973 = dyn_cast<TemplateTemplateParmDecl>(Template))
2974 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2975
2976 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002977 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002978 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002979
Douglas Gregor5590be02011-01-15 06:45:20 +00002980 if (SubstTemplateTemplateParmPackStorage *SubstPack
2981 = Name.getAsSubstTemplateTemplateParmPack()) {
2982 TemplateTemplateParmDecl *CanonParam
2983 = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack());
2984 TemplateArgument CanonArgPack
2985 = getCanonicalTemplateArgument(SubstPack->getArgumentPack());
2986 return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack);
2987 }
2988
John McCalld28ae272009-12-02 08:04:21 +00002989 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002990
Douglas Gregor6bc50582009-05-07 06:41:52 +00002991 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2992 assert(DTN && "Non-dependent template names must refer to template decls.");
2993 return DTN->CanonicalTemplateName;
2994}
2995
Douglas Gregoradee3e32009-11-11 23:06:43 +00002996bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2997 X = getCanonicalTemplateName(X);
2998 Y = getCanonicalTemplateName(Y);
2999 return X.getAsVoidPointer() == Y.getAsVoidPointer();
3000}
3001
Mike Stump11289f42009-09-09 15:08:12 +00003002TemplateArgument
Jay Foad39c79802011-01-12 09:06:06 +00003003ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
Douglas Gregora8e02e72009-07-28 23:00:59 +00003004 switch (Arg.getKind()) {
3005 case TemplateArgument::Null:
3006 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00003007
Douglas Gregora8e02e72009-07-28 23:00:59 +00003008 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00003009 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00003010
Douglas Gregora8e02e72009-07-28 23:00:59 +00003011 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00003012 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00003013
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003014 case TemplateArgument::Template:
3015 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003016
3017 case TemplateArgument::TemplateExpansion:
3018 return TemplateArgument(getCanonicalTemplateName(
3019 Arg.getAsTemplateOrTemplatePattern()),
Douglas Gregore1d60df2011-01-14 23:41:42 +00003020 Arg.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003021
Douglas Gregora8e02e72009-07-28 23:00:59 +00003022 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00003023 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00003024 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00003025
Douglas Gregora8e02e72009-07-28 23:00:59 +00003026 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00003027 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00003028
Douglas Gregora8e02e72009-07-28 23:00:59 +00003029 case TemplateArgument::Pack: {
Douglas Gregor0192c232010-12-20 16:52:59 +00003030 if (Arg.pack_size() == 0)
3031 return Arg;
3032
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003033 TemplateArgument *CanonArgs
3034 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregora8e02e72009-07-28 23:00:59 +00003035 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003036 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00003037 AEnd = Arg.pack_end();
3038 A != AEnd; (void)++A, ++Idx)
3039 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00003040
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003041 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregora8e02e72009-07-28 23:00:59 +00003042 }
3043 }
3044
3045 // Silence GCC warning
3046 assert(false && "Unhandled template argument kind");
3047 return TemplateArgument();
3048}
3049
Douglas Gregor333489b2009-03-27 23:10:48 +00003050NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +00003051ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
Mike Stump11289f42009-09-09 15:08:12 +00003052 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00003053 return 0;
3054
3055 switch (NNS->getKind()) {
3056 case NestedNameSpecifier::Identifier:
3057 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00003058 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00003059 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3060 NNS->getAsIdentifier());
3061
3062 case NestedNameSpecifier::Namespace:
3063 // A namespace is canonical; build a nested-name-specifier with
3064 // this namespace and no prefix.
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003065 return NestedNameSpecifier::Create(*this, 0,
3066 NNS->getAsNamespace()->getOriginalNamespace());
3067
3068 case NestedNameSpecifier::NamespaceAlias:
3069 // A namespace is canonical; build a nested-name-specifier with
3070 // this namespace and no prefix.
3071 return NestedNameSpecifier::Create(*this, 0,
3072 NNS->getAsNamespaceAlias()->getNamespace()
3073 ->getOriginalNamespace());
Douglas Gregor333489b2009-03-27 23:10:48 +00003074
3075 case NestedNameSpecifier::TypeSpec:
3076 case NestedNameSpecifier::TypeSpecWithTemplate: {
3077 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor3ade5702010-11-04 00:09:33 +00003078
3079 // If we have some kind of dependent-named type (e.g., "typename T::type"),
3080 // break it apart into its prefix and identifier, then reconsititute those
3081 // as the canonical nested-name-specifier. This is required to canonicalize
3082 // a dependent nested-name-specifier involving typedefs of dependent-name
3083 // types, e.g.,
3084 // typedef typename T::type T1;
3085 // typedef typename T1::type T2;
3086 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
3087 NestedNameSpecifier *Prefix
3088 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
3089 return NestedNameSpecifier::Create(*this, Prefix,
3090 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3091 }
3092
Douglas Gregorcc10cbf2010-11-04 00:14:23 +00003093 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor3ade5702010-11-04 00:09:33 +00003094 if (const DependentTemplateSpecializationType *DTST
3095 = T->getAs<DependentTemplateSpecializationType>()) {
3096 NestedNameSpecifier *Prefix
3097 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
Douglas Gregor6e068012011-02-28 00:04:36 +00003098
3099 T = getDependentTemplateSpecializationType(DTST->getKeyword(),
3100 Prefix, DTST->getIdentifier(),
3101 DTST->getNumArgs(),
3102 DTST->getArgs());
Douglas Gregor3ade5702010-11-04 00:09:33 +00003103 T = getCanonicalType(T);
3104 }
3105
John McCall33ddac02011-01-19 10:06:00 +00003106 return NestedNameSpecifier::Create(*this, 0, false,
3107 const_cast<Type*>(T.getTypePtr()));
Douglas Gregor333489b2009-03-27 23:10:48 +00003108 }
3109
3110 case NestedNameSpecifier::Global:
3111 // The global specifier is canonical and unique.
3112 return NNS;
3113 }
3114
3115 // Required to silence a GCC warning
3116 return 0;
3117}
3118
Chris Lattner7adf0762008-08-04 07:31:14 +00003119
Jay Foad39c79802011-01-12 09:06:06 +00003120const ArrayType *ASTContext::getAsArrayType(QualType T) const {
Chris Lattner7adf0762008-08-04 07:31:14 +00003121 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00003122 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00003123 // Handle the common positive case fast.
3124 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3125 return AT;
3126 }
Mike Stump11289f42009-09-09 15:08:12 +00003127
John McCall8ccfcb52009-09-24 19:53:00 +00003128 // Handle the common negative case fast.
John McCall33ddac02011-01-19 10:06:00 +00003129 if (!isa<ArrayType>(T.getCanonicalType()))
Chris Lattner7adf0762008-08-04 07:31:14 +00003130 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003131
John McCall8ccfcb52009-09-24 19:53:00 +00003132 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00003133 // implements C99 6.7.3p8: "If the specification of an array type includes
3134 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00003135
Chris Lattner7adf0762008-08-04 07:31:14 +00003136 // If we get here, we either have type qualifiers on the type, or we have
3137 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00003138 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00003139
John McCall33ddac02011-01-19 10:06:00 +00003140 SplitQualType split = T.getSplitDesugaredType();
3141 Qualifiers qs = split.second;
Mike Stump11289f42009-09-09 15:08:12 +00003142
Chris Lattner7adf0762008-08-04 07:31:14 +00003143 // If we have a simple case, just return now.
John McCall33ddac02011-01-19 10:06:00 +00003144 const ArrayType *ATy = dyn_cast<ArrayType>(split.first);
3145 if (ATy == 0 || qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00003146 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00003147
Chris Lattner7adf0762008-08-04 07:31:14 +00003148 // Otherwise, we have an array and we have qualifiers on it. Push the
3149 // qualifiers into the array element type and return a new array type.
John McCall33ddac02011-01-19 10:06:00 +00003150 QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
Mike Stump11289f42009-09-09 15:08:12 +00003151
Chris Lattner7adf0762008-08-04 07:31:14 +00003152 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3153 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3154 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003155 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00003156 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3157 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3158 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003159 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00003160
Mike Stump11289f42009-09-09 15:08:12 +00003161 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00003162 = dyn_cast<DependentSizedArrayType>(ATy))
3163 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00003164 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00003165 DSAT->getSizeExpr(),
Douglas Gregor4619e432008-12-05 23:32:09 +00003166 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003167 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00003168 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00003169
Chris Lattner7adf0762008-08-04 07:31:14 +00003170 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00003171 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00003172 VAT->getSizeExpr(),
Chris Lattner7adf0762008-08-04 07:31:14 +00003173 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003174 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00003175 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00003176}
3177
Chris Lattnera21ad802008-04-02 05:18:44 +00003178/// getArrayDecayedType - Return the properly qualified result of decaying the
3179/// specified array type to a pointer. This operation is non-trivial when
3180/// handling typedefs etc. The canonical type of "T" must be an array type,
3181/// this returns a pointer to a properly qualified element of the array.
3182///
3183/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
Jay Foad39c79802011-01-12 09:06:06 +00003184QualType ASTContext::getArrayDecayedType(QualType Ty) const {
Chris Lattner7adf0762008-08-04 07:31:14 +00003185 // Get the element type with 'getAsArrayType' so that we don't lose any
3186 // typedefs in the element type of the array. This also handles propagation
3187 // of type qualifiers from the array type into the element type if present
3188 // (C99 6.7.3p8).
3189 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3190 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00003191
Chris Lattner7adf0762008-08-04 07:31:14 +00003192 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00003193
3194 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00003195 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00003196}
3197
John McCall33ddac02011-01-19 10:06:00 +00003198QualType ASTContext::getBaseElementType(const ArrayType *array) const {
3199 return getBaseElementType(array->getElementType());
Douglas Gregor79f83ed2009-07-23 23:49:00 +00003200}
3201
John McCall33ddac02011-01-19 10:06:00 +00003202QualType ASTContext::getBaseElementType(QualType type) const {
3203 Qualifiers qs;
3204 while (true) {
3205 SplitQualType split = type.getSplitDesugaredType();
3206 const ArrayType *array = split.first->getAsArrayTypeUnsafe();
3207 if (!array) break;
Mike Stump11289f42009-09-09 15:08:12 +00003208
John McCall33ddac02011-01-19 10:06:00 +00003209 type = array->getElementType();
3210 qs.addConsistentQualifiers(split.second);
3211 }
Mike Stump11289f42009-09-09 15:08:12 +00003212
John McCall33ddac02011-01-19 10:06:00 +00003213 return getQualifiedType(type, qs);
Anders Carlssone0808df2008-12-21 03:44:36 +00003214}
3215
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00003216/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00003217uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00003218ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
3219 uint64_t ElementCount = 1;
3220 do {
3221 ElementCount *= CA->getSize().getZExtValue();
3222 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
3223 } while (CA);
3224 return ElementCount;
3225}
3226
Steve Naroff0af91202007-04-27 21:51:21 +00003227/// getFloatingRank - Return a relative rank for floating point types.
3228/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00003229static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00003230 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00003231 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00003232
John McCall9dd450b2009-09-21 23:43:11 +00003233 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3234 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00003235 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00003236 case BuiltinType::Float: return FloatRank;
3237 case BuiltinType::Double: return DoubleRank;
3238 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00003239 }
3240}
3241
Mike Stump11289f42009-09-09 15:08:12 +00003242/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
3243/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00003244/// 'typeDomain' is a real floating point or complex type.
3245/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00003246QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
3247 QualType Domain) const {
3248 FloatingRank EltRank = getFloatingRank(Size);
3249 if (Domain->isComplexType()) {
3250 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00003251 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00003252 case FloatRank: return FloatComplexTy;
3253 case DoubleRank: return DoubleComplexTy;
3254 case LongDoubleRank: return LongDoubleComplexTy;
3255 }
Steve Naroff0af91202007-04-27 21:51:21 +00003256 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00003257
3258 assert(Domain->isRealFloatingType() && "Unknown domain!");
3259 switch (EltRank) {
3260 default: assert(0 && "getFloatingRank(): illegal value for rank");
3261 case FloatRank: return FloatTy;
3262 case DoubleRank: return DoubleTy;
3263 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00003264 }
Steve Naroffe4718892007-04-27 18:30:00 +00003265}
3266
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003267/// getFloatingTypeOrder - Compare the rank of the two specified floating
3268/// point types, ignoring the domain of the type (i.e. 'double' ==
3269/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003270/// LHS < RHS, return -1.
Jay Foad39c79802011-01-12 09:06:06 +00003271int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
Chris Lattnerb90739d2008-04-06 23:38:49 +00003272 FloatingRank LHSR = getFloatingRank(LHS);
3273 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00003274
Chris Lattnerb90739d2008-04-06 23:38:49 +00003275 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00003276 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00003277 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00003278 return 1;
3279 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00003280}
3281
Chris Lattner76a00cf2008-04-06 22:59:24 +00003282/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3283/// routine will assert if passed a built-in type that isn't an integer or enum,
3284/// or if it is not canonicalized.
John McCall424cec92011-01-19 06:33:43 +00003285unsigned ASTContext::getIntegerRank(const Type *T) const {
John McCallb692a092009-10-22 20:10:53 +00003286 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
John McCall424cec92011-01-19 06:33:43 +00003287 if (const EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00003288 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00003289
Chris Lattnerad3467e2010-12-25 23:25:43 +00003290 if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
3291 T->isSpecificBuiltinType(BuiltinType::WChar_U))
Eli Friedmanc131d3b2009-07-05 23:44:27 +00003292 T = getFromTargetType(Target.getWCharType()).getTypePtr();
3293
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003294 if (T->isSpecificBuiltinType(BuiltinType::Char16))
3295 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
3296
3297 if (T->isSpecificBuiltinType(BuiltinType::Char32))
3298 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
3299
Chris Lattner76a00cf2008-04-06 22:59:24 +00003300 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003301 default: assert(0 && "getIntegerRank(): not a built-in integer");
3302 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003303 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003304 case BuiltinType::Char_S:
3305 case BuiltinType::Char_U:
3306 case BuiltinType::SChar:
3307 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003308 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003309 case BuiltinType::Short:
3310 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003311 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003312 case BuiltinType::Int:
3313 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003314 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003315 case BuiltinType::Long:
3316 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003317 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003318 case BuiltinType::LongLong:
3319 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003320 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00003321 case BuiltinType::Int128:
3322 case BuiltinType::UInt128:
3323 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00003324 }
3325}
3326
Eli Friedman629ffb92009-08-20 04:21:42 +00003327/// \brief Whether this is a promotable bitfield reference according
3328/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
3329///
3330/// \returns the type this bit-field will promote to, or NULL if no
3331/// promotion occurs.
Jay Foad39c79802011-01-12 09:06:06 +00003332QualType ASTContext::isPromotableBitField(Expr *E) const {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00003333 if (E->isTypeDependent() || E->isValueDependent())
3334 return QualType();
3335
Eli Friedman629ffb92009-08-20 04:21:42 +00003336 FieldDecl *Field = E->getBitField();
3337 if (!Field)
3338 return QualType();
3339
3340 QualType FT = Field->getType();
3341
3342 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3343 uint64_t BitWidth = BitWidthAP.getZExtValue();
3344 uint64_t IntSize = getTypeSize(IntTy);
3345 // GCC extension compatibility: if the bit-field size is less than or equal
3346 // to the size of int, it gets promoted no matter what its type is.
3347 // For instance, unsigned long bf : 4 gets promoted to signed int.
3348 if (BitWidth < IntSize)
3349 return IntTy;
3350
3351 if (BitWidth == IntSize)
3352 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3353
3354 // Types bigger than int are not subject to promotions, and therefore act
3355 // like the base type.
3356 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3357 // is ridiculous.
3358 return QualType();
3359}
3360
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003361/// getPromotedIntegerType - Returns the type that Promotable will
3362/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3363/// integer type.
Jay Foad39c79802011-01-12 09:06:06 +00003364QualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003365 assert(!Promotable.isNull());
3366 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00003367 if (const EnumType *ET = Promotable->getAs<EnumType>())
3368 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003369 if (Promotable->isSignedIntegerType())
3370 return IntTy;
3371 uint64_t PromotableSize = getTypeSize(Promotable);
3372 uint64_t IntSize = getTypeSize(IntTy);
3373 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3374 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3375}
3376
Mike Stump11289f42009-09-09 15:08:12 +00003377/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003378/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003379/// LHS < RHS, return -1.
Jay Foad39c79802011-01-12 09:06:06 +00003380int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
John McCall424cec92011-01-19 06:33:43 +00003381 const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3382 const Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003383 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003384
Chris Lattner76a00cf2008-04-06 22:59:24 +00003385 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3386 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00003387
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003388 unsigned LHSRank = getIntegerRank(LHSC);
3389 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00003390
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003391 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3392 if (LHSRank == RHSRank) return 0;
3393 return LHSRank > RHSRank ? 1 : -1;
3394 }
Mike Stump11289f42009-09-09 15:08:12 +00003395
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003396 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3397 if (LHSUnsigned) {
3398 // If the unsigned [LHS] type is larger, return it.
3399 if (LHSRank >= RHSRank)
3400 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00003401
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003402 // If the signed type can represent all values of the unsigned type, it
3403 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003404 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003405 return -1;
3406 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00003407
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003408 // If the unsigned [RHS] type is larger, return it.
3409 if (RHSRank >= LHSRank)
3410 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00003411
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003412 // If the signed type can represent all values of the unsigned type, it
3413 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003414 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003415 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00003416}
Anders Carlsson98f07902007-08-17 05:31:46 +00003417
Anders Carlsson6d417272009-11-14 21:45:58 +00003418static RecordDecl *
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003419CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3420 DeclContext *DC, IdentifierInfo *Id) {
3421 SourceLocation Loc;
Anders Carlsson6d417272009-11-14 21:45:58 +00003422 if (Ctx.getLangOptions().CPlusPlus)
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003423 return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
Anders Carlsson6d417272009-11-14 21:45:58 +00003424 else
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003425 return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
Anders Carlsson6d417272009-11-14 21:45:58 +00003426}
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003427
Mike Stump11289f42009-09-09 15:08:12 +00003428// getCFConstantStringType - Return the type used for constant CFStrings.
Jay Foad39c79802011-01-12 09:06:06 +00003429QualType ASTContext::getCFConstantStringType() const {
Anders Carlsson98f07902007-08-17 05:31:46 +00003430 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00003431 CFConstantStringTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003432 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003433 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00003434 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00003435
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003436 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00003437
Anders Carlsson98f07902007-08-17 05:31:46 +00003438 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00003439 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003440 // int flags;
3441 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00003442 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00003443 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00003444 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00003445 FieldTypes[3] = LongTy;
3446
Anders Carlsson98f07902007-08-17 05:31:46 +00003447 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00003448 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003449 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003450 SourceLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003451 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003452 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003453 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003454 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003455 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003456 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003457 }
3458
Douglas Gregord5058122010-02-11 01:19:42 +00003459 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00003460 }
Mike Stump11289f42009-09-09 15:08:12 +00003461
Anders Carlsson98f07902007-08-17 05:31:46 +00003462 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00003463}
Anders Carlsson87c149b2007-10-11 01:00:40 +00003464
Douglas Gregor512b0772009-04-23 22:29:11 +00003465void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003466 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003467 assert(Rec && "Invalid CFConstantStringType");
3468 CFConstantStringTypeDecl = Rec->getDecl();
3469}
3470
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003471// getNSConstantStringType - Return the type used for constant NSStrings.
Jay Foad39c79802011-01-12 09:06:06 +00003472QualType ASTContext::getNSConstantStringType() const {
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003473 if (!NSConstantStringTypeDecl) {
3474 NSConstantStringTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003475 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003476 &Idents.get("__builtin_NSString"));
3477 NSConstantStringTypeDecl->startDefinition();
3478
3479 QualType FieldTypes[3];
3480
3481 // const int *isa;
3482 FieldTypes[0] = getPointerType(IntTy.withConst());
3483 // const char *str;
3484 FieldTypes[1] = getPointerType(CharTy.withConst());
3485 // unsigned int length;
3486 FieldTypes[2] = UnsignedIntTy;
3487
3488 // Create fields
3489 for (unsigned i = 0; i < 3; ++i) {
3490 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003491 SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003492 SourceLocation(), 0,
3493 FieldTypes[i], /*TInfo=*/0,
3494 /*BitWidth=*/0,
3495 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003496 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003497 NSConstantStringTypeDecl->addDecl(Field);
3498 }
3499
3500 NSConstantStringTypeDecl->completeDefinition();
3501 }
3502
3503 return getTagDeclType(NSConstantStringTypeDecl);
3504}
3505
3506void ASTContext::setNSConstantStringType(QualType T) {
3507 const RecordType *Rec = T->getAs<RecordType>();
3508 assert(Rec && "Invalid NSConstantStringType");
3509 NSConstantStringTypeDecl = Rec->getDecl();
3510}
3511
Jay Foad39c79802011-01-12 09:06:06 +00003512QualType ASTContext::getObjCFastEnumerationStateType() const {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003513 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003514 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003515 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003516 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003517 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003518
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003519 QualType FieldTypes[] = {
3520 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003521 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003522 getPointerType(UnsignedLongTy),
3523 getConstantArrayType(UnsignedLongTy,
3524 llvm::APInt(32, 5), ArrayType::Normal, 0)
3525 };
Mike Stump11289f42009-09-09 15:08:12 +00003526
Douglas Gregor91f84212008-12-11 16:49:14 +00003527 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003528 FieldDecl *Field = FieldDecl::Create(*this,
3529 ObjCFastEnumerationStateTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003530 SourceLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00003531 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003532 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003533 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003534 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003535 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003536 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003537 }
Mike Stump11289f42009-09-09 15:08:12 +00003538
Douglas Gregord5058122010-02-11 01:19:42 +00003539 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003540 }
Mike Stump11289f42009-09-09 15:08:12 +00003541
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003542 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3543}
3544
Jay Foad39c79802011-01-12 09:06:06 +00003545QualType ASTContext::getBlockDescriptorType() const {
Mike Stumpd0153282009-10-20 02:12:22 +00003546 if (BlockDescriptorType)
3547 return getTagDeclType(BlockDescriptorType);
3548
3549 RecordDecl *T;
3550 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003551 T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003552 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003553 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003554
3555 QualType FieldTypes[] = {
3556 UnsignedLongTy,
3557 UnsignedLongTy,
3558 };
3559
3560 const char *FieldNames[] = {
3561 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003562 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003563 };
3564
3565 for (size_t i = 0; i < 2; ++i) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003566 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003567 SourceLocation(),
3568 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003569 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003570 /*BitWidth=*/0,
3571 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003572 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003573 T->addDecl(Field);
3574 }
3575
Douglas Gregord5058122010-02-11 01:19:42 +00003576 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003577
3578 BlockDescriptorType = T;
3579
3580 return getTagDeclType(BlockDescriptorType);
3581}
3582
3583void ASTContext::setBlockDescriptorType(QualType T) {
3584 const RecordType *Rec = T->getAs<RecordType>();
3585 assert(Rec && "Invalid BlockDescriptorType");
3586 BlockDescriptorType = Rec->getDecl();
3587}
3588
Jay Foad39c79802011-01-12 09:06:06 +00003589QualType ASTContext::getBlockDescriptorExtendedType() const {
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003590 if (BlockDescriptorExtendedType)
3591 return getTagDeclType(BlockDescriptorExtendedType);
3592
3593 RecordDecl *T;
3594 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003595 T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003596 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003597 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003598
3599 QualType FieldTypes[] = {
3600 UnsignedLongTy,
3601 UnsignedLongTy,
3602 getPointerType(VoidPtrTy),
3603 getPointerType(VoidPtrTy)
3604 };
3605
3606 const char *FieldNames[] = {
3607 "reserved",
3608 "Size",
3609 "CopyFuncPtr",
3610 "DestroyFuncPtr"
3611 };
3612
3613 for (size_t i = 0; i < 4; ++i) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003614 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003615 SourceLocation(),
3616 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003617 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003618 /*BitWidth=*/0,
3619 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003620 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003621 T->addDecl(Field);
3622 }
3623
Douglas Gregord5058122010-02-11 01:19:42 +00003624 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003625
3626 BlockDescriptorExtendedType = T;
3627
3628 return getTagDeclType(BlockDescriptorExtendedType);
3629}
3630
3631void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3632 const RecordType *Rec = T->getAs<RecordType>();
3633 assert(Rec && "Invalid BlockDescriptorType");
3634 BlockDescriptorExtendedType = Rec->getDecl();
3635}
3636
Jay Foad39c79802011-01-12 09:06:06 +00003637bool ASTContext::BlockRequiresCopying(QualType Ty) const {
Mike Stump94967902009-10-21 18:16:27 +00003638 if (Ty->isBlockPointerType())
3639 return true;
3640 if (isObjCNSObjectType(Ty))
3641 return true;
3642 if (Ty->isObjCObjectPointerType())
3643 return true;
Fariborz Jahaniana00076c2010-11-17 00:21:28 +00003644 if (getLangOptions().CPlusPlus) {
3645 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3646 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3647 return RD->hasConstCopyConstructor(*this);
3648
3649 }
3650 }
Mike Stump94967902009-10-21 18:16:27 +00003651 return false;
3652}
3653
Jay Foad39c79802011-01-12 09:06:06 +00003654QualType
3655ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const {
Mike Stump94967902009-10-21 18:16:27 +00003656 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003657 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003658 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003659 // unsigned int __flags;
3660 // unsigned int __size;
Eli Friedman04831922010-08-22 01:00:03 +00003661 // void *__copy_helper; // as needed
3662 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003663 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003664 // } *
3665
Mike Stump94967902009-10-21 18:16:27 +00003666 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3667
3668 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003669 llvm::SmallString<36> Name;
3670 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3671 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003672 RecordDecl *T;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003673 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003674 T->startDefinition();
3675 QualType Int32Ty = IntTy;
3676 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3677 QualType FieldTypes[] = {
3678 getPointerType(VoidPtrTy),
3679 getPointerType(getTagDeclType(T)),
3680 Int32Ty,
3681 Int32Ty,
3682 getPointerType(VoidPtrTy),
3683 getPointerType(VoidPtrTy),
3684 Ty
3685 };
3686
Daniel Dunbar56df9772010-08-17 22:39:59 +00003687 llvm::StringRef FieldNames[] = {
Mike Stump94967902009-10-21 18:16:27 +00003688 "__isa",
3689 "__forwarding",
3690 "__flags",
3691 "__size",
3692 "__copy_helper",
3693 "__destroy_helper",
3694 DeclName,
3695 };
3696
3697 for (size_t i = 0; i < 7; ++i) {
3698 if (!HasCopyAndDispose && i >=4 && i <= 5)
3699 continue;
3700 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003701 SourceLocation(),
Mike Stump94967902009-10-21 18:16:27 +00003702 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003703 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003704 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003705 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003706 T->addDecl(Field);
3707 }
3708
Douglas Gregord5058122010-02-11 01:19:42 +00003709 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003710
3711 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003712}
3713
Douglas Gregor512b0772009-04-23 22:29:11 +00003714void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003715 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003716 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3717 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3718}
3719
Anders Carlsson18acd442007-10-29 06:33:42 +00003720// This returns true if a type has been typedefed to BOOL:
3721// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003722static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003723 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003724 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3725 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003726
Anders Carlssond8499822007-10-29 05:01:08 +00003727 return false;
3728}
3729
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003730/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003731/// purpose.
Jay Foad39c79802011-01-12 09:06:06 +00003732CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
Ken Dyck40775002010-01-11 17:06:35 +00003733 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003734
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003735 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003736 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003737 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003738 // Treat arrays as pointers, since that's how they're passed in.
3739 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003740 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003741 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003742}
3743
3744static inline
3745std::string charUnitsToString(const CharUnits &CU) {
3746 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003747}
3748
John McCall351762c2011-02-07 10:33:21 +00003749/// getObjCEncodingForBlock - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003750/// declaration.
John McCall351762c2011-02-07 10:33:21 +00003751std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
3752 std::string S;
3753
David Chisnall950a9512009-11-17 19:33:30 +00003754 const BlockDecl *Decl = Expr->getBlockDecl();
3755 QualType BlockTy =
3756 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3757 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003758 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003759 // Compute size of all parameters.
3760 // Start with computing size of a pointer in number of bytes.
3761 // FIXME: There might(should) be a better way of doing this computation!
3762 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003763 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3764 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003765 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003766 E = Decl->param_end(); PI != E; ++PI) {
3767 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003768 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003769 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003770 ParmOffset += sz;
3771 }
3772 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003773 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003774 // Block pointer and offset.
3775 S += "@?0";
3776 ParmOffset = PtrSize;
3777
3778 // Argument types.
3779 ParmOffset = PtrSize;
3780 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3781 Decl->param_end(); PI != E; ++PI) {
3782 ParmVarDecl *PVDecl = *PI;
3783 QualType PType = PVDecl->getOriginalType();
3784 if (const ArrayType *AT =
3785 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3786 // Use array's original type only if it has known number of
3787 // elements.
3788 if (!isa<ConstantArrayType>(AT))
3789 PType = PVDecl->getType();
3790 } else if (PType->isFunctionType())
3791 PType = PVDecl->getType();
3792 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003793 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003794 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003795 }
John McCall351762c2011-02-07 10:33:21 +00003796
3797 return S;
David Chisnall950a9512009-11-17 19:33:30 +00003798}
3799
David Chisnall50e4eba2010-12-30 14:05:53 +00003800void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
3801 std::string& S) {
3802 // Encode result type.
3803 getObjCEncodingForType(Decl->getResultType(), S);
3804 CharUnits ParmOffset;
3805 // Compute size of all parameters.
3806 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3807 E = Decl->param_end(); PI != E; ++PI) {
3808 QualType PType = (*PI)->getType();
3809 CharUnits sz = getObjCEncodingTypeSize(PType);
3810 assert (sz.isPositive() &&
3811 "getObjCEncodingForMethodDecl - Incomplete param type");
3812 ParmOffset += sz;
3813 }
3814 S += charUnitsToString(ParmOffset);
3815 ParmOffset = CharUnits::Zero();
3816
3817 // Argument types.
3818 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3819 E = Decl->param_end(); PI != E; ++PI) {
3820 ParmVarDecl *PVDecl = *PI;
3821 QualType PType = PVDecl->getOriginalType();
3822 if (const ArrayType *AT =
3823 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3824 // Use array's original type only if it has known number of
3825 // elements.
3826 if (!isa<ConstantArrayType>(AT))
3827 PType = PVDecl->getType();
3828 } else if (PType->isFunctionType())
3829 PType = PVDecl->getType();
3830 getObjCEncodingForType(PType, S);
3831 S += charUnitsToString(ParmOffset);
3832 ParmOffset += getObjCEncodingTypeSize(PType);
3833 }
3834}
3835
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003836/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003837/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003838void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Jay Foad39c79802011-01-12 09:06:06 +00003839 std::string& S) const {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003840 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003841 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003842 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003843 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003844 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003845 // Compute size of all parameters.
3846 // Start with computing size of a pointer in number of bytes.
3847 // FIXME: There might(should) be a better way of doing this computation!
3848 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003849 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003850 // The first two arguments (self and _cmd) are pointers; account for
3851 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003852 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003853 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003854 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003855 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003856 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003857 assert (sz.isPositive() &&
3858 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003859 ParmOffset += sz;
3860 }
Ken Dyck40775002010-01-11 17:06:35 +00003861 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003862 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003863 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003864
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003865 // Argument types.
3866 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003867 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003868 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003869 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003870 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003871 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003872 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3873 // Use array's original type only if it has known number of
3874 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003875 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003876 PType = PVDecl->getType();
3877 } else if (PType->isFunctionType())
3878 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003879 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003880 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003881 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003882 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003883 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003884 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003885 }
3886}
3887
Daniel Dunbar4932b362008-08-28 04:38:10 +00003888/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003889/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003890/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3891/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003892/// Property attributes are stored as a comma-delimited C string. The simple
3893/// attributes readonly and bycopy are encoded as single characters. The
3894/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3895/// encoded as single characters, followed by an identifier. Property types
3896/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003897/// these attributes are defined by the following enumeration:
3898/// @code
3899/// enum PropertyAttributes {
3900/// kPropertyReadOnly = 'R', // property is read-only.
3901/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3902/// kPropertyByref = '&', // property is a reference to the value last assigned
3903/// kPropertyDynamic = 'D', // property is dynamic
3904/// kPropertyGetter = 'G', // followed by getter selector name
3905/// kPropertySetter = 'S', // followed by setter selector name
3906/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3907/// kPropertyType = 't' // followed by old-style type encoding.
3908/// kPropertyWeak = 'W' // 'weak' property
3909/// kPropertyStrong = 'P' // property GC'able
3910/// kPropertyNonAtomic = 'N' // property non-atomic
3911/// };
3912/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003913void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003914 const Decl *Container,
Jay Foad39c79802011-01-12 09:06:06 +00003915 std::string& S) const {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003916 // Collect information from the property implementation decl(s).
3917 bool Dynamic = false;
3918 ObjCPropertyImplDecl *SynthesizePID = 0;
3919
3920 // FIXME: Duplicated code due to poor abstraction.
3921 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003922 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003923 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3924 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003925 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003926 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003927 ObjCPropertyImplDecl *PID = *i;
3928 if (PID->getPropertyDecl() == PD) {
3929 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3930 Dynamic = true;
3931 } else {
3932 SynthesizePID = PID;
3933 }
3934 }
3935 }
3936 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003937 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003938 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003939 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003940 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003941 ObjCPropertyImplDecl *PID = *i;
3942 if (PID->getPropertyDecl() == PD) {
3943 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3944 Dynamic = true;
3945 } else {
3946 SynthesizePID = PID;
3947 }
3948 }
Mike Stump11289f42009-09-09 15:08:12 +00003949 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003950 }
3951 }
3952
3953 // FIXME: This is not very efficient.
3954 S = "T";
3955
3956 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003957 // GCC has some special rules regarding encoding of properties which
3958 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003959 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003960 true /* outermost type */,
3961 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003962
3963 if (PD->isReadOnly()) {
3964 S += ",R";
3965 } else {
3966 switch (PD->getSetterKind()) {
3967 case ObjCPropertyDecl::Assign: break;
3968 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003969 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003970 }
3971 }
3972
3973 // It really isn't clear at all what this means, since properties
3974 // are "dynamic by default".
3975 if (Dynamic)
3976 S += ",D";
3977
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003978 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3979 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003980
Daniel Dunbar4932b362008-08-28 04:38:10 +00003981 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3982 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003983 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003984 }
3985
3986 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3987 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003988 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003989 }
3990
3991 if (SynthesizePID) {
3992 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3993 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003994 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003995 }
3996
3997 // FIXME: OBJCGC: weak & strong
3998}
3999
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004000/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00004001/// Another legacy compatibility encoding: 32-bit longs are encoded as
4002/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004003/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4004///
4005void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00004006 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00004007 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Jay Foad39c79802011-01-12 09:06:06 +00004008 if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004009 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00004010 else
Jay Foad39c79802011-01-12 09:06:06 +00004011 if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004012 PointeeTy = IntTy;
4013 }
4014 }
4015}
4016
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004017void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Jay Foad39c79802011-01-12 09:06:06 +00004018 const FieldDecl *Field) const {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004019 // We follow the behavior of gcc, expanding structures which are
4020 // directly pointed to, and expanding embedded structures. Note that
4021 // these rules are sufficient to prevent recursive encoding of the
4022 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00004023 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00004024 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004025}
4026
David Chisnallb190a2c2010-06-04 01:10:52 +00004027static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
4028 switch (T->getAs<BuiltinType>()->getKind()) {
4029 default: assert(0 && "Unhandled builtin type kind");
4030 case BuiltinType::Void: return 'v';
4031 case BuiltinType::Bool: return 'B';
4032 case BuiltinType::Char_U:
4033 case BuiltinType::UChar: return 'C';
4034 case BuiltinType::UShort: return 'S';
4035 case BuiltinType::UInt: return 'I';
4036 case BuiltinType::ULong:
Jay Foad39c79802011-01-12 09:06:06 +00004037 return C->getIntWidth(T) == 32 ? 'L' : 'Q';
David Chisnallb190a2c2010-06-04 01:10:52 +00004038 case BuiltinType::UInt128: return 'T';
4039 case BuiltinType::ULongLong: return 'Q';
4040 case BuiltinType::Char_S:
4041 case BuiltinType::SChar: return 'c';
4042 case BuiltinType::Short: return 's';
Chris Lattnerad3467e2010-12-25 23:25:43 +00004043 case BuiltinType::WChar_S:
4044 case BuiltinType::WChar_U:
David Chisnallb190a2c2010-06-04 01:10:52 +00004045 case BuiltinType::Int: return 'i';
4046 case BuiltinType::Long:
Jay Foad39c79802011-01-12 09:06:06 +00004047 return C->getIntWidth(T) == 32 ? 'l' : 'q';
David Chisnallb190a2c2010-06-04 01:10:52 +00004048 case BuiltinType::LongLong: return 'q';
4049 case BuiltinType::Int128: return 't';
4050 case BuiltinType::Float: return 'f';
4051 case BuiltinType::Double: return 'd';
Daniel Dunbar7cba5a72010-10-11 21:13:48 +00004052 case BuiltinType::LongDouble: return 'D';
David Chisnallb190a2c2010-06-04 01:10:52 +00004053 }
4054}
4055
Jay Foad39c79802011-01-12 09:06:06 +00004056static void EncodeBitField(const ASTContext *Ctx, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00004057 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004058 const Expr *E = FD->getBitWidth();
4059 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004060 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00004061 // The NeXT runtime encodes bit fields as b followed by the number of bits.
4062 // The GNU runtime requires more information; bitfields are encoded as b,
4063 // then the offset (in bits) of the first element, then the type of the
4064 // bitfield, then the size in bits. For example, in this structure:
4065 //
4066 // struct
4067 // {
4068 // int integer;
4069 // int flags:2;
4070 // };
4071 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
4072 // runtime, but b32i2 for the GNU runtime. The reason for this extra
4073 // information is not especially sensible, but we're stuck with it for
4074 // compatibility with GCC, although providing it breaks anything that
4075 // actually uses runtime introspection and wants to work on both runtimes...
4076 if (!Ctx->getLangOptions().NeXTRuntime) {
4077 const RecordDecl *RD = FD->getParent();
4078 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
4079 // FIXME: This same linear search is also used in ExprConstant - it might
4080 // be better if the FieldDecl stored its offset. We'd be increasing the
4081 // size of the object slightly, but saving some time every time it is used.
4082 unsigned i = 0;
4083 for (RecordDecl::field_iterator Field = RD->field_begin(),
4084 FieldEnd = RD->field_end();
4085 Field != FieldEnd; (void)++Field, ++i) {
4086 if (*Field == FD)
4087 break;
4088 }
4089 S += llvm::utostr(RL.getFieldOffset(i));
David Chisnall6f0a7d22010-12-26 20:12:30 +00004090 if (T->isEnumeralType())
4091 S += 'i';
4092 else
Jay Foad39c79802011-01-12 09:06:06 +00004093 S += ObjCEncodingForPrimitiveKind(Ctx, T);
David Chisnallb190a2c2010-06-04 01:10:52 +00004094 }
4095 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004096 S += llvm::utostr(N);
4097}
4098
Daniel Dunbar07d07852009-10-18 21:17:35 +00004099// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004100void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
4101 bool ExpandPointedToStructures,
4102 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00004103 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00004104 bool OutermostType,
Jay Foad39c79802011-01-12 09:06:06 +00004105 bool EncodingProperty) const {
David Chisnallb190a2c2010-06-04 01:10:52 +00004106 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004107 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00004108 return EncodeBitField(this, S, T, FD);
4109 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004110 return;
4111 }
Mike Stump11289f42009-09-09 15:08:12 +00004112
John McCall9dd450b2009-09-21 23:43:11 +00004113 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00004114 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00004115 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00004116 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004117 return;
4118 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00004119
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004120 // encoding for pointer or r3eference types.
4121 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004122 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00004123 if (PT->isObjCSelType()) {
4124 S += ':';
4125 return;
4126 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004127 PointeeTy = PT->getPointeeType();
4128 }
4129 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4130 PointeeTy = RT->getPointeeType();
4131 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004132 bool isReadOnly = false;
4133 // For historical/compatibility reasons, the read-only qualifier of the
4134 // pointee gets emitted _before_ the '^'. The read-only qualifier of
4135 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00004136 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00004137 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004138 if (OutermostType && T.isConstQualified()) {
4139 isReadOnly = true;
4140 S += 'r';
4141 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00004142 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004143 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004144 while (P->getAs<PointerType>())
4145 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004146 if (P.isConstQualified()) {
4147 isReadOnly = true;
4148 S += 'r';
4149 }
4150 }
4151 if (isReadOnly) {
4152 // Another legacy compatibility encoding. Some ObjC qualifier and type
4153 // combinations need to be rearranged.
4154 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00004155 if (llvm::StringRef(S).endswith("nr"))
4156 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004157 }
Mike Stump11289f42009-09-09 15:08:12 +00004158
Anders Carlssond8499822007-10-29 05:01:08 +00004159 if (PointeeTy->isCharType()) {
4160 // char pointer types should be encoded as '*' unless it is a
4161 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00004162 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00004163 S += '*';
4164 return;
4165 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004166 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00004167 // GCC binary compat: Need to convert "struct objc_class *" to "#".
4168 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
4169 S += '#';
4170 return;
4171 }
4172 // GCC binary compat: Need to convert "struct objc_object *" to "@".
4173 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
4174 S += '@';
4175 return;
4176 }
4177 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00004178 }
Anders Carlssond8499822007-10-29 05:01:08 +00004179 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004180 getLegacyIntegralTypeEncoding(PointeeTy);
4181
Mike Stump11289f42009-09-09 15:08:12 +00004182 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004183 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004184 return;
4185 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004186
Chris Lattnere7cabb92009-07-13 00:10:46 +00004187 if (const ArrayType *AT =
4188 // Ignore type qualifiers etc.
4189 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00004190 if (isa<IncompleteArrayType>(AT)) {
4191 // Incomplete arrays are encoded as a pointer to the array element.
4192 S += '^';
4193
Mike Stump11289f42009-09-09 15:08:12 +00004194 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00004195 false, ExpandStructures, FD);
4196 } else {
4197 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00004198
Anders Carlssond05f44b2009-02-22 01:38:57 +00004199 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
4200 S += llvm::utostr(CAT->getSize().getZExtValue());
4201 else {
4202 //Variable length arrays are encoded as a regular array with 0 elements.
4203 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
4204 S += '0';
4205 }
Mike Stump11289f42009-09-09 15:08:12 +00004206
4207 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00004208 false, ExpandStructures, FD);
4209 S += ']';
4210 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004211 return;
4212 }
Mike Stump11289f42009-09-09 15:08:12 +00004213
John McCall9dd450b2009-09-21 23:43:11 +00004214 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00004215 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004216 return;
4217 }
Mike Stump11289f42009-09-09 15:08:12 +00004218
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004219 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004220 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004221 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00004222 // Anonymous structures print as '?'
4223 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4224 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00004225 if (ClassTemplateSpecializationDecl *Spec
4226 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
4227 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
4228 std::string TemplateArgsStr
4229 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004230 TemplateArgs.data(),
4231 TemplateArgs.size(),
Fariborz Jahanianc5158202010-05-07 00:28:49 +00004232 (*this).PrintingPolicy);
4233
4234 S += TemplateArgsStr;
4235 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00004236 } else {
4237 S += '?';
4238 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00004239 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004240 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004241 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
4242 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00004243 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004244 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004245 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00004246 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004247 S += '"';
4248 }
Mike Stump11289f42009-09-09 15:08:12 +00004249
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004250 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004251 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00004252 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004253 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004254 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004255 QualType qt = Field->getType();
4256 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00004257 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004258 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004259 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004260 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00004261 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004262 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004263 return;
4264 }
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004265
Chris Lattnere7cabb92009-07-13 00:10:46 +00004266 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004267 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00004268 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004269 else
4270 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004271 return;
4272 }
Mike Stump11289f42009-09-09 15:08:12 +00004273
Chris Lattnere7cabb92009-07-13 00:10:46 +00004274 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00004275 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00004276 return;
4277 }
Mike Stump11289f42009-09-09 15:08:12 +00004278
John McCall8b07ec22010-05-15 11:32:37 +00004279 // Ignore protocol qualifiers when mangling at this level.
4280 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4281 T = OT->getBaseType();
4282
John McCall8ccfcb52009-09-24 19:53:00 +00004283 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004284 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00004285 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004286 S += '{';
4287 const IdentifierInfo *II = OI->getIdentifier();
4288 S += II->getName();
4289 S += '=';
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004290 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
4291 DeepCollectObjCIvars(OI, true, Ivars);
4292 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4293 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4294 if (Field->isBitField())
4295 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004296 else
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004297 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004298 }
4299 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004300 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004301 }
Mike Stump11289f42009-09-09 15:08:12 +00004302
John McCall9dd450b2009-09-21 23:43:11 +00004303 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004304 if (OPT->isObjCIdType()) {
4305 S += '@';
4306 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004307 }
Mike Stump11289f42009-09-09 15:08:12 +00004308
Steve Narofff0c86112009-10-28 22:03:49 +00004309 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4310 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4311 // Since this is a binary compatibility issue, need to consult with runtime
4312 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004313 S += '#';
4314 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004315 }
Mike Stump11289f42009-09-09 15:08:12 +00004316
Chris Lattnere7cabb92009-07-13 00:10:46 +00004317 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00004318 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00004319 ExpandPointedToStructures,
4320 ExpandStructures, FD);
4321 if (FD || EncodingProperty) {
4322 // Note that we do extended encoding of protocol qualifer list
4323 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004324 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00004325 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4326 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004327 S += '<';
4328 S += (*I)->getNameAsString();
4329 S += '>';
4330 }
4331 S += '"';
4332 }
4333 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004334 }
Mike Stump11289f42009-09-09 15:08:12 +00004335
Chris Lattnere7cabb92009-07-13 00:10:46 +00004336 QualType PointeeTy = OPT->getPointeeType();
4337 if (!EncodingProperty &&
4338 isa<TypedefType>(PointeeTy.getTypePtr())) {
4339 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00004340 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00004341 // {...};
4342 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00004343 getObjCEncodingForTypeImpl(PointeeTy, S,
4344 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00004345 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00004346 return;
4347 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004348
4349 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00004350 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004351 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00004352 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00004353 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4354 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004355 S += '<';
4356 S += (*I)->getNameAsString();
4357 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00004358 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004359 S += '"';
4360 }
4361 return;
4362 }
Mike Stump11289f42009-09-09 15:08:12 +00004363
John McCalla9e6e8d2010-05-17 23:56:34 +00004364 // gcc just blithely ignores member pointers.
4365 // TODO: maybe there should be a mangling for these
4366 if (T->getAs<MemberPointerType>())
4367 return;
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004368
4369 if (T->isVectorType()) {
4370 // This matches gcc's encoding, even though technically it is
4371 // insufficient.
4372 // FIXME. We should do a better job than gcc.
4373 return;
4374 }
4375
Chris Lattnere7cabb92009-07-13 00:10:46 +00004376 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00004377}
4378
Mike Stump11289f42009-09-09 15:08:12 +00004379void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00004380 std::string& S) const {
4381 if (QT & Decl::OBJC_TQ_In)
4382 S += 'n';
4383 if (QT & Decl::OBJC_TQ_Inout)
4384 S += 'N';
4385 if (QT & Decl::OBJC_TQ_Out)
4386 S += 'o';
4387 if (QT & Decl::OBJC_TQ_Bycopy)
4388 S += 'O';
4389 if (QT & Decl::OBJC_TQ_Byref)
4390 S += 'R';
4391 if (QT & Decl::OBJC_TQ_Oneway)
4392 S += 'V';
4393}
4394
Chris Lattnere7cabb92009-07-13 00:10:46 +00004395void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00004396 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004397
Anders Carlsson87c149b2007-10-11 01:00:40 +00004398 BuiltinVaListType = T;
4399}
4400
Chris Lattnere7cabb92009-07-13 00:10:46 +00004401void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004402 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00004403}
4404
Chris Lattnere7cabb92009-07-13 00:10:46 +00004405void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00004406 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00004407}
4408
Chris Lattnere7cabb92009-07-13 00:10:46 +00004409void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004410 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004411}
4412
Chris Lattnere7cabb92009-07-13 00:10:46 +00004413void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004414 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00004415}
4416
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004417void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00004418 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00004419 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004420
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004421 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00004422}
4423
John McCalld28ae272009-12-02 08:04:21 +00004424/// \brief Retrieve the template name that corresponds to a non-empty
4425/// lookup.
Jay Foad39c79802011-01-12 09:06:06 +00004426TemplateName
4427ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4428 UnresolvedSetIterator End) const {
John McCalld28ae272009-12-02 08:04:21 +00004429 unsigned size = End - Begin;
4430 assert(size > 1 && "set is not overloaded!");
4431
4432 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4433 size * sizeof(FunctionTemplateDecl*));
4434 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4435
4436 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00004437 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00004438 NamedDecl *D = *I;
4439 assert(isa<FunctionTemplateDecl>(D) ||
4440 (isa<UsingShadowDecl>(D) &&
4441 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4442 *Storage++ = D;
4443 }
4444
4445 return TemplateName(OT);
4446}
4447
Douglas Gregordc572a32009-03-30 22:58:21 +00004448/// \brief Retrieve the template name that represents a qualified
4449/// template name such as \c std::vector.
Jay Foad39c79802011-01-12 09:06:06 +00004450TemplateName
4451ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
4452 bool TemplateKeyword,
4453 TemplateDecl *Template) const {
Douglas Gregore29139c2011-03-03 17:04:51 +00004454 assert(NNS && "Missing nested-name-specifier in qualified template name");
4455
Douglas Gregorc42075a2010-02-04 18:10:26 +00004456 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00004457 llvm::FoldingSetNodeID ID;
4458 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4459
4460 void *InsertPos = 0;
4461 QualifiedTemplateName *QTN =
4462 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4463 if (!QTN) {
4464 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4465 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4466 }
4467
4468 return TemplateName(QTN);
4469}
4470
4471/// \brief Retrieve the template name that represents a dependent
4472/// template name such as \c MetaFun::template apply.
Jay Foad39c79802011-01-12 09:06:06 +00004473TemplateName
4474ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4475 const IdentifierInfo *Name) const {
Mike Stump11289f42009-09-09 15:08:12 +00004476 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00004477 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00004478
4479 llvm::FoldingSetNodeID ID;
4480 DependentTemplateName::Profile(ID, NNS, Name);
4481
4482 void *InsertPos = 0;
4483 DependentTemplateName *QTN =
4484 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4485
4486 if (QTN)
4487 return TemplateName(QTN);
4488
4489 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4490 if (CanonNNS == NNS) {
4491 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4492 } else {
4493 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4494 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004495 DependentTemplateName *CheckQTN =
4496 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4497 assert(!CheckQTN && "Dependent type name canonicalization broken");
4498 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004499 }
4500
4501 DependentTemplateNames.InsertNode(QTN, InsertPos);
4502 return TemplateName(QTN);
4503}
4504
Douglas Gregor71395fa2009-11-04 00:56:37 +00004505/// \brief Retrieve the template name that represents a dependent
4506/// template name such as \c MetaFun::template operator+.
4507TemplateName
4508ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Jay Foad39c79802011-01-12 09:06:06 +00004509 OverloadedOperatorKind Operator) const {
Douglas Gregor71395fa2009-11-04 00:56:37 +00004510 assert((!NNS || NNS->isDependent()) &&
4511 "Nested name specifier must be dependent");
4512
4513 llvm::FoldingSetNodeID ID;
4514 DependentTemplateName::Profile(ID, NNS, Operator);
4515
4516 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004517 DependentTemplateName *QTN
4518 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004519
4520 if (QTN)
4521 return TemplateName(QTN);
4522
4523 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4524 if (CanonNNS == NNS) {
4525 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4526 } else {
4527 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4528 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004529
4530 DependentTemplateName *CheckQTN
4531 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4532 assert(!CheckQTN && "Dependent template name canonicalization broken");
4533 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004534 }
4535
4536 DependentTemplateNames.InsertNode(QTN, InsertPos);
4537 return TemplateName(QTN);
4538}
4539
Douglas Gregor5590be02011-01-15 06:45:20 +00004540TemplateName
4541ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
4542 const TemplateArgument &ArgPack) const {
4543 ASTContext &Self = const_cast<ASTContext &>(*this);
4544 llvm::FoldingSetNodeID ID;
4545 SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
4546
4547 void *InsertPos = 0;
4548 SubstTemplateTemplateParmPackStorage *Subst
4549 = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
4550
4551 if (!Subst) {
4552 Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param,
4553 ArgPack.pack_size(),
4554 ArgPack.pack_begin());
4555 SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
4556 }
4557
4558 return TemplateName(Subst);
4559}
4560
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004561/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004562/// TargetInfo, produce the corresponding type. The unsigned @p Type
4563/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004564CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004565 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004566 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004567 case TargetInfo::SignedShort: return ShortTy;
4568 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4569 case TargetInfo::SignedInt: return IntTy;
4570 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4571 case TargetInfo::SignedLong: return LongTy;
4572 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4573 case TargetInfo::SignedLongLong: return LongLongTy;
4574 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4575 }
4576
4577 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004578 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004579}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004580
4581//===----------------------------------------------------------------------===//
4582// Type Predicates.
4583//===----------------------------------------------------------------------===//
4584
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004585/// isObjCNSObjectType - Return true if this is an NSObject object using
4586/// NSObject attribute on a c-style pointer type.
4587/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004588/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004589///
4590bool ASTContext::isObjCNSObjectType(QualType Ty) const {
John McCall424cec92011-01-19 06:33:43 +00004591 if (const TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
Richard Smithdda56e42011-04-15 14:24:37 +00004592 if (TypedefNameDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004593 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004594 return true;
4595 }
Mike Stump11289f42009-09-09 15:08:12 +00004596 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004597}
4598
Fariborz Jahanian96207692009-02-18 21:49:28 +00004599/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4600/// garbage collection attribute.
4601///
John McCall553d45a2011-01-12 00:34:59 +00004602Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
4603 if (getLangOptions().getGCMode() == LangOptions::NonGC)
4604 return Qualifiers::GCNone;
4605
4606 assert(getLangOptions().ObjC1);
4607 Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
4608
4609 // Default behaviour under objective-C's gc is for ObjC pointers
4610 // (or pointers to them) be treated as though they were declared
4611 // as __strong.
4612 if (GCAttrs == Qualifiers::GCNone) {
4613 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
4614 return Qualifiers::Strong;
4615 else if (Ty->isPointerType())
4616 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
4617 } else {
4618 // It's not valid to set GC attributes on anything that isn't a
4619 // pointer.
4620#ifndef NDEBUG
4621 QualType CT = Ty->getCanonicalTypeInternal();
4622 while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
4623 CT = AT->getElementType();
4624 assert(CT->isAnyPointerType() || CT->isBlockPointerType());
4625#endif
Fariborz Jahanian96207692009-02-18 21:49:28 +00004626 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004627 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004628}
4629
Chris Lattner49af6a42008-04-07 06:51:04 +00004630//===----------------------------------------------------------------------===//
4631// Type Compatibility Testing
4632//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004633
Mike Stump11289f42009-09-09 15:08:12 +00004634/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004635/// compatible.
4636static bool areCompatVectorTypes(const VectorType *LHS,
4637 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004638 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004639 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004640 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004641}
4642
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004643bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4644 QualType SecondVec) {
4645 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4646 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4647
4648 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4649 return true;
4650
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004651 // Treat Neon vector types and most AltiVec vector types as if they are the
4652 // equivalent GCC vector types.
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004653 const VectorType *First = FirstVec->getAs<VectorType>();
4654 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004655 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004656 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004657 First->getVectorKind() != VectorType::AltiVecPixel &&
4658 First->getVectorKind() != VectorType::AltiVecBool &&
4659 Second->getVectorKind() != VectorType::AltiVecPixel &&
4660 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004661 return true;
4662
4663 return false;
4664}
4665
Steve Naroff8e6aee52009-07-23 01:01:38 +00004666//===----------------------------------------------------------------------===//
4667// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4668//===----------------------------------------------------------------------===//
4669
4670/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4671/// inheritance hierarchy of 'rProto'.
Jay Foad39c79802011-01-12 09:06:06 +00004672bool
4673ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4674 ObjCProtocolDecl *rProto) const {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004675 if (lProto == rProto)
4676 return true;
4677 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4678 E = rProto->protocol_end(); PI != E; ++PI)
4679 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4680 return true;
4681 return false;
4682}
4683
Steve Naroff8e6aee52009-07-23 01:01:38 +00004684/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4685/// return true if lhs's protocols conform to rhs's protocol; false
4686/// otherwise.
4687bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4688 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4689 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4690 return false;
4691}
4692
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004693/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4694/// Class<p1, ...>.
4695bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4696 QualType rhs) {
4697 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4698 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4699 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4700
4701 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4702 E = lhsQID->qual_end(); I != E; ++I) {
4703 bool match = false;
4704 ObjCProtocolDecl *lhsProto = *I;
4705 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4706 E = rhsOPT->qual_end(); J != E; ++J) {
4707 ObjCProtocolDecl *rhsProto = *J;
4708 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4709 match = true;
4710 break;
4711 }
4712 }
4713 if (!match)
4714 return false;
4715 }
4716 return true;
4717}
4718
Steve Naroff8e6aee52009-07-23 01:01:38 +00004719/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4720/// ObjCQualifiedIDType.
4721bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4722 bool compare) {
4723 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004724 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004725 lhs->isObjCIdType() || lhs->isObjCClassType())
4726 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004727 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004728 rhs->isObjCIdType() || rhs->isObjCClassType())
4729 return true;
4730
4731 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004732 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004733
Steve Naroff8e6aee52009-07-23 01:01:38 +00004734 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004735
Steve Naroff8e6aee52009-07-23 01:01:38 +00004736 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004737 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004738 // make sure we check the class hierarchy.
4739 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4740 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4741 E = lhsQID->qual_end(); I != E; ++I) {
4742 // when comparing an id<P> on lhs with a static type on rhs,
4743 // see if static class implements all of id's protocols, directly or
4744 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004745 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004746 return false;
4747 }
4748 }
4749 // If there are no qualifiers and no interface, we have an 'id'.
4750 return true;
4751 }
Mike Stump11289f42009-09-09 15:08:12 +00004752 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004753 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4754 E = lhsQID->qual_end(); I != E; ++I) {
4755 ObjCProtocolDecl *lhsProto = *I;
4756 bool match = false;
4757
4758 // when comparing an id<P> on lhs with a static type on rhs,
4759 // see if static class implements all of id's protocols, directly or
4760 // through its super class and categories.
4761 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4762 E = rhsOPT->qual_end(); J != E; ++J) {
4763 ObjCProtocolDecl *rhsProto = *J;
4764 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4765 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4766 match = true;
4767 break;
4768 }
4769 }
Mike Stump11289f42009-09-09 15:08:12 +00004770 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004771 // make sure we check the class hierarchy.
4772 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4773 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4774 E = lhsQID->qual_end(); I != E; ++I) {
4775 // when comparing an id<P> on lhs with a static type on rhs,
4776 // see if static class implements all of id's protocols, directly or
4777 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004778 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004779 match = true;
4780 break;
4781 }
4782 }
4783 }
4784 if (!match)
4785 return false;
4786 }
Mike Stump11289f42009-09-09 15:08:12 +00004787
Steve Naroff8e6aee52009-07-23 01:01:38 +00004788 return true;
4789 }
Mike Stump11289f42009-09-09 15:08:12 +00004790
Steve Naroff8e6aee52009-07-23 01:01:38 +00004791 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4792 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4793
Mike Stump11289f42009-09-09 15:08:12 +00004794 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004795 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004796 // If both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004797 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4798 E = lhsOPT->qual_end(); I != E; ++I) {
4799 ObjCProtocolDecl *lhsProto = *I;
4800 bool match = false;
4801
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004802 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff8e6aee52009-07-23 01:01:38 +00004803 // see if static class implements all of id's protocols, directly or
4804 // through its super class and categories.
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004805 // First, lhs protocols in the qualifier list must be found, direct
4806 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004807 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4808 E = rhsQID->qual_end(); J != E; ++J) {
4809 ObjCProtocolDecl *rhsProto = *J;
4810 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4811 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4812 match = true;
4813 break;
4814 }
4815 }
4816 if (!match)
4817 return false;
4818 }
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004819
4820 // Static class's protocols, or its super class or category protocols
4821 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4822 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4823 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4824 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4825 // This is rather dubious but matches gcc's behavior. If lhs has
4826 // no type qualifier and its class has no static protocol(s)
4827 // assume that it is mismatch.
4828 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4829 return false;
4830 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4831 LHSInheritedProtocols.begin(),
4832 E = LHSInheritedProtocols.end(); I != E; ++I) {
4833 bool match = false;
4834 ObjCProtocolDecl *lhsProto = (*I);
4835 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4836 E = rhsQID->qual_end(); J != E; ++J) {
4837 ObjCProtocolDecl *rhsProto = *J;
4838 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4839 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4840 match = true;
4841 break;
4842 }
4843 }
4844 if (!match)
4845 return false;
4846 }
4847 }
Steve Naroff8e6aee52009-07-23 01:01:38 +00004848 return true;
4849 }
4850 return false;
4851}
4852
Eli Friedman47f77112008-08-22 00:56:42 +00004853/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004854/// compatible for assignment from RHS to LHS. This handles validation of any
4855/// protocol qualifiers on the LHS or RHS.
4856///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004857bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4858 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004859 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4860 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4861
Steve Naroff1329fa02009-07-15 18:40:39 +00004862 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004863 if (LHS->isObjCUnqualifiedIdOrClass() ||
4864 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004865 return true;
4866
John McCall8b07ec22010-05-15 11:32:37 +00004867 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004868 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4869 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004870 false);
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004871
4872 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4873 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4874 QualType(RHSOPT,0));
4875
John McCall8b07ec22010-05-15 11:32:37 +00004876 // If we have 2 user-defined types, fall into that path.
4877 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004878 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004879
Steve Naroff8e6aee52009-07-23 01:01:38 +00004880 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004881}
4882
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004883/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
Chris Lattner57540c52011-04-15 05:22:18 +00004884/// for providing type-safety for objective-c pointers used to pass/return
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004885/// arguments in block literals. When passed as arguments, passing 'A*' where
4886/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4887/// not OK. For the return type, the opposite is not OK.
4888bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4889 const ObjCObjectPointerType *LHSOPT,
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004890 const ObjCObjectPointerType *RHSOPT,
4891 bool BlockReturnType) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004892 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004893 return true;
4894
4895 if (LHSOPT->isObjCBuiltinType()) {
4896 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4897 }
4898
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004899 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004900 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4901 QualType(RHSOPT,0),
4902 false);
4903
4904 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4905 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4906 if (LHS && RHS) { // We have 2 user-defined types.
4907 if (LHS != RHS) {
4908 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004909 return BlockReturnType;
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004910 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004911 return !BlockReturnType;
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004912 }
4913 else
4914 return true;
4915 }
4916 return false;
4917}
4918
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004919/// getIntersectionOfProtocols - This routine finds the intersection of set
4920/// of protocols inherited from two distinct objective-c pointer objects.
4921/// It is used to build composite qualifier list of the composite type of
4922/// the conditional expression involving two objective-c pointer objects.
4923static
4924void getIntersectionOfProtocols(ASTContext &Context,
4925 const ObjCObjectPointerType *LHSOPT,
4926 const ObjCObjectPointerType *RHSOPT,
4927 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4928
John McCall8b07ec22010-05-15 11:32:37 +00004929 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4930 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4931 assert(LHS->getInterface() && "LHS must have an interface base");
4932 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004933
4934 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4935 unsigned LHSNumProtocols = LHS->getNumProtocols();
4936 if (LHSNumProtocols > 0)
4937 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4938 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004939 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004940 Context.CollectInheritedProtocols(LHS->getInterface(),
4941 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004942 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4943 LHSInheritedProtocols.end());
4944 }
4945
4946 unsigned RHSNumProtocols = RHS->getNumProtocols();
4947 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004948 ObjCProtocolDecl **RHSProtocols =
4949 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004950 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4951 if (InheritedProtocolSet.count(RHSProtocols[i]))
4952 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4953 }
4954 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004955 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004956 Context.CollectInheritedProtocols(RHS->getInterface(),
4957 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004958 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4959 RHSInheritedProtocols.begin(),
4960 E = RHSInheritedProtocols.end(); I != E; ++I)
4961 if (InheritedProtocolSet.count((*I)))
4962 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004963 }
4964}
4965
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004966/// areCommonBaseCompatible - Returns common base class of the two classes if
4967/// one found. Note that this is O'2 algorithm. But it will be called as the
4968/// last type comparison in a ?-exp of ObjC pointer types before a
4969/// warning is issued. So, its invokation is extremely rare.
4970QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004971 const ObjCObjectPointerType *Lptr,
4972 const ObjCObjectPointerType *Rptr) {
4973 const ObjCObjectType *LHS = Lptr->getObjectType();
4974 const ObjCObjectType *RHS = Rptr->getObjectType();
4975 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4976 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
Fariborz Jahanianb1071432011-04-18 21:16:59 +00004977 if (!LDecl || !RDecl || (LDecl == RDecl))
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004978 return QualType();
4979
Fariborz Jahanianb1071432011-04-18 21:16:59 +00004980 do {
John McCall8b07ec22010-05-15 11:32:37 +00004981 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004982 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004983 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4984 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4985
4986 QualType Result = QualType(LHS, 0);
4987 if (!Protocols.empty())
4988 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4989 Result = getObjCObjectPointerType(Result);
4990 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004991 }
Fariborz Jahanianb1071432011-04-18 21:16:59 +00004992 } while ((LDecl = LDecl->getSuperClass()));
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004993
4994 return QualType();
4995}
4996
John McCall8b07ec22010-05-15 11:32:37 +00004997bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4998 const ObjCObjectType *RHS) {
4999 assert(LHS->getInterface() && "LHS is not an interface type");
5000 assert(RHS->getInterface() && "RHS is not an interface type");
5001
Chris Lattner49af6a42008-04-07 06:51:04 +00005002 // Verify that the base decls are compatible: the RHS must be a subclass of
5003 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00005004 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00005005 return false;
Mike Stump11289f42009-09-09 15:08:12 +00005006
Chris Lattner49af6a42008-04-07 06:51:04 +00005007 // RHS must have a superset of the protocols in the LHS. If the LHS is not
5008 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00005009 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00005010 return true;
Mike Stump11289f42009-09-09 15:08:12 +00005011
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005012 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't,
5013 // more detailed analysis is required.
5014 if (RHS->getNumProtocols() == 0) {
5015 // OK, if LHS is a superclass of RHS *and*
5016 // this superclass is assignment compatible with LHS.
5017 // false otherwise.
Fariborz Jahanian240400b2011-04-12 16:34:14 +00005018 bool IsSuperClass =
5019 LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5020 if (IsSuperClass) {
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005021 // OK if conversion of LHS to SuperClass results in narrowing of types
5022 // ; i.e., SuperClass may implement at least one of the protocols
5023 // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5024 // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5025 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
Fariborz Jahanian240400b2011-04-12 16:34:14 +00005026 CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005027 // If super class has no protocols, it is not a match.
5028 if (SuperClassInheritedProtocols.empty())
5029 return false;
5030
5031 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5032 LHSPE = LHS->qual_end();
5033 LHSPI != LHSPE; LHSPI++) {
5034 bool SuperImplementsProtocol = false;
5035 ObjCProtocolDecl *LHSProto = (*LHSPI);
5036
5037 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5038 SuperClassInheritedProtocols.begin(),
5039 E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5040 ObjCProtocolDecl *SuperClassProto = (*I);
5041 if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5042 SuperImplementsProtocol = true;
5043 break;
5044 }
5045 }
5046 if (!SuperImplementsProtocol)
5047 return false;
5048 }
5049 return true;
5050 }
5051 return false;
5052 }
Mike Stump11289f42009-09-09 15:08:12 +00005053
John McCall8b07ec22010-05-15 11:32:37 +00005054 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5055 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00005056 LHSPI != LHSPE; LHSPI++) {
5057 bool RHSImplementsProtocol = false;
5058
5059 // If the RHS doesn't implement the protocol on the left, the types
5060 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00005061 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5062 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00005063 RHSPI != RHSPE; RHSPI++) {
5064 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00005065 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00005066 break;
5067 }
Steve Naroff114aecb2009-03-01 16:12:44 +00005068 }
5069 // FIXME: For better diagnostics, consider passing back the protocol name.
5070 if (!RHSImplementsProtocol)
5071 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00005072 }
Steve Naroff114aecb2009-03-01 16:12:44 +00005073 // The RHS implements all protocols listed on the LHS.
5074 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00005075}
5076
Steve Naroffb7605152009-02-12 17:52:19 +00005077bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5078 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00005079 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5080 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00005081
Steve Naroff7cae42b2009-07-10 23:34:53 +00005082 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00005083 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00005084
5085 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
5086 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00005087}
5088
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00005089bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5090 return canAssignObjCInterfaces(
5091 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5092 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5093}
5094
Mike Stump11289f42009-09-09 15:08:12 +00005095/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00005096/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00005097/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00005098/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005099bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5100 bool CompareUnqualified) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00005101 if (getLangOptions().CPlusPlus)
5102 return hasSameType(LHS, RHS);
5103
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005104 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman47f77112008-08-22 00:56:42 +00005105}
5106
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005107bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5108 return !mergeTypes(LHS, RHS, true).isNull();
5109}
5110
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005111/// mergeTransparentUnionType - if T is a transparent union type and a member
5112/// of T is compatible with SubType, return the merged type, else return
5113/// QualType()
5114QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
5115 bool OfBlockPointer,
5116 bool Unqualified) {
5117 if (const RecordType *UT = T->getAsUnionType()) {
5118 RecordDecl *UD = UT->getDecl();
5119 if (UD->hasAttr<TransparentUnionAttr>()) {
5120 for (RecordDecl::field_iterator it = UD->field_begin(),
5121 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbourne19b961d2010-12-02 21:00:06 +00005122 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005123 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
5124 if (!MT.isNull())
5125 return MT;
5126 }
5127 }
5128 }
5129
5130 return QualType();
5131}
5132
5133/// mergeFunctionArgumentTypes - merge two types which appear as function
5134/// argument types
5135QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
5136 bool OfBlockPointer,
5137 bool Unqualified) {
5138 // GNU extension: two types are compatible if they appear as a function
5139 // argument, one of the types is a transparent union type and the other
5140 // type is compatible with a union member
5141 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
5142 Unqualified);
5143 if (!lmerge.isNull())
5144 return lmerge;
5145
5146 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
5147 Unqualified);
5148 if (!rmerge.isNull())
5149 return rmerge;
5150
5151 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
5152}
5153
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005154QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005155 bool OfBlockPointer,
5156 bool Unqualified) {
John McCall9dd450b2009-09-21 23:43:11 +00005157 const FunctionType *lbase = lhs->getAs<FunctionType>();
5158 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005159 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
5160 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00005161 bool allLTypes = true;
5162 bool allRTypes = true;
5163
5164 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005165 QualType retType;
Fariborz Jahanian17825972011-02-11 18:46:17 +00005166 if (OfBlockPointer) {
5167 QualType RHS = rbase->getResultType();
5168 QualType LHS = lbase->getResultType();
5169 bool UnqualifiedResult = Unqualified;
5170 if (!UnqualifiedResult)
5171 UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005172 retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
Fariborz Jahanian17825972011-02-11 18:46:17 +00005173 }
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005174 else
John McCall8c6b56f2010-12-15 01:06:38 +00005175 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
5176 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005177 if (retType.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005178
5179 if (Unqualified)
5180 retType = retType.getUnqualifiedType();
5181
5182 CanQualType LRetType = getCanonicalType(lbase->getResultType());
5183 CanQualType RRetType = getCanonicalType(rbase->getResultType());
5184 if (Unqualified) {
5185 LRetType = LRetType.getUnqualifiedType();
5186 RRetType = RRetType.getUnqualifiedType();
5187 }
5188
5189 if (getCanonicalType(retType) != LRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00005190 allLTypes = false;
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005191 if (getCanonicalType(retType) != RRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00005192 allRTypes = false;
John McCall8c6b56f2010-12-15 01:06:38 +00005193
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00005194 // FIXME: double check this
5195 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
5196 // rbase->getRegParmAttr() != 0 &&
5197 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005198 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
5199 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
John McCall8c6b56f2010-12-15 01:06:38 +00005200
Douglas Gregor8c940862010-01-18 17:14:39 +00005201 // Compatible functions must have compatible calling conventions
John McCall8c6b56f2010-12-15 01:06:38 +00005202 if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
Douglas Gregor8c940862010-01-18 17:14:39 +00005203 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00005204
John McCall8c6b56f2010-12-15 01:06:38 +00005205 // Regparm is part of the calling convention.
Eli Friedmanc5b20b52011-04-09 08:18:08 +00005206 if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
5207 return QualType();
John McCall8c6b56f2010-12-15 01:06:38 +00005208 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
5209 return QualType();
5210
5211 // It's noreturn if either type is.
5212 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
5213 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
5214 if (NoReturn != lbaseInfo.getNoReturn())
5215 allLTypes = false;
5216 if (NoReturn != rbaseInfo.getNoReturn())
5217 allRTypes = false;
5218
5219 FunctionType::ExtInfo einfo(NoReturn,
Eli Friedmanc5b20b52011-04-09 08:18:08 +00005220 lbaseInfo.getHasRegParm(),
John McCall8c6b56f2010-12-15 01:06:38 +00005221 lbaseInfo.getRegParm(),
5222 lbaseInfo.getCC());
John McCalldb40c7f2010-12-14 08:05:40 +00005223
Eli Friedman47f77112008-08-22 00:56:42 +00005224 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00005225 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
5226 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00005227 unsigned lproto_nargs = lproto->getNumArgs();
5228 unsigned rproto_nargs = rproto->getNumArgs();
5229
5230 // Compatible functions must have the same number of arguments
5231 if (lproto_nargs != rproto_nargs)
5232 return QualType();
5233
5234 // Variadic and non-variadic functions aren't compatible
5235 if (lproto->isVariadic() != rproto->isVariadic())
5236 return QualType();
5237
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00005238 if (lproto->getTypeQuals() != rproto->getTypeQuals())
5239 return QualType();
5240
Eli Friedman47f77112008-08-22 00:56:42 +00005241 // Check argument compatibility
5242 llvm::SmallVector<QualType, 10> types;
5243 for (unsigned i = 0; i < lproto_nargs; i++) {
5244 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
5245 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005246 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
5247 OfBlockPointer,
5248 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005249 if (argtype.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005250
5251 if (Unqualified)
5252 argtype = argtype.getUnqualifiedType();
5253
Eli Friedman47f77112008-08-22 00:56:42 +00005254 types.push_back(argtype);
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005255 if (Unqualified) {
5256 largtype = largtype.getUnqualifiedType();
5257 rargtype = rargtype.getUnqualifiedType();
5258 }
5259
Chris Lattner465fa322008-10-05 17:34:18 +00005260 if (getCanonicalType(argtype) != getCanonicalType(largtype))
5261 allLTypes = false;
5262 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
5263 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00005264 }
5265 if (allLTypes) return lhs;
5266 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00005267
5268 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
5269 EPI.ExtInfo = einfo;
5270 return getFunctionType(retType, types.begin(), types.size(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00005271 }
5272
5273 if (lproto) allRTypes = false;
5274 if (rproto) allLTypes = false;
5275
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005276 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00005277 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00005278 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00005279 if (proto->isVariadic()) return QualType();
5280 // Check that the types are compatible with the types that
5281 // would result from default argument promotions (C99 6.7.5.3p15).
5282 // The only types actually affected are promotable integer
5283 // types and floats, which would be passed as a different
5284 // type depending on whether the prototype is visible.
5285 unsigned proto_nargs = proto->getNumArgs();
5286 for (unsigned i = 0; i < proto_nargs; ++i) {
5287 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00005288
5289 // Look at the promotion type of enum types, since that is the type used
5290 // to pass enum values.
5291 if (const EnumType *Enum = argTy->getAs<EnumType>())
5292 argTy = Enum->getDecl()->getPromotionType();
5293
Eli Friedman47f77112008-08-22 00:56:42 +00005294 if (argTy->isPromotableIntegerType() ||
5295 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
5296 return QualType();
5297 }
5298
5299 if (allLTypes) return lhs;
5300 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00005301
5302 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
5303 EPI.ExtInfo = einfo;
Eli Friedman47f77112008-08-22 00:56:42 +00005304 return getFunctionType(retType, proto->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005305 proto->getNumArgs(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00005306 }
5307
5308 if (allLTypes) return lhs;
5309 if (allRTypes) return rhs;
John McCall8c6b56f2010-12-15 01:06:38 +00005310 return getFunctionNoProtoType(retType, einfo);
Eli Friedman47f77112008-08-22 00:56:42 +00005311}
5312
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005313QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005314 bool OfBlockPointer,
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005315 bool Unqualified, bool BlockReturnType) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00005316 // C++ [expr]: If an expression initially has the type "reference to T", the
5317 // type is adjusted to "T" prior to any further analysis, the expression
5318 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005319 // expression is an lvalue unless the reference is an rvalue reference and
5320 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00005321 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
5322 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005323
5324 if (Unqualified) {
5325 LHS = LHS.getUnqualifiedType();
5326 RHS = RHS.getUnqualifiedType();
5327 }
Douglas Gregor21e771e2010-02-03 21:02:30 +00005328
Eli Friedman47f77112008-08-22 00:56:42 +00005329 QualType LHSCan = getCanonicalType(LHS),
5330 RHSCan = getCanonicalType(RHS);
5331
5332 // If two types are identical, they are compatible.
5333 if (LHSCan == RHSCan)
5334 return LHS;
5335
John McCall8ccfcb52009-09-24 19:53:00 +00005336 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00005337 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5338 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00005339 if (LQuals != RQuals) {
5340 // If any of these qualifiers are different, we have a type
5341 // mismatch.
5342 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5343 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5344 return QualType();
5345
5346 // Exactly one GC qualifier difference is allowed: __strong is
5347 // okay if the other type has no GC qualifier but is an Objective
5348 // C object pointer (i.e. implicitly strong by default). We fix
5349 // this by pretending that the unqualified type was actually
5350 // qualified __strong.
5351 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5352 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5353 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5354
5355 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5356 return QualType();
5357
5358 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
5359 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
5360 }
5361 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
5362 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
5363 }
Eli Friedman47f77112008-08-22 00:56:42 +00005364 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00005365 }
5366
5367 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00005368
Eli Friedmandcca6332009-06-01 01:22:52 +00005369 Type::TypeClass LHSClass = LHSCan->getTypeClass();
5370 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00005371
Chris Lattnerfd652912008-01-14 05:45:46 +00005372 // We want to consider the two function types to be the same for these
5373 // comparisons, just force one to the other.
5374 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5375 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00005376
5377 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00005378 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5379 LHSClass = Type::ConstantArray;
5380 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5381 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00005382
John McCall8b07ec22010-05-15 11:32:37 +00005383 // ObjCInterfaces are just specialized ObjCObjects.
5384 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5385 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5386
Nate Begemance4d7fc2008-04-18 23:10:10 +00005387 // Canonicalize ExtVector -> Vector.
5388 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5389 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00005390
Chris Lattner95554662008-04-07 05:43:21 +00005391 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005392 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00005393 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00005394 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00005395 // Compatibility is based on the underlying type, not the promotion
5396 // type.
John McCall9dd450b2009-09-21 23:43:11 +00005397 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005398 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5399 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005400 }
John McCall9dd450b2009-09-21 23:43:11 +00005401 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005402 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5403 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005404 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005405
Eli Friedman47f77112008-08-22 00:56:42 +00005406 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005407 }
Eli Friedman47f77112008-08-22 00:56:42 +00005408
Steve Naroffc6edcbd2008-01-09 22:43:08 +00005409 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005410 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005411#define TYPE(Class, Base)
5412#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00005413#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005414#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5415#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5416#include "clang/AST/TypeNodes.def"
5417 assert(false && "Non-canonical and dependent types shouldn't get here");
5418 return QualType();
5419
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005420 case Type::LValueReference:
5421 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005422 case Type::MemberPointer:
5423 assert(false && "C++ should never be in mergeTypes");
5424 return QualType();
5425
John McCall8b07ec22010-05-15 11:32:37 +00005426 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005427 case Type::IncompleteArray:
5428 case Type::VariableArray:
5429 case Type::FunctionProto:
5430 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005431 assert(false && "Types are eliminated above");
5432 return QualType();
5433
Chris Lattnerfd652912008-01-14 05:45:46 +00005434 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00005435 {
5436 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005437 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5438 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005439 if (Unqualified) {
5440 LHSPointee = LHSPointee.getUnqualifiedType();
5441 RHSPointee = RHSPointee.getUnqualifiedType();
5442 }
5443 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5444 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005445 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00005446 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005447 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00005448 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005449 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005450 return getPointerType(ResultType);
5451 }
Steve Naroff68e167d2008-12-10 17:49:55 +00005452 case Type::BlockPointer:
5453 {
5454 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005455 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5456 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005457 if (Unqualified) {
5458 LHSPointee = LHSPointee.getUnqualifiedType();
5459 RHSPointee = RHSPointee.getUnqualifiedType();
5460 }
5461 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5462 Unqualified);
Steve Naroff68e167d2008-12-10 17:49:55 +00005463 if (ResultType.isNull()) return QualType();
5464 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5465 return LHS;
5466 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5467 return RHS;
5468 return getBlockPointerType(ResultType);
5469 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005470 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00005471 {
5472 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5473 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5474 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5475 return QualType();
5476
5477 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5478 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005479 if (Unqualified) {
5480 LHSElem = LHSElem.getUnqualifiedType();
5481 RHSElem = RHSElem.getUnqualifiedType();
5482 }
5483
5484 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005485 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00005486 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5487 return LHS;
5488 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5489 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00005490 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5491 ArrayType::ArraySizeModifier(), 0);
5492 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5493 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005494 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5495 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00005496 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5497 return LHS;
5498 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5499 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005500 if (LVAT) {
5501 // FIXME: This isn't correct! But tricky to implement because
5502 // the array's size has to be the size of LHS, but the type
5503 // has to be different.
5504 return LHS;
5505 }
5506 if (RVAT) {
5507 // FIXME: This isn't correct! But tricky to implement because
5508 // the array's size has to be the size of RHS, but the type
5509 // has to be different.
5510 return RHS;
5511 }
Eli Friedman3e62c212008-08-22 01:48:21 +00005512 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5513 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00005514 return getIncompleteArrayType(ResultType,
5515 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005516 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005517 case Type::FunctionNoProto:
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005518 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005519 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005520 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00005521 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00005522 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005523 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00005524 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00005525 case Type::Complex:
5526 // Distinct complex types are incompatible.
5527 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005528 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00005529 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00005530 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5531 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00005532 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00005533 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00005534 case Type::ObjCObject: {
5535 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00005536 // FIXME: This should be type compatibility, e.g. whether
5537 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00005538 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5539 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5540 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00005541 return LHS;
5542
Eli Friedman47f77112008-08-22 00:56:42 +00005543 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00005544 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00005545 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005546 if (OfBlockPointer) {
5547 if (canAssignObjCInterfacesInBlockPointer(
5548 LHS->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005549 RHS->getAs<ObjCObjectPointerType>(),
5550 BlockReturnType))
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005551 return LHS;
5552 return QualType();
5553 }
John McCall9dd450b2009-09-21 23:43:11 +00005554 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5555 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00005556 return LHS;
5557
Steve Naroffc68cfcf2008-12-10 22:14:21 +00005558 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005559 }
Steve Naroff32e44c02007-10-15 20:41:53 +00005560 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005561
5562 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005563}
Ted Kremenekfc581a92007-10-31 17:10:13 +00005564
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005565/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5566/// 'RHS' attributes and returns the merged version; including for function
5567/// return types.
5568QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5569 QualType LHSCan = getCanonicalType(LHS),
5570 RHSCan = getCanonicalType(RHS);
5571 // If two types are identical, they are compatible.
5572 if (LHSCan == RHSCan)
5573 return LHS;
5574 if (RHSCan->isFunctionType()) {
5575 if (!LHSCan->isFunctionType())
5576 return QualType();
5577 QualType OldReturnType =
5578 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5579 QualType NewReturnType =
5580 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5581 QualType ResReturnType =
5582 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5583 if (ResReturnType.isNull())
5584 return QualType();
5585 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5586 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5587 // In either case, use OldReturnType to build the new function type.
5588 const FunctionType *F = LHS->getAs<FunctionType>();
5589 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
John McCalldb40c7f2010-12-14 08:05:40 +00005590 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5591 EPI.ExtInfo = getFunctionExtInfo(LHS);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005592 QualType ResultType
5593 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005594 FPT->getNumArgs(), EPI);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005595 return ResultType;
5596 }
5597 }
5598 return QualType();
5599 }
5600
5601 // If the qualifiers are different, the types can still be merged.
5602 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5603 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5604 if (LQuals != RQuals) {
5605 // If any of these qualifiers are different, we have a type mismatch.
5606 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5607 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5608 return QualType();
5609
5610 // Exactly one GC qualifier difference is allowed: __strong is
5611 // okay if the other type has no GC qualifier but is an Objective
5612 // C object pointer (i.e. implicitly strong by default). We fix
5613 // this by pretending that the unqualified type was actually
5614 // qualified __strong.
5615 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5616 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5617 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5618
5619 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5620 return QualType();
5621
5622 if (GC_L == Qualifiers::Strong)
5623 return LHS;
5624 if (GC_R == Qualifiers::Strong)
5625 return RHS;
5626 return QualType();
5627 }
5628
5629 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5630 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5631 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5632 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5633 if (ResQT == LHSBaseQT)
5634 return LHS;
5635 if (ResQT == RHSBaseQT)
5636 return RHS;
5637 }
5638 return QualType();
5639}
5640
Chris Lattner4ba0cef2008-04-07 07:01:58 +00005641//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005642// Integer Predicates
5643//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00005644
Jay Foad39c79802011-01-12 09:06:06 +00005645unsigned ASTContext::getIntWidth(QualType T) const {
John McCall424cec92011-01-19 06:33:43 +00005646 if (const EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00005647 T = ET->getDecl()->getIntegerType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00005648 if (T->isBooleanType())
5649 return 1;
Eli Friedman1efaaea2009-02-13 02:31:07 +00005650 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005651 return (unsigned)getTypeSize(T);
5652}
5653
5654QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00005655 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00005656
5657 // Turn <4 x signed int> -> <4 x unsigned int>
5658 if (const VectorType *VTy = T->getAs<VectorType>())
5659 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsonaeb56442010-11-10 21:56:12 +00005660 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattnerec3a1562009-10-17 20:33:28 +00005661
5662 // For enums, we return the unsigned version of the base type.
5663 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005664 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00005665
5666 const BuiltinType *BTy = T->getAs<BuiltinType>();
5667 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005668 switch (BTy->getKind()) {
5669 case BuiltinType::Char_S:
5670 case BuiltinType::SChar:
5671 return UnsignedCharTy;
5672 case BuiltinType::Short:
5673 return UnsignedShortTy;
5674 case BuiltinType::Int:
5675 return UnsignedIntTy;
5676 case BuiltinType::Long:
5677 return UnsignedLongTy;
5678 case BuiltinType::LongLong:
5679 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00005680 case BuiltinType::Int128:
5681 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005682 default:
5683 assert(0 && "Unexpected signed integer type");
5684 return QualType();
5685 }
5686}
5687
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005688ASTMutationListener::~ASTMutationListener() { }
5689
Chris Lattnerecd79c62009-06-14 00:45:47 +00005690
5691//===----------------------------------------------------------------------===//
5692// Builtin Type Computation
5693//===----------------------------------------------------------------------===//
5694
5695/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattnerdc226c22010-10-01 22:42:38 +00005696/// pointer over the consumed characters. This returns the resultant type. If
5697/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5698/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5699/// a vector of "i*".
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005700///
5701/// RequiresICE is filled in on return to indicate whether the value is required
5702/// to be an Integer Constant Expression.
Jay Foad39c79802011-01-12 09:06:06 +00005703static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00005704 ASTContext::GetBuiltinTypeError &Error,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005705 bool &RequiresICE,
Chris Lattnerdc226c22010-10-01 22:42:38 +00005706 bool AllowTypeModifiers) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005707 // Modifiers.
5708 int HowLong = 0;
5709 bool Signed = false, Unsigned = false;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005710 RequiresICE = false;
Chris Lattner84733392010-10-01 07:13:18 +00005711
Chris Lattnerdc226c22010-10-01 22:42:38 +00005712 // Read the prefixed modifiers first.
Chris Lattnerecd79c62009-06-14 00:45:47 +00005713 bool Done = false;
5714 while (!Done) {
5715 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00005716 default: Done = true; --Str; break;
Chris Lattner84733392010-10-01 07:13:18 +00005717 case 'I':
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005718 RequiresICE = true;
Chris Lattner84733392010-10-01 07:13:18 +00005719 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005720 case 'S':
5721 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5722 assert(!Signed && "Can't use 'S' modifier multiple times!");
5723 Signed = true;
5724 break;
5725 case 'U':
5726 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5727 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5728 Unsigned = true;
5729 break;
5730 case 'L':
5731 assert(HowLong <= 2 && "Can't have LLLL modifier");
5732 ++HowLong;
5733 break;
5734 }
5735 }
5736
5737 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005738
Chris Lattnerecd79c62009-06-14 00:45:47 +00005739 // Read the base type.
5740 switch (*Str++) {
5741 default: assert(0 && "Unknown builtin type letter!");
5742 case 'v':
5743 assert(HowLong == 0 && !Signed && !Unsigned &&
5744 "Bad modifiers used with 'v'!");
5745 Type = Context.VoidTy;
5746 break;
5747 case 'f':
5748 assert(HowLong == 0 && !Signed && !Unsigned &&
5749 "Bad modifiers used with 'f'!");
5750 Type = Context.FloatTy;
5751 break;
5752 case 'd':
5753 assert(HowLong < 2 && !Signed && !Unsigned &&
5754 "Bad modifiers used with 'd'!");
5755 if (HowLong)
5756 Type = Context.LongDoubleTy;
5757 else
5758 Type = Context.DoubleTy;
5759 break;
5760 case 's':
5761 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5762 if (Unsigned)
5763 Type = Context.UnsignedShortTy;
5764 else
5765 Type = Context.ShortTy;
5766 break;
5767 case 'i':
5768 if (HowLong == 3)
5769 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5770 else if (HowLong == 2)
5771 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5772 else if (HowLong == 1)
5773 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5774 else
5775 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5776 break;
5777 case 'c':
5778 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5779 if (Signed)
5780 Type = Context.SignedCharTy;
5781 else if (Unsigned)
5782 Type = Context.UnsignedCharTy;
5783 else
5784 Type = Context.CharTy;
5785 break;
5786 case 'b': // boolean
5787 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5788 Type = Context.BoolTy;
5789 break;
5790 case 'z': // size_t.
5791 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5792 Type = Context.getSizeType();
5793 break;
5794 case 'F':
5795 Type = Context.getCFConstantStringType();
5796 break;
Fariborz Jahaniand11da7e2010-11-09 21:38:20 +00005797 case 'G':
5798 Type = Context.getObjCIdType();
5799 break;
5800 case 'H':
5801 Type = Context.getObjCSelType();
5802 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005803 case 'a':
5804 Type = Context.getBuiltinVaListType();
5805 assert(!Type.isNull() && "builtin va list type not initialized!");
5806 break;
5807 case 'A':
5808 // This is a "reference" to a va_list; however, what exactly
5809 // this means depends on how va_list is defined. There are two
5810 // different kinds of va_list: ones passed by value, and ones
5811 // passed by reference. An example of a by-value va_list is
5812 // x86, where va_list is a char*. An example of by-ref va_list
5813 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5814 // we want this argument to be a char*&; for x86-64, we want
5815 // it to be a __va_list_tag*.
5816 Type = Context.getBuiltinVaListType();
5817 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005818 if (Type->isArrayType())
Chris Lattnerecd79c62009-06-14 00:45:47 +00005819 Type = Context.getArrayDecayedType(Type);
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005820 else
Chris Lattnerecd79c62009-06-14 00:45:47 +00005821 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005822 break;
5823 case 'V': {
5824 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005825 unsigned NumElements = strtoul(Str, &End, 10);
5826 assert(End != Str && "Missing vector size");
Chris Lattnerecd79c62009-06-14 00:45:47 +00005827 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005828
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005829 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5830 RequiresICE, false);
5831 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattnerdc226c22010-10-01 22:42:38 +00005832
5833 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner37141f42010-06-23 06:00:24 +00005834 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00005835 VectorType::GenericVector);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005836 break;
5837 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005838 case 'X': {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005839 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5840 false);
5841 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005842 Type = Context.getComplexType(ElementType);
5843 break;
5844 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005845 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005846 Type = Context.getFILEType();
5847 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005848 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005849 return QualType();
5850 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005851 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005852 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005853 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005854 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005855 else
5856 Type = Context.getjmp_bufType();
5857
Mike Stump2adb4da2009-07-28 23:47:15 +00005858 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005859 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005860 return QualType();
5861 }
5862 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005863 }
Mike Stump11289f42009-09-09 15:08:12 +00005864
Chris Lattnerdc226c22010-10-01 22:42:38 +00005865 // If there are modifiers and if we're allowed to parse them, go for it.
5866 Done = !AllowTypeModifiers;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005867 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005868 switch (char c = *Str++) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005869 default: Done = true; --Str; break;
5870 case '*':
5871 case '&': {
5872 // Both pointers and references can have their pointee types
5873 // qualified with an address space.
5874 char *End;
5875 unsigned AddrSpace = strtoul(Str, &End, 10);
5876 if (End != Str && AddrSpace != 0) {
5877 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5878 Str = End;
5879 }
5880 if (c == '*')
5881 Type = Context.getPointerType(Type);
5882 else
5883 Type = Context.getLValueReferenceType(Type);
5884 break;
5885 }
5886 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5887 case 'C':
5888 Type = Type.withConst();
5889 break;
5890 case 'D':
5891 Type = Context.getVolatileType(Type);
5892 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005893 }
5894 }
Chris Lattner84733392010-10-01 07:13:18 +00005895
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005896 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner84733392010-10-01 07:13:18 +00005897 "Integer constant 'I' type must be an integer");
Mike Stump11289f42009-09-09 15:08:12 +00005898
Chris Lattnerecd79c62009-06-14 00:45:47 +00005899 return Type;
5900}
5901
5902/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattnerdc226c22010-10-01 22:42:38 +00005903QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005904 GetBuiltinTypeError &Error,
Jay Foad39c79802011-01-12 09:06:06 +00005905 unsigned *IntegerConstantArgs) const {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005906 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump11289f42009-09-09 15:08:12 +00005907
Chris Lattnerecd79c62009-06-14 00:45:47 +00005908 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005909
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005910 bool RequiresICE = false;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005911 Error = GE_None;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005912 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5913 RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005914 if (Error != GE_None)
5915 return QualType();
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005916
5917 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5918
Chris Lattnerecd79c62009-06-14 00:45:47 +00005919 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005920 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005921 if (Error != GE_None)
5922 return QualType();
5923
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005924 // If this argument is required to be an IntegerConstantExpression and the
5925 // caller cares, fill in the bitmask we return.
5926 if (RequiresICE && IntegerConstantArgs)
5927 *IntegerConstantArgs |= 1 << ArgTypes.size();
5928
Chris Lattnerecd79c62009-06-14 00:45:47 +00005929 // Do array -> pointer decay. The builtin should use the decayed type.
5930 if (Ty->isArrayType())
5931 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005932
Chris Lattnerecd79c62009-06-14 00:45:47 +00005933 ArgTypes.push_back(Ty);
5934 }
5935
5936 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5937 "'.' should only occur at end of builtin type list!");
5938
John McCall991eb4b2010-12-21 00:44:39 +00005939 FunctionType::ExtInfo EI;
5940 if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
5941
5942 bool Variadic = (TypeStr[0] == '.');
5943
5944 // We really shouldn't be making a no-proto type here, especially in C++.
5945 if (ArgTypes.empty() && Variadic)
5946 return getFunctionNoProtoType(ResType, EI);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005947
John McCalldb40c7f2010-12-14 08:05:40 +00005948 FunctionProtoType::ExtProtoInfo EPI;
John McCall991eb4b2010-12-21 00:44:39 +00005949 EPI.ExtInfo = EI;
5950 EPI.Variadic = Variadic;
John McCalldb40c7f2010-12-14 08:05:40 +00005951
5952 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005953}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005954
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005955GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5956 GVALinkage External = GVA_StrongExternal;
5957
5958 Linkage L = FD->getLinkage();
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005959 switch (L) {
5960 case NoLinkage:
5961 case InternalLinkage:
5962 case UniqueExternalLinkage:
5963 return GVA_Internal;
5964
5965 case ExternalLinkage:
5966 switch (FD->getTemplateSpecializationKind()) {
5967 case TSK_Undeclared:
5968 case TSK_ExplicitSpecialization:
5969 External = GVA_StrongExternal;
5970 break;
5971
5972 case TSK_ExplicitInstantiationDefinition:
5973 return GVA_ExplicitTemplateInstantiation;
5974
5975 case TSK_ExplicitInstantiationDeclaration:
5976 case TSK_ImplicitInstantiation:
5977 External = GVA_TemplateInstantiation;
5978 break;
5979 }
5980 }
5981
5982 if (!FD->isInlined())
5983 return External;
5984
5985 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5986 // GNU or C99 inline semantics. Determine whether this symbol should be
5987 // externally visible.
5988 if (FD->isInlineDefinitionExternallyVisible())
5989 return External;
5990
5991 // C99 inline semantics, where the symbol is not externally visible.
5992 return GVA_C99Inline;
5993 }
5994
5995 // C++0x [temp.explicit]p9:
5996 // [ Note: The intent is that an inline function that is the subject of
5997 // an explicit instantiation declaration will still be implicitly
5998 // instantiated when used so that the body can be considered for
5999 // inlining, but that no out-of-line copy of the inline function would be
6000 // generated in the translation unit. -- end note ]
6001 if (FD->getTemplateSpecializationKind()
6002 == TSK_ExplicitInstantiationDeclaration)
6003 return GVA_C99Inline;
6004
6005 return GVA_CXXInline;
6006}
6007
6008GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
6009 // If this is a static data member, compute the kind of template
6010 // specialization. Otherwise, this variable is not part of a
6011 // template.
6012 TemplateSpecializationKind TSK = TSK_Undeclared;
6013 if (VD->isStaticDataMember())
6014 TSK = VD->getTemplateSpecializationKind();
6015
6016 Linkage L = VD->getLinkage();
6017 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
6018 VD->getType()->getLinkage() == UniqueExternalLinkage)
6019 L = UniqueExternalLinkage;
6020
6021 switch (L) {
6022 case NoLinkage:
6023 case InternalLinkage:
6024 case UniqueExternalLinkage:
6025 return GVA_Internal;
6026
6027 case ExternalLinkage:
6028 switch (TSK) {
6029 case TSK_Undeclared:
6030 case TSK_ExplicitSpecialization:
6031 return GVA_StrongExternal;
6032
6033 case TSK_ExplicitInstantiationDeclaration:
6034 llvm_unreachable("Variable should not be instantiated");
6035 // Fall through to treat this like any other instantiation.
6036
6037 case TSK_ExplicitInstantiationDefinition:
6038 return GVA_ExplicitTemplateInstantiation;
6039
6040 case TSK_ImplicitInstantiation:
6041 return GVA_TemplateInstantiation;
6042 }
6043 }
6044
6045 return GVA_StrongExternal;
6046}
6047
Argyrios Kyrtzidisc9049332010-07-29 20:08:05 +00006048bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006049 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
6050 if (!VD->isFileVarDecl())
6051 return false;
6052 } else if (!isa<FunctionDecl>(D))
6053 return false;
6054
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00006055 // Weak references don't produce any output by themselves.
6056 if (D->hasAttr<WeakRefAttr>())
6057 return false;
6058
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006059 // Aliases and used decls are required.
6060 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
6061 return true;
6062
6063 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
6064 // Forward declarations aren't required.
6065 if (!FD->isThisDeclarationADefinition())
6066 return false;
6067
6068 // Constructors and destructors are required.
6069 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
6070 return true;
6071
6072 // The key function for a class is required.
6073 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
6074 const CXXRecordDecl *RD = MD->getParent();
6075 if (MD->isOutOfLine() && RD->isDynamicClass()) {
6076 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
6077 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
6078 return true;
6079 }
6080 }
6081
6082 GVALinkage Linkage = GetGVALinkageForFunction(FD);
6083
6084 // static, static inline, always_inline, and extern inline functions can
6085 // always be deferred. Normal inline functions can be deferred in C99/C++.
6086 // Implicit template instantiations can also be deferred in C++.
6087 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
6088 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
6089 return false;
6090 return true;
6091 }
6092
6093 const VarDecl *VD = cast<VarDecl>(D);
6094 assert(VD->isFileVarDecl() && "Expected file scoped var");
6095
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00006096 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
6097 return false;
6098
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006099 // Structs that have non-trivial constructors or destructors are required.
6100
6101 // FIXME: Handle references.
6102 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
6103 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +00006104 if (RD->hasDefinition() &&
6105 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006106 return true;
6107 }
6108 }
6109
6110 GVALinkage L = GetGVALinkageForVariable(VD);
6111 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
6112 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
6113 return false;
6114 }
6115
6116 return true;
6117}
Charles Davis53c59df2010-08-16 03:33:14 +00006118
Charles Davis99202b32010-11-09 18:04:24 +00006119CallingConv ASTContext::getDefaultMethodCallConv() {
6120 // Pass through to the C++ ABI object
6121 return ABI->getDefaultMethodCallConv();
6122}
6123
Jay Foad39c79802011-01-12 09:06:06 +00006124bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
Anders Carlsson60a62632010-11-25 01:51:53 +00006125 // Pass through to the C++ ABI object
6126 return ABI->isNearlyEmpty(RD);
6127}
6128
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00006129MangleContext *ASTContext::createMangleContext() {
6130 switch (Target.getCXXABI()) {
6131 case CXXABI_ARM:
6132 case CXXABI_Itanium:
6133 return createItaniumMangleContext(*this, getDiagnostics());
6134 case CXXABI_Microsoft:
6135 return createMicrosoftMangleContext(*this, getDiagnostics());
6136 }
6137 assert(0 && "Unsupported ABI");
6138 return 0;
6139}
6140
Charles Davis53c59df2010-08-16 03:33:14 +00006141CXXABI::~CXXABI() {}
Ted Kremenekf5df0ce2011-04-28 04:53:38 +00006142
6143size_t ASTContext::getSideTableAllocatedMemory() const {
6144 size_t bytes = 0;
6145 bytes += ASTRecordLayouts.getMemorySize();
6146 bytes += ObjCLayouts.getMemorySize();
6147 bytes += KeyFunctions.getMemorySize();
6148 bytes += ObjCImpls.getMemorySize();
6149 bytes += BlockVarCopyInits.getMemorySize();
6150 bytes += DeclAttrs.getMemorySize();
6151 bytes += InstantiatedFromStaticDataMember.getMemorySize();
6152 bytes += InstantiatedFromUsingDecl.getMemorySize();
6153 bytes += InstantiatedFromUsingShadowDecl.getMemorySize();
6154 bytes += InstantiatedFromUnnamedFieldDecl.getMemorySize();
6155 return bytes;
6156}
6157