blob: 1f4d9a3718653ec63bcfa2e6d60d61940da39580 [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
Eli Friedman786d0872011-04-30 19:24:24 +0000378 if (TargetInfo::isTypeSigned(Target.getWCharType()))
Chris Lattnerad3467e2010-12-25 23:25:43 +0000379 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,
Chandler Carruth08836322011-05-01 00:51:33 +00002199 TemplateTypeParmDecl *TTPDecl) const {
Douglas Gregoreff93e02009-02-05 23:33:38 +00002200 llvm::FoldingSetNodeID ID;
Chandler Carruth08836322011-05-01 00:51:33 +00002201 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
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
Chandler Carruth08836322011-05-01 00:51:33 +00002209 if (TTPDecl) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00002210 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Chandler Carruth08836322011-05-01 00:51:33 +00002211 TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002212
2213 TemplateTypeParmType *TypeCheck
2214 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2215 assert(!TypeCheck && "Template type parameter canonical type broken");
2216 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00002217 } else
John McCall90d1c2d2009-09-24 23:30:46 +00002218 TypeParm = new (*this, TypeAlignment)
2219 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00002220
2221 Types.push_back(TypeParm);
2222 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2223
2224 return QualType(TypeParm, 0);
2225}
2226
John McCalle78aac42010-03-10 03:28:59 +00002227TypeSourceInfo *
2228ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
2229 SourceLocation NameLoc,
2230 const TemplateArgumentListInfo &Args,
Jay Foad39c79802011-01-12 09:06:06 +00002231 QualType CanonType) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002232 assert(!Name.getAsDependentTemplateName() &&
2233 "No dependent template names here!");
John McCalle78aac42010-03-10 03:28:59 +00002234 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
2235
2236 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
2237 TemplateSpecializationTypeLoc TL
2238 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
2239 TL.setTemplateNameLoc(NameLoc);
2240 TL.setLAngleLoc(Args.getLAngleLoc());
2241 TL.setRAngleLoc(Args.getRAngleLoc());
2242 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2243 TL.setArgLocInfo(i, Args[i].getLocInfo());
2244 return DI;
2245}
2246
Mike Stump11289f42009-09-09 15:08:12 +00002247QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00002248ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00002249 const TemplateArgumentListInfo &Args,
Jay Foad39c79802011-01-12 09:06:06 +00002250 QualType Canon) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002251 assert(!Template.getAsDependentTemplateName() &&
2252 "No dependent template names here!");
2253
John McCall6b51f282009-11-23 01:53:49 +00002254 unsigned NumArgs = Args.size();
2255
John McCall0ad16662009-10-29 08:12:44 +00002256 llvm::SmallVector<TemplateArgument, 4> ArgVec;
2257 ArgVec.reserve(NumArgs);
2258 for (unsigned i = 0; i != NumArgs; ++i)
2259 ArgVec.push_back(Args[i].getArgument());
2260
John McCall2408e322010-04-27 00:57:59 +00002261 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00002262 Canon);
John McCall0ad16662009-10-29 08:12:44 +00002263}
2264
2265QualType
2266ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00002267 const TemplateArgument *Args,
2268 unsigned NumArgs,
Jay Foad39c79802011-01-12 09:06:06 +00002269 QualType Canon) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002270 assert(!Template.getAsDependentTemplateName() &&
2271 "No dependent template names here!");
Douglas Gregore29139c2011-03-03 17:04:51 +00002272 // Look through qualified template names.
2273 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2274 Template = TemplateName(QTN->getTemplateDecl());
Douglas Gregore16af532011-02-28 18:50:33 +00002275
Douglas Gregor15301382009-07-30 17:40:51 +00002276 if (!Canon.isNull())
2277 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002278 else
2279 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregord56a91e2009-02-26 22:19:44 +00002280
Douglas Gregora8e02e72009-07-28 23:00:59 +00002281 // Allocate the (non-canonical) template specialization type, but don't
2282 // try to unique it: these types typically have location information that
2283 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00002284 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00002285 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00002286 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00002287 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00002288 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00002289 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00002290 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00002291
Douglas Gregor8bf42052009-02-09 18:46:07 +00002292 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00002293 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002294}
2295
Mike Stump11289f42009-09-09 15:08:12 +00002296QualType
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002297ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2298 const TemplateArgument *Args,
Jay Foad39c79802011-01-12 09:06:06 +00002299 unsigned NumArgs) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002300 assert(!Template.getAsDependentTemplateName() &&
2301 "No dependent template names here!");
Douglas Gregore29139c2011-03-03 17:04:51 +00002302 // Look through qualified template names.
2303 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2304 Template = TemplateName(QTN->getTemplateDecl());
Douglas Gregore16af532011-02-28 18:50:33 +00002305
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002306 // Build the canonical template specialization type.
2307 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2308 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2309 CanonArgs.reserve(NumArgs);
2310 for (unsigned I = 0; I != NumArgs; ++I)
2311 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2312
2313 // Determine whether this canonical template specialization type already
2314 // exists.
2315 llvm::FoldingSetNodeID ID;
2316 TemplateSpecializationType::Profile(ID, CanonTemplate,
2317 CanonArgs.data(), NumArgs, *this);
2318
2319 void *InsertPos = 0;
2320 TemplateSpecializationType *Spec
2321 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2322
2323 if (!Spec) {
2324 // Allocate a new canonical template specialization type.
2325 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2326 sizeof(TemplateArgument) * NumArgs),
2327 TypeAlignment);
2328 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2329 CanonArgs.data(), NumArgs,
2330 QualType());
2331 Types.push_back(Spec);
2332 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2333 }
2334
2335 assert(Spec->isDependentType() &&
2336 "Non-dependent template-id type must have a canonical type");
2337 return QualType(Spec, 0);
2338}
2339
2340QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00002341ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2342 NestedNameSpecifier *NNS,
Jay Foad39c79802011-01-12 09:06:06 +00002343 QualType NamedType) const {
Douglas Gregor52537682009-03-19 00:18:19 +00002344 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002345 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00002346
2347 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002348 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002349 if (T)
2350 return QualType(T, 0);
2351
Douglas Gregorc42075a2010-02-04 18:10:26 +00002352 QualType Canon = NamedType;
2353 if (!Canon.isCanonical()) {
2354 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002355 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2356 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00002357 (void)CheckT;
2358 }
2359
Abramo Bagnara6150c882010-05-11 21:36:43 +00002360 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00002361 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002362 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002363 return QualType(T, 0);
2364}
2365
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002366QualType
Jay Foad39c79802011-01-12 09:06:06 +00002367ASTContext::getParenType(QualType InnerType) const {
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002368 llvm::FoldingSetNodeID ID;
2369 ParenType::Profile(ID, InnerType);
2370
2371 void *InsertPos = 0;
2372 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2373 if (T)
2374 return QualType(T, 0);
2375
2376 QualType Canon = InnerType;
2377 if (!Canon.isCanonical()) {
2378 Canon = getCanonicalType(InnerType);
2379 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2380 assert(!CheckT && "Paren canonical type broken");
2381 (void)CheckT;
2382 }
2383
2384 T = new (*this) ParenType(InnerType, Canon);
2385 Types.push_back(T);
2386 ParenTypes.InsertNode(T, InsertPos);
2387 return QualType(T, 0);
2388}
2389
Douglas Gregor02085352010-03-31 20:19:30 +00002390QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2391 NestedNameSpecifier *NNS,
2392 const IdentifierInfo *Name,
Jay Foad39c79802011-01-12 09:06:06 +00002393 QualType Canon) const {
Douglas Gregor333489b2009-03-27 23:10:48 +00002394 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2395
2396 if (Canon.isNull()) {
2397 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00002398 ElaboratedTypeKeyword CanonKeyword = Keyword;
2399 if (Keyword == ETK_None)
2400 CanonKeyword = ETK_Typename;
2401
2402 if (CanonNNS != NNS || CanonKeyword != Keyword)
2403 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002404 }
2405
2406 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002407 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002408
2409 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002410 DependentNameType *T
2411 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00002412 if (T)
2413 return QualType(T, 0);
2414
Douglas Gregor02085352010-03-31 20:19:30 +00002415 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002416 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002417 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002418 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002419}
2420
Mike Stump11289f42009-09-09 15:08:12 +00002421QualType
John McCallc392f372010-06-11 00:33:02 +00002422ASTContext::getDependentTemplateSpecializationType(
2423 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002424 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002425 const IdentifierInfo *Name,
Jay Foad39c79802011-01-12 09:06:06 +00002426 const TemplateArgumentListInfo &Args) const {
John McCallc392f372010-06-11 00:33:02 +00002427 // TODO: avoid this copy
2428 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2429 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2430 ArgCopy.push_back(Args[I].getArgument());
2431 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2432 ArgCopy.size(),
2433 ArgCopy.data());
2434}
2435
2436QualType
2437ASTContext::getDependentTemplateSpecializationType(
2438 ElaboratedTypeKeyword Keyword,
2439 NestedNameSpecifier *NNS,
2440 const IdentifierInfo *Name,
2441 unsigned NumArgs,
Jay Foad39c79802011-01-12 09:06:06 +00002442 const TemplateArgument *Args) const {
Douglas Gregor6e068012011-02-28 00:04:36 +00002443 assert((!NNS || NNS->isDependent()) &&
2444 "nested-name-specifier must be dependent");
Douglas Gregordce2b622009-04-01 00:28:59 +00002445
Douglas Gregorc42075a2010-02-04 18:10:26 +00002446 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002447 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2448 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002449
2450 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002451 DependentTemplateSpecializationType *T
2452 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002453 if (T)
2454 return QualType(T, 0);
2455
John McCallc392f372010-06-11 00:33:02 +00002456 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002457
John McCallc392f372010-06-11 00:33:02 +00002458 ElaboratedTypeKeyword CanonKeyword = Keyword;
2459 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2460
2461 bool AnyNonCanonArgs = false;
2462 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2463 for (unsigned I = 0; I != NumArgs; ++I) {
2464 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2465 if (!CanonArgs[I].structurallyEquals(Args[I]))
2466 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002467 }
2468
John McCallc392f372010-06-11 00:33:02 +00002469 QualType Canon;
2470 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2471 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2472 Name, NumArgs,
2473 CanonArgs.data());
2474
2475 // Find the insert position again.
2476 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2477 }
2478
2479 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2480 sizeof(TemplateArgument) * NumArgs),
2481 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002482 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002483 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002484 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002485 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002486 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002487}
2488
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002489QualType ASTContext::getPackExpansionType(QualType Pattern,
2490 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00002491 llvm::FoldingSetNodeID ID;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002492 PackExpansionType::Profile(ID, Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002493
2494 assert(Pattern->containsUnexpandedParameterPack() &&
2495 "Pack expansions must expand one or more parameter packs");
2496 void *InsertPos = 0;
2497 PackExpansionType *T
2498 = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2499 if (T)
2500 return QualType(T, 0);
2501
2502 QualType Canon;
2503 if (!Pattern.isCanonical()) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002504 Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002505
2506 // Find the insert position again.
2507 PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2508 }
2509
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002510 T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002511 Types.push_back(T);
2512 PackExpansionTypes.InsertNode(T, InsertPos);
2513 return QualType(T, 0);
2514}
2515
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002516/// CmpProtocolNames - Comparison predicate for sorting protocols
2517/// alphabetically.
2518static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2519 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002520 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002521}
2522
John McCall8b07ec22010-05-15 11:32:37 +00002523static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002524 unsigned NumProtocols) {
2525 if (NumProtocols == 0) return true;
2526
2527 for (unsigned i = 1; i != NumProtocols; ++i)
2528 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2529 return false;
2530 return true;
2531}
2532
2533static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002534 unsigned &NumProtocols) {
2535 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002536
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002537 // Sort protocols, keyed by name.
2538 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2539
2540 // Remove duplicates.
2541 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2542 NumProtocols = ProtocolsEnd-Protocols;
2543}
2544
John McCall8b07ec22010-05-15 11:32:37 +00002545QualType ASTContext::getObjCObjectType(QualType BaseType,
2546 ObjCProtocolDecl * const *Protocols,
Jay Foad39c79802011-01-12 09:06:06 +00002547 unsigned NumProtocols) const {
John McCall8b07ec22010-05-15 11:32:37 +00002548 // If the base type is an interface and there aren't any protocols
2549 // to add, then the interface type will do just fine.
2550 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2551 return BaseType;
2552
2553 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002554 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002555 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002556 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002557 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2558 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002559
John McCall8b07ec22010-05-15 11:32:37 +00002560 // Build the canonical type, which has the canonical base type and
2561 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002562 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002563 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2564 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2565 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002566 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2567 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002568 unsigned UniqueCount = NumProtocols;
2569
John McCallfc93cf92009-10-22 22:37:11 +00002570 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002571 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2572 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002573 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002574 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2575 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002576 }
2577
2578 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002579 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2580 }
2581
2582 unsigned Size = sizeof(ObjCObjectTypeImpl);
2583 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2584 void *Mem = Allocate(Size, TypeAlignment);
2585 ObjCObjectTypeImpl *T =
2586 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2587
2588 Types.push_back(T);
2589 ObjCObjectTypes.InsertNode(T, InsertPos);
2590 return QualType(T, 0);
2591}
2592
2593/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2594/// the given object type.
Jay Foad39c79802011-01-12 09:06:06 +00002595QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
John McCall8b07ec22010-05-15 11:32:37 +00002596 llvm::FoldingSetNodeID ID;
2597 ObjCObjectPointerType::Profile(ID, ObjectT);
2598
2599 void *InsertPos = 0;
2600 if (ObjCObjectPointerType *QT =
2601 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2602 return QualType(QT, 0);
2603
2604 // Find the canonical object type.
2605 QualType Canonical;
2606 if (!ObjectT.isCanonical()) {
2607 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2608
2609 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002610 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2611 }
2612
Douglas Gregorf85bee62010-02-08 22:59:26 +00002613 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002614 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2615 ObjCObjectPointerType *QType =
2616 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002617
Steve Narofffb4330f2009-06-17 22:40:22 +00002618 Types.push_back(QType);
2619 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002620 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002621}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002622
Douglas Gregor1c283312010-08-11 12:19:30 +00002623/// getObjCInterfaceType - Return the unique reference to the type for the
2624/// specified ObjC interface decl. The list of protocols is optional.
Jay Foad39c79802011-01-12 09:06:06 +00002625QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const {
Douglas Gregor1c283312010-08-11 12:19:30 +00002626 if (Decl->TypeForDecl)
2627 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002628
Douglas Gregor1c283312010-08-11 12:19:30 +00002629 // FIXME: redeclarations?
2630 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2631 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2632 Decl->TypeForDecl = T;
2633 Types.push_back(T);
2634 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002635}
2636
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002637/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2638/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002639/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002640/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002641/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002642QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
Douglas Gregorabd68132009-07-08 00:03:05 +00002643 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002644 if (tofExpr->isTypeDependent()) {
2645 llvm::FoldingSetNodeID ID;
2646 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002647
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002648 void *InsertPos = 0;
2649 DependentTypeOfExprType *Canon
2650 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2651 if (Canon) {
2652 // We already have a "canonical" version of an identical, dependent
2653 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002654 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002655 QualType((TypeOfExprType*)Canon, 0));
2656 }
2657 else {
2658 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002659 Canon
2660 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002661 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2662 toe = Canon;
2663 }
2664 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002665 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002666 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002667 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002668 Types.push_back(toe);
2669 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002670}
2671
Steve Naroffa773cd52007-08-01 18:02:17 +00002672/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2673/// TypeOfType AST's. The only motivation to unique these nodes would be
2674/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002675/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002676/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002677QualType ASTContext::getTypeOfType(QualType tofType) const {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002678 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002679 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002680 Types.push_back(tot);
2681 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002682}
2683
Anders Carlssonad6bd352009-06-24 21:24:56 +00002684/// getDecltypeForExpr - Given an expr, will return the decltype for that
2685/// expression, according to the rules in C++0x [dcl.type.simple]p4
Jay Foad39c79802011-01-12 09:06:06 +00002686static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002687 if (e->isTypeDependent())
2688 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002689
Anders Carlssonad6bd352009-06-24 21:24:56 +00002690 // If e is an id expression or a class member access, decltype(e) is defined
2691 // as the type of the entity named by e.
2692 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2693 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2694 return VD->getType();
2695 }
2696 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2697 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2698 return FD->getType();
2699 }
2700 // If e is a function call or an invocation of an overloaded operator,
2701 // (parentheses around e are ignored), decltype(e) is defined as the
2702 // return type of that function.
2703 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2704 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002705
Anders Carlssonad6bd352009-06-24 21:24:56 +00002706 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002707
2708 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002709 // defined as T&, otherwise decltype(e) is defined as T.
John McCall086a4642010-11-24 05:12:34 +00002710 if (e->isLValue())
Anders Carlssonad6bd352009-06-24 21:24:56 +00002711 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002712
Anders Carlssonad6bd352009-06-24 21:24:56 +00002713 return T;
2714}
2715
Anders Carlsson81df7b82009-06-24 19:06:50 +00002716/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2717/// DecltypeType AST's. The only motivation to unique these nodes would be
2718/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002719/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002720/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002721QualType ASTContext::getDecltypeType(Expr *e) const {
Douglas Gregorabd68132009-07-08 00:03:05 +00002722 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002723 if (e->isTypeDependent()) {
2724 llvm::FoldingSetNodeID ID;
2725 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002726
Douglas Gregora21f6c32009-07-30 23:36:40 +00002727 void *InsertPos = 0;
2728 DependentDecltypeType *Canon
2729 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2730 if (Canon) {
2731 // We already have a "canonical" version of an equivalent, dependent
2732 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002733 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002734 QualType((DecltypeType*)Canon, 0));
2735 }
2736 else {
2737 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002738 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002739 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2740 dt = Canon;
2741 }
2742 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002743 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002744 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002745 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002746 Types.push_back(dt);
2747 return QualType(dt, 0);
2748}
2749
Richard Smithb2bc2e62011-02-21 20:05:19 +00002750/// getAutoType - We only unique auto types after they've been deduced.
Richard Smith30482bc2011-02-20 03:19:35 +00002751QualType ASTContext::getAutoType(QualType DeducedType) const {
Richard Smithb2bc2e62011-02-21 20:05:19 +00002752 void *InsertPos = 0;
2753 if (!DeducedType.isNull()) {
2754 // Look in the folding set for an existing type.
2755 llvm::FoldingSetNodeID ID;
2756 AutoType::Profile(ID, DeducedType);
2757 if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
2758 return QualType(AT, 0);
2759 }
2760
2761 AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
2762 Types.push_back(AT);
2763 if (InsertPos)
2764 AutoTypes.InsertNode(AT, InsertPos);
2765 return QualType(AT, 0);
Richard Smith30482bc2011-02-20 03:19:35 +00002766}
2767
Richard Smith02e85f32011-04-14 22:09:26 +00002768/// getAutoDeductType - Get type pattern for deducing against 'auto'.
2769QualType ASTContext::getAutoDeductType() const {
2770 if (AutoDeductTy.isNull())
2771 AutoDeductTy = getAutoType(QualType());
2772 assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
2773 return AutoDeductTy;
2774}
2775
2776/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
2777QualType ASTContext::getAutoRRefDeductType() const {
2778 if (AutoRRefDeductTy.isNull())
2779 AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
2780 assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
2781 return AutoRRefDeductTy;
2782}
2783
Chris Lattnerfb072462007-01-23 05:45:31 +00002784/// getTagDeclType - Return the unique reference to the type for the
2785/// specified TagDecl (struct/union/class/enum) decl.
Jay Foad39c79802011-01-12 09:06:06 +00002786QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002787 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002788 // FIXME: What is the design on getTagDeclType when it requires casting
2789 // away const? mutable?
2790 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002791}
2792
Mike Stump11289f42009-09-09 15:08:12 +00002793/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2794/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2795/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002796CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002797 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002798}
Chris Lattnerfb072462007-01-23 05:45:31 +00002799
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002800/// getSignedWCharType - Return the type of "signed wchar_t".
2801/// Used when in C++, as a GCC extension.
2802QualType ASTContext::getSignedWCharType() const {
2803 // FIXME: derive from "Target" ?
2804 return WCharTy;
2805}
2806
2807/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2808/// Used when in C++, as a GCC extension.
2809QualType ASTContext::getUnsignedWCharType() const {
2810 // FIXME: derive from "Target" ?
2811 return UnsignedIntTy;
2812}
2813
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002814/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2815/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2816QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002817 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002818}
2819
Chris Lattnera21ad802008-04-02 05:18:44 +00002820//===----------------------------------------------------------------------===//
2821// Type Operators
2822//===----------------------------------------------------------------------===//
2823
Jay Foad39c79802011-01-12 09:06:06 +00002824CanQualType ASTContext::getCanonicalParamType(QualType T) const {
John McCallfc93cf92009-10-22 22:37:11 +00002825 // Push qualifiers into arrays, and then discard any remaining
2826 // qualifiers.
2827 T = getCanonicalType(T);
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00002828 T = getVariableArrayDecayedType(T);
John McCallfc93cf92009-10-22 22:37:11 +00002829 const Type *Ty = T.getTypePtr();
John McCallfc93cf92009-10-22 22:37:11 +00002830 QualType Result;
2831 if (isa<ArrayType>(Ty)) {
2832 Result = getArrayDecayedType(QualType(Ty,0));
2833 } else if (isa<FunctionType>(Ty)) {
2834 Result = getPointerType(QualType(Ty, 0));
2835 } else {
2836 Result = QualType(Ty, 0);
2837 }
2838
2839 return CanQualType::CreateUnsafe(Result);
2840}
2841
Chris Lattner7adf0762008-08-04 07:31:14 +00002842
John McCall6c9dd522011-01-18 07:41:22 +00002843QualType ASTContext::getUnqualifiedArrayType(QualType type,
2844 Qualifiers &quals) {
2845 SplitQualType splitType = type.getSplitUnqualifiedType();
2846
2847 // FIXME: getSplitUnqualifiedType() actually walks all the way to
2848 // the unqualified desugared type and then drops it on the floor.
2849 // We then have to strip that sugar back off with
2850 // getUnqualifiedDesugaredType(), which is silly.
2851 const ArrayType *AT =
2852 dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType());
2853
2854 // If we don't have an array, just use the results in splitType.
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002855 if (!AT) {
John McCall6c9dd522011-01-18 07:41:22 +00002856 quals = splitType.second;
2857 return QualType(splitType.first, 0);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002858 }
2859
John McCall6c9dd522011-01-18 07:41:22 +00002860 // Otherwise, recurse on the array's element type.
2861 QualType elementType = AT->getElementType();
2862 QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
2863
2864 // If that didn't change the element type, AT has no qualifiers, so we
2865 // can just use the results in splitType.
2866 if (elementType == unqualElementType) {
2867 assert(quals.empty()); // from the recursive call
2868 quals = splitType.second;
2869 return QualType(splitType.first, 0);
2870 }
2871
2872 // Otherwise, add in the qualifiers from the outermost type, then
2873 // build the type back up.
2874 quals.addConsistentQualifiers(splitType.second);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002875
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002876 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002877 return getConstantArrayType(unqualElementType, CAT->getSize(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002878 CAT->getSizeModifier(), 0);
2879 }
2880
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002881 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002882 return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002883 }
2884
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002885 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002886 return getVariableArrayType(unqualElementType,
John McCallc3007a22010-10-26 07:05:15 +00002887 VAT->getSizeExpr(),
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002888 VAT->getSizeModifier(),
2889 VAT->getIndexTypeCVRQualifiers(),
2890 VAT->getBracketsRange());
2891 }
2892
2893 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCall6c9dd522011-01-18 07:41:22 +00002894 return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002895 DSAT->getSizeModifier(), 0,
2896 SourceRange());
2897}
2898
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002899/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2900/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2901/// they point to and return true. If T1 and T2 aren't pointer types
2902/// or pointer-to-member types, or if they are not similar at this
2903/// level, returns false and leaves T1 and T2 unchanged. Top-level
2904/// qualifiers on T1 and T2 are ignored. This function will typically
2905/// be called in a loop that successively "unwraps" pointer and
2906/// pointer-to-member types to compare them at each level.
2907bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2908 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2909 *T2PtrType = T2->getAs<PointerType>();
2910 if (T1PtrType && T2PtrType) {
2911 T1 = T1PtrType->getPointeeType();
2912 T2 = T2PtrType->getPointeeType();
2913 return true;
2914 }
2915
2916 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2917 *T2MPType = T2->getAs<MemberPointerType>();
2918 if (T1MPType && T2MPType &&
2919 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2920 QualType(T2MPType->getClass(), 0))) {
2921 T1 = T1MPType->getPointeeType();
2922 T2 = T2MPType->getPointeeType();
2923 return true;
2924 }
2925
2926 if (getLangOptions().ObjC1) {
2927 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2928 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2929 if (T1OPType && T2OPType) {
2930 T1 = T1OPType->getPointeeType();
2931 T2 = T2OPType->getPointeeType();
2932 return true;
2933 }
2934 }
2935
2936 // FIXME: Block pointers, too?
2937
2938 return false;
2939}
2940
Jay Foad39c79802011-01-12 09:06:06 +00002941DeclarationNameInfo
2942ASTContext::getNameForTemplate(TemplateName Name,
2943 SourceLocation NameLoc) const {
John McCall847e2a12009-11-24 18:42:40 +00002944 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002945 // DNInfo work in progress: CHECKME: what about DNLoc?
2946 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2947
John McCall847e2a12009-11-24 18:42:40 +00002948 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002949 DeclarationName DName;
John McCall847e2a12009-11-24 18:42:40 +00002950 if (DTN->isIdentifier()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002951 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2952 return DeclarationNameInfo(DName, NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002953 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002954 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2955 // DNInfo work in progress: FIXME: source locations?
2956 DeclarationNameLoc DNLoc;
2957 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2958 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2959 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall847e2a12009-11-24 18:42:40 +00002960 }
2961 }
2962
John McCalld28ae272009-12-02 08:04:21 +00002963 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2964 assert(Storage);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002965 // DNInfo work in progress: CHECKME: what about DNLoc?
2966 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002967}
2968
Jay Foad39c79802011-01-12 09:06:06 +00002969TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002970 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2971 if (TemplateTemplateParmDecl *TTP
2972 = dyn_cast<TemplateTemplateParmDecl>(Template))
2973 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2974
2975 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002976 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002977 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002978
Douglas Gregor5590be02011-01-15 06:45:20 +00002979 if (SubstTemplateTemplateParmPackStorage *SubstPack
2980 = Name.getAsSubstTemplateTemplateParmPack()) {
2981 TemplateTemplateParmDecl *CanonParam
2982 = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack());
2983 TemplateArgument CanonArgPack
2984 = getCanonicalTemplateArgument(SubstPack->getArgumentPack());
2985 return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack);
2986 }
2987
John McCalld28ae272009-12-02 08:04:21 +00002988 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002989
Douglas Gregor6bc50582009-05-07 06:41:52 +00002990 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2991 assert(DTN && "Non-dependent template names must refer to template decls.");
2992 return DTN->CanonicalTemplateName;
2993}
2994
Douglas Gregoradee3e32009-11-11 23:06:43 +00002995bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2996 X = getCanonicalTemplateName(X);
2997 Y = getCanonicalTemplateName(Y);
2998 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2999}
3000
Mike Stump11289f42009-09-09 15:08:12 +00003001TemplateArgument
Jay Foad39c79802011-01-12 09:06:06 +00003002ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
Douglas Gregora8e02e72009-07-28 23:00:59 +00003003 switch (Arg.getKind()) {
3004 case TemplateArgument::Null:
3005 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00003006
Douglas Gregora8e02e72009-07-28 23:00:59 +00003007 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00003008 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00003009
Douglas Gregora8e02e72009-07-28 23:00:59 +00003010 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00003011 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00003012
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003013 case TemplateArgument::Template:
3014 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003015
3016 case TemplateArgument::TemplateExpansion:
3017 return TemplateArgument(getCanonicalTemplateName(
3018 Arg.getAsTemplateOrTemplatePattern()),
Douglas Gregore1d60df2011-01-14 23:41:42 +00003019 Arg.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003020
Douglas Gregora8e02e72009-07-28 23:00:59 +00003021 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00003022 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00003023 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00003024
Douglas Gregora8e02e72009-07-28 23:00:59 +00003025 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00003026 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00003027
Douglas Gregora8e02e72009-07-28 23:00:59 +00003028 case TemplateArgument::Pack: {
Douglas Gregor0192c232010-12-20 16:52:59 +00003029 if (Arg.pack_size() == 0)
3030 return Arg;
3031
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003032 TemplateArgument *CanonArgs
3033 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregora8e02e72009-07-28 23:00:59 +00003034 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003035 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00003036 AEnd = Arg.pack_end();
3037 A != AEnd; (void)++A, ++Idx)
3038 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00003039
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003040 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregora8e02e72009-07-28 23:00:59 +00003041 }
3042 }
3043
3044 // Silence GCC warning
3045 assert(false && "Unhandled template argument kind");
3046 return TemplateArgument();
3047}
3048
Douglas Gregor333489b2009-03-27 23:10:48 +00003049NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +00003050ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
Mike Stump11289f42009-09-09 15:08:12 +00003051 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00003052 return 0;
3053
3054 switch (NNS->getKind()) {
3055 case NestedNameSpecifier::Identifier:
3056 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00003057 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00003058 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3059 NNS->getAsIdentifier());
3060
3061 case NestedNameSpecifier::Namespace:
3062 // A namespace is canonical; build a nested-name-specifier with
3063 // this namespace and no prefix.
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003064 return NestedNameSpecifier::Create(*this, 0,
3065 NNS->getAsNamespace()->getOriginalNamespace());
3066
3067 case NestedNameSpecifier::NamespaceAlias:
3068 // A namespace is canonical; build a nested-name-specifier with
3069 // this namespace and no prefix.
3070 return NestedNameSpecifier::Create(*this, 0,
3071 NNS->getAsNamespaceAlias()->getNamespace()
3072 ->getOriginalNamespace());
Douglas Gregor333489b2009-03-27 23:10:48 +00003073
3074 case NestedNameSpecifier::TypeSpec:
3075 case NestedNameSpecifier::TypeSpecWithTemplate: {
3076 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor3ade5702010-11-04 00:09:33 +00003077
3078 // If we have some kind of dependent-named type (e.g., "typename T::type"),
3079 // break it apart into its prefix and identifier, then reconsititute those
3080 // as the canonical nested-name-specifier. This is required to canonicalize
3081 // a dependent nested-name-specifier involving typedefs of dependent-name
3082 // types, e.g.,
3083 // typedef typename T::type T1;
3084 // typedef typename T1::type T2;
3085 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
3086 NestedNameSpecifier *Prefix
3087 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
3088 return NestedNameSpecifier::Create(*this, Prefix,
3089 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3090 }
3091
Douglas Gregorcc10cbf2010-11-04 00:14:23 +00003092 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor3ade5702010-11-04 00:09:33 +00003093 if (const DependentTemplateSpecializationType *DTST
3094 = T->getAs<DependentTemplateSpecializationType>()) {
3095 NestedNameSpecifier *Prefix
3096 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
Douglas Gregor6e068012011-02-28 00:04:36 +00003097
3098 T = getDependentTemplateSpecializationType(DTST->getKeyword(),
3099 Prefix, DTST->getIdentifier(),
3100 DTST->getNumArgs(),
3101 DTST->getArgs());
Douglas Gregor3ade5702010-11-04 00:09:33 +00003102 T = getCanonicalType(T);
3103 }
3104
John McCall33ddac02011-01-19 10:06:00 +00003105 return NestedNameSpecifier::Create(*this, 0, false,
3106 const_cast<Type*>(T.getTypePtr()));
Douglas Gregor333489b2009-03-27 23:10:48 +00003107 }
3108
3109 case NestedNameSpecifier::Global:
3110 // The global specifier is canonical and unique.
3111 return NNS;
3112 }
3113
3114 // Required to silence a GCC warning
3115 return 0;
3116}
3117
Chris Lattner7adf0762008-08-04 07:31:14 +00003118
Jay Foad39c79802011-01-12 09:06:06 +00003119const ArrayType *ASTContext::getAsArrayType(QualType T) const {
Chris Lattner7adf0762008-08-04 07:31:14 +00003120 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00003121 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00003122 // Handle the common positive case fast.
3123 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3124 return AT;
3125 }
Mike Stump11289f42009-09-09 15:08:12 +00003126
John McCall8ccfcb52009-09-24 19:53:00 +00003127 // Handle the common negative case fast.
John McCall33ddac02011-01-19 10:06:00 +00003128 if (!isa<ArrayType>(T.getCanonicalType()))
Chris Lattner7adf0762008-08-04 07:31:14 +00003129 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003130
John McCall8ccfcb52009-09-24 19:53:00 +00003131 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00003132 // implements C99 6.7.3p8: "If the specification of an array type includes
3133 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00003134
Chris Lattner7adf0762008-08-04 07:31:14 +00003135 // If we get here, we either have type qualifiers on the type, or we have
3136 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00003137 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00003138
John McCall33ddac02011-01-19 10:06:00 +00003139 SplitQualType split = T.getSplitDesugaredType();
3140 Qualifiers qs = split.second;
Mike Stump11289f42009-09-09 15:08:12 +00003141
Chris Lattner7adf0762008-08-04 07:31:14 +00003142 // If we have a simple case, just return now.
John McCall33ddac02011-01-19 10:06:00 +00003143 const ArrayType *ATy = dyn_cast<ArrayType>(split.first);
3144 if (ATy == 0 || qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00003145 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00003146
Chris Lattner7adf0762008-08-04 07:31:14 +00003147 // Otherwise, we have an array and we have qualifiers on it. Push the
3148 // qualifiers into the array element type and return a new array type.
John McCall33ddac02011-01-19 10:06:00 +00003149 QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
Mike Stump11289f42009-09-09 15:08:12 +00003150
Chris Lattner7adf0762008-08-04 07:31:14 +00003151 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3152 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3153 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003154 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00003155 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3156 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3157 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003158 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00003159
Mike Stump11289f42009-09-09 15:08:12 +00003160 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00003161 = dyn_cast<DependentSizedArrayType>(ATy))
3162 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00003163 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00003164 DSAT->getSizeExpr(),
Douglas Gregor4619e432008-12-05 23:32:09 +00003165 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003166 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00003167 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00003168
Chris Lattner7adf0762008-08-04 07:31:14 +00003169 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00003170 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00003171 VAT->getSizeExpr(),
Chris Lattner7adf0762008-08-04 07:31:14 +00003172 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003173 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00003174 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00003175}
3176
Chris Lattnera21ad802008-04-02 05:18:44 +00003177/// getArrayDecayedType - Return the properly qualified result of decaying the
3178/// specified array type to a pointer. This operation is non-trivial when
3179/// handling typedefs etc. The canonical type of "T" must be an array type,
3180/// this returns a pointer to a properly qualified element of the array.
3181///
3182/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
Jay Foad39c79802011-01-12 09:06:06 +00003183QualType ASTContext::getArrayDecayedType(QualType Ty) const {
Chris Lattner7adf0762008-08-04 07:31:14 +00003184 // Get the element type with 'getAsArrayType' so that we don't lose any
3185 // typedefs in the element type of the array. This also handles propagation
3186 // of type qualifiers from the array type into the element type if present
3187 // (C99 6.7.3p8).
3188 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3189 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00003190
Chris Lattner7adf0762008-08-04 07:31:14 +00003191 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00003192
3193 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00003194 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00003195}
3196
John McCall33ddac02011-01-19 10:06:00 +00003197QualType ASTContext::getBaseElementType(const ArrayType *array) const {
3198 return getBaseElementType(array->getElementType());
Douglas Gregor79f83ed2009-07-23 23:49:00 +00003199}
3200
John McCall33ddac02011-01-19 10:06:00 +00003201QualType ASTContext::getBaseElementType(QualType type) const {
3202 Qualifiers qs;
3203 while (true) {
3204 SplitQualType split = type.getSplitDesugaredType();
3205 const ArrayType *array = split.first->getAsArrayTypeUnsafe();
3206 if (!array) break;
Mike Stump11289f42009-09-09 15:08:12 +00003207
John McCall33ddac02011-01-19 10:06:00 +00003208 type = array->getElementType();
3209 qs.addConsistentQualifiers(split.second);
3210 }
Mike Stump11289f42009-09-09 15:08:12 +00003211
John McCall33ddac02011-01-19 10:06:00 +00003212 return getQualifiedType(type, qs);
Anders Carlssone0808df2008-12-21 03:44:36 +00003213}
3214
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00003215/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00003216uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00003217ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
3218 uint64_t ElementCount = 1;
3219 do {
3220 ElementCount *= CA->getSize().getZExtValue();
3221 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
3222 } while (CA);
3223 return ElementCount;
3224}
3225
Steve Naroff0af91202007-04-27 21:51:21 +00003226/// getFloatingRank - Return a relative rank for floating point types.
3227/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00003228static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00003229 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00003230 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00003231
John McCall9dd450b2009-09-21 23:43:11 +00003232 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3233 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00003234 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00003235 case BuiltinType::Float: return FloatRank;
3236 case BuiltinType::Double: return DoubleRank;
3237 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00003238 }
3239}
3240
Mike Stump11289f42009-09-09 15:08:12 +00003241/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
3242/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00003243/// 'typeDomain' is a real floating point or complex type.
3244/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00003245QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
3246 QualType Domain) const {
3247 FloatingRank EltRank = getFloatingRank(Size);
3248 if (Domain->isComplexType()) {
3249 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00003250 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00003251 case FloatRank: return FloatComplexTy;
3252 case DoubleRank: return DoubleComplexTy;
3253 case LongDoubleRank: return LongDoubleComplexTy;
3254 }
Steve Naroff0af91202007-04-27 21:51:21 +00003255 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00003256
3257 assert(Domain->isRealFloatingType() && "Unknown domain!");
3258 switch (EltRank) {
3259 default: assert(0 && "getFloatingRank(): illegal value for rank");
3260 case FloatRank: return FloatTy;
3261 case DoubleRank: return DoubleTy;
3262 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00003263 }
Steve Naroffe4718892007-04-27 18:30:00 +00003264}
3265
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003266/// getFloatingTypeOrder - Compare the rank of the two specified floating
3267/// point types, ignoring the domain of the type (i.e. 'double' ==
3268/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003269/// LHS < RHS, return -1.
Jay Foad39c79802011-01-12 09:06:06 +00003270int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
Chris Lattnerb90739d2008-04-06 23:38:49 +00003271 FloatingRank LHSR = getFloatingRank(LHS);
3272 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00003273
Chris Lattnerb90739d2008-04-06 23:38:49 +00003274 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00003275 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00003276 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00003277 return 1;
3278 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00003279}
3280
Chris Lattner76a00cf2008-04-06 22:59:24 +00003281/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3282/// routine will assert if passed a built-in type that isn't an integer or enum,
3283/// or if it is not canonicalized.
John McCall424cec92011-01-19 06:33:43 +00003284unsigned ASTContext::getIntegerRank(const Type *T) const {
John McCallb692a092009-10-22 20:10:53 +00003285 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
John McCall424cec92011-01-19 06:33:43 +00003286 if (const EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00003287 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00003288
Chris Lattnerad3467e2010-12-25 23:25:43 +00003289 if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
3290 T->isSpecificBuiltinType(BuiltinType::WChar_U))
Eli Friedmanc131d3b2009-07-05 23:44:27 +00003291 T = getFromTargetType(Target.getWCharType()).getTypePtr();
3292
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003293 if (T->isSpecificBuiltinType(BuiltinType::Char16))
3294 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
3295
3296 if (T->isSpecificBuiltinType(BuiltinType::Char32))
3297 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
3298
Chris Lattner76a00cf2008-04-06 22:59:24 +00003299 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003300 default: assert(0 && "getIntegerRank(): not a built-in integer");
3301 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003302 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003303 case BuiltinType::Char_S:
3304 case BuiltinType::Char_U:
3305 case BuiltinType::SChar:
3306 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003307 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003308 case BuiltinType::Short:
3309 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003310 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003311 case BuiltinType::Int:
3312 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003313 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003314 case BuiltinType::Long:
3315 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003316 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003317 case BuiltinType::LongLong:
3318 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003319 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00003320 case BuiltinType::Int128:
3321 case BuiltinType::UInt128:
3322 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00003323 }
3324}
3325
Eli Friedman629ffb92009-08-20 04:21:42 +00003326/// \brief Whether this is a promotable bitfield reference according
3327/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
3328///
3329/// \returns the type this bit-field will promote to, or NULL if no
3330/// promotion occurs.
Jay Foad39c79802011-01-12 09:06:06 +00003331QualType ASTContext::isPromotableBitField(Expr *E) const {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00003332 if (E->isTypeDependent() || E->isValueDependent())
3333 return QualType();
3334
Eli Friedman629ffb92009-08-20 04:21:42 +00003335 FieldDecl *Field = E->getBitField();
3336 if (!Field)
3337 return QualType();
3338
3339 QualType FT = Field->getType();
3340
3341 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3342 uint64_t BitWidth = BitWidthAP.getZExtValue();
3343 uint64_t IntSize = getTypeSize(IntTy);
3344 // GCC extension compatibility: if the bit-field size is less than or equal
3345 // to the size of int, it gets promoted no matter what its type is.
3346 // For instance, unsigned long bf : 4 gets promoted to signed int.
3347 if (BitWidth < IntSize)
3348 return IntTy;
3349
3350 if (BitWidth == IntSize)
3351 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3352
3353 // Types bigger than int are not subject to promotions, and therefore act
3354 // like the base type.
3355 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3356 // is ridiculous.
3357 return QualType();
3358}
3359
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003360/// getPromotedIntegerType - Returns the type that Promotable will
3361/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3362/// integer type.
Jay Foad39c79802011-01-12 09:06:06 +00003363QualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003364 assert(!Promotable.isNull());
3365 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00003366 if (const EnumType *ET = Promotable->getAs<EnumType>())
3367 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003368 if (Promotable->isSignedIntegerType())
3369 return IntTy;
3370 uint64_t PromotableSize = getTypeSize(Promotable);
3371 uint64_t IntSize = getTypeSize(IntTy);
3372 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3373 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3374}
3375
Mike Stump11289f42009-09-09 15:08:12 +00003376/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003377/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003378/// LHS < RHS, return -1.
Jay Foad39c79802011-01-12 09:06:06 +00003379int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
John McCall424cec92011-01-19 06:33:43 +00003380 const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3381 const Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003382 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003383
Chris Lattner76a00cf2008-04-06 22:59:24 +00003384 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3385 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00003386
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003387 unsigned LHSRank = getIntegerRank(LHSC);
3388 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00003389
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003390 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3391 if (LHSRank == RHSRank) return 0;
3392 return LHSRank > RHSRank ? 1 : -1;
3393 }
Mike Stump11289f42009-09-09 15:08:12 +00003394
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003395 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3396 if (LHSUnsigned) {
3397 // If the unsigned [LHS] type is larger, return it.
3398 if (LHSRank >= RHSRank)
3399 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00003400
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003401 // If the signed type can represent all values of the unsigned type, it
3402 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003403 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003404 return -1;
3405 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00003406
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003407 // If the unsigned [RHS] type is larger, return it.
3408 if (RHSRank >= LHSRank)
3409 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00003410
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003411 // If the signed type can represent all values of the unsigned type, it
3412 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003413 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003414 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00003415}
Anders Carlsson98f07902007-08-17 05:31:46 +00003416
Anders Carlsson6d417272009-11-14 21:45:58 +00003417static RecordDecl *
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003418CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3419 DeclContext *DC, IdentifierInfo *Id) {
3420 SourceLocation Loc;
Anders Carlsson6d417272009-11-14 21:45:58 +00003421 if (Ctx.getLangOptions().CPlusPlus)
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003422 return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
Anders Carlsson6d417272009-11-14 21:45:58 +00003423 else
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003424 return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
Anders Carlsson6d417272009-11-14 21:45:58 +00003425}
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003426
Mike Stump11289f42009-09-09 15:08:12 +00003427// getCFConstantStringType - Return the type used for constant CFStrings.
Jay Foad39c79802011-01-12 09:06:06 +00003428QualType ASTContext::getCFConstantStringType() const {
Anders Carlsson98f07902007-08-17 05:31:46 +00003429 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00003430 CFConstantStringTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003431 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003432 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00003433 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00003434
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003435 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00003436
Anders Carlsson98f07902007-08-17 05:31:46 +00003437 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00003438 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003439 // int flags;
3440 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00003441 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00003442 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00003443 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00003444 FieldTypes[3] = LongTy;
3445
Anders Carlsson98f07902007-08-17 05:31:46 +00003446 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00003447 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003448 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003449 SourceLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003450 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003451 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003452 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003453 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003454 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003455 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003456 }
3457
Douglas Gregord5058122010-02-11 01:19:42 +00003458 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00003459 }
Mike Stump11289f42009-09-09 15:08:12 +00003460
Anders Carlsson98f07902007-08-17 05:31:46 +00003461 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00003462}
Anders Carlsson87c149b2007-10-11 01:00:40 +00003463
Douglas Gregor512b0772009-04-23 22:29:11 +00003464void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003465 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003466 assert(Rec && "Invalid CFConstantStringType");
3467 CFConstantStringTypeDecl = Rec->getDecl();
3468}
3469
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003470// getNSConstantStringType - Return the type used for constant NSStrings.
Jay Foad39c79802011-01-12 09:06:06 +00003471QualType ASTContext::getNSConstantStringType() const {
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003472 if (!NSConstantStringTypeDecl) {
3473 NSConstantStringTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003474 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003475 &Idents.get("__builtin_NSString"));
3476 NSConstantStringTypeDecl->startDefinition();
3477
3478 QualType FieldTypes[3];
3479
3480 // const int *isa;
3481 FieldTypes[0] = getPointerType(IntTy.withConst());
3482 // const char *str;
3483 FieldTypes[1] = getPointerType(CharTy.withConst());
3484 // unsigned int length;
3485 FieldTypes[2] = UnsignedIntTy;
3486
3487 // Create fields
3488 for (unsigned i = 0; i < 3; ++i) {
3489 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003490 SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003491 SourceLocation(), 0,
3492 FieldTypes[i], /*TInfo=*/0,
3493 /*BitWidth=*/0,
3494 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003495 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003496 NSConstantStringTypeDecl->addDecl(Field);
3497 }
3498
3499 NSConstantStringTypeDecl->completeDefinition();
3500 }
3501
3502 return getTagDeclType(NSConstantStringTypeDecl);
3503}
3504
3505void ASTContext::setNSConstantStringType(QualType T) {
3506 const RecordType *Rec = T->getAs<RecordType>();
3507 assert(Rec && "Invalid NSConstantStringType");
3508 NSConstantStringTypeDecl = Rec->getDecl();
3509}
3510
Jay Foad39c79802011-01-12 09:06:06 +00003511QualType ASTContext::getObjCFastEnumerationStateType() const {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003512 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003513 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003514 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003515 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003516 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003517
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003518 QualType FieldTypes[] = {
3519 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003520 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003521 getPointerType(UnsignedLongTy),
3522 getConstantArrayType(UnsignedLongTy,
3523 llvm::APInt(32, 5), ArrayType::Normal, 0)
3524 };
Mike Stump11289f42009-09-09 15:08:12 +00003525
Douglas Gregor91f84212008-12-11 16:49:14 +00003526 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003527 FieldDecl *Field = FieldDecl::Create(*this,
3528 ObjCFastEnumerationStateTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003529 SourceLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00003530 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003531 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003532 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003533 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003534 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003535 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003536 }
Mike Stump11289f42009-09-09 15:08:12 +00003537
Douglas Gregord5058122010-02-11 01:19:42 +00003538 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003539 }
Mike Stump11289f42009-09-09 15:08:12 +00003540
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003541 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3542}
3543
Jay Foad39c79802011-01-12 09:06:06 +00003544QualType ASTContext::getBlockDescriptorType() const {
Mike Stumpd0153282009-10-20 02:12:22 +00003545 if (BlockDescriptorType)
3546 return getTagDeclType(BlockDescriptorType);
3547
3548 RecordDecl *T;
3549 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003550 T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003551 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003552 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003553
3554 QualType FieldTypes[] = {
3555 UnsignedLongTy,
3556 UnsignedLongTy,
3557 };
3558
3559 const char *FieldNames[] = {
3560 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003561 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003562 };
3563
3564 for (size_t i = 0; i < 2; ++i) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003565 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003566 SourceLocation(),
3567 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003568 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003569 /*BitWidth=*/0,
3570 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003571 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003572 T->addDecl(Field);
3573 }
3574
Douglas Gregord5058122010-02-11 01:19:42 +00003575 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003576
3577 BlockDescriptorType = T;
3578
3579 return getTagDeclType(BlockDescriptorType);
3580}
3581
3582void ASTContext::setBlockDescriptorType(QualType T) {
3583 const RecordType *Rec = T->getAs<RecordType>();
3584 assert(Rec && "Invalid BlockDescriptorType");
3585 BlockDescriptorType = Rec->getDecl();
3586}
3587
Jay Foad39c79802011-01-12 09:06:06 +00003588QualType ASTContext::getBlockDescriptorExtendedType() const {
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003589 if (BlockDescriptorExtendedType)
3590 return getTagDeclType(BlockDescriptorExtendedType);
3591
3592 RecordDecl *T;
3593 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003594 T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003595 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003596 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003597
3598 QualType FieldTypes[] = {
3599 UnsignedLongTy,
3600 UnsignedLongTy,
3601 getPointerType(VoidPtrTy),
3602 getPointerType(VoidPtrTy)
3603 };
3604
3605 const char *FieldNames[] = {
3606 "reserved",
3607 "Size",
3608 "CopyFuncPtr",
3609 "DestroyFuncPtr"
3610 };
3611
3612 for (size_t i = 0; i < 4; ++i) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003613 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003614 SourceLocation(),
3615 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003616 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003617 /*BitWidth=*/0,
3618 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003619 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003620 T->addDecl(Field);
3621 }
3622
Douglas Gregord5058122010-02-11 01:19:42 +00003623 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003624
3625 BlockDescriptorExtendedType = T;
3626
3627 return getTagDeclType(BlockDescriptorExtendedType);
3628}
3629
3630void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3631 const RecordType *Rec = T->getAs<RecordType>();
3632 assert(Rec && "Invalid BlockDescriptorType");
3633 BlockDescriptorExtendedType = Rec->getDecl();
3634}
3635
Jay Foad39c79802011-01-12 09:06:06 +00003636bool ASTContext::BlockRequiresCopying(QualType Ty) const {
Mike Stump94967902009-10-21 18:16:27 +00003637 if (Ty->isBlockPointerType())
3638 return true;
3639 if (isObjCNSObjectType(Ty))
3640 return true;
3641 if (Ty->isObjCObjectPointerType())
3642 return true;
Fariborz Jahaniana00076c2010-11-17 00:21:28 +00003643 if (getLangOptions().CPlusPlus) {
3644 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3645 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3646 return RD->hasConstCopyConstructor(*this);
3647
3648 }
3649 }
Mike Stump94967902009-10-21 18:16:27 +00003650 return false;
3651}
3652
Jay Foad39c79802011-01-12 09:06:06 +00003653QualType
3654ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const {
Mike Stump94967902009-10-21 18:16:27 +00003655 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003656 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003657 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003658 // unsigned int __flags;
3659 // unsigned int __size;
Eli Friedman04831922010-08-22 01:00:03 +00003660 // void *__copy_helper; // as needed
3661 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003662 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003663 // } *
3664
Mike Stump94967902009-10-21 18:16:27 +00003665 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3666
3667 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003668 llvm::SmallString<36> Name;
3669 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3670 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003671 RecordDecl *T;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003672 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003673 T->startDefinition();
3674 QualType Int32Ty = IntTy;
3675 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3676 QualType FieldTypes[] = {
3677 getPointerType(VoidPtrTy),
3678 getPointerType(getTagDeclType(T)),
3679 Int32Ty,
3680 Int32Ty,
3681 getPointerType(VoidPtrTy),
3682 getPointerType(VoidPtrTy),
3683 Ty
3684 };
3685
Daniel Dunbar56df9772010-08-17 22:39:59 +00003686 llvm::StringRef FieldNames[] = {
Mike Stump94967902009-10-21 18:16:27 +00003687 "__isa",
3688 "__forwarding",
3689 "__flags",
3690 "__size",
3691 "__copy_helper",
3692 "__destroy_helper",
3693 DeclName,
3694 };
3695
3696 for (size_t i = 0; i < 7; ++i) {
3697 if (!HasCopyAndDispose && i >=4 && i <= 5)
3698 continue;
3699 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003700 SourceLocation(),
Mike Stump94967902009-10-21 18:16:27 +00003701 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003702 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003703 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003704 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003705 T->addDecl(Field);
3706 }
3707
Douglas Gregord5058122010-02-11 01:19:42 +00003708 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003709
3710 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003711}
3712
Douglas Gregor512b0772009-04-23 22:29:11 +00003713void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003714 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003715 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3716 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3717}
3718
Anders Carlsson18acd442007-10-29 06:33:42 +00003719// This returns true if a type has been typedefed to BOOL:
3720// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003721static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003722 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003723 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3724 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003725
Anders Carlssond8499822007-10-29 05:01:08 +00003726 return false;
3727}
3728
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003729/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003730/// purpose.
Jay Foad39c79802011-01-12 09:06:06 +00003731CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
Ken Dyck40775002010-01-11 17:06:35 +00003732 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003733
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003734 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003735 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003736 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003737 // Treat arrays as pointers, since that's how they're passed in.
3738 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003739 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003740 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003741}
3742
3743static inline
3744std::string charUnitsToString(const CharUnits &CU) {
3745 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003746}
3747
John McCall351762c2011-02-07 10:33:21 +00003748/// getObjCEncodingForBlock - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003749/// declaration.
John McCall351762c2011-02-07 10:33:21 +00003750std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
3751 std::string S;
3752
David Chisnall950a9512009-11-17 19:33:30 +00003753 const BlockDecl *Decl = Expr->getBlockDecl();
3754 QualType BlockTy =
3755 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3756 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003757 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003758 // Compute size of all parameters.
3759 // Start with computing size of a pointer in number of bytes.
3760 // FIXME: There might(should) be a better way of doing this computation!
3761 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003762 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3763 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003764 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003765 E = Decl->param_end(); PI != E; ++PI) {
3766 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003767 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003768 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003769 ParmOffset += sz;
3770 }
3771 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003772 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003773 // Block pointer and offset.
3774 S += "@?0";
3775 ParmOffset = PtrSize;
3776
3777 // Argument types.
3778 ParmOffset = PtrSize;
3779 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3780 Decl->param_end(); PI != E; ++PI) {
3781 ParmVarDecl *PVDecl = *PI;
3782 QualType PType = PVDecl->getOriginalType();
3783 if (const ArrayType *AT =
3784 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3785 // Use array's original type only if it has known number of
3786 // elements.
3787 if (!isa<ConstantArrayType>(AT))
3788 PType = PVDecl->getType();
3789 } else if (PType->isFunctionType())
3790 PType = PVDecl->getType();
3791 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003792 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003793 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003794 }
John McCall351762c2011-02-07 10:33:21 +00003795
3796 return S;
David Chisnall950a9512009-11-17 19:33:30 +00003797}
3798
David Chisnall50e4eba2010-12-30 14:05:53 +00003799void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
3800 std::string& S) {
3801 // Encode result type.
3802 getObjCEncodingForType(Decl->getResultType(), S);
3803 CharUnits ParmOffset;
3804 // Compute size of all parameters.
3805 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3806 E = Decl->param_end(); PI != E; ++PI) {
3807 QualType PType = (*PI)->getType();
3808 CharUnits sz = getObjCEncodingTypeSize(PType);
3809 assert (sz.isPositive() &&
3810 "getObjCEncodingForMethodDecl - Incomplete param type");
3811 ParmOffset += sz;
3812 }
3813 S += charUnitsToString(ParmOffset);
3814 ParmOffset = CharUnits::Zero();
3815
3816 // Argument types.
3817 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3818 E = Decl->param_end(); PI != E; ++PI) {
3819 ParmVarDecl *PVDecl = *PI;
3820 QualType PType = PVDecl->getOriginalType();
3821 if (const ArrayType *AT =
3822 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3823 // Use array's original type only if it has known number of
3824 // elements.
3825 if (!isa<ConstantArrayType>(AT))
3826 PType = PVDecl->getType();
3827 } else if (PType->isFunctionType())
3828 PType = PVDecl->getType();
3829 getObjCEncodingForType(PType, S);
3830 S += charUnitsToString(ParmOffset);
3831 ParmOffset += getObjCEncodingTypeSize(PType);
3832 }
3833}
3834
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003835/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003836/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003837void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Jay Foad39c79802011-01-12 09:06:06 +00003838 std::string& S) const {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003839 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003840 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003841 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003842 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003843 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003844 // Compute size of all parameters.
3845 // Start with computing size of a pointer in number of bytes.
3846 // FIXME: There might(should) be a better way of doing this computation!
3847 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003848 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003849 // The first two arguments (self and _cmd) are pointers; account for
3850 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003851 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003852 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003853 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003854 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003855 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003856 assert (sz.isPositive() &&
3857 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003858 ParmOffset += sz;
3859 }
Ken Dyck40775002010-01-11 17:06:35 +00003860 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003861 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003862 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003863
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003864 // Argument types.
3865 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003866 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003867 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003868 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003869 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003870 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003871 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3872 // Use array's original type only if it has known number of
3873 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003874 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003875 PType = PVDecl->getType();
3876 } else if (PType->isFunctionType())
3877 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003878 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003879 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003880 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003881 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003882 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003883 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003884 }
3885}
3886
Daniel Dunbar4932b362008-08-28 04:38:10 +00003887/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003888/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003889/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3890/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003891/// Property attributes are stored as a comma-delimited C string. The simple
3892/// attributes readonly and bycopy are encoded as single characters. The
3893/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3894/// encoded as single characters, followed by an identifier. Property types
3895/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003896/// these attributes are defined by the following enumeration:
3897/// @code
3898/// enum PropertyAttributes {
3899/// kPropertyReadOnly = 'R', // property is read-only.
3900/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3901/// kPropertyByref = '&', // property is a reference to the value last assigned
3902/// kPropertyDynamic = 'D', // property is dynamic
3903/// kPropertyGetter = 'G', // followed by getter selector name
3904/// kPropertySetter = 'S', // followed by setter selector name
3905/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3906/// kPropertyType = 't' // followed by old-style type encoding.
3907/// kPropertyWeak = 'W' // 'weak' property
3908/// kPropertyStrong = 'P' // property GC'able
3909/// kPropertyNonAtomic = 'N' // property non-atomic
3910/// };
3911/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003912void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003913 const Decl *Container,
Jay Foad39c79802011-01-12 09:06:06 +00003914 std::string& S) const {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003915 // Collect information from the property implementation decl(s).
3916 bool Dynamic = false;
3917 ObjCPropertyImplDecl *SynthesizePID = 0;
3918
3919 // FIXME: Duplicated code due to poor abstraction.
3920 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003921 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003922 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3923 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003924 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003925 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003926 ObjCPropertyImplDecl *PID = *i;
3927 if (PID->getPropertyDecl() == PD) {
3928 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3929 Dynamic = true;
3930 } else {
3931 SynthesizePID = PID;
3932 }
3933 }
3934 }
3935 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003936 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003937 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003938 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003939 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003940 ObjCPropertyImplDecl *PID = *i;
3941 if (PID->getPropertyDecl() == PD) {
3942 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3943 Dynamic = true;
3944 } else {
3945 SynthesizePID = PID;
3946 }
3947 }
Mike Stump11289f42009-09-09 15:08:12 +00003948 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003949 }
3950 }
3951
3952 // FIXME: This is not very efficient.
3953 S = "T";
3954
3955 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003956 // GCC has some special rules regarding encoding of properties which
3957 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003958 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003959 true /* outermost type */,
3960 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003961
3962 if (PD->isReadOnly()) {
3963 S += ",R";
3964 } else {
3965 switch (PD->getSetterKind()) {
3966 case ObjCPropertyDecl::Assign: break;
3967 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003968 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003969 }
3970 }
3971
3972 // It really isn't clear at all what this means, since properties
3973 // are "dynamic by default".
3974 if (Dynamic)
3975 S += ",D";
3976
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003977 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3978 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003979
Daniel Dunbar4932b362008-08-28 04:38:10 +00003980 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3981 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003982 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003983 }
3984
3985 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3986 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003987 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003988 }
3989
3990 if (SynthesizePID) {
3991 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3992 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003993 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003994 }
3995
3996 // FIXME: OBJCGC: weak & strong
3997}
3998
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003999/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00004000/// Another legacy compatibility encoding: 32-bit longs are encoded as
4001/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004002/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4003///
4004void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00004005 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00004006 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Jay Foad39c79802011-01-12 09:06:06 +00004007 if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004008 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00004009 else
Jay Foad39c79802011-01-12 09:06:06 +00004010 if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004011 PointeeTy = IntTy;
4012 }
4013 }
4014}
4015
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004016void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Jay Foad39c79802011-01-12 09:06:06 +00004017 const FieldDecl *Field) const {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004018 // We follow the behavior of gcc, expanding structures which are
4019 // directly pointed to, and expanding embedded structures. Note that
4020 // these rules are sufficient to prevent recursive encoding of the
4021 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00004022 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00004023 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004024}
4025
David Chisnallb190a2c2010-06-04 01:10:52 +00004026static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
4027 switch (T->getAs<BuiltinType>()->getKind()) {
4028 default: assert(0 && "Unhandled builtin type kind");
4029 case BuiltinType::Void: return 'v';
4030 case BuiltinType::Bool: return 'B';
4031 case BuiltinType::Char_U:
4032 case BuiltinType::UChar: return 'C';
4033 case BuiltinType::UShort: return 'S';
4034 case BuiltinType::UInt: return 'I';
4035 case BuiltinType::ULong:
Jay Foad39c79802011-01-12 09:06:06 +00004036 return C->getIntWidth(T) == 32 ? 'L' : 'Q';
David Chisnallb190a2c2010-06-04 01:10:52 +00004037 case BuiltinType::UInt128: return 'T';
4038 case BuiltinType::ULongLong: return 'Q';
4039 case BuiltinType::Char_S:
4040 case BuiltinType::SChar: return 'c';
4041 case BuiltinType::Short: return 's';
Chris Lattnerad3467e2010-12-25 23:25:43 +00004042 case BuiltinType::WChar_S:
4043 case BuiltinType::WChar_U:
David Chisnallb190a2c2010-06-04 01:10:52 +00004044 case BuiltinType::Int: return 'i';
4045 case BuiltinType::Long:
Jay Foad39c79802011-01-12 09:06:06 +00004046 return C->getIntWidth(T) == 32 ? 'l' : 'q';
David Chisnallb190a2c2010-06-04 01:10:52 +00004047 case BuiltinType::LongLong: return 'q';
4048 case BuiltinType::Int128: return 't';
4049 case BuiltinType::Float: return 'f';
4050 case BuiltinType::Double: return 'd';
Daniel Dunbar7cba5a72010-10-11 21:13:48 +00004051 case BuiltinType::LongDouble: return 'D';
David Chisnallb190a2c2010-06-04 01:10:52 +00004052 }
4053}
4054
Jay Foad39c79802011-01-12 09:06:06 +00004055static void EncodeBitField(const ASTContext *Ctx, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00004056 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004057 const Expr *E = FD->getBitWidth();
4058 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004059 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00004060 // The NeXT runtime encodes bit fields as b followed by the number of bits.
4061 // The GNU runtime requires more information; bitfields are encoded as b,
4062 // then the offset (in bits) of the first element, then the type of the
4063 // bitfield, then the size in bits. For example, in this structure:
4064 //
4065 // struct
4066 // {
4067 // int integer;
4068 // int flags:2;
4069 // };
4070 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
4071 // runtime, but b32i2 for the GNU runtime. The reason for this extra
4072 // information is not especially sensible, but we're stuck with it for
4073 // compatibility with GCC, although providing it breaks anything that
4074 // actually uses runtime introspection and wants to work on both runtimes...
4075 if (!Ctx->getLangOptions().NeXTRuntime) {
4076 const RecordDecl *RD = FD->getParent();
4077 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
4078 // FIXME: This same linear search is also used in ExprConstant - it might
4079 // be better if the FieldDecl stored its offset. We'd be increasing the
4080 // size of the object slightly, but saving some time every time it is used.
4081 unsigned i = 0;
4082 for (RecordDecl::field_iterator Field = RD->field_begin(),
4083 FieldEnd = RD->field_end();
4084 Field != FieldEnd; (void)++Field, ++i) {
4085 if (*Field == FD)
4086 break;
4087 }
4088 S += llvm::utostr(RL.getFieldOffset(i));
David Chisnall6f0a7d22010-12-26 20:12:30 +00004089 if (T->isEnumeralType())
4090 S += 'i';
4091 else
Jay Foad39c79802011-01-12 09:06:06 +00004092 S += ObjCEncodingForPrimitiveKind(Ctx, T);
David Chisnallb190a2c2010-06-04 01:10:52 +00004093 }
4094 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004095 S += llvm::utostr(N);
4096}
4097
Daniel Dunbar07d07852009-10-18 21:17:35 +00004098// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004099void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
4100 bool ExpandPointedToStructures,
4101 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00004102 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00004103 bool OutermostType,
Jay Foad39c79802011-01-12 09:06:06 +00004104 bool EncodingProperty) const {
David Chisnallb190a2c2010-06-04 01:10:52 +00004105 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004106 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00004107 return EncodeBitField(this, S, T, FD);
4108 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004109 return;
4110 }
Mike Stump11289f42009-09-09 15:08:12 +00004111
John McCall9dd450b2009-09-21 23:43:11 +00004112 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00004113 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00004114 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00004115 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004116 return;
4117 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00004118
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004119 // encoding for pointer or r3eference types.
4120 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004121 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00004122 if (PT->isObjCSelType()) {
4123 S += ':';
4124 return;
4125 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004126 PointeeTy = PT->getPointeeType();
4127 }
4128 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4129 PointeeTy = RT->getPointeeType();
4130 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004131 bool isReadOnly = false;
4132 // For historical/compatibility reasons, the read-only qualifier of the
4133 // pointee gets emitted _before_ the '^'. The read-only qualifier of
4134 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00004135 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00004136 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004137 if (OutermostType && T.isConstQualified()) {
4138 isReadOnly = true;
4139 S += 'r';
4140 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00004141 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004142 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004143 while (P->getAs<PointerType>())
4144 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004145 if (P.isConstQualified()) {
4146 isReadOnly = true;
4147 S += 'r';
4148 }
4149 }
4150 if (isReadOnly) {
4151 // Another legacy compatibility encoding. Some ObjC qualifier and type
4152 // combinations need to be rearranged.
4153 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00004154 if (llvm::StringRef(S).endswith("nr"))
4155 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004156 }
Mike Stump11289f42009-09-09 15:08:12 +00004157
Anders Carlssond8499822007-10-29 05:01:08 +00004158 if (PointeeTy->isCharType()) {
4159 // char pointer types should be encoded as '*' unless it is a
4160 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00004161 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00004162 S += '*';
4163 return;
4164 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004165 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00004166 // GCC binary compat: Need to convert "struct objc_class *" to "#".
4167 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
4168 S += '#';
4169 return;
4170 }
4171 // GCC binary compat: Need to convert "struct objc_object *" to "@".
4172 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
4173 S += '@';
4174 return;
4175 }
4176 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00004177 }
Anders Carlssond8499822007-10-29 05:01:08 +00004178 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004179 getLegacyIntegralTypeEncoding(PointeeTy);
4180
Mike Stump11289f42009-09-09 15:08:12 +00004181 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004182 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004183 return;
4184 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004185
Chris Lattnere7cabb92009-07-13 00:10:46 +00004186 if (const ArrayType *AT =
4187 // Ignore type qualifiers etc.
4188 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00004189 if (isa<IncompleteArrayType>(AT)) {
4190 // Incomplete arrays are encoded as a pointer to the array element.
4191 S += '^';
4192
Mike Stump11289f42009-09-09 15:08:12 +00004193 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00004194 false, ExpandStructures, FD);
4195 } else {
4196 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00004197
Anders Carlssond05f44b2009-02-22 01:38:57 +00004198 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
4199 S += llvm::utostr(CAT->getSize().getZExtValue());
4200 else {
4201 //Variable length arrays are encoded as a regular array with 0 elements.
4202 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
4203 S += '0';
4204 }
Mike Stump11289f42009-09-09 15:08:12 +00004205
4206 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00004207 false, ExpandStructures, FD);
4208 S += ']';
4209 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004210 return;
4211 }
Mike Stump11289f42009-09-09 15:08:12 +00004212
John McCall9dd450b2009-09-21 23:43:11 +00004213 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00004214 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004215 return;
4216 }
Mike Stump11289f42009-09-09 15:08:12 +00004217
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004218 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004219 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004220 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00004221 // Anonymous structures print as '?'
4222 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4223 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00004224 if (ClassTemplateSpecializationDecl *Spec
4225 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
4226 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
4227 std::string TemplateArgsStr
4228 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004229 TemplateArgs.data(),
4230 TemplateArgs.size(),
Fariborz Jahanianc5158202010-05-07 00:28:49 +00004231 (*this).PrintingPolicy);
4232
4233 S += TemplateArgsStr;
4234 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00004235 } else {
4236 S += '?';
4237 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00004238 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004239 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004240 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
4241 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00004242 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004243 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004244 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00004245 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004246 S += '"';
4247 }
Mike Stump11289f42009-09-09 15:08:12 +00004248
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004249 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004250 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00004251 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004252 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004253 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004254 QualType qt = Field->getType();
4255 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00004256 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004257 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004258 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004259 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00004260 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004261 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004262 return;
4263 }
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004264
Chris Lattnere7cabb92009-07-13 00:10:46 +00004265 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004266 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00004267 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004268 else
4269 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004270 return;
4271 }
Mike Stump11289f42009-09-09 15:08:12 +00004272
Chris Lattnere7cabb92009-07-13 00:10:46 +00004273 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00004274 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00004275 return;
4276 }
Mike Stump11289f42009-09-09 15:08:12 +00004277
John McCall8b07ec22010-05-15 11:32:37 +00004278 // Ignore protocol qualifiers when mangling at this level.
4279 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4280 T = OT->getBaseType();
4281
John McCall8ccfcb52009-09-24 19:53:00 +00004282 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004283 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00004284 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004285 S += '{';
4286 const IdentifierInfo *II = OI->getIdentifier();
4287 S += II->getName();
4288 S += '=';
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004289 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
4290 DeepCollectObjCIvars(OI, true, Ivars);
4291 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4292 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4293 if (Field->isBitField())
4294 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004295 else
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004296 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004297 }
4298 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004299 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004300 }
Mike Stump11289f42009-09-09 15:08:12 +00004301
John McCall9dd450b2009-09-21 23:43:11 +00004302 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004303 if (OPT->isObjCIdType()) {
4304 S += '@';
4305 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004306 }
Mike Stump11289f42009-09-09 15:08:12 +00004307
Steve Narofff0c86112009-10-28 22:03:49 +00004308 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4309 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4310 // Since this is a binary compatibility issue, need to consult with runtime
4311 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004312 S += '#';
4313 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004314 }
Mike Stump11289f42009-09-09 15:08:12 +00004315
Chris Lattnere7cabb92009-07-13 00:10:46 +00004316 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00004317 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00004318 ExpandPointedToStructures,
4319 ExpandStructures, FD);
4320 if (FD || EncodingProperty) {
4321 // Note that we do extended encoding of protocol qualifer list
4322 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004323 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00004324 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4325 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004326 S += '<';
4327 S += (*I)->getNameAsString();
4328 S += '>';
4329 }
4330 S += '"';
4331 }
4332 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004333 }
Mike Stump11289f42009-09-09 15:08:12 +00004334
Chris Lattnere7cabb92009-07-13 00:10:46 +00004335 QualType PointeeTy = OPT->getPointeeType();
4336 if (!EncodingProperty &&
4337 isa<TypedefType>(PointeeTy.getTypePtr())) {
4338 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00004339 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00004340 // {...};
4341 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00004342 getObjCEncodingForTypeImpl(PointeeTy, S,
4343 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00004344 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00004345 return;
4346 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004347
4348 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00004349 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004350 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00004351 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00004352 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4353 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004354 S += '<';
4355 S += (*I)->getNameAsString();
4356 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00004357 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004358 S += '"';
4359 }
4360 return;
4361 }
Mike Stump11289f42009-09-09 15:08:12 +00004362
John McCalla9e6e8d2010-05-17 23:56:34 +00004363 // gcc just blithely ignores member pointers.
4364 // TODO: maybe there should be a mangling for these
4365 if (T->getAs<MemberPointerType>())
4366 return;
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004367
4368 if (T->isVectorType()) {
4369 // This matches gcc's encoding, even though technically it is
4370 // insufficient.
4371 // FIXME. We should do a better job than gcc.
4372 return;
4373 }
4374
Chris Lattnere7cabb92009-07-13 00:10:46 +00004375 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00004376}
4377
Mike Stump11289f42009-09-09 15:08:12 +00004378void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00004379 std::string& S) const {
4380 if (QT & Decl::OBJC_TQ_In)
4381 S += 'n';
4382 if (QT & Decl::OBJC_TQ_Inout)
4383 S += 'N';
4384 if (QT & Decl::OBJC_TQ_Out)
4385 S += 'o';
4386 if (QT & Decl::OBJC_TQ_Bycopy)
4387 S += 'O';
4388 if (QT & Decl::OBJC_TQ_Byref)
4389 S += 'R';
4390 if (QT & Decl::OBJC_TQ_Oneway)
4391 S += 'V';
4392}
4393
Chris Lattnere7cabb92009-07-13 00:10:46 +00004394void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00004395 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004396
Anders Carlsson87c149b2007-10-11 01:00:40 +00004397 BuiltinVaListType = T;
4398}
4399
Chris Lattnere7cabb92009-07-13 00:10:46 +00004400void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004401 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00004402}
4403
Chris Lattnere7cabb92009-07-13 00:10:46 +00004404void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00004405 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00004406}
4407
Chris Lattnere7cabb92009-07-13 00:10:46 +00004408void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004409 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004410}
4411
Chris Lattnere7cabb92009-07-13 00:10:46 +00004412void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004413 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00004414}
4415
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004416void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00004417 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00004418 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004419
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004420 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00004421}
4422
John McCalld28ae272009-12-02 08:04:21 +00004423/// \brief Retrieve the template name that corresponds to a non-empty
4424/// lookup.
Jay Foad39c79802011-01-12 09:06:06 +00004425TemplateName
4426ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4427 UnresolvedSetIterator End) const {
John McCalld28ae272009-12-02 08:04:21 +00004428 unsigned size = End - Begin;
4429 assert(size > 1 && "set is not overloaded!");
4430
4431 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4432 size * sizeof(FunctionTemplateDecl*));
4433 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4434
4435 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00004436 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00004437 NamedDecl *D = *I;
4438 assert(isa<FunctionTemplateDecl>(D) ||
4439 (isa<UsingShadowDecl>(D) &&
4440 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4441 *Storage++ = D;
4442 }
4443
4444 return TemplateName(OT);
4445}
4446
Douglas Gregordc572a32009-03-30 22:58:21 +00004447/// \brief Retrieve the template name that represents a qualified
4448/// template name such as \c std::vector.
Jay Foad39c79802011-01-12 09:06:06 +00004449TemplateName
4450ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
4451 bool TemplateKeyword,
4452 TemplateDecl *Template) const {
Douglas Gregore29139c2011-03-03 17:04:51 +00004453 assert(NNS && "Missing nested-name-specifier in qualified template name");
4454
Douglas Gregorc42075a2010-02-04 18:10:26 +00004455 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00004456 llvm::FoldingSetNodeID ID;
4457 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4458
4459 void *InsertPos = 0;
4460 QualifiedTemplateName *QTN =
4461 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4462 if (!QTN) {
4463 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4464 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4465 }
4466
4467 return TemplateName(QTN);
4468}
4469
4470/// \brief Retrieve the template name that represents a dependent
4471/// template name such as \c MetaFun::template apply.
Jay Foad39c79802011-01-12 09:06:06 +00004472TemplateName
4473ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4474 const IdentifierInfo *Name) const {
Mike Stump11289f42009-09-09 15:08:12 +00004475 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00004476 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00004477
4478 llvm::FoldingSetNodeID ID;
4479 DependentTemplateName::Profile(ID, NNS, Name);
4480
4481 void *InsertPos = 0;
4482 DependentTemplateName *QTN =
4483 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4484
4485 if (QTN)
4486 return TemplateName(QTN);
4487
4488 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4489 if (CanonNNS == NNS) {
4490 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4491 } else {
4492 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4493 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004494 DependentTemplateName *CheckQTN =
4495 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4496 assert(!CheckQTN && "Dependent type name canonicalization broken");
4497 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004498 }
4499
4500 DependentTemplateNames.InsertNode(QTN, InsertPos);
4501 return TemplateName(QTN);
4502}
4503
Douglas Gregor71395fa2009-11-04 00:56:37 +00004504/// \brief Retrieve the template name that represents a dependent
4505/// template name such as \c MetaFun::template operator+.
4506TemplateName
4507ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Jay Foad39c79802011-01-12 09:06:06 +00004508 OverloadedOperatorKind Operator) const {
Douglas Gregor71395fa2009-11-04 00:56:37 +00004509 assert((!NNS || NNS->isDependent()) &&
4510 "Nested name specifier must be dependent");
4511
4512 llvm::FoldingSetNodeID ID;
4513 DependentTemplateName::Profile(ID, NNS, Operator);
4514
4515 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004516 DependentTemplateName *QTN
4517 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004518
4519 if (QTN)
4520 return TemplateName(QTN);
4521
4522 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4523 if (CanonNNS == NNS) {
4524 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4525 } else {
4526 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4527 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004528
4529 DependentTemplateName *CheckQTN
4530 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4531 assert(!CheckQTN && "Dependent template name canonicalization broken");
4532 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004533 }
4534
4535 DependentTemplateNames.InsertNode(QTN, InsertPos);
4536 return TemplateName(QTN);
4537}
4538
Douglas Gregor5590be02011-01-15 06:45:20 +00004539TemplateName
4540ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
4541 const TemplateArgument &ArgPack) const {
4542 ASTContext &Self = const_cast<ASTContext &>(*this);
4543 llvm::FoldingSetNodeID ID;
4544 SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
4545
4546 void *InsertPos = 0;
4547 SubstTemplateTemplateParmPackStorage *Subst
4548 = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
4549
4550 if (!Subst) {
4551 Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param,
4552 ArgPack.pack_size(),
4553 ArgPack.pack_begin());
4554 SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
4555 }
4556
4557 return TemplateName(Subst);
4558}
4559
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004560/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004561/// TargetInfo, produce the corresponding type. The unsigned @p Type
4562/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004563CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004564 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004565 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004566 case TargetInfo::SignedShort: return ShortTy;
4567 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4568 case TargetInfo::SignedInt: return IntTy;
4569 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4570 case TargetInfo::SignedLong: return LongTy;
4571 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4572 case TargetInfo::SignedLongLong: return LongLongTy;
4573 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4574 }
4575
4576 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004577 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004578}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004579
4580//===----------------------------------------------------------------------===//
4581// Type Predicates.
4582//===----------------------------------------------------------------------===//
4583
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004584/// isObjCNSObjectType - Return true if this is an NSObject object using
4585/// NSObject attribute on a c-style pointer type.
4586/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004587/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004588///
4589bool ASTContext::isObjCNSObjectType(QualType Ty) const {
John McCall424cec92011-01-19 06:33:43 +00004590 if (const TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
Richard Smithdda56e42011-04-15 14:24:37 +00004591 if (TypedefNameDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004592 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004593 return true;
4594 }
Mike Stump11289f42009-09-09 15:08:12 +00004595 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004596}
4597
Fariborz Jahanian96207692009-02-18 21:49:28 +00004598/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4599/// garbage collection attribute.
4600///
John McCall553d45a2011-01-12 00:34:59 +00004601Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
4602 if (getLangOptions().getGCMode() == LangOptions::NonGC)
4603 return Qualifiers::GCNone;
4604
4605 assert(getLangOptions().ObjC1);
4606 Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
4607
4608 // Default behaviour under objective-C's gc is for ObjC pointers
4609 // (or pointers to them) be treated as though they were declared
4610 // as __strong.
4611 if (GCAttrs == Qualifiers::GCNone) {
4612 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
4613 return Qualifiers::Strong;
4614 else if (Ty->isPointerType())
4615 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
4616 } else {
4617 // It's not valid to set GC attributes on anything that isn't a
4618 // pointer.
4619#ifndef NDEBUG
4620 QualType CT = Ty->getCanonicalTypeInternal();
4621 while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
4622 CT = AT->getElementType();
4623 assert(CT->isAnyPointerType() || CT->isBlockPointerType());
4624#endif
Fariborz Jahanian96207692009-02-18 21:49:28 +00004625 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004626 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004627}
4628
Chris Lattner49af6a42008-04-07 06:51:04 +00004629//===----------------------------------------------------------------------===//
4630// Type Compatibility Testing
4631//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004632
Mike Stump11289f42009-09-09 15:08:12 +00004633/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004634/// compatible.
4635static bool areCompatVectorTypes(const VectorType *LHS,
4636 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004637 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004638 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004639 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004640}
4641
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004642bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4643 QualType SecondVec) {
4644 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4645 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4646
4647 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4648 return true;
4649
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004650 // Treat Neon vector types and most AltiVec vector types as if they are the
4651 // equivalent GCC vector types.
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004652 const VectorType *First = FirstVec->getAs<VectorType>();
4653 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004654 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004655 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004656 First->getVectorKind() != VectorType::AltiVecPixel &&
4657 First->getVectorKind() != VectorType::AltiVecBool &&
4658 Second->getVectorKind() != VectorType::AltiVecPixel &&
4659 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004660 return true;
4661
4662 return false;
4663}
4664
Steve Naroff8e6aee52009-07-23 01:01:38 +00004665//===----------------------------------------------------------------------===//
4666// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4667//===----------------------------------------------------------------------===//
4668
4669/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4670/// inheritance hierarchy of 'rProto'.
Jay Foad39c79802011-01-12 09:06:06 +00004671bool
4672ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4673 ObjCProtocolDecl *rProto) const {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004674 if (lProto == rProto)
4675 return true;
4676 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4677 E = rProto->protocol_end(); PI != E; ++PI)
4678 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4679 return true;
4680 return false;
4681}
4682
Steve Naroff8e6aee52009-07-23 01:01:38 +00004683/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4684/// return true if lhs's protocols conform to rhs's protocol; false
4685/// otherwise.
4686bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4687 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4688 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4689 return false;
4690}
4691
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004692/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4693/// Class<p1, ...>.
4694bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4695 QualType rhs) {
4696 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4697 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4698 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4699
4700 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4701 E = lhsQID->qual_end(); I != E; ++I) {
4702 bool match = false;
4703 ObjCProtocolDecl *lhsProto = *I;
4704 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4705 E = rhsOPT->qual_end(); J != E; ++J) {
4706 ObjCProtocolDecl *rhsProto = *J;
4707 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4708 match = true;
4709 break;
4710 }
4711 }
4712 if (!match)
4713 return false;
4714 }
4715 return true;
4716}
4717
Steve Naroff8e6aee52009-07-23 01:01:38 +00004718/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4719/// ObjCQualifiedIDType.
4720bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4721 bool compare) {
4722 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004723 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004724 lhs->isObjCIdType() || lhs->isObjCClassType())
4725 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004726 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004727 rhs->isObjCIdType() || rhs->isObjCClassType())
4728 return true;
4729
4730 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004731 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004732
Steve Naroff8e6aee52009-07-23 01:01:38 +00004733 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004734
Steve Naroff8e6aee52009-07-23 01:01:38 +00004735 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004736 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004737 // make sure we check the class hierarchy.
4738 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4739 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4740 E = lhsQID->qual_end(); I != E; ++I) {
4741 // when comparing an id<P> on lhs with a static type on rhs,
4742 // see if static class implements all of id's protocols, directly or
4743 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004744 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004745 return false;
4746 }
4747 }
4748 // If there are no qualifiers and no interface, we have an 'id'.
4749 return true;
4750 }
Mike Stump11289f42009-09-09 15:08:12 +00004751 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004752 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4753 E = lhsQID->qual_end(); I != E; ++I) {
4754 ObjCProtocolDecl *lhsProto = *I;
4755 bool match = false;
4756
4757 // when comparing an id<P> on lhs with a static type on rhs,
4758 // see if static class implements all of id's protocols, directly or
4759 // through its super class and categories.
4760 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4761 E = rhsOPT->qual_end(); J != E; ++J) {
4762 ObjCProtocolDecl *rhsProto = *J;
4763 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4764 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4765 match = true;
4766 break;
4767 }
4768 }
Mike Stump11289f42009-09-09 15:08:12 +00004769 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004770 // make sure we check the class hierarchy.
4771 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4772 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4773 E = lhsQID->qual_end(); I != E; ++I) {
4774 // when comparing an id<P> on lhs with a static type on rhs,
4775 // see if static class implements all of id's protocols, directly or
4776 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004777 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004778 match = true;
4779 break;
4780 }
4781 }
4782 }
4783 if (!match)
4784 return false;
4785 }
Mike Stump11289f42009-09-09 15:08:12 +00004786
Steve Naroff8e6aee52009-07-23 01:01:38 +00004787 return true;
4788 }
Mike Stump11289f42009-09-09 15:08:12 +00004789
Steve Naroff8e6aee52009-07-23 01:01:38 +00004790 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4791 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4792
Mike Stump11289f42009-09-09 15:08:12 +00004793 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004794 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004795 // If both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004796 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4797 E = lhsOPT->qual_end(); I != E; ++I) {
4798 ObjCProtocolDecl *lhsProto = *I;
4799 bool match = false;
4800
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004801 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff8e6aee52009-07-23 01:01:38 +00004802 // see if static class implements all of id's protocols, directly or
4803 // through its super class and categories.
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004804 // First, lhs protocols in the qualifier list must be found, direct
4805 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004806 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4807 E = rhsQID->qual_end(); J != E; ++J) {
4808 ObjCProtocolDecl *rhsProto = *J;
4809 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4810 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4811 match = true;
4812 break;
4813 }
4814 }
4815 if (!match)
4816 return false;
4817 }
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004818
4819 // Static class's protocols, or its super class or category protocols
4820 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4821 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4822 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4823 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4824 // This is rather dubious but matches gcc's behavior. If lhs has
4825 // no type qualifier and its class has no static protocol(s)
4826 // assume that it is mismatch.
4827 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4828 return false;
4829 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4830 LHSInheritedProtocols.begin(),
4831 E = LHSInheritedProtocols.end(); I != E; ++I) {
4832 bool match = false;
4833 ObjCProtocolDecl *lhsProto = (*I);
4834 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4835 E = rhsQID->qual_end(); J != E; ++J) {
4836 ObjCProtocolDecl *rhsProto = *J;
4837 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4838 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4839 match = true;
4840 break;
4841 }
4842 }
4843 if (!match)
4844 return false;
4845 }
4846 }
Steve Naroff8e6aee52009-07-23 01:01:38 +00004847 return true;
4848 }
4849 return false;
4850}
4851
Eli Friedman47f77112008-08-22 00:56:42 +00004852/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004853/// compatible for assignment from RHS to LHS. This handles validation of any
4854/// protocol qualifiers on the LHS or RHS.
4855///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004856bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4857 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004858 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4859 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4860
Steve Naroff1329fa02009-07-15 18:40:39 +00004861 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004862 if (LHS->isObjCUnqualifiedIdOrClass() ||
4863 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004864 return true;
4865
John McCall8b07ec22010-05-15 11:32:37 +00004866 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004867 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4868 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004869 false);
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004870
4871 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4872 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4873 QualType(RHSOPT,0));
4874
John McCall8b07ec22010-05-15 11:32:37 +00004875 // If we have 2 user-defined types, fall into that path.
4876 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004877 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004878
Steve Naroff8e6aee52009-07-23 01:01:38 +00004879 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004880}
4881
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004882/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
Chris Lattner57540c52011-04-15 05:22:18 +00004883/// for providing type-safety for objective-c pointers used to pass/return
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004884/// arguments in block literals. When passed as arguments, passing 'A*' where
4885/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4886/// not OK. For the return type, the opposite is not OK.
4887bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4888 const ObjCObjectPointerType *LHSOPT,
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004889 const ObjCObjectPointerType *RHSOPT,
4890 bool BlockReturnType) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004891 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004892 return true;
4893
4894 if (LHSOPT->isObjCBuiltinType()) {
4895 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4896 }
4897
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004898 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004899 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4900 QualType(RHSOPT,0),
4901 false);
4902
4903 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4904 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4905 if (LHS && RHS) { // We have 2 user-defined types.
4906 if (LHS != RHS) {
4907 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004908 return BlockReturnType;
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004909 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004910 return !BlockReturnType;
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004911 }
4912 else
4913 return true;
4914 }
4915 return false;
4916}
4917
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004918/// getIntersectionOfProtocols - This routine finds the intersection of set
4919/// of protocols inherited from two distinct objective-c pointer objects.
4920/// It is used to build composite qualifier list of the composite type of
4921/// the conditional expression involving two objective-c pointer objects.
4922static
4923void getIntersectionOfProtocols(ASTContext &Context,
4924 const ObjCObjectPointerType *LHSOPT,
4925 const ObjCObjectPointerType *RHSOPT,
4926 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4927
John McCall8b07ec22010-05-15 11:32:37 +00004928 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4929 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4930 assert(LHS->getInterface() && "LHS must have an interface base");
4931 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004932
4933 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4934 unsigned LHSNumProtocols = LHS->getNumProtocols();
4935 if (LHSNumProtocols > 0)
4936 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4937 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004938 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004939 Context.CollectInheritedProtocols(LHS->getInterface(),
4940 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004941 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4942 LHSInheritedProtocols.end());
4943 }
4944
4945 unsigned RHSNumProtocols = RHS->getNumProtocols();
4946 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004947 ObjCProtocolDecl **RHSProtocols =
4948 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004949 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4950 if (InheritedProtocolSet.count(RHSProtocols[i]))
4951 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4952 }
4953 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004954 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004955 Context.CollectInheritedProtocols(RHS->getInterface(),
4956 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004957 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4958 RHSInheritedProtocols.begin(),
4959 E = RHSInheritedProtocols.end(); I != E; ++I)
4960 if (InheritedProtocolSet.count((*I)))
4961 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004962 }
4963}
4964
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004965/// areCommonBaseCompatible - Returns common base class of the two classes if
4966/// one found. Note that this is O'2 algorithm. But it will be called as the
4967/// last type comparison in a ?-exp of ObjC pointer types before a
4968/// warning is issued. So, its invokation is extremely rare.
4969QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004970 const ObjCObjectPointerType *Lptr,
4971 const ObjCObjectPointerType *Rptr) {
4972 const ObjCObjectType *LHS = Lptr->getObjectType();
4973 const ObjCObjectType *RHS = Rptr->getObjectType();
4974 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4975 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
Fariborz Jahanianb1071432011-04-18 21:16:59 +00004976 if (!LDecl || !RDecl || (LDecl == RDecl))
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004977 return QualType();
4978
Fariborz Jahanianb1071432011-04-18 21:16:59 +00004979 do {
John McCall8b07ec22010-05-15 11:32:37 +00004980 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004981 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004982 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4983 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4984
4985 QualType Result = QualType(LHS, 0);
4986 if (!Protocols.empty())
4987 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4988 Result = getObjCObjectPointerType(Result);
4989 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004990 }
Fariborz Jahanianb1071432011-04-18 21:16:59 +00004991 } while ((LDecl = LDecl->getSuperClass()));
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004992
4993 return QualType();
4994}
4995
John McCall8b07ec22010-05-15 11:32:37 +00004996bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4997 const ObjCObjectType *RHS) {
4998 assert(LHS->getInterface() && "LHS is not an interface type");
4999 assert(RHS->getInterface() && "RHS is not an interface type");
5000
Chris Lattner49af6a42008-04-07 06:51:04 +00005001 // Verify that the base decls are compatible: the RHS must be a subclass of
5002 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00005003 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00005004 return false;
Mike Stump11289f42009-09-09 15:08:12 +00005005
Chris Lattner49af6a42008-04-07 06:51:04 +00005006 // RHS must have a superset of the protocols in the LHS. If the LHS is not
5007 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00005008 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00005009 return true;
Mike Stump11289f42009-09-09 15:08:12 +00005010
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005011 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't,
5012 // more detailed analysis is required.
5013 if (RHS->getNumProtocols() == 0) {
5014 // OK, if LHS is a superclass of RHS *and*
5015 // this superclass is assignment compatible with LHS.
5016 // false otherwise.
Fariborz Jahanian240400b2011-04-12 16:34:14 +00005017 bool IsSuperClass =
5018 LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5019 if (IsSuperClass) {
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005020 // OK if conversion of LHS to SuperClass results in narrowing of types
5021 // ; i.e., SuperClass may implement at least one of the protocols
5022 // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5023 // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5024 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
Fariborz Jahanian240400b2011-04-12 16:34:14 +00005025 CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005026 // If super class has no protocols, it is not a match.
5027 if (SuperClassInheritedProtocols.empty())
5028 return false;
5029
5030 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5031 LHSPE = LHS->qual_end();
5032 LHSPI != LHSPE; LHSPI++) {
5033 bool SuperImplementsProtocol = false;
5034 ObjCProtocolDecl *LHSProto = (*LHSPI);
5035
5036 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5037 SuperClassInheritedProtocols.begin(),
5038 E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5039 ObjCProtocolDecl *SuperClassProto = (*I);
5040 if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5041 SuperImplementsProtocol = true;
5042 break;
5043 }
5044 }
5045 if (!SuperImplementsProtocol)
5046 return false;
5047 }
5048 return true;
5049 }
5050 return false;
5051 }
Mike Stump11289f42009-09-09 15:08:12 +00005052
John McCall8b07ec22010-05-15 11:32:37 +00005053 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5054 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00005055 LHSPI != LHSPE; LHSPI++) {
5056 bool RHSImplementsProtocol = false;
5057
5058 // If the RHS doesn't implement the protocol on the left, the types
5059 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00005060 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5061 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00005062 RHSPI != RHSPE; RHSPI++) {
5063 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00005064 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00005065 break;
5066 }
Steve Naroff114aecb2009-03-01 16:12:44 +00005067 }
5068 // FIXME: For better diagnostics, consider passing back the protocol name.
5069 if (!RHSImplementsProtocol)
5070 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00005071 }
Steve Naroff114aecb2009-03-01 16:12:44 +00005072 // The RHS implements all protocols listed on the LHS.
5073 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00005074}
5075
Steve Naroffb7605152009-02-12 17:52:19 +00005076bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5077 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00005078 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5079 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00005080
Steve Naroff7cae42b2009-07-10 23:34:53 +00005081 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00005082 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00005083
5084 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
5085 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00005086}
5087
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00005088bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5089 return canAssignObjCInterfaces(
5090 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5091 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5092}
5093
Mike Stump11289f42009-09-09 15:08:12 +00005094/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00005095/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00005096/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00005097/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005098bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5099 bool CompareUnqualified) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00005100 if (getLangOptions().CPlusPlus)
5101 return hasSameType(LHS, RHS);
5102
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005103 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman47f77112008-08-22 00:56:42 +00005104}
5105
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005106bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5107 return !mergeTypes(LHS, RHS, true).isNull();
5108}
5109
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005110/// mergeTransparentUnionType - if T is a transparent union type and a member
5111/// of T is compatible with SubType, return the merged type, else return
5112/// QualType()
5113QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
5114 bool OfBlockPointer,
5115 bool Unqualified) {
5116 if (const RecordType *UT = T->getAsUnionType()) {
5117 RecordDecl *UD = UT->getDecl();
5118 if (UD->hasAttr<TransparentUnionAttr>()) {
5119 for (RecordDecl::field_iterator it = UD->field_begin(),
5120 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbourne19b961d2010-12-02 21:00:06 +00005121 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005122 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
5123 if (!MT.isNull())
5124 return MT;
5125 }
5126 }
5127 }
5128
5129 return QualType();
5130}
5131
5132/// mergeFunctionArgumentTypes - merge two types which appear as function
5133/// argument types
5134QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
5135 bool OfBlockPointer,
5136 bool Unqualified) {
5137 // GNU extension: two types are compatible if they appear as a function
5138 // argument, one of the types is a transparent union type and the other
5139 // type is compatible with a union member
5140 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
5141 Unqualified);
5142 if (!lmerge.isNull())
5143 return lmerge;
5144
5145 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
5146 Unqualified);
5147 if (!rmerge.isNull())
5148 return rmerge;
5149
5150 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
5151}
5152
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005153QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005154 bool OfBlockPointer,
5155 bool Unqualified) {
John McCall9dd450b2009-09-21 23:43:11 +00005156 const FunctionType *lbase = lhs->getAs<FunctionType>();
5157 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005158 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
5159 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00005160 bool allLTypes = true;
5161 bool allRTypes = true;
5162
5163 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005164 QualType retType;
Fariborz Jahanian17825972011-02-11 18:46:17 +00005165 if (OfBlockPointer) {
5166 QualType RHS = rbase->getResultType();
5167 QualType LHS = lbase->getResultType();
5168 bool UnqualifiedResult = Unqualified;
5169 if (!UnqualifiedResult)
5170 UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005171 retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
Fariborz Jahanian17825972011-02-11 18:46:17 +00005172 }
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005173 else
John McCall8c6b56f2010-12-15 01:06:38 +00005174 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
5175 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005176 if (retType.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005177
5178 if (Unqualified)
5179 retType = retType.getUnqualifiedType();
5180
5181 CanQualType LRetType = getCanonicalType(lbase->getResultType());
5182 CanQualType RRetType = getCanonicalType(rbase->getResultType());
5183 if (Unqualified) {
5184 LRetType = LRetType.getUnqualifiedType();
5185 RRetType = RRetType.getUnqualifiedType();
5186 }
5187
5188 if (getCanonicalType(retType) != LRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00005189 allLTypes = false;
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005190 if (getCanonicalType(retType) != RRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00005191 allRTypes = false;
John McCall8c6b56f2010-12-15 01:06:38 +00005192
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00005193 // FIXME: double check this
5194 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
5195 // rbase->getRegParmAttr() != 0 &&
5196 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005197 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
5198 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
John McCall8c6b56f2010-12-15 01:06:38 +00005199
Douglas Gregor8c940862010-01-18 17:14:39 +00005200 // Compatible functions must have compatible calling conventions
John McCall8c6b56f2010-12-15 01:06:38 +00005201 if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
Douglas Gregor8c940862010-01-18 17:14:39 +00005202 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00005203
John McCall8c6b56f2010-12-15 01:06:38 +00005204 // Regparm is part of the calling convention.
Eli Friedmanc5b20b52011-04-09 08:18:08 +00005205 if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
5206 return QualType();
John McCall8c6b56f2010-12-15 01:06:38 +00005207 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
5208 return QualType();
5209
5210 // It's noreturn if either type is.
5211 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
5212 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
5213 if (NoReturn != lbaseInfo.getNoReturn())
5214 allLTypes = false;
5215 if (NoReturn != rbaseInfo.getNoReturn())
5216 allRTypes = false;
5217
5218 FunctionType::ExtInfo einfo(NoReturn,
Eli Friedmanc5b20b52011-04-09 08:18:08 +00005219 lbaseInfo.getHasRegParm(),
John McCall8c6b56f2010-12-15 01:06:38 +00005220 lbaseInfo.getRegParm(),
5221 lbaseInfo.getCC());
John McCalldb40c7f2010-12-14 08:05:40 +00005222
Eli Friedman47f77112008-08-22 00:56:42 +00005223 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00005224 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
5225 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00005226 unsigned lproto_nargs = lproto->getNumArgs();
5227 unsigned rproto_nargs = rproto->getNumArgs();
5228
5229 // Compatible functions must have the same number of arguments
5230 if (lproto_nargs != rproto_nargs)
5231 return QualType();
5232
5233 // Variadic and non-variadic functions aren't compatible
5234 if (lproto->isVariadic() != rproto->isVariadic())
5235 return QualType();
5236
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00005237 if (lproto->getTypeQuals() != rproto->getTypeQuals())
5238 return QualType();
5239
Eli Friedman47f77112008-08-22 00:56:42 +00005240 // Check argument compatibility
5241 llvm::SmallVector<QualType, 10> types;
5242 for (unsigned i = 0; i < lproto_nargs; i++) {
5243 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
5244 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005245 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
5246 OfBlockPointer,
5247 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005248 if (argtype.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005249
5250 if (Unqualified)
5251 argtype = argtype.getUnqualifiedType();
5252
Eli Friedman47f77112008-08-22 00:56:42 +00005253 types.push_back(argtype);
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005254 if (Unqualified) {
5255 largtype = largtype.getUnqualifiedType();
5256 rargtype = rargtype.getUnqualifiedType();
5257 }
5258
Chris Lattner465fa322008-10-05 17:34:18 +00005259 if (getCanonicalType(argtype) != getCanonicalType(largtype))
5260 allLTypes = false;
5261 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
5262 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00005263 }
5264 if (allLTypes) return lhs;
5265 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00005266
5267 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
5268 EPI.ExtInfo = einfo;
5269 return getFunctionType(retType, types.begin(), types.size(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00005270 }
5271
5272 if (lproto) allRTypes = false;
5273 if (rproto) allLTypes = false;
5274
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005275 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00005276 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00005277 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00005278 if (proto->isVariadic()) return QualType();
5279 // Check that the types are compatible with the types that
5280 // would result from default argument promotions (C99 6.7.5.3p15).
5281 // The only types actually affected are promotable integer
5282 // types and floats, which would be passed as a different
5283 // type depending on whether the prototype is visible.
5284 unsigned proto_nargs = proto->getNumArgs();
5285 for (unsigned i = 0; i < proto_nargs; ++i) {
5286 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00005287
5288 // Look at the promotion type of enum types, since that is the type used
5289 // to pass enum values.
5290 if (const EnumType *Enum = argTy->getAs<EnumType>())
5291 argTy = Enum->getDecl()->getPromotionType();
5292
Eli Friedman47f77112008-08-22 00:56:42 +00005293 if (argTy->isPromotableIntegerType() ||
5294 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
5295 return QualType();
5296 }
5297
5298 if (allLTypes) return lhs;
5299 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00005300
5301 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
5302 EPI.ExtInfo = einfo;
Eli Friedman47f77112008-08-22 00:56:42 +00005303 return getFunctionType(retType, proto->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005304 proto->getNumArgs(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00005305 }
5306
5307 if (allLTypes) return lhs;
5308 if (allRTypes) return rhs;
John McCall8c6b56f2010-12-15 01:06:38 +00005309 return getFunctionNoProtoType(retType, einfo);
Eli Friedman47f77112008-08-22 00:56:42 +00005310}
5311
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005312QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005313 bool OfBlockPointer,
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005314 bool Unqualified, bool BlockReturnType) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00005315 // C++ [expr]: If an expression initially has the type "reference to T", the
5316 // type is adjusted to "T" prior to any further analysis, the expression
5317 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005318 // expression is an lvalue unless the reference is an rvalue reference and
5319 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00005320 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
5321 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005322
5323 if (Unqualified) {
5324 LHS = LHS.getUnqualifiedType();
5325 RHS = RHS.getUnqualifiedType();
5326 }
Douglas Gregor21e771e2010-02-03 21:02:30 +00005327
Eli Friedman47f77112008-08-22 00:56:42 +00005328 QualType LHSCan = getCanonicalType(LHS),
5329 RHSCan = getCanonicalType(RHS);
5330
5331 // If two types are identical, they are compatible.
5332 if (LHSCan == RHSCan)
5333 return LHS;
5334
John McCall8ccfcb52009-09-24 19:53:00 +00005335 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00005336 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5337 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00005338 if (LQuals != RQuals) {
5339 // If any of these qualifiers are different, we have a type
5340 // mismatch.
5341 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5342 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5343 return QualType();
5344
5345 // Exactly one GC qualifier difference is allowed: __strong is
5346 // okay if the other type has no GC qualifier but is an Objective
5347 // C object pointer (i.e. implicitly strong by default). We fix
5348 // this by pretending that the unqualified type was actually
5349 // qualified __strong.
5350 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5351 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5352 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5353
5354 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5355 return QualType();
5356
5357 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
5358 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
5359 }
5360 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
5361 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
5362 }
Eli Friedman47f77112008-08-22 00:56:42 +00005363 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00005364 }
5365
5366 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00005367
Eli Friedmandcca6332009-06-01 01:22:52 +00005368 Type::TypeClass LHSClass = LHSCan->getTypeClass();
5369 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00005370
Chris Lattnerfd652912008-01-14 05:45:46 +00005371 // We want to consider the two function types to be the same for these
5372 // comparisons, just force one to the other.
5373 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5374 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00005375
5376 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00005377 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5378 LHSClass = Type::ConstantArray;
5379 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5380 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00005381
John McCall8b07ec22010-05-15 11:32:37 +00005382 // ObjCInterfaces are just specialized ObjCObjects.
5383 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5384 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5385
Nate Begemance4d7fc2008-04-18 23:10:10 +00005386 // Canonicalize ExtVector -> Vector.
5387 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5388 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00005389
Chris Lattner95554662008-04-07 05:43:21 +00005390 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005391 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00005392 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00005393 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00005394 // Compatibility is based on the underlying type, not the promotion
5395 // type.
John McCall9dd450b2009-09-21 23:43:11 +00005396 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005397 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5398 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005399 }
John McCall9dd450b2009-09-21 23:43:11 +00005400 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005401 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5402 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005403 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005404
Eli Friedman47f77112008-08-22 00:56:42 +00005405 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005406 }
Eli Friedman47f77112008-08-22 00:56:42 +00005407
Steve Naroffc6edcbd2008-01-09 22:43:08 +00005408 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005409 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005410#define TYPE(Class, Base)
5411#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00005412#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005413#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5414#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5415#include "clang/AST/TypeNodes.def"
5416 assert(false && "Non-canonical and dependent types shouldn't get here");
5417 return QualType();
5418
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005419 case Type::LValueReference:
5420 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005421 case Type::MemberPointer:
5422 assert(false && "C++ should never be in mergeTypes");
5423 return QualType();
5424
John McCall8b07ec22010-05-15 11:32:37 +00005425 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005426 case Type::IncompleteArray:
5427 case Type::VariableArray:
5428 case Type::FunctionProto:
5429 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005430 assert(false && "Types are eliminated above");
5431 return QualType();
5432
Chris Lattnerfd652912008-01-14 05:45:46 +00005433 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00005434 {
5435 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005436 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5437 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005438 if (Unqualified) {
5439 LHSPointee = LHSPointee.getUnqualifiedType();
5440 RHSPointee = RHSPointee.getUnqualifiedType();
5441 }
5442 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5443 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005444 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00005445 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005446 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00005447 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005448 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005449 return getPointerType(ResultType);
5450 }
Steve Naroff68e167d2008-12-10 17:49:55 +00005451 case Type::BlockPointer:
5452 {
5453 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005454 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5455 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005456 if (Unqualified) {
5457 LHSPointee = LHSPointee.getUnqualifiedType();
5458 RHSPointee = RHSPointee.getUnqualifiedType();
5459 }
5460 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5461 Unqualified);
Steve Naroff68e167d2008-12-10 17:49:55 +00005462 if (ResultType.isNull()) return QualType();
5463 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5464 return LHS;
5465 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5466 return RHS;
5467 return getBlockPointerType(ResultType);
5468 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005469 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00005470 {
5471 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5472 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5473 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5474 return QualType();
5475
5476 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5477 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005478 if (Unqualified) {
5479 LHSElem = LHSElem.getUnqualifiedType();
5480 RHSElem = RHSElem.getUnqualifiedType();
5481 }
5482
5483 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005484 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00005485 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5486 return LHS;
5487 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5488 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00005489 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5490 ArrayType::ArraySizeModifier(), 0);
5491 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5492 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005493 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5494 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00005495 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5496 return LHS;
5497 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5498 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005499 if (LVAT) {
5500 // FIXME: This isn't correct! But tricky to implement because
5501 // the array's size has to be the size of LHS, but the type
5502 // has to be different.
5503 return LHS;
5504 }
5505 if (RVAT) {
5506 // FIXME: This isn't correct! But tricky to implement because
5507 // the array's size has to be the size of RHS, but the type
5508 // has to be different.
5509 return RHS;
5510 }
Eli Friedman3e62c212008-08-22 01:48:21 +00005511 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5512 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00005513 return getIncompleteArrayType(ResultType,
5514 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005515 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005516 case Type::FunctionNoProto:
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005517 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005518 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005519 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00005520 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00005521 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005522 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00005523 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00005524 case Type::Complex:
5525 // Distinct complex types are incompatible.
5526 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005527 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00005528 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00005529 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5530 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00005531 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00005532 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00005533 case Type::ObjCObject: {
5534 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00005535 // FIXME: This should be type compatibility, e.g. whether
5536 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00005537 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5538 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5539 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00005540 return LHS;
5541
Eli Friedman47f77112008-08-22 00:56:42 +00005542 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00005543 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00005544 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005545 if (OfBlockPointer) {
5546 if (canAssignObjCInterfacesInBlockPointer(
5547 LHS->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005548 RHS->getAs<ObjCObjectPointerType>(),
5549 BlockReturnType))
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005550 return LHS;
5551 return QualType();
5552 }
John McCall9dd450b2009-09-21 23:43:11 +00005553 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5554 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00005555 return LHS;
5556
Steve Naroffc68cfcf2008-12-10 22:14:21 +00005557 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005558 }
Steve Naroff32e44c02007-10-15 20:41:53 +00005559 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005560
5561 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005562}
Ted Kremenekfc581a92007-10-31 17:10:13 +00005563
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005564/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5565/// 'RHS' attributes and returns the merged version; including for function
5566/// return types.
5567QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5568 QualType LHSCan = getCanonicalType(LHS),
5569 RHSCan = getCanonicalType(RHS);
5570 // If two types are identical, they are compatible.
5571 if (LHSCan == RHSCan)
5572 return LHS;
5573 if (RHSCan->isFunctionType()) {
5574 if (!LHSCan->isFunctionType())
5575 return QualType();
5576 QualType OldReturnType =
5577 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5578 QualType NewReturnType =
5579 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5580 QualType ResReturnType =
5581 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5582 if (ResReturnType.isNull())
5583 return QualType();
5584 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5585 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5586 // In either case, use OldReturnType to build the new function type.
5587 const FunctionType *F = LHS->getAs<FunctionType>();
5588 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
John McCalldb40c7f2010-12-14 08:05:40 +00005589 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5590 EPI.ExtInfo = getFunctionExtInfo(LHS);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005591 QualType ResultType
5592 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005593 FPT->getNumArgs(), EPI);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005594 return ResultType;
5595 }
5596 }
5597 return QualType();
5598 }
5599
5600 // If the qualifiers are different, the types can still be merged.
5601 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5602 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5603 if (LQuals != RQuals) {
5604 // If any of these qualifiers are different, we have a type mismatch.
5605 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5606 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5607 return QualType();
5608
5609 // Exactly one GC qualifier difference is allowed: __strong is
5610 // okay if the other type has no GC qualifier but is an Objective
5611 // C object pointer (i.e. implicitly strong by default). We fix
5612 // this by pretending that the unqualified type was actually
5613 // qualified __strong.
5614 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5615 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5616 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5617
5618 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5619 return QualType();
5620
5621 if (GC_L == Qualifiers::Strong)
5622 return LHS;
5623 if (GC_R == Qualifiers::Strong)
5624 return RHS;
5625 return QualType();
5626 }
5627
5628 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5629 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5630 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5631 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5632 if (ResQT == LHSBaseQT)
5633 return LHS;
5634 if (ResQT == RHSBaseQT)
5635 return RHS;
5636 }
5637 return QualType();
5638}
5639
Chris Lattner4ba0cef2008-04-07 07:01:58 +00005640//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005641// Integer Predicates
5642//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00005643
Jay Foad39c79802011-01-12 09:06:06 +00005644unsigned ASTContext::getIntWidth(QualType T) const {
John McCall424cec92011-01-19 06:33:43 +00005645 if (const EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00005646 T = ET->getDecl()->getIntegerType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00005647 if (T->isBooleanType())
5648 return 1;
Eli Friedman1efaaea2009-02-13 02:31:07 +00005649 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005650 return (unsigned)getTypeSize(T);
5651}
5652
5653QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00005654 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00005655
5656 // Turn <4 x signed int> -> <4 x unsigned int>
5657 if (const VectorType *VTy = T->getAs<VectorType>())
5658 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsonaeb56442010-11-10 21:56:12 +00005659 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattnerec3a1562009-10-17 20:33:28 +00005660
5661 // For enums, we return the unsigned version of the base type.
5662 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005663 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00005664
5665 const BuiltinType *BTy = T->getAs<BuiltinType>();
5666 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005667 switch (BTy->getKind()) {
5668 case BuiltinType::Char_S:
5669 case BuiltinType::SChar:
5670 return UnsignedCharTy;
5671 case BuiltinType::Short:
5672 return UnsignedShortTy;
5673 case BuiltinType::Int:
5674 return UnsignedIntTy;
5675 case BuiltinType::Long:
5676 return UnsignedLongTy;
5677 case BuiltinType::LongLong:
5678 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00005679 case BuiltinType::Int128:
5680 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005681 default:
5682 assert(0 && "Unexpected signed integer type");
5683 return QualType();
5684 }
5685}
5686
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005687ASTMutationListener::~ASTMutationListener() { }
5688
Chris Lattnerecd79c62009-06-14 00:45:47 +00005689
5690//===----------------------------------------------------------------------===//
5691// Builtin Type Computation
5692//===----------------------------------------------------------------------===//
5693
5694/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattnerdc226c22010-10-01 22:42:38 +00005695/// pointer over the consumed characters. This returns the resultant type. If
5696/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5697/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5698/// a vector of "i*".
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005699///
5700/// RequiresICE is filled in on return to indicate whether the value is required
5701/// to be an Integer Constant Expression.
Jay Foad39c79802011-01-12 09:06:06 +00005702static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00005703 ASTContext::GetBuiltinTypeError &Error,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005704 bool &RequiresICE,
Chris Lattnerdc226c22010-10-01 22:42:38 +00005705 bool AllowTypeModifiers) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005706 // Modifiers.
5707 int HowLong = 0;
5708 bool Signed = false, Unsigned = false;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005709 RequiresICE = false;
Chris Lattner84733392010-10-01 07:13:18 +00005710
Chris Lattnerdc226c22010-10-01 22:42:38 +00005711 // Read the prefixed modifiers first.
Chris Lattnerecd79c62009-06-14 00:45:47 +00005712 bool Done = false;
5713 while (!Done) {
5714 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00005715 default: Done = true; --Str; break;
Chris Lattner84733392010-10-01 07:13:18 +00005716 case 'I':
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005717 RequiresICE = true;
Chris Lattner84733392010-10-01 07:13:18 +00005718 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005719 case 'S':
5720 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5721 assert(!Signed && "Can't use 'S' modifier multiple times!");
5722 Signed = true;
5723 break;
5724 case 'U':
5725 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5726 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5727 Unsigned = true;
5728 break;
5729 case 'L':
5730 assert(HowLong <= 2 && "Can't have LLLL modifier");
5731 ++HowLong;
5732 break;
5733 }
5734 }
5735
5736 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005737
Chris Lattnerecd79c62009-06-14 00:45:47 +00005738 // Read the base type.
5739 switch (*Str++) {
5740 default: assert(0 && "Unknown builtin type letter!");
5741 case 'v':
5742 assert(HowLong == 0 && !Signed && !Unsigned &&
5743 "Bad modifiers used with 'v'!");
5744 Type = Context.VoidTy;
5745 break;
5746 case 'f':
5747 assert(HowLong == 0 && !Signed && !Unsigned &&
5748 "Bad modifiers used with 'f'!");
5749 Type = Context.FloatTy;
5750 break;
5751 case 'd':
5752 assert(HowLong < 2 && !Signed && !Unsigned &&
5753 "Bad modifiers used with 'd'!");
5754 if (HowLong)
5755 Type = Context.LongDoubleTy;
5756 else
5757 Type = Context.DoubleTy;
5758 break;
5759 case 's':
5760 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5761 if (Unsigned)
5762 Type = Context.UnsignedShortTy;
5763 else
5764 Type = Context.ShortTy;
5765 break;
5766 case 'i':
5767 if (HowLong == 3)
5768 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5769 else if (HowLong == 2)
5770 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5771 else if (HowLong == 1)
5772 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5773 else
5774 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5775 break;
5776 case 'c':
5777 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5778 if (Signed)
5779 Type = Context.SignedCharTy;
5780 else if (Unsigned)
5781 Type = Context.UnsignedCharTy;
5782 else
5783 Type = Context.CharTy;
5784 break;
5785 case 'b': // boolean
5786 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5787 Type = Context.BoolTy;
5788 break;
5789 case 'z': // size_t.
5790 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5791 Type = Context.getSizeType();
5792 break;
5793 case 'F':
5794 Type = Context.getCFConstantStringType();
5795 break;
Fariborz Jahaniand11da7e2010-11-09 21:38:20 +00005796 case 'G':
5797 Type = Context.getObjCIdType();
5798 break;
5799 case 'H':
5800 Type = Context.getObjCSelType();
5801 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005802 case 'a':
5803 Type = Context.getBuiltinVaListType();
5804 assert(!Type.isNull() && "builtin va list type not initialized!");
5805 break;
5806 case 'A':
5807 // This is a "reference" to a va_list; however, what exactly
5808 // this means depends on how va_list is defined. There are two
5809 // different kinds of va_list: ones passed by value, and ones
5810 // passed by reference. An example of a by-value va_list is
5811 // x86, where va_list is a char*. An example of by-ref va_list
5812 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5813 // we want this argument to be a char*&; for x86-64, we want
5814 // it to be a __va_list_tag*.
5815 Type = Context.getBuiltinVaListType();
5816 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005817 if (Type->isArrayType())
Chris Lattnerecd79c62009-06-14 00:45:47 +00005818 Type = Context.getArrayDecayedType(Type);
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005819 else
Chris Lattnerecd79c62009-06-14 00:45:47 +00005820 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005821 break;
5822 case 'V': {
5823 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005824 unsigned NumElements = strtoul(Str, &End, 10);
5825 assert(End != Str && "Missing vector size");
Chris Lattnerecd79c62009-06-14 00:45:47 +00005826 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005827
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005828 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5829 RequiresICE, false);
5830 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattnerdc226c22010-10-01 22:42:38 +00005831
5832 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner37141f42010-06-23 06:00:24 +00005833 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00005834 VectorType::GenericVector);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005835 break;
5836 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005837 case 'X': {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005838 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5839 false);
5840 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005841 Type = Context.getComplexType(ElementType);
5842 break;
5843 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005844 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005845 Type = Context.getFILEType();
5846 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005847 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005848 return QualType();
5849 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005850 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005851 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005852 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005853 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005854 else
5855 Type = Context.getjmp_bufType();
5856
Mike Stump2adb4da2009-07-28 23:47:15 +00005857 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005858 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005859 return QualType();
5860 }
5861 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005862 }
Mike Stump11289f42009-09-09 15:08:12 +00005863
Chris Lattnerdc226c22010-10-01 22:42:38 +00005864 // If there are modifiers and if we're allowed to parse them, go for it.
5865 Done = !AllowTypeModifiers;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005866 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005867 switch (char c = *Str++) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005868 default: Done = true; --Str; break;
5869 case '*':
5870 case '&': {
5871 // Both pointers and references can have their pointee types
5872 // qualified with an address space.
5873 char *End;
5874 unsigned AddrSpace = strtoul(Str, &End, 10);
5875 if (End != Str && AddrSpace != 0) {
5876 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5877 Str = End;
5878 }
5879 if (c == '*')
5880 Type = Context.getPointerType(Type);
5881 else
5882 Type = Context.getLValueReferenceType(Type);
5883 break;
5884 }
5885 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5886 case 'C':
5887 Type = Type.withConst();
5888 break;
5889 case 'D':
5890 Type = Context.getVolatileType(Type);
5891 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005892 }
5893 }
Chris Lattner84733392010-10-01 07:13:18 +00005894
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005895 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner84733392010-10-01 07:13:18 +00005896 "Integer constant 'I' type must be an integer");
Mike Stump11289f42009-09-09 15:08:12 +00005897
Chris Lattnerecd79c62009-06-14 00:45:47 +00005898 return Type;
5899}
5900
5901/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattnerdc226c22010-10-01 22:42:38 +00005902QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005903 GetBuiltinTypeError &Error,
Jay Foad39c79802011-01-12 09:06:06 +00005904 unsigned *IntegerConstantArgs) const {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005905 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump11289f42009-09-09 15:08:12 +00005906
Chris Lattnerecd79c62009-06-14 00:45:47 +00005907 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005908
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005909 bool RequiresICE = false;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005910 Error = GE_None;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005911 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5912 RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005913 if (Error != GE_None)
5914 return QualType();
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005915
5916 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5917
Chris Lattnerecd79c62009-06-14 00:45:47 +00005918 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005919 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005920 if (Error != GE_None)
5921 return QualType();
5922
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005923 // If this argument is required to be an IntegerConstantExpression and the
5924 // caller cares, fill in the bitmask we return.
5925 if (RequiresICE && IntegerConstantArgs)
5926 *IntegerConstantArgs |= 1 << ArgTypes.size();
5927
Chris Lattnerecd79c62009-06-14 00:45:47 +00005928 // Do array -> pointer decay. The builtin should use the decayed type.
5929 if (Ty->isArrayType())
5930 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005931
Chris Lattnerecd79c62009-06-14 00:45:47 +00005932 ArgTypes.push_back(Ty);
5933 }
5934
5935 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5936 "'.' should only occur at end of builtin type list!");
5937
John McCall991eb4b2010-12-21 00:44:39 +00005938 FunctionType::ExtInfo EI;
5939 if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
5940
5941 bool Variadic = (TypeStr[0] == '.');
5942
5943 // We really shouldn't be making a no-proto type here, especially in C++.
5944 if (ArgTypes.empty() && Variadic)
5945 return getFunctionNoProtoType(ResType, EI);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005946
John McCalldb40c7f2010-12-14 08:05:40 +00005947 FunctionProtoType::ExtProtoInfo EPI;
John McCall991eb4b2010-12-21 00:44:39 +00005948 EPI.ExtInfo = EI;
5949 EPI.Variadic = Variadic;
John McCalldb40c7f2010-12-14 08:05:40 +00005950
5951 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005952}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005953
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005954GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5955 GVALinkage External = GVA_StrongExternal;
5956
5957 Linkage L = FD->getLinkage();
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005958 switch (L) {
5959 case NoLinkage:
5960 case InternalLinkage:
5961 case UniqueExternalLinkage:
5962 return GVA_Internal;
5963
5964 case ExternalLinkage:
5965 switch (FD->getTemplateSpecializationKind()) {
5966 case TSK_Undeclared:
5967 case TSK_ExplicitSpecialization:
5968 External = GVA_StrongExternal;
5969 break;
5970
5971 case TSK_ExplicitInstantiationDefinition:
5972 return GVA_ExplicitTemplateInstantiation;
5973
5974 case TSK_ExplicitInstantiationDeclaration:
5975 case TSK_ImplicitInstantiation:
5976 External = GVA_TemplateInstantiation;
5977 break;
5978 }
5979 }
5980
5981 if (!FD->isInlined())
5982 return External;
5983
5984 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5985 // GNU or C99 inline semantics. Determine whether this symbol should be
5986 // externally visible.
5987 if (FD->isInlineDefinitionExternallyVisible())
5988 return External;
5989
5990 // C99 inline semantics, where the symbol is not externally visible.
5991 return GVA_C99Inline;
5992 }
5993
5994 // C++0x [temp.explicit]p9:
5995 // [ Note: The intent is that an inline function that is the subject of
5996 // an explicit instantiation declaration will still be implicitly
5997 // instantiated when used so that the body can be considered for
5998 // inlining, but that no out-of-line copy of the inline function would be
5999 // generated in the translation unit. -- end note ]
6000 if (FD->getTemplateSpecializationKind()
6001 == TSK_ExplicitInstantiationDeclaration)
6002 return GVA_C99Inline;
6003
6004 return GVA_CXXInline;
6005}
6006
6007GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
6008 // If this is a static data member, compute the kind of template
6009 // specialization. Otherwise, this variable is not part of a
6010 // template.
6011 TemplateSpecializationKind TSK = TSK_Undeclared;
6012 if (VD->isStaticDataMember())
6013 TSK = VD->getTemplateSpecializationKind();
6014
6015 Linkage L = VD->getLinkage();
6016 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
6017 VD->getType()->getLinkage() == UniqueExternalLinkage)
6018 L = UniqueExternalLinkage;
6019
6020 switch (L) {
6021 case NoLinkage:
6022 case InternalLinkage:
6023 case UniqueExternalLinkage:
6024 return GVA_Internal;
6025
6026 case ExternalLinkage:
6027 switch (TSK) {
6028 case TSK_Undeclared:
6029 case TSK_ExplicitSpecialization:
6030 return GVA_StrongExternal;
6031
6032 case TSK_ExplicitInstantiationDeclaration:
6033 llvm_unreachable("Variable should not be instantiated");
6034 // Fall through to treat this like any other instantiation.
6035
6036 case TSK_ExplicitInstantiationDefinition:
6037 return GVA_ExplicitTemplateInstantiation;
6038
6039 case TSK_ImplicitInstantiation:
6040 return GVA_TemplateInstantiation;
6041 }
6042 }
6043
6044 return GVA_StrongExternal;
6045}
6046
Argyrios Kyrtzidisc9049332010-07-29 20:08:05 +00006047bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006048 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
6049 if (!VD->isFileVarDecl())
6050 return false;
6051 } else if (!isa<FunctionDecl>(D))
6052 return false;
6053
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00006054 // Weak references don't produce any output by themselves.
6055 if (D->hasAttr<WeakRefAttr>())
6056 return false;
6057
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006058 // Aliases and used decls are required.
6059 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
6060 return true;
6061
6062 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
6063 // Forward declarations aren't required.
6064 if (!FD->isThisDeclarationADefinition())
6065 return false;
6066
6067 // Constructors and destructors are required.
6068 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
6069 return true;
6070
6071 // The key function for a class is required.
6072 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
6073 const CXXRecordDecl *RD = MD->getParent();
6074 if (MD->isOutOfLine() && RD->isDynamicClass()) {
6075 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
6076 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
6077 return true;
6078 }
6079 }
6080
6081 GVALinkage Linkage = GetGVALinkageForFunction(FD);
6082
6083 // static, static inline, always_inline, and extern inline functions can
6084 // always be deferred. Normal inline functions can be deferred in C99/C++.
6085 // Implicit template instantiations can also be deferred in C++.
6086 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
6087 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
6088 return false;
6089 return true;
6090 }
6091
6092 const VarDecl *VD = cast<VarDecl>(D);
6093 assert(VD->isFileVarDecl() && "Expected file scoped var");
6094
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00006095 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
6096 return false;
6097
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006098 // Structs that have non-trivial constructors or destructors are required.
6099
6100 // FIXME: Handle references.
6101 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
6102 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +00006103 if (RD->hasDefinition() &&
6104 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006105 return true;
6106 }
6107 }
6108
6109 GVALinkage L = GetGVALinkageForVariable(VD);
6110 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
6111 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
6112 return false;
6113 }
6114
6115 return true;
6116}
Charles Davis53c59df2010-08-16 03:33:14 +00006117
Charles Davis99202b32010-11-09 18:04:24 +00006118CallingConv ASTContext::getDefaultMethodCallConv() {
6119 // Pass through to the C++ ABI object
6120 return ABI->getDefaultMethodCallConv();
6121}
6122
Jay Foad39c79802011-01-12 09:06:06 +00006123bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
Anders Carlsson60a62632010-11-25 01:51:53 +00006124 // Pass through to the C++ ABI object
6125 return ABI->isNearlyEmpty(RD);
6126}
6127
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00006128MangleContext *ASTContext::createMangleContext() {
6129 switch (Target.getCXXABI()) {
6130 case CXXABI_ARM:
6131 case CXXABI_Itanium:
6132 return createItaniumMangleContext(*this, getDiagnostics());
6133 case CXXABI_Microsoft:
6134 return createMicrosoftMangleContext(*this, getDiagnostics());
6135 }
6136 assert(0 && "Unsupported ABI");
6137 return 0;
6138}
6139
Charles Davis53c59df2010-08-16 03:33:14 +00006140CXXABI::~CXXABI() {}
Ted Kremenekf5df0ce2011-04-28 04:53:38 +00006141
6142size_t ASTContext::getSideTableAllocatedMemory() const {
6143 size_t bytes = 0;
6144 bytes += ASTRecordLayouts.getMemorySize();
6145 bytes += ObjCLayouts.getMemorySize();
6146 bytes += KeyFunctions.getMemorySize();
6147 bytes += ObjCImpls.getMemorySize();
6148 bytes += BlockVarCopyInits.getMemorySize();
6149 bytes += DeclAttrs.getMemorySize();
6150 bytes += InstantiatedFromStaticDataMember.getMemorySize();
6151 bytes += InstantiatedFromUsingDecl.getMemorySize();
6152 bytes += InstantiatedFromUsingShadowDecl.getMemorySize();
6153 bytes += InstantiatedFromUnnamedFieldDecl.getMemorySize();
6154 return bytes;
6155}
6156