blob: 8316ea68e9e46cf1ac2a52957f7b05b69a5d35e2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyckbdc601b2009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
John McCallea1471e2010-05-20 01:18:31 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000023#include "clang/AST/ASTMutationListener.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000025#include "clang/AST/Mangle.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000027#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028#include "clang/Basic/TargetInfo.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000029#include "llvm/ADT/SmallString.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000030#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000031#include "llvm/Support/MathExtras.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000032#include "llvm/Support/raw_ostream.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000033#include "CXXABI.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000034
Reid Spencer5f016e22007-07-11 17:01:13 +000035using namespace clang;
36
Douglas Gregor18274032010-07-03 00:47:00 +000037unsigned ASTContext::NumImplicitDefaultConstructors;
38unsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
Douglas Gregor22584312010-07-02 23:41:54 +000039unsigned ASTContext::NumImplicitCopyConstructors;
40unsigned ASTContext::NumImplicitCopyConstructorsDeclared;
Douglas Gregora376d102010-07-02 21:50:04 +000041unsigned ASTContext::NumImplicitCopyAssignmentOperators;
42unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
Douglas Gregor4923aa22010-07-02 20:37:36 +000043unsigned ASTContext::NumImplicitDestructors;
44unsigned ASTContext::NumImplicitDestructorsDeclared;
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046enum FloatingRank {
47 FloatRank, DoubleRank, LongDoubleRank
48};
49
Douglas Gregor3e1274f2010-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 Gregor61c4d282011-01-05 15:48:55 +000055 ID.AddBoolean(Parm->isParameterPack());
Douglas Gregor3e1274f2010-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 Gregor61c4d282011-01-05 15:48:55 +000070 ID.AddBoolean(NTTP->isParameterPack());
Douglas Gregor3e1274f2010-06-16 21:09:37 +000071 ID.AddPointer(NTTP->getType().getAsOpaquePtr());
Douglas Gregor6952f1e2011-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 Gregor3e1274f2010-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 Foad4ba2a172011-01-12 09:06:06 +000090 TemplateTemplateParmDecl *TTP) const {
Douglas Gregor3e1274f2010-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 Bagnara344577e2011-03-06 15:48:19 +0000110 SourceLocation(),
111 SourceLocation(),
112 TTP->getDepth(),
Douglas Gregor3e1274f2010-06-16 21:09:37 +0000113 TTP->getIndex(), 0, false,
114 TTP->isParameterPack()));
115 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-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 Bagnaraff676cb2011-03-08 08:55:46 +0000130 SourceLocation(),
131 SourceLocation(),
Douglas Gregor6952f1e2011-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 Bagnaraff676cb2011-03-08 08:55:46 +0000141 SourceLocation(),
142 SourceLocation(),
Douglas Gregor6952f1e2011-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 Gregor3e1274f2010-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 Gregor61c4d282011-01-05 15:48:55 +0000159 TTP->getPosition(),
160 TTP->isParameterPack(),
161 0,
Douglas Gregor3e1274f2010-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 Davis071cc7d2010-08-16 03:33:14 +0000179CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
John McCallee79a4c2010-08-21 22:46:04 +0000180 if (!LangOpts.CPlusPlus) return 0;
181
Charles Davis20cf7172010-08-19 02:18:14 +0000182 switch (T.getCXXABI()) {
John McCallee79a4c2010-08-21 22:46:04 +0000183 case CXXABI_ARM:
184 return CreateARMCXXABI(*this);
185 case CXXABI_Itanium:
Charles Davis071cc7d2010-08-16 03:33:14 +0000186 return CreateItaniumCXXABI(*this);
Charles Davis20cf7172010-08-19 02:18:14 +0000187 case CXXABI_Microsoft:
188 return CreateMicrosoftCXXABI(*this);
189 }
John McCallee79a4c2010-08-21 22:46:04 +0000190 return 0;
Charles Davis071cc7d2010-08-16 03:33:14 +0000191}
192
Peter Collingbourne207f4d82011-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 Lattner61710852008-10-05 17:34:18 +0000209ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar444be732009-11-13 05:51:54 +0000210 const TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +0000211 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +0000212 Builtin::Context &builtins,
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000213 unsigned size_reserve) :
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000214 FunctionProtoTypes(this_()),
John McCallef990012010-06-11 11:07:21 +0000215 TemplateSpecializationTypes(this_()),
216 DependentTemplateSpecializationTypes(this_()),
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +0000217 GlobalNestedNameSpecifier(0), IsInt128Installed(false),
218 CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +0000219 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump083c25e2009-10-22 00:49:09 +0000220 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
Peter Collingbourne14b6ba72011-02-09 21:04:32 +0000221 cudaConfigureCallDecl(0),
John McCallbf1a0282010-06-04 23:28:52 +0000222 NullTypeSourceInfo(QualType()),
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000223 SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)),
224 AddrSpaceMap(getAddressSpaceMap(t, LOpts)), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +0000225 Idents(idents), Selectors(sels),
Ted Kremenekac9590e2010-05-10 20:40:08 +0000226 BuiltinInfo(builtins),
227 DeclarationNames(*this),
Argyrios Kyrtzidis9a44b5f2010-10-28 09:29:35 +0000228 ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
Ted Kremenekf057bf72010-06-18 00:31:04 +0000229 LastSDM(0, 0),
John McCall8178df32011-02-22 22:38:33 +0000230 UniqueBlockByRefTypeID(0) {
David Chisnall0f436562009-08-17 16:35:33 +0000231 ObjCIdRedefinitionType = QualType();
232 ObjCClassRedefinitionType = QualType();
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000233 ObjCSelRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000234 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +0000235 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +0000236 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +0000237}
238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239ASTContext::~ASTContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000240 // Release the DenseMaps associated with DeclContext objects.
241 // FIXME: Is this the ideal solution?
242 ReleaseDeclContextMaps();
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000243
Douglas Gregor63fe86b2010-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 Gregor00545312010-05-23 18:26:36 +0000247
Douglas Gregor7d10b7e2010-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 Kremenek3478eb62010-02-11 07:12:28 +0000253
Ted Kremenekdcfcfbe2010-06-08 23:00:58 +0000254 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
Douglas Gregor63fe86b2010-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 Kremenekdcfcfbe2010-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 Gregor63200642010-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 Gregorab452ba2009-03-26 23:50:42 +0000275
Douglas Gregor00545312010-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 Stump1eb44332009-09-09 15:08:12 +0000280void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000281ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
282 ExternalSource.reset(Source.take());
283}
284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285void ASTContext::PrintStats() const {
286 fprintf(stderr, "*** AST Context Stats:\n");
287 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000288
Douglas Gregordbe833d2009-05-26 14:40:08 +0000289 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000290#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000291#define ABSTRACT_TYPE(Name, Parent)
292#include "clang/AST/TypeNodes.def"
293 0 // Extra
294 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
297 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000298 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 }
300
Douglas Gregordbe833d2009-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 Stump1eb44332009-09-09 15:08:12 +0000310
Douglas Gregordbe833d2009-05-26 14:40:08 +0000311 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregored8abf12010-07-08 06:14:04 +0000312
Douglas Gregor4923aa22010-07-02 20:37:36 +0000313 // Implicit special member functions.
Douglas Gregor18274032010-07-03 00:47:00 +0000314 fprintf(stderr, " %u/%u implicit default constructors created\n",
315 NumImplicitDefaultConstructorsDeclared,
316 NumImplicitDefaultConstructors);
Douglas Gregor22584312010-07-02 23:41:54 +0000317 fprintf(stderr, " %u/%u implicit copy constructors created\n",
318 NumImplicitCopyConstructorsDeclared,
319 NumImplicitCopyConstructors);
Douglas Gregora376d102010-07-02 21:50:04 +0000320 fprintf(stderr, " %u/%u implicit copy assignment operators created\n",
321 NumImplicitCopyAssignmentOperatorsDeclared,
322 NumImplicitCopyAssignmentOperators);
Douglas Gregor4923aa22010-07-02 20:37:36 +0000323 fprintf(stderr, " %u/%u implicit destructors created\n",
324 NumImplicitDestructorsDeclared, NumImplicitDestructors);
325
Douglas Gregor2cf26342009-04-09 22:27:44 +0000326 if (ExternalSource.get()) {
327 fprintf(stderr, "\n");
328 ExternalSource->PrintStats();
329 }
Douglas Gregored8abf12010-07-08 06:14:04 +0000330
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000331 BumpAlloc.PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +0000332}
333
334
John McCalle27ec8a2009-10-23 23:03:21 +0000335void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000336 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000337 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000338 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000339}
340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341void ASTContext::InitBuiltinTypes() {
342 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 // C99 6.2.5p19.
345 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 // C99 6.2.5p2.
348 InitBuiltinType(BoolTy, BuiltinType::Bool);
349 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000350 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 InitBuiltinType(CharTy, BuiltinType::Char_S);
352 else
353 InitBuiltinType(CharTy, BuiltinType::Char_U);
354 // C99 6.2.5p4.
355 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 Stump1eb44332009-09-09 15:08:12 +0000360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 // C99 6.2.5p6.
362 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 Stump1eb44332009-09-09 15:08:12 +0000367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 // C99 6.2.5p10.
369 InitBuiltinType(FloatTy, BuiltinType::Float);
370 InitBuiltinType(DoubleTy, BuiltinType::Double);
371 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000372
Chris Lattner2df9ced2009-04-30 02:43:43 +0000373 // GNU extension, 128-bit integers.
374 InitBuiltinType(Int128Ty, BuiltinType::Int128);
375 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
376
Chris Lattner3f59c972010-12-25 23:25:43 +0000377 if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
Eli Friedmand3d77cd2011-04-30 19:24:24 +0000378 if (TargetInfo::isTypeSigned(Target.getWCharType()))
Chris Lattner3f59c972010-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 Lattner3a250322009-02-26 23:43:47 +0000383 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000384
Alisdair Meredithf5c209d2009-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 Gregor898574e2008-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 Gregor8e9bebd2008-10-21 16:13:35 +0000401
John McCall2a984ca2010-10-12 00:20:44 +0000402 // Placeholder type for functions.
403 InitBuiltinType(OverloadTy, BuiltinType::Overload);
404
John McCall864c0412011-04-26 20:42:42 +0000405 // Placeholder type for bound members.
406 InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember);
407
John McCall1de4d4e2011-04-07 08:22:57 +0000408 // "any" type; useful for debugger-like clients.
409 InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny);
410
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 // C99 6.2.5p11.
412 FloatComplexTy = getComplexType(FloatTy);
413 DoubleComplexTy = getComplexType(DoubleTy);
414 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000415
Steve Naroff7e219e42007-10-15 14:41:52 +0000416 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Steve Naroffde2e22d2009-07-15 18:40:39 +0000418 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
419 ObjCIdTypedefType = QualType();
420 ObjCClassTypedefType = QualType();
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000421 ObjCSelTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000423 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroffde2e22d2009-07-15 18:40:39 +0000424 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
425 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000426 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff14108da2009-07-10 23:34:53 +0000427
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000428 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000430 // void * type
431 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000432
433 // nullptr type (C++0x 2.14.7)
434 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000435}
436
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000437Diagnostic &ASTContext::getDiagnostics() const {
438 return SourceMgr.getDiagnostics();
439}
440
Douglas Gregor63200642010-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 Gregor251b4ff2009-10-08 07:24:58 +0000460MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000461ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000462 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000463 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000464 = InstantiatedFromStaticDataMember.find(Var);
465 if (Pos == InstantiatedFromStaticDataMember.end())
466 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor7caa6822009-07-24 20:34:43 +0000468 return Pos->second;
469}
470
Mike Stump1eb44332009-09-09 15:08:12 +0000471void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000472ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000473 TemplateSpecializationKind TSK,
474 SourceLocation PointOfInstantiation) {
Douglas Gregor7caa6822009-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 Gregor251b4ff2009-10-08 07:24:58 +0000479 InstantiatedFromStaticDataMember[Inst]
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000480 = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000481}
482
John McCall7ba107a2009-11-18 02:36:19 +0000483NamedDecl *
John McCalled976492009-12-04 22:46:56 +0000484ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCall7ba107a2009-11-18 02:36:19 +0000485 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCalled976492009-12-04 22:46:56 +0000486 = InstantiatedFromUsingDecl.find(UUD);
487 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson0d8df782009-08-29 19:37:28 +0000488 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Anders Carlsson0d8df782009-08-29 19:37:28 +0000490 return Pos->second;
491}
492
493void
John McCalled976492009-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 Carlsson0d8df782009-08-29 19:37:28 +0000518}
519
Anders Carlssond8b285f2009-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 Stump1eb44332009-09-09 15:08:12 +0000525
Anders Carlssond8b285f2009-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 Stump1eb44332009-09-09 15:08:12 +0000535
Anders Carlssond8b285f2009-09-01 04:26:58 +0000536 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
537}
538
Fariborz Jahanian14d56ef2011-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 Jahanian340fa242011-05-02 17:20:56 +0000546bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
547 const FieldDecl *LastFD) const {
548 return (FD->isBitField() && LastFD && LastFD->isBitField() &&
549 FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() == 0);
550
551}
552
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000553ASTContext::overridden_cxx_method_iterator
554ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
555 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
556 = OverriddenMethods.find(Method);
557 if (Pos == OverriddenMethods.end())
558 return 0;
559
560 return Pos->second.begin();
561}
562
563ASTContext::overridden_cxx_method_iterator
564ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
565 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
566 = OverriddenMethods.find(Method);
567 if (Pos == OverriddenMethods.end())
568 return 0;
569
570 return Pos->second.end();
571}
572
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000573unsigned
574ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
575 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
576 = OverriddenMethods.find(Method);
577 if (Pos == OverriddenMethods.end())
578 return 0;
579
580 return Pos->second.size();
581}
582
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000583void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
584 const CXXMethodDecl *Overridden) {
585 OverriddenMethods[Method].push_back(Overridden);
586}
587
Chris Lattner464175b2007-07-18 17:52:12 +0000588//===----------------------------------------------------------------------===//
589// Type Sizing and Analysis
590//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000591
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000592/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
593/// scalar floating point type.
594const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000595 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000596 assert(BT && "Not a floating point type!");
597 switch (BT->getKind()) {
598 default: assert(0 && "Not a floating point type!");
599 case BuiltinType::Float: return Target.getFloatFormat();
600 case BuiltinType::Double: return Target.getDoubleFormat();
601 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
602 }
603}
604
Ken Dyck8b752f12010-01-27 17:10:57 +0000605/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000606/// specified decl. Note that bitfields do not have a valid alignment, so
607/// this method will assert on them.
Sebastian Redl5d484e82009-11-23 17:18:46 +0000608/// If @p RefAsPointee, references are treated like their underlying type
609/// (for alignof), else they're treated like pointers (for CodeGen).
Jay Foad4ba2a172011-01-12 09:06:06 +0000610CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000611 unsigned Align = Target.getCharWidth();
612
John McCall4081a5c2010-10-08 18:24:19 +0000613 bool UseAlignAttrOnly = false;
614 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
615 Align = AlignFromAttr;
Eli Friedmandcdafb62009-02-22 02:56:25 +0000616
John McCall4081a5c2010-10-08 18:24:19 +0000617 // __attribute__((aligned)) can increase or decrease alignment
618 // *except* on a struct or struct member, where it only increases
619 // alignment unless 'packed' is also specified.
620 //
621 // It is an error for [[align]] to decrease alignment, so we can
622 // ignore that possibility; Sema should diagnose it.
623 if (isa<FieldDecl>(D)) {
624 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
625 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
626 } else {
627 UseAlignAttrOnly = true;
628 }
629 }
630
John McCallba4f5d52011-01-20 07:57:12 +0000631 // If we're using the align attribute only, just ignore everything
632 // else about the declaration and its type.
John McCall4081a5c2010-10-08 18:24:19 +0000633 if (UseAlignAttrOnly) {
John McCallba4f5d52011-01-20 07:57:12 +0000634 // do nothing
635
John McCall4081a5c2010-10-08 18:24:19 +0000636 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneraf707ab2009-01-24 21:53:27 +0000637 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000638 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl5d484e82009-11-23 17:18:46 +0000639 if (RefAsPointee)
640 T = RT->getPointeeType();
641 else
642 T = getPointerType(RT->getPointeeType());
643 }
644 if (!T->isIncompleteType() && !T->isFunctionType()) {
John McCall3b657512011-01-19 10:06:00 +0000645 // Adjust alignments of declarations with array type by the
646 // large-array alignment on the target.
Rafael Espindola6deecb02010-06-04 23:15:27 +0000647 unsigned MinWidth = Target.getLargeArrayMinWidth();
John McCall3b657512011-01-19 10:06:00 +0000648 const ArrayType *arrayType;
649 if (MinWidth && (arrayType = getAsArrayType(T))) {
650 if (isa<VariableArrayType>(arrayType))
651 Align = std::max(Align, Target.getLargeArrayAlign());
652 else if (isa<ConstantArrayType>(arrayType) &&
653 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
654 Align = std::max(Align, Target.getLargeArrayAlign());
Eli Friedmandcdafb62009-02-22 02:56:25 +0000655
John McCall3b657512011-01-19 10:06:00 +0000656 // Walk through any array types while we're at it.
657 T = getBaseElementType(arrayType);
658 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000659 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
660 }
John McCallba4f5d52011-01-20 07:57:12 +0000661
662 // Fields can be subject to extra alignment constraints, like if
663 // the field is packed, the struct is packed, or the struct has a
664 // a max-field-alignment constraint (#pragma pack). So calculate
665 // the actual alignment of the field within the struct, and then
666 // (as we're expected to) constrain that by the alignment of the type.
667 if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
668 // So calculate the alignment of the field.
669 const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
670
671 // Start with the record's overall alignment.
Ken Dyckdac54c12011-02-15 02:32:40 +0000672 unsigned fieldAlign = toBits(layout.getAlignment());
John McCallba4f5d52011-01-20 07:57:12 +0000673
674 // Use the GCD of that and the offset within the record.
675 uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
676 if (offset > 0) {
677 // Alignment is always a power of 2, so the GCD will be a power of 2,
678 // which means we get to do this crazy thing instead of Euclid's.
679 uint64_t lowBitOfOffset = offset & (~offset + 1);
680 if (lowBitOfOffset < fieldAlign)
681 fieldAlign = static_cast<unsigned>(lowBitOfOffset);
682 }
683
684 Align = std::min(Align, fieldAlign);
Charles Davis05f62472010-02-23 04:52:00 +0000685 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000686 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000687
Ken Dyckeb6f5dc2011-01-15 18:38:59 +0000688 return toCharUnitsFromBits(Align);
Chris Lattneraf707ab2009-01-24 21:53:27 +0000689}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000690
John McCallea1471e2010-05-20 01:18:31 +0000691std::pair<CharUnits, CharUnits>
Ken Dyckbee5a792011-02-20 01:55:18 +0000692ASTContext::getTypeInfoInChars(const Type *T) const {
John McCallea1471e2010-05-20 01:18:31 +0000693 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
Ken Dyckeb6f5dc2011-01-15 18:38:59 +0000694 return std::make_pair(toCharUnitsFromBits(Info.first),
695 toCharUnitsFromBits(Info.second));
John McCallea1471e2010-05-20 01:18:31 +0000696}
697
698std::pair<CharUnits, CharUnits>
Ken Dyckbee5a792011-02-20 01:55:18 +0000699ASTContext::getTypeInfoInChars(QualType T) const {
John McCallea1471e2010-05-20 01:18:31 +0000700 return getTypeInfoInChars(T.getTypePtr());
701}
702
Chris Lattnera7674d82007-07-13 22:13:22 +0000703/// getTypeSize - Return the size of the specified type, in bits. This method
704/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000705///
706/// FIXME: Pointers into different addr spaces could have different sizes and
707/// alignment requirements: getPointerInfo should take an AddrSpace, this
708/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000709std::pair<uint64_t, unsigned>
Jay Foad4ba2a172011-01-12 09:06:06 +0000710ASTContext::getTypeInfo(const Type *T) const {
Mike Stump5e301002009-02-27 18:32:39 +0000711 uint64_t Width=0;
712 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000713 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000714#define TYPE(Class, Base)
715#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000716#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000717#define DEPENDENT_TYPE(Class, Base) case Type::Class:
718#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000719 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000720 break;
721
Chris Lattner692233e2007-07-13 22:27:08 +0000722 case Type::FunctionNoProto:
723 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000724 // GCC extension: alignof(function) = 32 bits
725 Width = 0;
726 Align = 32;
727 break;
728
Douglas Gregor72564e72009-02-26 23:50:07 +0000729 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000730 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000731 Width = 0;
732 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
733 break;
734
Steve Narofffb22d962007-08-30 01:06:46 +0000735 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000736 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattner98be4942008-03-05 18:54:05 +0000738 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000739 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000740 Align = EltInfo.second;
Argyrios Kyrtzidiscd88b412011-04-26 21:05:39 +0000741 Width = llvm::RoundUpToAlignment(Width, Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000742 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000743 }
Nate Begeman213541a2008-04-18 23:10:10 +0000744 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000745 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000746 const VectorType *VT = cast<VectorType>(T);
747 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
748 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000749 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000750 // If the alignment is not a power of 2, round up to the next power of 2.
751 // This happens for non-power-of-2 length vectors.
Dan Gohman8eefcd32010-04-21 23:32:43 +0000752 if (Align & (Align-1)) {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000753 Align = llvm::NextPowerOf2(Align);
754 Width = llvm::RoundUpToAlignment(Width, Align);
755 }
Chris Lattner030d8842007-07-19 22:06:24 +0000756 break;
757 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000758
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000759 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000760 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000761 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000762 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000763 // GCC extension: alignof(void) = 8 bits.
764 Width = 0;
765 Align = 8;
766 break;
767
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000768 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000769 Width = Target.getBoolWidth();
770 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000771 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000772 case BuiltinType::Char_S:
773 case BuiltinType::Char_U:
774 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000775 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000776 Width = Target.getCharWidth();
777 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000778 break;
Chris Lattner3f59c972010-12-25 23:25:43 +0000779 case BuiltinType::WChar_S:
780 case BuiltinType::WChar_U:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000781 Width = Target.getWCharWidth();
782 Align = Target.getWCharAlign();
783 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000784 case BuiltinType::Char16:
785 Width = Target.getChar16Width();
786 Align = Target.getChar16Align();
787 break;
788 case BuiltinType::Char32:
789 Width = Target.getChar32Width();
790 Align = Target.getChar32Align();
791 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000792 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000793 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000794 Width = Target.getShortWidth();
795 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000796 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000797 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000798 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000799 Width = Target.getIntWidth();
800 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000801 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000802 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000803 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000804 Width = Target.getLongWidth();
805 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000806 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000807 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000808 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000809 Width = Target.getLongLongWidth();
810 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000811 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000812 case BuiltinType::Int128:
813 case BuiltinType::UInt128:
814 Width = 128;
815 Align = 128; // int128_t is 128-bit aligned on all targets.
816 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000817 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000818 Width = Target.getFloatWidth();
819 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000820 break;
821 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000822 Width = Target.getDoubleWidth();
823 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000824 break;
825 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000826 Width = Target.getLongDoubleWidth();
827 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000828 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000829 case BuiltinType::NullPtr:
830 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
831 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000832 break;
Fariborz Jahaniane04f5fc2010-08-02 18:03:20 +0000833 case BuiltinType::ObjCId:
834 case BuiltinType::ObjCClass:
835 case BuiltinType::ObjCSel:
836 Width = Target.getPointerWidth(0);
837 Align = Target.getPointerAlign(0);
838 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000839 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000840 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000841 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000842 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000843 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000844 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000845 case Type::BlockPointer: {
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000846 unsigned AS = getTargetAddressSpace(
847 cast<BlockPointerType>(T)->getPointeeType());
Steve Naroff485eeff2008-09-24 15:05:44 +0000848 Width = Target.getPointerWidth(AS);
849 Align = Target.getPointerAlign(AS);
850 break;
851 }
Sebastian Redl5d484e82009-11-23 17:18:46 +0000852 case Type::LValueReference:
853 case Type::RValueReference: {
854 // alignof and sizeof should never enter this code path here, so we go
855 // the pointer route.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000856 unsigned AS = getTargetAddressSpace(
857 cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl5d484e82009-11-23 17:18:46 +0000858 Width = Target.getPointerWidth(AS);
859 Align = Target.getPointerAlign(AS);
860 break;
861 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000862 case Type::Pointer: {
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000863 unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
Chris Lattner5426bf62008-04-07 07:01:58 +0000864 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000865 Align = Target.getPointerAlign(AS);
866 break;
867 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000868 case Type::MemberPointer: {
Charles Davis071cc7d2010-08-16 03:33:14 +0000869 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000870 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000871 getTypeInfo(getPointerDiffType());
Charles Davis071cc7d2010-08-16 03:33:14 +0000872 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000873 Align = PtrDiffInfo.second;
874 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000875 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000876 case Type::Complex: {
877 // Complex types have the same alignment as their elements, but twice the
878 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000879 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000880 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000881 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000882 Align = EltInfo.second;
883 break;
884 }
John McCallc12c5bb2010-05-15 11:32:37 +0000885 case Type::ObjCObject:
886 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Patel44a3dde2008-06-04 21:54:36 +0000887 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000888 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000889 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000890 Width = toBits(Layout.getSize());
Ken Dyckdac54c12011-02-15 02:32:40 +0000891 Align = toBits(Layout.getAlignment());
Devang Patel44a3dde2008-06-04 21:54:36 +0000892 break;
893 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000894 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000895 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000896 const TagType *TT = cast<TagType>(T);
897
898 if (TT->getDecl()->isInvalidDecl()) {
Douglas Gregor22ce41d2011-04-20 17:29:44 +0000899 Width = 8;
900 Align = 8;
Chris Lattner8389eab2008-08-09 21:35:13 +0000901 break;
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Daniel Dunbar1d751182008-11-08 05:48:37 +0000904 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000905 return getTypeInfo(ET->getDecl()->getIntegerType());
906
Daniel Dunbar1d751182008-11-08 05:48:37 +0000907 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000908 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000909 Width = toBits(Layout.getSize());
Ken Dyckdac54c12011-02-15 02:32:40 +0000910 Align = toBits(Layout.getAlignment());
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000911 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000912 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000913
Chris Lattner9fcfe922009-10-22 05:17:15 +0000914 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000915 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
916 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000917
Richard Smith34b41d92011-02-20 03:19:35 +0000918 case Type::Auto: {
919 const AutoType *A = cast<AutoType>(T);
920 assert(A->isDeduced() && "Cannot request the size of a dependent type");
Matt Beaumont-Gaydc856af2011-02-22 20:00:16 +0000921 return getTypeInfo(A->getDeducedType().getTypePtr());
Richard Smith34b41d92011-02-20 03:19:35 +0000922 }
923
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000924 case Type::Paren:
925 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
926
Douglas Gregor18857642009-04-30 17:32:17 +0000927 case Type::Typedef: {
Richard Smith162e1c12011-04-15 14:24:37 +0000928 const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregordf1367a2010-08-27 00:11:28 +0000929 std::pair<uint64_t, unsigned> Info
930 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Chris Lattnerc1de52d2011-02-19 22:55:41 +0000931 // If the typedef has an aligned attribute on it, it overrides any computed
932 // alignment we have. This violates the GCC documentation (which says that
933 // attribute(aligned) can only round up) but matches its implementation.
934 if (unsigned AttrAlign = Typedef->getMaxAlignment())
935 Align = AttrAlign;
936 else
937 Align = Info.second;
Douglas Gregordf1367a2010-08-27 00:11:28 +0000938 Width = Info.first;
Douglas Gregor7532dc62009-03-30 22:58:21 +0000939 break;
Chris Lattner71763312008-04-06 22:05:18 +0000940 }
Douglas Gregor18857642009-04-30 17:32:17 +0000941
942 case Type::TypeOfExpr:
943 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
944 .getTypePtr());
945
946 case Type::TypeOf:
947 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
948
Anders Carlsson395b4752009-06-24 19:06:50 +0000949 case Type::Decltype:
950 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
951 .getTypePtr());
952
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000953 case Type::Elaborated:
954 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000955
John McCall9d156a72011-01-06 01:58:22 +0000956 case Type::Attributed:
957 return getTypeInfo(
958 cast<AttributedType>(T)->getEquivalentType().getTypePtr());
959
Douglas Gregor18857642009-04-30 17:32:17 +0000960 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000961 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000962 "Cannot request the size of a dependent type");
963 // FIXME: this is likely to be wrong once we support template
964 // aliases, since a template alias could refer to a typedef that
965 // has an __aligned__ attribute on it.
966 return getTypeInfo(getCanonicalType(T));
967 }
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Chris Lattner464175b2007-07-18 17:52:12 +0000969 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000970 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000971}
972
Ken Dyckeb6f5dc2011-01-15 18:38:59 +0000973/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
974CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
975 return CharUnits::fromQuantity(BitSize / getCharWidth());
976}
977
Ken Dyckdd76a9a2011-02-11 01:54:29 +0000978/// toBits - Convert a size in characters to a size in characters.
979int64_t ASTContext::toBits(CharUnits CharSize) const {
980 return CharSize.getQuantity() * getCharWidth();
981}
982
Ken Dyckbdc601b2009-12-22 14:23:30 +0000983/// getTypeSizeInChars - Return the size of the specified type, in characters.
984/// This method does not work on incomplete types.
Jay Foad4ba2a172011-01-12 09:06:06 +0000985CharUnits ASTContext::getTypeSizeInChars(QualType T) const {
Ken Dyckeb6f5dc2011-01-15 18:38:59 +0000986 return toCharUnitsFromBits(getTypeSize(T));
Ken Dyckbdc601b2009-12-22 14:23:30 +0000987}
Jay Foad4ba2a172011-01-12 09:06:06 +0000988CharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
Ken Dyckeb6f5dc2011-01-15 18:38:59 +0000989 return toCharUnitsFromBits(getTypeSize(T));
Ken Dyckbdc601b2009-12-22 14:23:30 +0000990}
991
Ken Dyck16e20cc2010-01-26 17:25:18 +0000992/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck86fa4312010-01-26 17:22:55 +0000993/// characters. This method does not work on incomplete types.
Jay Foad4ba2a172011-01-12 09:06:06 +0000994CharUnits ASTContext::getTypeAlignInChars(QualType T) const {
Ken Dyckeb6f5dc2011-01-15 18:38:59 +0000995 return toCharUnitsFromBits(getTypeAlign(T));
Ken Dyck86fa4312010-01-26 17:22:55 +0000996}
Jay Foad4ba2a172011-01-12 09:06:06 +0000997CharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
Ken Dyckeb6f5dc2011-01-15 18:38:59 +0000998 return toCharUnitsFromBits(getTypeAlign(T));
Ken Dyck86fa4312010-01-26 17:22:55 +0000999}
1000
Chris Lattner34ebde42009-01-27 18:08:34 +00001001/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
1002/// type for the current target in bits. This can be different than the ABI
1003/// alignment in cases where it is beneficial for performance to overalign
1004/// a data type.
Jay Foad4ba2a172011-01-12 09:06:06 +00001005unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
Chris Lattner34ebde42009-01-27 18:08:34 +00001006 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +00001007
1008 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +00001009 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +00001010 T = CT->getElementType().getTypePtr();
1011 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
1012 T->isSpecificBuiltinType(BuiltinType::LongLong))
1013 return std::max(ABIAlign, (unsigned)getTypeSize(T));
1014
Chris Lattner34ebde42009-01-27 18:08:34 +00001015 return ABIAlign;
1016}
1017
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001018/// ShallowCollectObjCIvars -
1019/// Collect all ivars, including those synthesized, in the current class.
1020///
1021void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Jay Foad4ba2a172011-01-12 09:06:06 +00001022 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001023 // FIXME. This need be removed but there are two many places which
1024 // assume const-ness of ObjCInterfaceDecl
1025 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1026 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1027 Iv= Iv->getNextIvar())
1028 Ivars.push_back(Iv);
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001029}
1030
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001031/// DeepCollectObjCIvars -
1032/// This routine first collects all declared, but not synthesized, ivars in
1033/// super class and then collects all ivars, including those synthesized for
1034/// current class. This routine is used for implementation of current class
1035/// when all ivars, declared and synthesized are known.
Fariborz Jahanian98200742009-05-12 18:14:29 +00001036///
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001037void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
1038 bool leafClass,
Jay Foad4ba2a172011-01-12 09:06:06 +00001039 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001040 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
1041 DeepCollectObjCIvars(SuperClass, false, Ivars);
1042 if (!leafClass) {
1043 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1044 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001045 Ivars.push_back(*I);
1046 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001047 else
1048 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +00001049}
1050
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001051/// CollectInheritedProtocols - Collect all protocols in current class and
1052/// those inherited by it.
1053void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahanian432a8892010-02-12 19:27:33 +00001054 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001055 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001056 // We can use protocol_iterator here instead of
1057 // all_referenced_protocol_iterator since we are walking all categories.
1058 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
1059 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001060 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00001061 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001062 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanianb2f81212010-02-25 18:24:33 +00001063 PE = Proto->protocol_end(); P != PE; ++P) {
1064 Protocols.insert(*P);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001065 CollectInheritedProtocols(*P, Protocols);
1066 }
Fariborz Jahanianb2f81212010-02-25 18:24:33 +00001067 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001068
1069 // Categories of this Interface.
1070 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1071 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1072 CollectInheritedProtocols(CDeclChain, Protocols);
1073 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1074 while (SD) {
1075 CollectInheritedProtocols(SD, Protocols);
1076 SD = SD->getSuperClass();
1077 }
Benjamin Kramerb170ca52010-04-27 17:47:25 +00001078 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001079 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001080 PE = OC->protocol_end(); P != PE; ++P) {
1081 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00001082 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001083 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1084 PE = Proto->protocol_end(); P != PE; ++P)
1085 CollectInheritedProtocols(*P, Protocols);
1086 }
Benjamin Kramerb170ca52010-04-27 17:47:25 +00001087 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001088 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1089 PE = OP->protocol_end(); P != PE; ++P) {
1090 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00001091 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001092 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1093 PE = Proto->protocol_end(); P != PE; ++P)
1094 CollectInheritedProtocols(*P, Protocols);
1095 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00001096 }
1097}
1098
Jay Foad4ba2a172011-01-12 09:06:06 +00001099unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +00001100 unsigned count = 0;
1101 // Count ivars declared in class extension.
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001102 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
1103 CDecl = CDecl->getNextClassExtension())
Benjamin Kramerb170ca52010-04-27 17:47:25 +00001104 count += CDecl->ivar_size();
1105
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +00001106 // Count ivar defined in this class's implementation. This
1107 // includes synthesized ivars.
1108 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramerb170ca52010-04-27 17:47:25 +00001109 count += ImplDecl->ivar_size();
1110
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001111 return count;
1112}
1113
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001114/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
1115ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
1116 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1117 I = ObjCImpls.find(D);
1118 if (I != ObjCImpls.end())
1119 return cast<ObjCImplementationDecl>(I->second);
1120 return 0;
1121}
1122/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1123ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
1124 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1125 I = ObjCImpls.find(D);
1126 if (I != ObjCImpls.end())
1127 return cast<ObjCCategoryImplDecl>(I->second);
1128 return 0;
1129}
1130
1131/// \brief Set the implementation of ObjCInterfaceDecl.
1132void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1133 ObjCImplementationDecl *ImplD) {
1134 assert(IFaceD && ImplD && "Passed null params");
1135 ObjCImpls[IFaceD] = ImplD;
1136}
1137/// \brief Set the implementation of ObjCCategoryDecl.
1138void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1139 ObjCCategoryImplDecl *ImplD) {
1140 assert(CatD && ImplD && "Passed null params");
1141 ObjCImpls[CatD] = ImplD;
1142}
1143
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001144/// \brief Get the copy initialization expression of VarDecl,or NULL if
1145/// none exists.
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001146Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001147 assert(VD && "Passed null params");
1148 assert(VD->hasAttr<BlocksAttr>() &&
1149 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001150 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001151 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001152 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1153}
1154
1155/// \brief Set the copy inialization expression of a block var decl.
1156void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1157 assert(VD && Init && "Passed null params");
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001158 assert(VD->hasAttr<BlocksAttr>() &&
1159 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001160 BlockVarCopyInits[VD] = Init;
1161}
1162
John McCalla93c9342009-12-07 02:54:59 +00001163/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001164///
John McCalla93c9342009-12-07 02:54:59 +00001165/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001166/// the TypeLoc wrappers.
1167///
1168/// \param T the type that will be the basis for type source info. This type
1169/// should refer to how the declarator was written in source code, not to
1170/// what type semantic analysis resolved the declarator to.
John McCalla93c9342009-12-07 02:54:59 +00001171TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
Jay Foad4ba2a172011-01-12 09:06:06 +00001172 unsigned DataSize) const {
John McCall109de5e2009-10-21 00:23:54 +00001173 if (!DataSize)
1174 DataSize = TypeLoc::getFullDataSizeForType(T);
1175 else
1176 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCalla93c9342009-12-07 02:54:59 +00001177 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall109de5e2009-10-21 00:23:54 +00001178
John McCalla93c9342009-12-07 02:54:59 +00001179 TypeSourceInfo *TInfo =
1180 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1181 new (TInfo) TypeSourceInfo(T);
1182 return TInfo;
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001183}
1184
John McCalla93c9342009-12-07 02:54:59 +00001185TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001186 SourceLocation L) const {
John McCalla93c9342009-12-07 02:54:59 +00001187 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
Douglas Gregorc21c7e92011-01-25 19:13:18 +00001188 DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
John McCalla4eb74d2009-10-23 21:14:09 +00001189 return DI;
1190}
1191
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001192const ASTRecordLayout &
Jay Foad4ba2a172011-01-12 09:06:06 +00001193ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001194 return getObjCLayout(D, 0);
1195}
1196
1197const ASTRecordLayout &
Jay Foad4ba2a172011-01-12 09:06:06 +00001198ASTContext::getASTObjCImplementationLayout(
1199 const ObjCImplementationDecl *D) const {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001200 return getObjCLayout(D->getClassInterface(), D);
1201}
1202
Chris Lattnera7674d82007-07-13 22:13:22 +00001203//===----------------------------------------------------------------------===//
1204// Type creation/memoization methods
1205//===----------------------------------------------------------------------===//
1206
Jay Foad4ba2a172011-01-12 09:06:06 +00001207QualType
John McCall3b657512011-01-19 10:06:00 +00001208ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
1209 unsigned fastQuals = quals.getFastQualifiers();
1210 quals.removeFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00001211
1212 // Check if we've already instantiated this type.
1213 llvm::FoldingSetNodeID ID;
John McCall3b657512011-01-19 10:06:00 +00001214 ExtQuals::Profile(ID, baseType, quals);
1215 void *insertPos = 0;
1216 if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
1217 assert(eq->getQualifiers() == quals);
1218 return QualType(eq, fastQuals);
John McCall0953e762009-09-24 19:53:00 +00001219 }
1220
John McCall3b657512011-01-19 10:06:00 +00001221 // If the base type is not canonical, make the appropriate canonical type.
1222 QualType canon;
1223 if (!baseType->isCanonicalUnqualified()) {
1224 SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
1225 canonSplit.second.addConsistentQualifiers(quals);
1226 canon = getExtQualType(canonSplit.first, canonSplit.second);
1227
1228 // Re-find the insert position.
1229 (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
1230 }
1231
1232 ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
1233 ExtQualNodes.InsertNode(eq, insertPos);
1234 return QualType(eq, fastQuals);
John McCall0953e762009-09-24 19:53:00 +00001235}
1236
Jay Foad4ba2a172011-01-12 09:06:06 +00001237QualType
1238ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001239 QualType CanT = getCanonicalType(T);
1240 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001241 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001242
John McCall0953e762009-09-24 19:53:00 +00001243 // If we are composing extended qualifiers together, merge together
1244 // into one ExtQuals node.
1245 QualifierCollector Quals;
1246 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001247
John McCall0953e762009-09-24 19:53:00 +00001248 // If this type already has an address space specified, it cannot get
1249 // another one.
1250 assert(!Quals.hasAddressSpace() &&
1251 "Type cannot be in multiple addr spaces!");
1252 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001253
John McCall0953e762009-09-24 19:53:00 +00001254 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001255}
1256
Chris Lattnerb7d25532009-02-18 22:53:11 +00001257QualType ASTContext::getObjCGCQualType(QualType T,
Jay Foad4ba2a172011-01-12 09:06:06 +00001258 Qualifiers::GC GCAttr) const {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001259 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001260 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001261 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001262
John McCall7f040a92010-12-24 02:08:15 +00001263 if (const PointerType *ptr = T->getAs<PointerType>()) {
1264 QualType Pointee = ptr->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001265 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001266 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1267 return getPointerType(ResultType);
1268 }
1269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
John McCall0953e762009-09-24 19:53:00 +00001271 // If we are composing extended qualifiers together, merge together
1272 // into one ExtQuals node.
1273 QualifierCollector Quals;
1274 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001275
John McCall0953e762009-09-24 19:53:00 +00001276 // If this type already has an ObjCGC specified, it cannot get
1277 // another one.
1278 assert(!Quals.hasObjCGCAttr() &&
1279 "Type cannot have multiple ObjCGCs!");
1280 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001281
John McCall0953e762009-09-24 19:53:00 +00001282 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001283}
Chris Lattnera7674d82007-07-13 22:13:22 +00001284
John McCalle6a365d2010-12-19 02:44:49 +00001285const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1286 FunctionType::ExtInfo Info) {
1287 if (T->getExtInfo() == Info)
1288 return T;
1289
1290 QualType Result;
1291 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1292 Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1293 } else {
1294 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1295 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1296 EPI.ExtInfo = Info;
1297 Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1298 FPT->getNumArgs(), EPI);
1299 }
1300
1301 return cast<FunctionType>(Result.getTypePtr());
1302}
1303
Reid Spencer5f016e22007-07-11 17:01:13 +00001304/// getComplexType - Return the uniqued reference to the type for a complex
1305/// number with the specified element type.
Jay Foad4ba2a172011-01-12 09:06:06 +00001306QualType ASTContext::getComplexType(QualType T) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 // Unique pointers, to guarantee there is only one pointer of a particular
1308 // structure.
1309 llvm::FoldingSetNodeID ID;
1310 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 void *InsertPos = 0;
1313 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1314 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 // If the pointee type isn't canonical, this won't be a canonical type either,
1317 // so fill in the canonical type field.
1318 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001319 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001320 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 // Get the new insert position for the node we care about.
1323 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001324 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 }
John McCall6b304a02009-09-24 23:30:46 +00001326 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 Types.push_back(New);
1328 ComplexTypes.InsertNode(New, InsertPos);
1329 return QualType(New, 0);
1330}
1331
Reid Spencer5f016e22007-07-11 17:01:13 +00001332/// getPointerType - Return the uniqued reference to the type for a pointer to
1333/// the specified type.
Jay Foad4ba2a172011-01-12 09:06:06 +00001334QualType ASTContext::getPointerType(QualType T) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // Unique pointers, to guarantee there is only one pointer of a particular
1336 // structure.
1337 llvm::FoldingSetNodeID ID;
1338 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 void *InsertPos = 0;
1341 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1342 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 // If the pointee type isn't canonical, this won't be a canonical type either,
1345 // so fill in the canonical type field.
1346 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001347 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001348 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 // Get the new insert position for the node we care about.
1351 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001352 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 }
John McCall6b304a02009-09-24 23:30:46 +00001354 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 Types.push_back(New);
1356 PointerTypes.InsertNode(New, InsertPos);
1357 return QualType(New, 0);
1358}
1359
Mike Stump1eb44332009-09-09 15:08:12 +00001360/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001361/// a pointer to the specified block.
Jay Foad4ba2a172011-01-12 09:06:06 +00001362QualType ASTContext::getBlockPointerType(QualType T) const {
Steve Naroff296e8d52008-08-28 19:20:44 +00001363 assert(T->isFunctionType() && "block of function types only");
1364 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001365 // structure.
1366 llvm::FoldingSetNodeID ID;
1367 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Steve Naroff5618bd42008-08-27 16:04:49 +00001369 void *InsertPos = 0;
1370 if (BlockPointerType *PT =
1371 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1372 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001373
1374 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001375 // type either so fill in the canonical type field.
1376 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001377 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001378 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Steve Naroff5618bd42008-08-27 16:04:49 +00001380 // Get the new insert position for the node we care about.
1381 BlockPointerType *NewIP =
1382 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001383 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001384 }
John McCall6b304a02009-09-24 23:30:46 +00001385 BlockPointerType *New
1386 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001387 Types.push_back(New);
1388 BlockPointerTypes.InsertNode(New, InsertPos);
1389 return QualType(New, 0);
1390}
1391
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001392/// getLValueReferenceType - Return the uniqued reference to the type for an
1393/// lvalue reference to the specified type.
Jay Foad4ba2a172011-01-12 09:06:06 +00001394QualType
1395ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 // Unique pointers, to guarantee there is only one pointer of a particular
1397 // structure.
1398 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001399 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001400
1401 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001402 if (LValueReferenceType *RT =
1403 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001405
John McCall54e14c42009-10-22 22:37:11 +00001406 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1407
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 // If the referencee type isn't canonical, this won't be a canonical type
1409 // either, so fill in the canonical type field.
1410 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001411 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1412 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1413 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001414
Reid Spencer5f016e22007-07-11 17:01:13 +00001415 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001416 LValueReferenceType *NewIP =
1417 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001418 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 }
1420
John McCall6b304a02009-09-24 23:30:46 +00001421 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001422 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1423 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001425 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001426
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001427 return QualType(New, 0);
1428}
1429
1430/// getRValueReferenceType - Return the uniqued reference to the type for an
1431/// rvalue reference to the specified type.
Jay Foad4ba2a172011-01-12 09:06:06 +00001432QualType ASTContext::getRValueReferenceType(QualType T) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001433 // Unique pointers, to guarantee there is only one pointer of a particular
1434 // structure.
1435 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001436 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001437
1438 void *InsertPos = 0;
1439 if (RValueReferenceType *RT =
1440 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1441 return QualType(RT, 0);
1442
John McCall54e14c42009-10-22 22:37:11 +00001443 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1444
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001445 // If the referencee type isn't canonical, this won't be a canonical type
1446 // either, so fill in the canonical type field.
1447 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001448 if (InnerRef || !T.isCanonical()) {
1449 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1450 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001451
1452 // Get the new insert position for the node we care about.
1453 RValueReferenceType *NewIP =
1454 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001455 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001456 }
1457
John McCall6b304a02009-09-24 23:30:46 +00001458 RValueReferenceType *New
1459 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001460 Types.push_back(New);
1461 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 return QualType(New, 0);
1463}
1464
Sebastian Redlf30208a2009-01-24 21:16:55 +00001465/// getMemberPointerType - Return the uniqued reference to the type for a
1466/// member pointer to the specified type, in the specified class.
Jay Foad4ba2a172011-01-12 09:06:06 +00001467QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001468 // Unique pointers, to guarantee there is only one pointer of a particular
1469 // structure.
1470 llvm::FoldingSetNodeID ID;
1471 MemberPointerType::Profile(ID, T, Cls);
1472
1473 void *InsertPos = 0;
1474 if (MemberPointerType *PT =
1475 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1476 return QualType(PT, 0);
1477
1478 // If the pointee or class type isn't canonical, this won't be a canonical
1479 // type either, so fill in the canonical type field.
1480 QualType Canonical;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001481 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001482 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1483
1484 // Get the new insert position for the node we care about.
1485 MemberPointerType *NewIP =
1486 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001487 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001488 }
John McCall6b304a02009-09-24 23:30:46 +00001489 MemberPointerType *New
1490 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001491 Types.push_back(New);
1492 MemberPointerTypes.InsertNode(New, InsertPos);
1493 return QualType(New, 0);
1494}
1495
Mike Stump1eb44332009-09-09 15:08:12 +00001496/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001497/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001498QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001499 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001500 ArrayType::ArraySizeModifier ASM,
Abramo Bagnara63e7d252011-01-27 19:55:10 +00001501 unsigned IndexTypeQuals) const {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001502 assert((EltTy->isDependentType() ||
1503 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedman587cbdf2009-05-29 20:17:55 +00001504 "Constant array of VLAs is illegal!");
1505
Chris Lattner38aeec72009-05-13 04:12:56 +00001506 // Convert the array size into a canonical width matching the pointer size for
1507 // the target.
1508 llvm::APInt ArySize(ArySizeIn);
Jay Foad9f71a8f2010-12-07 08:25:34 +00001509 ArySize =
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001510 ArySize.zextOrTrunc(Target.getPointerWidth(getTargetAddressSpace(EltTy)));
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 llvm::FoldingSetNodeID ID;
Abramo Bagnara63e7d252011-01-27 19:55:10 +00001513 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001516 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001517 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001518 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001519
John McCall3b657512011-01-19 10:06:00 +00001520 // If the element type isn't canonical or has qualifiers, this won't
1521 // be a canonical type either, so fill in the canonical type field.
1522 QualType Canon;
1523 if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
1524 SplitQualType canonSplit = getCanonicalType(EltTy).split();
1525 Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize,
Abramo Bagnara63e7d252011-01-27 19:55:10 +00001526 ASM, IndexTypeQuals);
John McCall3b657512011-01-19 10:06:00 +00001527 Canon = getQualifiedType(Canon, canonSplit.second);
1528
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001530 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001531 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001532 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
John McCall6b304a02009-09-24 23:30:46 +00001535 ConstantArrayType *New = new(*this,TypeAlignment)
Abramo Bagnara63e7d252011-01-27 19:55:10 +00001536 ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001537 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001538 Types.push_back(New);
1539 return QualType(New, 0);
1540}
1541
John McCallce889032011-01-18 08:40:38 +00001542/// getVariableArrayDecayedType - Turns the given type, which may be
1543/// variably-modified, into the corresponding type with all the known
1544/// sizes replaced with [*].
1545QualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1546 // Vastly most common case.
1547 if (!type->isVariablyModifiedType()) return type;
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00001548
John McCallce889032011-01-18 08:40:38 +00001549 QualType result;
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00001550
John McCallce889032011-01-18 08:40:38 +00001551 SplitQualType split = type.getSplitDesugaredType();
1552 const Type *ty = split.first;
1553 switch (ty->getTypeClass()) {
1554#define TYPE(Class, Base)
1555#define ABSTRACT_TYPE(Class, Base)
1556#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1557#include "clang/AST/TypeNodes.def"
1558 llvm_unreachable("didn't desugar past all non-canonical types?");
1559
1560 // These types should never be variably-modified.
1561 case Type::Builtin:
1562 case Type::Complex:
1563 case Type::Vector:
1564 case Type::ExtVector:
1565 case Type::DependentSizedExtVector:
1566 case Type::ObjCObject:
1567 case Type::ObjCInterface:
1568 case Type::ObjCObjectPointer:
1569 case Type::Record:
1570 case Type::Enum:
1571 case Type::UnresolvedUsing:
1572 case Type::TypeOfExpr:
1573 case Type::TypeOf:
1574 case Type::Decltype:
1575 case Type::DependentName:
1576 case Type::InjectedClassName:
1577 case Type::TemplateSpecialization:
1578 case Type::DependentTemplateSpecialization:
1579 case Type::TemplateTypeParm:
1580 case Type::SubstTemplateTypeParmPack:
Richard Smith34b41d92011-02-20 03:19:35 +00001581 case Type::Auto:
John McCallce889032011-01-18 08:40:38 +00001582 case Type::PackExpansion:
1583 llvm_unreachable("type should never be variably-modified");
1584
1585 // These types can be variably-modified but should never need to
1586 // further decay.
1587 case Type::FunctionNoProto:
1588 case Type::FunctionProto:
1589 case Type::BlockPointer:
1590 case Type::MemberPointer:
1591 return type;
1592
1593 // These types can be variably-modified. All these modifications
1594 // preserve structure except as noted by comments.
1595 // TODO: if we ever care about optimizing VLAs, there are no-op
1596 // optimizations available here.
1597 case Type::Pointer:
1598 result = getPointerType(getVariableArrayDecayedType(
1599 cast<PointerType>(ty)->getPointeeType()));
1600 break;
1601
1602 case Type::LValueReference: {
1603 const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
1604 result = getLValueReferenceType(
1605 getVariableArrayDecayedType(lv->getPointeeType()),
1606 lv->isSpelledAsLValue());
1607 break;
1608 }
1609
1610 case Type::RValueReference: {
1611 const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
1612 result = getRValueReferenceType(
1613 getVariableArrayDecayedType(lv->getPointeeType()));
1614 break;
1615 }
1616
1617 case Type::ConstantArray: {
1618 const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
1619 result = getConstantArrayType(
1620 getVariableArrayDecayedType(cat->getElementType()),
1621 cat->getSize(),
1622 cat->getSizeModifier(),
1623 cat->getIndexTypeCVRQualifiers());
1624 break;
1625 }
1626
1627 case Type::DependentSizedArray: {
1628 const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
1629 result = getDependentSizedArrayType(
1630 getVariableArrayDecayedType(dat->getElementType()),
1631 dat->getSizeExpr(),
1632 dat->getSizeModifier(),
1633 dat->getIndexTypeCVRQualifiers(),
1634 dat->getBracketsRange());
1635 break;
1636 }
1637
1638 // Turn incomplete types into [*] types.
1639 case Type::IncompleteArray: {
1640 const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
1641 result = getVariableArrayType(
1642 getVariableArrayDecayedType(iat->getElementType()),
1643 /*size*/ 0,
1644 ArrayType::Normal,
1645 iat->getIndexTypeCVRQualifiers(),
1646 SourceRange());
1647 break;
1648 }
1649
1650 // Turn VLA types into [*] types.
1651 case Type::VariableArray: {
1652 const VariableArrayType *vat = cast<VariableArrayType>(ty);
1653 result = getVariableArrayType(
1654 getVariableArrayDecayedType(vat->getElementType()),
1655 /*size*/ 0,
1656 ArrayType::Star,
1657 vat->getIndexTypeCVRQualifiers(),
1658 vat->getBracketsRange());
1659 break;
1660 }
1661 }
1662
1663 // Apply the top-level qualifiers from the original.
1664 return getQualifiedType(result, split.second);
1665}
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00001666
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001667/// getVariableArrayType - Returns a non-unique reference to the type for a
1668/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001669QualType ASTContext::getVariableArrayType(QualType EltTy,
1670 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001671 ArrayType::ArraySizeModifier ASM,
Abramo Bagnara63e7d252011-01-27 19:55:10 +00001672 unsigned IndexTypeQuals,
Jay Foad4ba2a172011-01-12 09:06:06 +00001673 SourceRange Brackets) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001674 // Since we don't unique expressions, it isn't possible to unique VLA's
1675 // that have an expression provided for their size.
John McCall3b657512011-01-19 10:06:00 +00001676 QualType Canon;
Douglas Gregor715e9c82010-05-23 16:10:32 +00001677
John McCall3b657512011-01-19 10:06:00 +00001678 // Be sure to pull qualifiers off the element type.
1679 if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
1680 SplitQualType canonSplit = getCanonicalType(EltTy).split();
1681 Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM,
Abramo Bagnara63e7d252011-01-27 19:55:10 +00001682 IndexTypeQuals, Brackets);
John McCall3b657512011-01-19 10:06:00 +00001683 Canon = getQualifiedType(Canon, canonSplit.second);
Douglas Gregor715e9c82010-05-23 16:10:32 +00001684 }
1685
John McCall6b304a02009-09-24 23:30:46 +00001686 VariableArrayType *New = new(*this, TypeAlignment)
Abramo Bagnara63e7d252011-01-27 19:55:10 +00001687 VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001688
1689 VariableArrayTypes.push_back(New);
1690 Types.push_back(New);
1691 return QualType(New, 0);
1692}
1693
Douglas Gregor898574e2008-12-05 23:32:09 +00001694/// getDependentSizedArrayType - Returns a non-unique reference to
1695/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001696/// type.
John McCall3b657512011-01-19 10:06:00 +00001697QualType ASTContext::getDependentSizedArrayType(QualType elementType,
1698 Expr *numElements,
Douglas Gregor898574e2008-12-05 23:32:09 +00001699 ArrayType::ArraySizeModifier ASM,
John McCall3b657512011-01-19 10:06:00 +00001700 unsigned elementTypeQuals,
1701 SourceRange brackets) const {
1702 assert((!numElements || numElements->isTypeDependent() ||
1703 numElements->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001704 "Size must be type- or value-dependent!");
1705
John McCall3b657512011-01-19 10:06:00 +00001706 // Dependently-sized array types that do not have a specified number
1707 // of elements will have their sizes deduced from a dependent
1708 // initializer. We do no canonicalization here at all, which is okay
1709 // because they can't be used in most locations.
1710 if (!numElements) {
1711 DependentSizedArrayType *newType
1712 = new (*this, TypeAlignment)
1713 DependentSizedArrayType(*this, elementType, QualType(),
1714 numElements, ASM, elementTypeQuals,
1715 brackets);
1716 Types.push_back(newType);
1717 return QualType(newType, 0);
1718 }
1719
1720 // Otherwise, we actually build a new type every time, but we
1721 // also build a canonical type.
1722
1723 SplitQualType canonElementType = getCanonicalType(elementType).split();
1724
1725 void *insertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00001726 llvm::FoldingSetNodeID ID;
John McCall3b657512011-01-19 10:06:00 +00001727 DependentSizedArrayType::Profile(ID, *this,
1728 QualType(canonElementType.first, 0),
1729 ASM, elementTypeQuals, numElements);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001730
John McCall3b657512011-01-19 10:06:00 +00001731 // Look for an existing type with these properties.
1732 DependentSizedArrayType *canonTy =
1733 DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001734
John McCall3b657512011-01-19 10:06:00 +00001735 // If we don't have one, build one.
1736 if (!canonTy) {
1737 canonTy = new (*this, TypeAlignment)
1738 DependentSizedArrayType(*this, QualType(canonElementType.first, 0),
1739 QualType(), numElements, ASM, elementTypeQuals,
1740 brackets);
1741 DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
1742 Types.push_back(canonTy);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001743 }
1744
John McCall3b657512011-01-19 10:06:00 +00001745 // Apply qualifiers from the element type to the array.
1746 QualType canon = getQualifiedType(QualType(canonTy,0),
1747 canonElementType.second);
Mike Stump1eb44332009-09-09 15:08:12 +00001748
John McCall3b657512011-01-19 10:06:00 +00001749 // If we didn't need extra canonicalization for the element type,
1750 // then just use that as our result.
1751 if (QualType(canonElementType.first, 0) == elementType)
1752 return canon;
1753
1754 // Otherwise, we need to build a type which follows the spelling
1755 // of the element type.
1756 DependentSizedArrayType *sugaredType
1757 = new (*this, TypeAlignment)
1758 DependentSizedArrayType(*this, elementType, canon, numElements,
1759 ASM, elementTypeQuals, brackets);
1760 Types.push_back(sugaredType);
1761 return QualType(sugaredType, 0);
Douglas Gregor898574e2008-12-05 23:32:09 +00001762}
1763
John McCall3b657512011-01-19 10:06:00 +00001764QualType ASTContext::getIncompleteArrayType(QualType elementType,
Eli Friedmanc5773c42008-02-15 18:16:39 +00001765 ArrayType::ArraySizeModifier ASM,
John McCall3b657512011-01-19 10:06:00 +00001766 unsigned elementTypeQuals) const {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001767 llvm::FoldingSetNodeID ID;
John McCall3b657512011-01-19 10:06:00 +00001768 IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001769
John McCall3b657512011-01-19 10:06:00 +00001770 void *insertPos = 0;
1771 if (IncompleteArrayType *iat =
1772 IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
1773 return QualType(iat, 0);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001774
1775 // If the element type isn't canonical, this won't be a canonical type
John McCall3b657512011-01-19 10:06:00 +00001776 // either, so fill in the canonical type field. We also have to pull
1777 // qualifiers off the element type.
1778 QualType canon;
Eli Friedmanc5773c42008-02-15 18:16:39 +00001779
John McCall3b657512011-01-19 10:06:00 +00001780 if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
1781 SplitQualType canonSplit = getCanonicalType(elementType).split();
1782 canon = getIncompleteArrayType(QualType(canonSplit.first, 0),
1783 ASM, elementTypeQuals);
1784 canon = getQualifiedType(canon, canonSplit.second);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001785
1786 // Get the new insert position for the node we care about.
John McCall3b657512011-01-19 10:06:00 +00001787 IncompleteArrayType *existing =
1788 IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
1789 assert(!existing && "Shouldn't be in the map!"); (void) existing;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001790 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001791
John McCall3b657512011-01-19 10:06:00 +00001792 IncompleteArrayType *newType = new (*this, TypeAlignment)
1793 IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001794
John McCall3b657512011-01-19 10:06:00 +00001795 IncompleteArrayTypes.InsertNode(newType, insertPos);
1796 Types.push_back(newType);
1797 return QualType(newType, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001798}
1799
Steve Naroff73322922007-07-18 18:00:27 +00001800/// getVectorType - Return the unique reference to a vector type of
1801/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001802QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Jay Foad4ba2a172011-01-12 09:06:06 +00001803 VectorType::VectorKind VecKind) const {
John McCall3b657512011-01-19 10:06:00 +00001804 assert(vecType->isBuiltinType());
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 // Check if we've already instantiated a vector of this type.
1807 llvm::FoldingSetNodeID ID;
Bob Wilsone86d78c2010-11-10 21:56:12 +00001808 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner788b0fd2010-06-23 06:00:24 +00001809
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 void *InsertPos = 0;
1811 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1812 return QualType(VTP, 0);
1813
1814 // If the element type isn't canonical, this won't be a canonical type either,
1815 // so fill in the canonical type field.
1816 QualType Canonical;
Douglas Gregor255210e2010-08-06 10:14:59 +00001817 if (!vecType.isCanonical()) {
Bob Wilson231da7e2010-11-16 00:32:20 +00001818 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 // Get the new insert position for the node we care about.
1821 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001822 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001823 }
John McCall6b304a02009-09-24 23:30:46 +00001824 VectorType *New = new (*this, TypeAlignment)
Bob Wilsone86d78c2010-11-10 21:56:12 +00001825 VectorType(vecType, NumElts, Canonical, VecKind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001826 VectorTypes.InsertNode(New, InsertPos);
1827 Types.push_back(New);
1828 return QualType(New, 0);
1829}
1830
Nate Begeman213541a2008-04-18 23:10:10 +00001831/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001832/// the specified element type and size. VectorType must be a built-in type.
Jay Foad4ba2a172011-01-12 09:06:06 +00001833QualType
1834ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
John McCall3b657512011-01-19 10:06:00 +00001835 assert(vecType->isBuiltinType());
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Steve Naroff73322922007-07-18 18:00:27 +00001837 // Check if we've already instantiated a vector of this type.
1838 llvm::FoldingSetNodeID ID;
Chris Lattner788b0fd2010-06-23 06:00:24 +00001839 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsone86d78c2010-11-10 21:56:12 +00001840 VectorType::GenericVector);
Steve Naroff73322922007-07-18 18:00:27 +00001841 void *InsertPos = 0;
1842 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1843 return QualType(VTP, 0);
1844
1845 // If the element type isn't canonical, this won't be a canonical type either,
1846 // so fill in the canonical type field.
1847 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001848 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001849 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Steve Naroff73322922007-07-18 18:00:27 +00001851 // Get the new insert position for the node we care about.
1852 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001853 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001854 }
John McCall6b304a02009-09-24 23:30:46 +00001855 ExtVectorType *New = new (*this, TypeAlignment)
1856 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001857 VectorTypes.InsertNode(New, InsertPos);
1858 Types.push_back(New);
1859 return QualType(New, 0);
1860}
1861
Jay Foad4ba2a172011-01-12 09:06:06 +00001862QualType
1863ASTContext::getDependentSizedExtVectorType(QualType vecType,
1864 Expr *SizeExpr,
1865 SourceLocation AttrLoc) const {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001866 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001867 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001868 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001870 void *InsertPos = 0;
1871 DependentSizedExtVectorType *Canon
1872 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1873 DependentSizedExtVectorType *New;
1874 if (Canon) {
1875 // We already have a canonical version of this array type; use it as
1876 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001877 New = new (*this, TypeAlignment)
1878 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1879 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001880 } else {
1881 QualType CanonVecTy = getCanonicalType(vecType);
1882 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001883 New = new (*this, TypeAlignment)
1884 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1885 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001886
1887 DependentSizedExtVectorType *CanonCheck
1888 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1889 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1890 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001891 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1892 } else {
1893 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1894 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001895 New = new (*this, TypeAlignment)
1896 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001897 }
1898 }
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001900 Types.push_back(New);
1901 return QualType(New, 0);
1902}
1903
Douglas Gregor72564e72009-02-26 23:50:07 +00001904/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001905///
Jay Foad4ba2a172011-01-12 09:06:06 +00001906QualType
1907ASTContext::getFunctionNoProtoType(QualType ResultTy,
1908 const FunctionType::ExtInfo &Info) const {
Roman Divackycfe9af22011-03-01 17:40:53 +00001909 const CallingConv DefaultCC = Info.getCC();
1910 const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
1911 CC_X86StdCall : DefaultCC;
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 // Unique functions, to guarantee there is only one function of a particular
1913 // structure.
1914 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +00001915 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001918 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001919 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001923 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001924 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindola264ba482010-03-30 20:24:48 +00001925 Canonical =
1926 getFunctionNoProtoType(getCanonicalType(ResultTy),
1927 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001930 FunctionNoProtoType *NewIP =
1931 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001932 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 }
Mike Stump1eb44332009-09-09 15:08:12 +00001934
Roman Divackycfe9af22011-03-01 17:40:53 +00001935 FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
John McCall6b304a02009-09-24 23:30:46 +00001936 FunctionNoProtoType *New = new (*this, TypeAlignment)
Roman Divackycfe9af22011-03-01 17:40:53 +00001937 FunctionNoProtoType(ResultTy, Canonical, newInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001939 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001940 return QualType(New, 0);
1941}
1942
1943/// getFunctionType - Return a normal function type with a typed argument
1944/// list. isVariadic indicates whether the argument list includes '...'.
Jay Foad4ba2a172011-01-12 09:06:06 +00001945QualType
1946ASTContext::getFunctionType(QualType ResultTy,
1947 const QualType *ArgArray, unsigned NumArgs,
1948 const FunctionProtoType::ExtProtoInfo &EPI) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 // Unique functions, to guarantee there is only one function of a particular
1950 // structure.
1951 llvm::FoldingSetNodeID ID;
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001952 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
Reid Spencer5f016e22007-07-11 17:01:13 +00001953
1954 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001955 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001956 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001958
1959 // Determine whether the type being created is already canonical or not.
Sebastian Redl8b5b4092011-03-06 10:52:04 +00001960 bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001961 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001962 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001963 isCanonical = false;
1964
Roman Divackycfe9af22011-03-01 17:40:53 +00001965 const CallingConv DefaultCC = EPI.ExtInfo.getCC();
1966 const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
1967 CC_X86StdCall : DefaultCC;
John McCalle23cf432010-12-14 08:05:40 +00001968
Reid Spencer5f016e22007-07-11 17:01:13 +00001969 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001970 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001971 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001972 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001973 llvm::SmallVector<QualType, 16> CanonicalArgs;
1974 CanonicalArgs.reserve(NumArgs);
1975 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001976 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001977
John McCalle23cf432010-12-14 08:05:40 +00001978 FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
Sebastian Redl8b5b4092011-03-06 10:52:04 +00001979 CanonicalEPI.ExceptionSpecType = EST_None;
1980 CanonicalEPI.NumExceptions = 0;
John McCalle23cf432010-12-14 08:05:40 +00001981 CanonicalEPI.ExtInfo
1982 = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
1983
Chris Lattnerf52ab252008-04-06 22:59:24 +00001984 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001985 CanonicalArgs.data(), NumArgs,
John McCalle23cf432010-12-14 08:05:40 +00001986 CanonicalEPI);
Sebastian Redl465226e2009-05-27 22:11:52 +00001987
Reid Spencer5f016e22007-07-11 17:01:13 +00001988 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001989 FunctionProtoType *NewIP =
1990 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001991 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001992 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001993
Douglas Gregor72564e72009-02-26 23:50:07 +00001994 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001995 // for two variable size arrays (for parameter and exception types) at the
Sebastian Redl60618fa2011-03-12 11:50:43 +00001996 // end of them. Instead of the exception types, there could be a noexcept
1997 // expression and a context pointer.
John McCalle23cf432010-12-14 08:05:40 +00001998 size_t Size = sizeof(FunctionProtoType) +
Sebastian Redl60618fa2011-03-12 11:50:43 +00001999 NumArgs * sizeof(QualType);
2000 if (EPI.ExceptionSpecType == EST_Dynamic)
2001 Size += EPI.NumExceptions * sizeof(QualType);
2002 else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00002003 Size += sizeof(Expr*);
Sebastian Redl60618fa2011-03-12 11:50:43 +00002004 }
John McCalle23cf432010-12-14 08:05:40 +00002005 FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
Roman Divackycfe9af22011-03-01 17:40:53 +00002006 FunctionProtoType::ExtProtoInfo newEPI = EPI;
2007 newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
Sebastian Redl8026f6d2011-03-13 17:09:40 +00002008 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
Reid Spencer5f016e22007-07-11 17:01:13 +00002009 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00002010 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00002011 return QualType(FTP, 0);
2012}
2013
John McCall3cb0ebd2010-03-10 03:28:59 +00002014#ifndef NDEBUG
2015static bool NeedsInjectedClassNameType(const RecordDecl *D) {
2016 if (!isa<CXXRecordDecl>(D)) return false;
2017 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
2018 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
2019 return true;
2020 if (RD->getDescribedClassTemplate() &&
2021 !isa<ClassTemplateSpecializationDecl>(RD))
2022 return true;
2023 return false;
2024}
2025#endif
2026
2027/// getInjectedClassNameType - Return the unique reference to the
2028/// injected class name type for the specified templated declaration.
2029QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
Jay Foad4ba2a172011-01-12 09:06:06 +00002030 QualType TST) const {
John McCall3cb0ebd2010-03-10 03:28:59 +00002031 assert(NeedsInjectedClassNameType(Decl));
2032 if (Decl->TypeForDecl) {
2033 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00002034 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCall3cb0ebd2010-03-10 03:28:59 +00002035 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
2036 Decl->TypeForDecl = PrevDecl->TypeForDecl;
2037 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2038 } else {
John McCallf4c73712011-01-19 06:33:43 +00002039 Type *newType =
John McCall31f17ec2010-04-27 00:57:59 +00002040 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCallf4c73712011-01-19 06:33:43 +00002041 Decl->TypeForDecl = newType;
2042 Types.push_back(newType);
John McCall3cb0ebd2010-03-10 03:28:59 +00002043 }
2044 return QualType(Decl->TypeForDecl, 0);
2045}
2046
Douglas Gregor2ce52f32008-04-13 21:07:44 +00002047/// getTypeDeclType - Return the unique reference to the type for the
2048/// specified type declaration.
Jay Foad4ba2a172011-01-12 09:06:06 +00002049QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00002050 assert(Decl && "Passed null for Decl param");
John McCallbecb8d52010-03-10 06:48:02 +00002051 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Richard Smith162e1c12011-04-15 14:24:37 +00002053 if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00002054 return getTypedefType(Typedef);
John McCallbecb8d52010-03-10 06:48:02 +00002055
John McCallbecb8d52010-03-10 06:48:02 +00002056 assert(!isa<TemplateTypeParmDecl>(Decl) &&
2057 "Template type parameter types are always available.");
2058
John McCall19c85762010-02-16 03:57:14 +00002059 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00002060 assert(!Record->getPreviousDeclaration() &&
2061 "struct/union has previous declaration");
2062 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00002063 return getRecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00002064 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00002065 assert(!Enum->getPreviousDeclaration() &&
2066 "enum has previous declaration");
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00002067 return getEnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00002068 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00002069 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
John McCallf4c73712011-01-19 06:33:43 +00002070 Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2071 Decl->TypeForDecl = newType;
2072 Types.push_back(newType);
Mike Stump9fdbab32009-07-31 02:02:20 +00002073 } else
John McCallbecb8d52010-03-10 06:48:02 +00002074 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00002075
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00002076 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00002077}
2078
Reid Spencer5f016e22007-07-11 17:01:13 +00002079/// getTypedefType - Return the unique reference to the type for the
Richard Smith162e1c12011-04-15 14:24:37 +00002080/// specified typedef name decl.
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002081QualType
Richard Smith162e1c12011-04-15 14:24:37 +00002082ASTContext::getTypedefType(const TypedefNameDecl *Decl,
2083 QualType Canonical) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00002084 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002085
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002086 if (Canonical.isNull())
2087 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCallf4c73712011-01-19 06:33:43 +00002088 TypedefType *newType = new(*this, TypeAlignment)
John McCall6b304a02009-09-24 23:30:46 +00002089 TypedefType(Type::Typedef, Decl, Canonical);
John McCallf4c73712011-01-19 06:33:43 +00002090 Decl->TypeForDecl = newType;
2091 Types.push_back(newType);
2092 return QualType(newType, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +00002093}
2094
Jay Foad4ba2a172011-01-12 09:06:06 +00002095QualType ASTContext::getRecordType(const RecordDecl *Decl) const {
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00002096 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2097
2098 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
2099 if (PrevDecl->TypeForDecl)
2100 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2101
John McCallf4c73712011-01-19 06:33:43 +00002102 RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2103 Decl->TypeForDecl = newType;
2104 Types.push_back(newType);
2105 return QualType(newType, 0);
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00002106}
2107
Jay Foad4ba2a172011-01-12 09:06:06 +00002108QualType ASTContext::getEnumType(const EnumDecl *Decl) const {
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00002109 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2110
2111 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
2112 if (PrevDecl->TypeForDecl)
2113 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2114
John McCallf4c73712011-01-19 06:33:43 +00002115 EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2116 Decl->TypeForDecl = newType;
2117 Types.push_back(newType);
2118 return QualType(newType, 0);
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00002119}
2120
John McCall9d156a72011-01-06 01:58:22 +00002121QualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
2122 QualType modifiedType,
2123 QualType equivalentType) {
2124 llvm::FoldingSetNodeID id;
2125 AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
2126
2127 void *insertPos = 0;
2128 AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
2129 if (type) return QualType(type, 0);
2130
2131 QualType canon = getCanonicalType(equivalentType);
2132 type = new (*this, TypeAlignment)
2133 AttributedType(canon, attrKind, modifiedType, equivalentType);
2134
2135 Types.push_back(type);
2136 AttributedTypes.InsertNode(type, insertPos);
2137
2138 return QualType(type, 0);
2139}
2140
2141
John McCall49a832b2009-10-18 09:09:24 +00002142/// \brief Retrieve a substitution-result type.
2143QualType
2144ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
Jay Foad4ba2a172011-01-12 09:06:06 +00002145 QualType Replacement) const {
John McCall467b27b2009-10-22 20:10:53 +00002146 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00002147 && "replacement types must always be canonical");
2148
2149 llvm::FoldingSetNodeID ID;
2150 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
2151 void *InsertPos = 0;
2152 SubstTemplateTypeParmType *SubstParm
2153 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2154
2155 if (!SubstParm) {
2156 SubstParm = new (*this, TypeAlignment)
2157 SubstTemplateTypeParmType(Parm, Replacement);
2158 Types.push_back(SubstParm);
2159 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2160 }
2161
2162 return QualType(SubstParm, 0);
2163}
2164
Douglas Gregorc3069d62011-01-14 02:55:32 +00002165/// \brief Retrieve a
2166QualType ASTContext::getSubstTemplateTypeParmPackType(
2167 const TemplateTypeParmType *Parm,
2168 const TemplateArgument &ArgPack) {
2169#ifndef NDEBUG
2170 for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2171 PEnd = ArgPack.pack_end();
2172 P != PEnd; ++P) {
2173 assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2174 assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2175 }
2176#endif
2177
2178 llvm::FoldingSetNodeID ID;
2179 SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2180 void *InsertPos = 0;
2181 if (SubstTemplateTypeParmPackType *SubstParm
2182 = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2183 return QualType(SubstParm, 0);
2184
2185 QualType Canon;
2186 if (!Parm->isCanonicalUnqualified()) {
2187 Canon = getCanonicalType(QualType(Parm, 0));
2188 Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2189 ArgPack);
2190 SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2191 }
2192
2193 SubstTemplateTypeParmPackType *SubstParm
2194 = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2195 ArgPack);
2196 Types.push_back(SubstParm);
2197 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2198 return QualType(SubstParm, 0);
2199}
2200
Douglas Gregorfab9d672009-02-05 23:33:38 +00002201/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00002202/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00002203/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00002204QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00002205 bool ParameterPack,
Chandler Carruth4fb86f82011-05-01 00:51:33 +00002206 TemplateTypeParmDecl *TTPDecl) const {
Douglas Gregorfab9d672009-02-05 23:33:38 +00002207 llvm::FoldingSetNodeID ID;
Chandler Carruth4fb86f82011-05-01 00:51:33 +00002208 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
Douglas Gregorfab9d672009-02-05 23:33:38 +00002209 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002210 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00002211 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2212
2213 if (TypeParm)
2214 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002215
Chandler Carruth4fb86f82011-05-01 00:51:33 +00002216 if (TTPDecl) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +00002217 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Chandler Carruth4fb86f82011-05-01 00:51:33 +00002218 TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002219
2220 TemplateTypeParmType *TypeCheck
2221 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2222 assert(!TypeCheck && "Template type parameter canonical type broken");
2223 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00002224 } else
John McCall6b304a02009-09-24 23:30:46 +00002225 TypeParm = new (*this, TypeAlignment)
2226 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00002227
2228 Types.push_back(TypeParm);
2229 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2230
2231 return QualType(TypeParm, 0);
2232}
2233
John McCall3cb0ebd2010-03-10 03:28:59 +00002234TypeSourceInfo *
2235ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
2236 SourceLocation NameLoc,
2237 const TemplateArgumentListInfo &Args,
Jay Foad4ba2a172011-01-12 09:06:06 +00002238 QualType CanonType) const {
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002239 assert(!Name.getAsDependentTemplateName() &&
2240 "No dependent template names here!");
John McCall3cb0ebd2010-03-10 03:28:59 +00002241 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
2242
2243 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
2244 TemplateSpecializationTypeLoc TL
2245 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
2246 TL.setTemplateNameLoc(NameLoc);
2247 TL.setLAngleLoc(Args.getLAngleLoc());
2248 TL.setRAngleLoc(Args.getRAngleLoc());
2249 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2250 TL.setArgLocInfo(i, Args[i].getLocInfo());
2251 return DI;
2252}
2253
Mike Stump1eb44332009-09-09 15:08:12 +00002254QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00002255ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00002256 const TemplateArgumentListInfo &Args,
Jay Foad4ba2a172011-01-12 09:06:06 +00002257 QualType Canon) const {
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002258 assert(!Template.getAsDependentTemplateName() &&
2259 "No dependent template names here!");
2260
John McCalld5532b62009-11-23 01:53:49 +00002261 unsigned NumArgs = Args.size();
2262
John McCall833ca992009-10-29 08:12:44 +00002263 llvm::SmallVector<TemplateArgument, 4> ArgVec;
2264 ArgVec.reserve(NumArgs);
2265 for (unsigned i = 0; i != NumArgs; ++i)
2266 ArgVec.push_back(Args[i].getArgument());
2267
John McCall31f17ec2010-04-27 00:57:59 +00002268 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00002269 Canon);
John McCall833ca992009-10-29 08:12:44 +00002270}
2271
2272QualType
2273ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002274 const TemplateArgument *Args,
2275 unsigned NumArgs,
Jay Foad4ba2a172011-01-12 09:06:06 +00002276 QualType Canon) const {
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002277 assert(!Template.getAsDependentTemplateName() &&
2278 "No dependent template names here!");
Douglas Gregor0f0ea2a2011-03-03 17:04:51 +00002279 // Look through qualified template names.
2280 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2281 Template = TemplateName(QTN->getTemplateDecl());
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002282
Douglas Gregorb88e8882009-07-30 17:40:51 +00002283 if (!Canon.isNull())
2284 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002285 else
2286 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregorfc705b82009-02-26 22:19:44 +00002287
Douglas Gregor1275ae02009-07-28 23:00:59 +00002288 // Allocate the (non-canonical) template specialization type, but don't
2289 // try to unique it: these types typically have location information that
2290 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00002291 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00002292 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00002293 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00002294 TemplateSpecializationType *Spec
John McCallef990012010-06-11 11:07:21 +00002295 = new (Mem) TemplateSpecializationType(Template,
John McCall31f17ec2010-04-27 00:57:59 +00002296 Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00002297 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00002298
Douglas Gregor55f6b142009-02-09 18:46:07 +00002299 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00002300 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002301}
2302
Mike Stump1eb44332009-09-09 15:08:12 +00002303QualType
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002304ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2305 const TemplateArgument *Args,
Jay Foad4ba2a172011-01-12 09:06:06 +00002306 unsigned NumArgs) const {
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002307 assert(!Template.getAsDependentTemplateName() &&
2308 "No dependent template names here!");
Douglas Gregor0f0ea2a2011-03-03 17:04:51 +00002309 // Look through qualified template names.
2310 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2311 Template = TemplateName(QTN->getTemplateDecl());
Douglas Gregor7c3179c2011-02-28 18:50:33 +00002312
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002313 // Build the canonical template specialization type.
2314 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2315 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2316 CanonArgs.reserve(NumArgs);
2317 for (unsigned I = 0; I != NumArgs; ++I)
2318 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2319
2320 // Determine whether this canonical template specialization type already
2321 // exists.
2322 llvm::FoldingSetNodeID ID;
2323 TemplateSpecializationType::Profile(ID, CanonTemplate,
2324 CanonArgs.data(), NumArgs, *this);
2325
2326 void *InsertPos = 0;
2327 TemplateSpecializationType *Spec
2328 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2329
2330 if (!Spec) {
2331 // Allocate a new canonical template specialization type.
2332 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2333 sizeof(TemplateArgument) * NumArgs),
2334 TypeAlignment);
2335 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2336 CanonArgs.data(), NumArgs,
2337 QualType());
2338 Types.push_back(Spec);
2339 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2340 }
2341
2342 assert(Spec->isDependentType() &&
2343 "Non-dependent template-id type must have a canonical type");
2344 return QualType(Spec, 0);
2345}
2346
2347QualType
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002348ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2349 NestedNameSpecifier *NNS,
Jay Foad4ba2a172011-01-12 09:06:06 +00002350 QualType NamedType) const {
Douglas Gregore4e5b052009-03-19 00:18:19 +00002351 llvm::FoldingSetNodeID ID;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002352 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002353
2354 void *InsertPos = 0;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002355 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002356 if (T)
2357 return QualType(T, 0);
2358
Douglas Gregor789b1f62010-02-04 18:10:26 +00002359 QualType Canon = NamedType;
2360 if (!Canon.isCanonical()) {
2361 Canon = getCanonicalType(NamedType);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002362 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2363 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregor789b1f62010-02-04 18:10:26 +00002364 (void)CheckT;
2365 }
2366
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002367 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002368 Types.push_back(T);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002369 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002370 return QualType(T, 0);
2371}
2372
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002373QualType
Jay Foad4ba2a172011-01-12 09:06:06 +00002374ASTContext::getParenType(QualType InnerType) const {
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002375 llvm::FoldingSetNodeID ID;
2376 ParenType::Profile(ID, InnerType);
2377
2378 void *InsertPos = 0;
2379 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2380 if (T)
2381 return QualType(T, 0);
2382
2383 QualType Canon = InnerType;
2384 if (!Canon.isCanonical()) {
2385 Canon = getCanonicalType(InnerType);
2386 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2387 assert(!CheckT && "Paren canonical type broken");
2388 (void)CheckT;
2389 }
2390
2391 T = new (*this) ParenType(InnerType, Canon);
2392 Types.push_back(T);
2393 ParenTypes.InsertNode(T, InsertPos);
2394 return QualType(T, 0);
2395}
2396
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002397QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2398 NestedNameSpecifier *NNS,
2399 const IdentifierInfo *Name,
Jay Foad4ba2a172011-01-12 09:06:06 +00002400 QualType Canon) const {
Douglas Gregord57959a2009-03-27 23:10:48 +00002401 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2402
2403 if (Canon.isNull()) {
2404 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002405 ElaboratedTypeKeyword CanonKeyword = Keyword;
2406 if (Keyword == ETK_None)
2407 CanonKeyword = ETK_Typename;
2408
2409 if (CanonNNS != NNS || CanonKeyword != Keyword)
2410 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002411 }
2412
2413 llvm::FoldingSetNodeID ID;
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002414 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002415
2416 void *InsertPos = 0;
Douglas Gregor4714c122010-03-31 17:34:00 +00002417 DependentNameType *T
2418 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregord57959a2009-03-27 23:10:48 +00002419 if (T)
2420 return QualType(T, 0);
2421
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002422 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregord57959a2009-03-27 23:10:48 +00002423 Types.push_back(T);
Douglas Gregor4714c122010-03-31 17:34:00 +00002424 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002425 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00002426}
2427
Mike Stump1eb44332009-09-09 15:08:12 +00002428QualType
John McCall33500952010-06-11 00:33:02 +00002429ASTContext::getDependentTemplateSpecializationType(
2430 ElaboratedTypeKeyword Keyword,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002431 NestedNameSpecifier *NNS,
John McCall33500952010-06-11 00:33:02 +00002432 const IdentifierInfo *Name,
Jay Foad4ba2a172011-01-12 09:06:06 +00002433 const TemplateArgumentListInfo &Args) const {
John McCall33500952010-06-11 00:33:02 +00002434 // TODO: avoid this copy
2435 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2436 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2437 ArgCopy.push_back(Args[I].getArgument());
2438 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2439 ArgCopy.size(),
2440 ArgCopy.data());
2441}
2442
2443QualType
2444ASTContext::getDependentTemplateSpecializationType(
2445 ElaboratedTypeKeyword Keyword,
2446 NestedNameSpecifier *NNS,
2447 const IdentifierInfo *Name,
2448 unsigned NumArgs,
Jay Foad4ba2a172011-01-12 09:06:06 +00002449 const TemplateArgument *Args) const {
Douglas Gregoraa2187d2011-02-28 00:04:36 +00002450 assert((!NNS || NNS->isDependent()) &&
2451 "nested-name-specifier must be dependent");
Douglas Gregor17343172009-04-01 00:28:59 +00002452
Douglas Gregor789b1f62010-02-04 18:10:26 +00002453 llvm::FoldingSetNodeID ID;
John McCall33500952010-06-11 00:33:02 +00002454 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2455 Name, NumArgs, Args);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002456
2457 void *InsertPos = 0;
John McCall33500952010-06-11 00:33:02 +00002458 DependentTemplateSpecializationType *T
2459 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002460 if (T)
2461 return QualType(T, 0);
2462
John McCall33500952010-06-11 00:33:02 +00002463 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002464
John McCall33500952010-06-11 00:33:02 +00002465 ElaboratedTypeKeyword CanonKeyword = Keyword;
2466 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2467
2468 bool AnyNonCanonArgs = false;
2469 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2470 for (unsigned I = 0; I != NumArgs; ++I) {
2471 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2472 if (!CanonArgs[I].structurallyEquals(Args[I]))
2473 AnyNonCanonArgs = true;
Douglas Gregor17343172009-04-01 00:28:59 +00002474 }
2475
John McCall33500952010-06-11 00:33:02 +00002476 QualType Canon;
2477 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2478 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2479 Name, NumArgs,
2480 CanonArgs.data());
2481
2482 // Find the insert position again.
2483 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2484 }
2485
2486 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2487 sizeof(TemplateArgument) * NumArgs),
2488 TypeAlignment);
John McCallef990012010-06-11 11:07:21 +00002489 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCall33500952010-06-11 00:33:02 +00002490 Name, NumArgs, Args, Canon);
Douglas Gregor17343172009-04-01 00:28:59 +00002491 Types.push_back(T);
John McCall33500952010-06-11 00:33:02 +00002492 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002493 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002494}
2495
Douglas Gregorcded4f62011-01-14 17:04:44 +00002496QualType ASTContext::getPackExpansionType(QualType Pattern,
2497 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00002498 llvm::FoldingSetNodeID ID;
Douglas Gregorcded4f62011-01-14 17:04:44 +00002499 PackExpansionType::Profile(ID, Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00002500
2501 assert(Pattern->containsUnexpandedParameterPack() &&
2502 "Pack expansions must expand one or more parameter packs");
2503 void *InsertPos = 0;
2504 PackExpansionType *T
2505 = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2506 if (T)
2507 return QualType(T, 0);
2508
2509 QualType Canon;
2510 if (!Pattern.isCanonical()) {
Douglas Gregorcded4f62011-01-14 17:04:44 +00002511 Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00002512
2513 // Find the insert position again.
2514 PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2515 }
2516
Douglas Gregorcded4f62011-01-14 17:04:44 +00002517 T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00002518 Types.push_back(T);
2519 PackExpansionTypes.InsertNode(T, InsertPos);
2520 return QualType(T, 0);
2521}
2522
Chris Lattner88cb27a2008-04-07 04:56:42 +00002523/// CmpProtocolNames - Comparison predicate for sorting protocols
2524/// alphabetically.
2525static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2526 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002527 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00002528}
2529
John McCallc12c5bb2010-05-15 11:32:37 +00002530static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCall54e14c42009-10-22 22:37:11 +00002531 unsigned NumProtocols) {
2532 if (NumProtocols == 0) return true;
2533
2534 for (unsigned i = 1; i != NumProtocols; ++i)
2535 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2536 return false;
2537 return true;
2538}
2539
2540static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00002541 unsigned &NumProtocols) {
2542 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00002543
Chris Lattner88cb27a2008-04-07 04:56:42 +00002544 // Sort protocols, keyed by name.
2545 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2546
2547 // Remove duplicates.
2548 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2549 NumProtocols = ProtocolsEnd-Protocols;
2550}
2551
John McCallc12c5bb2010-05-15 11:32:37 +00002552QualType ASTContext::getObjCObjectType(QualType BaseType,
2553 ObjCProtocolDecl * const *Protocols,
Jay Foad4ba2a172011-01-12 09:06:06 +00002554 unsigned NumProtocols) const {
John McCallc12c5bb2010-05-15 11:32:37 +00002555 // If the base type is an interface and there aren't any protocols
2556 // to add, then the interface type will do just fine.
2557 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2558 return BaseType;
2559
2560 // Look in the folding set for an existing type.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002561 llvm::FoldingSetNodeID ID;
John McCallc12c5bb2010-05-15 11:32:37 +00002562 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002563 void *InsertPos = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002564 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2565 return QualType(QT, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002566
John McCallc12c5bb2010-05-15 11:32:37 +00002567 // Build the canonical type, which has the canonical base type and
2568 // a sorted-and-uniqued list of protocols.
John McCall54e14c42009-10-22 22:37:11 +00002569 QualType Canonical;
John McCallc12c5bb2010-05-15 11:32:37 +00002570 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2571 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2572 if (!ProtocolsSorted) {
Benjamin Kramer02379412010-04-27 17:12:11 +00002573 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2574 Protocols + NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002575 unsigned UniqueCount = NumProtocols;
2576
John McCall54e14c42009-10-22 22:37:11 +00002577 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCallc12c5bb2010-05-15 11:32:37 +00002578 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2579 &Sorted[0], UniqueCount);
John McCall54e14c42009-10-22 22:37:11 +00002580 } else {
John McCallc12c5bb2010-05-15 11:32:37 +00002581 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2582 Protocols, NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002583 }
2584
2585 // Regenerate InsertPos.
John McCallc12c5bb2010-05-15 11:32:37 +00002586 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2587 }
2588
2589 unsigned Size = sizeof(ObjCObjectTypeImpl);
2590 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2591 void *Mem = Allocate(Size, TypeAlignment);
2592 ObjCObjectTypeImpl *T =
2593 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2594
2595 Types.push_back(T);
2596 ObjCObjectTypes.InsertNode(T, InsertPos);
2597 return QualType(T, 0);
2598}
2599
2600/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2601/// the given object type.
Jay Foad4ba2a172011-01-12 09:06:06 +00002602QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
John McCallc12c5bb2010-05-15 11:32:37 +00002603 llvm::FoldingSetNodeID ID;
2604 ObjCObjectPointerType::Profile(ID, ObjectT);
2605
2606 void *InsertPos = 0;
2607 if (ObjCObjectPointerType *QT =
2608 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2609 return QualType(QT, 0);
2610
2611 // Find the canonical object type.
2612 QualType Canonical;
2613 if (!ObjectT.isCanonical()) {
2614 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2615
2616 // Regenerate InsertPos.
John McCall54e14c42009-10-22 22:37:11 +00002617 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2618 }
2619
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002620 // No match.
John McCallc12c5bb2010-05-15 11:32:37 +00002621 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2622 ObjCObjectPointerType *QType =
2623 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002625 Types.push_back(QType);
2626 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCallc12c5bb2010-05-15 11:32:37 +00002627 return QualType(QType, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002628}
Chris Lattner88cb27a2008-04-07 04:56:42 +00002629
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002630/// getObjCInterfaceType - Return the unique reference to the type for the
2631/// specified ObjC interface decl. The list of protocols is optional.
Jay Foad4ba2a172011-01-12 09:06:06 +00002632QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const {
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002633 if (Decl->TypeForDecl)
2634 return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002635
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002636 // FIXME: redeclarations?
2637 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2638 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2639 Decl->TypeForDecl = T;
2640 Types.push_back(T);
2641 return QualType(T, 0);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002642}
2643
Douglas Gregor72564e72009-02-26 23:50:07 +00002644/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2645/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002646/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002647/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002648/// on canonical type's (which are always unique).
Jay Foad4ba2a172011-01-12 09:06:06 +00002649QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002650 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002651 if (tofExpr->isTypeDependent()) {
2652 llvm::FoldingSetNodeID ID;
2653 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002654
Douglas Gregorb1975722009-07-30 23:18:24 +00002655 void *InsertPos = 0;
2656 DependentTypeOfExprType *Canon
2657 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2658 if (Canon) {
2659 // We already have a "canonical" version of an identical, dependent
2660 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002661 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002662 QualType((TypeOfExprType*)Canon, 0));
2663 }
2664 else {
2665 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002666 Canon
2667 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002668 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2669 toe = Canon;
2670 }
2671 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002672 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002673 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002674 }
Steve Naroff9752f252007-08-01 18:02:17 +00002675 Types.push_back(toe);
2676 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002677}
2678
Steve Naroff9752f252007-08-01 18:02:17 +00002679/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2680/// TypeOfType AST's. The only motivation to unique these nodes would be
2681/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002682/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002683/// on canonical type's (which are always unique).
Jay Foad4ba2a172011-01-12 09:06:06 +00002684QualType ASTContext::getTypeOfType(QualType tofType) const {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002685 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002686 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002687 Types.push_back(tot);
2688 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002689}
2690
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002691/// getDecltypeForExpr - Given an expr, will return the decltype for that
2692/// expression, according to the rules in C++0x [dcl.type.simple]p4
Jay Foad4ba2a172011-01-12 09:06:06 +00002693static QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002694 if (e->isTypeDependent())
2695 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002697 // If e is an id expression or a class member access, decltype(e) is defined
2698 // as the type of the entity named by e.
2699 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2700 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2701 return VD->getType();
2702 }
2703 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2704 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2705 return FD->getType();
2706 }
2707 // If e is a function call or an invocation of an overloaded operator,
2708 // (parentheses around e are ignored), decltype(e) is defined as the
2709 // return type of that function.
2710 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2711 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002712
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002713 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002714
2715 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002716 // defined as T&, otherwise decltype(e) is defined as T.
John McCall7eb0a9e2010-11-24 05:12:34 +00002717 if (e->isLValue())
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002718 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002719
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002720 return T;
2721}
2722
Anders Carlsson395b4752009-06-24 19:06:50 +00002723/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2724/// DecltypeType AST's. The only motivation to unique these nodes would be
2725/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002726/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002727/// on canonical type's (which are always unique).
Jay Foad4ba2a172011-01-12 09:06:06 +00002728QualType ASTContext::getDecltypeType(Expr *e) const {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002729 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002730 if (e->isTypeDependent()) {
2731 llvm::FoldingSetNodeID ID;
2732 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002733
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002734 void *InsertPos = 0;
2735 DependentDecltypeType *Canon
2736 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2737 if (Canon) {
2738 // We already have a "canonical" version of an equivalent, dependent
2739 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002740 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002741 QualType((DecltypeType*)Canon, 0));
2742 }
2743 else {
2744 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002745 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002746 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2747 dt = Canon;
2748 }
2749 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002750 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002751 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002752 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002753 Types.push_back(dt);
2754 return QualType(dt, 0);
2755}
2756
Richard Smith483b9f32011-02-21 20:05:19 +00002757/// getAutoType - We only unique auto types after they've been deduced.
Richard Smith34b41d92011-02-20 03:19:35 +00002758QualType ASTContext::getAutoType(QualType DeducedType) const {
Richard Smith483b9f32011-02-21 20:05:19 +00002759 void *InsertPos = 0;
2760 if (!DeducedType.isNull()) {
2761 // Look in the folding set for an existing type.
2762 llvm::FoldingSetNodeID ID;
2763 AutoType::Profile(ID, DeducedType);
2764 if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
2765 return QualType(AT, 0);
2766 }
2767
2768 AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
2769 Types.push_back(AT);
2770 if (InsertPos)
2771 AutoTypes.InsertNode(AT, InsertPos);
2772 return QualType(AT, 0);
Richard Smith34b41d92011-02-20 03:19:35 +00002773}
2774
Richard Smithad762fc2011-04-14 22:09:26 +00002775/// getAutoDeductType - Get type pattern for deducing against 'auto'.
2776QualType ASTContext::getAutoDeductType() const {
2777 if (AutoDeductTy.isNull())
2778 AutoDeductTy = getAutoType(QualType());
2779 assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
2780 return AutoDeductTy;
2781}
2782
2783/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
2784QualType ASTContext::getAutoRRefDeductType() const {
2785 if (AutoRRefDeductTy.isNull())
2786 AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
2787 assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
2788 return AutoRRefDeductTy;
2789}
2790
Reid Spencer5f016e22007-07-11 17:01:13 +00002791/// getTagDeclType - Return the unique reference to the type for the
2792/// specified TagDecl (struct/union/class/enum) decl.
Jay Foad4ba2a172011-01-12 09:06:06 +00002793QualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
Ted Kremenekd778f882007-11-26 21:16:01 +00002794 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002795 // FIXME: What is the design on getTagDeclType when it requires casting
2796 // away const? mutable?
2797 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002798}
2799
Mike Stump1eb44332009-09-09 15:08:12 +00002800/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2801/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2802/// needs to agree with the definition in <stddef.h>.
Anders Carlssona3ccda52009-12-12 00:26:23 +00002803CanQualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002804 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002805}
2806
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002807/// getSignedWCharType - Return the type of "signed wchar_t".
2808/// Used when in C++, as a GCC extension.
2809QualType ASTContext::getSignedWCharType() const {
2810 // FIXME: derive from "Target" ?
2811 return WCharTy;
2812}
2813
2814/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2815/// Used when in C++, as a GCC extension.
2816QualType ASTContext::getUnsignedWCharType() const {
2817 // FIXME: derive from "Target" ?
2818 return UnsignedIntTy;
2819}
2820
Chris Lattner8b9023b2007-07-13 03:05:23 +00002821/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2822/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2823QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002824 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002825}
2826
Chris Lattnere6327742008-04-02 05:18:44 +00002827//===----------------------------------------------------------------------===//
2828// Type Operators
2829//===----------------------------------------------------------------------===//
2830
Jay Foad4ba2a172011-01-12 09:06:06 +00002831CanQualType ASTContext::getCanonicalParamType(QualType T) const {
John McCall54e14c42009-10-22 22:37:11 +00002832 // Push qualifiers into arrays, and then discard any remaining
2833 // qualifiers.
2834 T = getCanonicalType(T);
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002835 T = getVariableArrayDecayedType(T);
John McCall54e14c42009-10-22 22:37:11 +00002836 const Type *Ty = T.getTypePtr();
John McCall54e14c42009-10-22 22:37:11 +00002837 QualType Result;
2838 if (isa<ArrayType>(Ty)) {
2839 Result = getArrayDecayedType(QualType(Ty,0));
2840 } else if (isa<FunctionType>(Ty)) {
2841 Result = getPointerType(QualType(Ty, 0));
2842 } else {
2843 Result = QualType(Ty, 0);
2844 }
2845
2846 return CanQualType::CreateUnsafe(Result);
2847}
2848
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002849
John McCall62c28c82011-01-18 07:41:22 +00002850QualType ASTContext::getUnqualifiedArrayType(QualType type,
2851 Qualifiers &quals) {
2852 SplitQualType splitType = type.getSplitUnqualifiedType();
2853
2854 // FIXME: getSplitUnqualifiedType() actually walks all the way to
2855 // the unqualified desugared type and then drops it on the floor.
2856 // We then have to strip that sugar back off with
2857 // getUnqualifiedDesugaredType(), which is silly.
2858 const ArrayType *AT =
2859 dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType());
2860
2861 // If we don't have an array, just use the results in splitType.
Douglas Gregor9dadd942010-05-17 18:45:21 +00002862 if (!AT) {
John McCall62c28c82011-01-18 07:41:22 +00002863 quals = splitType.second;
2864 return QualType(splitType.first, 0);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002865 }
2866
John McCall62c28c82011-01-18 07:41:22 +00002867 // Otherwise, recurse on the array's element type.
2868 QualType elementType = AT->getElementType();
2869 QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
2870
2871 // If that didn't change the element type, AT has no qualifiers, so we
2872 // can just use the results in splitType.
2873 if (elementType == unqualElementType) {
2874 assert(quals.empty()); // from the recursive call
2875 quals = splitType.second;
2876 return QualType(splitType.first, 0);
2877 }
2878
2879 // Otherwise, add in the qualifiers from the outermost type, then
2880 // build the type back up.
2881 quals.addConsistentQualifiers(splitType.second);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002882
Douglas Gregor9dadd942010-05-17 18:45:21 +00002883 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
John McCall62c28c82011-01-18 07:41:22 +00002884 return getConstantArrayType(unqualElementType, CAT->getSize(),
Chandler Carruth28e318c2009-12-29 07:16:59 +00002885 CAT->getSizeModifier(), 0);
2886 }
2887
Douglas Gregor9dadd942010-05-17 18:45:21 +00002888 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
John McCall62c28c82011-01-18 07:41:22 +00002889 return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002890 }
2891
Douglas Gregor9dadd942010-05-17 18:45:21 +00002892 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
John McCall62c28c82011-01-18 07:41:22 +00002893 return getVariableArrayType(unqualElementType,
John McCall3fa5cae2010-10-26 07:05:15 +00002894 VAT->getSizeExpr(),
Douglas Gregor9dadd942010-05-17 18:45:21 +00002895 VAT->getSizeModifier(),
2896 VAT->getIndexTypeCVRQualifiers(),
2897 VAT->getBracketsRange());
2898 }
2899
2900 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCall62c28c82011-01-18 07:41:22 +00002901 return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
Chandler Carruth28e318c2009-12-29 07:16:59 +00002902 DSAT->getSizeModifier(), 0,
2903 SourceRange());
2904}
2905
Douglas Gregor5a57efd2010-06-09 03:53:18 +00002906/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2907/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2908/// they point to and return true. If T1 and T2 aren't pointer types
2909/// or pointer-to-member types, or if they are not similar at this
2910/// level, returns false and leaves T1 and T2 unchanged. Top-level
2911/// qualifiers on T1 and T2 are ignored. This function will typically
2912/// be called in a loop that successively "unwraps" pointer and
2913/// pointer-to-member types to compare them at each level.
2914bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2915 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2916 *T2PtrType = T2->getAs<PointerType>();
2917 if (T1PtrType && T2PtrType) {
2918 T1 = T1PtrType->getPointeeType();
2919 T2 = T2PtrType->getPointeeType();
2920 return true;
2921 }
2922
2923 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2924 *T2MPType = T2->getAs<MemberPointerType>();
2925 if (T1MPType && T2MPType &&
2926 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2927 QualType(T2MPType->getClass(), 0))) {
2928 T1 = T1MPType->getPointeeType();
2929 T2 = T2MPType->getPointeeType();
2930 return true;
2931 }
2932
2933 if (getLangOptions().ObjC1) {
2934 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2935 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2936 if (T1OPType && T2OPType) {
2937 T1 = T1OPType->getPointeeType();
2938 T2 = T2OPType->getPointeeType();
2939 return true;
2940 }
2941 }
2942
2943 // FIXME: Block pointers, too?
2944
2945 return false;
2946}
2947
Jay Foad4ba2a172011-01-12 09:06:06 +00002948DeclarationNameInfo
2949ASTContext::getNameForTemplate(TemplateName Name,
2950 SourceLocation NameLoc) const {
John McCall80ad16f2009-11-24 18:42:40 +00002951 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnara25777432010-08-11 22:01:17 +00002952 // DNInfo work in progress: CHECKME: what about DNLoc?
2953 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2954
John McCall80ad16f2009-11-24 18:42:40 +00002955 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002956 DeclarationName DName;
John McCall80ad16f2009-11-24 18:42:40 +00002957 if (DTN->isIdentifier()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002958 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2959 return DeclarationNameInfo(DName, NameLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002960 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002961 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2962 // DNInfo work in progress: FIXME: source locations?
2963 DeclarationNameLoc DNLoc;
2964 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2965 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2966 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002967 }
2968 }
2969
John McCall0bd6feb2009-12-02 08:04:21 +00002970 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2971 assert(Storage);
Abramo Bagnara25777432010-08-11 22:01:17 +00002972 // DNInfo work in progress: CHECKME: what about DNLoc?
2973 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002974}
2975
Jay Foad4ba2a172011-01-12 09:06:06 +00002976TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
Douglas Gregor3e1274f2010-06-16 21:09:37 +00002977 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2978 if (TemplateTemplateParmDecl *TTP
2979 = dyn_cast<TemplateTemplateParmDecl>(Template))
2980 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2981
2982 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002983 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor3e1274f2010-06-16 21:09:37 +00002984 }
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002985
Douglas Gregor1aee05d2011-01-15 06:45:20 +00002986 if (SubstTemplateTemplateParmPackStorage *SubstPack
2987 = Name.getAsSubstTemplateTemplateParmPack()) {
2988 TemplateTemplateParmDecl *CanonParam
2989 = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack());
2990 TemplateArgument CanonArgPack
2991 = getCanonicalTemplateArgument(SubstPack->getArgumentPack());
2992 return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack);
2993 }
2994
John McCall0bd6feb2009-12-02 08:04:21 +00002995 assert(!Name.getAsOverloadedTemplate());
Mike Stump1eb44332009-09-09 15:08:12 +00002996
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002997 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2998 assert(DTN && "Non-dependent template names must refer to template decls.");
2999 return DTN->CanonicalTemplateName;
3000}
3001
Douglas Gregordb0d4b72009-11-11 23:06:43 +00003002bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3003 X = getCanonicalTemplateName(X);
3004 Y = getCanonicalTemplateName(Y);
3005 return X.getAsVoidPointer() == Y.getAsVoidPointer();
3006}
3007
Mike Stump1eb44332009-09-09 15:08:12 +00003008TemplateArgument
Jay Foad4ba2a172011-01-12 09:06:06 +00003009ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
Douglas Gregor1275ae02009-07-28 23:00:59 +00003010 switch (Arg.getKind()) {
3011 case TemplateArgument::Null:
3012 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00003013
Douglas Gregor1275ae02009-07-28 23:00:59 +00003014 case TemplateArgument::Expression:
Douglas Gregor1275ae02009-07-28 23:00:59 +00003015 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00003016
Douglas Gregor1275ae02009-07-28 23:00:59 +00003017 case TemplateArgument::Declaration:
John McCall833ca992009-10-29 08:12:44 +00003018 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00003019
Douglas Gregor788cd062009-11-11 01:00:40 +00003020 case TemplateArgument::Template:
3021 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
Douglas Gregora7fc9012011-01-05 18:58:31 +00003022
3023 case TemplateArgument::TemplateExpansion:
3024 return TemplateArgument(getCanonicalTemplateName(
3025 Arg.getAsTemplateOrTemplatePattern()),
Douglas Gregor2be29f42011-01-14 23:41:42 +00003026 Arg.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00003027
Douglas Gregor1275ae02009-07-28 23:00:59 +00003028 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00003029 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00003030 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00003031
Douglas Gregor1275ae02009-07-28 23:00:59 +00003032 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00003033 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00003034
Douglas Gregor1275ae02009-07-28 23:00:59 +00003035 case TemplateArgument::Pack: {
Douglas Gregor87dd6972010-12-20 16:52:59 +00003036 if (Arg.pack_size() == 0)
3037 return Arg;
3038
Douglas Gregor910f8002010-11-07 23:05:16 +00003039 TemplateArgument *CanonArgs
3040 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregor1275ae02009-07-28 23:00:59 +00003041 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003042 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00003043 AEnd = Arg.pack_end();
3044 A != AEnd; (void)++A, ++Idx)
3045 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00003046
Douglas Gregor910f8002010-11-07 23:05:16 +00003047 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregor1275ae02009-07-28 23:00:59 +00003048 }
3049 }
3050
3051 // Silence GCC warning
3052 assert(false && "Unhandled template argument kind");
3053 return TemplateArgument();
3054}
3055
Douglas Gregord57959a2009-03-27 23:10:48 +00003056NestedNameSpecifier *
Jay Foad4ba2a172011-01-12 09:06:06 +00003057ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
Mike Stump1eb44332009-09-09 15:08:12 +00003058 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00003059 return 0;
3060
3061 switch (NNS->getKind()) {
3062 case NestedNameSpecifier::Identifier:
3063 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00003064 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00003065 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3066 NNS->getAsIdentifier());
3067
3068 case NestedNameSpecifier::Namespace:
3069 // A namespace is canonical; build a nested-name-specifier with
3070 // this namespace and no prefix.
Douglas Gregor14aba762011-02-24 02:36:08 +00003071 return NestedNameSpecifier::Create(*this, 0,
3072 NNS->getAsNamespace()->getOriginalNamespace());
3073
3074 case NestedNameSpecifier::NamespaceAlias:
3075 // A namespace is canonical; build a nested-name-specifier with
3076 // this namespace and no prefix.
3077 return NestedNameSpecifier::Create(*this, 0,
3078 NNS->getAsNamespaceAlias()->getNamespace()
3079 ->getOriginalNamespace());
Douglas Gregord57959a2009-03-27 23:10:48 +00003080
3081 case NestedNameSpecifier::TypeSpec:
3082 case NestedNameSpecifier::TypeSpecWithTemplate: {
3083 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor264bf662010-11-04 00:09:33 +00003084
3085 // If we have some kind of dependent-named type (e.g., "typename T::type"),
3086 // break it apart into its prefix and identifier, then reconsititute those
3087 // as the canonical nested-name-specifier. This is required to canonicalize
3088 // a dependent nested-name-specifier involving typedefs of dependent-name
3089 // types, e.g.,
3090 // typedef typename T::type T1;
3091 // typedef typename T1::type T2;
3092 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
3093 NestedNameSpecifier *Prefix
3094 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
3095 return NestedNameSpecifier::Create(*this, Prefix,
3096 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3097 }
3098
Douglas Gregor643f8432010-11-04 00:14:23 +00003099 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor264bf662010-11-04 00:09:33 +00003100 if (const DependentTemplateSpecializationType *DTST
3101 = T->getAs<DependentTemplateSpecializationType>()) {
3102 NestedNameSpecifier *Prefix
3103 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
Douglas Gregoraa2187d2011-02-28 00:04:36 +00003104
3105 T = getDependentTemplateSpecializationType(DTST->getKeyword(),
3106 Prefix, DTST->getIdentifier(),
3107 DTST->getNumArgs(),
3108 DTST->getArgs());
Douglas Gregor264bf662010-11-04 00:09:33 +00003109 T = getCanonicalType(T);
3110 }
3111
John McCall3b657512011-01-19 10:06:00 +00003112 return NestedNameSpecifier::Create(*this, 0, false,
3113 const_cast<Type*>(T.getTypePtr()));
Douglas Gregord57959a2009-03-27 23:10:48 +00003114 }
3115
3116 case NestedNameSpecifier::Global:
3117 // The global specifier is canonical and unique.
3118 return NNS;
3119 }
3120
3121 // Required to silence a GCC warning
3122 return 0;
3123}
3124
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003125
Jay Foad4ba2a172011-01-12 09:06:06 +00003126const ArrayType *ASTContext::getAsArrayType(QualType T) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003127 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00003128 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003129 // Handle the common positive case fast.
3130 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3131 return AT;
3132 }
Mike Stump1eb44332009-09-09 15:08:12 +00003133
John McCall0953e762009-09-24 19:53:00 +00003134 // Handle the common negative case fast.
John McCall3b657512011-01-19 10:06:00 +00003135 if (!isa<ArrayType>(T.getCanonicalType()))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003136 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003137
John McCall0953e762009-09-24 19:53:00 +00003138 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003139 // implements C99 6.7.3p8: "If the specification of an array type includes
3140 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00003141
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003142 // If we get here, we either have type qualifiers on the type, or we have
3143 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00003144 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00003145
John McCall3b657512011-01-19 10:06:00 +00003146 SplitQualType split = T.getSplitDesugaredType();
3147 Qualifiers qs = split.second;
Mike Stump1eb44332009-09-09 15:08:12 +00003148
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003149 // If we have a simple case, just return now.
John McCall3b657512011-01-19 10:06:00 +00003150 const ArrayType *ATy = dyn_cast<ArrayType>(split.first);
3151 if (ATy == 0 || qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003152 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00003153
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003154 // Otherwise, we have an array and we have qualifiers on it. Push the
3155 // qualifiers into the array element type and return a new array type.
John McCall3b657512011-01-19 10:06:00 +00003156 QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
Mike Stump1eb44332009-09-09 15:08:12 +00003157
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003158 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3159 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3160 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00003161 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003162 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3163 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3164 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00003165 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00003166
Mike Stump1eb44332009-09-09 15:08:12 +00003167 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00003168 = dyn_cast<DependentSizedArrayType>(ATy))
3169 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00003170 getDependentSizedArrayType(NewEltTy,
John McCall3fa5cae2010-10-26 07:05:15 +00003171 DSAT->getSizeExpr(),
Douglas Gregor898574e2008-12-05 23:32:09 +00003172 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00003173 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003174 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00003175
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003176 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003177 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCall3fa5cae2010-10-26 07:05:15 +00003178 VAT->getSizeExpr(),
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003179 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00003180 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003181 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00003182}
3183
Chris Lattnere6327742008-04-02 05:18:44 +00003184/// getArrayDecayedType - Return the properly qualified result of decaying the
3185/// specified array type to a pointer. This operation is non-trivial when
3186/// handling typedefs etc. The canonical type of "T" must be an array type,
3187/// this returns a pointer to a properly qualified element of the array.
3188///
3189/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
Jay Foad4ba2a172011-01-12 09:06:06 +00003190QualType ASTContext::getArrayDecayedType(QualType Ty) const {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003191 // Get the element type with 'getAsArrayType' so that we don't lose any
3192 // typedefs in the element type of the array. This also handles propagation
3193 // of type qualifiers from the array type into the element type if present
3194 // (C99 6.7.3p8).
3195 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3196 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00003197
Chris Lattnerc63a1f22008-08-04 07:31:14 +00003198 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00003199
3200 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00003201 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00003202}
3203
John McCall3b657512011-01-19 10:06:00 +00003204QualType ASTContext::getBaseElementType(const ArrayType *array) const {
3205 return getBaseElementType(array->getElementType());
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00003206}
3207
John McCall3b657512011-01-19 10:06:00 +00003208QualType ASTContext::getBaseElementType(QualType type) const {
3209 Qualifiers qs;
3210 while (true) {
3211 SplitQualType split = type.getSplitDesugaredType();
3212 const ArrayType *array = split.first->getAsArrayTypeUnsafe();
3213 if (!array) break;
Mike Stump1eb44332009-09-09 15:08:12 +00003214
John McCall3b657512011-01-19 10:06:00 +00003215 type = array->getElementType();
3216 qs.addConsistentQualifiers(split.second);
3217 }
Mike Stump1eb44332009-09-09 15:08:12 +00003218
John McCall3b657512011-01-19 10:06:00 +00003219 return getQualifiedType(type, qs);
Anders Carlsson6183a992008-12-21 03:44:36 +00003220}
3221
Fariborz Jahanian0de78992009-08-21 16:31:06 +00003222/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00003223uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00003224ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
3225 uint64_t ElementCount = 1;
3226 do {
3227 ElementCount *= CA->getSize().getZExtValue();
3228 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
3229 } while (CA);
3230 return ElementCount;
3231}
3232
Reid Spencer5f016e22007-07-11 17:01:13 +00003233/// getFloatingRank - Return a relative rank for floating point types.
3234/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00003235static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00003236 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00003237 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00003238
John McCall183700f2009-09-21 23:43:11 +00003239 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3240 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00003241 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00003242 case BuiltinType::Float: return FloatRank;
3243 case BuiltinType::Double: return DoubleRank;
3244 case BuiltinType::LongDouble: return LongDoubleRank;
3245 }
3246}
3247
Mike Stump1eb44332009-09-09 15:08:12 +00003248/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
3249/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00003250/// 'typeDomain' is a real floating point or complex type.
3251/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00003252QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
3253 QualType Domain) const {
3254 FloatingRank EltRank = getFloatingRank(Size);
3255 if (Domain->isComplexType()) {
3256 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00003257 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00003258 case FloatRank: return FloatComplexTy;
3259 case DoubleRank: return DoubleComplexTy;
3260 case LongDoubleRank: return LongDoubleComplexTy;
3261 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003262 }
Chris Lattner1361b112008-04-06 23:58:54 +00003263
3264 assert(Domain->isRealFloatingType() && "Unknown domain!");
3265 switch (EltRank) {
3266 default: assert(0 && "getFloatingRank(): illegal value for rank");
3267 case FloatRank: return FloatTy;
3268 case DoubleRank: return DoubleTy;
3269 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00003270 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003271}
3272
Chris Lattner7cfeb082008-04-06 23:55:33 +00003273/// getFloatingTypeOrder - Compare the rank of the two specified floating
3274/// point types, ignoring the domain of the type (i.e. 'double' ==
3275/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00003276/// LHS < RHS, return -1.
Jay Foad4ba2a172011-01-12 09:06:06 +00003277int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
Chris Lattnera75cea32008-04-06 23:38:49 +00003278 FloatingRank LHSR = getFloatingRank(LHS);
3279 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003280
Chris Lattnera75cea32008-04-06 23:38:49 +00003281 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00003282 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00003283 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00003284 return 1;
3285 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00003286}
3287
Chris Lattnerf52ab252008-04-06 22:59:24 +00003288/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3289/// routine will assert if passed a built-in type that isn't an integer or enum,
3290/// or if it is not canonicalized.
John McCallf4c73712011-01-19 06:33:43 +00003291unsigned ASTContext::getIntegerRank(const Type *T) const {
John McCall467b27b2009-10-22 20:10:53 +00003292 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
John McCallf4c73712011-01-19 06:33:43 +00003293 if (const EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00003294 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00003295
Chris Lattner3f59c972010-12-25 23:25:43 +00003296 if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
3297 T->isSpecificBuiltinType(BuiltinType::WChar_U))
Eli Friedmana3426752009-07-05 23:44:27 +00003298 T = getFromTargetType(Target.getWCharType()).getTypePtr();
3299
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003300 if (T->isSpecificBuiltinType(BuiltinType::Char16))
3301 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
3302
3303 if (T->isSpecificBuiltinType(BuiltinType::Char32))
3304 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
3305
Chris Lattnerf52ab252008-04-06 22:59:24 +00003306 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00003307 default: assert(0 && "getIntegerRank(): not a built-in integer");
3308 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003309 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003310 case BuiltinType::Char_S:
3311 case BuiltinType::Char_U:
3312 case BuiltinType::SChar:
3313 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003314 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003315 case BuiltinType::Short:
3316 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003317 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003318 case BuiltinType::Int:
3319 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003320 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003321 case BuiltinType::Long:
3322 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003323 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003324 case BuiltinType::LongLong:
3325 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003326 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00003327 case BuiltinType::Int128:
3328 case BuiltinType::UInt128:
3329 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00003330 }
3331}
3332
Eli Friedman04e83572009-08-20 04:21:42 +00003333/// \brief Whether this is a promotable bitfield reference according
3334/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
3335///
3336/// \returns the type this bit-field will promote to, or NULL if no
3337/// promotion occurs.
Jay Foad4ba2a172011-01-12 09:06:06 +00003338QualType ASTContext::isPromotableBitField(Expr *E) const {
Douglas Gregorceafbde2010-05-24 20:13:53 +00003339 if (E->isTypeDependent() || E->isValueDependent())
3340 return QualType();
3341
Eli Friedman04e83572009-08-20 04:21:42 +00003342 FieldDecl *Field = E->getBitField();
3343 if (!Field)
3344 return QualType();
3345
3346 QualType FT = Field->getType();
3347
3348 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3349 uint64_t BitWidth = BitWidthAP.getZExtValue();
3350 uint64_t IntSize = getTypeSize(IntTy);
3351 // GCC extension compatibility: if the bit-field size is less than or equal
3352 // to the size of int, it gets promoted no matter what its type is.
3353 // For instance, unsigned long bf : 4 gets promoted to signed int.
3354 if (BitWidth < IntSize)
3355 return IntTy;
3356
3357 if (BitWidth == IntSize)
3358 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3359
3360 // Types bigger than int are not subject to promotions, and therefore act
3361 // like the base type.
3362 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3363 // is ridiculous.
3364 return QualType();
3365}
3366
Eli Friedmana95d7572009-08-19 07:44:53 +00003367/// getPromotedIntegerType - Returns the type that Promotable will
3368/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3369/// integer type.
Jay Foad4ba2a172011-01-12 09:06:06 +00003370QualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
Eli Friedmana95d7572009-08-19 07:44:53 +00003371 assert(!Promotable.isNull());
3372 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00003373 if (const EnumType *ET = Promotable->getAs<EnumType>())
3374 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00003375 if (Promotable->isSignedIntegerType())
3376 return IntTy;
3377 uint64_t PromotableSize = getTypeSize(Promotable);
3378 uint64_t IntSize = getTypeSize(IntTy);
3379 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3380 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3381}
3382
Mike Stump1eb44332009-09-09 15:08:12 +00003383/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00003384/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00003385/// LHS < RHS, return -1.
Jay Foad4ba2a172011-01-12 09:06:06 +00003386int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
John McCallf4c73712011-01-19 06:33:43 +00003387 const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3388 const Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00003389 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003390
Chris Lattnerf52ab252008-04-06 22:59:24 +00003391 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3392 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00003393
Chris Lattner7cfeb082008-04-06 23:55:33 +00003394 unsigned LHSRank = getIntegerRank(LHSC);
3395 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00003396
Chris Lattner7cfeb082008-04-06 23:55:33 +00003397 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3398 if (LHSRank == RHSRank) return 0;
3399 return LHSRank > RHSRank ? 1 : -1;
3400 }
Mike Stump1eb44332009-09-09 15:08:12 +00003401
Chris Lattner7cfeb082008-04-06 23:55:33 +00003402 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3403 if (LHSUnsigned) {
3404 // If the unsigned [LHS] type is larger, return it.
3405 if (LHSRank >= RHSRank)
3406 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00003407
Chris Lattner7cfeb082008-04-06 23:55:33 +00003408 // If the signed type can represent all values of the unsigned type, it
3409 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003410 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003411 return -1;
3412 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00003413
Chris Lattner7cfeb082008-04-06 23:55:33 +00003414 // If the unsigned [RHS] type is larger, return it.
3415 if (RHSRank >= LHSRank)
3416 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00003417
Chris Lattner7cfeb082008-04-06 23:55:33 +00003418 // If the signed type can represent all values of the unsigned type, it
3419 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003420 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003421 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00003422}
Anders Carlsson71993dd2007-08-17 05:31:46 +00003423
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003424static RecordDecl *
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003425CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3426 DeclContext *DC, IdentifierInfo *Id) {
3427 SourceLocation Loc;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003428 if (Ctx.getLangOptions().CPlusPlus)
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003429 return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003430 else
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003431 return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003432}
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003433
Mike Stump1eb44332009-09-09 15:08:12 +00003434// getCFConstantStringType - Return the type used for constant CFStrings.
Jay Foad4ba2a172011-01-12 09:06:06 +00003435QualType ASTContext::getCFConstantStringType() const {
Anders Carlsson71993dd2007-08-17 05:31:46 +00003436 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003437 CFConstantStringTypeDecl =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003438 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003439 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00003440 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003441
Anders Carlssonf06273f2007-11-19 00:25:30 +00003442 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00003443
Anders Carlsson71993dd2007-08-17 05:31:46 +00003444 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00003445 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00003446 // int flags;
3447 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00003448 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00003449 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00003450 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00003451 FieldTypes[3] = LongTy;
3452
Anders Carlsson71993dd2007-08-17 05:31:46 +00003453 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00003454 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003455 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003456 SourceLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003457 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003458 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003459 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003460 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003461 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003462 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003463 }
3464
Douglas Gregor838db382010-02-11 01:19:42 +00003465 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00003466 }
Mike Stump1eb44332009-09-09 15:08:12 +00003467
Anders Carlsson71993dd2007-08-17 05:31:46 +00003468 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00003469}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003470
Douglas Gregor319ac892009-04-23 22:29:11 +00003471void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003472 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003473 assert(Rec && "Invalid CFConstantStringType");
3474 CFConstantStringTypeDecl = Rec->getDecl();
3475}
3476
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003477// getNSConstantStringType - Return the type used for constant NSStrings.
Jay Foad4ba2a172011-01-12 09:06:06 +00003478QualType ASTContext::getNSConstantStringType() const {
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003479 if (!NSConstantStringTypeDecl) {
3480 NSConstantStringTypeDecl =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003481 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003482 &Idents.get("__builtin_NSString"));
3483 NSConstantStringTypeDecl->startDefinition();
3484
3485 QualType FieldTypes[3];
3486
3487 // const int *isa;
3488 FieldTypes[0] = getPointerType(IntTy.withConst());
3489 // const char *str;
3490 FieldTypes[1] = getPointerType(CharTy.withConst());
3491 // unsigned int length;
3492 FieldTypes[2] = UnsignedIntTy;
3493
3494 // Create fields
3495 for (unsigned i = 0; i < 3; ++i) {
3496 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003497 SourceLocation(),
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003498 SourceLocation(), 0,
3499 FieldTypes[i], /*TInfo=*/0,
3500 /*BitWidth=*/0,
3501 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003502 Field->setAccess(AS_public);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003503 NSConstantStringTypeDecl->addDecl(Field);
3504 }
3505
3506 NSConstantStringTypeDecl->completeDefinition();
3507 }
3508
3509 return getTagDeclType(NSConstantStringTypeDecl);
3510}
3511
3512void ASTContext::setNSConstantStringType(QualType T) {
3513 const RecordType *Rec = T->getAs<RecordType>();
3514 assert(Rec && "Invalid NSConstantStringType");
3515 NSConstantStringTypeDecl = Rec->getDecl();
3516}
3517
Jay Foad4ba2a172011-01-12 09:06:06 +00003518QualType ASTContext::getObjCFastEnumerationStateType() const {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003519 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00003520 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003521 CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003522 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00003523 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00003524
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003525 QualType FieldTypes[] = {
3526 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00003527 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003528 getPointerType(UnsignedLongTy),
3529 getConstantArrayType(UnsignedLongTy,
3530 llvm::APInt(32, 5), ArrayType::Normal, 0)
3531 };
Mike Stump1eb44332009-09-09 15:08:12 +00003532
Douglas Gregor44b43212008-12-11 16:49:14 +00003533 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003534 FieldDecl *Field = FieldDecl::Create(*this,
3535 ObjCFastEnumerationStateTypeDecl,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003536 SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00003537 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003538 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003539 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003540 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003541 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003542 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003543 }
Mike Stump1eb44332009-09-09 15:08:12 +00003544
Douglas Gregor838db382010-02-11 01:19:42 +00003545 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003546 }
Mike Stump1eb44332009-09-09 15:08:12 +00003547
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003548 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3549}
3550
Jay Foad4ba2a172011-01-12 09:06:06 +00003551QualType ASTContext::getBlockDescriptorType() const {
Mike Stumpadaaad32009-10-20 02:12:22 +00003552 if (BlockDescriptorType)
3553 return getTagDeclType(BlockDescriptorType);
3554
3555 RecordDecl *T;
3556 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003557 T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003558 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00003559 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003560
3561 QualType FieldTypes[] = {
3562 UnsignedLongTy,
3563 UnsignedLongTy,
3564 };
3565
3566 const char *FieldNames[] = {
3567 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00003568 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00003569 };
3570
3571 for (size_t i = 0; i < 2; ++i) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003572 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003573 SourceLocation(),
3574 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003575 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00003576 /*BitWidth=*/0,
3577 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003578 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003579 T->addDecl(Field);
3580 }
3581
Douglas Gregor838db382010-02-11 01:19:42 +00003582 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003583
3584 BlockDescriptorType = T;
3585
3586 return getTagDeclType(BlockDescriptorType);
3587}
3588
3589void ASTContext::setBlockDescriptorType(QualType T) {
3590 const RecordType *Rec = T->getAs<RecordType>();
3591 assert(Rec && "Invalid BlockDescriptorType");
3592 BlockDescriptorType = Rec->getDecl();
3593}
3594
Jay Foad4ba2a172011-01-12 09:06:06 +00003595QualType ASTContext::getBlockDescriptorExtendedType() const {
Mike Stump083c25e2009-10-22 00:49:09 +00003596 if (BlockDescriptorExtendedType)
3597 return getTagDeclType(BlockDescriptorExtendedType);
3598
3599 RecordDecl *T;
3600 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003601 T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003602 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00003603 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003604
3605 QualType FieldTypes[] = {
3606 UnsignedLongTy,
3607 UnsignedLongTy,
3608 getPointerType(VoidPtrTy),
3609 getPointerType(VoidPtrTy)
3610 };
3611
3612 const char *FieldNames[] = {
3613 "reserved",
3614 "Size",
3615 "CopyFuncPtr",
3616 "DestroyFuncPtr"
3617 };
3618
3619 for (size_t i = 0; i < 4; ++i) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003620 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stump083c25e2009-10-22 00:49:09 +00003621 SourceLocation(),
3622 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003623 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00003624 /*BitWidth=*/0,
3625 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003626 Field->setAccess(AS_public);
Mike Stump083c25e2009-10-22 00:49:09 +00003627 T->addDecl(Field);
3628 }
3629
Douglas Gregor838db382010-02-11 01:19:42 +00003630 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003631
3632 BlockDescriptorExtendedType = T;
3633
3634 return getTagDeclType(BlockDescriptorExtendedType);
3635}
3636
3637void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3638 const RecordType *Rec = T->getAs<RecordType>();
3639 assert(Rec && "Invalid BlockDescriptorType");
3640 BlockDescriptorExtendedType = Rec->getDecl();
3641}
3642
Jay Foad4ba2a172011-01-12 09:06:06 +00003643bool ASTContext::BlockRequiresCopying(QualType Ty) const {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003644 if (Ty->isBlockPointerType())
3645 return true;
3646 if (isObjCNSObjectType(Ty))
3647 return true;
3648 if (Ty->isObjCObjectPointerType())
3649 return true;
Fariborz Jahaniane38be612010-11-17 00:21:28 +00003650 if (getLangOptions().CPlusPlus) {
3651 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3652 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3653 return RD->hasConstCopyConstructor(*this);
3654
3655 }
3656 }
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003657 return false;
3658}
3659
Jay Foad4ba2a172011-01-12 09:06:06 +00003660QualType
3661ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003662 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003663 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003664 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003665 // unsigned int __flags;
3666 // unsigned int __size;
Eli Friedmana7e68452010-08-22 01:00:03 +00003667 // void *__copy_helper; // as needed
3668 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003669 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003670 // } *
3671
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003672 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3673
3674 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003675 llvm::SmallString<36> Name;
3676 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3677 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003678 RecordDecl *T;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003679 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003680 T->startDefinition();
3681 QualType Int32Ty = IntTy;
3682 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3683 QualType FieldTypes[] = {
3684 getPointerType(VoidPtrTy),
3685 getPointerType(getTagDeclType(T)),
3686 Int32Ty,
3687 Int32Ty,
3688 getPointerType(VoidPtrTy),
3689 getPointerType(VoidPtrTy),
3690 Ty
3691 };
3692
Daniel Dunbar4087f272010-08-17 22:39:59 +00003693 llvm::StringRef FieldNames[] = {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003694 "__isa",
3695 "__forwarding",
3696 "__flags",
3697 "__size",
3698 "__copy_helper",
3699 "__destroy_helper",
3700 DeclName,
3701 };
3702
3703 for (size_t i = 0; i < 7; ++i) {
3704 if (!HasCopyAndDispose && i >=4 && i <= 5)
3705 continue;
3706 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003707 SourceLocation(),
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003708 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003709 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003710 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003711 Field->setAccess(AS_public);
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003712 T->addDecl(Field);
3713 }
3714
Douglas Gregor838db382010-02-11 01:19:42 +00003715 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003716
3717 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003718}
3719
Douglas Gregor319ac892009-04-23 22:29:11 +00003720void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003721 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003722 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3723 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3724}
3725
Anders Carlssone8c49532007-10-29 06:33:42 +00003726// This returns true if a type has been typedefed to BOOL:
3727// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003728static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003729 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003730 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3731 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003732
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003733 return false;
3734}
3735
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003736/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003737/// purpose.
Jay Foad4ba2a172011-01-12 09:06:06 +00003738CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
Ken Dyck199c3d62010-01-11 17:06:35 +00003739 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003740
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003741 // Make all integer and enum types at least as large as an int
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003742 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003743 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003744 // Treat arrays as pointers, since that's how they're passed in.
3745 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003746 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003747 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003748}
3749
3750static inline
3751std::string charUnitsToString(const CharUnits &CU) {
3752 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003753}
3754
John McCall6b5a61b2011-02-07 10:33:21 +00003755/// getObjCEncodingForBlock - Return the encoded type for this block
David Chisnall5e530af2009-11-17 19:33:30 +00003756/// declaration.
John McCall6b5a61b2011-02-07 10:33:21 +00003757std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
3758 std::string S;
3759
David Chisnall5e530af2009-11-17 19:33:30 +00003760 const BlockDecl *Decl = Expr->getBlockDecl();
3761 QualType BlockTy =
3762 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3763 // Encode result type.
John McCallc71a4912010-06-04 19:02:56 +00003764 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall5e530af2009-11-17 19:33:30 +00003765 // Compute size of all parameters.
3766 // Start with computing size of a pointer in number of bytes.
3767 // FIXME: There might(should) be a better way of doing this computation!
3768 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003769 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3770 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003771 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall5e530af2009-11-17 19:33:30 +00003772 E = Decl->param_end(); PI != E; ++PI) {
3773 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003774 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003775 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003776 ParmOffset += sz;
3777 }
3778 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003779 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003780 // Block pointer and offset.
3781 S += "@?0";
3782 ParmOffset = PtrSize;
3783
3784 // Argument types.
3785 ParmOffset = PtrSize;
3786 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3787 Decl->param_end(); PI != E; ++PI) {
3788 ParmVarDecl *PVDecl = *PI;
3789 QualType PType = PVDecl->getOriginalType();
3790 if (const ArrayType *AT =
3791 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3792 // Use array's original type only if it has known number of
3793 // elements.
3794 if (!isa<ConstantArrayType>(AT))
3795 PType = PVDecl->getType();
3796 } else if (PType->isFunctionType())
3797 PType = PVDecl->getType();
3798 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003799 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003800 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003801 }
John McCall6b5a61b2011-02-07 10:33:21 +00003802
3803 return S;
David Chisnall5e530af2009-11-17 19:33:30 +00003804}
3805
David Chisnall5389f482010-12-30 14:05:53 +00003806void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
3807 std::string& S) {
3808 // Encode result type.
3809 getObjCEncodingForType(Decl->getResultType(), S);
3810 CharUnits ParmOffset;
3811 // Compute size of all parameters.
3812 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3813 E = Decl->param_end(); PI != E; ++PI) {
3814 QualType PType = (*PI)->getType();
3815 CharUnits sz = getObjCEncodingTypeSize(PType);
3816 assert (sz.isPositive() &&
3817 "getObjCEncodingForMethodDecl - Incomplete param type");
3818 ParmOffset += sz;
3819 }
3820 S += charUnitsToString(ParmOffset);
3821 ParmOffset = CharUnits::Zero();
3822
3823 // Argument types.
3824 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3825 E = Decl->param_end(); PI != E; ++PI) {
3826 ParmVarDecl *PVDecl = *PI;
3827 QualType PType = PVDecl->getOriginalType();
3828 if (const ArrayType *AT =
3829 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3830 // Use array's original type only if it has known number of
3831 // elements.
3832 if (!isa<ConstantArrayType>(AT))
3833 PType = PVDecl->getType();
3834 } else if (PType->isFunctionType())
3835 PType = PVDecl->getType();
3836 getObjCEncodingForType(PType, S);
3837 S += charUnitsToString(ParmOffset);
3838 ParmOffset += getObjCEncodingTypeSize(PType);
3839 }
3840}
3841
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003842/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003843/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003844void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Jay Foad4ba2a172011-01-12 09:06:06 +00003845 std::string& S) const {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003846 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003847 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003848 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003849 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003850 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003851 // Compute size of all parameters.
3852 // Start with computing size of a pointer in number of bytes.
3853 // FIXME: There might(should) be a better way of doing this computation!
3854 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003855 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003856 // The first two arguments (self and _cmd) are pointers; account for
3857 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003858 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003859 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003860 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003861 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003862 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003863 assert (sz.isPositive() &&
3864 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003865 ParmOffset += sz;
3866 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003867 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003868 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003869 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003870
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003871 // Argument types.
3872 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003873 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003874 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003875 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003876 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003877 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003878 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3879 // Use array's original type only if it has known number of
3880 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003881 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003882 PType = PVDecl->getType();
3883 } else if (PType->isFunctionType())
3884 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003885 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003886 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003887 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003888 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003889 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003890 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003891 }
3892}
3893
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003894/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003895/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003896/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3897/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003898/// Property attributes are stored as a comma-delimited C string. The simple
3899/// attributes readonly and bycopy are encoded as single characters. The
3900/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3901/// encoded as single characters, followed by an identifier. Property types
3902/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003903/// these attributes are defined by the following enumeration:
3904/// @code
3905/// enum PropertyAttributes {
3906/// kPropertyReadOnly = 'R', // property is read-only.
3907/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3908/// kPropertyByref = '&', // property is a reference to the value last assigned
3909/// kPropertyDynamic = 'D', // property is dynamic
3910/// kPropertyGetter = 'G', // followed by getter selector name
3911/// kPropertySetter = 'S', // followed by setter selector name
3912/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3913/// kPropertyType = 't' // followed by old-style type encoding.
3914/// kPropertyWeak = 'W' // 'weak' property
3915/// kPropertyStrong = 'P' // property GC'able
3916/// kPropertyNonAtomic = 'N' // property non-atomic
3917/// };
3918/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003919void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003920 const Decl *Container,
Jay Foad4ba2a172011-01-12 09:06:06 +00003921 std::string& S) const {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003922 // Collect information from the property implementation decl(s).
3923 bool Dynamic = false;
3924 ObjCPropertyImplDecl *SynthesizePID = 0;
3925
3926 // FIXME: Duplicated code due to poor abstraction.
3927 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003928 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003929 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3930 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003931 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003932 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003933 ObjCPropertyImplDecl *PID = *i;
3934 if (PID->getPropertyDecl() == PD) {
3935 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3936 Dynamic = true;
3937 } else {
3938 SynthesizePID = PID;
3939 }
3940 }
3941 }
3942 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003943 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003944 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003945 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003946 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003947 ObjCPropertyImplDecl *PID = *i;
3948 if (PID->getPropertyDecl() == PD) {
3949 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3950 Dynamic = true;
3951 } else {
3952 SynthesizePID = PID;
3953 }
3954 }
Mike Stump1eb44332009-09-09 15:08:12 +00003955 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003956 }
3957 }
3958
3959 // FIXME: This is not very efficient.
3960 S = "T";
3961
3962 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003963 // GCC has some special rules regarding encoding of properties which
3964 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003965 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003966 true /* outermost type */,
3967 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003968
3969 if (PD->isReadOnly()) {
3970 S += ",R";
3971 } else {
3972 switch (PD->getSetterKind()) {
3973 case ObjCPropertyDecl::Assign: break;
3974 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003975 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003976 }
3977 }
3978
3979 // It really isn't clear at all what this means, since properties
3980 // are "dynamic by default".
3981 if (Dynamic)
3982 S += ",D";
3983
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003984 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3985 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003986
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003987 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3988 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003989 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003990 }
3991
3992 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3993 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003994 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003995 }
3996
3997 if (SynthesizePID) {
3998 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3999 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00004000 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004001 }
4002
4003 // FIXME: OBJCGC: weak & strong
4004}
4005
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004006/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00004007/// Another legacy compatibility encoding: 32-bit longs are encoded as
4008/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004009/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4010///
4011void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00004012 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00004013 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Jay Foad4ba2a172011-01-12 09:06:06 +00004014 if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004015 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00004016 else
Jay Foad4ba2a172011-01-12 09:06:06 +00004017 if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004018 PointeeTy = IntTy;
4019 }
4020 }
4021}
4022
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00004023void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Jay Foad4ba2a172011-01-12 09:06:06 +00004024 const FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00004025 // We follow the behavior of gcc, expanding structures which are
4026 // directly pointed to, and expanding embedded structures. Note that
4027 // these rules are sufficient to prevent recursive encoding of the
4028 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00004029 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00004030 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00004031}
4032
David Chisnall64fd7e82010-06-04 01:10:52 +00004033static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
4034 switch (T->getAs<BuiltinType>()->getKind()) {
4035 default: assert(0 && "Unhandled builtin type kind");
4036 case BuiltinType::Void: return 'v';
4037 case BuiltinType::Bool: return 'B';
4038 case BuiltinType::Char_U:
4039 case BuiltinType::UChar: return 'C';
4040 case BuiltinType::UShort: return 'S';
4041 case BuiltinType::UInt: return 'I';
4042 case BuiltinType::ULong:
Jay Foad4ba2a172011-01-12 09:06:06 +00004043 return C->getIntWidth(T) == 32 ? 'L' : 'Q';
David Chisnall64fd7e82010-06-04 01:10:52 +00004044 case BuiltinType::UInt128: return 'T';
4045 case BuiltinType::ULongLong: return 'Q';
4046 case BuiltinType::Char_S:
4047 case BuiltinType::SChar: return 'c';
4048 case BuiltinType::Short: return 's';
Chris Lattner3f59c972010-12-25 23:25:43 +00004049 case BuiltinType::WChar_S:
4050 case BuiltinType::WChar_U:
David Chisnall64fd7e82010-06-04 01:10:52 +00004051 case BuiltinType::Int: return 'i';
4052 case BuiltinType::Long:
Jay Foad4ba2a172011-01-12 09:06:06 +00004053 return C->getIntWidth(T) == 32 ? 'l' : 'q';
David Chisnall64fd7e82010-06-04 01:10:52 +00004054 case BuiltinType::LongLong: return 'q';
4055 case BuiltinType::Int128: return 't';
4056 case BuiltinType::Float: return 'f';
4057 case BuiltinType::Double: return 'd';
Daniel Dunbar3a0be842010-10-11 21:13:48 +00004058 case BuiltinType::LongDouble: return 'D';
David Chisnall64fd7e82010-06-04 01:10:52 +00004059 }
4060}
4061
Jay Foad4ba2a172011-01-12 09:06:06 +00004062static void EncodeBitField(const ASTContext *Ctx, std::string& S,
David Chisnall64fd7e82010-06-04 01:10:52 +00004063 QualType T, const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00004064 const Expr *E = FD->getBitWidth();
4065 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00004066 S += 'b';
David Chisnall64fd7e82010-06-04 01:10:52 +00004067 // The NeXT runtime encodes bit fields as b followed by the number of bits.
4068 // The GNU runtime requires more information; bitfields are encoded as b,
4069 // then the offset (in bits) of the first element, then the type of the
4070 // bitfield, then the size in bits. For example, in this structure:
4071 //
4072 // struct
4073 // {
4074 // int integer;
4075 // int flags:2;
4076 // };
4077 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
4078 // runtime, but b32i2 for the GNU runtime. The reason for this extra
4079 // information is not especially sensible, but we're stuck with it for
4080 // compatibility with GCC, although providing it breaks anything that
4081 // actually uses runtime introspection and wants to work on both runtimes...
4082 if (!Ctx->getLangOptions().NeXTRuntime) {
4083 const RecordDecl *RD = FD->getParent();
4084 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
4085 // FIXME: This same linear search is also used in ExprConstant - it might
4086 // be better if the FieldDecl stored its offset. We'd be increasing the
4087 // size of the object slightly, but saving some time every time it is used.
4088 unsigned i = 0;
4089 for (RecordDecl::field_iterator Field = RD->field_begin(),
4090 FieldEnd = RD->field_end();
4091 Field != FieldEnd; (void)++Field, ++i) {
4092 if (*Field == FD)
4093 break;
4094 }
4095 S += llvm::utostr(RL.getFieldOffset(i));
David Chisnallc7ff82c2010-12-26 20:12:30 +00004096 if (T->isEnumeralType())
4097 S += 'i';
4098 else
Jay Foad4ba2a172011-01-12 09:06:06 +00004099 S += ObjCEncodingForPrimitiveKind(Ctx, T);
David Chisnall64fd7e82010-06-04 01:10:52 +00004100 }
4101 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00004102 S += llvm::utostr(N);
4103}
4104
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004105// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00004106void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
4107 bool ExpandPointedToStructures,
4108 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00004109 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00004110 bool OutermostType,
Jay Foad4ba2a172011-01-12 09:06:06 +00004111 bool EncodingProperty) const {
David Chisnall64fd7e82010-06-04 01:10:52 +00004112 if (T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004113 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00004114 return EncodeBitField(this, S, T, FD);
4115 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004116 return;
4117 }
Mike Stump1eb44332009-09-09 15:08:12 +00004118
John McCall183700f2009-09-21 23:43:11 +00004119 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00004120 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00004121 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00004122 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004123 return;
4124 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00004125
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00004126 // encoding for pointer or r3eference types.
4127 QualType PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00004128 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00004129 if (PT->isObjCSelType()) {
4130 S += ':';
4131 return;
4132 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00004133 PointeeTy = PT->getPointeeType();
4134 }
4135 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4136 PointeeTy = RT->getPointeeType();
4137 if (!PointeeTy.isNull()) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004138 bool isReadOnly = false;
4139 // For historical/compatibility reasons, the read-only qualifier of the
4140 // pointee gets emitted _before_ the '^'. The read-only qualifier of
4141 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00004142 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00004143 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004144 if (OutermostType && T.isConstQualified()) {
4145 isReadOnly = true;
4146 S += 'r';
4147 }
Mike Stump9fdbab32009-07-31 02:02:20 +00004148 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004149 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00004150 while (P->getAs<PointerType>())
4151 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004152 if (P.isConstQualified()) {
4153 isReadOnly = true;
4154 S += 'r';
4155 }
4156 }
4157 if (isReadOnly) {
4158 // Another legacy compatibility encoding. Some ObjC qualifier and type
4159 // combinations need to be rearranged.
4160 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer02379412010-04-27 17:12:11 +00004161 if (llvm::StringRef(S).endswith("nr"))
4162 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004163 }
Mike Stump1eb44332009-09-09 15:08:12 +00004164
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004165 if (PointeeTy->isCharType()) {
4166 // char pointer types should be encoded as '*' unless it is a
4167 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00004168 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004169 S += '*';
4170 return;
4171 }
Ted Kremenek6217b802009-07-29 21:53:49 +00004172 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00004173 // GCC binary compat: Need to convert "struct objc_class *" to "#".
4174 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
4175 S += '#';
4176 return;
4177 }
4178 // GCC binary compat: Need to convert "struct objc_object *" to "@".
4179 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
4180 S += '@';
4181 return;
4182 }
4183 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004184 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004185 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004186 getLegacyIntegralTypeEncoding(PointeeTy);
4187
Mike Stump1eb44332009-09-09 15:08:12 +00004188 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004189 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004190 return;
4191 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00004192
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004193 if (const ArrayType *AT =
4194 // Ignore type qualifiers etc.
4195 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00004196 if (isa<IncompleteArrayType>(AT)) {
4197 // Incomplete arrays are encoded as a pointer to the array element.
4198 S += '^';
4199
Mike Stump1eb44332009-09-09 15:08:12 +00004200 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00004201 false, ExpandStructures, FD);
4202 } else {
4203 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00004204
Anders Carlsson559a8332009-02-22 01:38:57 +00004205 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
4206 S += llvm::utostr(CAT->getSize().getZExtValue());
4207 else {
4208 //Variable length arrays are encoded as a regular array with 0 elements.
4209 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
4210 S += '0';
4211 }
Mike Stump1eb44332009-09-09 15:08:12 +00004212
4213 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00004214 false, ExpandStructures, FD);
4215 S += ']';
4216 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004217 return;
4218 }
Mike Stump1eb44332009-09-09 15:08:12 +00004219
John McCall183700f2009-09-21 23:43:11 +00004220 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00004221 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004222 return;
4223 }
Mike Stump1eb44332009-09-09 15:08:12 +00004224
Ted Kremenek6217b802009-07-29 21:53:49 +00004225 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00004226 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004227 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00004228 // Anonymous structures print as '?'
4229 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4230 S += II->getName();
Fariborz Jahanian6fb94392010-05-07 00:28:49 +00004231 if (ClassTemplateSpecializationDecl *Spec
4232 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
4233 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
4234 std::string TemplateArgsStr
4235 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00004236 TemplateArgs.data(),
4237 TemplateArgs.size(),
Fariborz Jahanian6fb94392010-05-07 00:28:49 +00004238 (*this).PrintingPolicy);
4239
4240 S += TemplateArgsStr;
4241 }
Daniel Dunbar502a4a12008-10-17 06:22:57 +00004242 } else {
4243 S += '?';
4244 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00004245 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00004246 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004247 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
4248 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00004249 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004250 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004251 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00004252 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004253 S += '"';
4254 }
Mike Stump1eb44332009-09-09 15:08:12 +00004255
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004256 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004257 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004258 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004259 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004260 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004261 QualType qt = Field->getType();
4262 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00004263 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004264 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004265 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00004266 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00004267 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004268 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004269 return;
4270 }
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00004271
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004272 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00004273 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00004274 EncodeBitField(this, S, T, FD);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00004275 else
4276 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004277 return;
4278 }
Mike Stump1eb44332009-09-09 15:08:12 +00004279
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004280 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00004281 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004282 return;
4283 }
Mike Stump1eb44332009-09-09 15:08:12 +00004284
John McCallc12c5bb2010-05-15 11:32:37 +00004285 // Ignore protocol qualifiers when mangling at this level.
4286 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4287 T = OT->getBaseType();
4288
John McCall0953e762009-09-24 19:53:00 +00004289 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004290 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00004291 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004292 S += '{';
4293 const IdentifierInfo *II = OI->getIdentifier();
4294 S += II->getName();
4295 S += '=';
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00004296 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
4297 DeepCollectObjCIvars(OI, true, Ivars);
4298 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4299 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4300 if (Field->isBitField())
4301 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004302 else
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00004303 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004304 }
4305 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004306 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004307 }
Mike Stump1eb44332009-09-09 15:08:12 +00004308
John McCall183700f2009-09-21 23:43:11 +00004309 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00004310 if (OPT->isObjCIdType()) {
4311 S += '@';
4312 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004313 }
Mike Stump1eb44332009-09-09 15:08:12 +00004314
Steve Naroff27d20a22009-10-28 22:03:49 +00004315 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4316 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4317 // Since this is a binary compatibility issue, need to consult with runtime
4318 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00004319 S += '#';
4320 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004321 }
Mike Stump1eb44332009-09-09 15:08:12 +00004322
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004323 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004324 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00004325 ExpandPointedToStructures,
4326 ExpandStructures, FD);
4327 if (FD || EncodingProperty) {
4328 // Note that we do extended encoding of protocol qualifer list
4329 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00004330 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00004331 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4332 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00004333 S += '<';
4334 S += (*I)->getNameAsString();
4335 S += '>';
4336 }
4337 S += '"';
4338 }
4339 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004340 }
Mike Stump1eb44332009-09-09 15:08:12 +00004341
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004342 QualType PointeeTy = OPT->getPointeeType();
4343 if (!EncodingProperty &&
4344 isa<TypedefType>(PointeeTy.getTypePtr())) {
4345 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00004346 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004347 // {...};
4348 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00004349 getObjCEncodingForTypeImpl(PointeeTy, S,
4350 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004351 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00004352 return;
4353 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004354
4355 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00004356 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004357 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00004358 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00004359 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4360 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004361 S += '<';
4362 S += (*I)->getNameAsString();
4363 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00004364 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004365 S += '"';
4366 }
4367 return;
4368 }
Mike Stump1eb44332009-09-09 15:08:12 +00004369
John McCall532ec7b2010-05-17 23:56:34 +00004370 // gcc just blithely ignores member pointers.
4371 // TODO: maybe there should be a mangling for these
4372 if (T->getAs<MemberPointerType>())
4373 return;
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00004374
4375 if (T->isVectorType()) {
4376 // This matches gcc's encoding, even though technically it is
4377 // insufficient.
4378 // FIXME. We should do a better job than gcc.
4379 return;
4380 }
4381
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004382 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004383}
4384
Mike Stump1eb44332009-09-09 15:08:12 +00004385void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00004386 std::string& S) const {
4387 if (QT & Decl::OBJC_TQ_In)
4388 S += 'n';
4389 if (QT & Decl::OBJC_TQ_Inout)
4390 S += 'N';
4391 if (QT & Decl::OBJC_TQ_Out)
4392 S += 'o';
4393 if (QT & Decl::OBJC_TQ_Bycopy)
4394 S += 'O';
4395 if (QT & Decl::OBJC_TQ_Byref)
4396 S += 'R';
4397 if (QT & Decl::OBJC_TQ_Oneway)
4398 S += 'V';
4399}
4400
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004401void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004402 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004403
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004404 BuiltinVaListType = T;
4405}
4406
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004407void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004408 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00004409}
4410
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004411void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00004412 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00004413}
4414
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004415void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004416 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00004417}
4418
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004419void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004420 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00004421}
4422
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004423void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00004424 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00004425 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004426
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004427 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00004428}
4429
John McCall0bd6feb2009-12-02 08:04:21 +00004430/// \brief Retrieve the template name that corresponds to a non-empty
4431/// lookup.
Jay Foad4ba2a172011-01-12 09:06:06 +00004432TemplateName
4433ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4434 UnresolvedSetIterator End) const {
John McCall0bd6feb2009-12-02 08:04:21 +00004435 unsigned size = End - Begin;
4436 assert(size > 1 && "set is not overloaded!");
4437
4438 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4439 size * sizeof(FunctionTemplateDecl*));
4440 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4441
4442 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00004443 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00004444 NamedDecl *D = *I;
4445 assert(isa<FunctionTemplateDecl>(D) ||
4446 (isa<UsingShadowDecl>(D) &&
4447 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4448 *Storage++ = D;
4449 }
4450
4451 return TemplateName(OT);
4452}
4453
Douglas Gregor7532dc62009-03-30 22:58:21 +00004454/// \brief Retrieve the template name that represents a qualified
4455/// template name such as \c std::vector.
Jay Foad4ba2a172011-01-12 09:06:06 +00004456TemplateName
4457ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
4458 bool TemplateKeyword,
4459 TemplateDecl *Template) const {
Douglas Gregor0f0ea2a2011-03-03 17:04:51 +00004460 assert(NNS && "Missing nested-name-specifier in qualified template name");
4461
Douglas Gregor789b1f62010-02-04 18:10:26 +00004462 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00004463 llvm::FoldingSetNodeID ID;
4464 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4465
4466 void *InsertPos = 0;
4467 QualifiedTemplateName *QTN =
4468 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4469 if (!QTN) {
4470 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4471 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4472 }
4473
4474 return TemplateName(QTN);
4475}
4476
4477/// \brief Retrieve the template name that represents a dependent
4478/// template name such as \c MetaFun::template apply.
Jay Foad4ba2a172011-01-12 09:06:06 +00004479TemplateName
4480ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4481 const IdentifierInfo *Name) const {
Mike Stump1eb44332009-09-09 15:08:12 +00004482 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00004483 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00004484
4485 llvm::FoldingSetNodeID ID;
4486 DependentTemplateName::Profile(ID, NNS, Name);
4487
4488 void *InsertPos = 0;
4489 DependentTemplateName *QTN =
4490 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4491
4492 if (QTN)
4493 return TemplateName(QTN);
4494
4495 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4496 if (CanonNNS == NNS) {
4497 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4498 } else {
4499 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4500 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004501 DependentTemplateName *CheckQTN =
4502 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4503 assert(!CheckQTN && "Dependent type name canonicalization broken");
4504 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00004505 }
4506
4507 DependentTemplateNames.InsertNode(QTN, InsertPos);
4508 return TemplateName(QTN);
4509}
4510
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004511/// \brief Retrieve the template name that represents a dependent
4512/// template name such as \c MetaFun::template operator+.
4513TemplateName
4514ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Jay Foad4ba2a172011-01-12 09:06:06 +00004515 OverloadedOperatorKind Operator) const {
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004516 assert((!NNS || NNS->isDependent()) &&
4517 "Nested name specifier must be dependent");
4518
4519 llvm::FoldingSetNodeID ID;
4520 DependentTemplateName::Profile(ID, NNS, Operator);
4521
4522 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00004523 DependentTemplateName *QTN
4524 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004525
4526 if (QTN)
4527 return TemplateName(QTN);
4528
4529 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4530 if (CanonNNS == NNS) {
4531 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4532 } else {
4533 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4534 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004535
4536 DependentTemplateName *CheckQTN
4537 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4538 assert(!CheckQTN && "Dependent template name canonicalization broken");
4539 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004540 }
4541
4542 DependentTemplateNames.InsertNode(QTN, InsertPos);
4543 return TemplateName(QTN);
4544}
4545
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004546TemplateName
4547ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
4548 const TemplateArgument &ArgPack) const {
4549 ASTContext &Self = const_cast<ASTContext &>(*this);
4550 llvm::FoldingSetNodeID ID;
4551 SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
4552
4553 void *InsertPos = 0;
4554 SubstTemplateTemplateParmPackStorage *Subst
4555 = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
4556
4557 if (!Subst) {
4558 Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param,
4559 ArgPack.pack_size(),
4560 ArgPack.pack_begin());
4561 SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
4562 }
4563
4564 return TemplateName(Subst);
4565}
4566
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004567/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00004568/// TargetInfo, produce the corresponding type. The unsigned @p Type
4569/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00004570CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004571 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00004572 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004573 case TargetInfo::SignedShort: return ShortTy;
4574 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4575 case TargetInfo::SignedInt: return IntTy;
4576 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4577 case TargetInfo::SignedLong: return LongTy;
4578 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4579 case TargetInfo::SignedLongLong: return LongLongTy;
4580 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4581 }
4582
4583 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00004584 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004585}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00004586
4587//===----------------------------------------------------------------------===//
4588// Type Predicates.
4589//===----------------------------------------------------------------------===//
4590
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004591/// isObjCNSObjectType - Return true if this is an NSObject object using
4592/// NSObject attribute on a c-style pointer type.
4593/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00004594/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004595///
4596bool ASTContext::isObjCNSObjectType(QualType Ty) const {
John McCallf4c73712011-01-19 06:33:43 +00004597 if (const TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
Richard Smith162e1c12011-04-15 14:24:37 +00004598 if (TypedefNameDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00004599 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004600 return true;
4601 }
Mike Stump1eb44332009-09-09 15:08:12 +00004602 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004603}
4604
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004605/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4606/// garbage collection attribute.
4607///
John McCallae278a32011-01-12 00:34:59 +00004608Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
4609 if (getLangOptions().getGCMode() == LangOptions::NonGC)
4610 return Qualifiers::GCNone;
4611
4612 assert(getLangOptions().ObjC1);
4613 Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
4614
4615 // Default behaviour under objective-C's gc is for ObjC pointers
4616 // (or pointers to them) be treated as though they were declared
4617 // as __strong.
4618 if (GCAttrs == Qualifiers::GCNone) {
4619 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
4620 return Qualifiers::Strong;
4621 else if (Ty->isPointerType())
4622 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
4623 } else {
4624 // It's not valid to set GC attributes on anything that isn't a
4625 // pointer.
4626#ifndef NDEBUG
4627 QualType CT = Ty->getCanonicalTypeInternal();
4628 while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
4629 CT = AT->getElementType();
4630 assert(CT->isAnyPointerType() || CT->isBlockPointerType());
4631#endif
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004632 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00004633 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004634}
4635
Chris Lattner6ac46a42008-04-07 06:51:04 +00004636//===----------------------------------------------------------------------===//
4637// Type Compatibility Testing
4638//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00004639
Mike Stump1eb44332009-09-09 15:08:12 +00004640/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004641/// compatible.
4642static bool areCompatVectorTypes(const VectorType *LHS,
4643 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00004644 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00004645 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00004646 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00004647}
4648
Douglas Gregor255210e2010-08-06 10:14:59 +00004649bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4650 QualType SecondVec) {
4651 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4652 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4653
4654 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4655 return true;
4656
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004657 // Treat Neon vector types and most AltiVec vector types as if they are the
4658 // equivalent GCC vector types.
Douglas Gregor255210e2010-08-06 10:14:59 +00004659 const VectorType *First = FirstVec->getAs<VectorType>();
4660 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004661 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor255210e2010-08-06 10:14:59 +00004662 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004663 First->getVectorKind() != VectorType::AltiVecPixel &&
4664 First->getVectorKind() != VectorType::AltiVecBool &&
4665 Second->getVectorKind() != VectorType::AltiVecPixel &&
4666 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor255210e2010-08-06 10:14:59 +00004667 return true;
4668
4669 return false;
4670}
4671
Steve Naroff4084c302009-07-23 01:01:38 +00004672//===----------------------------------------------------------------------===//
4673// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4674//===----------------------------------------------------------------------===//
4675
4676/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4677/// inheritance hierarchy of 'rProto'.
Jay Foad4ba2a172011-01-12 09:06:06 +00004678bool
4679ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4680 ObjCProtocolDecl *rProto) const {
Steve Naroff4084c302009-07-23 01:01:38 +00004681 if (lProto == rProto)
4682 return true;
4683 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4684 E = rProto->protocol_end(); PI != E; ++PI)
4685 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4686 return true;
4687 return false;
4688}
4689
Steve Naroff4084c302009-07-23 01:01:38 +00004690/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4691/// return true if lhs's protocols conform to rhs's protocol; false
4692/// otherwise.
4693bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4694 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4695 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4696 return false;
4697}
4698
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004699/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4700/// Class<p1, ...>.
4701bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4702 QualType rhs) {
4703 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4704 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4705 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4706
4707 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4708 E = lhsQID->qual_end(); I != E; ++I) {
4709 bool match = false;
4710 ObjCProtocolDecl *lhsProto = *I;
4711 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4712 E = rhsOPT->qual_end(); J != E; ++J) {
4713 ObjCProtocolDecl *rhsProto = *J;
4714 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4715 match = true;
4716 break;
4717 }
4718 }
4719 if (!match)
4720 return false;
4721 }
4722 return true;
4723}
4724
Steve Naroff4084c302009-07-23 01:01:38 +00004725/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4726/// ObjCQualifiedIDType.
4727bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4728 bool compare) {
4729 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00004730 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004731 lhs->isObjCIdType() || lhs->isObjCClassType())
4732 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004733 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004734 rhs->isObjCIdType() || rhs->isObjCClassType())
4735 return true;
4736
4737 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00004738 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004739
Steve Naroff4084c302009-07-23 01:01:38 +00004740 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004741
Steve Naroff4084c302009-07-23 01:01:38 +00004742 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004743 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00004744 // make sure we check the class hierarchy.
4745 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4746 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4747 E = lhsQID->qual_end(); I != E; ++I) {
4748 // when comparing an id<P> on lhs with a static type on rhs,
4749 // see if static class implements all of id's protocols, directly or
4750 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004751 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00004752 return false;
4753 }
4754 }
4755 // If there are no qualifiers and no interface, we have an 'id'.
4756 return true;
4757 }
Mike Stump1eb44332009-09-09 15:08:12 +00004758 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004759 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4760 E = lhsQID->qual_end(); I != E; ++I) {
4761 ObjCProtocolDecl *lhsProto = *I;
4762 bool match = false;
4763
4764 // when comparing an id<P> on lhs with a static type on rhs,
4765 // see if static class implements all of id's protocols, directly or
4766 // through its super class and categories.
4767 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4768 E = rhsOPT->qual_end(); J != E; ++J) {
4769 ObjCProtocolDecl *rhsProto = *J;
4770 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4771 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4772 match = true;
4773 break;
4774 }
4775 }
Mike Stump1eb44332009-09-09 15:08:12 +00004776 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004777 // make sure we check the class hierarchy.
4778 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4779 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4780 E = lhsQID->qual_end(); I != E; ++I) {
4781 // when comparing an id<P> on lhs with a static type on rhs,
4782 // see if static class implements all of id's protocols, directly or
4783 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004784 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004785 match = true;
4786 break;
4787 }
4788 }
4789 }
4790 if (!match)
4791 return false;
4792 }
Mike Stump1eb44332009-09-09 15:08:12 +00004793
Steve Naroff4084c302009-07-23 01:01:38 +00004794 return true;
4795 }
Mike Stump1eb44332009-09-09 15:08:12 +00004796
Steve Naroff4084c302009-07-23 01:01:38 +00004797 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4798 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4799
Mike Stump1eb44332009-09-09 15:08:12 +00004800 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004801 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004802 // If both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004803 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4804 E = lhsOPT->qual_end(); I != E; ++I) {
4805 ObjCProtocolDecl *lhsProto = *I;
4806 bool match = false;
4807
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004808 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff4084c302009-07-23 01:01:38 +00004809 // see if static class implements all of id's protocols, directly or
4810 // through its super class and categories.
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004811 // First, lhs protocols in the qualifier list must be found, direct
4812 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff4084c302009-07-23 01:01:38 +00004813 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4814 E = rhsQID->qual_end(); J != E; ++J) {
4815 ObjCProtocolDecl *rhsProto = *J;
4816 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4817 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4818 match = true;
4819 break;
4820 }
4821 }
4822 if (!match)
4823 return false;
4824 }
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004825
4826 // Static class's protocols, or its super class or category protocols
4827 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4828 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4829 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4830 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4831 // This is rather dubious but matches gcc's behavior. If lhs has
4832 // no type qualifier and its class has no static protocol(s)
4833 // assume that it is mismatch.
4834 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4835 return false;
4836 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4837 LHSInheritedProtocols.begin(),
4838 E = LHSInheritedProtocols.end(); I != E; ++I) {
4839 bool match = false;
4840 ObjCProtocolDecl *lhsProto = (*I);
4841 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4842 E = rhsQID->qual_end(); J != E; ++J) {
4843 ObjCProtocolDecl *rhsProto = *J;
4844 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4845 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4846 match = true;
4847 break;
4848 }
4849 }
4850 if (!match)
4851 return false;
4852 }
4853 }
Steve Naroff4084c302009-07-23 01:01:38 +00004854 return true;
4855 }
4856 return false;
4857}
4858
Eli Friedman3d815e72008-08-22 00:56:42 +00004859/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004860/// compatible for assignment from RHS to LHS. This handles validation of any
4861/// protocol qualifiers on the LHS or RHS.
4862///
Steve Naroff14108da2009-07-10 23:34:53 +00004863bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4864 const ObjCObjectPointerType *RHSOPT) {
John McCallc12c5bb2010-05-15 11:32:37 +00004865 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4866 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4867
Steve Naroffde2e22d2009-07-15 18:40:39 +00004868 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCallc12c5bb2010-05-15 11:32:37 +00004869 if (LHS->isObjCUnqualifiedIdOrClass() ||
4870 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff14108da2009-07-10 23:34:53 +00004871 return true;
4872
John McCallc12c5bb2010-05-15 11:32:37 +00004873 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump1eb44332009-09-09 15:08:12 +00004874 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4875 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004876 false);
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004877
4878 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4879 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4880 QualType(RHSOPT,0));
4881
John McCallc12c5bb2010-05-15 11:32:37 +00004882 // If we have 2 user-defined types, fall into that path.
4883 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff4084c302009-07-23 01:01:38 +00004884 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004885
Steve Naroff4084c302009-07-23 01:01:38 +00004886 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004887}
4888
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004889/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004890/// for providing type-safety for objective-c pointers used to pass/return
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004891/// arguments in block literals. When passed as arguments, passing 'A*' where
4892/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4893/// not OK. For the return type, the opposite is not OK.
4894bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4895 const ObjCObjectPointerType *LHSOPT,
Fariborz Jahaniana4fdbfa2011-03-14 16:07:00 +00004896 const ObjCObjectPointerType *RHSOPT,
4897 bool BlockReturnType) {
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004898 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004899 return true;
4900
4901 if (LHSOPT->isObjCBuiltinType()) {
4902 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4903 }
4904
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004905 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004906 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4907 QualType(RHSOPT,0),
4908 false);
4909
4910 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4911 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4912 if (LHS && RHS) { // We have 2 user-defined types.
4913 if (LHS != RHS) {
4914 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
Fariborz Jahaniana4fdbfa2011-03-14 16:07:00 +00004915 return BlockReturnType;
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004916 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
Fariborz Jahaniana4fdbfa2011-03-14 16:07:00 +00004917 return !BlockReturnType;
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004918 }
4919 else
4920 return true;
4921 }
4922 return false;
4923}
4924
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004925/// getIntersectionOfProtocols - This routine finds the intersection of set
4926/// of protocols inherited from two distinct objective-c pointer objects.
4927/// It is used to build composite qualifier list of the composite type of
4928/// the conditional expression involving two objective-c pointer objects.
4929static
4930void getIntersectionOfProtocols(ASTContext &Context,
4931 const ObjCObjectPointerType *LHSOPT,
4932 const ObjCObjectPointerType *RHSOPT,
4933 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4934
John McCallc12c5bb2010-05-15 11:32:37 +00004935 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4936 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4937 assert(LHS->getInterface() && "LHS must have an interface base");
4938 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004939
4940 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4941 unsigned LHSNumProtocols = LHS->getNumProtocols();
4942 if (LHSNumProtocols > 0)
4943 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4944 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004945 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004946 Context.CollectInheritedProtocols(LHS->getInterface(),
4947 LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004948 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4949 LHSInheritedProtocols.end());
4950 }
4951
4952 unsigned RHSNumProtocols = RHS->getNumProtocols();
4953 if (RHSNumProtocols > 0) {
Dan Gohmancb421fa2010-04-19 16:39:44 +00004954 ObjCProtocolDecl **RHSProtocols =
4955 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004956 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4957 if (InheritedProtocolSet.count(RHSProtocols[i]))
4958 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4959 }
4960 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004961 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004962 Context.CollectInheritedProtocols(RHS->getInterface(),
4963 RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004964 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4965 RHSInheritedProtocols.begin(),
4966 E = RHSInheritedProtocols.end(); I != E; ++I)
4967 if (InheritedProtocolSet.count((*I)))
4968 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004969 }
4970}
4971
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004972/// areCommonBaseCompatible - Returns common base class of the two classes if
4973/// one found. Note that this is O'2 algorithm. But it will be called as the
4974/// last type comparison in a ?-exp of ObjC pointer types before a
4975/// warning is issued. So, its invokation is extremely rare.
4976QualType ASTContext::areCommonBaseCompatible(
John McCallc12c5bb2010-05-15 11:32:37 +00004977 const ObjCObjectPointerType *Lptr,
4978 const ObjCObjectPointerType *Rptr) {
4979 const ObjCObjectType *LHS = Lptr->getObjectType();
4980 const ObjCObjectType *RHS = Rptr->getObjectType();
4981 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4982 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
Fariborz Jahanian7c2bdcb2011-04-18 21:16:59 +00004983 if (!LDecl || !RDecl || (LDecl == RDecl))
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004984 return QualType();
4985
Fariborz Jahanian7c2bdcb2011-04-18 21:16:59 +00004986 do {
John McCallc12c5bb2010-05-15 11:32:37 +00004987 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004988 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCallc12c5bb2010-05-15 11:32:37 +00004989 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4990 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4991
4992 QualType Result = QualType(LHS, 0);
4993 if (!Protocols.empty())
4994 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4995 Result = getObjCObjectPointerType(Result);
4996 return Result;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004997 }
Fariborz Jahanian7c2bdcb2011-04-18 21:16:59 +00004998 } while ((LDecl = LDecl->getSuperClass()));
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004999
5000 return QualType();
5001}
5002
John McCallc12c5bb2010-05-15 11:32:37 +00005003bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
5004 const ObjCObjectType *RHS) {
5005 assert(LHS->getInterface() && "LHS is not an interface type");
5006 assert(RHS->getInterface() && "RHS is not an interface type");
5007
Chris Lattner6ac46a42008-04-07 06:51:04 +00005008 // Verify that the base decls are compatible: the RHS must be a subclass of
5009 // the LHS.
John McCallc12c5bb2010-05-15 11:32:37 +00005010 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner6ac46a42008-04-07 06:51:04 +00005011 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00005012
Chris Lattner6ac46a42008-04-07 06:51:04 +00005013 // RHS must have a superset of the protocols in the LHS. If the LHS is not
5014 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00005015 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00005016 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00005017
Fariborz Jahanianb7bc34a2011-04-08 18:25:29 +00005018 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't,
5019 // more detailed analysis is required.
5020 if (RHS->getNumProtocols() == 0) {
5021 // OK, if LHS is a superclass of RHS *and*
5022 // this superclass is assignment compatible with LHS.
5023 // false otherwise.
Fariborz Jahanian627788c2011-04-12 16:34:14 +00005024 bool IsSuperClass =
5025 LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5026 if (IsSuperClass) {
Fariborz Jahanianb7bc34a2011-04-08 18:25:29 +00005027 // OK if conversion of LHS to SuperClass results in narrowing of types
5028 // ; i.e., SuperClass may implement at least one of the protocols
5029 // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5030 // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5031 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
Fariborz Jahanian627788c2011-04-12 16:34:14 +00005032 CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
Fariborz Jahanianb7bc34a2011-04-08 18:25:29 +00005033 // If super class has no protocols, it is not a match.
5034 if (SuperClassInheritedProtocols.empty())
5035 return false;
5036
5037 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5038 LHSPE = LHS->qual_end();
5039 LHSPI != LHSPE; LHSPI++) {
5040 bool SuperImplementsProtocol = false;
5041 ObjCProtocolDecl *LHSProto = (*LHSPI);
5042
5043 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5044 SuperClassInheritedProtocols.begin(),
5045 E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5046 ObjCProtocolDecl *SuperClassProto = (*I);
5047 if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5048 SuperImplementsProtocol = true;
5049 break;
5050 }
5051 }
5052 if (!SuperImplementsProtocol)
5053 return false;
5054 }
5055 return true;
5056 }
5057 return false;
5058 }
Mike Stump1eb44332009-09-09 15:08:12 +00005059
John McCallc12c5bb2010-05-15 11:32:37 +00005060 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5061 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00005062 LHSPI != LHSPE; LHSPI++) {
5063 bool RHSImplementsProtocol = false;
5064
5065 // If the RHS doesn't implement the protocol on the left, the types
5066 // are incompatible.
John McCallc12c5bb2010-05-15 11:32:37 +00005067 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5068 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00005069 RHSPI != RHSPE; RHSPI++) {
5070 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00005071 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00005072 break;
5073 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00005074 }
5075 // FIXME: For better diagnostics, consider passing back the protocol name.
5076 if (!RHSImplementsProtocol)
5077 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00005078 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00005079 // The RHS implements all protocols listed on the LHS.
5080 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00005081}
5082
Steve Naroff389bf462009-02-12 17:52:19 +00005083bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5084 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00005085 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5086 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00005087
Steve Naroff14108da2009-07-10 23:34:53 +00005088 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00005089 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00005090
5091 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
5092 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00005093}
5094
Douglas Gregor569c3162010-08-07 11:51:51 +00005095bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5096 return canAssignObjCInterfaces(
5097 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5098 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5099}
5100
Mike Stump1eb44332009-09-09 15:08:12 +00005101/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00005102/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00005103/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00005104/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor447234d2010-07-29 15:18:02 +00005105bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5106 bool CompareUnqualified) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00005107 if (getLangOptions().CPlusPlus)
5108 return hasSameType(LHS, RHS);
5109
Douglas Gregor447234d2010-07-29 15:18:02 +00005110 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman3d815e72008-08-22 00:56:42 +00005111}
5112
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005113bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5114 return !mergeTypes(LHS, RHS, true).isNull();
5115}
5116
Peter Collingbourne48466752010-10-24 18:30:18 +00005117/// mergeTransparentUnionType - if T is a transparent union type and a member
5118/// of T is compatible with SubType, return the merged type, else return
5119/// QualType()
5120QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
5121 bool OfBlockPointer,
5122 bool Unqualified) {
5123 if (const RecordType *UT = T->getAsUnionType()) {
5124 RecordDecl *UD = UT->getDecl();
5125 if (UD->hasAttr<TransparentUnionAttr>()) {
5126 for (RecordDecl::field_iterator it = UD->field_begin(),
5127 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbournef91d7572010-12-02 21:00:06 +00005128 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbourne48466752010-10-24 18:30:18 +00005129 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
5130 if (!MT.isNull())
5131 return MT;
5132 }
5133 }
5134 }
5135
5136 return QualType();
5137}
5138
5139/// mergeFunctionArgumentTypes - merge two types which appear as function
5140/// argument types
5141QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
5142 bool OfBlockPointer,
5143 bool Unqualified) {
5144 // GNU extension: two types are compatible if they appear as a function
5145 // argument, one of the types is a transparent union type and the other
5146 // type is compatible with a union member
5147 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
5148 Unqualified);
5149 if (!lmerge.isNull())
5150 return lmerge;
5151
5152 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
5153 Unqualified);
5154 if (!rmerge.isNull())
5155 return rmerge;
5156
5157 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
5158}
5159
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005160QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor447234d2010-07-29 15:18:02 +00005161 bool OfBlockPointer,
5162 bool Unqualified) {
John McCall183700f2009-09-21 23:43:11 +00005163 const FunctionType *lbase = lhs->getAs<FunctionType>();
5164 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00005165 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
5166 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00005167 bool allLTypes = true;
5168 bool allRTypes = true;
5169
5170 // Check return type
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005171 QualType retType;
Fariborz Jahaniand263fd12011-02-11 18:46:17 +00005172 if (OfBlockPointer) {
5173 QualType RHS = rbase->getResultType();
5174 QualType LHS = lbase->getResultType();
5175 bool UnqualifiedResult = Unqualified;
5176 if (!UnqualifiedResult)
5177 UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
Fariborz Jahaniana4fdbfa2011-03-14 16:07:00 +00005178 retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
Fariborz Jahaniand263fd12011-02-11 18:46:17 +00005179 }
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005180 else
John McCall8cc246c2010-12-15 01:06:38 +00005181 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
5182 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005183 if (retType.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005184
5185 if (Unqualified)
5186 retType = retType.getUnqualifiedType();
5187
5188 CanQualType LRetType = getCanonicalType(lbase->getResultType());
5189 CanQualType RRetType = getCanonicalType(rbase->getResultType());
5190 if (Unqualified) {
5191 LRetType = LRetType.getUnqualifiedType();
5192 RRetType = RRetType.getUnqualifiedType();
5193 }
5194
5195 if (getCanonicalType(retType) != LRetType)
Chris Lattner61710852008-10-05 17:34:18 +00005196 allLTypes = false;
Douglas Gregor447234d2010-07-29 15:18:02 +00005197 if (getCanonicalType(retType) != RRetType)
Chris Lattner61710852008-10-05 17:34:18 +00005198 allRTypes = false;
John McCall8cc246c2010-12-15 01:06:38 +00005199
Daniel Dunbar6a15c852010-04-28 16:20:58 +00005200 // FIXME: double check this
5201 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
5202 // rbase->getRegParmAttr() != 0 &&
5203 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindola264ba482010-03-30 20:24:48 +00005204 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
5205 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
John McCall8cc246c2010-12-15 01:06:38 +00005206
Douglas Gregorab8bbf42010-01-18 17:14:39 +00005207 // Compatible functions must have compatible calling conventions
John McCall8cc246c2010-12-15 01:06:38 +00005208 if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00005209 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00005210
John McCall8cc246c2010-12-15 01:06:38 +00005211 // Regparm is part of the calling convention.
Eli Friedmana49218e2011-04-09 08:18:08 +00005212 if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
5213 return QualType();
John McCall8cc246c2010-12-15 01:06:38 +00005214 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
5215 return QualType();
5216
5217 // It's noreturn if either type is.
5218 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
5219 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
5220 if (NoReturn != lbaseInfo.getNoReturn())
5221 allLTypes = false;
5222 if (NoReturn != rbaseInfo.getNoReturn())
5223 allRTypes = false;
5224
5225 FunctionType::ExtInfo einfo(NoReturn,
Eli Friedmana49218e2011-04-09 08:18:08 +00005226 lbaseInfo.getHasRegParm(),
John McCall8cc246c2010-12-15 01:06:38 +00005227 lbaseInfo.getRegParm(),
5228 lbaseInfo.getCC());
John McCalle23cf432010-12-14 08:05:40 +00005229
Eli Friedman3d815e72008-08-22 00:56:42 +00005230 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00005231 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
5232 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00005233 unsigned lproto_nargs = lproto->getNumArgs();
5234 unsigned rproto_nargs = rproto->getNumArgs();
5235
5236 // Compatible functions must have the same number of arguments
5237 if (lproto_nargs != rproto_nargs)
5238 return QualType();
5239
5240 // Variadic and non-variadic functions aren't compatible
5241 if (lproto->isVariadic() != rproto->isVariadic())
5242 return QualType();
5243
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00005244 if (lproto->getTypeQuals() != rproto->getTypeQuals())
5245 return QualType();
5246
Eli Friedman3d815e72008-08-22 00:56:42 +00005247 // Check argument compatibility
5248 llvm::SmallVector<QualType, 10> types;
5249 for (unsigned i = 0; i < lproto_nargs; i++) {
5250 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
5251 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbourne48466752010-10-24 18:30:18 +00005252 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
5253 OfBlockPointer,
5254 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005255 if (argtype.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005256
5257 if (Unqualified)
5258 argtype = argtype.getUnqualifiedType();
5259
Eli Friedman3d815e72008-08-22 00:56:42 +00005260 types.push_back(argtype);
Douglas Gregor447234d2010-07-29 15:18:02 +00005261 if (Unqualified) {
5262 largtype = largtype.getUnqualifiedType();
5263 rargtype = rargtype.getUnqualifiedType();
5264 }
5265
Chris Lattner61710852008-10-05 17:34:18 +00005266 if (getCanonicalType(argtype) != getCanonicalType(largtype))
5267 allLTypes = false;
5268 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
5269 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00005270 }
5271 if (allLTypes) return lhs;
5272 if (allRTypes) return rhs;
John McCalle23cf432010-12-14 08:05:40 +00005273
5274 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
5275 EPI.ExtInfo = einfo;
5276 return getFunctionType(retType, types.begin(), types.size(), EPI);
Eli Friedman3d815e72008-08-22 00:56:42 +00005277 }
5278
5279 if (lproto) allRTypes = false;
5280 if (rproto) allLTypes = false;
5281
Douglas Gregor72564e72009-02-26 23:50:07 +00005282 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00005283 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00005284 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00005285 if (proto->isVariadic()) return QualType();
5286 // Check that the types are compatible with the types that
5287 // would result from default argument promotions (C99 6.7.5.3p15).
5288 // The only types actually affected are promotable integer
5289 // types and floats, which would be passed as a different
5290 // type depending on whether the prototype is visible.
5291 unsigned proto_nargs = proto->getNumArgs();
5292 for (unsigned i = 0; i < proto_nargs; ++i) {
5293 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00005294
5295 // Look at the promotion type of enum types, since that is the type used
5296 // to pass enum values.
5297 if (const EnumType *Enum = argTy->getAs<EnumType>())
5298 argTy = Enum->getDecl()->getPromotionType();
5299
Eli Friedman3d815e72008-08-22 00:56:42 +00005300 if (argTy->isPromotableIntegerType() ||
5301 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
5302 return QualType();
5303 }
5304
5305 if (allLTypes) return lhs;
5306 if (allRTypes) return rhs;
John McCalle23cf432010-12-14 08:05:40 +00005307
5308 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
5309 EPI.ExtInfo = einfo;
Eli Friedman3d815e72008-08-22 00:56:42 +00005310 return getFunctionType(retType, proto->arg_type_begin(),
John McCalle23cf432010-12-14 08:05:40 +00005311 proto->getNumArgs(), EPI);
Eli Friedman3d815e72008-08-22 00:56:42 +00005312 }
5313
5314 if (allLTypes) return lhs;
5315 if (allRTypes) return rhs;
John McCall8cc246c2010-12-15 01:06:38 +00005316 return getFunctionNoProtoType(retType, einfo);
Eli Friedman3d815e72008-08-22 00:56:42 +00005317}
5318
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005319QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor447234d2010-07-29 15:18:02 +00005320 bool OfBlockPointer,
Fariborz Jahaniana4fdbfa2011-03-14 16:07:00 +00005321 bool Unqualified, bool BlockReturnType) {
Bill Wendling43d69752007-12-03 07:33:35 +00005322 // C++ [expr]: If an expression initially has the type "reference to T", the
5323 // type is adjusted to "T" prior to any further analysis, the expression
5324 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00005325 // expression is an lvalue unless the reference is an rvalue reference and
5326 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00005327 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
5328 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor447234d2010-07-29 15:18:02 +00005329
5330 if (Unqualified) {
5331 LHS = LHS.getUnqualifiedType();
5332 RHS = RHS.getUnqualifiedType();
5333 }
Douglas Gregor0e709ab2010-02-03 21:02:30 +00005334
Eli Friedman3d815e72008-08-22 00:56:42 +00005335 QualType LHSCan = getCanonicalType(LHS),
5336 RHSCan = getCanonicalType(RHS);
5337
5338 // If two types are identical, they are compatible.
5339 if (LHSCan == RHSCan)
5340 return LHS;
5341
John McCall0953e762009-09-24 19:53:00 +00005342 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00005343 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5344 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00005345 if (LQuals != RQuals) {
5346 // If any of these qualifiers are different, we have a type
5347 // mismatch.
5348 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5349 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5350 return QualType();
5351
5352 // Exactly one GC qualifier difference is allowed: __strong is
5353 // okay if the other type has no GC qualifier but is an Objective
5354 // C object pointer (i.e. implicitly strong by default). We fix
5355 // this by pretending that the unqualified type was actually
5356 // qualified __strong.
5357 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5358 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5359 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5360
5361 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5362 return QualType();
5363
5364 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
5365 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
5366 }
5367 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
5368 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
5369 }
Eli Friedman3d815e72008-08-22 00:56:42 +00005370 return QualType();
John McCall0953e762009-09-24 19:53:00 +00005371 }
5372
5373 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00005374
Eli Friedman852d63b2009-06-01 01:22:52 +00005375 Type::TypeClass LHSClass = LHSCan->getTypeClass();
5376 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00005377
Chris Lattner1adb8832008-01-14 05:45:46 +00005378 // We want to consider the two function types to be the same for these
5379 // comparisons, just force one to the other.
5380 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5381 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00005382
5383 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00005384 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5385 LHSClass = Type::ConstantArray;
5386 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5387 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00005388
John McCallc12c5bb2010-05-15 11:32:37 +00005389 // ObjCInterfaces are just specialized ObjCObjects.
5390 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5391 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5392
Nate Begeman213541a2008-04-18 23:10:10 +00005393 // Canonicalize ExtVector -> Vector.
5394 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5395 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00005396
Chris Lattnera36a61f2008-04-07 05:43:21 +00005397 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00005398 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00005399 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00005400 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00005401 // Compatibility is based on the underlying type, not the promotion
5402 // type.
John McCall183700f2009-09-21 23:43:11 +00005403 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00005404 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5405 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00005406 }
John McCall183700f2009-09-21 23:43:11 +00005407 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00005408 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5409 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00005410 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005411
Eli Friedman3d815e72008-08-22 00:56:42 +00005412 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00005413 }
Eli Friedman3d815e72008-08-22 00:56:42 +00005414
Steve Naroff4a746782008-01-09 22:43:08 +00005415 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00005416 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00005417#define TYPE(Class, Base)
5418#define ABSTRACT_TYPE(Class, Base)
John McCallad5e7382010-03-01 23:49:17 +00005419#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +00005420#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5421#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5422#include "clang/AST/TypeNodes.def"
5423 assert(false && "Non-canonical and dependent types shouldn't get here");
5424 return QualType();
5425
Sebastian Redl7c80bd62009-03-16 23:22:08 +00005426 case Type::LValueReference:
5427 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00005428 case Type::MemberPointer:
5429 assert(false && "C++ should never be in mergeTypes");
5430 return QualType();
5431
John McCallc12c5bb2010-05-15 11:32:37 +00005432 case Type::ObjCInterface:
Douglas Gregor72564e72009-02-26 23:50:07 +00005433 case Type::IncompleteArray:
5434 case Type::VariableArray:
5435 case Type::FunctionProto:
5436 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00005437 assert(false && "Types are eliminated above");
5438 return QualType();
5439
Chris Lattner1adb8832008-01-14 05:45:46 +00005440 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00005441 {
5442 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00005443 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5444 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005445 if (Unqualified) {
5446 LHSPointee = LHSPointee.getUnqualifiedType();
5447 RHSPointee = RHSPointee.getUnqualifiedType();
5448 }
5449 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5450 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005451 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00005452 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005453 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00005454 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005455 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005456 return getPointerType(ResultType);
5457 }
Steve Naroffc0febd52008-12-10 17:49:55 +00005458 case Type::BlockPointer:
5459 {
5460 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00005461 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5462 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005463 if (Unqualified) {
5464 LHSPointee = LHSPointee.getUnqualifiedType();
5465 RHSPointee = RHSPointee.getUnqualifiedType();
5466 }
5467 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5468 Unqualified);
Steve Naroffc0febd52008-12-10 17:49:55 +00005469 if (ResultType.isNull()) return QualType();
5470 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5471 return LHS;
5472 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5473 return RHS;
5474 return getBlockPointerType(ResultType);
5475 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005476 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00005477 {
5478 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5479 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5480 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5481 return QualType();
5482
5483 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5484 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005485 if (Unqualified) {
5486 LHSElem = LHSElem.getUnqualifiedType();
5487 RHSElem = RHSElem.getUnqualifiedType();
5488 }
5489
5490 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005491 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00005492 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5493 return LHS;
5494 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5495 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00005496 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5497 ArrayType::ArraySizeModifier(), 0);
5498 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5499 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005500 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5501 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00005502 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5503 return LHS;
5504 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5505 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005506 if (LVAT) {
5507 // FIXME: This isn't correct! But tricky to implement because
5508 // the array's size has to be the size of LHS, but the type
5509 // has to be different.
5510 return LHS;
5511 }
5512 if (RVAT) {
5513 // FIXME: This isn't correct! But tricky to implement because
5514 // the array's size has to be the size of RHS, but the type
5515 // has to be different.
5516 return RHS;
5517 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00005518 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5519 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005520 return getIncompleteArrayType(ResultType,
5521 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005522 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005523 case Type::FunctionNoProto:
Douglas Gregor447234d2010-07-29 15:18:02 +00005524 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregor72564e72009-02-26 23:50:07 +00005525 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00005526 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00005527 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00005528 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005529 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00005530 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00005531 case Type::Complex:
5532 // Distinct complex types are incompatible.
5533 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005534 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005535 // FIXME: The merged type should be an ExtVector!
John McCall1c471f32010-03-12 23:14:13 +00005536 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5537 RHSCan->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00005538 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00005539 return QualType();
John McCallc12c5bb2010-05-15 11:32:37 +00005540 case Type::ObjCObject: {
5541 // Check if the types are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005542 // FIXME: This should be type compatibility, e.g. whether
5543 // "LHS x; RHS x;" at global scope is legal.
John McCallc12c5bb2010-05-15 11:32:37 +00005544 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5545 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5546 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff5fd659d2009-02-21 16:18:07 +00005547 return LHS;
5548
Eli Friedman3d815e72008-08-22 00:56:42 +00005549 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00005550 }
Steve Naroff14108da2009-07-10 23:34:53 +00005551 case Type::ObjCObjectPointer: {
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005552 if (OfBlockPointer) {
5553 if (canAssignObjCInterfacesInBlockPointer(
5554 LHS->getAs<ObjCObjectPointerType>(),
Fariborz Jahaniana4fdbfa2011-03-14 16:07:00 +00005555 RHS->getAs<ObjCObjectPointerType>(),
5556 BlockReturnType))
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005557 return LHS;
5558 return QualType();
5559 }
John McCall183700f2009-09-21 23:43:11 +00005560 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5561 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00005562 return LHS;
5563
Steve Naroffbc76dd02008-12-10 22:14:21 +00005564 return QualType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005565 }
Steve Naroffec0550f2007-10-15 20:41:53 +00005566 }
Douglas Gregor72564e72009-02-26 23:50:07 +00005567
5568 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00005569}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00005570
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005571/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5572/// 'RHS' attributes and returns the merged version; including for function
5573/// return types.
5574QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5575 QualType LHSCan = getCanonicalType(LHS),
5576 RHSCan = getCanonicalType(RHS);
5577 // If two types are identical, they are compatible.
5578 if (LHSCan == RHSCan)
5579 return LHS;
5580 if (RHSCan->isFunctionType()) {
5581 if (!LHSCan->isFunctionType())
5582 return QualType();
5583 QualType OldReturnType =
5584 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5585 QualType NewReturnType =
5586 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5587 QualType ResReturnType =
5588 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5589 if (ResReturnType.isNull())
5590 return QualType();
5591 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5592 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5593 // In either case, use OldReturnType to build the new function type.
5594 const FunctionType *F = LHS->getAs<FunctionType>();
5595 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
John McCalle23cf432010-12-14 08:05:40 +00005596 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5597 EPI.ExtInfo = getFunctionExtInfo(LHS);
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005598 QualType ResultType
5599 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
John McCalle23cf432010-12-14 08:05:40 +00005600 FPT->getNumArgs(), EPI);
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005601 return ResultType;
5602 }
5603 }
5604 return QualType();
5605 }
5606
5607 // If the qualifiers are different, the types can still be merged.
5608 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5609 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5610 if (LQuals != RQuals) {
5611 // If any of these qualifiers are different, we have a type mismatch.
5612 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5613 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5614 return QualType();
5615
5616 // Exactly one GC qualifier difference is allowed: __strong is
5617 // okay if the other type has no GC qualifier but is an Objective
5618 // C object pointer (i.e. implicitly strong by default). We fix
5619 // this by pretending that the unqualified type was actually
5620 // qualified __strong.
5621 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5622 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5623 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5624
5625 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5626 return QualType();
5627
5628 if (GC_L == Qualifiers::Strong)
5629 return LHS;
5630 if (GC_R == Qualifiers::Strong)
5631 return RHS;
5632 return QualType();
5633 }
5634
5635 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5636 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5637 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5638 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5639 if (ResQT == LHSBaseQT)
5640 return LHS;
5641 if (ResQT == RHSBaseQT)
5642 return RHS;
5643 }
5644 return QualType();
5645}
5646
Chris Lattner5426bf62008-04-07 07:01:58 +00005647//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00005648// Integer Predicates
5649//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00005650
Jay Foad4ba2a172011-01-12 09:06:06 +00005651unsigned ASTContext::getIntWidth(QualType T) const {
John McCallf4c73712011-01-19 06:33:43 +00005652 if (const EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00005653 T = ET->getDecl()->getIntegerType();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005654 if (T->isBooleanType())
5655 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00005656 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00005657 return (unsigned)getTypeSize(T);
5658}
5659
5660QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregorf6094622010-07-23 15:58:24 +00005661 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00005662
5663 // Turn <4 x signed int> -> <4 x unsigned int>
5664 if (const VectorType *VTy = T->getAs<VectorType>())
5665 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsone86d78c2010-11-10 21:56:12 +00005666 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattner6a2b9262009-10-17 20:33:28 +00005667
5668 // For enums, we return the unsigned version of the base type.
5669 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00005670 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00005671
5672 const BuiltinType *BTy = T->getAs<BuiltinType>();
5673 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00005674 switch (BTy->getKind()) {
5675 case BuiltinType::Char_S:
5676 case BuiltinType::SChar:
5677 return UnsignedCharTy;
5678 case BuiltinType::Short:
5679 return UnsignedShortTy;
5680 case BuiltinType::Int:
5681 return UnsignedIntTy;
5682 case BuiltinType::Long:
5683 return UnsignedLongTy;
5684 case BuiltinType::LongLong:
5685 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00005686 case BuiltinType::Int128:
5687 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00005688 default:
5689 assert(0 && "Unexpected signed integer type");
5690 return QualType();
5691 }
5692}
5693
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005694ASTMutationListener::~ASTMutationListener() { }
5695
Chris Lattner86df27b2009-06-14 00:45:47 +00005696
5697//===----------------------------------------------------------------------===//
5698// Builtin Type Computation
5699//===----------------------------------------------------------------------===//
5700
5701/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattner33daae62010-10-01 22:42:38 +00005702/// pointer over the consumed characters. This returns the resultant type. If
5703/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5704/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5705/// a vector of "i*".
Chris Lattner14e0e742010-10-01 22:53:11 +00005706///
5707/// RequiresICE is filled in on return to indicate whether the value is required
5708/// to be an Integer Constant Expression.
Jay Foad4ba2a172011-01-12 09:06:06 +00005709static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00005710 ASTContext::GetBuiltinTypeError &Error,
Chris Lattner14e0e742010-10-01 22:53:11 +00005711 bool &RequiresICE,
Chris Lattner33daae62010-10-01 22:42:38 +00005712 bool AllowTypeModifiers) {
Chris Lattner86df27b2009-06-14 00:45:47 +00005713 // Modifiers.
5714 int HowLong = 0;
5715 bool Signed = false, Unsigned = false;
Chris Lattner14e0e742010-10-01 22:53:11 +00005716 RequiresICE = false;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005717
Chris Lattner33daae62010-10-01 22:42:38 +00005718 // Read the prefixed modifiers first.
Chris Lattner86df27b2009-06-14 00:45:47 +00005719 bool Done = false;
5720 while (!Done) {
5721 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00005722 default: Done = true; --Str; break;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005723 case 'I':
Chris Lattner14e0e742010-10-01 22:53:11 +00005724 RequiresICE = true;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005725 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005726 case 'S':
5727 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5728 assert(!Signed && "Can't use 'S' modifier multiple times!");
5729 Signed = true;
5730 break;
5731 case 'U':
5732 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5733 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5734 Unsigned = true;
5735 break;
5736 case 'L':
5737 assert(HowLong <= 2 && "Can't have LLLL modifier");
5738 ++HowLong;
5739 break;
5740 }
5741 }
5742
5743 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00005744
Chris Lattner86df27b2009-06-14 00:45:47 +00005745 // Read the base type.
5746 switch (*Str++) {
5747 default: assert(0 && "Unknown builtin type letter!");
5748 case 'v':
5749 assert(HowLong == 0 && !Signed && !Unsigned &&
5750 "Bad modifiers used with 'v'!");
5751 Type = Context.VoidTy;
5752 break;
5753 case 'f':
5754 assert(HowLong == 0 && !Signed && !Unsigned &&
5755 "Bad modifiers used with 'f'!");
5756 Type = Context.FloatTy;
5757 break;
5758 case 'd':
5759 assert(HowLong < 2 && !Signed && !Unsigned &&
5760 "Bad modifiers used with 'd'!");
5761 if (HowLong)
5762 Type = Context.LongDoubleTy;
5763 else
5764 Type = Context.DoubleTy;
5765 break;
5766 case 's':
5767 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5768 if (Unsigned)
5769 Type = Context.UnsignedShortTy;
5770 else
5771 Type = Context.ShortTy;
5772 break;
5773 case 'i':
5774 if (HowLong == 3)
5775 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5776 else if (HowLong == 2)
5777 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5778 else if (HowLong == 1)
5779 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5780 else
5781 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5782 break;
5783 case 'c':
5784 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5785 if (Signed)
5786 Type = Context.SignedCharTy;
5787 else if (Unsigned)
5788 Type = Context.UnsignedCharTy;
5789 else
5790 Type = Context.CharTy;
5791 break;
5792 case 'b': // boolean
5793 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5794 Type = Context.BoolTy;
5795 break;
5796 case 'z': // size_t.
5797 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5798 Type = Context.getSizeType();
5799 break;
5800 case 'F':
5801 Type = Context.getCFConstantStringType();
5802 break;
Fariborz Jahanianba8bda02010-11-09 21:38:20 +00005803 case 'G':
5804 Type = Context.getObjCIdType();
5805 break;
5806 case 'H':
5807 Type = Context.getObjCSelType();
5808 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005809 case 'a':
5810 Type = Context.getBuiltinVaListType();
5811 assert(!Type.isNull() && "builtin va list type not initialized!");
5812 break;
5813 case 'A':
5814 // This is a "reference" to a va_list; however, what exactly
5815 // this means depends on how va_list is defined. There are two
5816 // different kinds of va_list: ones passed by value, and ones
5817 // passed by reference. An example of a by-value va_list is
5818 // x86, where va_list is a char*. An example of by-ref va_list
5819 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5820 // we want this argument to be a char*&; for x86-64, we want
5821 // it to be a __va_list_tag*.
5822 Type = Context.getBuiltinVaListType();
5823 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattner14e0e742010-10-01 22:53:11 +00005824 if (Type->isArrayType())
Chris Lattner86df27b2009-06-14 00:45:47 +00005825 Type = Context.getArrayDecayedType(Type);
Chris Lattner14e0e742010-10-01 22:53:11 +00005826 else
Chris Lattner86df27b2009-06-14 00:45:47 +00005827 Type = Context.getLValueReferenceType(Type);
Chris Lattner86df27b2009-06-14 00:45:47 +00005828 break;
5829 case 'V': {
5830 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00005831 unsigned NumElements = strtoul(Str, &End, 10);
5832 assert(End != Str && "Missing vector size");
Chris Lattner86df27b2009-06-14 00:45:47 +00005833 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00005834
Chris Lattner14e0e742010-10-01 22:53:11 +00005835 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5836 RequiresICE, false);
5837 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattner33daae62010-10-01 22:42:38 +00005838
5839 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner788b0fd2010-06-23 06:00:24 +00005840 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00005841 VectorType::GenericVector);
Chris Lattner86df27b2009-06-14 00:45:47 +00005842 break;
5843 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00005844 case 'X': {
Chris Lattner14e0e742010-10-01 22:53:11 +00005845 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5846 false);
5847 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregord3a23b22009-09-28 21:45:01 +00005848 Type = Context.getComplexType(ElementType);
5849 break;
5850 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005851 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00005852 Type = Context.getFILEType();
5853 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005854 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00005855 return QualType();
5856 }
Mike Stumpfd612db2009-07-28 23:47:15 +00005857 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005858 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00005859 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00005860 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00005861 else
5862 Type = Context.getjmp_bufType();
5863
Mike Stumpfd612db2009-07-28 23:47:15 +00005864 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005865 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00005866 return QualType();
5867 }
5868 break;
Mike Stump782fa302009-07-28 02:25:19 +00005869 }
Mike Stump1eb44332009-09-09 15:08:12 +00005870
Chris Lattner33daae62010-10-01 22:42:38 +00005871 // If there are modifiers and if we're allowed to parse them, go for it.
5872 Done = !AllowTypeModifiers;
Chris Lattner86df27b2009-06-14 00:45:47 +00005873 while (!Done) {
John McCall187ab372010-03-12 04:21:28 +00005874 switch (char c = *Str++) {
Chris Lattner33daae62010-10-01 22:42:38 +00005875 default: Done = true; --Str; break;
5876 case '*':
5877 case '&': {
5878 // Both pointers and references can have their pointee types
5879 // qualified with an address space.
5880 char *End;
5881 unsigned AddrSpace = strtoul(Str, &End, 10);
5882 if (End != Str && AddrSpace != 0) {
5883 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5884 Str = End;
5885 }
5886 if (c == '*')
5887 Type = Context.getPointerType(Type);
5888 else
5889 Type = Context.getLValueReferenceType(Type);
5890 break;
5891 }
5892 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5893 case 'C':
5894 Type = Type.withConst();
5895 break;
5896 case 'D':
5897 Type = Context.getVolatileType(Type);
5898 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005899 }
5900 }
Chris Lattner393bd8e2010-10-01 07:13:18 +00005901
Chris Lattner14e0e742010-10-01 22:53:11 +00005902 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner393bd8e2010-10-01 07:13:18 +00005903 "Integer constant 'I' type must be an integer");
Mike Stump1eb44332009-09-09 15:08:12 +00005904
Chris Lattner86df27b2009-06-14 00:45:47 +00005905 return Type;
5906}
5907
5908/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattner33daae62010-10-01 22:42:38 +00005909QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattner14e0e742010-10-01 22:53:11 +00005910 GetBuiltinTypeError &Error,
Jay Foad4ba2a172011-01-12 09:06:06 +00005911 unsigned *IntegerConstantArgs) const {
Chris Lattner33daae62010-10-01 22:42:38 +00005912 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump1eb44332009-09-09 15:08:12 +00005913
Chris Lattner86df27b2009-06-14 00:45:47 +00005914 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00005915
Chris Lattner14e0e742010-10-01 22:53:11 +00005916 bool RequiresICE = false;
Chris Lattner86df27b2009-06-14 00:45:47 +00005917 Error = GE_None;
Chris Lattner14e0e742010-10-01 22:53:11 +00005918 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5919 RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005920 if (Error != GE_None)
5921 return QualType();
Chris Lattner14e0e742010-10-01 22:53:11 +00005922
5923 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5924
Chris Lattner86df27b2009-06-14 00:45:47 +00005925 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattner14e0e742010-10-01 22:53:11 +00005926 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005927 if (Error != GE_None)
5928 return QualType();
5929
Chris Lattner14e0e742010-10-01 22:53:11 +00005930 // If this argument is required to be an IntegerConstantExpression and the
5931 // caller cares, fill in the bitmask we return.
5932 if (RequiresICE && IntegerConstantArgs)
5933 *IntegerConstantArgs |= 1 << ArgTypes.size();
5934
Chris Lattner86df27b2009-06-14 00:45:47 +00005935 // Do array -> pointer decay. The builtin should use the decayed type.
5936 if (Ty->isArrayType())
5937 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00005938
Chris Lattner86df27b2009-06-14 00:45:47 +00005939 ArgTypes.push_back(Ty);
5940 }
5941
5942 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5943 "'.' should only occur at end of builtin type list!");
5944
John McCall00ccbef2010-12-21 00:44:39 +00005945 FunctionType::ExtInfo EI;
5946 if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
5947
5948 bool Variadic = (TypeStr[0] == '.');
5949
5950 // We really shouldn't be making a no-proto type here, especially in C++.
5951 if (ArgTypes.empty() && Variadic)
5952 return getFunctionNoProtoType(ResType, EI);
Douglas Gregorce056bc2010-02-21 22:15:06 +00005953
John McCalle23cf432010-12-14 08:05:40 +00005954 FunctionProtoType::ExtProtoInfo EPI;
John McCall00ccbef2010-12-21 00:44:39 +00005955 EPI.ExtInfo = EI;
5956 EPI.Variadic = Variadic;
John McCalle23cf432010-12-14 08:05:40 +00005957
5958 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
Chris Lattner86df27b2009-06-14 00:45:47 +00005959}
Eli Friedmana95d7572009-08-19 07:44:53 +00005960
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005961GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5962 GVALinkage External = GVA_StrongExternal;
5963
5964 Linkage L = FD->getLinkage();
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005965 switch (L) {
5966 case NoLinkage:
5967 case InternalLinkage:
5968 case UniqueExternalLinkage:
5969 return GVA_Internal;
5970
5971 case ExternalLinkage:
5972 switch (FD->getTemplateSpecializationKind()) {
5973 case TSK_Undeclared:
5974 case TSK_ExplicitSpecialization:
5975 External = GVA_StrongExternal;
5976 break;
5977
5978 case TSK_ExplicitInstantiationDefinition:
5979 return GVA_ExplicitTemplateInstantiation;
5980
5981 case TSK_ExplicitInstantiationDeclaration:
5982 case TSK_ImplicitInstantiation:
5983 External = GVA_TemplateInstantiation;
5984 break;
5985 }
5986 }
5987
5988 if (!FD->isInlined())
5989 return External;
5990
5991 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5992 // GNU or C99 inline semantics. Determine whether this symbol should be
5993 // externally visible.
5994 if (FD->isInlineDefinitionExternallyVisible())
5995 return External;
5996
5997 // C99 inline semantics, where the symbol is not externally visible.
5998 return GVA_C99Inline;
5999 }
6000
6001 // C++0x [temp.explicit]p9:
6002 // [ Note: The intent is that an inline function that is the subject of
6003 // an explicit instantiation declaration will still be implicitly
6004 // instantiated when used so that the body can be considered for
6005 // inlining, but that no out-of-line copy of the inline function would be
6006 // generated in the translation unit. -- end note ]
6007 if (FD->getTemplateSpecializationKind()
6008 == TSK_ExplicitInstantiationDeclaration)
6009 return GVA_C99Inline;
6010
6011 return GVA_CXXInline;
6012}
6013
6014GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
6015 // If this is a static data member, compute the kind of template
6016 // specialization. Otherwise, this variable is not part of a
6017 // template.
6018 TemplateSpecializationKind TSK = TSK_Undeclared;
6019 if (VD->isStaticDataMember())
6020 TSK = VD->getTemplateSpecializationKind();
6021
6022 Linkage L = VD->getLinkage();
6023 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
6024 VD->getType()->getLinkage() == UniqueExternalLinkage)
6025 L = UniqueExternalLinkage;
6026
6027 switch (L) {
6028 case NoLinkage:
6029 case InternalLinkage:
6030 case UniqueExternalLinkage:
6031 return GVA_Internal;
6032
6033 case ExternalLinkage:
6034 switch (TSK) {
6035 case TSK_Undeclared:
6036 case TSK_ExplicitSpecialization:
6037 return GVA_StrongExternal;
6038
6039 case TSK_ExplicitInstantiationDeclaration:
6040 llvm_unreachable("Variable should not be instantiated");
6041 // Fall through to treat this like any other instantiation.
6042
6043 case TSK_ExplicitInstantiationDefinition:
6044 return GVA_ExplicitTemplateInstantiation;
6045
6046 case TSK_ImplicitInstantiation:
6047 return GVA_TemplateInstantiation;
6048 }
6049 }
6050
6051 return GVA_StrongExternal;
6052}
6053
Argyrios Kyrtzidis4ac7c0b2010-07-29 20:08:05 +00006054bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00006055 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
6056 if (!VD->isFileVarDecl())
6057 return false;
6058 } else if (!isa<FunctionDecl>(D))
6059 return false;
6060
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00006061 // Weak references don't produce any output by themselves.
6062 if (D->hasAttr<WeakRefAttr>())
6063 return false;
6064
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00006065 // Aliases and used decls are required.
6066 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
6067 return true;
6068
6069 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
6070 // Forward declarations aren't required.
6071 if (!FD->isThisDeclarationADefinition())
6072 return false;
6073
6074 // Constructors and destructors are required.
6075 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
6076 return true;
6077
6078 // The key function for a class is required.
6079 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
6080 const CXXRecordDecl *RD = MD->getParent();
6081 if (MD->isOutOfLine() && RD->isDynamicClass()) {
6082 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
6083 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
6084 return true;
6085 }
6086 }
6087
6088 GVALinkage Linkage = GetGVALinkageForFunction(FD);
6089
6090 // static, static inline, always_inline, and extern inline functions can
6091 // always be deferred. Normal inline functions can be deferred in C99/C++.
6092 // Implicit template instantiations can also be deferred in C++.
6093 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
6094 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
6095 return false;
6096 return true;
6097 }
6098
6099 const VarDecl *VD = cast<VarDecl>(D);
6100 assert(VD->isFileVarDecl() && "Expected file scoped var");
6101
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00006102 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
6103 return false;
6104
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00006105 // Structs that have non-trivial constructors or destructors are required.
6106
6107 // FIXME: Handle references.
6108 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
6109 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00006110 if (RD->hasDefinition() &&
6111 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00006112 return true;
6113 }
6114 }
6115
6116 GVALinkage L = GetGVALinkageForVariable(VD);
6117 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
6118 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
6119 return false;
6120 }
6121
6122 return true;
6123}
Charles Davis071cc7d2010-08-16 03:33:14 +00006124
Charles Davisee743f92010-11-09 18:04:24 +00006125CallingConv ASTContext::getDefaultMethodCallConv() {
6126 // Pass through to the C++ ABI object
6127 return ABI->getDefaultMethodCallConv();
6128}
6129
Jay Foad4ba2a172011-01-12 09:06:06 +00006130bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
Anders Carlssondae0cb52010-11-25 01:51:53 +00006131 // Pass through to the C++ ABI object
6132 return ABI->isNearlyEmpty(RD);
6133}
6134
Peter Collingbourne14110472011-01-13 18:57:25 +00006135MangleContext *ASTContext::createMangleContext() {
6136 switch (Target.getCXXABI()) {
6137 case CXXABI_ARM:
6138 case CXXABI_Itanium:
6139 return createItaniumMangleContext(*this, getDiagnostics());
6140 case CXXABI_Microsoft:
6141 return createMicrosoftMangleContext(*this, getDiagnostics());
6142 }
6143 assert(0 && "Unsupported ABI");
6144 return 0;
6145}
6146
Charles Davis071cc7d2010-08-16 03:33:14 +00006147CXXABI::~CXXABI() {}
Ted Kremenekba29bd22011-04-28 04:53:38 +00006148
6149size_t ASTContext::getSideTableAllocatedMemory() const {
6150 size_t bytes = 0;
6151 bytes += ASTRecordLayouts.getMemorySize();
6152 bytes += ObjCLayouts.getMemorySize();
6153 bytes += KeyFunctions.getMemorySize();
6154 bytes += ObjCImpls.getMemorySize();
6155 bytes += BlockVarCopyInits.getMemorySize();
6156 bytes += DeclAttrs.getMemorySize();
6157 bytes += InstantiatedFromStaticDataMember.getMemorySize();
6158 bytes += InstantiatedFromUsingDecl.getMemorySize();
6159 bytes += InstantiatedFromUsingShadowDecl.getMemorySize();
6160 bytes += InstantiatedFromUnnamedFieldDecl.getMemorySize();
6161 return bytes;
6162}
6163