blob: 717c3a8ffee14858181b861d475bf3f610ebbd36 [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
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000546bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
547 const FieldDecl *LastFD) const {
548 return (FD->isBitField() && LastFD && LastFD->isBitField() &&
Fariborz Jahanian759e4a12011-05-03 22:07:14 +0000549 FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() == 0 &&
550 LastFD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() != 0);
Fariborz Jahanianeb397412011-05-02 17:20:56 +0000551
552}
553
Fariborz Jahanian84335f72011-05-04 18:51:37 +0000554bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
555 const FieldDecl *LastFD) const {
556 return (FD->isBitField() && LastFD && LastFD->isBitField() &&
557 FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() &&
558 LastFD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue());
559}
560
Douglas Gregor832940b2010-03-02 23:58:15 +0000561ASTContext::overridden_cxx_method_iterator
562ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
563 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
564 = OverriddenMethods.find(Method);
565 if (Pos == OverriddenMethods.end())
566 return 0;
567
568 return Pos->second.begin();
569}
570
571ASTContext::overridden_cxx_method_iterator
572ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
573 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
574 = OverriddenMethods.find(Method);
575 if (Pos == OverriddenMethods.end())
576 return 0;
577
578 return Pos->second.end();
579}
580
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000581unsigned
582ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
583 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
584 = OverriddenMethods.find(Method);
585 if (Pos == OverriddenMethods.end())
586 return 0;
587
588 return Pos->second.size();
589}
590
Douglas Gregor832940b2010-03-02 23:58:15 +0000591void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
592 const CXXMethodDecl *Overridden) {
593 OverriddenMethods[Method].push_back(Overridden);
594}
595
Chris Lattner53cfe802007-07-18 17:52:12 +0000596//===----------------------------------------------------------------------===//
597// Type Sizing and Analysis
598//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000599
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000600/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
601/// scalar floating point type.
602const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000603 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000604 assert(BT && "Not a floating point type!");
605 switch (BT->getKind()) {
606 default: assert(0 && "Not a floating point type!");
607 case BuiltinType::Float: return Target.getFloatFormat();
608 case BuiltinType::Double: return Target.getDoubleFormat();
609 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
610 }
611}
612
Ken Dyck160146e2010-01-27 17:10:57 +0000613/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000614/// specified decl. Note that bitfields do not have a valid alignment, so
615/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000616/// If @p RefAsPointee, references are treated like their underlying type
617/// (for alignof), else they're treated like pointers (for CodeGen).
Jay Foad39c79802011-01-12 09:06:06 +0000618CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
Eli Friedman19a546c2009-02-22 02:56:25 +0000619 unsigned Align = Target.getCharWidth();
620
John McCall94268702010-10-08 18:24:19 +0000621 bool UseAlignAttrOnly = false;
622 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
623 Align = AlignFromAttr;
Eli Friedman19a546c2009-02-22 02:56:25 +0000624
John McCall94268702010-10-08 18:24:19 +0000625 // __attribute__((aligned)) can increase or decrease alignment
626 // *except* on a struct or struct member, where it only increases
627 // alignment unless 'packed' is also specified.
628 //
629 // It is an error for [[align]] to decrease alignment, so we can
630 // ignore that possibility; Sema should diagnose it.
631 if (isa<FieldDecl>(D)) {
632 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
633 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
634 } else {
635 UseAlignAttrOnly = true;
636 }
637 }
638
John McCall4e819612011-01-20 07:57:12 +0000639 // If we're using the align attribute only, just ignore everything
640 // else about the declaration and its type.
John McCall94268702010-10-08 18:24:19 +0000641 if (UseAlignAttrOnly) {
John McCall4e819612011-01-20 07:57:12 +0000642 // do nothing
643
John McCall94268702010-10-08 18:24:19 +0000644 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner68061312009-01-24 21:53:27 +0000645 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000646 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000647 if (RefAsPointee)
648 T = RT->getPointeeType();
649 else
650 T = getPointerType(RT->getPointeeType());
651 }
652 if (!T->isIncompleteType() && !T->isFunctionType()) {
John McCall33ddac02011-01-19 10:06:00 +0000653 // Adjust alignments of declarations with array type by the
654 // large-array alignment on the target.
Rafael Espindolae971b9a2010-06-04 23:15:27 +0000655 unsigned MinWidth = Target.getLargeArrayMinWidth();
John McCall33ddac02011-01-19 10:06:00 +0000656 const ArrayType *arrayType;
657 if (MinWidth && (arrayType = getAsArrayType(T))) {
658 if (isa<VariableArrayType>(arrayType))
659 Align = std::max(Align, Target.getLargeArrayAlign());
660 else if (isa<ConstantArrayType>(arrayType) &&
661 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
662 Align = std::max(Align, Target.getLargeArrayAlign());
Eli Friedman19a546c2009-02-22 02:56:25 +0000663
John McCall33ddac02011-01-19 10:06:00 +0000664 // Walk through any array types while we're at it.
665 T = getBaseElementType(arrayType);
666 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000667 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
668 }
John McCall4e819612011-01-20 07:57:12 +0000669
670 // Fields can be subject to extra alignment constraints, like if
671 // the field is packed, the struct is packed, or the struct has a
672 // a max-field-alignment constraint (#pragma pack). So calculate
673 // the actual alignment of the field within the struct, and then
674 // (as we're expected to) constrain that by the alignment of the type.
675 if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
676 // So calculate the alignment of the field.
677 const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
678
679 // Start with the record's overall alignment.
Ken Dyck7ad11e72011-02-15 02:32:40 +0000680 unsigned fieldAlign = toBits(layout.getAlignment());
John McCall4e819612011-01-20 07:57:12 +0000681
682 // Use the GCD of that and the offset within the record.
683 uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
684 if (offset > 0) {
685 // Alignment is always a power of 2, so the GCD will be a power of 2,
686 // which means we get to do this crazy thing instead of Euclid's.
687 uint64_t lowBitOfOffset = offset & (~offset + 1);
688 if (lowBitOfOffset < fieldAlign)
689 fieldAlign = static_cast<unsigned>(lowBitOfOffset);
690 }
691
692 Align = std::min(Align, fieldAlign);
Charles Davis3fc51072010-02-23 04:52:00 +0000693 }
Chris Lattner68061312009-01-24 21:53:27 +0000694 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000695
Ken Dyckcc56c542011-01-15 18:38:59 +0000696 return toCharUnitsFromBits(Align);
Chris Lattner68061312009-01-24 21:53:27 +0000697}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000698
John McCall87fe5d52010-05-20 01:18:31 +0000699std::pair<CharUnits, CharUnits>
Ken Dyckd24099d2011-02-20 01:55:18 +0000700ASTContext::getTypeInfoInChars(const Type *T) const {
John McCall87fe5d52010-05-20 01:18:31 +0000701 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
Ken Dyckcc56c542011-01-15 18:38:59 +0000702 return std::make_pair(toCharUnitsFromBits(Info.first),
703 toCharUnitsFromBits(Info.second));
John McCall87fe5d52010-05-20 01:18:31 +0000704}
705
706std::pair<CharUnits, CharUnits>
Ken Dyckd24099d2011-02-20 01:55:18 +0000707ASTContext::getTypeInfoInChars(QualType T) const {
John McCall87fe5d52010-05-20 01:18:31 +0000708 return getTypeInfoInChars(T.getTypePtr());
709}
710
Chris Lattner983a8bb2007-07-13 22:13:22 +0000711/// getTypeSize - Return the size of the specified type, in bits. This method
712/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000713///
714/// FIXME: Pointers into different addr spaces could have different sizes and
715/// alignment requirements: getPointerInfo should take an AddrSpace, this
716/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000717std::pair<uint64_t, unsigned>
Jay Foad39c79802011-01-12 09:06:06 +0000718ASTContext::getTypeInfo(const Type *T) const {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000719 uint64_t Width=0;
720 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000721 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000722#define TYPE(Class, Base)
723#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000724#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000725#define DEPENDENT_TYPE(Class, Base) case Type::Class:
726#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000727 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000728 break;
729
Chris Lattner355332d2007-07-13 22:27:08 +0000730 case Type::FunctionNoProto:
731 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000732 // GCC extension: alignof(function) = 32 bits
733 Width = 0;
734 Align = 32;
735 break;
736
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000737 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000738 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000739 Width = 0;
740 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
741 break;
742
Steve Naroff5c131802007-08-30 01:06:46 +0000743 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000744 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattner37e05872008-03-05 18:54:05 +0000746 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000747 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000748 Align = EltInfo.second;
Argyrios Kyrtzidisae40e4e2011-04-26 21:05:39 +0000749 Width = llvm::RoundUpToAlignment(Width, Align);
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000750 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000751 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000752 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000753 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000754 const VectorType *VT = cast<VectorType>(T);
755 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
756 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000757 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000758 // If the alignment is not a power of 2, round up to the next power of 2.
759 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000760 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000761 Align = llvm::NextPowerOf2(Align);
762 Width = llvm::RoundUpToAlignment(Width, Align);
763 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000764 break;
765 }
Chris Lattner647fb222007-07-18 18:26:58 +0000766
Chris Lattner7570e9c2008-03-08 08:52:55 +0000767 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000768 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000769 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000770 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000771 // GCC extension: alignof(void) = 8 bits.
772 Width = 0;
773 Align = 8;
774 break;
775
Chris Lattner6a4f7452007-12-19 19:23:28 +0000776 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000777 Width = Target.getBoolWidth();
778 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000779 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000780 case BuiltinType::Char_S:
781 case BuiltinType::Char_U:
782 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000783 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000784 Width = Target.getCharWidth();
785 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000786 break;
Chris Lattnerad3467e2010-12-25 23:25:43 +0000787 case BuiltinType::WChar_S:
788 case BuiltinType::WChar_U:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000789 Width = Target.getWCharWidth();
790 Align = Target.getWCharAlign();
791 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000792 case BuiltinType::Char16:
793 Width = Target.getChar16Width();
794 Align = Target.getChar16Align();
795 break;
796 case BuiltinType::Char32:
797 Width = Target.getChar32Width();
798 Align = Target.getChar32Align();
799 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000800 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000801 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000802 Width = Target.getShortWidth();
803 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000804 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000805 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000806 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000807 Width = Target.getIntWidth();
808 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000809 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000810 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000811 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000812 Width = Target.getLongWidth();
813 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000814 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000815 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000816 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000817 Width = Target.getLongLongWidth();
818 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000819 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000820 case BuiltinType::Int128:
821 case BuiltinType::UInt128:
822 Width = 128;
823 Align = 128; // int128_t is 128-bit aligned on all targets.
824 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000825 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000826 Width = Target.getFloatWidth();
827 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000828 break;
829 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000830 Width = Target.getDoubleWidth();
831 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000832 break;
833 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000834 Width = Target.getLongDoubleWidth();
835 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000836 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000837 case BuiltinType::NullPtr:
838 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
839 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000840 break;
Fariborz Jahanian9c6a39e2010-08-02 18:03:20 +0000841 case BuiltinType::ObjCId:
842 case BuiltinType::ObjCClass:
843 case BuiltinType::ObjCSel:
844 Width = Target.getPointerWidth(0);
845 Align = Target.getPointerAlign(0);
846 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000847 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000848 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000849 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000850 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000851 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000852 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000853 case Type::BlockPointer: {
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000854 unsigned AS = getTargetAddressSpace(
855 cast<BlockPointerType>(T)->getPointeeType());
Steve Naroff921a45c2008-09-24 15:05:44 +0000856 Width = Target.getPointerWidth(AS);
857 Align = Target.getPointerAlign(AS);
858 break;
859 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000860 case Type::LValueReference:
861 case Type::RValueReference: {
862 // alignof and sizeof should never enter this code path here, so we go
863 // the pointer route.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000864 unsigned AS = getTargetAddressSpace(
865 cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000866 Width = Target.getPointerWidth(AS);
867 Align = Target.getPointerAlign(AS);
868 break;
869 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000870 case Type::Pointer: {
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000871 unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000872 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000873 Align = Target.getPointerAlign(AS);
874 break;
875 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000876 case Type::MemberPointer: {
Charles Davis53c59df2010-08-16 03:33:14 +0000877 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000878 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000879 getTypeInfo(getPointerDiffType());
Charles Davis53c59df2010-08-16 03:33:14 +0000880 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson32440a02009-05-17 02:06:04 +0000881 Align = PtrDiffInfo.second;
882 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000883 }
Chris Lattner647fb222007-07-18 18:26:58 +0000884 case Type::Complex: {
885 // Complex types have the same alignment as their elements, but twice the
886 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000887 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000888 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000889 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000890 Align = EltInfo.second;
891 break;
892 }
John McCall8b07ec22010-05-15 11:32:37 +0000893 case Type::ObjCObject:
894 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Pateldbb72632008-06-04 21:54:36 +0000895 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000896 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000897 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
Ken Dyckb0fcc592011-02-11 01:54:29 +0000898 Width = toBits(Layout.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000899 Align = toBits(Layout.getAlignment());
Devang Pateldbb72632008-06-04 21:54:36 +0000900 break;
901 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000902 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000903 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000904 const TagType *TT = cast<TagType>(T);
905
906 if (TT->getDecl()->isInvalidDecl()) {
Douglas Gregor7f971892011-04-20 17:29:44 +0000907 Width = 8;
908 Align = 8;
Chris Lattner572100b2008-08-09 21:35:13 +0000909 break;
910 }
Mike Stump11289f42009-09-09 15:08:12 +0000911
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000912 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000913 return getTypeInfo(ET->getDecl()->getIntegerType());
914
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000915 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000916 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
Ken Dyckb0fcc592011-02-11 01:54:29 +0000917 Width = toBits(Layout.getSize());
Ken Dyck7ad11e72011-02-15 02:32:40 +0000918 Align = toBits(Layout.getAlignment());
Chris Lattner49a953a2007-07-23 22:46:22 +0000919 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000920 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000921
Chris Lattner63d2b362009-10-22 05:17:15 +0000922 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000923 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
924 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000925
Richard Smith30482bc2011-02-20 03:19:35 +0000926 case Type::Auto: {
927 const AutoType *A = cast<AutoType>(T);
928 assert(A->isDeduced() && "Cannot request the size of a dependent type");
Matt Beaumont-Gay7a24210e2011-02-22 20:00:16 +0000929 return getTypeInfo(A->getDeducedType().getTypePtr());
Richard Smith30482bc2011-02-20 03:19:35 +0000930 }
931
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000932 case Type::Paren:
933 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
934
Douglas Gregoref462e62009-04-30 17:32:17 +0000935 case Type::Typedef: {
Richard Smithdda56e42011-04-15 14:24:37 +0000936 const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregorf5bae222010-08-27 00:11:28 +0000937 std::pair<uint64_t, unsigned> Info
938 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Chris Lattner29eb47b2011-02-19 22:55:41 +0000939 // If the typedef has an aligned attribute on it, it overrides any computed
940 // alignment we have. This violates the GCC documentation (which says that
941 // attribute(aligned) can only round up) but matches its implementation.
942 if (unsigned AttrAlign = Typedef->getMaxAlignment())
943 Align = AttrAlign;
944 else
945 Align = Info.second;
Douglas Gregorf5bae222010-08-27 00:11:28 +0000946 Width = Info.first;
Douglas Gregordc572a32009-03-30 22:58:21 +0000947 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000948 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000949
950 case Type::TypeOfExpr:
951 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
952 .getTypePtr());
953
954 case Type::TypeOf:
955 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
956
Anders Carlsson81df7b82009-06-24 19:06:50 +0000957 case Type::Decltype:
958 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
959 .getTypePtr());
960
Abramo Bagnara6150c882010-05-11 21:36:43 +0000961 case Type::Elaborated:
962 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000963
John McCall81904512011-01-06 01:58:22 +0000964 case Type::Attributed:
965 return getTypeInfo(
966 cast<AttributedType>(T)->getEquivalentType().getTypePtr());
967
Douglas Gregoref462e62009-04-30 17:32:17 +0000968 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000969 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000970 "Cannot request the size of a dependent type");
971 // FIXME: this is likely to be wrong once we support template
972 // aliases, since a template alias could refer to a typedef that
973 // has an __aligned__ attribute on it.
974 return getTypeInfo(getCanonicalType(T));
975 }
Mike Stump11289f42009-09-09 15:08:12 +0000976
Chris Lattner53cfe802007-07-18 17:52:12 +0000977 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000978 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000979}
980
Ken Dyckcc56c542011-01-15 18:38:59 +0000981/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
982CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
983 return CharUnits::fromQuantity(BitSize / getCharWidth());
984}
985
Ken Dyckb0fcc592011-02-11 01:54:29 +0000986/// toBits - Convert a size in characters to a size in characters.
987int64_t ASTContext::toBits(CharUnits CharSize) const {
988 return CharSize.getQuantity() * getCharWidth();
989}
990
Ken Dyck8c89d592009-12-22 14:23:30 +0000991/// getTypeSizeInChars - Return the size of the specified type, in characters.
992/// This method does not work on incomplete types.
Jay Foad39c79802011-01-12 09:06:06 +0000993CharUnits ASTContext::getTypeSizeInChars(QualType T) const {
Ken Dyckcc56c542011-01-15 18:38:59 +0000994 return toCharUnitsFromBits(getTypeSize(T));
Ken Dyck8c89d592009-12-22 14:23:30 +0000995}
Jay Foad39c79802011-01-12 09:06:06 +0000996CharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
Ken Dyckcc56c542011-01-15 18:38:59 +0000997 return toCharUnitsFromBits(getTypeSize(T));
Ken Dyck8c89d592009-12-22 14:23:30 +0000998}
999
Ken Dycka6046ab2010-01-26 17:25:18 +00001000/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +00001001/// characters. This method does not work on incomplete types.
Jay Foad39c79802011-01-12 09:06:06 +00001002CharUnits ASTContext::getTypeAlignInChars(QualType T) const {
Ken Dyckcc56c542011-01-15 18:38:59 +00001003 return toCharUnitsFromBits(getTypeAlign(T));
Ken Dyck24d28d62010-01-26 17:22:55 +00001004}
Jay Foad39c79802011-01-12 09:06:06 +00001005CharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
Ken Dyckcc56c542011-01-15 18:38:59 +00001006 return toCharUnitsFromBits(getTypeAlign(T));
Ken Dyck24d28d62010-01-26 17:22:55 +00001007}
1008
Chris Lattnera3402cd2009-01-27 18:08:34 +00001009/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
1010/// type for the current target in bits. This can be different than the ABI
1011/// alignment in cases where it is beneficial for performance to overalign
1012/// a data type.
Jay Foad39c79802011-01-12 09:06:06 +00001013unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
Chris Lattnera3402cd2009-01-27 18:08:34 +00001014 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +00001015
1016 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +00001017 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +00001018 T = CT->getElementType().getTypePtr();
1019 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
1020 T->isSpecificBuiltinType(BuiltinType::LongLong))
1021 return std::max(ABIAlign, (unsigned)getTypeSize(T));
1022
Chris Lattnera3402cd2009-01-27 18:08:34 +00001023 return ABIAlign;
1024}
1025
Fariborz Jahanian7c809592009-06-04 01:19:09 +00001026/// ShallowCollectObjCIvars -
1027/// Collect all ivars, including those synthesized, in the current class.
1028///
1029void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Jay Foad39c79802011-01-12 09:06:06 +00001030 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001031 // FIXME. This need be removed but there are two many places which
1032 // assume const-ness of ObjCInterfaceDecl
1033 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1034 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1035 Iv= Iv->getNextIvar())
1036 Ivars.push_back(Iv);
Fariborz Jahanian7c809592009-06-04 01:19:09 +00001037}
1038
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001039/// DeepCollectObjCIvars -
1040/// This routine first collects all declared, but not synthesized, ivars in
1041/// super class and then collects all ivars, including those synthesized for
1042/// current class. This routine is used for implementation of current class
1043/// when all ivars, declared and synthesized are known.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00001044///
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001045void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
1046 bool leafClass,
Jay Foad39c79802011-01-12 09:06:06 +00001047 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001048 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
1049 DeepCollectObjCIvars(SuperClass, false, Ivars);
1050 if (!leafClass) {
1051 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1052 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanianaef66222010-02-19 00:31:17 +00001053 Ivars.push_back(*I);
1054 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00001055 else
1056 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00001057}
1058
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001059/// CollectInheritedProtocols - Collect all protocols in current class and
1060/// those inherited by it.
1061void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00001062 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001063 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001064 // We can use protocol_iterator here instead of
1065 // all_referenced_protocol_iterator since we are walking all categories.
1066 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
1067 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001068 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00001069 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001070 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +00001071 PE = Proto->protocol_end(); P != PE; ++P) {
1072 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001073 CollectInheritedProtocols(*P, Protocols);
1074 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +00001075 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001076
1077 // Categories of this Interface.
1078 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1079 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1080 CollectInheritedProtocols(CDeclChain, Protocols);
1081 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1082 while (SD) {
1083 CollectInheritedProtocols(SD, Protocols);
1084 SD = SD->getSuperClass();
1085 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +00001086 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001087 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001088 PE = OC->protocol_end(); P != PE; ++P) {
1089 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00001090 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001091 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1092 PE = Proto->protocol_end(); P != PE; ++P)
1093 CollectInheritedProtocols(*P, Protocols);
1094 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +00001095 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001096 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1097 PE = OP->protocol_end(); P != PE; ++P) {
1098 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00001099 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001100 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1101 PE = Proto->protocol_end(); P != PE; ++P)
1102 CollectInheritedProtocols(*P, Protocols);
1103 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00001104 }
1105}
1106
Jay Foad39c79802011-01-12 09:06:06 +00001107unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +00001108 unsigned count = 0;
1109 // Count ivars declared in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00001110 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
1111 CDecl = CDecl->getNextClassExtension())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +00001112 count += CDecl->ivar_size();
1113
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +00001114 // Count ivar defined in this class's implementation. This
1115 // includes synthesized ivars.
1116 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +00001117 count += ImplDecl->ivar_size();
1118
Fariborz Jahanian7c809592009-06-04 01:19:09 +00001119 return count;
1120}
1121
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001122/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
1123ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
1124 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1125 I = ObjCImpls.find(D);
1126 if (I != ObjCImpls.end())
1127 return cast<ObjCImplementationDecl>(I->second);
1128 return 0;
1129}
1130/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1131ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
1132 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1133 I = ObjCImpls.find(D);
1134 if (I != ObjCImpls.end())
1135 return cast<ObjCCategoryImplDecl>(I->second);
1136 return 0;
1137}
1138
1139/// \brief Set the implementation of ObjCInterfaceDecl.
1140void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1141 ObjCImplementationDecl *ImplD) {
1142 assert(IFaceD && ImplD && "Passed null params");
1143 ObjCImpls[IFaceD] = ImplD;
1144}
1145/// \brief Set the implementation of ObjCCategoryDecl.
1146void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1147 ObjCCategoryImplDecl *ImplD) {
1148 assert(CatD && ImplD && "Passed null params");
1149 ObjCImpls[CatD] = ImplD;
1150}
1151
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001152/// \brief Get the copy initialization expression of VarDecl,or NULL if
1153/// none exists.
Fariborz Jahanian50198092010-12-02 17:02:11 +00001154Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001155 assert(VD && "Passed null params");
1156 assert(VD->hasAttr<BlocksAttr>() &&
1157 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian50198092010-12-02 17:02:11 +00001158 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001159 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001160 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1161}
1162
1163/// \brief Set the copy inialization expression of a block var decl.
1164void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1165 assert(VD && Init && "Passed null params");
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001166 assert(VD->hasAttr<BlocksAttr>() &&
1167 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001168 BlockVarCopyInits[VD] = Init;
1169}
1170
John McCallbcd03502009-12-07 02:54:59 +00001171/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001172///
John McCallbcd03502009-12-07 02:54:59 +00001173/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001174/// the TypeLoc wrappers.
1175///
1176/// \param T the type that will be the basis for type source info. This type
1177/// should refer to how the declarator was written in source code, not to
1178/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +00001179TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
Jay Foad39c79802011-01-12 09:06:06 +00001180 unsigned DataSize) const {
John McCall26fe7e02009-10-21 00:23:54 +00001181 if (!DataSize)
1182 DataSize = TypeLoc::getFullDataSizeForType(T);
1183 else
1184 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +00001185 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +00001186
John McCallbcd03502009-12-07 02:54:59 +00001187 TypeSourceInfo *TInfo =
1188 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1189 new (TInfo) TypeSourceInfo(T);
1190 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001191}
1192
John McCallbcd03502009-12-07 02:54:59 +00001193TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001194 SourceLocation L) const {
John McCallbcd03502009-12-07 02:54:59 +00001195 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
Douglas Gregor2d525f02011-01-25 19:13:18 +00001196 DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
John McCall3665e002009-10-23 21:14:09 +00001197 return DI;
1198}
1199
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001200const ASTRecordLayout &
Jay Foad39c79802011-01-12 09:06:06 +00001201ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001202 return getObjCLayout(D, 0);
1203}
1204
1205const ASTRecordLayout &
Jay Foad39c79802011-01-12 09:06:06 +00001206ASTContext::getASTObjCImplementationLayout(
1207 const ObjCImplementationDecl *D) const {
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001208 return getObjCLayout(D->getClassInterface(), D);
1209}
1210
Chris Lattner983a8bb2007-07-13 22:13:22 +00001211//===----------------------------------------------------------------------===//
1212// Type creation/memoization methods
1213//===----------------------------------------------------------------------===//
1214
Jay Foad39c79802011-01-12 09:06:06 +00001215QualType
John McCall33ddac02011-01-19 10:06:00 +00001216ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
1217 unsigned fastQuals = quals.getFastQualifiers();
1218 quals.removeFastQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00001219
1220 // Check if we've already instantiated this type.
1221 llvm::FoldingSetNodeID ID;
John McCall33ddac02011-01-19 10:06:00 +00001222 ExtQuals::Profile(ID, baseType, quals);
1223 void *insertPos = 0;
1224 if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
1225 assert(eq->getQualifiers() == quals);
1226 return QualType(eq, fastQuals);
John McCall8ccfcb52009-09-24 19:53:00 +00001227 }
1228
John McCall33ddac02011-01-19 10:06:00 +00001229 // If the base type is not canonical, make the appropriate canonical type.
1230 QualType canon;
1231 if (!baseType->isCanonicalUnqualified()) {
1232 SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
1233 canonSplit.second.addConsistentQualifiers(quals);
1234 canon = getExtQualType(canonSplit.first, canonSplit.second);
1235
1236 // Re-find the insert position.
1237 (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
1238 }
1239
1240 ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
1241 ExtQualNodes.InsertNode(eq, insertPos);
1242 return QualType(eq, fastQuals);
John McCall8ccfcb52009-09-24 19:53:00 +00001243}
1244
Jay Foad39c79802011-01-12 09:06:06 +00001245QualType
1246ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001247 QualType CanT = getCanonicalType(T);
1248 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001249 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001250
John McCall8ccfcb52009-09-24 19:53:00 +00001251 // If we are composing extended qualifiers together, merge together
1252 // into one ExtQuals node.
1253 QualifierCollector Quals;
1254 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001255
John McCall8ccfcb52009-09-24 19:53:00 +00001256 // If this type already has an address space specified, it cannot get
1257 // another one.
1258 assert(!Quals.hasAddressSpace() &&
1259 "Type cannot be in multiple addr spaces!");
1260 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001261
John McCall8ccfcb52009-09-24 19:53:00 +00001262 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001263}
1264
Chris Lattnerd60183d2009-02-18 22:53:11 +00001265QualType ASTContext::getObjCGCQualType(QualType T,
Jay Foad39c79802011-01-12 09:06:06 +00001266 Qualifiers::GC GCAttr) const {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001267 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001268 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001269 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001270
John McCall53fa7142010-12-24 02:08:15 +00001271 if (const PointerType *ptr = T->getAs<PointerType>()) {
1272 QualType Pointee = ptr->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001273 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001274 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1275 return getPointerType(ResultType);
1276 }
1277 }
Mike Stump11289f42009-09-09 15:08:12 +00001278
John McCall8ccfcb52009-09-24 19:53:00 +00001279 // If we are composing extended qualifiers together, merge together
1280 // into one ExtQuals node.
1281 QualifierCollector Quals;
1282 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001283
John McCall8ccfcb52009-09-24 19:53:00 +00001284 // If this type already has an ObjCGC specified, it cannot get
1285 // another one.
1286 assert(!Quals.hasObjCGCAttr() &&
1287 "Type cannot have multiple ObjCGCs!");
1288 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001289
John McCall8ccfcb52009-09-24 19:53:00 +00001290 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001291}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001292
John McCall4f5019e2010-12-19 02:44:49 +00001293const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1294 FunctionType::ExtInfo Info) {
1295 if (T->getExtInfo() == Info)
1296 return T;
1297
1298 QualType Result;
1299 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1300 Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1301 } else {
1302 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1303 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1304 EPI.ExtInfo = Info;
1305 Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1306 FPT->getNumArgs(), EPI);
1307 }
1308
1309 return cast<FunctionType>(Result.getTypePtr());
1310}
1311
Chris Lattnerc6395932007-06-22 20:56:16 +00001312/// getComplexType - Return the uniqued reference to the type for a complex
1313/// number with the specified element type.
Jay Foad39c79802011-01-12 09:06:06 +00001314QualType ASTContext::getComplexType(QualType T) const {
Chris Lattnerc6395932007-06-22 20:56:16 +00001315 // Unique pointers, to guarantee there is only one pointer of a particular
1316 // structure.
1317 llvm::FoldingSetNodeID ID;
1318 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001319
Chris Lattnerc6395932007-06-22 20:56:16 +00001320 void *InsertPos = 0;
1321 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1322 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattnerc6395932007-06-22 20:56:16 +00001324 // If the pointee type isn't canonical, this won't be a canonical type either,
1325 // so fill in the canonical type field.
1326 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001327 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001328 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001329
Chris Lattnerc6395932007-06-22 20:56:16 +00001330 // Get the new insert position for the node we care about.
1331 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001332 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001333 }
John McCall90d1c2d2009-09-24 23:30:46 +00001334 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001335 Types.push_back(New);
1336 ComplexTypes.InsertNode(New, InsertPos);
1337 return QualType(New, 0);
1338}
1339
Chris Lattner970e54e2006-11-12 00:37:36 +00001340/// getPointerType - Return the uniqued reference to the type for a pointer to
1341/// the specified type.
Jay Foad39c79802011-01-12 09:06:06 +00001342QualType ASTContext::getPointerType(QualType T) const {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001343 // Unique pointers, to guarantee there is only one pointer of a particular
1344 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001345 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001346 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001347
Chris Lattner67521df2007-01-27 01:29:36 +00001348 void *InsertPos = 0;
1349 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001350 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001351
Chris Lattner7ccecb92006-11-12 08:50:50 +00001352 // If the pointee type isn't canonical, this won't be a canonical type either,
1353 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001354 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001355 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001356 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001357
Chris Lattner67521df2007-01-27 01:29:36 +00001358 // Get the new insert position for the node we care about.
1359 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001360 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001361 }
John McCall90d1c2d2009-09-24 23:30:46 +00001362 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001363 Types.push_back(New);
1364 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001365 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001366}
1367
Mike Stump11289f42009-09-09 15:08:12 +00001368/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001369/// a pointer to the specified block.
Jay Foad39c79802011-01-12 09:06:06 +00001370QualType ASTContext::getBlockPointerType(QualType T) const {
Steve Naroff0ac012832008-08-28 19:20:44 +00001371 assert(T->isFunctionType() && "block of function types only");
1372 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001373 // structure.
1374 llvm::FoldingSetNodeID ID;
1375 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001376
Steve Naroffec33ed92008-08-27 16:04:49 +00001377 void *InsertPos = 0;
1378 if (BlockPointerType *PT =
1379 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1380 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001381
1382 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001383 // type either so fill in the canonical type field.
1384 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001385 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001386 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001387
Steve Naroffec33ed92008-08-27 16:04:49 +00001388 // Get the new insert position for the node we care about.
1389 BlockPointerType *NewIP =
1390 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001391 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001392 }
John McCall90d1c2d2009-09-24 23:30:46 +00001393 BlockPointerType *New
1394 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001395 Types.push_back(New);
1396 BlockPointerTypes.InsertNode(New, InsertPos);
1397 return QualType(New, 0);
1398}
1399
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001400/// getLValueReferenceType - Return the uniqued reference to the type for an
1401/// lvalue reference to the specified type.
Jay Foad39c79802011-01-12 09:06:06 +00001402QualType
1403ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
Bill Wendling3708c182007-05-27 10:15:43 +00001404 // Unique pointers, to guarantee there is only one pointer of a particular
1405 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001406 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001407 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001408
1409 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001410 if (LValueReferenceType *RT =
1411 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001412 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001413
John McCallfc93cf92009-10-22 22:37:11 +00001414 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1415
Bill Wendling3708c182007-05-27 10:15:43 +00001416 // If the referencee type isn't canonical, this won't be a canonical type
1417 // either, so fill in the canonical type field.
1418 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001419 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1420 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1421 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001422
Bill Wendling3708c182007-05-27 10:15:43 +00001423 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001424 LValueReferenceType *NewIP =
1425 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001426 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001427 }
1428
John McCall90d1c2d2009-09-24 23:30:46 +00001429 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001430 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1431 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001432 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001433 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001434
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001435 return QualType(New, 0);
1436}
1437
1438/// getRValueReferenceType - Return the uniqued reference to the type for an
1439/// rvalue reference to the specified type.
Jay Foad39c79802011-01-12 09:06:06 +00001440QualType ASTContext::getRValueReferenceType(QualType T) const {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001441 // Unique pointers, to guarantee there is only one pointer of a particular
1442 // structure.
1443 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001444 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001445
1446 void *InsertPos = 0;
1447 if (RValueReferenceType *RT =
1448 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1449 return QualType(RT, 0);
1450
John McCallfc93cf92009-10-22 22:37:11 +00001451 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1452
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001453 // If the referencee type isn't canonical, this won't be a canonical type
1454 // either, so fill in the canonical type field.
1455 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001456 if (InnerRef || !T.isCanonical()) {
1457 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1458 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001459
1460 // Get the new insert position for the node we care about.
1461 RValueReferenceType *NewIP =
1462 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001463 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001464 }
1465
John McCall90d1c2d2009-09-24 23:30:46 +00001466 RValueReferenceType *New
1467 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001468 Types.push_back(New);
1469 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001470 return QualType(New, 0);
1471}
1472
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001473/// getMemberPointerType - Return the uniqued reference to the type for a
1474/// member pointer to the specified type, in the specified class.
Jay Foad39c79802011-01-12 09:06:06 +00001475QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001476 // Unique pointers, to guarantee there is only one pointer of a particular
1477 // structure.
1478 llvm::FoldingSetNodeID ID;
1479 MemberPointerType::Profile(ID, T, Cls);
1480
1481 void *InsertPos = 0;
1482 if (MemberPointerType *PT =
1483 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1484 return QualType(PT, 0);
1485
1486 // If the pointee or class type isn't canonical, this won't be a canonical
1487 // type either, so fill in the canonical type field.
1488 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001489 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001490 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1491
1492 // Get the new insert position for the node we care about.
1493 MemberPointerType *NewIP =
1494 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001495 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001496 }
John McCall90d1c2d2009-09-24 23:30:46 +00001497 MemberPointerType *New
1498 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001499 Types.push_back(New);
1500 MemberPointerTypes.InsertNode(New, InsertPos);
1501 return QualType(New, 0);
1502}
1503
Mike Stump11289f42009-09-09 15:08:12 +00001504/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001505/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001506QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001507 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001508 ArrayType::ArraySizeModifier ASM,
Abramo Bagnara92141d22011-01-27 19:55:10 +00001509 unsigned IndexTypeQuals) const {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001510 assert((EltTy->isDependentType() ||
1511 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001512 "Constant array of VLAs is illegal!");
1513
Chris Lattnere2df3f92009-05-13 04:12:56 +00001514 // Convert the array size into a canonical width matching the pointer size for
1515 // the target.
1516 llvm::APInt ArySize(ArySizeIn);
Jay Foad6d4db0c2010-12-07 08:25:34 +00001517 ArySize =
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001518 ArySize.zextOrTrunc(Target.getPointerWidth(getTargetAddressSpace(EltTy)));
Mike Stump11289f42009-09-09 15:08:12 +00001519
Chris Lattner23b7eb62007-06-15 23:05:46 +00001520 llvm::FoldingSetNodeID ID;
Abramo Bagnara92141d22011-01-27 19:55:10 +00001521 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001522
Chris Lattner36f8e652007-01-27 08:31:04 +00001523 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001524 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001525 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001526 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001527
John McCall33ddac02011-01-19 10:06:00 +00001528 // If the element type isn't canonical or has qualifiers, this won't
1529 // be a canonical type either, so fill in the canonical type field.
1530 QualType Canon;
1531 if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
1532 SplitQualType canonSplit = getCanonicalType(EltTy).split();
1533 Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize,
Abramo Bagnara92141d22011-01-27 19:55:10 +00001534 ASM, IndexTypeQuals);
John McCall33ddac02011-01-19 10:06:00 +00001535 Canon = getQualifiedType(Canon, canonSplit.second);
1536
Chris Lattner36f8e652007-01-27 08:31:04 +00001537 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001538 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001539 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001540 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001541 }
Mike Stump11289f42009-09-09 15:08:12 +00001542
John McCall90d1c2d2009-09-24 23:30:46 +00001543 ConstantArrayType *New = new(*this,TypeAlignment)
Abramo Bagnara92141d22011-01-27 19:55:10 +00001544 ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001545 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001546 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001547 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001548}
1549
John McCall06549462011-01-18 08:40:38 +00001550/// getVariableArrayDecayedType - Turns the given type, which may be
1551/// variably-modified, into the corresponding type with all the known
1552/// sizes replaced with [*].
1553QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1554 // Vastly most common case.
1555 if (!type->isVariablyModifiedType()) return type;
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001556
John McCall06549462011-01-18 08:40:38 +00001557 QualType result;
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001558
John McCall06549462011-01-18 08:40:38 +00001559 SplitQualType split = type.getSplitDesugaredType();
1560 const Type *ty = split.first;
1561 switch (ty->getTypeClass()) {
1562#define TYPE(Class, Base)
1563#define ABSTRACT_TYPE(Class, Base)
1564#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1565#include "clang/AST/TypeNodes.def"
1566 llvm_unreachable("didn't desugar past all non-canonical types?");
1567
1568 // These types should never be variably-modified.
1569 case Type::Builtin:
1570 case Type::Complex:
1571 case Type::Vector:
1572 case Type::ExtVector:
1573 case Type::DependentSizedExtVector:
1574 case Type::ObjCObject:
1575 case Type::ObjCInterface:
1576 case Type::ObjCObjectPointer:
1577 case Type::Record:
1578 case Type::Enum:
1579 case Type::UnresolvedUsing:
1580 case Type::TypeOfExpr:
1581 case Type::TypeOf:
1582 case Type::Decltype:
1583 case Type::DependentName:
1584 case Type::InjectedClassName:
1585 case Type::TemplateSpecialization:
1586 case Type::DependentTemplateSpecialization:
1587 case Type::TemplateTypeParm:
1588 case Type::SubstTemplateTypeParmPack:
Richard Smith30482bc2011-02-20 03:19:35 +00001589 case Type::Auto:
John McCall06549462011-01-18 08:40:38 +00001590 case Type::PackExpansion:
1591 llvm_unreachable("type should never be variably-modified");
1592
1593 // These types can be variably-modified but should never need to
1594 // further decay.
1595 case Type::FunctionNoProto:
1596 case Type::FunctionProto:
1597 case Type::BlockPointer:
1598 case Type::MemberPointer:
1599 return type;
1600
1601 // These types can be variably-modified. All these modifications
1602 // preserve structure except as noted by comments.
1603 // TODO: if we ever care about optimizing VLAs, there are no-op
1604 // optimizations available here.
1605 case Type::Pointer:
1606 result = getPointerType(getVariableArrayDecayedType(
1607 cast<PointerType>(ty)->getPointeeType()));
1608 break;
1609
1610 case Type::LValueReference: {
1611 const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
1612 result = getLValueReferenceType(
1613 getVariableArrayDecayedType(lv->getPointeeType()),
1614 lv->isSpelledAsLValue());
1615 break;
1616 }
1617
1618 case Type::RValueReference: {
1619 const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
1620 result = getRValueReferenceType(
1621 getVariableArrayDecayedType(lv->getPointeeType()));
1622 break;
1623 }
1624
1625 case Type::ConstantArray: {
1626 const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
1627 result = getConstantArrayType(
1628 getVariableArrayDecayedType(cat->getElementType()),
1629 cat->getSize(),
1630 cat->getSizeModifier(),
1631 cat->getIndexTypeCVRQualifiers());
1632 break;
1633 }
1634
1635 case Type::DependentSizedArray: {
1636 const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
1637 result = getDependentSizedArrayType(
1638 getVariableArrayDecayedType(dat->getElementType()),
1639 dat->getSizeExpr(),
1640 dat->getSizeModifier(),
1641 dat->getIndexTypeCVRQualifiers(),
1642 dat->getBracketsRange());
1643 break;
1644 }
1645
1646 // Turn incomplete types into [*] types.
1647 case Type::IncompleteArray: {
1648 const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
1649 result = getVariableArrayType(
1650 getVariableArrayDecayedType(iat->getElementType()),
1651 /*size*/ 0,
1652 ArrayType::Normal,
1653 iat->getIndexTypeCVRQualifiers(),
1654 SourceRange());
1655 break;
1656 }
1657
1658 // Turn VLA types into [*] types.
1659 case Type::VariableArray: {
1660 const VariableArrayType *vat = cast<VariableArrayType>(ty);
1661 result = getVariableArrayType(
1662 getVariableArrayDecayedType(vat->getElementType()),
1663 /*size*/ 0,
1664 ArrayType::Star,
1665 vat->getIndexTypeCVRQualifiers(),
1666 vat->getBracketsRange());
1667 break;
1668 }
1669 }
1670
1671 // Apply the top-level qualifiers from the original.
1672 return getQualifiedType(result, split.second);
1673}
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001674
Steve Naroffcadebd02007-08-30 18:14:25 +00001675/// getVariableArrayType - Returns a non-unique reference to the type for a
1676/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001677QualType ASTContext::getVariableArrayType(QualType EltTy,
1678 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001679 ArrayType::ArraySizeModifier ASM,
Abramo Bagnara92141d22011-01-27 19:55:10 +00001680 unsigned IndexTypeQuals,
Jay Foad39c79802011-01-12 09:06:06 +00001681 SourceRange Brackets) const {
Eli Friedmanbd258282008-02-15 18:16:39 +00001682 // Since we don't unique expressions, it isn't possible to unique VLA's
1683 // that have an expression provided for their size.
John McCall33ddac02011-01-19 10:06:00 +00001684 QualType Canon;
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001685
John McCall33ddac02011-01-19 10:06:00 +00001686 // Be sure to pull qualifiers off the element type.
1687 if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
1688 SplitQualType canonSplit = getCanonicalType(EltTy).split();
1689 Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM,
Abramo Bagnara92141d22011-01-27 19:55:10 +00001690 IndexTypeQuals, Brackets);
John McCall33ddac02011-01-19 10:06:00 +00001691 Canon = getQualifiedType(Canon, canonSplit.second);
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001692 }
1693
John McCall90d1c2d2009-09-24 23:30:46 +00001694 VariableArrayType *New = new(*this, TypeAlignment)
Abramo Bagnara92141d22011-01-27 19:55:10 +00001695 VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001696
1697 VariableArrayTypes.push_back(New);
1698 Types.push_back(New);
1699 return QualType(New, 0);
1700}
1701
Douglas Gregor4619e432008-12-05 23:32:09 +00001702/// getDependentSizedArrayType - Returns a non-unique reference to
1703/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001704/// type.
John McCall33ddac02011-01-19 10:06:00 +00001705QualType ASTContext::getDependentSizedArrayType(QualType elementType,
1706 Expr *numElements,
Douglas Gregor4619e432008-12-05 23:32:09 +00001707 ArrayType::ArraySizeModifier ASM,
John McCall33ddac02011-01-19 10:06:00 +00001708 unsigned elementTypeQuals,
1709 SourceRange brackets) const {
1710 assert((!numElements || numElements->isTypeDependent() ||
1711 numElements->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001712 "Size must be type- or value-dependent!");
1713
John McCall33ddac02011-01-19 10:06:00 +00001714 // Dependently-sized array types that do not have a specified number
1715 // of elements will have their sizes deduced from a dependent
1716 // initializer. We do no canonicalization here at all, which is okay
1717 // because they can't be used in most locations.
1718 if (!numElements) {
1719 DependentSizedArrayType *newType
1720 = new (*this, TypeAlignment)
1721 DependentSizedArrayType(*this, elementType, QualType(),
1722 numElements, ASM, elementTypeQuals,
1723 brackets);
1724 Types.push_back(newType);
1725 return QualType(newType, 0);
1726 }
1727
1728 // Otherwise, we actually build a new type every time, but we
1729 // also build a canonical type.
1730
1731 SplitQualType canonElementType = getCanonicalType(elementType).split();
1732
1733 void *insertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001734 llvm::FoldingSetNodeID ID;
John McCall33ddac02011-01-19 10:06:00 +00001735 DependentSizedArrayType::Profile(ID, *this,
1736 QualType(canonElementType.first, 0),
1737 ASM, elementTypeQuals, numElements);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001738
John McCall33ddac02011-01-19 10:06:00 +00001739 // Look for an existing type with these properties.
1740 DependentSizedArrayType *canonTy =
1741 DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001742
John McCall33ddac02011-01-19 10:06:00 +00001743 // If we don't have one, build one.
1744 if (!canonTy) {
1745 canonTy = new (*this, TypeAlignment)
1746 DependentSizedArrayType(*this, QualType(canonElementType.first, 0),
1747 QualType(), numElements, ASM, elementTypeQuals,
1748 brackets);
1749 DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
1750 Types.push_back(canonTy);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001751 }
1752
John McCall33ddac02011-01-19 10:06:00 +00001753 // Apply qualifiers from the element type to the array.
1754 QualType canon = getQualifiedType(QualType(canonTy,0),
1755 canonElementType.second);
Mike Stump11289f42009-09-09 15:08:12 +00001756
John McCall33ddac02011-01-19 10:06:00 +00001757 // If we didn't need extra canonicalization for the element type,
1758 // then just use that as our result.
1759 if (QualType(canonElementType.first, 0) == elementType)
1760 return canon;
1761
1762 // Otherwise, we need to build a type which follows the spelling
1763 // of the element type.
1764 DependentSizedArrayType *sugaredType
1765 = new (*this, TypeAlignment)
1766 DependentSizedArrayType(*this, elementType, canon, numElements,
1767 ASM, elementTypeQuals, brackets);
1768 Types.push_back(sugaredType);
1769 return QualType(sugaredType, 0);
Douglas Gregor4619e432008-12-05 23:32:09 +00001770}
1771
John McCall33ddac02011-01-19 10:06:00 +00001772QualType ASTContext::getIncompleteArrayType(QualType elementType,
Eli Friedmanbd258282008-02-15 18:16:39 +00001773 ArrayType::ArraySizeModifier ASM,
John McCall33ddac02011-01-19 10:06:00 +00001774 unsigned elementTypeQuals) const {
Eli Friedmanbd258282008-02-15 18:16:39 +00001775 llvm::FoldingSetNodeID ID;
John McCall33ddac02011-01-19 10:06:00 +00001776 IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001777
John McCall33ddac02011-01-19 10:06:00 +00001778 void *insertPos = 0;
1779 if (IncompleteArrayType *iat =
1780 IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
1781 return QualType(iat, 0);
Eli Friedmanbd258282008-02-15 18:16:39 +00001782
1783 // If the element type isn't canonical, this won't be a canonical type
John McCall33ddac02011-01-19 10:06:00 +00001784 // either, so fill in the canonical type field. We also have to pull
1785 // qualifiers off the element type.
1786 QualType canon;
Eli Friedmanbd258282008-02-15 18:16:39 +00001787
John McCall33ddac02011-01-19 10:06:00 +00001788 if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
1789 SplitQualType canonSplit = getCanonicalType(elementType).split();
1790 canon = getIncompleteArrayType(QualType(canonSplit.first, 0),
1791 ASM, elementTypeQuals);
1792 canon = getQualifiedType(canon, canonSplit.second);
Eli Friedmanbd258282008-02-15 18:16:39 +00001793
1794 // Get the new insert position for the node we care about.
John McCall33ddac02011-01-19 10:06:00 +00001795 IncompleteArrayType *existing =
1796 IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
1797 assert(!existing && "Shouldn't be in the map!"); (void) existing;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001798 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001799
John McCall33ddac02011-01-19 10:06:00 +00001800 IncompleteArrayType *newType = new (*this, TypeAlignment)
1801 IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001802
John McCall33ddac02011-01-19 10:06:00 +00001803 IncompleteArrayTypes.InsertNode(newType, insertPos);
1804 Types.push_back(newType);
1805 return QualType(newType, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001806}
1807
Steve Naroff91fcddb2007-07-18 18:00:27 +00001808/// getVectorType - Return the unique reference to a vector type of
1809/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001810QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Jay Foad39c79802011-01-12 09:06:06 +00001811 VectorType::VectorKind VecKind) const {
John McCall33ddac02011-01-19 10:06:00 +00001812 assert(vecType->isBuiltinType());
Mike Stump11289f42009-09-09 15:08:12 +00001813
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001814 // Check if we've already instantiated a vector of this type.
1815 llvm::FoldingSetNodeID ID;
Bob Wilsonaeb56442010-11-10 21:56:12 +00001816 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner37141f42010-06-23 06:00:24 +00001817
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001818 void *InsertPos = 0;
1819 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1820 return QualType(VTP, 0);
1821
1822 // If the element type isn't canonical, this won't be a canonical type either,
1823 // so fill in the canonical type field.
1824 QualType Canonical;
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00001825 if (!vecType.isCanonical()) {
Bob Wilson77954802010-11-16 00:32:20 +00001826 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump11289f42009-09-09 15:08:12 +00001827
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001828 // Get the new insert position for the node we care about.
1829 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001830 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001831 }
John McCall90d1c2d2009-09-24 23:30:46 +00001832 VectorType *New = new (*this, TypeAlignment)
Bob Wilsonaeb56442010-11-10 21:56:12 +00001833 VectorType(vecType, NumElts, Canonical, VecKind);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001834 VectorTypes.InsertNode(New, InsertPos);
1835 Types.push_back(New);
1836 return QualType(New, 0);
1837}
1838
Nate Begemance4d7fc2008-04-18 23:10:10 +00001839/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001840/// the specified element type and size. VectorType must be a built-in type.
Jay Foad39c79802011-01-12 09:06:06 +00001841QualType
1842ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
John McCall33ddac02011-01-19 10:06:00 +00001843 assert(vecType->isBuiltinType());
Mike Stump11289f42009-09-09 15:08:12 +00001844
Steve Naroff91fcddb2007-07-18 18:00:27 +00001845 // Check if we've already instantiated a vector of this type.
1846 llvm::FoldingSetNodeID ID;
Chris Lattner37141f42010-06-23 06:00:24 +00001847 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001848 VectorType::GenericVector);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001849 void *InsertPos = 0;
1850 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1851 return QualType(VTP, 0);
1852
1853 // If the element type isn't canonical, this won't be a canonical type either,
1854 // so fill in the canonical type field.
1855 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001856 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001857 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001858
Steve Naroff91fcddb2007-07-18 18:00:27 +00001859 // Get the new insert position for the node we care about.
1860 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001861 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001862 }
John McCall90d1c2d2009-09-24 23:30:46 +00001863 ExtVectorType *New = new (*this, TypeAlignment)
1864 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001865 VectorTypes.InsertNode(New, InsertPos);
1866 Types.push_back(New);
1867 return QualType(New, 0);
1868}
1869
Jay Foad39c79802011-01-12 09:06:06 +00001870QualType
1871ASTContext::getDependentSizedExtVectorType(QualType vecType,
1872 Expr *SizeExpr,
1873 SourceLocation AttrLoc) const {
Douglas Gregor352169a2009-07-31 03:54:25 +00001874 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001875 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001876 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001877
Douglas Gregor352169a2009-07-31 03:54:25 +00001878 void *InsertPos = 0;
1879 DependentSizedExtVectorType *Canon
1880 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1881 DependentSizedExtVectorType *New;
1882 if (Canon) {
1883 // We already have a canonical version of this array type; use it as
1884 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001885 New = new (*this, TypeAlignment)
1886 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1887 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001888 } else {
1889 QualType CanonVecTy = getCanonicalType(vecType);
1890 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001891 New = new (*this, TypeAlignment)
1892 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1893 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001894
1895 DependentSizedExtVectorType *CanonCheck
1896 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1897 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1898 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001899 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1900 } else {
1901 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1902 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001903 New = new (*this, TypeAlignment)
1904 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001905 }
1906 }
Mike Stump11289f42009-09-09 15:08:12 +00001907
Douglas Gregor758a8692009-06-17 21:51:59 +00001908 Types.push_back(New);
1909 return QualType(New, 0);
1910}
1911
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001912/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001913///
Jay Foad39c79802011-01-12 09:06:06 +00001914QualType
1915ASTContext::getFunctionNoProtoType(QualType ResultTy,
1916 const FunctionType::ExtInfo &Info) const {
Roman Divacky65b88cd2011-03-01 17:40:53 +00001917 const CallingConv DefaultCC = Info.getCC();
1918 const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
1919 CC_X86StdCall : DefaultCC;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001920 // Unique functions, to guarantee there is only one function of a particular
1921 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001922 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001923 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001924
Chris Lattner47955de2007-01-27 08:37:20 +00001925 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001926 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001927 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001928 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001929
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001930 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001931 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001932 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001933 Canonical =
1934 getFunctionNoProtoType(getCanonicalType(ResultTy),
1935 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001936
Chris Lattner47955de2007-01-27 08:37:20 +00001937 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001938 FunctionNoProtoType *NewIP =
1939 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001940 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001941 }
Mike Stump11289f42009-09-09 15:08:12 +00001942
Roman Divacky65b88cd2011-03-01 17:40:53 +00001943 FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
John McCall90d1c2d2009-09-24 23:30:46 +00001944 FunctionNoProtoType *New = new (*this, TypeAlignment)
Roman Divacky65b88cd2011-03-01 17:40:53 +00001945 FunctionNoProtoType(ResultTy, Canonical, newInfo);
Chris Lattner47955de2007-01-27 08:37:20 +00001946 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001947 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001948 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001949}
1950
1951/// getFunctionType - Return a normal function type with a typed argument
1952/// list. isVariadic indicates whether the argument list includes '...'.
Jay Foad39c79802011-01-12 09:06:06 +00001953QualType
1954ASTContext::getFunctionType(QualType ResultTy,
1955 const QualType *ArgArray, unsigned NumArgs,
1956 const FunctionProtoType::ExtProtoInfo &EPI) const {
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001957 // Unique functions, to guarantee there is only one function of a particular
1958 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001959 llvm::FoldingSetNodeID ID;
Sebastian Redl31ad7542011-03-13 17:09:40 +00001960 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001961
1962 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001963 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001964 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001965 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001966
1967 // Determine whether the type being created is already canonical or not.
Sebastian Redl7c6c9e92011-03-06 10:52:04 +00001968 bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001969 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001970 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001971 isCanonical = false;
1972
Roman Divacky65b88cd2011-03-01 17:40:53 +00001973 const CallingConv DefaultCC = EPI.ExtInfo.getCC();
1974 const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
1975 CC_X86StdCall : DefaultCC;
John McCalldb40c7f2010-12-14 08:05:40 +00001976
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001977 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001978 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001979 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001980 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001981 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001982 CanonicalArgs.reserve(NumArgs);
1983 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001984 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001985
John McCalldb40c7f2010-12-14 08:05:40 +00001986 FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
Sebastian Redl7c6c9e92011-03-06 10:52:04 +00001987 CanonicalEPI.ExceptionSpecType = EST_None;
1988 CanonicalEPI.NumExceptions = 0;
John McCalldb40c7f2010-12-14 08:05:40 +00001989 CanonicalEPI.ExtInfo
1990 = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
1991
Chris Lattner76a00cf2008-04-06 22:59:24 +00001992 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001993 CanonicalArgs.data(), NumArgs,
John McCalldb40c7f2010-12-14 08:05:40 +00001994 CanonicalEPI);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001995
Chris Lattnerfd4de792007-01-27 01:15:32 +00001996 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001997 FunctionProtoType *NewIP =
1998 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001999 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00002000 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002001
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002002 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00002003 // for two variable size arrays (for parameter and exception types) at the
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002004 // end of them. Instead of the exception types, there could be a noexcept
2005 // expression and a context pointer.
John McCalldb40c7f2010-12-14 08:05:40 +00002006 size_t Size = sizeof(FunctionProtoType) +
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002007 NumArgs * sizeof(QualType);
2008 if (EPI.ExceptionSpecType == EST_Dynamic)
2009 Size += EPI.NumExceptions * sizeof(QualType);
2010 else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
Sebastian Redl31ad7542011-03-13 17:09:40 +00002011 Size += sizeof(Expr*);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002012 }
John McCalldb40c7f2010-12-14 08:05:40 +00002013 FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
Roman Divacky65b88cd2011-03-01 17:40:53 +00002014 FunctionProtoType::ExtProtoInfo newEPI = EPI;
2015 newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
Sebastian Redl31ad7542011-03-13 17:09:40 +00002016 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00002017 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002018 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00002019 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00002020}
Chris Lattneref51c202006-11-10 07:17:23 +00002021
John McCalle78aac42010-03-10 03:28:59 +00002022#ifndef NDEBUG
2023static bool NeedsInjectedClassNameType(const RecordDecl *D) {
2024 if (!isa<CXXRecordDecl>(D)) return false;
2025 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
2026 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
2027 return true;
2028 if (RD->getDescribedClassTemplate() &&
2029 !isa<ClassTemplateSpecializationDecl>(RD))
2030 return true;
2031 return false;
2032}
2033#endif
2034
2035/// getInjectedClassNameType - Return the unique reference to the
2036/// injected class name type for the specified templated declaration.
2037QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
Jay Foad39c79802011-01-12 09:06:06 +00002038 QualType TST) const {
John McCalle78aac42010-03-10 03:28:59 +00002039 assert(NeedsInjectedClassNameType(Decl));
2040 if (Decl->TypeForDecl) {
2041 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00002042 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCalle78aac42010-03-10 03:28:59 +00002043 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
2044 Decl->TypeForDecl = PrevDecl->TypeForDecl;
2045 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2046 } else {
John McCall424cec92011-01-19 06:33:43 +00002047 Type *newType =
John McCall2408e322010-04-27 00:57:59 +00002048 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCall424cec92011-01-19 06:33:43 +00002049 Decl->TypeForDecl = newType;
2050 Types.push_back(newType);
John McCalle78aac42010-03-10 03:28:59 +00002051 }
2052 return QualType(Decl->TypeForDecl, 0);
2053}
2054
Douglas Gregor83a586e2008-04-13 21:07:44 +00002055/// getTypeDeclType - Return the unique reference to the type for the
2056/// specified type declaration.
Jay Foad39c79802011-01-12 09:06:06 +00002057QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00002058 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00002059 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00002060
Richard Smithdda56e42011-04-15 14:24:37 +00002061 if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00002062 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00002063
John McCall96f0b5f2010-03-10 06:48:02 +00002064 assert(!isa<TemplateTypeParmDecl>(Decl) &&
2065 "Template type parameter types are always available.");
2066
John McCall81e38502010-02-16 03:57:14 +00002067 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00002068 assert(!Record->getPreviousDeclaration() &&
2069 "struct/union has previous declaration");
2070 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002071 return getRecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00002072 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00002073 assert(!Enum->getPreviousDeclaration() &&
2074 "enum has previous declaration");
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002075 return getEnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00002076 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00002077 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
John McCall424cec92011-01-19 06:33:43 +00002078 Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2079 Decl->TypeForDecl = newType;
2080 Types.push_back(newType);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00002081 } else
John McCall96f0b5f2010-03-10 06:48:02 +00002082 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00002083
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00002084 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00002085}
2086
Chris Lattner32d920b2007-01-26 02:01:53 +00002087/// getTypedefType - Return the unique reference to the type for the
Richard Smithdda56e42011-04-15 14:24:37 +00002088/// specified typedef name decl.
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002089QualType
Richard Smithdda56e42011-04-15 14:24:37 +00002090ASTContext::getTypedefType(const TypedefNameDecl *Decl,
2091 QualType Canonical) const {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00002092 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002093
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002094 if (Canonical.isNull())
2095 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall424cec92011-01-19 06:33:43 +00002096 TypedefType *newType = new(*this, TypeAlignment)
John McCall90d1c2d2009-09-24 23:30:46 +00002097 TypedefType(Type::Typedef, Decl, Canonical);
John McCall424cec92011-01-19 06:33:43 +00002098 Decl->TypeForDecl = newType;
2099 Types.push_back(newType);
2100 return QualType(newType, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00002101}
2102
Jay Foad39c79802011-01-12 09:06:06 +00002103QualType ASTContext::getRecordType(const RecordDecl *Decl) const {
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002104 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2105
2106 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
2107 if (PrevDecl->TypeForDecl)
2108 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2109
John McCall424cec92011-01-19 06:33:43 +00002110 RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2111 Decl->TypeForDecl = newType;
2112 Types.push_back(newType);
2113 return QualType(newType, 0);
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002114}
2115
Jay Foad39c79802011-01-12 09:06:06 +00002116QualType ASTContext::getEnumType(const EnumDecl *Decl) const {
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002117 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2118
2119 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
2120 if (PrevDecl->TypeForDecl)
2121 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2122
John McCall424cec92011-01-19 06:33:43 +00002123 EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2124 Decl->TypeForDecl = newType;
2125 Types.push_back(newType);
2126 return QualType(newType, 0);
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00002127}
2128
John McCall81904512011-01-06 01:58:22 +00002129QualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
2130 QualType modifiedType,
2131 QualType equivalentType) {
2132 llvm::FoldingSetNodeID id;
2133 AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
2134
2135 void *insertPos = 0;
2136 AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
2137 if (type) return QualType(type, 0);
2138
2139 QualType canon = getCanonicalType(equivalentType);
2140 type = new (*this, TypeAlignment)
2141 AttributedType(canon, attrKind, modifiedType, equivalentType);
2142
2143 Types.push_back(type);
2144 AttributedTypes.InsertNode(type, insertPos);
2145
2146 return QualType(type, 0);
2147}
2148
2149
John McCallcebee162009-10-18 09:09:24 +00002150/// \brief Retrieve a substitution-result type.
2151QualType
2152ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
Jay Foad39c79802011-01-12 09:06:06 +00002153 QualType Replacement) const {
John McCallb692a092009-10-22 20:10:53 +00002154 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00002155 && "replacement types must always be canonical");
2156
2157 llvm::FoldingSetNodeID ID;
2158 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
2159 void *InsertPos = 0;
2160 SubstTemplateTypeParmType *SubstParm
2161 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2162
2163 if (!SubstParm) {
2164 SubstParm = new (*this, TypeAlignment)
2165 SubstTemplateTypeParmType(Parm, Replacement);
2166 Types.push_back(SubstParm);
2167 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2168 }
2169
2170 return QualType(SubstParm, 0);
2171}
2172
Douglas Gregorada4b792011-01-14 02:55:32 +00002173/// \brief Retrieve a
2174QualType ASTContext::getSubstTemplateTypeParmPackType(
2175 const TemplateTypeParmType *Parm,
2176 const TemplateArgument &ArgPack) {
2177#ifndef NDEBUG
2178 for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2179 PEnd = ArgPack.pack_end();
2180 P != PEnd; ++P) {
2181 assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2182 assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2183 }
2184#endif
2185
2186 llvm::FoldingSetNodeID ID;
2187 SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2188 void *InsertPos = 0;
2189 if (SubstTemplateTypeParmPackType *SubstParm
2190 = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2191 return QualType(SubstParm, 0);
2192
2193 QualType Canon;
2194 if (!Parm->isCanonicalUnqualified()) {
2195 Canon = getCanonicalType(QualType(Parm, 0));
2196 Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2197 ArgPack);
2198 SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2199 }
2200
2201 SubstTemplateTypeParmPackType *SubstParm
2202 = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2203 ArgPack);
2204 Types.push_back(SubstParm);
2205 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2206 return QualType(SubstParm, 0);
2207}
2208
Douglas Gregoreff93e02009-02-05 23:33:38 +00002209/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00002210/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00002211/// name.
Mike Stump11289f42009-09-09 15:08:12 +00002212QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00002213 bool ParameterPack,
Chandler Carruth08836322011-05-01 00:51:33 +00002214 TemplateTypeParmDecl *TTPDecl) const {
Douglas Gregoreff93e02009-02-05 23:33:38 +00002215 llvm::FoldingSetNodeID ID;
Chandler Carruth08836322011-05-01 00:51:33 +00002216 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
Douglas Gregoreff93e02009-02-05 23:33:38 +00002217 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002218 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00002219 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2220
2221 if (TypeParm)
2222 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002223
Chandler Carruth08836322011-05-01 00:51:33 +00002224 if (TTPDecl) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00002225 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Chandler Carruth08836322011-05-01 00:51:33 +00002226 TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002227
2228 TemplateTypeParmType *TypeCheck
2229 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2230 assert(!TypeCheck && "Template type parameter canonical type broken");
2231 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00002232 } else
John McCall90d1c2d2009-09-24 23:30:46 +00002233 TypeParm = new (*this, TypeAlignment)
2234 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00002235
2236 Types.push_back(TypeParm);
2237 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2238
2239 return QualType(TypeParm, 0);
2240}
2241
John McCalle78aac42010-03-10 03:28:59 +00002242TypeSourceInfo *
2243ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
2244 SourceLocation NameLoc,
2245 const TemplateArgumentListInfo &Args,
Jay Foad39c79802011-01-12 09:06:06 +00002246 QualType CanonType) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002247 assert(!Name.getAsDependentTemplateName() &&
2248 "No dependent template names here!");
John McCalle78aac42010-03-10 03:28:59 +00002249 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
2250
2251 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
2252 TemplateSpecializationTypeLoc TL
2253 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
2254 TL.setTemplateNameLoc(NameLoc);
2255 TL.setLAngleLoc(Args.getLAngleLoc());
2256 TL.setRAngleLoc(Args.getRAngleLoc());
2257 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2258 TL.setArgLocInfo(i, Args[i].getLocInfo());
2259 return DI;
2260}
2261
Mike Stump11289f42009-09-09 15:08:12 +00002262QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00002263ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00002264 const TemplateArgumentListInfo &Args,
Jay Foad39c79802011-01-12 09:06:06 +00002265 QualType Canon) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002266 assert(!Template.getAsDependentTemplateName() &&
2267 "No dependent template names here!");
2268
John McCall6b51f282009-11-23 01:53:49 +00002269 unsigned NumArgs = Args.size();
2270
John McCall0ad16662009-10-29 08:12:44 +00002271 llvm::SmallVector<TemplateArgument, 4> ArgVec;
2272 ArgVec.reserve(NumArgs);
2273 for (unsigned i = 0; i != NumArgs; ++i)
2274 ArgVec.push_back(Args[i].getArgument());
2275
John McCall2408e322010-04-27 00:57:59 +00002276 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00002277 Canon);
John McCall0ad16662009-10-29 08:12:44 +00002278}
2279
2280QualType
2281ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00002282 const TemplateArgument *Args,
2283 unsigned NumArgs,
Jay Foad39c79802011-01-12 09:06:06 +00002284 QualType Canon) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002285 assert(!Template.getAsDependentTemplateName() &&
2286 "No dependent template names here!");
Douglas Gregore29139c2011-03-03 17:04:51 +00002287 // Look through qualified template names.
2288 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2289 Template = TemplateName(QTN->getTemplateDecl());
Douglas Gregore16af532011-02-28 18:50:33 +00002290
Douglas Gregor15301382009-07-30 17:40:51 +00002291 if (!Canon.isNull())
2292 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002293 else
2294 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregord56a91e2009-02-26 22:19:44 +00002295
Douglas Gregora8e02e72009-07-28 23:00:59 +00002296 // Allocate the (non-canonical) template specialization type, but don't
2297 // try to unique it: these types typically have location information that
2298 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00002299 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00002300 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00002301 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00002302 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00002303 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00002304 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00002305 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00002306
Douglas Gregor8bf42052009-02-09 18:46:07 +00002307 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00002308 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002309}
2310
Mike Stump11289f42009-09-09 15:08:12 +00002311QualType
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002312ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2313 const TemplateArgument *Args,
Jay Foad39c79802011-01-12 09:06:06 +00002314 unsigned NumArgs) const {
Douglas Gregore16af532011-02-28 18:50:33 +00002315 assert(!Template.getAsDependentTemplateName() &&
2316 "No dependent template names here!");
Douglas Gregore29139c2011-03-03 17:04:51 +00002317 // Look through qualified template names.
2318 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2319 Template = TemplateName(QTN->getTemplateDecl());
Douglas Gregore16af532011-02-28 18:50:33 +00002320
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002321 // Build the canonical template specialization type.
2322 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2323 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2324 CanonArgs.reserve(NumArgs);
2325 for (unsigned I = 0; I != NumArgs; ++I)
2326 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2327
2328 // Determine whether this canonical template specialization type already
2329 // exists.
2330 llvm::FoldingSetNodeID ID;
2331 TemplateSpecializationType::Profile(ID, CanonTemplate,
2332 CanonArgs.data(), NumArgs, *this);
2333
2334 void *InsertPos = 0;
2335 TemplateSpecializationType *Spec
2336 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2337
2338 if (!Spec) {
2339 // Allocate a new canonical template specialization type.
2340 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2341 sizeof(TemplateArgument) * NumArgs),
2342 TypeAlignment);
2343 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2344 CanonArgs.data(), NumArgs,
2345 QualType());
2346 Types.push_back(Spec);
2347 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2348 }
2349
2350 assert(Spec->isDependentType() &&
2351 "Non-dependent template-id type must have a canonical type");
2352 return QualType(Spec, 0);
2353}
2354
2355QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00002356ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2357 NestedNameSpecifier *NNS,
Jay Foad39c79802011-01-12 09:06:06 +00002358 QualType NamedType) const {
Douglas Gregor52537682009-03-19 00:18:19 +00002359 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002360 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00002361
2362 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002363 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002364 if (T)
2365 return QualType(T, 0);
2366
Douglas Gregorc42075a2010-02-04 18:10:26 +00002367 QualType Canon = NamedType;
2368 if (!Canon.isCanonical()) {
2369 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002370 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2371 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00002372 (void)CheckT;
2373 }
2374
Abramo Bagnara6150c882010-05-11 21:36:43 +00002375 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00002376 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002377 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002378 return QualType(T, 0);
2379}
2380
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002381QualType
Jay Foad39c79802011-01-12 09:06:06 +00002382ASTContext::getParenType(QualType InnerType) const {
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002383 llvm::FoldingSetNodeID ID;
2384 ParenType::Profile(ID, InnerType);
2385
2386 void *InsertPos = 0;
2387 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2388 if (T)
2389 return QualType(T, 0);
2390
2391 QualType Canon = InnerType;
2392 if (!Canon.isCanonical()) {
2393 Canon = getCanonicalType(InnerType);
2394 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2395 assert(!CheckT && "Paren canonical type broken");
2396 (void)CheckT;
2397 }
2398
2399 T = new (*this) ParenType(InnerType, Canon);
2400 Types.push_back(T);
2401 ParenTypes.InsertNode(T, InsertPos);
2402 return QualType(T, 0);
2403}
2404
Douglas Gregor02085352010-03-31 20:19:30 +00002405QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2406 NestedNameSpecifier *NNS,
2407 const IdentifierInfo *Name,
Jay Foad39c79802011-01-12 09:06:06 +00002408 QualType Canon) const {
Douglas Gregor333489b2009-03-27 23:10:48 +00002409 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2410
2411 if (Canon.isNull()) {
2412 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00002413 ElaboratedTypeKeyword CanonKeyword = Keyword;
2414 if (Keyword == ETK_None)
2415 CanonKeyword = ETK_Typename;
2416
2417 if (CanonNNS != NNS || CanonKeyword != Keyword)
2418 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002419 }
2420
2421 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002422 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002423
2424 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002425 DependentNameType *T
2426 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00002427 if (T)
2428 return QualType(T, 0);
2429
Douglas Gregor02085352010-03-31 20:19:30 +00002430 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002431 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002432 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002433 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002434}
2435
Mike Stump11289f42009-09-09 15:08:12 +00002436QualType
John McCallc392f372010-06-11 00:33:02 +00002437ASTContext::getDependentTemplateSpecializationType(
2438 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002439 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002440 const IdentifierInfo *Name,
Jay Foad39c79802011-01-12 09:06:06 +00002441 const TemplateArgumentListInfo &Args) const {
John McCallc392f372010-06-11 00:33:02 +00002442 // TODO: avoid this copy
2443 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2444 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2445 ArgCopy.push_back(Args[I].getArgument());
2446 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2447 ArgCopy.size(),
2448 ArgCopy.data());
2449}
2450
2451QualType
2452ASTContext::getDependentTemplateSpecializationType(
2453 ElaboratedTypeKeyword Keyword,
2454 NestedNameSpecifier *NNS,
2455 const IdentifierInfo *Name,
2456 unsigned NumArgs,
Jay Foad39c79802011-01-12 09:06:06 +00002457 const TemplateArgument *Args) const {
Douglas Gregor6e068012011-02-28 00:04:36 +00002458 assert((!NNS || NNS->isDependent()) &&
2459 "nested-name-specifier must be dependent");
Douglas Gregordce2b622009-04-01 00:28:59 +00002460
Douglas Gregorc42075a2010-02-04 18:10:26 +00002461 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002462 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2463 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002464
2465 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002466 DependentTemplateSpecializationType *T
2467 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002468 if (T)
2469 return QualType(T, 0);
2470
John McCallc392f372010-06-11 00:33:02 +00002471 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002472
John McCallc392f372010-06-11 00:33:02 +00002473 ElaboratedTypeKeyword CanonKeyword = Keyword;
2474 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2475
2476 bool AnyNonCanonArgs = false;
2477 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2478 for (unsigned I = 0; I != NumArgs; ++I) {
2479 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2480 if (!CanonArgs[I].structurallyEquals(Args[I]))
2481 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002482 }
2483
John McCallc392f372010-06-11 00:33:02 +00002484 QualType Canon;
2485 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2486 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2487 Name, NumArgs,
2488 CanonArgs.data());
2489
2490 // Find the insert position again.
2491 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2492 }
2493
2494 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2495 sizeof(TemplateArgument) * NumArgs),
2496 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002497 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002498 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002499 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002500 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002501 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002502}
2503
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002504QualType ASTContext::getPackExpansionType(QualType Pattern,
2505 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00002506 llvm::FoldingSetNodeID ID;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002507 PackExpansionType::Profile(ID, Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002508
2509 assert(Pattern->containsUnexpandedParameterPack() &&
2510 "Pack expansions must expand one or more parameter packs");
2511 void *InsertPos = 0;
2512 PackExpansionType *T
2513 = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2514 if (T)
2515 return QualType(T, 0);
2516
2517 QualType Canon;
2518 if (!Pattern.isCanonical()) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002519 Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002520
2521 // Find the insert position again.
2522 PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2523 }
2524
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00002525 T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00002526 Types.push_back(T);
2527 PackExpansionTypes.InsertNode(T, InsertPos);
2528 return QualType(T, 0);
2529}
2530
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002531/// CmpProtocolNames - Comparison predicate for sorting protocols
2532/// alphabetically.
2533static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2534 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002535 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002536}
2537
John McCall8b07ec22010-05-15 11:32:37 +00002538static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002539 unsigned NumProtocols) {
2540 if (NumProtocols == 0) return true;
2541
2542 for (unsigned i = 1; i != NumProtocols; ++i)
2543 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2544 return false;
2545 return true;
2546}
2547
2548static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002549 unsigned &NumProtocols) {
2550 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002551
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002552 // Sort protocols, keyed by name.
2553 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2554
2555 // Remove duplicates.
2556 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2557 NumProtocols = ProtocolsEnd-Protocols;
2558}
2559
John McCall8b07ec22010-05-15 11:32:37 +00002560QualType ASTContext::getObjCObjectType(QualType BaseType,
2561 ObjCProtocolDecl * const *Protocols,
Jay Foad39c79802011-01-12 09:06:06 +00002562 unsigned NumProtocols) const {
John McCall8b07ec22010-05-15 11:32:37 +00002563 // If the base type is an interface and there aren't any protocols
2564 // to add, then the interface type will do just fine.
2565 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2566 return BaseType;
2567
2568 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002569 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002570 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002571 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002572 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2573 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002574
John McCall8b07ec22010-05-15 11:32:37 +00002575 // Build the canonical type, which has the canonical base type and
2576 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002577 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002578 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2579 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2580 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002581 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2582 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002583 unsigned UniqueCount = NumProtocols;
2584
John McCallfc93cf92009-10-22 22:37:11 +00002585 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002586 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2587 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002588 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002589 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2590 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002591 }
2592
2593 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002594 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2595 }
2596
2597 unsigned Size = sizeof(ObjCObjectTypeImpl);
2598 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2599 void *Mem = Allocate(Size, TypeAlignment);
2600 ObjCObjectTypeImpl *T =
2601 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2602
2603 Types.push_back(T);
2604 ObjCObjectTypes.InsertNode(T, InsertPos);
2605 return QualType(T, 0);
2606}
2607
2608/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2609/// the given object type.
Jay Foad39c79802011-01-12 09:06:06 +00002610QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
John McCall8b07ec22010-05-15 11:32:37 +00002611 llvm::FoldingSetNodeID ID;
2612 ObjCObjectPointerType::Profile(ID, ObjectT);
2613
2614 void *InsertPos = 0;
2615 if (ObjCObjectPointerType *QT =
2616 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2617 return QualType(QT, 0);
2618
2619 // Find the canonical object type.
2620 QualType Canonical;
2621 if (!ObjectT.isCanonical()) {
2622 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2623
2624 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002625 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2626 }
2627
Douglas Gregorf85bee62010-02-08 22:59:26 +00002628 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002629 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2630 ObjCObjectPointerType *QType =
2631 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002632
Steve Narofffb4330f2009-06-17 22:40:22 +00002633 Types.push_back(QType);
2634 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002635 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002636}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002637
Douglas Gregor1c283312010-08-11 12:19:30 +00002638/// getObjCInterfaceType - Return the unique reference to the type for the
2639/// specified ObjC interface decl. The list of protocols is optional.
Jay Foad39c79802011-01-12 09:06:06 +00002640QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const {
Douglas Gregor1c283312010-08-11 12:19:30 +00002641 if (Decl->TypeForDecl)
2642 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002643
Douglas Gregor1c283312010-08-11 12:19:30 +00002644 // FIXME: redeclarations?
2645 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2646 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2647 Decl->TypeForDecl = T;
2648 Types.push_back(T);
2649 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002650}
2651
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002652/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2653/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002654/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002655/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002656/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002657QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
Douglas Gregorabd68132009-07-08 00:03:05 +00002658 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002659 if (tofExpr->isTypeDependent()) {
2660 llvm::FoldingSetNodeID ID;
2661 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002662
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002663 void *InsertPos = 0;
2664 DependentTypeOfExprType *Canon
2665 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2666 if (Canon) {
2667 // We already have a "canonical" version of an identical, dependent
2668 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002669 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002670 QualType((TypeOfExprType*)Canon, 0));
2671 }
2672 else {
2673 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002674 Canon
2675 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002676 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2677 toe = Canon;
2678 }
2679 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002680 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002681 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002682 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002683 Types.push_back(toe);
2684 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002685}
2686
Steve Naroffa773cd52007-08-01 18:02:17 +00002687/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2688/// TypeOfType AST's. The only motivation to unique these nodes would be
2689/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002690/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002691/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002692QualType ASTContext::getTypeOfType(QualType tofType) const {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002693 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002694 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002695 Types.push_back(tot);
2696 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002697}
2698
Anders Carlssonad6bd352009-06-24 21:24:56 +00002699/// getDecltypeForExpr - Given an expr, will return the decltype for that
2700/// expression, according to the rules in C++0x [dcl.type.simple]p4
Jay Foad39c79802011-01-12 09:06:06 +00002701static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002702 if (e->isTypeDependent())
2703 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002704
Anders Carlssonad6bd352009-06-24 21:24:56 +00002705 // If e is an id expression or a class member access, decltype(e) is defined
2706 // as the type of the entity named by e.
2707 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2708 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2709 return VD->getType();
2710 }
2711 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2712 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2713 return FD->getType();
2714 }
2715 // If e is a function call or an invocation of an overloaded operator,
2716 // (parentheses around e are ignored), decltype(e) is defined as the
2717 // return type of that function.
2718 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2719 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002720
Anders Carlssonad6bd352009-06-24 21:24:56 +00002721 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002722
2723 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002724 // defined as T&, otherwise decltype(e) is defined as T.
John McCall086a4642010-11-24 05:12:34 +00002725 if (e->isLValue())
Anders Carlssonad6bd352009-06-24 21:24:56 +00002726 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002727
Anders Carlssonad6bd352009-06-24 21:24:56 +00002728 return T;
2729}
2730
Anders Carlsson81df7b82009-06-24 19:06:50 +00002731/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2732/// DecltypeType AST's. The only motivation to unique these nodes would be
2733/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002734/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002735/// on canonical type's (which are always unique).
Jay Foad39c79802011-01-12 09:06:06 +00002736QualType ASTContext::getDecltypeType(Expr *e) const {
Douglas Gregorabd68132009-07-08 00:03:05 +00002737 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002738 if (e->isTypeDependent()) {
2739 llvm::FoldingSetNodeID ID;
2740 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002741
Douglas Gregora21f6c32009-07-30 23:36:40 +00002742 void *InsertPos = 0;
2743 DependentDecltypeType *Canon
2744 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2745 if (Canon) {
2746 // We already have a "canonical" version of an equivalent, dependent
2747 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002748 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002749 QualType((DecltypeType*)Canon, 0));
2750 }
2751 else {
2752 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002753 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002754 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2755 dt = Canon;
2756 }
2757 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002758 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002759 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002760 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002761 Types.push_back(dt);
2762 return QualType(dt, 0);
2763}
2764
Richard Smithb2bc2e62011-02-21 20:05:19 +00002765/// getAutoType - We only unique auto types after they've been deduced.
Richard Smith30482bc2011-02-20 03:19:35 +00002766QualType ASTContext::getAutoType(QualType DeducedType) const {
Richard Smithb2bc2e62011-02-21 20:05:19 +00002767 void *InsertPos = 0;
2768 if (!DeducedType.isNull()) {
2769 // Look in the folding set for an existing type.
2770 llvm::FoldingSetNodeID ID;
2771 AutoType::Profile(ID, DeducedType);
2772 if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
2773 return QualType(AT, 0);
2774 }
2775
2776 AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
2777 Types.push_back(AT);
2778 if (InsertPos)
2779 AutoTypes.InsertNode(AT, InsertPos);
2780 return QualType(AT, 0);
Richard Smith30482bc2011-02-20 03:19:35 +00002781}
2782
Richard Smith02e85f32011-04-14 22:09:26 +00002783/// getAutoDeductType - Get type pattern for deducing against 'auto'.
2784QualType ASTContext::getAutoDeductType() const {
2785 if (AutoDeductTy.isNull())
2786 AutoDeductTy = getAutoType(QualType());
2787 assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
2788 return AutoDeductTy;
2789}
2790
2791/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
2792QualType ASTContext::getAutoRRefDeductType() const {
2793 if (AutoRRefDeductTy.isNull())
2794 AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
2795 assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
2796 return AutoRRefDeductTy;
2797}
2798
Chris Lattnerfb072462007-01-23 05:45:31 +00002799/// getTagDeclType - Return the unique reference to the type for the
2800/// specified TagDecl (struct/union/class/enum) decl.
Jay Foad39c79802011-01-12 09:06:06 +00002801QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002802 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002803 // FIXME: What is the design on getTagDeclType when it requires casting
2804 // away const? mutable?
2805 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002806}
2807
Mike Stump11289f42009-09-09 15:08:12 +00002808/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2809/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2810/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002811CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002812 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002813}
Chris Lattnerfb072462007-01-23 05:45:31 +00002814
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002815/// getSignedWCharType - Return the type of "signed wchar_t".
2816/// Used when in C++, as a GCC extension.
2817QualType ASTContext::getSignedWCharType() const {
2818 // FIXME: derive from "Target" ?
2819 return WCharTy;
2820}
2821
2822/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2823/// Used when in C++, as a GCC extension.
2824QualType ASTContext::getUnsignedWCharType() const {
2825 // FIXME: derive from "Target" ?
2826 return UnsignedIntTy;
2827}
2828
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002829/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2830/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2831QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002832 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002833}
2834
Chris Lattnera21ad802008-04-02 05:18:44 +00002835//===----------------------------------------------------------------------===//
2836// Type Operators
2837//===----------------------------------------------------------------------===//
2838
Jay Foad39c79802011-01-12 09:06:06 +00002839CanQualType ASTContext::getCanonicalParamType(QualType T) const {
John McCallfc93cf92009-10-22 22:37:11 +00002840 // Push qualifiers into arrays, and then discard any remaining
2841 // qualifiers.
2842 T = getCanonicalType(T);
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00002843 T = getVariableArrayDecayedType(T);
John McCallfc93cf92009-10-22 22:37:11 +00002844 const Type *Ty = T.getTypePtr();
John McCallfc93cf92009-10-22 22:37:11 +00002845 QualType Result;
2846 if (isa<ArrayType>(Ty)) {
2847 Result = getArrayDecayedType(QualType(Ty,0));
2848 } else if (isa<FunctionType>(Ty)) {
2849 Result = getPointerType(QualType(Ty, 0));
2850 } else {
2851 Result = QualType(Ty, 0);
2852 }
2853
2854 return CanQualType::CreateUnsafe(Result);
2855}
2856
Chris Lattner7adf0762008-08-04 07:31:14 +00002857
John McCall6c9dd522011-01-18 07:41:22 +00002858QualType ASTContext::getUnqualifiedArrayType(QualType type,
2859 Qualifiers &quals) {
2860 SplitQualType splitType = type.getSplitUnqualifiedType();
2861
2862 // FIXME: getSplitUnqualifiedType() actually walks all the way to
2863 // the unqualified desugared type and then drops it on the floor.
2864 // We then have to strip that sugar back off with
2865 // getUnqualifiedDesugaredType(), which is silly.
2866 const ArrayType *AT =
2867 dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType());
2868
2869 // If we don't have an array, just use the results in splitType.
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002870 if (!AT) {
John McCall6c9dd522011-01-18 07:41:22 +00002871 quals = splitType.second;
2872 return QualType(splitType.first, 0);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002873 }
2874
John McCall6c9dd522011-01-18 07:41:22 +00002875 // Otherwise, recurse on the array's element type.
2876 QualType elementType = AT->getElementType();
2877 QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
2878
2879 // If that didn't change the element type, AT has no qualifiers, so we
2880 // can just use the results in splitType.
2881 if (elementType == unqualElementType) {
2882 assert(quals.empty()); // from the recursive call
2883 quals = splitType.second;
2884 return QualType(splitType.first, 0);
2885 }
2886
2887 // Otherwise, add in the qualifiers from the outermost type, then
2888 // build the type back up.
2889 quals.addConsistentQualifiers(splitType.second);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002890
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002891 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002892 return getConstantArrayType(unqualElementType, CAT->getSize(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002893 CAT->getSizeModifier(), 0);
2894 }
2895
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002896 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002897 return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002898 }
2899
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002900 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
John McCall6c9dd522011-01-18 07:41:22 +00002901 return getVariableArrayType(unqualElementType,
John McCallc3007a22010-10-26 07:05:15 +00002902 VAT->getSizeExpr(),
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002903 VAT->getSizeModifier(),
2904 VAT->getIndexTypeCVRQualifiers(),
2905 VAT->getBracketsRange());
2906 }
2907
2908 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCall6c9dd522011-01-18 07:41:22 +00002909 return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002910 DSAT->getSizeModifier(), 0,
2911 SourceRange());
2912}
2913
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002914/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2915/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2916/// they point to and return true. If T1 and T2 aren't pointer types
2917/// or pointer-to-member types, or if they are not similar at this
2918/// level, returns false and leaves T1 and T2 unchanged. Top-level
2919/// qualifiers on T1 and T2 are ignored. This function will typically
2920/// be called in a loop that successively "unwraps" pointer and
2921/// pointer-to-member types to compare them at each level.
2922bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2923 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2924 *T2PtrType = T2->getAs<PointerType>();
2925 if (T1PtrType && T2PtrType) {
2926 T1 = T1PtrType->getPointeeType();
2927 T2 = T2PtrType->getPointeeType();
2928 return true;
2929 }
2930
2931 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2932 *T2MPType = T2->getAs<MemberPointerType>();
2933 if (T1MPType && T2MPType &&
2934 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2935 QualType(T2MPType->getClass(), 0))) {
2936 T1 = T1MPType->getPointeeType();
2937 T2 = T2MPType->getPointeeType();
2938 return true;
2939 }
2940
2941 if (getLangOptions().ObjC1) {
2942 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2943 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2944 if (T1OPType && T2OPType) {
2945 T1 = T1OPType->getPointeeType();
2946 T2 = T2OPType->getPointeeType();
2947 return true;
2948 }
2949 }
2950
2951 // FIXME: Block pointers, too?
2952
2953 return false;
2954}
2955
Jay Foad39c79802011-01-12 09:06:06 +00002956DeclarationNameInfo
2957ASTContext::getNameForTemplate(TemplateName Name,
2958 SourceLocation NameLoc) const {
John McCall847e2a12009-11-24 18:42:40 +00002959 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002960 // DNInfo work in progress: CHECKME: what about DNLoc?
2961 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2962
John McCall847e2a12009-11-24 18:42:40 +00002963 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002964 DeclarationName DName;
John McCall847e2a12009-11-24 18:42:40 +00002965 if (DTN->isIdentifier()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002966 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2967 return DeclarationNameInfo(DName, NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002968 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002969 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2970 // DNInfo work in progress: FIXME: source locations?
2971 DeclarationNameLoc DNLoc;
2972 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2973 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2974 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall847e2a12009-11-24 18:42:40 +00002975 }
2976 }
2977
John McCalld28ae272009-12-02 08:04:21 +00002978 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2979 assert(Storage);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002980 // DNInfo work in progress: CHECKME: what about DNLoc?
2981 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002982}
2983
Jay Foad39c79802011-01-12 09:06:06 +00002984TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002985 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2986 if (TemplateTemplateParmDecl *TTP
2987 = dyn_cast<TemplateTemplateParmDecl>(Template))
2988 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2989
2990 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002991 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002992 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002993
Douglas Gregor5590be02011-01-15 06:45:20 +00002994 if (SubstTemplateTemplateParmPackStorage *SubstPack
2995 = Name.getAsSubstTemplateTemplateParmPack()) {
2996 TemplateTemplateParmDecl *CanonParam
2997 = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack());
2998 TemplateArgument CanonArgPack
2999 = getCanonicalTemplateArgument(SubstPack->getArgumentPack());
3000 return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack);
3001 }
3002
John McCalld28ae272009-12-02 08:04:21 +00003003 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00003004
Douglas Gregor6bc50582009-05-07 06:41:52 +00003005 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
3006 assert(DTN && "Non-dependent template names must refer to template decls.");
3007 return DTN->CanonicalTemplateName;
3008}
3009
Douglas Gregoradee3e32009-11-11 23:06:43 +00003010bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3011 X = getCanonicalTemplateName(X);
3012 Y = getCanonicalTemplateName(Y);
3013 return X.getAsVoidPointer() == Y.getAsVoidPointer();
3014}
3015
Mike Stump11289f42009-09-09 15:08:12 +00003016TemplateArgument
Jay Foad39c79802011-01-12 09:06:06 +00003017ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
Douglas Gregora8e02e72009-07-28 23:00:59 +00003018 switch (Arg.getKind()) {
3019 case TemplateArgument::Null:
3020 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00003021
Douglas Gregora8e02e72009-07-28 23:00:59 +00003022 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00003023 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00003024
Douglas Gregora8e02e72009-07-28 23:00:59 +00003025 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00003026 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00003027
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003028 case TemplateArgument::Template:
3029 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003030
3031 case TemplateArgument::TemplateExpansion:
3032 return TemplateArgument(getCanonicalTemplateName(
3033 Arg.getAsTemplateOrTemplatePattern()),
Douglas Gregore1d60df2011-01-14 23:41:42 +00003034 Arg.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003035
Douglas Gregora8e02e72009-07-28 23:00:59 +00003036 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00003037 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00003038 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00003039
Douglas Gregora8e02e72009-07-28 23:00:59 +00003040 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00003041 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00003042
Douglas Gregora8e02e72009-07-28 23:00:59 +00003043 case TemplateArgument::Pack: {
Douglas Gregor0192c232010-12-20 16:52:59 +00003044 if (Arg.pack_size() == 0)
3045 return Arg;
3046
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003047 TemplateArgument *CanonArgs
3048 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregora8e02e72009-07-28 23:00:59 +00003049 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003050 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00003051 AEnd = Arg.pack_end();
3052 A != AEnd; (void)++A, ++Idx)
3053 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00003054
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003055 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregora8e02e72009-07-28 23:00:59 +00003056 }
3057 }
3058
3059 // Silence GCC warning
3060 assert(false && "Unhandled template argument kind");
3061 return TemplateArgument();
3062}
3063
Douglas Gregor333489b2009-03-27 23:10:48 +00003064NestedNameSpecifier *
Jay Foad39c79802011-01-12 09:06:06 +00003065ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
Mike Stump11289f42009-09-09 15:08:12 +00003066 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00003067 return 0;
3068
3069 switch (NNS->getKind()) {
3070 case NestedNameSpecifier::Identifier:
3071 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00003072 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00003073 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3074 NNS->getAsIdentifier());
3075
3076 case NestedNameSpecifier::Namespace:
3077 // A namespace is canonical; build a nested-name-specifier with
3078 // this namespace and no prefix.
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003079 return NestedNameSpecifier::Create(*this, 0,
3080 NNS->getAsNamespace()->getOriginalNamespace());
3081
3082 case NestedNameSpecifier::NamespaceAlias:
3083 // A namespace is canonical; build a nested-name-specifier with
3084 // this namespace and no prefix.
3085 return NestedNameSpecifier::Create(*this, 0,
3086 NNS->getAsNamespaceAlias()->getNamespace()
3087 ->getOriginalNamespace());
Douglas Gregor333489b2009-03-27 23:10:48 +00003088
3089 case NestedNameSpecifier::TypeSpec:
3090 case NestedNameSpecifier::TypeSpecWithTemplate: {
3091 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor3ade5702010-11-04 00:09:33 +00003092
3093 // If we have some kind of dependent-named type (e.g., "typename T::type"),
3094 // break it apart into its prefix and identifier, then reconsititute those
3095 // as the canonical nested-name-specifier. This is required to canonicalize
3096 // a dependent nested-name-specifier involving typedefs of dependent-name
3097 // types, e.g.,
3098 // typedef typename T::type T1;
3099 // typedef typename T1::type T2;
3100 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
3101 NestedNameSpecifier *Prefix
3102 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
3103 return NestedNameSpecifier::Create(*this, Prefix,
3104 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3105 }
3106
Douglas Gregorcc10cbf2010-11-04 00:14:23 +00003107 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor3ade5702010-11-04 00:09:33 +00003108 if (const DependentTemplateSpecializationType *DTST
3109 = T->getAs<DependentTemplateSpecializationType>()) {
3110 NestedNameSpecifier *Prefix
3111 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
Douglas Gregor6e068012011-02-28 00:04:36 +00003112
3113 T = getDependentTemplateSpecializationType(DTST->getKeyword(),
3114 Prefix, DTST->getIdentifier(),
3115 DTST->getNumArgs(),
3116 DTST->getArgs());
Douglas Gregor3ade5702010-11-04 00:09:33 +00003117 T = getCanonicalType(T);
3118 }
3119
John McCall33ddac02011-01-19 10:06:00 +00003120 return NestedNameSpecifier::Create(*this, 0, false,
3121 const_cast<Type*>(T.getTypePtr()));
Douglas Gregor333489b2009-03-27 23:10:48 +00003122 }
3123
3124 case NestedNameSpecifier::Global:
3125 // The global specifier is canonical and unique.
3126 return NNS;
3127 }
3128
3129 // Required to silence a GCC warning
3130 return 0;
3131}
3132
Chris Lattner7adf0762008-08-04 07:31:14 +00003133
Jay Foad39c79802011-01-12 09:06:06 +00003134const ArrayType *ASTContext::getAsArrayType(QualType T) const {
Chris Lattner7adf0762008-08-04 07:31:14 +00003135 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00003136 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00003137 // Handle the common positive case fast.
3138 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3139 return AT;
3140 }
Mike Stump11289f42009-09-09 15:08:12 +00003141
John McCall8ccfcb52009-09-24 19:53:00 +00003142 // Handle the common negative case fast.
John McCall33ddac02011-01-19 10:06:00 +00003143 if (!isa<ArrayType>(T.getCanonicalType()))
Chris Lattner7adf0762008-08-04 07:31:14 +00003144 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003145
John McCall8ccfcb52009-09-24 19:53:00 +00003146 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00003147 // implements C99 6.7.3p8: "If the specification of an array type includes
3148 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00003149
Chris Lattner7adf0762008-08-04 07:31:14 +00003150 // If we get here, we either have type qualifiers on the type, or we have
3151 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00003152 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00003153
John McCall33ddac02011-01-19 10:06:00 +00003154 SplitQualType split = T.getSplitDesugaredType();
3155 Qualifiers qs = split.second;
Mike Stump11289f42009-09-09 15:08:12 +00003156
Chris Lattner7adf0762008-08-04 07:31:14 +00003157 // If we have a simple case, just return now.
John McCall33ddac02011-01-19 10:06:00 +00003158 const ArrayType *ATy = dyn_cast<ArrayType>(split.first);
3159 if (ATy == 0 || qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00003160 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00003161
Chris Lattner7adf0762008-08-04 07:31:14 +00003162 // Otherwise, we have an array and we have qualifiers on it. Push the
3163 // qualifiers into the array element type and return a new array type.
John McCall33ddac02011-01-19 10:06:00 +00003164 QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
Mike Stump11289f42009-09-09 15:08:12 +00003165
Chris Lattner7adf0762008-08-04 07:31:14 +00003166 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3167 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3168 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003169 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00003170 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3171 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3172 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003173 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00003174
Mike Stump11289f42009-09-09 15:08:12 +00003175 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00003176 = dyn_cast<DependentSizedArrayType>(ATy))
3177 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00003178 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00003179 DSAT->getSizeExpr(),
Douglas Gregor4619e432008-12-05 23:32:09 +00003180 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003181 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00003182 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00003183
Chris Lattner7adf0762008-08-04 07:31:14 +00003184 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00003185 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00003186 VAT->getSizeExpr(),
Chris Lattner7adf0762008-08-04 07:31:14 +00003187 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00003188 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00003189 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00003190}
3191
Chris Lattnera21ad802008-04-02 05:18:44 +00003192/// getArrayDecayedType - Return the properly qualified result of decaying the
3193/// specified array type to a pointer. This operation is non-trivial when
3194/// handling typedefs etc. The canonical type of "T" must be an array type,
3195/// this returns a pointer to a properly qualified element of the array.
3196///
3197/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
Jay Foad39c79802011-01-12 09:06:06 +00003198QualType ASTContext::getArrayDecayedType(QualType Ty) const {
Chris Lattner7adf0762008-08-04 07:31:14 +00003199 // Get the element type with 'getAsArrayType' so that we don't lose any
3200 // typedefs in the element type of the array. This also handles propagation
3201 // of type qualifiers from the array type into the element type if present
3202 // (C99 6.7.3p8).
3203 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3204 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00003205
Chris Lattner7adf0762008-08-04 07:31:14 +00003206 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00003207
3208 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00003209 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00003210}
3211
John McCall33ddac02011-01-19 10:06:00 +00003212QualType ASTContext::getBaseElementType(const ArrayType *array) const {
3213 return getBaseElementType(array->getElementType());
Douglas Gregor79f83ed2009-07-23 23:49:00 +00003214}
3215
John McCall33ddac02011-01-19 10:06:00 +00003216QualType ASTContext::getBaseElementType(QualType type) const {
3217 Qualifiers qs;
3218 while (true) {
3219 SplitQualType split = type.getSplitDesugaredType();
3220 const ArrayType *array = split.first->getAsArrayTypeUnsafe();
3221 if (!array) break;
Mike Stump11289f42009-09-09 15:08:12 +00003222
John McCall33ddac02011-01-19 10:06:00 +00003223 type = array->getElementType();
3224 qs.addConsistentQualifiers(split.second);
3225 }
Mike Stump11289f42009-09-09 15:08:12 +00003226
John McCall33ddac02011-01-19 10:06:00 +00003227 return getQualifiedType(type, qs);
Anders Carlssone0808df2008-12-21 03:44:36 +00003228}
3229
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00003230/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00003231uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00003232ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
3233 uint64_t ElementCount = 1;
3234 do {
3235 ElementCount *= CA->getSize().getZExtValue();
3236 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
3237 } while (CA);
3238 return ElementCount;
3239}
3240
Steve Naroff0af91202007-04-27 21:51:21 +00003241/// getFloatingRank - Return a relative rank for floating point types.
3242/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00003243static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00003244 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00003245 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00003246
John McCall9dd450b2009-09-21 23:43:11 +00003247 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3248 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00003249 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00003250 case BuiltinType::Float: return FloatRank;
3251 case BuiltinType::Double: return DoubleRank;
3252 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00003253 }
3254}
3255
Mike Stump11289f42009-09-09 15:08:12 +00003256/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
3257/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00003258/// 'typeDomain' is a real floating point or complex type.
3259/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00003260QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
3261 QualType Domain) const {
3262 FloatingRank EltRank = getFloatingRank(Size);
3263 if (Domain->isComplexType()) {
3264 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00003265 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00003266 case FloatRank: return FloatComplexTy;
3267 case DoubleRank: return DoubleComplexTy;
3268 case LongDoubleRank: return LongDoubleComplexTy;
3269 }
Steve Naroff0af91202007-04-27 21:51:21 +00003270 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00003271
3272 assert(Domain->isRealFloatingType() && "Unknown domain!");
3273 switch (EltRank) {
3274 default: assert(0 && "getFloatingRank(): illegal value for rank");
3275 case FloatRank: return FloatTy;
3276 case DoubleRank: return DoubleTy;
3277 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00003278 }
Steve Naroffe4718892007-04-27 18:30:00 +00003279}
3280
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003281/// getFloatingTypeOrder - Compare the rank of the two specified floating
3282/// point types, ignoring the domain of the type (i.e. 'double' ==
3283/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003284/// LHS < RHS, return -1.
Jay Foad39c79802011-01-12 09:06:06 +00003285int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
Chris Lattnerb90739d2008-04-06 23:38:49 +00003286 FloatingRank LHSR = getFloatingRank(LHS);
3287 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00003288
Chris Lattnerb90739d2008-04-06 23:38:49 +00003289 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00003290 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00003291 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00003292 return 1;
3293 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00003294}
3295
Chris Lattner76a00cf2008-04-06 22:59:24 +00003296/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3297/// routine will assert if passed a built-in type that isn't an integer or enum,
3298/// or if it is not canonicalized.
John McCall424cec92011-01-19 06:33:43 +00003299unsigned ASTContext::getIntegerRank(const Type *T) const {
John McCallb692a092009-10-22 20:10:53 +00003300 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
John McCall424cec92011-01-19 06:33:43 +00003301 if (const EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00003302 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00003303
Chris Lattnerad3467e2010-12-25 23:25:43 +00003304 if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
3305 T->isSpecificBuiltinType(BuiltinType::WChar_U))
Eli Friedmanc131d3b2009-07-05 23:44:27 +00003306 T = getFromTargetType(Target.getWCharType()).getTypePtr();
3307
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003308 if (T->isSpecificBuiltinType(BuiltinType::Char16))
3309 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
3310
3311 if (T->isSpecificBuiltinType(BuiltinType::Char32))
3312 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
3313
Chris Lattner76a00cf2008-04-06 22:59:24 +00003314 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003315 default: assert(0 && "getIntegerRank(): not a built-in integer");
3316 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003317 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003318 case BuiltinType::Char_S:
3319 case BuiltinType::Char_U:
3320 case BuiltinType::SChar:
3321 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003322 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003323 case BuiltinType::Short:
3324 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003325 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003326 case BuiltinType::Int:
3327 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003328 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003329 case BuiltinType::Long:
3330 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003331 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003332 case BuiltinType::LongLong:
3333 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00003334 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00003335 case BuiltinType::Int128:
3336 case BuiltinType::UInt128:
3337 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00003338 }
3339}
3340
Eli Friedman629ffb92009-08-20 04:21:42 +00003341/// \brief Whether this is a promotable bitfield reference according
3342/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
3343///
3344/// \returns the type this bit-field will promote to, or NULL if no
3345/// promotion occurs.
Jay Foad39c79802011-01-12 09:06:06 +00003346QualType ASTContext::isPromotableBitField(Expr *E) const {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00003347 if (E->isTypeDependent() || E->isValueDependent())
3348 return QualType();
3349
Eli Friedman629ffb92009-08-20 04:21:42 +00003350 FieldDecl *Field = E->getBitField();
3351 if (!Field)
3352 return QualType();
3353
3354 QualType FT = Field->getType();
3355
3356 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3357 uint64_t BitWidth = BitWidthAP.getZExtValue();
3358 uint64_t IntSize = getTypeSize(IntTy);
3359 // GCC extension compatibility: if the bit-field size is less than or equal
3360 // to the size of int, it gets promoted no matter what its type is.
3361 // For instance, unsigned long bf : 4 gets promoted to signed int.
3362 if (BitWidth < IntSize)
3363 return IntTy;
3364
3365 if (BitWidth == IntSize)
3366 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3367
3368 // Types bigger than int are not subject to promotions, and therefore act
3369 // like the base type.
3370 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3371 // is ridiculous.
3372 return QualType();
3373}
3374
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003375/// getPromotedIntegerType - Returns the type that Promotable will
3376/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3377/// integer type.
Jay Foad39c79802011-01-12 09:06:06 +00003378QualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003379 assert(!Promotable.isNull());
3380 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00003381 if (const EnumType *ET = Promotable->getAs<EnumType>())
3382 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003383 if (Promotable->isSignedIntegerType())
3384 return IntTy;
3385 uint64_t PromotableSize = getTypeSize(Promotable);
3386 uint64_t IntSize = getTypeSize(IntTy);
3387 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3388 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3389}
3390
Mike Stump11289f42009-09-09 15:08:12 +00003391/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003392/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003393/// LHS < RHS, return -1.
Jay Foad39c79802011-01-12 09:06:06 +00003394int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
John McCall424cec92011-01-19 06:33:43 +00003395 const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3396 const Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003397 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003398
Chris Lattner76a00cf2008-04-06 22:59:24 +00003399 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3400 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00003401
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003402 unsigned LHSRank = getIntegerRank(LHSC);
3403 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00003404
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003405 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3406 if (LHSRank == RHSRank) return 0;
3407 return LHSRank > RHSRank ? 1 : -1;
3408 }
Mike Stump11289f42009-09-09 15:08:12 +00003409
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003410 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3411 if (LHSUnsigned) {
3412 // If the unsigned [LHS] type is larger, return it.
3413 if (LHSRank >= RHSRank)
3414 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00003415
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003416 // If the signed type can represent all values of the unsigned type, it
3417 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003418 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003419 return -1;
3420 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00003421
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003422 // If the unsigned [RHS] type is larger, return it.
3423 if (RHSRank >= LHSRank)
3424 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00003425
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003426 // If the signed type can represent all values of the unsigned type, it
3427 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003428 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003429 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00003430}
Anders Carlsson98f07902007-08-17 05:31:46 +00003431
Anders Carlsson6d417272009-11-14 21:45:58 +00003432static RecordDecl *
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003433CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3434 DeclContext *DC, IdentifierInfo *Id) {
3435 SourceLocation Loc;
Anders Carlsson6d417272009-11-14 21:45:58 +00003436 if (Ctx.getLangOptions().CPlusPlus)
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003437 return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
Anders Carlsson6d417272009-11-14 21:45:58 +00003438 else
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003439 return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
Anders Carlsson6d417272009-11-14 21:45:58 +00003440}
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003441
Mike Stump11289f42009-09-09 15:08:12 +00003442// getCFConstantStringType - Return the type used for constant CFStrings.
Jay Foad39c79802011-01-12 09:06:06 +00003443QualType ASTContext::getCFConstantStringType() const {
Anders Carlsson98f07902007-08-17 05:31:46 +00003444 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00003445 CFConstantStringTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003446 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003447 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00003448 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00003449
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003450 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00003451
Anders Carlsson98f07902007-08-17 05:31:46 +00003452 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00003453 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003454 // int flags;
3455 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00003456 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00003457 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00003458 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00003459 FieldTypes[3] = LongTy;
3460
Anders Carlsson98f07902007-08-17 05:31:46 +00003461 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00003462 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003463 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003464 SourceLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003465 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003466 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003467 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003468 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003469 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003470 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003471 }
3472
Douglas Gregord5058122010-02-11 01:19:42 +00003473 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00003474 }
Mike Stump11289f42009-09-09 15:08:12 +00003475
Anders Carlsson98f07902007-08-17 05:31:46 +00003476 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00003477}
Anders Carlsson87c149b2007-10-11 01:00:40 +00003478
Douglas Gregor512b0772009-04-23 22:29:11 +00003479void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003480 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003481 assert(Rec && "Invalid CFConstantStringType");
3482 CFConstantStringTypeDecl = Rec->getDecl();
3483}
3484
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003485// getNSConstantStringType - Return the type used for constant NSStrings.
Jay Foad39c79802011-01-12 09:06:06 +00003486QualType ASTContext::getNSConstantStringType() const {
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003487 if (!NSConstantStringTypeDecl) {
3488 NSConstantStringTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003489 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003490 &Idents.get("__builtin_NSString"));
3491 NSConstantStringTypeDecl->startDefinition();
3492
3493 QualType FieldTypes[3];
3494
3495 // const int *isa;
3496 FieldTypes[0] = getPointerType(IntTy.withConst());
3497 // const char *str;
3498 FieldTypes[1] = getPointerType(CharTy.withConst());
3499 // unsigned int length;
3500 FieldTypes[2] = UnsignedIntTy;
3501
3502 // Create fields
3503 for (unsigned i = 0; i < 3; ++i) {
3504 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003505 SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003506 SourceLocation(), 0,
3507 FieldTypes[i], /*TInfo=*/0,
3508 /*BitWidth=*/0,
3509 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003510 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003511 NSConstantStringTypeDecl->addDecl(Field);
3512 }
3513
3514 NSConstantStringTypeDecl->completeDefinition();
3515 }
3516
3517 return getTagDeclType(NSConstantStringTypeDecl);
3518}
3519
3520void ASTContext::setNSConstantStringType(QualType T) {
3521 const RecordType *Rec = T->getAs<RecordType>();
3522 assert(Rec && "Invalid NSConstantStringType");
3523 NSConstantStringTypeDecl = Rec->getDecl();
3524}
3525
Jay Foad39c79802011-01-12 09:06:06 +00003526QualType ASTContext::getObjCFastEnumerationStateType() const {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003527 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003528 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003529 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003530 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003531 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003532
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003533 QualType FieldTypes[] = {
3534 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003535 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003536 getPointerType(UnsignedLongTy),
3537 getConstantArrayType(UnsignedLongTy,
3538 llvm::APInt(32, 5), ArrayType::Normal, 0)
3539 };
Mike Stump11289f42009-09-09 15:08:12 +00003540
Douglas Gregor91f84212008-12-11 16:49:14 +00003541 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003542 FieldDecl *Field = FieldDecl::Create(*this,
3543 ObjCFastEnumerationStateTypeDecl,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003544 SourceLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00003545 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003546 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003547 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003548 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003549 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003550 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003551 }
Mike Stump11289f42009-09-09 15:08:12 +00003552
Douglas Gregord5058122010-02-11 01:19:42 +00003553 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003554 }
Mike Stump11289f42009-09-09 15:08:12 +00003555
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003556 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3557}
3558
Jay Foad39c79802011-01-12 09:06:06 +00003559QualType ASTContext::getBlockDescriptorType() const {
Mike Stumpd0153282009-10-20 02:12:22 +00003560 if (BlockDescriptorType)
3561 return getTagDeclType(BlockDescriptorType);
3562
3563 RecordDecl *T;
3564 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003565 T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003566 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003567 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003568
3569 QualType FieldTypes[] = {
3570 UnsignedLongTy,
3571 UnsignedLongTy,
3572 };
3573
3574 const char *FieldNames[] = {
3575 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003576 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003577 };
3578
3579 for (size_t i = 0; i < 2; ++i) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003580 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003581 SourceLocation(),
3582 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003583 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003584 /*BitWidth=*/0,
3585 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003586 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003587 T->addDecl(Field);
3588 }
3589
Douglas Gregord5058122010-02-11 01:19:42 +00003590 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003591
3592 BlockDescriptorType = T;
3593
3594 return getTagDeclType(BlockDescriptorType);
3595}
3596
3597void ASTContext::setBlockDescriptorType(QualType T) {
3598 const RecordType *Rec = T->getAs<RecordType>();
3599 assert(Rec && "Invalid BlockDescriptorType");
3600 BlockDescriptorType = Rec->getDecl();
3601}
3602
Jay Foad39c79802011-01-12 09:06:06 +00003603QualType ASTContext::getBlockDescriptorExtendedType() const {
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003604 if (BlockDescriptorExtendedType)
3605 return getTagDeclType(BlockDescriptorExtendedType);
3606
3607 RecordDecl *T;
3608 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003609 T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson6d417272009-11-14 21:45:58 +00003610 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003611 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003612
3613 QualType FieldTypes[] = {
3614 UnsignedLongTy,
3615 UnsignedLongTy,
3616 getPointerType(VoidPtrTy),
3617 getPointerType(VoidPtrTy)
3618 };
3619
3620 const char *FieldNames[] = {
3621 "reserved",
3622 "Size",
3623 "CopyFuncPtr",
3624 "DestroyFuncPtr"
3625 };
3626
3627 for (size_t i = 0; i < 4; ++i) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003628 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003629 SourceLocation(),
3630 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003631 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003632 /*BitWidth=*/0,
3633 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003634 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003635 T->addDecl(Field);
3636 }
3637
Douglas Gregord5058122010-02-11 01:19:42 +00003638 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003639
3640 BlockDescriptorExtendedType = T;
3641
3642 return getTagDeclType(BlockDescriptorExtendedType);
3643}
3644
3645void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3646 const RecordType *Rec = T->getAs<RecordType>();
3647 assert(Rec && "Invalid BlockDescriptorType");
3648 BlockDescriptorExtendedType = Rec->getDecl();
3649}
3650
Jay Foad39c79802011-01-12 09:06:06 +00003651bool ASTContext::BlockRequiresCopying(QualType Ty) const {
Mike Stump94967902009-10-21 18:16:27 +00003652 if (Ty->isBlockPointerType())
3653 return true;
3654 if (isObjCNSObjectType(Ty))
3655 return true;
3656 if (Ty->isObjCObjectPointerType())
3657 return true;
Fariborz Jahaniana00076c2010-11-17 00:21:28 +00003658 if (getLangOptions().CPlusPlus) {
3659 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3660 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3661 return RD->hasConstCopyConstructor(*this);
3662
3663 }
3664 }
Mike Stump94967902009-10-21 18:16:27 +00003665 return false;
3666}
3667
Jay Foad39c79802011-01-12 09:06:06 +00003668QualType
3669ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const {
Mike Stump94967902009-10-21 18:16:27 +00003670 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003671 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003672 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003673 // unsigned int __flags;
3674 // unsigned int __size;
Eli Friedman04831922010-08-22 01:00:03 +00003675 // void *__copy_helper; // as needed
3676 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003677 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003678 // } *
3679
Mike Stump94967902009-10-21 18:16:27 +00003680 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3681
3682 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003683 llvm::SmallString<36> Name;
3684 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3685 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003686 RecordDecl *T;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003687 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003688 T->startDefinition();
3689 QualType Int32Ty = IntTy;
3690 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3691 QualType FieldTypes[] = {
3692 getPointerType(VoidPtrTy),
3693 getPointerType(getTagDeclType(T)),
3694 Int32Ty,
3695 Int32Ty,
3696 getPointerType(VoidPtrTy),
3697 getPointerType(VoidPtrTy),
3698 Ty
3699 };
3700
Daniel Dunbar56df9772010-08-17 22:39:59 +00003701 llvm::StringRef FieldNames[] = {
Mike Stump94967902009-10-21 18:16:27 +00003702 "__isa",
3703 "__forwarding",
3704 "__flags",
3705 "__size",
3706 "__copy_helper",
3707 "__destroy_helper",
3708 DeclName,
3709 };
3710
3711 for (size_t i = 0; i < 7; ++i) {
3712 if (!HasCopyAndDispose && i >=4 && i <= 5)
3713 continue;
3714 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003715 SourceLocation(),
Mike Stump94967902009-10-21 18:16:27 +00003716 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003717 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003718 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003719 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003720 T->addDecl(Field);
3721 }
3722
Douglas Gregord5058122010-02-11 01:19:42 +00003723 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003724
3725 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003726}
3727
Douglas Gregor512b0772009-04-23 22:29:11 +00003728void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003729 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003730 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3731 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3732}
3733
Anders Carlsson18acd442007-10-29 06:33:42 +00003734// This returns true if a type has been typedefed to BOOL:
3735// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003736static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003737 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003738 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3739 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003740
Anders Carlssond8499822007-10-29 05:01:08 +00003741 return false;
3742}
3743
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003744/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003745/// purpose.
Jay Foad39c79802011-01-12 09:06:06 +00003746CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
Ken Dyck40775002010-01-11 17:06:35 +00003747 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003748
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003749 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003750 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003751 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003752 // Treat arrays as pointers, since that's how they're passed in.
3753 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003754 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003755 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003756}
3757
3758static inline
3759std::string charUnitsToString(const CharUnits &CU) {
3760 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003761}
3762
John McCall351762c2011-02-07 10:33:21 +00003763/// getObjCEncodingForBlock - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003764/// declaration.
John McCall351762c2011-02-07 10:33:21 +00003765std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
3766 std::string S;
3767
David Chisnall950a9512009-11-17 19:33:30 +00003768 const BlockDecl *Decl = Expr->getBlockDecl();
3769 QualType BlockTy =
3770 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3771 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003772 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003773 // Compute size of all parameters.
3774 // Start with computing size of a pointer in number of bytes.
3775 // FIXME: There might(should) be a better way of doing this computation!
3776 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003777 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3778 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003779 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003780 E = Decl->param_end(); PI != E; ++PI) {
3781 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003782 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003783 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003784 ParmOffset += sz;
3785 }
3786 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003787 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003788 // Block pointer and offset.
3789 S += "@?0";
3790 ParmOffset = PtrSize;
3791
3792 // Argument types.
3793 ParmOffset = PtrSize;
3794 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3795 Decl->param_end(); PI != E; ++PI) {
3796 ParmVarDecl *PVDecl = *PI;
3797 QualType PType = PVDecl->getOriginalType();
3798 if (const ArrayType *AT =
3799 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3800 // Use array's original type only if it has known number of
3801 // elements.
3802 if (!isa<ConstantArrayType>(AT))
3803 PType = PVDecl->getType();
3804 } else if (PType->isFunctionType())
3805 PType = PVDecl->getType();
3806 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003807 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003808 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003809 }
John McCall351762c2011-02-07 10:33:21 +00003810
3811 return S;
David Chisnall950a9512009-11-17 19:33:30 +00003812}
3813
David Chisnall50e4eba2010-12-30 14:05:53 +00003814void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
3815 std::string& S) {
3816 // Encode result type.
3817 getObjCEncodingForType(Decl->getResultType(), S);
3818 CharUnits ParmOffset;
3819 // Compute size of all parameters.
3820 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3821 E = Decl->param_end(); PI != E; ++PI) {
3822 QualType PType = (*PI)->getType();
3823 CharUnits sz = getObjCEncodingTypeSize(PType);
3824 assert (sz.isPositive() &&
3825 "getObjCEncodingForMethodDecl - Incomplete param type");
3826 ParmOffset += sz;
3827 }
3828 S += charUnitsToString(ParmOffset);
3829 ParmOffset = CharUnits::Zero();
3830
3831 // Argument types.
3832 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3833 E = Decl->param_end(); PI != E; ++PI) {
3834 ParmVarDecl *PVDecl = *PI;
3835 QualType PType = PVDecl->getOriginalType();
3836 if (const ArrayType *AT =
3837 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3838 // Use array's original type only if it has known number of
3839 // elements.
3840 if (!isa<ConstantArrayType>(AT))
3841 PType = PVDecl->getType();
3842 } else if (PType->isFunctionType())
3843 PType = PVDecl->getType();
3844 getObjCEncodingForType(PType, S);
3845 S += charUnitsToString(ParmOffset);
3846 ParmOffset += getObjCEncodingTypeSize(PType);
3847 }
3848}
3849
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003850/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003851/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003852void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Jay Foad39c79802011-01-12 09:06:06 +00003853 std::string& S) const {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003854 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003855 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003856 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003857 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003858 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003859 // Compute size of all parameters.
3860 // Start with computing size of a pointer in number of bytes.
3861 // FIXME: There might(should) be a better way of doing this computation!
3862 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003863 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003864 // The first two arguments (self and _cmd) are pointers; account for
3865 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003866 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003867 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003868 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003869 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003870 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003871 assert (sz.isPositive() &&
3872 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003873 ParmOffset += sz;
3874 }
Ken Dyck40775002010-01-11 17:06:35 +00003875 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003876 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003877 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003878
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003879 // Argument types.
3880 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003881 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003882 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003883 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003884 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003885 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003886 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3887 // Use array's original type only if it has known number of
3888 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003889 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003890 PType = PVDecl->getType();
3891 } else if (PType->isFunctionType())
3892 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003893 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003894 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003895 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003896 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003897 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003898 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003899 }
3900}
3901
Daniel Dunbar4932b362008-08-28 04:38:10 +00003902/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003903/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003904/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3905/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003906/// Property attributes are stored as a comma-delimited C string. The simple
3907/// attributes readonly and bycopy are encoded as single characters. The
3908/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3909/// encoded as single characters, followed by an identifier. Property types
3910/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003911/// these attributes are defined by the following enumeration:
3912/// @code
3913/// enum PropertyAttributes {
3914/// kPropertyReadOnly = 'R', // property is read-only.
3915/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3916/// kPropertyByref = '&', // property is a reference to the value last assigned
3917/// kPropertyDynamic = 'D', // property is dynamic
3918/// kPropertyGetter = 'G', // followed by getter selector name
3919/// kPropertySetter = 'S', // followed by setter selector name
3920/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3921/// kPropertyType = 't' // followed by old-style type encoding.
3922/// kPropertyWeak = 'W' // 'weak' property
3923/// kPropertyStrong = 'P' // property GC'able
3924/// kPropertyNonAtomic = 'N' // property non-atomic
3925/// };
3926/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003927void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003928 const Decl *Container,
Jay Foad39c79802011-01-12 09:06:06 +00003929 std::string& S) const {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003930 // Collect information from the property implementation decl(s).
3931 bool Dynamic = false;
3932 ObjCPropertyImplDecl *SynthesizePID = 0;
3933
3934 // FIXME: Duplicated code due to poor abstraction.
3935 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003936 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003937 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3938 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003939 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003940 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003941 ObjCPropertyImplDecl *PID = *i;
3942 if (PID->getPropertyDecl() == PD) {
3943 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3944 Dynamic = true;
3945 } else {
3946 SynthesizePID = PID;
3947 }
3948 }
3949 }
3950 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003951 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003952 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003953 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003954 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003955 ObjCPropertyImplDecl *PID = *i;
3956 if (PID->getPropertyDecl() == PD) {
3957 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3958 Dynamic = true;
3959 } else {
3960 SynthesizePID = PID;
3961 }
3962 }
Mike Stump11289f42009-09-09 15:08:12 +00003963 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003964 }
3965 }
3966
3967 // FIXME: This is not very efficient.
3968 S = "T";
3969
3970 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003971 // GCC has some special rules regarding encoding of properties which
3972 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003973 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003974 true /* outermost type */,
3975 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003976
3977 if (PD->isReadOnly()) {
3978 S += ",R";
3979 } else {
3980 switch (PD->getSetterKind()) {
3981 case ObjCPropertyDecl::Assign: break;
3982 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003983 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003984 }
3985 }
3986
3987 // It really isn't clear at all what this means, since properties
3988 // are "dynamic by default".
3989 if (Dynamic)
3990 S += ",D";
3991
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003992 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3993 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003994
Daniel Dunbar4932b362008-08-28 04:38:10 +00003995 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3996 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003997 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003998 }
3999
4000 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4001 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00004002 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00004003 }
4004
4005 if (SynthesizePID) {
4006 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4007 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00004008 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00004009 }
4010
4011 // FIXME: OBJCGC: weak & strong
4012}
4013
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004014/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00004015/// Another legacy compatibility encoding: 32-bit longs are encoded as
4016/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004017/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4018///
4019void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00004020 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00004021 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Jay Foad39c79802011-01-12 09:06:06 +00004022 if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004023 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00004024 else
Jay Foad39c79802011-01-12 09:06:06 +00004025 if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004026 PointeeTy = IntTy;
4027 }
4028 }
4029}
4030
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004031void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Jay Foad39c79802011-01-12 09:06:06 +00004032 const FieldDecl *Field) const {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004033 // We follow the behavior of gcc, expanding structures which are
4034 // directly pointed to, and expanding embedded structures. Note that
4035 // these rules are sufficient to prevent recursive encoding of the
4036 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00004037 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00004038 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004039}
4040
David Chisnallb190a2c2010-06-04 01:10:52 +00004041static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
4042 switch (T->getAs<BuiltinType>()->getKind()) {
4043 default: assert(0 && "Unhandled builtin type kind");
4044 case BuiltinType::Void: return 'v';
4045 case BuiltinType::Bool: return 'B';
4046 case BuiltinType::Char_U:
4047 case BuiltinType::UChar: return 'C';
4048 case BuiltinType::UShort: return 'S';
4049 case BuiltinType::UInt: return 'I';
4050 case BuiltinType::ULong:
Jay Foad39c79802011-01-12 09:06:06 +00004051 return C->getIntWidth(T) == 32 ? 'L' : 'Q';
David Chisnallb190a2c2010-06-04 01:10:52 +00004052 case BuiltinType::UInt128: return 'T';
4053 case BuiltinType::ULongLong: return 'Q';
4054 case BuiltinType::Char_S:
4055 case BuiltinType::SChar: return 'c';
4056 case BuiltinType::Short: return 's';
Chris Lattnerad3467e2010-12-25 23:25:43 +00004057 case BuiltinType::WChar_S:
4058 case BuiltinType::WChar_U:
David Chisnallb190a2c2010-06-04 01:10:52 +00004059 case BuiltinType::Int: return 'i';
4060 case BuiltinType::Long:
Jay Foad39c79802011-01-12 09:06:06 +00004061 return C->getIntWidth(T) == 32 ? 'l' : 'q';
David Chisnallb190a2c2010-06-04 01:10:52 +00004062 case BuiltinType::LongLong: return 'q';
4063 case BuiltinType::Int128: return 't';
4064 case BuiltinType::Float: return 'f';
4065 case BuiltinType::Double: return 'd';
Daniel Dunbar7cba5a72010-10-11 21:13:48 +00004066 case BuiltinType::LongDouble: return 'D';
David Chisnallb190a2c2010-06-04 01:10:52 +00004067 }
4068}
4069
Jay Foad39c79802011-01-12 09:06:06 +00004070static void EncodeBitField(const ASTContext *Ctx, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00004071 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004072 const Expr *E = FD->getBitWidth();
4073 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004074 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00004075 // The NeXT runtime encodes bit fields as b followed by the number of bits.
4076 // The GNU runtime requires more information; bitfields are encoded as b,
4077 // then the offset (in bits) of the first element, then the type of the
4078 // bitfield, then the size in bits. For example, in this structure:
4079 //
4080 // struct
4081 // {
4082 // int integer;
4083 // int flags:2;
4084 // };
4085 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
4086 // runtime, but b32i2 for the GNU runtime. The reason for this extra
4087 // information is not especially sensible, but we're stuck with it for
4088 // compatibility with GCC, although providing it breaks anything that
4089 // actually uses runtime introspection and wants to work on both runtimes...
4090 if (!Ctx->getLangOptions().NeXTRuntime) {
4091 const RecordDecl *RD = FD->getParent();
4092 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
4093 // FIXME: This same linear search is also used in ExprConstant - it might
4094 // be better if the FieldDecl stored its offset. We'd be increasing the
4095 // size of the object slightly, but saving some time every time it is used.
4096 unsigned i = 0;
4097 for (RecordDecl::field_iterator Field = RD->field_begin(),
4098 FieldEnd = RD->field_end();
4099 Field != FieldEnd; (void)++Field, ++i) {
4100 if (*Field == FD)
4101 break;
4102 }
4103 S += llvm::utostr(RL.getFieldOffset(i));
David Chisnall6f0a7d22010-12-26 20:12:30 +00004104 if (T->isEnumeralType())
4105 S += 'i';
4106 else
Jay Foad39c79802011-01-12 09:06:06 +00004107 S += ObjCEncodingForPrimitiveKind(Ctx, T);
David Chisnallb190a2c2010-06-04 01:10:52 +00004108 }
4109 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004110 S += llvm::utostr(N);
4111}
4112
Daniel Dunbar07d07852009-10-18 21:17:35 +00004113// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004114void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
4115 bool ExpandPointedToStructures,
4116 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00004117 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00004118 bool OutermostType,
Jay Foad39c79802011-01-12 09:06:06 +00004119 bool EncodingProperty) const {
David Chisnallb190a2c2010-06-04 01:10:52 +00004120 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004121 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00004122 return EncodeBitField(this, S, T, FD);
4123 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004124 return;
4125 }
Mike Stump11289f42009-09-09 15:08:12 +00004126
John McCall9dd450b2009-09-21 23:43:11 +00004127 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00004128 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00004129 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00004130 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004131 return;
4132 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00004133
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004134 // encoding for pointer or r3eference types.
4135 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004136 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00004137 if (PT->isObjCSelType()) {
4138 S += ':';
4139 return;
4140 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004141 PointeeTy = PT->getPointeeType();
4142 }
4143 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4144 PointeeTy = RT->getPointeeType();
4145 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004146 bool isReadOnly = false;
4147 // For historical/compatibility reasons, the read-only qualifier of the
4148 // pointee gets emitted _before_ the '^'. The read-only qualifier of
4149 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00004150 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00004151 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004152 if (OutermostType && T.isConstQualified()) {
4153 isReadOnly = true;
4154 S += 'r';
4155 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00004156 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004157 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004158 while (P->getAs<PointerType>())
4159 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004160 if (P.isConstQualified()) {
4161 isReadOnly = true;
4162 S += 'r';
4163 }
4164 }
4165 if (isReadOnly) {
4166 // Another legacy compatibility encoding. Some ObjC qualifier and type
4167 // combinations need to be rearranged.
4168 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00004169 if (llvm::StringRef(S).endswith("nr"))
4170 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004171 }
Mike Stump11289f42009-09-09 15:08:12 +00004172
Anders Carlssond8499822007-10-29 05:01:08 +00004173 if (PointeeTy->isCharType()) {
4174 // char pointer types should be encoded as '*' unless it is a
4175 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00004176 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00004177 S += '*';
4178 return;
4179 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004180 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00004181 // GCC binary compat: Need to convert "struct objc_class *" to "#".
4182 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
4183 S += '#';
4184 return;
4185 }
4186 // GCC binary compat: Need to convert "struct objc_object *" to "@".
4187 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
4188 S += '@';
4189 return;
4190 }
4191 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00004192 }
Anders Carlssond8499822007-10-29 05:01:08 +00004193 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004194 getLegacyIntegralTypeEncoding(PointeeTy);
4195
Mike Stump11289f42009-09-09 15:08:12 +00004196 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004197 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00004198 return;
4199 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00004200
Chris Lattnere7cabb92009-07-13 00:10:46 +00004201 if (const ArrayType *AT =
4202 // Ignore type qualifiers etc.
4203 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00004204 if (isa<IncompleteArrayType>(AT)) {
4205 // Incomplete arrays are encoded as a pointer to the array element.
4206 S += '^';
4207
Mike Stump11289f42009-09-09 15:08:12 +00004208 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00004209 false, ExpandStructures, FD);
4210 } else {
4211 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00004212
Anders Carlssond05f44b2009-02-22 01:38:57 +00004213 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
4214 S += llvm::utostr(CAT->getSize().getZExtValue());
4215 else {
4216 //Variable length arrays are encoded as a regular array with 0 elements.
4217 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
4218 S += '0';
4219 }
Mike Stump11289f42009-09-09 15:08:12 +00004220
4221 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00004222 false, ExpandStructures, FD);
4223 S += ']';
4224 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004225 return;
4226 }
Mike Stump11289f42009-09-09 15:08:12 +00004227
John McCall9dd450b2009-09-21 23:43:11 +00004228 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00004229 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004230 return;
4231 }
Mike Stump11289f42009-09-09 15:08:12 +00004232
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004233 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00004234 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004235 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00004236 // Anonymous structures print as '?'
4237 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4238 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00004239 if (ClassTemplateSpecializationDecl *Spec
4240 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
4241 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
4242 std::string TemplateArgsStr
4243 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004244 TemplateArgs.data(),
4245 TemplateArgs.size(),
Fariborz Jahanianc5158202010-05-07 00:28:49 +00004246 (*this).PrintingPolicy);
4247
4248 S += TemplateArgsStr;
4249 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00004250 } else {
4251 S += '?';
4252 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00004253 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004254 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004255 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
4256 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00004257 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004258 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004259 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00004260 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004261 S += '"';
4262 }
Mike Stump11289f42009-09-09 15:08:12 +00004263
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004264 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004265 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00004266 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004267 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004268 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00004269 QualType qt = Field->getType();
4270 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00004271 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004272 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004273 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00004274 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00004275 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00004276 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004277 return;
4278 }
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004279
Chris Lattnere7cabb92009-07-13 00:10:46 +00004280 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004281 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00004282 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004283 else
4284 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004285 return;
4286 }
Mike Stump11289f42009-09-09 15:08:12 +00004287
Chris Lattnere7cabb92009-07-13 00:10:46 +00004288 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00004289 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00004290 return;
4291 }
Mike Stump11289f42009-09-09 15:08:12 +00004292
John McCall8b07ec22010-05-15 11:32:37 +00004293 // Ignore protocol qualifiers when mangling at this level.
4294 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4295 T = OT->getBaseType();
4296
John McCall8ccfcb52009-09-24 19:53:00 +00004297 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004298 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00004299 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004300 S += '{';
4301 const IdentifierInfo *II = OI->getIdentifier();
4302 S += II->getName();
4303 S += '=';
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004304 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
4305 DeepCollectObjCIvars(OI, true, Ivars);
4306 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4307 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4308 if (Field->isBitField())
4309 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004310 else
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004311 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004312 }
4313 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004314 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004315 }
Mike Stump11289f42009-09-09 15:08:12 +00004316
John McCall9dd450b2009-09-21 23:43:11 +00004317 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004318 if (OPT->isObjCIdType()) {
4319 S += '@';
4320 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004321 }
Mike Stump11289f42009-09-09 15:08:12 +00004322
Steve Narofff0c86112009-10-28 22:03:49 +00004323 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4324 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4325 // Since this is a binary compatibility issue, need to consult with runtime
4326 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004327 S += '#';
4328 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004329 }
Mike Stump11289f42009-09-09 15:08:12 +00004330
Chris Lattnere7cabb92009-07-13 00:10:46 +00004331 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00004332 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00004333 ExpandPointedToStructures,
4334 ExpandStructures, FD);
4335 if (FD || EncodingProperty) {
4336 // Note that we do extended encoding of protocol qualifer list
4337 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004338 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00004339 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4340 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004341 S += '<';
4342 S += (*I)->getNameAsString();
4343 S += '>';
4344 }
4345 S += '"';
4346 }
4347 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004348 }
Mike Stump11289f42009-09-09 15:08:12 +00004349
Chris Lattnere7cabb92009-07-13 00:10:46 +00004350 QualType PointeeTy = OPT->getPointeeType();
4351 if (!EncodingProperty &&
4352 isa<TypedefType>(PointeeTy.getTypePtr())) {
4353 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00004354 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00004355 // {...};
4356 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00004357 getObjCEncodingForTypeImpl(PointeeTy, S,
4358 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00004359 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00004360 return;
4361 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004362
4363 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00004364 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004365 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00004366 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00004367 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4368 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004369 S += '<';
4370 S += (*I)->getNameAsString();
4371 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00004372 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004373 S += '"';
4374 }
4375 return;
4376 }
Mike Stump11289f42009-09-09 15:08:12 +00004377
John McCalla9e6e8d2010-05-17 23:56:34 +00004378 // gcc just blithely ignores member pointers.
4379 // TODO: maybe there should be a mangling for these
4380 if (T->getAs<MemberPointerType>())
4381 return;
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004382
4383 if (T->isVectorType()) {
4384 // This matches gcc's encoding, even though technically it is
4385 // insufficient.
4386 // FIXME. We should do a better job than gcc.
4387 return;
4388 }
4389
Chris Lattnere7cabb92009-07-13 00:10:46 +00004390 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00004391}
4392
Mike Stump11289f42009-09-09 15:08:12 +00004393void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00004394 std::string& S) const {
4395 if (QT & Decl::OBJC_TQ_In)
4396 S += 'n';
4397 if (QT & Decl::OBJC_TQ_Inout)
4398 S += 'N';
4399 if (QT & Decl::OBJC_TQ_Out)
4400 S += 'o';
4401 if (QT & Decl::OBJC_TQ_Bycopy)
4402 S += 'O';
4403 if (QT & Decl::OBJC_TQ_Byref)
4404 S += 'R';
4405 if (QT & Decl::OBJC_TQ_Oneway)
4406 S += 'V';
4407}
4408
Chris Lattnere7cabb92009-07-13 00:10:46 +00004409void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00004410 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004411
Anders Carlsson87c149b2007-10-11 01:00:40 +00004412 BuiltinVaListType = T;
4413}
4414
Chris Lattnere7cabb92009-07-13 00:10:46 +00004415void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004416 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00004417}
4418
Chris Lattnere7cabb92009-07-13 00:10:46 +00004419void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00004420 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00004421}
4422
Chris Lattnere7cabb92009-07-13 00:10:46 +00004423void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004424 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004425}
4426
Chris Lattnere7cabb92009-07-13 00:10:46 +00004427void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004428 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00004429}
4430
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004431void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00004432 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00004433 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004434
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004435 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00004436}
4437
John McCalld28ae272009-12-02 08:04:21 +00004438/// \brief Retrieve the template name that corresponds to a non-empty
4439/// lookup.
Jay Foad39c79802011-01-12 09:06:06 +00004440TemplateName
4441ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4442 UnresolvedSetIterator End) const {
John McCalld28ae272009-12-02 08:04:21 +00004443 unsigned size = End - Begin;
4444 assert(size > 1 && "set is not overloaded!");
4445
4446 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4447 size * sizeof(FunctionTemplateDecl*));
4448 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4449
4450 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00004451 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00004452 NamedDecl *D = *I;
4453 assert(isa<FunctionTemplateDecl>(D) ||
4454 (isa<UsingShadowDecl>(D) &&
4455 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4456 *Storage++ = D;
4457 }
4458
4459 return TemplateName(OT);
4460}
4461
Douglas Gregordc572a32009-03-30 22:58:21 +00004462/// \brief Retrieve the template name that represents a qualified
4463/// template name such as \c std::vector.
Jay Foad39c79802011-01-12 09:06:06 +00004464TemplateName
4465ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
4466 bool TemplateKeyword,
4467 TemplateDecl *Template) const {
Douglas Gregore29139c2011-03-03 17:04:51 +00004468 assert(NNS && "Missing nested-name-specifier in qualified template name");
4469
Douglas Gregorc42075a2010-02-04 18:10:26 +00004470 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00004471 llvm::FoldingSetNodeID ID;
4472 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4473
4474 void *InsertPos = 0;
4475 QualifiedTemplateName *QTN =
4476 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4477 if (!QTN) {
4478 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4479 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4480 }
4481
4482 return TemplateName(QTN);
4483}
4484
4485/// \brief Retrieve the template name that represents a dependent
4486/// template name such as \c MetaFun::template apply.
Jay Foad39c79802011-01-12 09:06:06 +00004487TemplateName
4488ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4489 const IdentifierInfo *Name) const {
Mike Stump11289f42009-09-09 15:08:12 +00004490 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00004491 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00004492
4493 llvm::FoldingSetNodeID ID;
4494 DependentTemplateName::Profile(ID, NNS, Name);
4495
4496 void *InsertPos = 0;
4497 DependentTemplateName *QTN =
4498 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4499
4500 if (QTN)
4501 return TemplateName(QTN);
4502
4503 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4504 if (CanonNNS == NNS) {
4505 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4506 } else {
4507 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4508 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004509 DependentTemplateName *CheckQTN =
4510 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4511 assert(!CheckQTN && "Dependent type name canonicalization broken");
4512 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004513 }
4514
4515 DependentTemplateNames.InsertNode(QTN, InsertPos);
4516 return TemplateName(QTN);
4517}
4518
Douglas Gregor71395fa2009-11-04 00:56:37 +00004519/// \brief Retrieve the template name that represents a dependent
4520/// template name such as \c MetaFun::template operator+.
4521TemplateName
4522ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Jay Foad39c79802011-01-12 09:06:06 +00004523 OverloadedOperatorKind Operator) const {
Douglas Gregor71395fa2009-11-04 00:56:37 +00004524 assert((!NNS || NNS->isDependent()) &&
4525 "Nested name specifier must be dependent");
4526
4527 llvm::FoldingSetNodeID ID;
4528 DependentTemplateName::Profile(ID, NNS, Operator);
4529
4530 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004531 DependentTemplateName *QTN
4532 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004533
4534 if (QTN)
4535 return TemplateName(QTN);
4536
4537 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4538 if (CanonNNS == NNS) {
4539 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4540 } else {
4541 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4542 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004543
4544 DependentTemplateName *CheckQTN
4545 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4546 assert(!CheckQTN && "Dependent template name canonicalization broken");
4547 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004548 }
4549
4550 DependentTemplateNames.InsertNode(QTN, InsertPos);
4551 return TemplateName(QTN);
4552}
4553
Douglas Gregor5590be02011-01-15 06:45:20 +00004554TemplateName
4555ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
4556 const TemplateArgument &ArgPack) const {
4557 ASTContext &Self = const_cast<ASTContext &>(*this);
4558 llvm::FoldingSetNodeID ID;
4559 SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
4560
4561 void *InsertPos = 0;
4562 SubstTemplateTemplateParmPackStorage *Subst
4563 = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
4564
4565 if (!Subst) {
4566 Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param,
4567 ArgPack.pack_size(),
4568 ArgPack.pack_begin());
4569 SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
4570 }
4571
4572 return TemplateName(Subst);
4573}
4574
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004575/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004576/// TargetInfo, produce the corresponding type. The unsigned @p Type
4577/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004578CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004579 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004580 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004581 case TargetInfo::SignedShort: return ShortTy;
4582 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4583 case TargetInfo::SignedInt: return IntTy;
4584 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4585 case TargetInfo::SignedLong: return LongTy;
4586 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4587 case TargetInfo::SignedLongLong: return LongLongTy;
4588 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4589 }
4590
4591 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004592 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004593}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004594
4595//===----------------------------------------------------------------------===//
4596// Type Predicates.
4597//===----------------------------------------------------------------------===//
4598
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004599/// isObjCNSObjectType - Return true if this is an NSObject object using
4600/// NSObject attribute on a c-style pointer type.
4601/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004602/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004603///
4604bool ASTContext::isObjCNSObjectType(QualType Ty) const {
John McCall424cec92011-01-19 06:33:43 +00004605 if (const TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
Richard Smithdda56e42011-04-15 14:24:37 +00004606 if (TypedefNameDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004607 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004608 return true;
4609 }
Mike Stump11289f42009-09-09 15:08:12 +00004610 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004611}
4612
Fariborz Jahanian96207692009-02-18 21:49:28 +00004613/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4614/// garbage collection attribute.
4615///
John McCall553d45a2011-01-12 00:34:59 +00004616Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
4617 if (getLangOptions().getGCMode() == LangOptions::NonGC)
4618 return Qualifiers::GCNone;
4619
4620 assert(getLangOptions().ObjC1);
4621 Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
4622
4623 // Default behaviour under objective-C's gc is for ObjC pointers
4624 // (or pointers to them) be treated as though they were declared
4625 // as __strong.
4626 if (GCAttrs == Qualifiers::GCNone) {
4627 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
4628 return Qualifiers::Strong;
4629 else if (Ty->isPointerType())
4630 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
4631 } else {
4632 // It's not valid to set GC attributes on anything that isn't a
4633 // pointer.
4634#ifndef NDEBUG
4635 QualType CT = Ty->getCanonicalTypeInternal();
4636 while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
4637 CT = AT->getElementType();
4638 assert(CT->isAnyPointerType() || CT->isBlockPointerType());
4639#endif
Fariborz Jahanian96207692009-02-18 21:49:28 +00004640 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004641 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004642}
4643
Chris Lattner49af6a42008-04-07 06:51:04 +00004644//===----------------------------------------------------------------------===//
4645// Type Compatibility Testing
4646//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004647
Mike Stump11289f42009-09-09 15:08:12 +00004648/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004649/// compatible.
4650static bool areCompatVectorTypes(const VectorType *LHS,
4651 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004652 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004653 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004654 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004655}
4656
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004657bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4658 QualType SecondVec) {
4659 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4660 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4661
4662 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4663 return true;
4664
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004665 // Treat Neon vector types and most AltiVec vector types as if they are the
4666 // equivalent GCC vector types.
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004667 const VectorType *First = FirstVec->getAs<VectorType>();
4668 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004669 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004670 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004671 First->getVectorKind() != VectorType::AltiVecPixel &&
4672 First->getVectorKind() != VectorType::AltiVecBool &&
4673 Second->getVectorKind() != VectorType::AltiVecPixel &&
4674 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004675 return true;
4676
4677 return false;
4678}
4679
Steve Naroff8e6aee52009-07-23 01:01:38 +00004680//===----------------------------------------------------------------------===//
4681// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4682//===----------------------------------------------------------------------===//
4683
4684/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4685/// inheritance hierarchy of 'rProto'.
Jay Foad39c79802011-01-12 09:06:06 +00004686bool
4687ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4688 ObjCProtocolDecl *rProto) const {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004689 if (lProto == rProto)
4690 return true;
4691 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4692 E = rProto->protocol_end(); PI != E; ++PI)
4693 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4694 return true;
4695 return false;
4696}
4697
Steve Naroff8e6aee52009-07-23 01:01:38 +00004698/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4699/// return true if lhs's protocols conform to rhs's protocol; false
4700/// otherwise.
4701bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4702 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4703 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4704 return false;
4705}
4706
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004707/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4708/// Class<p1, ...>.
4709bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4710 QualType rhs) {
4711 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4712 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4713 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4714
4715 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4716 E = lhsQID->qual_end(); I != E; ++I) {
4717 bool match = false;
4718 ObjCProtocolDecl *lhsProto = *I;
4719 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4720 E = rhsOPT->qual_end(); J != E; ++J) {
4721 ObjCProtocolDecl *rhsProto = *J;
4722 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4723 match = true;
4724 break;
4725 }
4726 }
4727 if (!match)
4728 return false;
4729 }
4730 return true;
4731}
4732
Steve Naroff8e6aee52009-07-23 01:01:38 +00004733/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4734/// ObjCQualifiedIDType.
4735bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4736 bool compare) {
4737 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004738 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004739 lhs->isObjCIdType() || lhs->isObjCClassType())
4740 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004741 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004742 rhs->isObjCIdType() || rhs->isObjCClassType())
4743 return true;
4744
4745 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004746 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004747
Steve Naroff8e6aee52009-07-23 01:01:38 +00004748 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004749
Steve Naroff8e6aee52009-07-23 01:01:38 +00004750 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004751 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004752 // make sure we check the class hierarchy.
4753 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4754 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4755 E = lhsQID->qual_end(); I != E; ++I) {
4756 // when comparing an id<P> on lhs with a static type on rhs,
4757 // see if static class implements all of id's protocols, directly or
4758 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004759 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004760 return false;
4761 }
4762 }
4763 // If there are no qualifiers and no interface, we have an 'id'.
4764 return true;
4765 }
Mike Stump11289f42009-09-09 15:08:12 +00004766 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004767 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4768 E = lhsQID->qual_end(); I != E; ++I) {
4769 ObjCProtocolDecl *lhsProto = *I;
4770 bool match = false;
4771
4772 // when comparing an id<P> on lhs with a static type on rhs,
4773 // see if static class implements all of id's protocols, directly or
4774 // through its super class and categories.
4775 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4776 E = rhsOPT->qual_end(); J != E; ++J) {
4777 ObjCProtocolDecl *rhsProto = *J;
4778 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4779 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4780 match = true;
4781 break;
4782 }
4783 }
Mike Stump11289f42009-09-09 15:08:12 +00004784 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004785 // make sure we check the class hierarchy.
4786 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4787 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4788 E = lhsQID->qual_end(); I != E; ++I) {
4789 // when comparing an id<P> on lhs with a static type on rhs,
4790 // see if static class implements all of id's protocols, directly or
4791 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004792 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004793 match = true;
4794 break;
4795 }
4796 }
4797 }
4798 if (!match)
4799 return false;
4800 }
Mike Stump11289f42009-09-09 15:08:12 +00004801
Steve Naroff8e6aee52009-07-23 01:01:38 +00004802 return true;
4803 }
Mike Stump11289f42009-09-09 15:08:12 +00004804
Steve Naroff8e6aee52009-07-23 01:01:38 +00004805 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4806 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4807
Mike Stump11289f42009-09-09 15:08:12 +00004808 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004809 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004810 // If both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004811 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4812 E = lhsOPT->qual_end(); I != E; ++I) {
4813 ObjCProtocolDecl *lhsProto = *I;
4814 bool match = false;
4815
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004816 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff8e6aee52009-07-23 01:01:38 +00004817 // see if static class implements all of id's protocols, directly or
4818 // through its super class and categories.
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004819 // First, lhs protocols in the qualifier list must be found, direct
4820 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004821 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4822 E = rhsQID->qual_end(); J != E; ++J) {
4823 ObjCProtocolDecl *rhsProto = *J;
4824 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4825 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4826 match = true;
4827 break;
4828 }
4829 }
4830 if (!match)
4831 return false;
4832 }
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004833
4834 // Static class's protocols, or its super class or category protocols
4835 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4836 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4837 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4838 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4839 // This is rather dubious but matches gcc's behavior. If lhs has
4840 // no type qualifier and its class has no static protocol(s)
4841 // assume that it is mismatch.
4842 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4843 return false;
4844 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4845 LHSInheritedProtocols.begin(),
4846 E = LHSInheritedProtocols.end(); I != E; ++I) {
4847 bool match = false;
4848 ObjCProtocolDecl *lhsProto = (*I);
4849 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4850 E = rhsQID->qual_end(); J != E; ++J) {
4851 ObjCProtocolDecl *rhsProto = *J;
4852 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4853 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4854 match = true;
4855 break;
4856 }
4857 }
4858 if (!match)
4859 return false;
4860 }
4861 }
Steve Naroff8e6aee52009-07-23 01:01:38 +00004862 return true;
4863 }
4864 return false;
4865}
4866
Eli Friedman47f77112008-08-22 00:56:42 +00004867/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004868/// compatible for assignment from RHS to LHS. This handles validation of any
4869/// protocol qualifiers on the LHS or RHS.
4870///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004871bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4872 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004873 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4874 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4875
Steve Naroff1329fa02009-07-15 18:40:39 +00004876 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004877 if (LHS->isObjCUnqualifiedIdOrClass() ||
4878 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004879 return true;
4880
John McCall8b07ec22010-05-15 11:32:37 +00004881 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004882 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4883 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004884 false);
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004885
4886 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4887 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4888 QualType(RHSOPT,0));
4889
John McCall8b07ec22010-05-15 11:32:37 +00004890 // If we have 2 user-defined types, fall into that path.
4891 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004892 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004893
Steve Naroff8e6aee52009-07-23 01:01:38 +00004894 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004895}
4896
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004897/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
Chris Lattner57540c52011-04-15 05:22:18 +00004898/// for providing type-safety for objective-c pointers used to pass/return
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004899/// arguments in block literals. When passed as arguments, passing 'A*' where
4900/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4901/// not OK. For the return type, the opposite is not OK.
4902bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4903 const ObjCObjectPointerType *LHSOPT,
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004904 const ObjCObjectPointerType *RHSOPT,
4905 bool BlockReturnType) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004906 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004907 return true;
4908
4909 if (LHSOPT->isObjCBuiltinType()) {
4910 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4911 }
4912
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004913 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004914 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4915 QualType(RHSOPT,0),
4916 false);
4917
4918 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4919 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4920 if (LHS && RHS) { // We have 2 user-defined types.
4921 if (LHS != RHS) {
4922 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004923 return BlockReturnType;
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004924 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
Fariborz Jahanian90186f82011-03-14 16:07:00 +00004925 return !BlockReturnType;
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004926 }
4927 else
4928 return true;
4929 }
4930 return false;
4931}
4932
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004933/// getIntersectionOfProtocols - This routine finds the intersection of set
4934/// of protocols inherited from two distinct objective-c pointer objects.
4935/// It is used to build composite qualifier list of the composite type of
4936/// the conditional expression involving two objective-c pointer objects.
4937static
4938void getIntersectionOfProtocols(ASTContext &Context,
4939 const ObjCObjectPointerType *LHSOPT,
4940 const ObjCObjectPointerType *RHSOPT,
4941 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4942
John McCall8b07ec22010-05-15 11:32:37 +00004943 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4944 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4945 assert(LHS->getInterface() && "LHS must have an interface base");
4946 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004947
4948 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4949 unsigned LHSNumProtocols = LHS->getNumProtocols();
4950 if (LHSNumProtocols > 0)
4951 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4952 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004953 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004954 Context.CollectInheritedProtocols(LHS->getInterface(),
4955 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004956 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4957 LHSInheritedProtocols.end());
4958 }
4959
4960 unsigned RHSNumProtocols = RHS->getNumProtocols();
4961 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004962 ObjCProtocolDecl **RHSProtocols =
4963 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004964 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4965 if (InheritedProtocolSet.count(RHSProtocols[i]))
4966 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4967 }
4968 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004969 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004970 Context.CollectInheritedProtocols(RHS->getInterface(),
4971 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004972 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4973 RHSInheritedProtocols.begin(),
4974 E = RHSInheritedProtocols.end(); I != E; ++I)
4975 if (InheritedProtocolSet.count((*I)))
4976 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004977 }
4978}
4979
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004980/// areCommonBaseCompatible - Returns common base class of the two classes if
4981/// one found. Note that this is O'2 algorithm. But it will be called as the
4982/// last type comparison in a ?-exp of ObjC pointer types before a
4983/// warning is issued. So, its invokation is extremely rare.
4984QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004985 const ObjCObjectPointerType *Lptr,
4986 const ObjCObjectPointerType *Rptr) {
4987 const ObjCObjectType *LHS = Lptr->getObjectType();
4988 const ObjCObjectType *RHS = Rptr->getObjectType();
4989 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4990 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
Fariborz Jahanianb1071432011-04-18 21:16:59 +00004991 if (!LDecl || !RDecl || (LDecl == RDecl))
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004992 return QualType();
4993
Fariborz Jahanianb1071432011-04-18 21:16:59 +00004994 do {
John McCall8b07ec22010-05-15 11:32:37 +00004995 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004996 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004997 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4998 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4999
5000 QualType Result = QualType(LHS, 0);
5001 if (!Protocols.empty())
5002 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
5003 Result = getObjCObjectPointerType(Result);
5004 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00005005 }
Fariborz Jahanianb1071432011-04-18 21:16:59 +00005006 } while ((LDecl = LDecl->getSuperClass()));
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00005007
5008 return QualType();
5009}
5010
John McCall8b07ec22010-05-15 11:32:37 +00005011bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
5012 const ObjCObjectType *RHS) {
5013 assert(LHS->getInterface() && "LHS is not an interface type");
5014 assert(RHS->getInterface() && "RHS is not an interface type");
5015
Chris Lattner49af6a42008-04-07 06:51:04 +00005016 // Verify that the base decls are compatible: the RHS must be a subclass of
5017 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00005018 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00005019 return false;
Mike Stump11289f42009-09-09 15:08:12 +00005020
Chris Lattner49af6a42008-04-07 06:51:04 +00005021 // RHS must have a superset of the protocols in the LHS. If the LHS is not
5022 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00005023 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00005024 return true;
Mike Stump11289f42009-09-09 15:08:12 +00005025
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005026 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't,
5027 // more detailed analysis is required.
5028 if (RHS->getNumProtocols() == 0) {
5029 // OK, if LHS is a superclass of RHS *and*
5030 // this superclass is assignment compatible with LHS.
5031 // false otherwise.
Fariborz Jahanian240400b2011-04-12 16:34:14 +00005032 bool IsSuperClass =
5033 LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5034 if (IsSuperClass) {
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005035 // OK if conversion of LHS to SuperClass results in narrowing of types
5036 // ; i.e., SuperClass may implement at least one of the protocols
5037 // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5038 // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5039 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
Fariborz Jahanian240400b2011-04-12 16:34:14 +00005040 CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
Fariborz Jahanian4806ff82011-04-08 18:25:29 +00005041 // If super class has no protocols, it is not a match.
5042 if (SuperClassInheritedProtocols.empty())
5043 return false;
5044
5045 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5046 LHSPE = LHS->qual_end();
5047 LHSPI != LHSPE; LHSPI++) {
5048 bool SuperImplementsProtocol = false;
5049 ObjCProtocolDecl *LHSProto = (*LHSPI);
5050
5051 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5052 SuperClassInheritedProtocols.begin(),
5053 E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5054 ObjCProtocolDecl *SuperClassProto = (*I);
5055 if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5056 SuperImplementsProtocol = true;
5057 break;
5058 }
5059 }
5060 if (!SuperImplementsProtocol)
5061 return false;
5062 }
5063 return true;
5064 }
5065 return false;
5066 }
Mike Stump11289f42009-09-09 15:08:12 +00005067
John McCall8b07ec22010-05-15 11:32:37 +00005068 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5069 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00005070 LHSPI != LHSPE; LHSPI++) {
5071 bool RHSImplementsProtocol = false;
5072
5073 // If the RHS doesn't implement the protocol on the left, the types
5074 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00005075 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5076 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00005077 RHSPI != RHSPE; RHSPI++) {
5078 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00005079 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00005080 break;
5081 }
Steve Naroff114aecb2009-03-01 16:12:44 +00005082 }
5083 // FIXME: For better diagnostics, consider passing back the protocol name.
5084 if (!RHSImplementsProtocol)
5085 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00005086 }
Steve Naroff114aecb2009-03-01 16:12:44 +00005087 // The RHS implements all protocols listed on the LHS.
5088 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00005089}
5090
Steve Naroffb7605152009-02-12 17:52:19 +00005091bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5092 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00005093 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5094 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00005095
Steve Naroff7cae42b2009-07-10 23:34:53 +00005096 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00005097 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00005098
5099 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
5100 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00005101}
5102
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00005103bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5104 return canAssignObjCInterfaces(
5105 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5106 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5107}
5108
Mike Stump11289f42009-09-09 15:08:12 +00005109/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00005110/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00005111/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00005112/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005113bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5114 bool CompareUnqualified) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00005115 if (getLangOptions().CPlusPlus)
5116 return hasSameType(LHS, RHS);
5117
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005118 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman47f77112008-08-22 00:56:42 +00005119}
5120
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005121bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5122 return !mergeTypes(LHS, RHS, true).isNull();
5123}
5124
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005125/// mergeTransparentUnionType - if T is a transparent union type and a member
5126/// of T is compatible with SubType, return the merged type, else return
5127/// QualType()
5128QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
5129 bool OfBlockPointer,
5130 bool Unqualified) {
5131 if (const RecordType *UT = T->getAsUnionType()) {
5132 RecordDecl *UD = UT->getDecl();
5133 if (UD->hasAttr<TransparentUnionAttr>()) {
5134 for (RecordDecl::field_iterator it = UD->field_begin(),
5135 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbourne19b961d2010-12-02 21:00:06 +00005136 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005137 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
5138 if (!MT.isNull())
5139 return MT;
5140 }
5141 }
5142 }
5143
5144 return QualType();
5145}
5146
5147/// mergeFunctionArgumentTypes - merge two types which appear as function
5148/// argument types
5149QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
5150 bool OfBlockPointer,
5151 bool Unqualified) {
5152 // GNU extension: two types are compatible if they appear as a function
5153 // argument, one of the types is a transparent union type and the other
5154 // type is compatible with a union member
5155 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
5156 Unqualified);
5157 if (!lmerge.isNull())
5158 return lmerge;
5159
5160 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
5161 Unqualified);
5162 if (!rmerge.isNull())
5163 return rmerge;
5164
5165 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
5166}
5167
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005168QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005169 bool OfBlockPointer,
5170 bool Unqualified) {
John McCall9dd450b2009-09-21 23:43:11 +00005171 const FunctionType *lbase = lhs->getAs<FunctionType>();
5172 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005173 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
5174 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00005175 bool allLTypes = true;
5176 bool allRTypes = true;
5177
5178 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005179 QualType retType;
Fariborz Jahanian17825972011-02-11 18:46:17 +00005180 if (OfBlockPointer) {
5181 QualType RHS = rbase->getResultType();
5182 QualType LHS = lbase->getResultType();
5183 bool UnqualifiedResult = Unqualified;
5184 if (!UnqualifiedResult)
5185 UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005186 retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
Fariborz Jahanian17825972011-02-11 18:46:17 +00005187 }
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005188 else
John McCall8c6b56f2010-12-15 01:06:38 +00005189 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
5190 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005191 if (retType.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005192
5193 if (Unqualified)
5194 retType = retType.getUnqualifiedType();
5195
5196 CanQualType LRetType = getCanonicalType(lbase->getResultType());
5197 CanQualType RRetType = getCanonicalType(rbase->getResultType());
5198 if (Unqualified) {
5199 LRetType = LRetType.getUnqualifiedType();
5200 RRetType = RRetType.getUnqualifiedType();
5201 }
5202
5203 if (getCanonicalType(retType) != LRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00005204 allLTypes = false;
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005205 if (getCanonicalType(retType) != RRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00005206 allRTypes = false;
John McCall8c6b56f2010-12-15 01:06:38 +00005207
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00005208 // FIXME: double check this
5209 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
5210 // rbase->getRegParmAttr() != 0 &&
5211 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005212 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
5213 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
John McCall8c6b56f2010-12-15 01:06:38 +00005214
Douglas Gregor8c940862010-01-18 17:14:39 +00005215 // Compatible functions must have compatible calling conventions
John McCall8c6b56f2010-12-15 01:06:38 +00005216 if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
Douglas Gregor8c940862010-01-18 17:14:39 +00005217 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00005218
John McCall8c6b56f2010-12-15 01:06:38 +00005219 // Regparm is part of the calling convention.
Eli Friedmanc5b20b52011-04-09 08:18:08 +00005220 if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
5221 return QualType();
John McCall8c6b56f2010-12-15 01:06:38 +00005222 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
5223 return QualType();
5224
5225 // It's noreturn if either type is.
5226 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
5227 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
5228 if (NoReturn != lbaseInfo.getNoReturn())
5229 allLTypes = false;
5230 if (NoReturn != rbaseInfo.getNoReturn())
5231 allRTypes = false;
5232
5233 FunctionType::ExtInfo einfo(NoReturn,
Eli Friedmanc5b20b52011-04-09 08:18:08 +00005234 lbaseInfo.getHasRegParm(),
John McCall8c6b56f2010-12-15 01:06:38 +00005235 lbaseInfo.getRegParm(),
5236 lbaseInfo.getCC());
John McCalldb40c7f2010-12-14 08:05:40 +00005237
Eli Friedman47f77112008-08-22 00:56:42 +00005238 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00005239 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
5240 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00005241 unsigned lproto_nargs = lproto->getNumArgs();
5242 unsigned rproto_nargs = rproto->getNumArgs();
5243
5244 // Compatible functions must have the same number of arguments
5245 if (lproto_nargs != rproto_nargs)
5246 return QualType();
5247
5248 // Variadic and non-variadic functions aren't compatible
5249 if (lproto->isVariadic() != rproto->isVariadic())
5250 return QualType();
5251
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00005252 if (lproto->getTypeQuals() != rproto->getTypeQuals())
5253 return QualType();
5254
Eli Friedman47f77112008-08-22 00:56:42 +00005255 // Check argument compatibility
5256 llvm::SmallVector<QualType, 10> types;
5257 for (unsigned i = 0; i < lproto_nargs; i++) {
5258 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
5259 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00005260 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
5261 OfBlockPointer,
5262 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005263 if (argtype.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005264
5265 if (Unqualified)
5266 argtype = argtype.getUnqualifiedType();
5267
Eli Friedman47f77112008-08-22 00:56:42 +00005268 types.push_back(argtype);
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005269 if (Unqualified) {
5270 largtype = largtype.getUnqualifiedType();
5271 rargtype = rargtype.getUnqualifiedType();
5272 }
5273
Chris Lattner465fa322008-10-05 17:34:18 +00005274 if (getCanonicalType(argtype) != getCanonicalType(largtype))
5275 allLTypes = false;
5276 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
5277 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00005278 }
5279 if (allLTypes) return lhs;
5280 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00005281
5282 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
5283 EPI.ExtInfo = einfo;
5284 return getFunctionType(retType, types.begin(), types.size(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00005285 }
5286
5287 if (lproto) allRTypes = false;
5288 if (rproto) allLTypes = false;
5289
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005290 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00005291 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00005292 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00005293 if (proto->isVariadic()) return QualType();
5294 // Check that the types are compatible with the types that
5295 // would result from default argument promotions (C99 6.7.5.3p15).
5296 // The only types actually affected are promotable integer
5297 // types and floats, which would be passed as a different
5298 // type depending on whether the prototype is visible.
5299 unsigned proto_nargs = proto->getNumArgs();
5300 for (unsigned i = 0; i < proto_nargs; ++i) {
5301 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00005302
5303 // Look at the promotion type of enum types, since that is the type used
5304 // to pass enum values.
5305 if (const EnumType *Enum = argTy->getAs<EnumType>())
5306 argTy = Enum->getDecl()->getPromotionType();
5307
Eli Friedman47f77112008-08-22 00:56:42 +00005308 if (argTy->isPromotableIntegerType() ||
5309 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
5310 return QualType();
5311 }
5312
5313 if (allLTypes) return lhs;
5314 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00005315
5316 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
5317 EPI.ExtInfo = einfo;
Eli Friedman47f77112008-08-22 00:56:42 +00005318 return getFunctionType(retType, proto->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005319 proto->getNumArgs(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00005320 }
5321
5322 if (allLTypes) return lhs;
5323 if (allRTypes) return rhs;
John McCall8c6b56f2010-12-15 01:06:38 +00005324 return getFunctionNoProtoType(retType, einfo);
Eli Friedman47f77112008-08-22 00:56:42 +00005325}
5326
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005327QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005328 bool OfBlockPointer,
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005329 bool Unqualified, bool BlockReturnType) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00005330 // C++ [expr]: If an expression initially has the type "reference to T", the
5331 // type is adjusted to "T" prior to any further analysis, the expression
5332 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005333 // expression is an lvalue unless the reference is an rvalue reference and
5334 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00005335 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
5336 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005337
5338 if (Unqualified) {
5339 LHS = LHS.getUnqualifiedType();
5340 RHS = RHS.getUnqualifiedType();
5341 }
Douglas Gregor21e771e2010-02-03 21:02:30 +00005342
Eli Friedman47f77112008-08-22 00:56:42 +00005343 QualType LHSCan = getCanonicalType(LHS),
5344 RHSCan = getCanonicalType(RHS);
5345
5346 // If two types are identical, they are compatible.
5347 if (LHSCan == RHSCan)
5348 return LHS;
5349
John McCall8ccfcb52009-09-24 19:53:00 +00005350 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00005351 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5352 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00005353 if (LQuals != RQuals) {
5354 // If any of these qualifiers are different, we have a type
5355 // mismatch.
5356 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5357 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5358 return QualType();
5359
5360 // Exactly one GC qualifier difference is allowed: __strong is
5361 // okay if the other type has no GC qualifier but is an Objective
5362 // C object pointer (i.e. implicitly strong by default). We fix
5363 // this by pretending that the unqualified type was actually
5364 // qualified __strong.
5365 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5366 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5367 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5368
5369 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5370 return QualType();
5371
5372 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
5373 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
5374 }
5375 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
5376 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
5377 }
Eli Friedman47f77112008-08-22 00:56:42 +00005378 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00005379 }
5380
5381 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00005382
Eli Friedmandcca6332009-06-01 01:22:52 +00005383 Type::TypeClass LHSClass = LHSCan->getTypeClass();
5384 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00005385
Chris Lattnerfd652912008-01-14 05:45:46 +00005386 // We want to consider the two function types to be the same for these
5387 // comparisons, just force one to the other.
5388 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5389 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00005390
5391 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00005392 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5393 LHSClass = Type::ConstantArray;
5394 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5395 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00005396
John McCall8b07ec22010-05-15 11:32:37 +00005397 // ObjCInterfaces are just specialized ObjCObjects.
5398 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5399 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5400
Nate Begemance4d7fc2008-04-18 23:10:10 +00005401 // Canonicalize ExtVector -> Vector.
5402 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5403 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00005404
Chris Lattner95554662008-04-07 05:43:21 +00005405 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005406 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00005407 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00005408 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00005409 // Compatibility is based on the underlying type, not the promotion
5410 // type.
John McCall9dd450b2009-09-21 23:43:11 +00005411 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005412 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5413 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005414 }
John McCall9dd450b2009-09-21 23:43:11 +00005415 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005416 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5417 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005418 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005419
Eli Friedman47f77112008-08-22 00:56:42 +00005420 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005421 }
Eli Friedman47f77112008-08-22 00:56:42 +00005422
Steve Naroffc6edcbd2008-01-09 22:43:08 +00005423 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005424 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005425#define TYPE(Class, Base)
5426#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00005427#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005428#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5429#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5430#include "clang/AST/TypeNodes.def"
5431 assert(false && "Non-canonical and dependent types shouldn't get here");
5432 return QualType();
5433
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005434 case Type::LValueReference:
5435 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005436 case Type::MemberPointer:
5437 assert(false && "C++ should never be in mergeTypes");
5438 return QualType();
5439
John McCall8b07ec22010-05-15 11:32:37 +00005440 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005441 case Type::IncompleteArray:
5442 case Type::VariableArray:
5443 case Type::FunctionProto:
5444 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005445 assert(false && "Types are eliminated above");
5446 return QualType();
5447
Chris Lattnerfd652912008-01-14 05:45:46 +00005448 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00005449 {
5450 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005451 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5452 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005453 if (Unqualified) {
5454 LHSPointee = LHSPointee.getUnqualifiedType();
5455 RHSPointee = RHSPointee.getUnqualifiedType();
5456 }
5457 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5458 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005459 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00005460 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005461 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00005462 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005463 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005464 return getPointerType(ResultType);
5465 }
Steve Naroff68e167d2008-12-10 17:49:55 +00005466 case Type::BlockPointer:
5467 {
5468 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005469 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5470 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005471 if (Unqualified) {
5472 LHSPointee = LHSPointee.getUnqualifiedType();
5473 RHSPointee = RHSPointee.getUnqualifiedType();
5474 }
5475 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5476 Unqualified);
Steve Naroff68e167d2008-12-10 17:49:55 +00005477 if (ResultType.isNull()) return QualType();
5478 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5479 return LHS;
5480 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5481 return RHS;
5482 return getBlockPointerType(ResultType);
5483 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005484 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00005485 {
5486 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5487 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5488 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5489 return QualType();
5490
5491 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5492 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005493 if (Unqualified) {
5494 LHSElem = LHSElem.getUnqualifiedType();
5495 RHSElem = RHSElem.getUnqualifiedType();
5496 }
5497
5498 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005499 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00005500 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5501 return LHS;
5502 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5503 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00005504 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5505 ArrayType::ArraySizeModifier(), 0);
5506 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5507 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005508 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5509 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00005510 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5511 return LHS;
5512 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5513 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005514 if (LVAT) {
5515 // FIXME: This isn't correct! But tricky to implement because
5516 // the array's size has to be the size of LHS, but the type
5517 // has to be different.
5518 return LHS;
5519 }
5520 if (RVAT) {
5521 // FIXME: This isn't correct! But tricky to implement because
5522 // the array's size has to be the size of RHS, but the type
5523 // has to be different.
5524 return RHS;
5525 }
Eli Friedman3e62c212008-08-22 01:48:21 +00005526 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5527 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00005528 return getIncompleteArrayType(ResultType,
5529 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005530 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005531 case Type::FunctionNoProto:
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005532 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005533 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005534 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00005535 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00005536 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005537 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00005538 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00005539 case Type::Complex:
5540 // Distinct complex types are incompatible.
5541 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005542 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00005543 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00005544 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5545 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00005546 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00005547 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00005548 case Type::ObjCObject: {
5549 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00005550 // FIXME: This should be type compatibility, e.g. whether
5551 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00005552 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5553 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5554 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00005555 return LHS;
5556
Eli Friedman47f77112008-08-22 00:56:42 +00005557 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00005558 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00005559 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005560 if (OfBlockPointer) {
5561 if (canAssignObjCInterfacesInBlockPointer(
5562 LHS->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian90186f82011-03-14 16:07:00 +00005563 RHS->getAs<ObjCObjectPointerType>(),
5564 BlockReturnType))
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005565 return LHS;
5566 return QualType();
5567 }
John McCall9dd450b2009-09-21 23:43:11 +00005568 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5569 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00005570 return LHS;
5571
Steve Naroffc68cfcf2008-12-10 22:14:21 +00005572 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005573 }
Steve Naroff32e44c02007-10-15 20:41:53 +00005574 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005575
5576 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005577}
Ted Kremenekfc581a92007-10-31 17:10:13 +00005578
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005579/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5580/// 'RHS' attributes and returns the merged version; including for function
5581/// return types.
5582QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5583 QualType LHSCan = getCanonicalType(LHS),
5584 RHSCan = getCanonicalType(RHS);
5585 // If two types are identical, they are compatible.
5586 if (LHSCan == RHSCan)
5587 return LHS;
5588 if (RHSCan->isFunctionType()) {
5589 if (!LHSCan->isFunctionType())
5590 return QualType();
5591 QualType OldReturnType =
5592 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5593 QualType NewReturnType =
5594 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5595 QualType ResReturnType =
5596 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5597 if (ResReturnType.isNull())
5598 return QualType();
5599 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5600 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5601 // In either case, use OldReturnType to build the new function type.
5602 const FunctionType *F = LHS->getAs<FunctionType>();
5603 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
John McCalldb40c7f2010-12-14 08:05:40 +00005604 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5605 EPI.ExtInfo = getFunctionExtInfo(LHS);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005606 QualType ResultType
5607 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005608 FPT->getNumArgs(), EPI);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005609 return ResultType;
5610 }
5611 }
5612 return QualType();
5613 }
5614
5615 // If the qualifiers are different, the types can still be merged.
5616 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5617 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5618 if (LQuals != RQuals) {
5619 // If any of these qualifiers are different, we have a type mismatch.
5620 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5621 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5622 return QualType();
5623
5624 // Exactly one GC qualifier difference is allowed: __strong is
5625 // okay if the other type has no GC qualifier but is an Objective
5626 // C object pointer (i.e. implicitly strong by default). We fix
5627 // this by pretending that the unqualified type was actually
5628 // qualified __strong.
5629 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5630 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5631 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5632
5633 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5634 return QualType();
5635
5636 if (GC_L == Qualifiers::Strong)
5637 return LHS;
5638 if (GC_R == Qualifiers::Strong)
5639 return RHS;
5640 return QualType();
5641 }
5642
5643 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5644 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5645 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5646 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5647 if (ResQT == LHSBaseQT)
5648 return LHS;
5649 if (ResQT == RHSBaseQT)
5650 return RHS;
5651 }
5652 return QualType();
5653}
5654
Chris Lattner4ba0cef2008-04-07 07:01:58 +00005655//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005656// Integer Predicates
5657//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00005658
Jay Foad39c79802011-01-12 09:06:06 +00005659unsigned ASTContext::getIntWidth(QualType T) const {
John McCall424cec92011-01-19 06:33:43 +00005660 if (const EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00005661 T = ET->getDecl()->getIntegerType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00005662 if (T->isBooleanType())
5663 return 1;
Eli Friedman1efaaea2009-02-13 02:31:07 +00005664 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005665 return (unsigned)getTypeSize(T);
5666}
5667
5668QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00005669 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00005670
5671 // Turn <4 x signed int> -> <4 x unsigned int>
5672 if (const VectorType *VTy = T->getAs<VectorType>())
5673 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsonaeb56442010-11-10 21:56:12 +00005674 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattnerec3a1562009-10-17 20:33:28 +00005675
5676 // For enums, we return the unsigned version of the base type.
5677 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005678 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00005679
5680 const BuiltinType *BTy = T->getAs<BuiltinType>();
5681 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005682 switch (BTy->getKind()) {
5683 case BuiltinType::Char_S:
5684 case BuiltinType::SChar:
5685 return UnsignedCharTy;
5686 case BuiltinType::Short:
5687 return UnsignedShortTy;
5688 case BuiltinType::Int:
5689 return UnsignedIntTy;
5690 case BuiltinType::Long:
5691 return UnsignedLongTy;
5692 case BuiltinType::LongLong:
5693 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00005694 case BuiltinType::Int128:
5695 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005696 default:
5697 assert(0 && "Unexpected signed integer type");
5698 return QualType();
5699 }
5700}
5701
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005702ASTMutationListener::~ASTMutationListener() { }
5703
Chris Lattnerecd79c62009-06-14 00:45:47 +00005704
5705//===----------------------------------------------------------------------===//
5706// Builtin Type Computation
5707//===----------------------------------------------------------------------===//
5708
5709/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattnerdc226c22010-10-01 22:42:38 +00005710/// pointer over the consumed characters. This returns the resultant type. If
5711/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5712/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5713/// a vector of "i*".
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005714///
5715/// RequiresICE is filled in on return to indicate whether the value is required
5716/// to be an Integer Constant Expression.
Jay Foad39c79802011-01-12 09:06:06 +00005717static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00005718 ASTContext::GetBuiltinTypeError &Error,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005719 bool &RequiresICE,
Chris Lattnerdc226c22010-10-01 22:42:38 +00005720 bool AllowTypeModifiers) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005721 // Modifiers.
5722 int HowLong = 0;
5723 bool Signed = false, Unsigned = false;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005724 RequiresICE = false;
Chris Lattner84733392010-10-01 07:13:18 +00005725
Chris Lattnerdc226c22010-10-01 22:42:38 +00005726 // Read the prefixed modifiers first.
Chris Lattnerecd79c62009-06-14 00:45:47 +00005727 bool Done = false;
5728 while (!Done) {
5729 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00005730 default: Done = true; --Str; break;
Chris Lattner84733392010-10-01 07:13:18 +00005731 case 'I':
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005732 RequiresICE = true;
Chris Lattner84733392010-10-01 07:13:18 +00005733 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005734 case 'S':
5735 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5736 assert(!Signed && "Can't use 'S' modifier multiple times!");
5737 Signed = true;
5738 break;
5739 case 'U':
5740 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5741 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5742 Unsigned = true;
5743 break;
5744 case 'L':
5745 assert(HowLong <= 2 && "Can't have LLLL modifier");
5746 ++HowLong;
5747 break;
5748 }
5749 }
5750
5751 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005752
Chris Lattnerecd79c62009-06-14 00:45:47 +00005753 // Read the base type.
5754 switch (*Str++) {
5755 default: assert(0 && "Unknown builtin type letter!");
5756 case 'v':
5757 assert(HowLong == 0 && !Signed && !Unsigned &&
5758 "Bad modifiers used with 'v'!");
5759 Type = Context.VoidTy;
5760 break;
5761 case 'f':
5762 assert(HowLong == 0 && !Signed && !Unsigned &&
5763 "Bad modifiers used with 'f'!");
5764 Type = Context.FloatTy;
5765 break;
5766 case 'd':
5767 assert(HowLong < 2 && !Signed && !Unsigned &&
5768 "Bad modifiers used with 'd'!");
5769 if (HowLong)
5770 Type = Context.LongDoubleTy;
5771 else
5772 Type = Context.DoubleTy;
5773 break;
5774 case 's':
5775 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5776 if (Unsigned)
5777 Type = Context.UnsignedShortTy;
5778 else
5779 Type = Context.ShortTy;
5780 break;
5781 case 'i':
5782 if (HowLong == 3)
5783 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5784 else if (HowLong == 2)
5785 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5786 else if (HowLong == 1)
5787 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5788 else
5789 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5790 break;
5791 case 'c':
5792 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5793 if (Signed)
5794 Type = Context.SignedCharTy;
5795 else if (Unsigned)
5796 Type = Context.UnsignedCharTy;
5797 else
5798 Type = Context.CharTy;
5799 break;
5800 case 'b': // boolean
5801 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5802 Type = Context.BoolTy;
5803 break;
5804 case 'z': // size_t.
5805 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5806 Type = Context.getSizeType();
5807 break;
5808 case 'F':
5809 Type = Context.getCFConstantStringType();
5810 break;
Fariborz Jahaniand11da7e2010-11-09 21:38:20 +00005811 case 'G':
5812 Type = Context.getObjCIdType();
5813 break;
5814 case 'H':
5815 Type = Context.getObjCSelType();
5816 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005817 case 'a':
5818 Type = Context.getBuiltinVaListType();
5819 assert(!Type.isNull() && "builtin va list type not initialized!");
5820 break;
5821 case 'A':
5822 // This is a "reference" to a va_list; however, what exactly
5823 // this means depends on how va_list is defined. There are two
5824 // different kinds of va_list: ones passed by value, and ones
5825 // passed by reference. An example of a by-value va_list is
5826 // x86, where va_list is a char*. An example of by-ref va_list
5827 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5828 // we want this argument to be a char*&; for x86-64, we want
5829 // it to be a __va_list_tag*.
5830 Type = Context.getBuiltinVaListType();
5831 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005832 if (Type->isArrayType())
Chris Lattnerecd79c62009-06-14 00:45:47 +00005833 Type = Context.getArrayDecayedType(Type);
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005834 else
Chris Lattnerecd79c62009-06-14 00:45:47 +00005835 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005836 break;
5837 case 'V': {
5838 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005839 unsigned NumElements = strtoul(Str, &End, 10);
5840 assert(End != Str && "Missing vector size");
Chris Lattnerecd79c62009-06-14 00:45:47 +00005841 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005842
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005843 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5844 RequiresICE, false);
5845 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattnerdc226c22010-10-01 22:42:38 +00005846
5847 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner37141f42010-06-23 06:00:24 +00005848 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00005849 VectorType::GenericVector);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005850 break;
5851 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005852 case 'X': {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005853 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5854 false);
5855 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005856 Type = Context.getComplexType(ElementType);
5857 break;
5858 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005859 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005860 Type = Context.getFILEType();
5861 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005862 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005863 return QualType();
5864 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005865 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005866 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005867 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005868 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005869 else
5870 Type = Context.getjmp_bufType();
5871
Mike Stump2adb4da2009-07-28 23:47:15 +00005872 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005873 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005874 return QualType();
5875 }
5876 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005877 }
Mike Stump11289f42009-09-09 15:08:12 +00005878
Chris Lattnerdc226c22010-10-01 22:42:38 +00005879 // If there are modifiers and if we're allowed to parse them, go for it.
5880 Done = !AllowTypeModifiers;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005881 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005882 switch (char c = *Str++) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005883 default: Done = true; --Str; break;
5884 case '*':
5885 case '&': {
5886 // Both pointers and references can have their pointee types
5887 // qualified with an address space.
5888 char *End;
5889 unsigned AddrSpace = strtoul(Str, &End, 10);
5890 if (End != Str && AddrSpace != 0) {
5891 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5892 Str = End;
5893 }
5894 if (c == '*')
5895 Type = Context.getPointerType(Type);
5896 else
5897 Type = Context.getLValueReferenceType(Type);
5898 break;
5899 }
5900 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5901 case 'C':
5902 Type = Type.withConst();
5903 break;
5904 case 'D':
5905 Type = Context.getVolatileType(Type);
5906 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005907 }
5908 }
Chris Lattner84733392010-10-01 07:13:18 +00005909
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005910 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner84733392010-10-01 07:13:18 +00005911 "Integer constant 'I' type must be an integer");
Mike Stump11289f42009-09-09 15:08:12 +00005912
Chris Lattnerecd79c62009-06-14 00:45:47 +00005913 return Type;
5914}
5915
5916/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattnerdc226c22010-10-01 22:42:38 +00005917QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005918 GetBuiltinTypeError &Error,
Jay Foad39c79802011-01-12 09:06:06 +00005919 unsigned *IntegerConstantArgs) const {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005920 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump11289f42009-09-09 15:08:12 +00005921
Chris Lattnerecd79c62009-06-14 00:45:47 +00005922 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005923
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005924 bool RequiresICE = false;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005925 Error = GE_None;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005926 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5927 RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005928 if (Error != GE_None)
5929 return QualType();
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005930
5931 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5932
Chris Lattnerecd79c62009-06-14 00:45:47 +00005933 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005934 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005935 if (Error != GE_None)
5936 return QualType();
5937
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005938 // If this argument is required to be an IntegerConstantExpression and the
5939 // caller cares, fill in the bitmask we return.
5940 if (RequiresICE && IntegerConstantArgs)
5941 *IntegerConstantArgs |= 1 << ArgTypes.size();
5942
Chris Lattnerecd79c62009-06-14 00:45:47 +00005943 // Do array -> pointer decay. The builtin should use the decayed type.
5944 if (Ty->isArrayType())
5945 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005946
Chris Lattnerecd79c62009-06-14 00:45:47 +00005947 ArgTypes.push_back(Ty);
5948 }
5949
5950 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5951 "'.' should only occur at end of builtin type list!");
5952
John McCall991eb4b2010-12-21 00:44:39 +00005953 FunctionType::ExtInfo EI;
5954 if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
5955
5956 bool Variadic = (TypeStr[0] == '.');
5957
5958 // We really shouldn't be making a no-proto type here, especially in C++.
5959 if (ArgTypes.empty() && Variadic)
5960 return getFunctionNoProtoType(ResType, EI);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005961
John McCalldb40c7f2010-12-14 08:05:40 +00005962 FunctionProtoType::ExtProtoInfo EPI;
John McCall991eb4b2010-12-21 00:44:39 +00005963 EPI.ExtInfo = EI;
5964 EPI.Variadic = Variadic;
John McCalldb40c7f2010-12-14 08:05:40 +00005965
5966 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005967}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005968
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005969GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5970 GVALinkage External = GVA_StrongExternal;
5971
5972 Linkage L = FD->getLinkage();
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005973 switch (L) {
5974 case NoLinkage:
5975 case InternalLinkage:
5976 case UniqueExternalLinkage:
5977 return GVA_Internal;
5978
5979 case ExternalLinkage:
5980 switch (FD->getTemplateSpecializationKind()) {
5981 case TSK_Undeclared:
5982 case TSK_ExplicitSpecialization:
5983 External = GVA_StrongExternal;
5984 break;
5985
5986 case TSK_ExplicitInstantiationDefinition:
5987 return GVA_ExplicitTemplateInstantiation;
5988
5989 case TSK_ExplicitInstantiationDeclaration:
5990 case TSK_ImplicitInstantiation:
5991 External = GVA_TemplateInstantiation;
5992 break;
5993 }
5994 }
5995
5996 if (!FD->isInlined())
5997 return External;
5998
5999 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
6000 // GNU or C99 inline semantics. Determine whether this symbol should be
6001 // externally visible.
6002 if (FD->isInlineDefinitionExternallyVisible())
6003 return External;
6004
6005 // C99 inline semantics, where the symbol is not externally visible.
6006 return GVA_C99Inline;
6007 }
6008
6009 // C++0x [temp.explicit]p9:
6010 // [ Note: The intent is that an inline function that is the subject of
6011 // an explicit instantiation declaration will still be implicitly
6012 // instantiated when used so that the body can be considered for
6013 // inlining, but that no out-of-line copy of the inline function would be
6014 // generated in the translation unit. -- end note ]
6015 if (FD->getTemplateSpecializationKind()
6016 == TSK_ExplicitInstantiationDeclaration)
6017 return GVA_C99Inline;
6018
6019 return GVA_CXXInline;
6020}
6021
6022GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
6023 // If this is a static data member, compute the kind of template
6024 // specialization. Otherwise, this variable is not part of a
6025 // template.
6026 TemplateSpecializationKind TSK = TSK_Undeclared;
6027 if (VD->isStaticDataMember())
6028 TSK = VD->getTemplateSpecializationKind();
6029
6030 Linkage L = VD->getLinkage();
6031 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
6032 VD->getType()->getLinkage() == UniqueExternalLinkage)
6033 L = UniqueExternalLinkage;
6034
6035 switch (L) {
6036 case NoLinkage:
6037 case InternalLinkage:
6038 case UniqueExternalLinkage:
6039 return GVA_Internal;
6040
6041 case ExternalLinkage:
6042 switch (TSK) {
6043 case TSK_Undeclared:
6044 case TSK_ExplicitSpecialization:
6045 return GVA_StrongExternal;
6046
6047 case TSK_ExplicitInstantiationDeclaration:
6048 llvm_unreachable("Variable should not be instantiated");
6049 // Fall through to treat this like any other instantiation.
6050
6051 case TSK_ExplicitInstantiationDefinition:
6052 return GVA_ExplicitTemplateInstantiation;
6053
6054 case TSK_ImplicitInstantiation:
6055 return GVA_TemplateInstantiation;
6056 }
6057 }
6058
6059 return GVA_StrongExternal;
6060}
6061
Argyrios Kyrtzidisc9049332010-07-29 20:08:05 +00006062bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006063 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
6064 if (!VD->isFileVarDecl())
6065 return false;
6066 } else if (!isa<FunctionDecl>(D))
6067 return false;
6068
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00006069 // Weak references don't produce any output by themselves.
6070 if (D->hasAttr<WeakRefAttr>())
6071 return false;
6072
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006073 // Aliases and used decls are required.
6074 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
6075 return true;
6076
6077 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
6078 // Forward declarations aren't required.
6079 if (!FD->isThisDeclarationADefinition())
6080 return false;
6081
6082 // Constructors and destructors are required.
6083 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
6084 return true;
6085
6086 // The key function for a class is required.
6087 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
6088 const CXXRecordDecl *RD = MD->getParent();
6089 if (MD->isOutOfLine() && RD->isDynamicClass()) {
6090 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
6091 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
6092 return true;
6093 }
6094 }
6095
6096 GVALinkage Linkage = GetGVALinkageForFunction(FD);
6097
6098 // static, static inline, always_inline, and extern inline functions can
6099 // always be deferred. Normal inline functions can be deferred in C99/C++.
6100 // Implicit template instantiations can also be deferred in C++.
6101 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
6102 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
6103 return false;
6104 return true;
6105 }
6106
6107 const VarDecl *VD = cast<VarDecl>(D);
6108 assert(VD->isFileVarDecl() && "Expected file scoped var");
6109
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00006110 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
6111 return false;
6112
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006113 // Structs that have non-trivial constructors or destructors are required.
6114
6115 // FIXME: Handle references.
6116 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
6117 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +00006118 if (RD->hasDefinition() &&
6119 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00006120 return true;
6121 }
6122 }
6123
6124 GVALinkage L = GetGVALinkageForVariable(VD);
6125 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
6126 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
6127 return false;
6128 }
6129
6130 return true;
6131}
Charles Davis53c59df2010-08-16 03:33:14 +00006132
Charles Davis99202b32010-11-09 18:04:24 +00006133CallingConv ASTContext::getDefaultMethodCallConv() {
6134 // Pass through to the C++ ABI object
6135 return ABI->getDefaultMethodCallConv();
6136}
6137
Jay Foad39c79802011-01-12 09:06:06 +00006138bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
Anders Carlsson60a62632010-11-25 01:51:53 +00006139 // Pass through to the C++ ABI object
6140 return ABI->isNearlyEmpty(RD);
6141}
6142
Peter Collingbourne0ff0b372011-01-13 18:57:25 +00006143MangleContext *ASTContext::createMangleContext() {
6144 switch (Target.getCXXABI()) {
6145 case CXXABI_ARM:
6146 case CXXABI_Itanium:
6147 return createItaniumMangleContext(*this, getDiagnostics());
6148 case CXXABI_Microsoft:
6149 return createMicrosoftMangleContext(*this, getDiagnostics());
6150 }
6151 assert(0 && "Unsupported ABI");
6152 return 0;
6153}
6154
Charles Davis53c59df2010-08-16 03:33:14 +00006155CXXABI::~CXXABI() {}
Ted Kremenekf5df0ce2011-04-28 04:53:38 +00006156
6157size_t ASTContext::getSideTableAllocatedMemory() const {
6158 size_t bytes = 0;
6159 bytes += ASTRecordLayouts.getMemorySize();
6160 bytes += ObjCLayouts.getMemorySize();
6161 bytes += KeyFunctions.getMemorySize();
6162 bytes += ObjCImpls.getMemorySize();
6163 bytes += BlockVarCopyInits.getMemorySize();
6164 bytes += DeclAttrs.getMemorySize();
6165 bytes += InstantiatedFromStaticDataMember.getMemorySize();
6166 bytes += InstantiatedFromUsingDecl.getMemorySize();
6167 bytes += InstantiatedFromUsingShadowDecl.getMemorySize();
6168 bytes += InstantiatedFromUnnamedFieldDecl.getMemorySize();
6169 return bytes;
6170}
6171