blob: e9d8c0f304c217a6b3f57251813c085cbbdec85d [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerddc135e2006-11-10 06:34:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyck8c89d592009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff67391b82007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
John McCall87fe5d52010-05-20 01:18:31 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000023#include "clang/AST/ASTMutationListener.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000026#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000028#include "llvm/ADT/SmallString.h"
Anders Carlssond8499822007-10-29 05:01:08 +000029#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000030#include "llvm/Support/MathExtras.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000031#include "llvm/Support/raw_ostream.h"
Charles Davis53c59df2010-08-16 03:33:14 +000032#include "CXXABI.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000033
Chris Lattnerddc135e2006-11-10 06:34:16 +000034using namespace clang;
35
Douglas Gregor9672f922010-07-03 00:47:00 +000036unsigned ASTContext::NumImplicitDefaultConstructors;
37unsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
Douglas Gregora6d69502010-07-02 23:41:54 +000038unsigned ASTContext::NumImplicitCopyConstructors;
39unsigned ASTContext::NumImplicitCopyConstructorsDeclared;
Douglas Gregor330b9cf2010-07-02 21:50:04 +000040unsigned ASTContext::NumImplicitCopyAssignmentOperators;
41unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
Douglas Gregor7454c562010-07-02 20:37:36 +000042unsigned ASTContext::NumImplicitDestructors;
43unsigned ASTContext::NumImplicitDestructorsDeclared;
44
Steve Naroff0af91202007-04-27 21:51:21 +000045enum FloatingRank {
46 FloatRank, DoubleRank, LongDoubleRank
47};
48
Douglas Gregor7dbfb462010-06-16 21:09:37 +000049void
50ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
51 TemplateTemplateParmDecl *Parm) {
52 ID.AddInteger(Parm->getDepth());
53 ID.AddInteger(Parm->getPosition());
54 // FIXME: Parameter pack
55
56 TemplateParameterList *Params = Parm->getTemplateParameters();
57 ID.AddInteger(Params->size());
58 for (TemplateParameterList::const_iterator P = Params->begin(),
59 PEnd = Params->end();
60 P != PEnd; ++P) {
61 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
62 ID.AddInteger(0);
63 ID.AddBoolean(TTP->isParameterPack());
64 continue;
65 }
66
67 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
68 ID.AddInteger(1);
69 // FIXME: Parameter pack
70 ID.AddPointer(NTTP->getType().getAsOpaquePtr());
71 continue;
72 }
73
74 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
75 ID.AddInteger(2);
76 Profile(ID, TTP);
77 }
78}
79
80TemplateTemplateParmDecl *
81ASTContext::getCanonicalTemplateTemplateParmDecl(
82 TemplateTemplateParmDecl *TTP) {
83 // Check if we already have a canonical template template parameter.
84 llvm::FoldingSetNodeID ID;
85 CanonicalTemplateTemplateParm::Profile(ID, TTP);
86 void *InsertPos = 0;
87 CanonicalTemplateTemplateParm *Canonical
88 = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
89 if (Canonical)
90 return Canonical->getParam();
91
92 // Build a canonical template parameter list.
93 TemplateParameterList *Params = TTP->getTemplateParameters();
94 llvm::SmallVector<NamedDecl *, 4> CanonParams;
95 CanonParams.reserve(Params->size());
96 for (TemplateParameterList::const_iterator P = Params->begin(),
97 PEnd = Params->end();
98 P != PEnd; ++P) {
99 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
100 CanonParams.push_back(
101 TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
102 SourceLocation(), TTP->getDepth(),
103 TTP->getIndex(), 0, false,
104 TTP->isParameterPack()));
105 else if (NonTypeTemplateParmDecl *NTTP
106 = dyn_cast<NonTypeTemplateParmDecl>(*P))
107 CanonParams.push_back(
108 NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
109 SourceLocation(), NTTP->getDepth(),
110 NTTP->getPosition(), 0,
111 getCanonicalType(NTTP->getType()),
112 0));
113 else
114 CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
115 cast<TemplateTemplateParmDecl>(*P)));
116 }
117
118 TemplateTemplateParmDecl *CanonTTP
119 = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
120 SourceLocation(), TTP->getDepth(),
121 TTP->getPosition(), 0,
122 TemplateParameterList::Create(*this, SourceLocation(),
123 SourceLocation(),
124 CanonParams.data(),
125 CanonParams.size(),
126 SourceLocation()));
127
128 // Get the new insert position for the node we care about.
129 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
130 assert(Canonical == 0 && "Shouldn't be in the map!");
131 (void)Canonical;
132
133 // Create the canonical template template parameter entry.
134 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
135 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
136 return CanonTTP;
137}
138
Charles Davis53c59df2010-08-16 03:33:14 +0000139CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
John McCall86353412010-08-21 22:46:04 +0000140 if (!LangOpts.CPlusPlus) return 0;
141
Charles Davis6bcb07a2010-08-19 02:18:14 +0000142 switch (T.getCXXABI()) {
John McCall86353412010-08-21 22:46:04 +0000143 case CXXABI_ARM:
144 return CreateARMCXXABI(*this);
145 case CXXABI_Itanium:
Charles Davis53c59df2010-08-16 03:33:14 +0000146 return CreateItaniumCXXABI(*this);
Charles Davis6bcb07a2010-08-19 02:18:14 +0000147 case CXXABI_Microsoft:
148 return CreateMicrosoftCXXABI(*this);
149 }
John McCall86353412010-08-21 22:46:04 +0000150 return 0;
Charles Davis53c59df2010-08-16 03:33:14 +0000151}
152
Chris Lattner465fa322008-10-05 17:34:18 +0000153ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar1b444192009-11-13 05:51:54 +0000154 const TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +0000155 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +0000156 Builtin::Context &builtins,
Douglas Gregor5b11d492010-07-25 17:53:33 +0000157 unsigned size_reserve) :
John McCall773cc982010-06-11 11:07:21 +0000158 TemplateSpecializationTypes(this_()),
159 DependentTemplateSpecializationTypes(this_()),
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +0000160 GlobalNestedNameSpecifier(0), IsInt128Installed(false),
161 CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +0000162 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +0000163 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
John McCall8cb7bdf2010-06-04 23:28:52 +0000164 NullTypeSourceInfo(QualType()),
Charles Davis53c59df2010-08-16 03:33:14 +0000165 SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000166 Idents(idents), Selectors(sels),
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000167 BuiltinInfo(builtins),
168 DeclarationNames(*this),
Argyrios Kyrtzidis440ea322010-10-28 09:29:35 +0000169 ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
Ted Kremenek520f47b2010-06-18 00:31:04 +0000170 LastSDM(0, 0),
171 UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) {
David Chisnall9f57c292009-08-17 16:35:33 +0000172 ObjCIdRedefinitionType = QualType();
173 ObjCClassRedefinitionType = QualType();
Douglas Gregor5b11d492010-07-25 17:53:33 +0000174 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000175 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +0000176 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000177 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +0000178}
179
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000180ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000181 // Release the DenseMaps associated with DeclContext objects.
182 // FIXME: Is this the ideal solution?
183 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +0000184
Douglas Gregor5b11d492010-07-25 17:53:33 +0000185 // Call all of the deallocation functions.
186 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
187 Deallocations[I].first(Deallocations[I].second);
Douglas Gregor1a809332010-05-23 18:26:36 +0000188
Douglas Gregor832940b2010-03-02 23:58:15 +0000189 // Release all of the memory associated with overridden C++ methods.
190 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
191 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
192 OM != OMEnd; ++OM)
193 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000194
Ted Kremenek076baeb2010-06-08 23:00:58 +0000195 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
Douglas Gregor5b11d492010-07-25 17:53:33 +0000196 // because they can contain DenseMaps.
197 for (llvm::DenseMap<const ObjCContainerDecl*,
198 const ASTRecordLayout*>::iterator
199 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
200 // Increment in loop to prevent using deallocated memory.
201 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
202 R->Destroy(*this);
203
Ted Kremenek076baeb2010-06-08 23:00:58 +0000204 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
205 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
206 // Increment in loop to prevent using deallocated memory.
207 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
208 R->Destroy(*this);
209 }
Douglas Gregor561eceb2010-08-30 16:49:28 +0000210
211 for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
212 AEnd = DeclAttrs.end();
213 A != AEnd; ++A)
214 A->second->~AttrVec();
215}
Douglas Gregorf21eb492009-03-26 23:50:42 +0000216
Douglas Gregor1a809332010-05-23 18:26:36 +0000217void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
218 Deallocations.push_back(std::make_pair(Callback, Data));
219}
220
Mike Stump11289f42009-09-09 15:08:12 +0000221void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
223 ExternalSource.reset(Source.take());
224}
225
Chris Lattner4eb445d2007-01-26 01:27:23 +0000226void ASTContext::PrintStats() const {
227 fprintf(stderr, "*** AST Context Stats:\n");
228 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000229
Douglas Gregora30d0462009-05-26 14:40:08 +0000230 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000231#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000232#define ABSTRACT_TYPE(Name, Parent)
233#include "clang/AST/TypeNodes.def"
234 0 // Extra
235 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000236
Chris Lattner4eb445d2007-01-26 01:27:23 +0000237 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
238 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000239 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000240 }
241
Douglas Gregora30d0462009-05-26 14:40:08 +0000242 unsigned Idx = 0;
243 unsigned TotalBytes = 0;
244#define TYPE(Name, Parent) \
245 if (counts[Idx]) \
246 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
247 TotalBytes += counts[Idx] * sizeof(Name##Type); \
248 ++Idx;
249#define ABSTRACT_TYPE(Name, Parent)
250#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000251
Douglas Gregora30d0462009-05-26 14:40:08 +0000252 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor747eb782010-07-08 06:14:04 +0000253
Douglas Gregor7454c562010-07-02 20:37:36 +0000254 // Implicit special member functions.
Douglas Gregor9672f922010-07-03 00:47:00 +0000255 fprintf(stderr, " %u/%u implicit default constructors created\n",
256 NumImplicitDefaultConstructorsDeclared,
257 NumImplicitDefaultConstructors);
Douglas Gregora6d69502010-07-02 23:41:54 +0000258 fprintf(stderr, " %u/%u implicit copy constructors created\n",
259 NumImplicitCopyConstructorsDeclared,
260 NumImplicitCopyConstructors);
Douglas Gregor330b9cf2010-07-02 21:50:04 +0000261 fprintf(stderr, " %u/%u implicit copy assignment operators created\n",
262 NumImplicitCopyAssignmentOperatorsDeclared,
263 NumImplicitCopyAssignmentOperators);
Douglas Gregor7454c562010-07-02 20:37:36 +0000264 fprintf(stderr, " %u/%u implicit destructors created\n",
265 NumImplicitDestructorsDeclared, NumImplicitDestructors);
266
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000267 if (ExternalSource.get()) {
268 fprintf(stderr, "\n");
269 ExternalSource->PrintStats();
270 }
Douglas Gregor747eb782010-07-08 06:14:04 +0000271
Douglas Gregor5b11d492010-07-25 17:53:33 +0000272 BumpAlloc.PrintStats();
Chris Lattner4eb445d2007-01-26 01:27:23 +0000273}
274
275
John McCall48f2d582009-10-23 23:03:21 +0000276void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000277 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000278 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000279 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000280}
281
Chris Lattner970e54e2006-11-12 00:37:36 +0000282void ASTContext::InitBuiltinTypes() {
283 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000284
Chris Lattner970e54e2006-11-12 00:37:36 +0000285 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000286 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000287
Chris Lattner970e54e2006-11-12 00:37:36 +0000288 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000289 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000290 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000291 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000292 InitBuiltinType(CharTy, BuiltinType::Char_S);
293 else
294 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000295 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000296 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
297 InitBuiltinType(ShortTy, BuiltinType::Short);
298 InitBuiltinType(IntTy, BuiltinType::Int);
299 InitBuiltinType(LongTy, BuiltinType::Long);
300 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattner970e54e2006-11-12 00:37:36 +0000302 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000303 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
304 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
305 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
306 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
307 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattner970e54e2006-11-12 00:37:36 +0000309 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000310 InitBuiltinType(FloatTy, BuiltinType::Float);
311 InitBuiltinType(DoubleTy, BuiltinType::Double);
312 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000313
Chris Lattnerf122cef2009-04-30 02:43:43 +0000314 // GNU extension, 128-bit integers.
315 InitBuiltinType(Int128Ty, BuiltinType::Int128);
316 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
317
Chris Lattner007cb022009-02-26 23:43:47 +0000318 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
319 InitBuiltinType(WCharTy, BuiltinType::WChar);
320 else // C99
321 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000322
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000323 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
324 InitBuiltinType(Char16Ty, BuiltinType::Char16);
325 else // C99
326 Char16Ty = getFromTargetType(Target.getChar16Type());
327
328 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
329 InitBuiltinType(Char32Ty, BuiltinType::Char32);
330 else // C99
331 Char32Ty = getFromTargetType(Target.getChar32Type());
332
Douglas Gregor4619e432008-12-05 23:32:09 +0000333 // Placeholder type for type-dependent expressions whose type is
334 // completely unknown. No code should ever check a type against
335 // DependentTy and users should never see it; however, it is here to
336 // help diagnose failures to properly check for type-dependent
337 // expressions.
338 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000339
John McCall36e7fe32010-10-12 00:20:44 +0000340 // Placeholder type for functions.
341 InitBuiltinType(OverloadTy, BuiltinType::Overload);
342
Mike Stump11289f42009-09-09 15:08:12 +0000343 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000344 // not yet been deduced.
John McCall36e7fe32010-10-12 00:20:44 +0000345 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner970e54e2006-11-12 00:37:36 +0000347 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000348 FloatComplexTy = getComplexType(FloatTy);
349 DoubleComplexTy = getComplexType(DoubleTy);
350 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000351
Steve Naroff66e9f332007-10-15 14:41:52 +0000352 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000353
Steve Naroff1329fa02009-07-15 18:40:39 +0000354 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
355 ObjCIdTypedefType = QualType();
356 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000357 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000358
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000359 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000360 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
361 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000362 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000363
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000364 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000365
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000366 // void * type
367 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000368
369 // nullptr type (C++0x 2.14.7)
370 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000371}
372
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000373Diagnostic &ASTContext::getDiagnostics() const {
374 return SourceMgr.getDiagnostics();
375}
376
Douglas Gregor561eceb2010-08-30 16:49:28 +0000377AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
378 AttrVec *&Result = DeclAttrs[D];
379 if (!Result) {
380 void *Mem = Allocate(sizeof(AttrVec));
381 Result = new (Mem) AttrVec;
382 }
383
384 return *Result;
385}
386
387/// \brief Erase the attributes corresponding to the given declaration.
388void ASTContext::eraseDeclAttrs(const Decl *D) {
389 llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
390 if (Pos != DeclAttrs.end()) {
391 Pos->second->~AttrVec();
392 DeclAttrs.erase(Pos);
393 }
394}
395
396
Douglas Gregor86d142a2009-10-08 07:24:58 +0000397MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000398ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000399 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000400 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000401 = InstantiatedFromStaticDataMember.find(Var);
402 if (Pos == InstantiatedFromStaticDataMember.end())
403 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000404
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000405 return Pos->second;
406}
407
Mike Stump11289f42009-09-09 15:08:12 +0000408void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000409ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000410 TemplateSpecializationKind TSK,
411 SourceLocation PointOfInstantiation) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000412 assert(Inst->isStaticDataMember() && "Not a static data member");
413 assert(Tmpl->isStaticDataMember() && "Not a static data member");
414 assert(!InstantiatedFromStaticDataMember[Inst] &&
415 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000416 InstantiatedFromStaticDataMember[Inst]
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000417 = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000418}
419
John McCalle61f2ba2009-11-18 02:36:19 +0000420NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000421ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000422 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000423 = InstantiatedFromUsingDecl.find(UUD);
424 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000425 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000427 return Pos->second;
428}
429
430void
John McCallb96ec562009-12-04 22:46:56 +0000431ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
432 assert((isa<UsingDecl>(Pattern) ||
433 isa<UnresolvedUsingValueDecl>(Pattern) ||
434 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
435 "pattern decl is not a using decl");
436 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
437 InstantiatedFromUsingDecl[Inst] = Pattern;
438}
439
440UsingShadowDecl *
441ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
442 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
443 = InstantiatedFromUsingShadowDecl.find(Inst);
444 if (Pos == InstantiatedFromUsingShadowDecl.end())
445 return 0;
446
447 return Pos->second;
448}
449
450void
451ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
452 UsingShadowDecl *Pattern) {
453 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
454 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000455}
456
Anders Carlsson5da84842009-09-01 04:26:58 +0000457FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
458 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
459 = InstantiatedFromUnnamedFieldDecl.find(Field);
460 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
461 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000462
Anders Carlsson5da84842009-09-01 04:26:58 +0000463 return Pos->second;
464}
465
466void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
467 FieldDecl *Tmpl) {
468 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
469 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
470 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
471 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000472
Anders Carlsson5da84842009-09-01 04:26:58 +0000473 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
474}
475
Douglas Gregor832940b2010-03-02 23:58:15 +0000476ASTContext::overridden_cxx_method_iterator
477ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
478 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
479 = OverriddenMethods.find(Method);
480 if (Pos == OverriddenMethods.end())
481 return 0;
482
483 return Pos->second.begin();
484}
485
486ASTContext::overridden_cxx_method_iterator
487ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
488 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
489 = OverriddenMethods.find(Method);
490 if (Pos == OverriddenMethods.end())
491 return 0;
492
493 return Pos->second.end();
494}
495
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000496unsigned
497ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
498 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
499 = OverriddenMethods.find(Method);
500 if (Pos == OverriddenMethods.end())
501 return 0;
502
503 return Pos->second.size();
504}
505
Douglas Gregor832940b2010-03-02 23:58:15 +0000506void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
507 const CXXMethodDecl *Overridden) {
508 OverriddenMethods[Method].push_back(Overridden);
509}
510
Chris Lattner53cfe802007-07-18 17:52:12 +0000511//===----------------------------------------------------------------------===//
512// Type Sizing and Analysis
513//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000514
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000515/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
516/// scalar floating point type.
517const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000518 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000519 assert(BT && "Not a floating point type!");
520 switch (BT->getKind()) {
521 default: assert(0 && "Not a floating point type!");
522 case BuiltinType::Float: return Target.getFloatFormat();
523 case BuiltinType::Double: return Target.getDoubleFormat();
524 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
525 }
526}
527
Ken Dyck160146e2010-01-27 17:10:57 +0000528/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000529/// specified decl. Note that bitfields do not have a valid alignment, so
530/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000531/// If @p RefAsPointee, references are treated like their underlying type
532/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000533CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000534 unsigned Align = Target.getCharWidth();
535
John McCall94268702010-10-08 18:24:19 +0000536 bool UseAlignAttrOnly = false;
537 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
538 Align = AlignFromAttr;
Eli Friedman19a546c2009-02-22 02:56:25 +0000539
John McCall94268702010-10-08 18:24:19 +0000540 // __attribute__((aligned)) can increase or decrease alignment
541 // *except* on a struct or struct member, where it only increases
542 // alignment unless 'packed' is also specified.
543 //
544 // It is an error for [[align]] to decrease alignment, so we can
545 // ignore that possibility; Sema should diagnose it.
546 if (isa<FieldDecl>(D)) {
547 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
548 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
549 } else {
550 UseAlignAttrOnly = true;
551 }
552 }
553
554 if (UseAlignAttrOnly) {
555 // ignore type of value
556 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner68061312009-01-24 21:53:27 +0000557 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000558 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000559 if (RefAsPointee)
560 T = RT->getPointeeType();
561 else
562 T = getPointerType(RT->getPointeeType());
563 }
564 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindolae971b9a2010-06-04 23:15:27 +0000565 unsigned MinWidth = Target.getLargeArrayMinWidth();
566 unsigned ArrayAlign = Target.getLargeArrayAlign();
567 if (isa<VariableArrayType>(T) && MinWidth != 0)
568 Align = std::max(Align, ArrayAlign);
569 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
570 unsigned Size = getTypeSize(CT);
571 if (MinWidth != 0 && MinWidth <= Size)
572 Align = std::max(Align, ArrayAlign);
573 }
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000574 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000575 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
576 T = cast<ArrayType>(T)->getElementType();
577
578 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
579 }
Charles Davis3fc51072010-02-23 04:52:00 +0000580 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
581 // In the case of a field in a packed struct, we want the minimum
582 // of the alignment of the field and the alignment of the struct.
583 Align = std::min(Align,
584 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
585 }
Chris Lattner68061312009-01-24 21:53:27 +0000586 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000587
Ken Dyck160146e2010-01-27 17:10:57 +0000588 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000589}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000590
John McCall87fe5d52010-05-20 01:18:31 +0000591std::pair<CharUnits, CharUnits>
592ASTContext::getTypeInfoInChars(const Type *T) {
593 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
594 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
595 CharUnits::fromQuantity(Info.second / getCharWidth()));
596}
597
598std::pair<CharUnits, CharUnits>
599ASTContext::getTypeInfoInChars(QualType T) {
600 return getTypeInfoInChars(T.getTypePtr());
601}
602
Chris Lattner983a8bb2007-07-13 22:13:22 +0000603/// getTypeSize - Return the size of the specified type, in bits. This method
604/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000605///
606/// FIXME: Pointers into different addr spaces could have different sizes and
607/// alignment requirements: getPointerInfo should take an AddrSpace, this
608/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000609std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000610ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000611 uint64_t Width=0;
612 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000613 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000614#define TYPE(Class, Base)
615#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000616#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000617#define DEPENDENT_TYPE(Class, Base) case Type::Class:
618#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000619 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000620 break;
621
Chris Lattner355332d2007-07-13 22:27:08 +0000622 case Type::FunctionNoProto:
623 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000624 // GCC extension: alignof(function) = 32 bits
625 Width = 0;
626 Align = 32;
627 break;
628
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000629 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000630 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000631 Width = 0;
632 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
633 break;
634
Steve Naroff5c131802007-08-30 01:06:46 +0000635 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000636 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000637
Chris Lattner37e05872008-03-05 18:54:05 +0000638 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000639 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000640 Align = EltInfo.second;
641 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000642 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000643 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000644 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000645 const VectorType *VT = cast<VectorType>(T);
646 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
647 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000648 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000649 // If the alignment is not a power of 2, round up to the next power of 2.
650 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000651 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000652 Align = llvm::NextPowerOf2(Align);
653 Width = llvm::RoundUpToAlignment(Width, Align);
654 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000655 break;
656 }
Chris Lattner647fb222007-07-18 18:26:58 +0000657
Chris Lattner7570e9c2008-03-08 08:52:55 +0000658 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000659 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000660 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000661 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000662 // GCC extension: alignof(void) = 8 bits.
663 Width = 0;
664 Align = 8;
665 break;
666
Chris Lattner6a4f7452007-12-19 19:23:28 +0000667 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000668 Width = Target.getBoolWidth();
669 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000670 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000671 case BuiltinType::Char_S:
672 case BuiltinType::Char_U:
673 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000674 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000675 Width = Target.getCharWidth();
676 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000677 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000678 case BuiltinType::WChar:
679 Width = Target.getWCharWidth();
680 Align = Target.getWCharAlign();
681 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000682 case BuiltinType::Char16:
683 Width = Target.getChar16Width();
684 Align = Target.getChar16Align();
685 break;
686 case BuiltinType::Char32:
687 Width = Target.getChar32Width();
688 Align = Target.getChar32Align();
689 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000690 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000691 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000692 Width = Target.getShortWidth();
693 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000694 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000695 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000696 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000697 Width = Target.getIntWidth();
698 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000699 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000700 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000701 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000702 Width = Target.getLongWidth();
703 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000704 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000705 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000706 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000707 Width = Target.getLongLongWidth();
708 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000709 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000710 case BuiltinType::Int128:
711 case BuiltinType::UInt128:
712 Width = 128;
713 Align = 128; // int128_t is 128-bit aligned on all targets.
714 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000715 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000716 Width = Target.getFloatWidth();
717 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000718 break;
719 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000720 Width = Target.getDoubleWidth();
721 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000722 break;
723 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000724 Width = Target.getLongDoubleWidth();
725 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000726 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000727 case BuiltinType::NullPtr:
728 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
729 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000730 break;
Fariborz Jahanian9c6a39e2010-08-02 18:03:20 +0000731 case BuiltinType::ObjCId:
732 case BuiltinType::ObjCClass:
733 case BuiltinType::ObjCSel:
734 Width = Target.getPointerWidth(0);
735 Align = Target.getPointerAlign(0);
736 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000737 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000738 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000739 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000740 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000741 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000742 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000743 case Type::BlockPointer: {
744 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
745 Width = Target.getPointerWidth(AS);
746 Align = Target.getPointerAlign(AS);
747 break;
748 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000749 case Type::LValueReference:
750 case Type::RValueReference: {
751 // alignof and sizeof should never enter this code path here, so we go
752 // the pointer route.
753 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
754 Width = Target.getPointerWidth(AS);
755 Align = Target.getPointerAlign(AS);
756 break;
757 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000758 case Type::Pointer: {
759 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000760 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000761 Align = Target.getPointerAlign(AS);
762 break;
763 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000764 case Type::MemberPointer: {
Charles Davis53c59df2010-08-16 03:33:14 +0000765 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000766 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000767 getTypeInfo(getPointerDiffType());
Charles Davis53c59df2010-08-16 03:33:14 +0000768 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson32440a02009-05-17 02:06:04 +0000769 Align = PtrDiffInfo.second;
770 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000771 }
Chris Lattner647fb222007-07-18 18:26:58 +0000772 case Type::Complex: {
773 // Complex types have the same alignment as their elements, but twice the
774 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000775 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000776 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000777 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000778 Align = EltInfo.second;
779 break;
780 }
John McCall8b07ec22010-05-15 11:32:37 +0000781 case Type::ObjCObject:
782 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Pateldbb72632008-06-04 21:54:36 +0000783 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000784 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000785 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
786 Width = Layout.getSize();
787 Align = Layout.getAlignment();
788 break;
789 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000790 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000791 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000792 const TagType *TT = cast<TagType>(T);
793
794 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000795 Width = 1;
796 Align = 1;
797 break;
798 }
Mike Stump11289f42009-09-09 15:08:12 +0000799
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000800 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000801 return getTypeInfo(ET->getDecl()->getIntegerType());
802
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000803 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000804 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
805 Width = Layout.getSize();
806 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000807 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000808 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000809
Chris Lattner63d2b362009-10-22 05:17:15 +0000810 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000811 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
812 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000813
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000814 case Type::Paren:
815 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
816
Douglas Gregoref462e62009-04-30 17:32:17 +0000817 case Type::Typedef: {
818 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregorf5bae222010-08-27 00:11:28 +0000819 std::pair<uint64_t, unsigned> Info
820 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
821 Align = std::max(Typedef->getMaxAlignment(), Info.second);
822 Width = Info.first;
Douglas Gregordc572a32009-03-30 22:58:21 +0000823 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000824 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000825
826 case Type::TypeOfExpr:
827 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
828 .getTypePtr());
829
830 case Type::TypeOf:
831 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
832
Anders Carlsson81df7b82009-06-24 19:06:50 +0000833 case Type::Decltype:
834 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
835 .getTypePtr());
836
Abramo Bagnara6150c882010-05-11 21:36:43 +0000837 case Type::Elaborated:
838 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000839
Douglas Gregoref462e62009-04-30 17:32:17 +0000840 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000841 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000842 "Cannot request the size of a dependent type");
843 // FIXME: this is likely to be wrong once we support template
844 // aliases, since a template alias could refer to a typedef that
845 // has an __aligned__ attribute on it.
846 return getTypeInfo(getCanonicalType(T));
847 }
Mike Stump11289f42009-09-09 15:08:12 +0000848
Chris Lattner53cfe802007-07-18 17:52:12 +0000849 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000850 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000851}
852
Ken Dyck8c89d592009-12-22 14:23:30 +0000853/// getTypeSizeInChars - Return the size of the specified type, in characters.
854/// This method does not work on incomplete types.
855CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000856 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000857}
858CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000859 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000860}
861
Ken Dycka6046ab2010-01-26 17:25:18 +0000862/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000863/// characters. This method does not work on incomplete types.
864CharUnits ASTContext::getTypeAlignInChars(QualType T) {
865 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
866}
867CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
868 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
869}
870
Chris Lattnera3402cd2009-01-27 18:08:34 +0000871/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
872/// type for the current target in bits. This can be different than the ABI
873/// alignment in cases where it is beneficial for performance to overalign
874/// a data type.
875unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
876 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000877
878 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000879 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000880 T = CT->getElementType().getTypePtr();
881 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
882 T->isSpecificBuiltinType(BuiltinType::LongLong))
883 return std::max(ABIAlign, (unsigned)getTypeSize(T));
884
Chris Lattnera3402cd2009-01-27 18:08:34 +0000885 return ABIAlign;
886}
887
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000888/// ShallowCollectObjCIvars -
889/// Collect all ivars, including those synthesized, in the current class.
890///
891void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000892 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000893 // FIXME. This need be removed but there are two many places which
894 // assume const-ness of ObjCInterfaceDecl
895 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
896 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
897 Iv= Iv->getNextIvar())
898 Ivars.push_back(Iv);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000899}
900
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000901/// DeepCollectObjCIvars -
902/// This routine first collects all declared, but not synthesized, ivars in
903/// super class and then collects all ivars, including those synthesized for
904/// current class. This routine is used for implementation of current class
905/// when all ivars, declared and synthesized are known.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000906///
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000907void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
908 bool leafClass,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000909 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000910 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
911 DeepCollectObjCIvars(SuperClass, false, Ivars);
912 if (!leafClass) {
913 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
914 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000915 Ivars.push_back(*I);
916 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000917 else
918 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000919}
920
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000921/// CollectInheritedProtocols - Collect all protocols in current class and
922/// those inherited by it.
923void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000924 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000925 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000926 // We can use protocol_iterator here instead of
927 // all_referenced_protocol_iterator since we are walking all categories.
928 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
929 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000930 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000931 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000932 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000933 PE = Proto->protocol_end(); P != PE; ++P) {
934 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000935 CollectInheritedProtocols(*P, Protocols);
936 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000937 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000938
939 // Categories of this Interface.
940 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
941 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
942 CollectInheritedProtocols(CDeclChain, Protocols);
943 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
944 while (SD) {
945 CollectInheritedProtocols(SD, Protocols);
946 SD = SD->getSuperClass();
947 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000948 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000949 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000950 PE = OC->protocol_end(); P != PE; ++P) {
951 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000952 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000953 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
954 PE = Proto->protocol_end(); P != PE; ++P)
955 CollectInheritedProtocols(*P, Protocols);
956 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000957 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000958 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
959 PE = OP->protocol_end(); P != PE; ++P) {
960 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000961 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000962 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
963 PE = Proto->protocol_end(); P != PE; ++P)
964 CollectInheritedProtocols(*P, Protocols);
965 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000966 }
967}
968
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000969unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
970 unsigned count = 0;
971 // Count ivars declared in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000972 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
973 CDecl = CDecl->getNextClassExtension())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000974 count += CDecl->ivar_size();
975
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000976 // Count ivar defined in this class's implementation. This
977 // includes synthesized ivars.
978 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000979 count += ImplDecl->ivar_size();
980
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000981 return count;
982}
983
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000984/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
985ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
986 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
987 I = ObjCImpls.find(D);
988 if (I != ObjCImpls.end())
989 return cast<ObjCImplementationDecl>(I->second);
990 return 0;
991}
992/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
993ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
994 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
995 I = ObjCImpls.find(D);
996 if (I != ObjCImpls.end())
997 return cast<ObjCCategoryImplDecl>(I->second);
998 return 0;
999}
1000
1001/// \brief Set the implementation of ObjCInterfaceDecl.
1002void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1003 ObjCImplementationDecl *ImplD) {
1004 assert(IFaceD && ImplD && "Passed null params");
1005 ObjCImpls[IFaceD] = ImplD;
1006}
1007/// \brief Set the implementation of ObjCCategoryDecl.
1008void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1009 ObjCCategoryImplDecl *ImplD) {
1010 assert(CatD && ImplD && "Passed null params");
1011 ObjCImpls[CatD] = ImplD;
1012}
1013
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001014/// \brief Get the copy initialization expression of VarDecl,or NULL if
1015/// none exists.
Fariborz Jahanian50198092010-12-02 17:02:11 +00001016Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001017 assert(VD && "Passed null params");
1018 assert(VD->hasAttr<BlocksAttr>() &&
1019 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian50198092010-12-02 17:02:11 +00001020 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001021 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001022 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1023}
1024
1025/// \brief Set the copy inialization expression of a block var decl.
1026void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1027 assert(VD && Init && "Passed null params");
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001028 assert(VD->hasAttr<BlocksAttr>() &&
1029 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001030 BlockVarCopyInits[VD] = Init;
1031}
1032
John McCallbcd03502009-12-07 02:54:59 +00001033/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001034///
John McCallbcd03502009-12-07 02:54:59 +00001035/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001036/// the TypeLoc wrappers.
1037///
1038/// \param T the type that will be the basis for type source info. This type
1039/// should refer to how the declarator was written in source code, not to
1040/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +00001041TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +00001042 unsigned DataSize) {
1043 if (!DataSize)
1044 DataSize = TypeLoc::getFullDataSizeForType(T);
1045 else
1046 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +00001047 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +00001048
John McCallbcd03502009-12-07 02:54:59 +00001049 TypeSourceInfo *TInfo =
1050 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1051 new (TInfo) TypeSourceInfo(T);
1052 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001053}
1054
John McCallbcd03502009-12-07 02:54:59 +00001055TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +00001056 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +00001057 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +00001058 DI->getTypeLoc().initialize(L);
1059 return DI;
1060}
1061
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001062const ASTRecordLayout &
1063ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1064 return getObjCLayout(D, 0);
1065}
1066
1067const ASTRecordLayout &
1068ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1069 return getObjCLayout(D->getClassInterface(), D);
1070}
1071
Chris Lattner983a8bb2007-07-13 22:13:22 +00001072//===----------------------------------------------------------------------===//
1073// Type creation/memoization methods
1074//===----------------------------------------------------------------------===//
1075
John McCall8ccfcb52009-09-24 19:53:00 +00001076QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1077 unsigned Fast = Quals.getFastQualifiers();
1078 Quals.removeFastQualifiers();
1079
1080 // Check if we've already instantiated this type.
1081 llvm::FoldingSetNodeID ID;
1082 ExtQuals::Profile(ID, TypeNode, Quals);
1083 void *InsertPos = 0;
1084 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1085 assert(EQ->getQualifiers() == Quals);
1086 QualType T = QualType(EQ, Fast);
1087 return T;
1088 }
1089
John McCall717d9b02010-12-10 11:01:00 +00001090 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001091 ExtQualNodes.InsertNode(New, InsertPos);
1092 QualType T = QualType(New, Fast);
1093 return T;
1094}
1095
Fariborz Jahanianece85822009-02-17 18:27:45 +00001096QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001097 QualType CanT = getCanonicalType(T);
1098 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001099 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001100
John McCall8ccfcb52009-09-24 19:53:00 +00001101 // If we are composing extended qualifiers together, merge together
1102 // into one ExtQuals node.
1103 QualifierCollector Quals;
1104 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001105
John McCall8ccfcb52009-09-24 19:53:00 +00001106 // If this type already has an address space specified, it cannot get
1107 // another one.
1108 assert(!Quals.hasAddressSpace() &&
1109 "Type cannot be in multiple addr spaces!");
1110 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001111
John McCall8ccfcb52009-09-24 19:53:00 +00001112 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001113}
1114
Chris Lattnerd60183d2009-02-18 22:53:11 +00001115QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001116 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001117 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001118 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001119 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001120
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001121 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001122 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001123 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001124 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1125 return getPointerType(ResultType);
1126 }
1127 }
Mike Stump11289f42009-09-09 15:08:12 +00001128
John McCall8ccfcb52009-09-24 19:53:00 +00001129 // If we are composing extended qualifiers together, merge together
1130 // into one ExtQuals node.
1131 QualifierCollector Quals;
1132 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001133
John McCall8ccfcb52009-09-24 19:53:00 +00001134 // If this type already has an ObjCGC specified, it cannot get
1135 // another one.
1136 assert(!Quals.hasObjCGCAttr() &&
1137 "Type cannot have multiple ObjCGCs!");
1138 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001139
John McCall8ccfcb52009-09-24 19:53:00 +00001140 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001141}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001142
John McCall4f5019e2010-12-19 02:44:49 +00001143const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1144 FunctionType::ExtInfo Info) {
1145 if (T->getExtInfo() == Info)
1146 return T;
1147
1148 QualType Result;
1149 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1150 Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1151 } else {
1152 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1153 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1154 EPI.ExtInfo = Info;
1155 Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1156 FPT->getNumArgs(), EPI);
1157 }
1158
1159 return cast<FunctionType>(Result.getTypePtr());
1160}
1161
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001162static QualType getExtFunctionType(ASTContext& Context, QualType T,
John McCall4f5019e2010-12-19 02:44:49 +00001163 FunctionType::ExtInfo Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001164 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001165 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1166 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001167 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001168 if (ResultType == Pointee)
1169 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001170
1171 ResultType = Context.getPointerType(ResultType);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001172 } else if (const ParenType *Paren = T->getAs<ParenType>()) {
1173 QualType Inner = Paren->getInnerType();
1174 ResultType = getExtFunctionType(Context, Inner, Info);
1175 if (ResultType == Inner)
1176 return T;
1177
1178 ResultType = Context.getParenType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001179 } else if (const BlockPointerType *BlockPointer
1180 = T->getAs<BlockPointerType>()) {
1181 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001182 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001183 if (ResultType == Pointee)
1184 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001185
1186 ResultType = Context.getBlockPointerType(ResultType);
Douglas Gregoreece0ea2010-09-01 16:29:03 +00001187 } else if (const MemberPointerType *MemberPointer
1188 = T->getAs<MemberPointerType>()) {
1189 QualType Pointee = MemberPointer->getPointeeType();
1190 ResultType = getExtFunctionType(Context, Pointee, Info);
1191 if (ResultType == Pointee)
1192 return T;
1193
1194 ResultType = Context.getMemberPointerType(ResultType,
1195 MemberPointer->getClass());
Douglas Gregor8c940862010-01-18 17:14:39 +00001196 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
John McCall4f5019e2010-12-19 02:44:49 +00001197 ResultType = QualType(Context.adjustFunctionType(F, Info), 0);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001198 } else
1199 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001200
1201 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1202}
1203
1204QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001205 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
John McCalldb40c7f2010-12-14 08:05:40 +00001206 return getExtFunctionType(*this, T, Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001207}
1208
Chris Lattnerc6395932007-06-22 20:56:16 +00001209/// getComplexType - Return the uniqued reference to the type for a complex
1210/// number with the specified element type.
1211QualType ASTContext::getComplexType(QualType T) {
1212 // Unique pointers, to guarantee there is only one pointer of a particular
1213 // structure.
1214 llvm::FoldingSetNodeID ID;
1215 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001216
Chris Lattnerc6395932007-06-22 20:56:16 +00001217 void *InsertPos = 0;
1218 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1219 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001220
Chris Lattnerc6395932007-06-22 20:56:16 +00001221 // If the pointee type isn't canonical, this won't be a canonical type either,
1222 // so fill in the canonical type field.
1223 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001224 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001225 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001226
Chris Lattnerc6395932007-06-22 20:56:16 +00001227 // Get the new insert position for the node we care about.
1228 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001229 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001230 }
John McCall90d1c2d2009-09-24 23:30:46 +00001231 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001232 Types.push_back(New);
1233 ComplexTypes.InsertNode(New, InsertPos);
1234 return QualType(New, 0);
1235}
1236
Chris Lattner970e54e2006-11-12 00:37:36 +00001237/// getPointerType - Return the uniqued reference to the type for a pointer to
1238/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001239QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001240 // Unique pointers, to guarantee there is only one pointer of a particular
1241 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001242 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001243 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001244
Chris Lattner67521df2007-01-27 01:29:36 +00001245 void *InsertPos = 0;
1246 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001247 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001248
Chris Lattner7ccecb92006-11-12 08:50:50 +00001249 // If the pointee type isn't canonical, this won't be a canonical type either,
1250 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001251 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001252 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001253 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001254
Chris Lattner67521df2007-01-27 01:29:36 +00001255 // Get the new insert position for the node we care about.
1256 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001257 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001258 }
John McCall90d1c2d2009-09-24 23:30:46 +00001259 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001260 Types.push_back(New);
1261 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001262 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001263}
1264
Mike Stump11289f42009-09-09 15:08:12 +00001265/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001266/// a pointer to the specified block.
1267QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001268 assert(T->isFunctionType() && "block of function types only");
1269 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001270 // structure.
1271 llvm::FoldingSetNodeID ID;
1272 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001273
Steve Naroffec33ed92008-08-27 16:04:49 +00001274 void *InsertPos = 0;
1275 if (BlockPointerType *PT =
1276 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1277 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001278
1279 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001280 // type either so fill in the canonical type field.
1281 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001282 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001283 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001284
Steve Naroffec33ed92008-08-27 16:04:49 +00001285 // Get the new insert position for the node we care about.
1286 BlockPointerType *NewIP =
1287 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001288 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001289 }
John McCall90d1c2d2009-09-24 23:30:46 +00001290 BlockPointerType *New
1291 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001292 Types.push_back(New);
1293 BlockPointerTypes.InsertNode(New, InsertPos);
1294 return QualType(New, 0);
1295}
1296
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001297/// getLValueReferenceType - Return the uniqued reference to the type for an
1298/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001299QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001300 // Unique pointers, to guarantee there is only one pointer of a particular
1301 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001302 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001303 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001304
1305 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001306 if (LValueReferenceType *RT =
1307 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001308 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001309
John McCallfc93cf92009-10-22 22:37:11 +00001310 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1311
Bill Wendling3708c182007-05-27 10:15:43 +00001312 // If the referencee type isn't canonical, this won't be a canonical type
1313 // either, so fill in the canonical type field.
1314 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001315 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1316 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1317 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001318
Bill Wendling3708c182007-05-27 10:15:43 +00001319 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001320 LValueReferenceType *NewIP =
1321 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001322 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001323 }
1324
John McCall90d1c2d2009-09-24 23:30:46 +00001325 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001326 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1327 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001328 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001329 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001330
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001331 return QualType(New, 0);
1332}
1333
1334/// getRValueReferenceType - Return the uniqued reference to the type for an
1335/// rvalue reference to the specified type.
1336QualType ASTContext::getRValueReferenceType(QualType T) {
1337 // Unique pointers, to guarantee there is only one pointer of a particular
1338 // structure.
1339 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001340 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001341
1342 void *InsertPos = 0;
1343 if (RValueReferenceType *RT =
1344 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1345 return QualType(RT, 0);
1346
John McCallfc93cf92009-10-22 22:37:11 +00001347 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1348
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001349 // If the referencee type isn't canonical, this won't be a canonical type
1350 // either, so fill in the canonical type field.
1351 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001352 if (InnerRef || !T.isCanonical()) {
1353 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1354 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001355
1356 // Get the new insert position for the node we care about.
1357 RValueReferenceType *NewIP =
1358 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1359 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1360 }
1361
John McCall90d1c2d2009-09-24 23:30:46 +00001362 RValueReferenceType *New
1363 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001364 Types.push_back(New);
1365 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001366 return QualType(New, 0);
1367}
1368
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001369/// getMemberPointerType - Return the uniqued reference to the type for a
1370/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001371QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001372 // Unique pointers, to guarantee there is only one pointer of a particular
1373 // structure.
1374 llvm::FoldingSetNodeID ID;
1375 MemberPointerType::Profile(ID, T, Cls);
1376
1377 void *InsertPos = 0;
1378 if (MemberPointerType *PT =
1379 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1380 return QualType(PT, 0);
1381
1382 // If the pointee or class type isn't canonical, this won't be a canonical
1383 // type either, so fill in the canonical type field.
1384 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001385 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001386 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1387
1388 // Get the new insert position for the node we care about.
1389 MemberPointerType *NewIP =
1390 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1391 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1392 }
John McCall90d1c2d2009-09-24 23:30:46 +00001393 MemberPointerType *New
1394 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001395 Types.push_back(New);
1396 MemberPointerTypes.InsertNode(New, InsertPos);
1397 return QualType(New, 0);
1398}
1399
Mike Stump11289f42009-09-09 15:08:12 +00001400/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001401/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001402QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001403 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001404 ArrayType::ArraySizeModifier ASM,
1405 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001406 assert((EltTy->isDependentType() ||
1407 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001408 "Constant array of VLAs is illegal!");
1409
Chris Lattnere2df3f92009-05-13 04:12:56 +00001410 // Convert the array size into a canonical width matching the pointer size for
1411 // the target.
1412 llvm::APInt ArySize(ArySizeIn);
Jay Foad6d4db0c2010-12-07 08:25:34 +00001413 ArySize =
1414 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001415
Chris Lattner23b7eb62007-06-15 23:05:46 +00001416 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001417 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001418
Chris Lattner36f8e652007-01-27 08:31:04 +00001419 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001420 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001421 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001422 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001423
Chris Lattner7ccecb92006-11-12 08:50:50 +00001424 // If the element type isn't canonical, this won't be a canonical type either,
1425 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001426 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001427 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001428 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001429 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001430 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001431 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001432 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001433 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001434 }
Mike Stump11289f42009-09-09 15:08:12 +00001435
John McCall90d1c2d2009-09-24 23:30:46 +00001436 ConstantArrayType *New = new(*this,TypeAlignment)
1437 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001438 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001439 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001440 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001441}
1442
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001443/// getIncompleteArrayType - Returns a unique reference to the type for a
1444/// incomplete array of the specified element type.
1445QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) {
1446 QualType ElemTy = getBaseElementType(Ty);
1447 DeclarationName Name;
1448 llvm::SmallVector<QualType, 8> ATypes;
1449 QualType ATy = Ty;
1450 while (const ArrayType *AT = getAsArrayType(ATy)) {
1451 ATypes.push_back(ATy);
1452 ATy = AT->getElementType();
1453 }
1454 for (int i = ATypes.size() - 1; i >= 0; i--) {
1455 if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) {
1456 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star,
1457 0, VAT->getBracketsRange());
1458 }
1459 else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) {
1460 llvm::APSInt ConstVal(CAT->getSize());
1461 ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0);
1462 }
1463 else if (getAsIncompleteArrayType(ATypes[i])) {
1464 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal,
1465 0, SourceRange());
1466 }
1467 else
1468 assert(false && "DependentArrayType is seen");
1469 }
1470 return ElemTy;
1471}
1472
1473/// getVariableArrayDecayedType - Returns a vla type where known sizes
1474/// are replaced with [*]
1475QualType ASTContext::getVariableArrayDecayedType(QualType Ty) {
1476 if (Ty->isPointerType()) {
1477 QualType BaseType = Ty->getAs<PointerType>()->getPointeeType();
1478 if (isa<VariableArrayType>(BaseType)) {
1479 ArrayType *AT = dyn_cast<ArrayType>(BaseType);
1480 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1481 if (VAT->getSizeExpr()) {
1482 Ty = getUnknownSizeVariableArrayType(BaseType);
1483 Ty = getPointerType(Ty);
1484 }
1485 }
1486 }
1487 return Ty;
1488}
1489
1490
Steve Naroffcadebd02007-08-30 18:14:25 +00001491/// getVariableArrayType - Returns a non-unique reference to the type for a
1492/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001493QualType ASTContext::getVariableArrayType(QualType EltTy,
1494 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001495 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001496 unsigned EltTypeQuals,
1497 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001498 // Since we don't unique expressions, it isn't possible to unique VLA's
1499 // that have an expression provided for their size.
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001500 QualType CanonType;
1501
1502 if (!EltTy.isCanonical()) {
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001503 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1504 EltTypeQuals, Brackets);
1505 }
1506
John McCall90d1c2d2009-09-24 23:30:46 +00001507 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001508 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001509
1510 VariableArrayTypes.push_back(New);
1511 Types.push_back(New);
1512 return QualType(New, 0);
1513}
1514
Douglas Gregor4619e432008-12-05 23:32:09 +00001515/// getDependentSizedArrayType - Returns a non-unique reference to
1516/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001517/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001518QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1519 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001520 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001521 unsigned EltTypeQuals,
1522 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001523 assert((!NumElts || NumElts->isTypeDependent() ||
1524 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001525 "Size must be type- or value-dependent!");
1526
Douglas Gregorf3f95522009-07-31 00:23:35 +00001527 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001528 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001529 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001530
Douglas Gregorf86c9392010-10-26 00:51:02 +00001531 QualType CanonicalEltTy = getCanonicalType(EltTy);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001532 if (NumElts) {
1533 // Dependently-sized array types that do not have a specified
1534 // number of elements will have their sizes deduced from an
1535 // initializer.
Douglas Gregorf86c9392010-10-26 00:51:02 +00001536 DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM,
Douglas Gregorad2956c2009-11-19 18:03:26 +00001537 EltTypeQuals, NumElts);
1538
1539 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1540 }
1541
Douglas Gregorf3f95522009-07-31 00:23:35 +00001542 DependentSizedArrayType *New;
1543 if (Canon) {
1544 // We already have a canonical version of this array type; use it as
1545 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001546 New = new (*this, TypeAlignment)
1547 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1548 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf86c9392010-10-26 00:51:02 +00001549 } else if (CanonicalEltTy == EltTy) {
1550 // This is a canonical type. Record it.
1551 New = new (*this, TypeAlignment)
1552 DependentSizedArrayType(*this, EltTy, QualType(),
1553 NumElts, ASM, EltTypeQuals, Brackets);
1554
1555 if (NumElts) {
1556#ifndef NDEBUG
1557 DependentSizedArrayType *CanonCheck
1558 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1559 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1560 (void)CanonCheck;
1561#endif
1562 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001563 }
Douglas Gregorf86c9392010-10-26 00:51:02 +00001564 } else {
1565 QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts,
1566 ASM, EltTypeQuals,
1567 SourceRange());
1568 New = new (*this, TypeAlignment)
1569 DependentSizedArrayType(*this, EltTy, Canon,
1570 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001571 }
Mike Stump11289f42009-09-09 15:08:12 +00001572
Douglas Gregor4619e432008-12-05 23:32:09 +00001573 Types.push_back(New);
1574 return QualType(New, 0);
1575}
1576
Eli Friedmanbd258282008-02-15 18:16:39 +00001577QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1578 ArrayType::ArraySizeModifier ASM,
1579 unsigned EltTypeQuals) {
1580 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001581 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001582
1583 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001584 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001585 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1586 return QualType(ATP, 0);
1587
1588 // If the element type isn't canonical, this won't be a canonical type
1589 // either, so fill in the canonical type field.
1590 QualType Canonical;
1591
John McCallb692a092009-10-22 20:10:53 +00001592 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001593 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001594 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001595
1596 // Get the new insert position for the node we care about.
1597 IncompleteArrayType *NewIP =
1598 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001599 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001600 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001601
John McCall90d1c2d2009-09-24 23:30:46 +00001602 IncompleteArrayType *New = new (*this, TypeAlignment)
1603 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001604
1605 IncompleteArrayTypes.InsertNode(New, InsertPos);
1606 Types.push_back(New);
1607 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001608}
1609
Steve Naroff91fcddb2007-07-18 18:00:27 +00001610/// getVectorType - Return the unique reference to a vector type of
1611/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001612QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001613 VectorType::VectorKind VecKind) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00001614 BuiltinType *BaseType;
Mike Stump11289f42009-09-09 15:08:12 +00001615
Chris Lattnerdc226c22010-10-01 22:42:38 +00001616 BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1617 assert(BaseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001618
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001619 // Check if we've already instantiated a vector of this type.
1620 llvm::FoldingSetNodeID ID;
Bob Wilsonaeb56442010-11-10 21:56:12 +00001621 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner37141f42010-06-23 06:00:24 +00001622
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001623 void *InsertPos = 0;
1624 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1625 return QualType(VTP, 0);
1626
1627 // If the element type isn't canonical, this won't be a canonical type either,
1628 // so fill in the canonical type field.
1629 QualType Canonical;
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00001630 if (!vecType.isCanonical()) {
Bob Wilson77954802010-11-16 00:32:20 +00001631 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump11289f42009-09-09 15:08:12 +00001632
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001633 // Get the new insert position for the node we care about.
1634 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001635 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001636 }
John McCall90d1c2d2009-09-24 23:30:46 +00001637 VectorType *New = new (*this, TypeAlignment)
Bob Wilsonaeb56442010-11-10 21:56:12 +00001638 VectorType(vecType, NumElts, Canonical, VecKind);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001639 VectorTypes.InsertNode(New, InsertPos);
1640 Types.push_back(New);
1641 return QualType(New, 0);
1642}
1643
Nate Begemance4d7fc2008-04-18 23:10:10 +00001644/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001645/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001646QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001647 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001648
Chris Lattner76a00cf2008-04-06 22:59:24 +00001649 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001650 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001651
Steve Naroff91fcddb2007-07-18 18:00:27 +00001652 // Check if we've already instantiated a vector of this type.
1653 llvm::FoldingSetNodeID ID;
Chris Lattner37141f42010-06-23 06:00:24 +00001654 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001655 VectorType::GenericVector);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001656 void *InsertPos = 0;
1657 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1658 return QualType(VTP, 0);
1659
1660 // If the element type isn't canonical, this won't be a canonical type either,
1661 // so fill in the canonical type field.
1662 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001663 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001664 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001665
Steve Naroff91fcddb2007-07-18 18:00:27 +00001666 // Get the new insert position for the node we care about.
1667 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001668 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001669 }
John McCall90d1c2d2009-09-24 23:30:46 +00001670 ExtVectorType *New = new (*this, TypeAlignment)
1671 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001672 VectorTypes.InsertNode(New, InsertPos);
1673 Types.push_back(New);
1674 return QualType(New, 0);
1675}
1676
Mike Stump11289f42009-09-09 15:08:12 +00001677QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001678 Expr *SizeExpr,
1679 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001680 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001681 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001682 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001683
Douglas Gregor352169a2009-07-31 03:54:25 +00001684 void *InsertPos = 0;
1685 DependentSizedExtVectorType *Canon
1686 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1687 DependentSizedExtVectorType *New;
1688 if (Canon) {
1689 // We already have a canonical version of this array type; use it as
1690 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001691 New = new (*this, TypeAlignment)
1692 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1693 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001694 } else {
1695 QualType CanonVecTy = getCanonicalType(vecType);
1696 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001697 New = new (*this, TypeAlignment)
1698 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1699 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001700
1701 DependentSizedExtVectorType *CanonCheck
1702 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1703 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1704 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001705 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1706 } else {
1707 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1708 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001709 New = new (*this, TypeAlignment)
1710 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001711 }
1712 }
Mike Stump11289f42009-09-09 15:08:12 +00001713
Douglas Gregor758a8692009-06-17 21:51:59 +00001714 Types.push_back(New);
1715 return QualType(New, 0);
1716}
1717
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001718/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001719///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001720QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1721 const FunctionType::ExtInfo &Info) {
1722 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001723 // Unique functions, to guarantee there is only one function of a particular
1724 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001725 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001726 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001727
Chris Lattner47955de2007-01-27 08:37:20 +00001728 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001729 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001730 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001731 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001732
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001733 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001734 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001735 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001736 Canonical =
1737 getFunctionNoProtoType(getCanonicalType(ResultTy),
1738 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001739
Chris Lattner47955de2007-01-27 08:37:20 +00001740 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001741 FunctionNoProtoType *NewIP =
1742 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001743 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001744 }
Mike Stump11289f42009-09-09 15:08:12 +00001745
John McCall90d1c2d2009-09-24 23:30:46 +00001746 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001747 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001748 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001749 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001750 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001751}
1752
1753/// getFunctionType - Return a normal function type with a typed argument
1754/// list. isVariadic indicates whether the argument list includes '...'.
John McCalldb40c7f2010-12-14 08:05:40 +00001755QualType ASTContext::getFunctionType(QualType ResultTy,
1756 const QualType *ArgArray, unsigned NumArgs,
1757 const FunctionProtoType::ExtProtoInfo &EPI) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001758 // Unique functions, to guarantee there is only one function of a particular
1759 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001760 llvm::FoldingSetNodeID ID;
John McCalldb40c7f2010-12-14 08:05:40 +00001761 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001762
1763 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001764 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001765 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001766 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001767
1768 // Determine whether the type being created is already canonical or not.
John McCalldb40c7f2010-12-14 08:05:40 +00001769 bool isCanonical = !EPI.HasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001770 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001771 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001772 isCanonical = false;
1773
John McCalldb40c7f2010-12-14 08:05:40 +00001774 const CallingConv CallConv = EPI.ExtInfo.getCC();
1775
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001776 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001777 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001778 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001779 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001780 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001781 CanonicalArgs.reserve(NumArgs);
1782 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001783 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001784
John McCalldb40c7f2010-12-14 08:05:40 +00001785 FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
1786 if (CanonicalEPI.HasExceptionSpec) {
1787 CanonicalEPI.HasExceptionSpec = false;
1788 CanonicalEPI.HasAnyExceptionSpec = false;
1789 CanonicalEPI.NumExceptions = 0;
1790 }
1791 CanonicalEPI.ExtInfo
1792 = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
1793
Chris Lattner76a00cf2008-04-06 22:59:24 +00001794 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001795 CanonicalArgs.data(), NumArgs,
John McCalldb40c7f2010-12-14 08:05:40 +00001796 CanonicalEPI);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001797
Chris Lattnerfd4de792007-01-27 01:15:32 +00001798 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001799 FunctionProtoType *NewIP =
1800 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001801 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001802 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001803
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001804 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001805 // for two variable size arrays (for parameter and exception types) at the
1806 // end of them.
John McCalldb40c7f2010-12-14 08:05:40 +00001807 size_t Size = sizeof(FunctionProtoType) +
1808 NumArgs * sizeof(QualType) +
1809 EPI.NumExceptions * sizeof(QualType);
1810 FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
1811 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, EPI);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001812 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001813 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001814 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001815}
Chris Lattneref51c202006-11-10 07:17:23 +00001816
John McCalle78aac42010-03-10 03:28:59 +00001817#ifndef NDEBUG
1818static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1819 if (!isa<CXXRecordDecl>(D)) return false;
1820 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1821 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1822 return true;
1823 if (RD->getDescribedClassTemplate() &&
1824 !isa<ClassTemplateSpecializationDecl>(RD))
1825 return true;
1826 return false;
1827}
1828#endif
1829
1830/// getInjectedClassNameType - Return the unique reference to the
1831/// injected class name type for the specified templated declaration.
1832QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1833 QualType TST) {
1834 assert(NeedsInjectedClassNameType(Decl));
1835 if (Decl->TypeForDecl) {
1836 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001837 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCalle78aac42010-03-10 03:28:59 +00001838 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1839 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1840 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1841 } else {
John McCall2408e322010-04-27 00:57:59 +00001842 Decl->TypeForDecl =
1843 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001844 Types.push_back(Decl->TypeForDecl);
1845 }
1846 return QualType(Decl->TypeForDecl, 0);
1847}
1848
Douglas Gregor83a586e2008-04-13 21:07:44 +00001849/// getTypeDeclType - Return the unique reference to the type for the
1850/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001851QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001852 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001853 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001854
John McCall81e38502010-02-16 03:57:14 +00001855 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001856 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001857
John McCall96f0b5f2010-03-10 06:48:02 +00001858 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1859 "Template type parameter types are always available.");
1860
John McCall81e38502010-02-16 03:57:14 +00001861 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001862 assert(!Record->getPreviousDeclaration() &&
1863 "struct/union has previous declaration");
1864 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001865 return getRecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001866 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001867 assert(!Enum->getPreviousDeclaration() &&
1868 "enum has previous declaration");
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001869 return getEnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001870 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001871 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1872 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001873 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001874 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001875
John McCall96f0b5f2010-03-10 06:48:02 +00001876 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001877 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001878}
1879
Chris Lattner32d920b2007-01-26 02:01:53 +00001880/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001881/// specified typename decl.
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001882QualType
1883ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001884 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001885
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001886 if (Canonical.isNull())
1887 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001888 Decl->TypeForDecl = new(*this, TypeAlignment)
1889 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001890 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001891 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001892}
1893
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001894QualType ASTContext::getRecordType(const RecordDecl *Decl) {
1895 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1896
1897 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
1898 if (PrevDecl->TypeForDecl)
1899 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1900
1901 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
1902 Types.push_back(Decl->TypeForDecl);
1903 return QualType(Decl->TypeForDecl, 0);
1904}
1905
1906QualType ASTContext::getEnumType(const EnumDecl *Decl) {
1907 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1908
1909 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
1910 if (PrevDecl->TypeForDecl)
1911 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1912
1913 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
1914 Types.push_back(Decl->TypeForDecl);
1915 return QualType(Decl->TypeForDecl, 0);
1916}
1917
John McCallcebee162009-10-18 09:09:24 +00001918/// \brief Retrieve a substitution-result type.
1919QualType
1920ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1921 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001922 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001923 && "replacement types must always be canonical");
1924
1925 llvm::FoldingSetNodeID ID;
1926 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1927 void *InsertPos = 0;
1928 SubstTemplateTypeParmType *SubstParm
1929 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1930
1931 if (!SubstParm) {
1932 SubstParm = new (*this, TypeAlignment)
1933 SubstTemplateTypeParmType(Parm, Replacement);
1934 Types.push_back(SubstParm);
1935 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1936 }
1937
1938 return QualType(SubstParm, 0);
1939}
1940
Douglas Gregoreff93e02009-02-05 23:33:38 +00001941/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001942/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001943/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001944QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001945 bool ParameterPack,
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001946 IdentifierInfo *Name) {
Douglas Gregoreff93e02009-02-05 23:33:38 +00001947 llvm::FoldingSetNodeID ID;
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001948 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001949 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001950 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001951 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1952
1953 if (TypeParm)
1954 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001955
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001956 if (Name) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00001957 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001958 TypeParm = new (*this, TypeAlignment)
1959 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001960
1961 TemplateTypeParmType *TypeCheck
1962 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1963 assert(!TypeCheck && "Template type parameter canonical type broken");
1964 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001965 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001966 TypeParm = new (*this, TypeAlignment)
1967 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001968
1969 Types.push_back(TypeParm);
1970 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1971
1972 return QualType(TypeParm, 0);
1973}
1974
John McCalle78aac42010-03-10 03:28:59 +00001975TypeSourceInfo *
1976ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1977 SourceLocation NameLoc,
1978 const TemplateArgumentListInfo &Args,
1979 QualType CanonType) {
1980 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1981
1982 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1983 TemplateSpecializationTypeLoc TL
1984 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1985 TL.setTemplateNameLoc(NameLoc);
1986 TL.setLAngleLoc(Args.getLAngleLoc());
1987 TL.setRAngleLoc(Args.getRAngleLoc());
1988 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1989 TL.setArgLocInfo(i, Args[i].getLocInfo());
1990 return DI;
1991}
1992
Mike Stump11289f42009-09-09 15:08:12 +00001993QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001994ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001995 const TemplateArgumentListInfo &Args,
John McCall30576cd2010-06-13 09:25:03 +00001996 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00001997 unsigned NumArgs = Args.size();
1998
John McCall0ad16662009-10-29 08:12:44 +00001999 llvm::SmallVector<TemplateArgument, 4> ArgVec;
2000 ArgVec.reserve(NumArgs);
2001 for (unsigned i = 0; i != NumArgs; ++i)
2002 ArgVec.push_back(Args[i].getArgument());
2003
John McCall2408e322010-04-27 00:57:59 +00002004 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00002005 Canon);
John McCall0ad16662009-10-29 08:12:44 +00002006}
2007
2008QualType
2009ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00002010 const TemplateArgument *Args,
2011 unsigned NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00002012 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00002013 if (!Canon.isNull())
2014 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002015 else
2016 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregord56a91e2009-02-26 22:19:44 +00002017
Douglas Gregora8e02e72009-07-28 23:00:59 +00002018 // Allocate the (non-canonical) template specialization type, but don't
2019 // try to unique it: these types typically have location information that
2020 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00002021 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00002022 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00002023 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00002024 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00002025 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00002026 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00002027 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00002028
Douglas Gregor8bf42052009-02-09 18:46:07 +00002029 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00002030 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002031}
2032
Mike Stump11289f42009-09-09 15:08:12 +00002033QualType
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002034ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2035 const TemplateArgument *Args,
2036 unsigned NumArgs) {
2037 // Build the canonical template specialization type.
2038 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2039 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2040 CanonArgs.reserve(NumArgs);
2041 for (unsigned I = 0; I != NumArgs; ++I)
2042 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2043
2044 // Determine whether this canonical template specialization type already
2045 // exists.
2046 llvm::FoldingSetNodeID ID;
2047 TemplateSpecializationType::Profile(ID, CanonTemplate,
2048 CanonArgs.data(), NumArgs, *this);
2049
2050 void *InsertPos = 0;
2051 TemplateSpecializationType *Spec
2052 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2053
2054 if (!Spec) {
2055 // Allocate a new canonical template specialization type.
2056 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2057 sizeof(TemplateArgument) * NumArgs),
2058 TypeAlignment);
2059 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2060 CanonArgs.data(), NumArgs,
2061 QualType());
2062 Types.push_back(Spec);
2063 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2064 }
2065
2066 assert(Spec->isDependentType() &&
2067 "Non-dependent template-id type must have a canonical type");
2068 return QualType(Spec, 0);
2069}
2070
2071QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00002072ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2073 NestedNameSpecifier *NNS,
2074 QualType NamedType) {
Douglas Gregor52537682009-03-19 00:18:19 +00002075 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002076 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00002077
2078 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002079 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002080 if (T)
2081 return QualType(T, 0);
2082
Douglas Gregorc42075a2010-02-04 18:10:26 +00002083 QualType Canon = NamedType;
2084 if (!Canon.isCanonical()) {
2085 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002086 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2087 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00002088 (void)CheckT;
2089 }
2090
Abramo Bagnara6150c882010-05-11 21:36:43 +00002091 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00002092 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002093 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002094 return QualType(T, 0);
2095}
2096
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002097QualType
2098ASTContext::getParenType(QualType InnerType) {
2099 llvm::FoldingSetNodeID ID;
2100 ParenType::Profile(ID, InnerType);
2101
2102 void *InsertPos = 0;
2103 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2104 if (T)
2105 return QualType(T, 0);
2106
2107 QualType Canon = InnerType;
2108 if (!Canon.isCanonical()) {
2109 Canon = getCanonicalType(InnerType);
2110 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2111 assert(!CheckT && "Paren canonical type broken");
2112 (void)CheckT;
2113 }
2114
2115 T = new (*this) ParenType(InnerType, Canon);
2116 Types.push_back(T);
2117 ParenTypes.InsertNode(T, InsertPos);
2118 return QualType(T, 0);
2119}
2120
Douglas Gregor02085352010-03-31 20:19:30 +00002121QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2122 NestedNameSpecifier *NNS,
2123 const IdentifierInfo *Name,
2124 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00002125 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2126
2127 if (Canon.isNull()) {
2128 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00002129 ElaboratedTypeKeyword CanonKeyword = Keyword;
2130 if (Keyword == ETK_None)
2131 CanonKeyword = ETK_Typename;
2132
2133 if (CanonNNS != NNS || CanonKeyword != Keyword)
2134 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002135 }
2136
2137 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002138 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002139
2140 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002141 DependentNameType *T
2142 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00002143 if (T)
2144 return QualType(T, 0);
2145
Douglas Gregor02085352010-03-31 20:19:30 +00002146 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002147 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002148 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002149 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002150}
2151
Mike Stump11289f42009-09-09 15:08:12 +00002152QualType
John McCallc392f372010-06-11 00:33:02 +00002153ASTContext::getDependentTemplateSpecializationType(
2154 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002155 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002156 const IdentifierInfo *Name,
2157 const TemplateArgumentListInfo &Args) {
2158 // TODO: avoid this copy
2159 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2160 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2161 ArgCopy.push_back(Args[I].getArgument());
2162 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2163 ArgCopy.size(),
2164 ArgCopy.data());
2165}
2166
2167QualType
2168ASTContext::getDependentTemplateSpecializationType(
2169 ElaboratedTypeKeyword Keyword,
2170 NestedNameSpecifier *NNS,
2171 const IdentifierInfo *Name,
2172 unsigned NumArgs,
2173 const TemplateArgument *Args) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002174 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2175
Douglas Gregorc42075a2010-02-04 18:10:26 +00002176 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002177 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2178 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002179
2180 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002181 DependentTemplateSpecializationType *T
2182 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002183 if (T)
2184 return QualType(T, 0);
2185
John McCallc392f372010-06-11 00:33:02 +00002186 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002187
John McCallc392f372010-06-11 00:33:02 +00002188 ElaboratedTypeKeyword CanonKeyword = Keyword;
2189 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2190
2191 bool AnyNonCanonArgs = false;
2192 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2193 for (unsigned I = 0; I != NumArgs; ++I) {
2194 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2195 if (!CanonArgs[I].structurallyEquals(Args[I]))
2196 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002197 }
2198
John McCallc392f372010-06-11 00:33:02 +00002199 QualType Canon;
2200 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2201 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2202 Name, NumArgs,
2203 CanonArgs.data());
2204
2205 // Find the insert position again.
2206 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2207 }
2208
2209 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2210 sizeof(TemplateArgument) * NumArgs),
2211 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002212 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002213 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002214 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002215 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002216 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002217}
2218
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002219/// CmpProtocolNames - Comparison predicate for sorting protocols
2220/// alphabetically.
2221static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2222 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002223 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002224}
2225
John McCall8b07ec22010-05-15 11:32:37 +00002226static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002227 unsigned NumProtocols) {
2228 if (NumProtocols == 0) return true;
2229
2230 for (unsigned i = 1; i != NumProtocols; ++i)
2231 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2232 return false;
2233 return true;
2234}
2235
2236static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002237 unsigned &NumProtocols) {
2238 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002239
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002240 // Sort protocols, keyed by name.
2241 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2242
2243 // Remove duplicates.
2244 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2245 NumProtocols = ProtocolsEnd-Protocols;
2246}
2247
John McCall8b07ec22010-05-15 11:32:37 +00002248QualType ASTContext::getObjCObjectType(QualType BaseType,
2249 ObjCProtocolDecl * const *Protocols,
2250 unsigned NumProtocols) {
2251 // If the base type is an interface and there aren't any protocols
2252 // to add, then the interface type will do just fine.
2253 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2254 return BaseType;
2255
2256 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002257 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002258 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002259 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002260 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2261 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002262
John McCall8b07ec22010-05-15 11:32:37 +00002263 // Build the canonical type, which has the canonical base type and
2264 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002265 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002266 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2267 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2268 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002269 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2270 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002271 unsigned UniqueCount = NumProtocols;
2272
John McCallfc93cf92009-10-22 22:37:11 +00002273 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002274 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2275 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002276 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002277 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2278 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002279 }
2280
2281 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002282 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2283 }
2284
2285 unsigned Size = sizeof(ObjCObjectTypeImpl);
2286 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2287 void *Mem = Allocate(Size, TypeAlignment);
2288 ObjCObjectTypeImpl *T =
2289 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2290
2291 Types.push_back(T);
2292 ObjCObjectTypes.InsertNode(T, InsertPos);
2293 return QualType(T, 0);
2294}
2295
2296/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2297/// the given object type.
2298QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2299 llvm::FoldingSetNodeID ID;
2300 ObjCObjectPointerType::Profile(ID, ObjectT);
2301
2302 void *InsertPos = 0;
2303 if (ObjCObjectPointerType *QT =
2304 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2305 return QualType(QT, 0);
2306
2307 // Find the canonical object type.
2308 QualType Canonical;
2309 if (!ObjectT.isCanonical()) {
2310 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2311
2312 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002313 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2314 }
2315
Douglas Gregorf85bee62010-02-08 22:59:26 +00002316 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002317 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2318 ObjCObjectPointerType *QType =
2319 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002320
Steve Narofffb4330f2009-06-17 22:40:22 +00002321 Types.push_back(QType);
2322 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002323 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002324}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002325
Douglas Gregor1c283312010-08-11 12:19:30 +00002326/// getObjCInterfaceType - Return the unique reference to the type for the
2327/// specified ObjC interface decl. The list of protocols is optional.
2328QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2329 if (Decl->TypeForDecl)
2330 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002331
Douglas Gregor1c283312010-08-11 12:19:30 +00002332 // FIXME: redeclarations?
2333 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2334 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2335 Decl->TypeForDecl = T;
2336 Types.push_back(T);
2337 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002338}
2339
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002340/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2341/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002342/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002343/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002344/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002345QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002346 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002347 if (tofExpr->isTypeDependent()) {
2348 llvm::FoldingSetNodeID ID;
2349 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002350
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002351 void *InsertPos = 0;
2352 DependentTypeOfExprType *Canon
2353 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2354 if (Canon) {
2355 // We already have a "canonical" version of an identical, dependent
2356 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002357 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002358 QualType((TypeOfExprType*)Canon, 0));
2359 }
2360 else {
2361 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002362 Canon
2363 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002364 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2365 toe = Canon;
2366 }
2367 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002368 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002369 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002370 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002371 Types.push_back(toe);
2372 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002373}
2374
Steve Naroffa773cd52007-08-01 18:02:17 +00002375/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2376/// TypeOfType AST's. The only motivation to unique these nodes would be
2377/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002378/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002379/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002380QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002381 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002382 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002383 Types.push_back(tot);
2384 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002385}
2386
Anders Carlssonad6bd352009-06-24 21:24:56 +00002387/// getDecltypeForExpr - Given an expr, will return the decltype for that
2388/// expression, according to the rules in C++0x [dcl.type.simple]p4
2389static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002390 if (e->isTypeDependent())
2391 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002392
Anders Carlssonad6bd352009-06-24 21:24:56 +00002393 // If e is an id expression or a class member access, decltype(e) is defined
2394 // as the type of the entity named by e.
2395 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2396 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2397 return VD->getType();
2398 }
2399 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2400 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2401 return FD->getType();
2402 }
2403 // If e is a function call or an invocation of an overloaded operator,
2404 // (parentheses around e are ignored), decltype(e) is defined as the
2405 // return type of that function.
2406 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2407 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002408
Anders Carlssonad6bd352009-06-24 21:24:56 +00002409 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002410
2411 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002412 // defined as T&, otherwise decltype(e) is defined as T.
John McCall086a4642010-11-24 05:12:34 +00002413 if (e->isLValue())
Anders Carlssonad6bd352009-06-24 21:24:56 +00002414 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002415
Anders Carlssonad6bd352009-06-24 21:24:56 +00002416 return T;
2417}
2418
Anders Carlsson81df7b82009-06-24 19:06:50 +00002419/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2420/// DecltypeType AST's. The only motivation to unique these nodes would be
2421/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002422/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002423/// on canonical type's (which are always unique).
2424QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002425 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002426 if (e->isTypeDependent()) {
2427 llvm::FoldingSetNodeID ID;
2428 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002429
Douglas Gregora21f6c32009-07-30 23:36:40 +00002430 void *InsertPos = 0;
2431 DependentDecltypeType *Canon
2432 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2433 if (Canon) {
2434 // We already have a "canonical" version of an equivalent, dependent
2435 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002436 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002437 QualType((DecltypeType*)Canon, 0));
2438 }
2439 else {
2440 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002441 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002442 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2443 dt = Canon;
2444 }
2445 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002446 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002447 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002448 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002449 Types.push_back(dt);
2450 return QualType(dt, 0);
2451}
2452
Chris Lattnerfb072462007-01-23 05:45:31 +00002453/// getTagDeclType - Return the unique reference to the type for the
2454/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002455QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002456 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002457 // FIXME: What is the design on getTagDeclType when it requires casting
2458 // away const? mutable?
2459 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002460}
2461
Mike Stump11289f42009-09-09 15:08:12 +00002462/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2463/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2464/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002465CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002466 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002467}
Chris Lattnerfb072462007-01-23 05:45:31 +00002468
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002469/// getSignedWCharType - Return the type of "signed wchar_t".
2470/// Used when in C++, as a GCC extension.
2471QualType ASTContext::getSignedWCharType() const {
2472 // FIXME: derive from "Target" ?
2473 return WCharTy;
2474}
2475
2476/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2477/// Used when in C++, as a GCC extension.
2478QualType ASTContext::getUnsignedWCharType() const {
2479 // FIXME: derive from "Target" ?
2480 return UnsignedIntTy;
2481}
2482
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002483/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2484/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2485QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002486 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002487}
2488
Chris Lattnera21ad802008-04-02 05:18:44 +00002489//===----------------------------------------------------------------------===//
2490// Type Operators
2491//===----------------------------------------------------------------------===//
2492
John McCallfc93cf92009-10-22 22:37:11 +00002493CanQualType ASTContext::getCanonicalParamType(QualType T) {
2494 // Push qualifiers into arrays, and then discard any remaining
2495 // qualifiers.
2496 T = getCanonicalType(T);
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00002497 T = getVariableArrayDecayedType(T);
John McCallfc93cf92009-10-22 22:37:11 +00002498 const Type *Ty = T.getTypePtr();
John McCallfc93cf92009-10-22 22:37:11 +00002499 QualType Result;
2500 if (isa<ArrayType>(Ty)) {
2501 Result = getArrayDecayedType(QualType(Ty,0));
2502 } else if (isa<FunctionType>(Ty)) {
2503 Result = getPointerType(QualType(Ty, 0));
2504 } else {
2505 Result = QualType(Ty, 0);
2506 }
2507
2508 return CanQualType::CreateUnsafe(Result);
2509}
2510
Chris Lattnered0d0792008-04-06 22:41:35 +00002511/// getCanonicalType - Return the canonical (structural) type corresponding to
2512/// the specified potentially non-canonical type. The non-canonical version
2513/// of a type may have many "decorated" versions of types. Decorators can
2514/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2515/// to be free of any of these, allowing two canonical types to be compared
2516/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002517CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002518 QualifierCollector Quals;
2519 const Type *Ptr = Quals.strip(T);
2520 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002521
John McCall8ccfcb52009-09-24 19:53:00 +00002522 // The canonical internal type will be the canonical type *except*
2523 // that we push type qualifiers down through array types.
2524
2525 // If there are no new qualifiers to push down, stop here.
2526 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002527 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002528
John McCall8ccfcb52009-09-24 19:53:00 +00002529 // If the type qualifiers are on an array type, get the canonical
2530 // type of the array with the qualifiers applied to the element
2531 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002532 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2533 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002534 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002535
Chris Lattner7adf0762008-08-04 07:31:14 +00002536 // Get the canonical version of the element with the extra qualifiers on it.
2537 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002538 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002539 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002540
Chris Lattner7adf0762008-08-04 07:31:14 +00002541 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002542 return CanQualType::CreateUnsafe(
2543 getConstantArrayType(NewEltTy, CAT->getSize(),
2544 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002545 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002546 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002547 return CanQualType::CreateUnsafe(
2548 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002549 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002550
Douglas Gregor4619e432008-12-05 23:32:09 +00002551 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002552 return CanQualType::CreateUnsafe(
2553 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002554 DSAT->getSizeExpr(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002555 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002556 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002557 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002558
Chris Lattner7adf0762008-08-04 07:31:14 +00002559 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002560 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002561 VAT->getSizeExpr(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002562 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002563 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002564 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002565}
2566
Chandler Carruth607f38e2009-12-29 07:16:59 +00002567QualType ASTContext::getUnqualifiedArrayType(QualType T,
2568 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002569 Quals = T.getQualifiers();
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002570 const ArrayType *AT = getAsArrayType(T);
2571 if (!AT) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002572 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002573 }
2574
Chandler Carruth607f38e2009-12-29 07:16:59 +00002575 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002576 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002577 if (Elt == UnqualElt)
2578 return T;
2579
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002580 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002581 return getConstantArrayType(UnqualElt, CAT->getSize(),
2582 CAT->getSizeModifier(), 0);
2583 }
2584
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002585 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002586 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2587 }
2588
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002589 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2590 return getVariableArrayType(UnqualElt,
John McCallc3007a22010-10-26 07:05:15 +00002591 VAT->getSizeExpr(),
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002592 VAT->getSizeModifier(),
2593 VAT->getIndexTypeCVRQualifiers(),
2594 VAT->getBracketsRange());
2595 }
2596
2597 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCallc3007a22010-10-26 07:05:15 +00002598 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002599 DSAT->getSizeModifier(), 0,
2600 SourceRange());
2601}
2602
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002603/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2604/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2605/// they point to and return true. If T1 and T2 aren't pointer types
2606/// or pointer-to-member types, or if they are not similar at this
2607/// level, returns false and leaves T1 and T2 unchanged. Top-level
2608/// qualifiers on T1 and T2 are ignored. This function will typically
2609/// be called in a loop that successively "unwraps" pointer and
2610/// pointer-to-member types to compare them at each level.
2611bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2612 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2613 *T2PtrType = T2->getAs<PointerType>();
2614 if (T1PtrType && T2PtrType) {
2615 T1 = T1PtrType->getPointeeType();
2616 T2 = T2PtrType->getPointeeType();
2617 return true;
2618 }
2619
2620 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2621 *T2MPType = T2->getAs<MemberPointerType>();
2622 if (T1MPType && T2MPType &&
2623 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2624 QualType(T2MPType->getClass(), 0))) {
2625 T1 = T1MPType->getPointeeType();
2626 T2 = T2MPType->getPointeeType();
2627 return true;
2628 }
2629
2630 if (getLangOptions().ObjC1) {
2631 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2632 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2633 if (T1OPType && T2OPType) {
2634 T1 = T1OPType->getPointeeType();
2635 T2 = T2OPType->getPointeeType();
2636 return true;
2637 }
2638 }
2639
2640 // FIXME: Block pointers, too?
2641
2642 return false;
2643}
2644
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002645DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name,
2646 SourceLocation NameLoc) {
John McCall847e2a12009-11-24 18:42:40 +00002647 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002648 // DNInfo work in progress: CHECKME: what about DNLoc?
2649 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2650
John McCall847e2a12009-11-24 18:42:40 +00002651 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002652 DeclarationName DName;
John McCall847e2a12009-11-24 18:42:40 +00002653 if (DTN->isIdentifier()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002654 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2655 return DeclarationNameInfo(DName, NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002656 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002657 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2658 // DNInfo work in progress: FIXME: source locations?
2659 DeclarationNameLoc DNLoc;
2660 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2661 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2662 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall847e2a12009-11-24 18:42:40 +00002663 }
2664 }
2665
John McCalld28ae272009-12-02 08:04:21 +00002666 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2667 assert(Storage);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002668 // DNInfo work in progress: CHECKME: what about DNLoc?
2669 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002670}
2671
Douglas Gregor6bc50582009-05-07 06:41:52 +00002672TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002673 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2674 if (TemplateTemplateParmDecl *TTP
2675 = dyn_cast<TemplateTemplateParmDecl>(Template))
2676 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2677
2678 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002679 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002680 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002681
John McCalld28ae272009-12-02 08:04:21 +00002682 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002683
Douglas Gregor6bc50582009-05-07 06:41:52 +00002684 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2685 assert(DTN && "Non-dependent template names must refer to template decls.");
2686 return DTN->CanonicalTemplateName;
2687}
2688
Douglas Gregoradee3e32009-11-11 23:06:43 +00002689bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2690 X = getCanonicalTemplateName(X);
2691 Y = getCanonicalTemplateName(Y);
2692 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2693}
2694
Mike Stump11289f42009-09-09 15:08:12 +00002695TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002696ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2697 switch (Arg.getKind()) {
2698 case TemplateArgument::Null:
2699 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002700
Douglas Gregora8e02e72009-07-28 23:00:59 +00002701 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002702 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002703
Douglas Gregora8e02e72009-07-28 23:00:59 +00002704 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002705 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002706
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002707 case TemplateArgument::Template:
2708 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2709
Douglas Gregora8e02e72009-07-28 23:00:59 +00002710 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002711 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002712 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002713
Douglas Gregora8e02e72009-07-28 23:00:59 +00002714 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002715 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002716
Douglas Gregora8e02e72009-07-28 23:00:59 +00002717 case TemplateArgument::Pack: {
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002718 TemplateArgument *CanonArgs
2719 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregora8e02e72009-07-28 23:00:59 +00002720 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002721 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002722 AEnd = Arg.pack_end();
2723 A != AEnd; (void)++A, ++Idx)
2724 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002725
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002726 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregora8e02e72009-07-28 23:00:59 +00002727 }
2728 }
2729
2730 // Silence GCC warning
2731 assert(false && "Unhandled template argument kind");
2732 return TemplateArgument();
2733}
2734
Douglas Gregor333489b2009-03-27 23:10:48 +00002735NestedNameSpecifier *
2736ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002737 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002738 return 0;
2739
2740 switch (NNS->getKind()) {
2741 case NestedNameSpecifier::Identifier:
2742 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002743 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002744 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2745 NNS->getAsIdentifier());
2746
2747 case NestedNameSpecifier::Namespace:
2748 // A namespace is canonical; build a nested-name-specifier with
2749 // this namespace and no prefix.
2750 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2751
2752 case NestedNameSpecifier::TypeSpec:
2753 case NestedNameSpecifier::TypeSpecWithTemplate: {
2754 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor3ade5702010-11-04 00:09:33 +00002755
2756 // If we have some kind of dependent-named type (e.g., "typename T::type"),
2757 // break it apart into its prefix and identifier, then reconsititute those
2758 // as the canonical nested-name-specifier. This is required to canonicalize
2759 // a dependent nested-name-specifier involving typedefs of dependent-name
2760 // types, e.g.,
2761 // typedef typename T::type T1;
2762 // typedef typename T1::type T2;
2763 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
2764 NestedNameSpecifier *Prefix
2765 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
2766 return NestedNameSpecifier::Create(*this, Prefix,
2767 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
2768 }
2769
Douglas Gregorcc10cbf2010-11-04 00:14:23 +00002770 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor3ade5702010-11-04 00:09:33 +00002771 if (const DependentTemplateSpecializationType *DTST
2772 = T->getAs<DependentTemplateSpecializationType>()) {
2773 NestedNameSpecifier *Prefix
2774 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
2775 TemplateName Name
2776 = getDependentTemplateName(Prefix, DTST->getIdentifier());
2777 T = getTemplateSpecializationType(Name,
2778 DTST->getArgs(), DTST->getNumArgs());
2779 T = getCanonicalType(T);
2780 }
2781
2782 return NestedNameSpecifier::Create(*this, 0, false, T.getTypePtr());
Douglas Gregor333489b2009-03-27 23:10:48 +00002783 }
2784
2785 case NestedNameSpecifier::Global:
2786 // The global specifier is canonical and unique.
2787 return NNS;
2788 }
2789
2790 // Required to silence a GCC warning
2791 return 0;
2792}
2793
Chris Lattner7adf0762008-08-04 07:31:14 +00002794
2795const ArrayType *ASTContext::getAsArrayType(QualType T) {
2796 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002797 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002798 // Handle the common positive case fast.
2799 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2800 return AT;
2801 }
Mike Stump11289f42009-09-09 15:08:12 +00002802
John McCall8ccfcb52009-09-24 19:53:00 +00002803 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002804 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002805 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002806 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002807
John McCall8ccfcb52009-09-24 19:53:00 +00002808 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002809 // implements C99 6.7.3p8: "If the specification of an array type includes
2810 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002811
Chris Lattner7adf0762008-08-04 07:31:14 +00002812 // If we get here, we either have type qualifiers on the type, or we have
2813 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002814 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002815
John McCall8ccfcb52009-09-24 19:53:00 +00002816 QualifierCollector Qs;
John McCall717d9b02010-12-10 11:01:00 +00002817 const Type *Ty = Qs.strip(T.getDesugaredType(*this));
Mike Stump11289f42009-09-09 15:08:12 +00002818
Chris Lattner7adf0762008-08-04 07:31:14 +00002819 // If we have a simple case, just return now.
2820 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002821 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002822 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002823
Chris Lattner7adf0762008-08-04 07:31:14 +00002824 // Otherwise, we have an array and we have qualifiers on it. Push the
2825 // qualifiers into the array element type and return a new array type.
2826 // Get the canonical version of the element with the extra qualifiers on it.
2827 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002828 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002829
Chris Lattner7adf0762008-08-04 07:31:14 +00002830 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2831 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2832 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002833 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002834 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2835 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2836 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002837 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002838
Mike Stump11289f42009-09-09 15:08:12 +00002839 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002840 = dyn_cast<DependentSizedArrayType>(ATy))
2841 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002842 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002843 DSAT->getSizeExpr(),
Douglas Gregor4619e432008-12-05 23:32:09 +00002844 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002845 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002846 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002847
Chris Lattner7adf0762008-08-04 07:31:14 +00002848 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002849 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002850 VAT->getSizeExpr(),
Chris Lattner7adf0762008-08-04 07:31:14 +00002851 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002852 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002853 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002854}
2855
Chris Lattnera21ad802008-04-02 05:18:44 +00002856/// getArrayDecayedType - Return the properly qualified result of decaying the
2857/// specified array type to a pointer. This operation is non-trivial when
2858/// handling typedefs etc. The canonical type of "T" must be an array type,
2859/// this returns a pointer to a properly qualified element of the array.
2860///
2861/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2862QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002863 // Get the element type with 'getAsArrayType' so that we don't lose any
2864 // typedefs in the element type of the array. This also handles propagation
2865 // of type qualifiers from the array type into the element type if present
2866 // (C99 6.7.3p8).
2867 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2868 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002869
Chris Lattner7adf0762008-08-04 07:31:14 +00002870 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002871
2872 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002873 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002874}
2875
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002876QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002877 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002878 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2879 QT = AT->getElementType();
John McCall717d9b02010-12-10 11:01:00 +00002880 return Qs.apply(*this, QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002881}
2882
Anders Carlsson4bf82142009-09-25 01:23:32 +00002883QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2884 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002885
Anders Carlsson4bf82142009-09-25 01:23:32 +00002886 if (const ArrayType *AT = getAsArrayType(ElemTy))
2887 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002888
Anders Carlssone0808df2008-12-21 03:44:36 +00002889 return ElemTy;
2890}
2891
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002892/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002893uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002894ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2895 uint64_t ElementCount = 1;
2896 do {
2897 ElementCount *= CA->getSize().getZExtValue();
2898 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2899 } while (CA);
2900 return ElementCount;
2901}
2902
Steve Naroff0af91202007-04-27 21:51:21 +00002903/// getFloatingRank - Return a relative rank for floating point types.
2904/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002905static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002906 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002907 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002908
John McCall9dd450b2009-09-21 23:43:11 +00002909 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2910 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002911 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002912 case BuiltinType::Float: return FloatRank;
2913 case BuiltinType::Double: return DoubleRank;
2914 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002915 }
2916}
2917
Mike Stump11289f42009-09-09 15:08:12 +00002918/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2919/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002920/// 'typeDomain' is a real floating point or complex type.
2921/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002922QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2923 QualType Domain) const {
2924 FloatingRank EltRank = getFloatingRank(Size);
2925 if (Domain->isComplexType()) {
2926 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002927 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002928 case FloatRank: return FloatComplexTy;
2929 case DoubleRank: return DoubleComplexTy;
2930 case LongDoubleRank: return LongDoubleComplexTy;
2931 }
Steve Naroff0af91202007-04-27 21:51:21 +00002932 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002933
2934 assert(Domain->isRealFloatingType() && "Unknown domain!");
2935 switch (EltRank) {
2936 default: assert(0 && "getFloatingRank(): illegal value for rank");
2937 case FloatRank: return FloatTy;
2938 case DoubleRank: return DoubleTy;
2939 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002940 }
Steve Naroffe4718892007-04-27 18:30:00 +00002941}
2942
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002943/// getFloatingTypeOrder - Compare the rank of the two specified floating
2944/// point types, ignoring the domain of the type (i.e. 'double' ==
2945/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002946/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002947int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2948 FloatingRank LHSR = getFloatingRank(LHS);
2949 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002950
Chris Lattnerb90739d2008-04-06 23:38:49 +00002951 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002952 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002953 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002954 return 1;
2955 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002956}
2957
Chris Lattner76a00cf2008-04-06 22:59:24 +00002958/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2959/// routine will assert if passed a built-in type that isn't an integer or enum,
2960/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002961unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002962 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002963 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002964 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002965
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002966 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2967 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2968
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002969 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2970 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2971
2972 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2973 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2974
Chris Lattner76a00cf2008-04-06 22:59:24 +00002975 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002976 default: assert(0 && "getIntegerRank(): not a built-in integer");
2977 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002978 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002979 case BuiltinType::Char_S:
2980 case BuiltinType::Char_U:
2981 case BuiltinType::SChar:
2982 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002983 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002984 case BuiltinType::Short:
2985 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002986 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002987 case BuiltinType::Int:
2988 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002989 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002990 case BuiltinType::Long:
2991 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002992 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002993 case BuiltinType::LongLong:
2994 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002995 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002996 case BuiltinType::Int128:
2997 case BuiltinType::UInt128:
2998 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002999 }
3000}
3001
Eli Friedman629ffb92009-08-20 04:21:42 +00003002/// \brief Whether this is a promotable bitfield reference according
3003/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
3004///
3005/// \returns the type this bit-field will promote to, or NULL if no
3006/// promotion occurs.
3007QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00003008 if (E->isTypeDependent() || E->isValueDependent())
3009 return QualType();
3010
Eli Friedman629ffb92009-08-20 04:21:42 +00003011 FieldDecl *Field = E->getBitField();
3012 if (!Field)
3013 return QualType();
3014
3015 QualType FT = Field->getType();
3016
3017 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3018 uint64_t BitWidth = BitWidthAP.getZExtValue();
3019 uint64_t IntSize = getTypeSize(IntTy);
3020 // GCC extension compatibility: if the bit-field size is less than or equal
3021 // to the size of int, it gets promoted no matter what its type is.
3022 // For instance, unsigned long bf : 4 gets promoted to signed int.
3023 if (BitWidth < IntSize)
3024 return IntTy;
3025
3026 if (BitWidth == IntSize)
3027 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3028
3029 // Types bigger than int are not subject to promotions, and therefore act
3030 // like the base type.
3031 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3032 // is ridiculous.
3033 return QualType();
3034}
3035
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003036/// getPromotedIntegerType - Returns the type that Promotable will
3037/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3038/// integer type.
3039QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
3040 assert(!Promotable.isNull());
3041 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00003042 if (const EnumType *ET = Promotable->getAs<EnumType>())
3043 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003044 if (Promotable->isSignedIntegerType())
3045 return IntTy;
3046 uint64_t PromotableSize = getTypeSize(Promotable);
3047 uint64_t IntSize = getTypeSize(IntTy);
3048 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3049 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3050}
3051
Mike Stump11289f42009-09-09 15:08:12 +00003052/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003053/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003054/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003055int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00003056 Type *LHSC = getCanonicalType(LHS).getTypePtr();
3057 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003058 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003059
Chris Lattner76a00cf2008-04-06 22:59:24 +00003060 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3061 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00003062
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003063 unsigned LHSRank = getIntegerRank(LHSC);
3064 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00003065
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003066 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3067 if (LHSRank == RHSRank) return 0;
3068 return LHSRank > RHSRank ? 1 : -1;
3069 }
Mike Stump11289f42009-09-09 15:08:12 +00003070
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003071 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3072 if (LHSUnsigned) {
3073 // If the unsigned [LHS] type is larger, return it.
3074 if (LHSRank >= RHSRank)
3075 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00003076
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003077 // If the signed type can represent all values of the unsigned type, it
3078 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003079 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003080 return -1;
3081 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00003082
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003083 // If the unsigned [RHS] type is larger, return it.
3084 if (RHSRank >= LHSRank)
3085 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00003086
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003087 // If the signed type can represent all values of the unsigned type, it
3088 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003089 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003090 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00003091}
Anders Carlsson98f07902007-08-17 05:31:46 +00003092
Anders Carlsson6d417272009-11-14 21:45:58 +00003093static RecordDecl *
3094CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
3095 SourceLocation L, IdentifierInfo *Id) {
3096 if (Ctx.getLangOptions().CPlusPlus)
3097 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
3098 else
3099 return RecordDecl::Create(Ctx, TK, DC, L, Id);
3100}
3101
Mike Stump11289f42009-09-09 15:08:12 +00003102// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00003103QualType ASTContext::getCFConstantStringType() {
3104 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00003105 CFConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003106 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003107 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00003108 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00003109
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003110 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00003111
Anders Carlsson98f07902007-08-17 05:31:46 +00003112 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00003113 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003114 // int flags;
3115 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00003116 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00003117 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00003118 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00003119 FieldTypes[3] = LongTy;
3120
Anders Carlsson98f07902007-08-17 05:31:46 +00003121 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00003122 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003123 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00003124 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003125 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003126 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003127 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003128 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003129 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003130 }
3131
Douglas Gregord5058122010-02-11 01:19:42 +00003132 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00003133 }
Mike Stump11289f42009-09-09 15:08:12 +00003134
Anders Carlsson98f07902007-08-17 05:31:46 +00003135 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00003136}
Anders Carlsson87c149b2007-10-11 01:00:40 +00003137
Douglas Gregor512b0772009-04-23 22:29:11 +00003138void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003139 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003140 assert(Rec && "Invalid CFConstantStringType");
3141 CFConstantStringTypeDecl = Rec->getDecl();
3142}
3143
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003144// getNSConstantStringType - Return the type used for constant NSStrings.
3145QualType ASTContext::getNSConstantStringType() {
3146 if (!NSConstantStringTypeDecl) {
3147 NSConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003148 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003149 &Idents.get("__builtin_NSString"));
3150 NSConstantStringTypeDecl->startDefinition();
3151
3152 QualType FieldTypes[3];
3153
3154 // const int *isa;
3155 FieldTypes[0] = getPointerType(IntTy.withConst());
3156 // const char *str;
3157 FieldTypes[1] = getPointerType(CharTy.withConst());
3158 // unsigned int length;
3159 FieldTypes[2] = UnsignedIntTy;
3160
3161 // Create fields
3162 for (unsigned i = 0; i < 3; ++i) {
3163 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3164 SourceLocation(), 0,
3165 FieldTypes[i], /*TInfo=*/0,
3166 /*BitWidth=*/0,
3167 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003168 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003169 NSConstantStringTypeDecl->addDecl(Field);
3170 }
3171
3172 NSConstantStringTypeDecl->completeDefinition();
3173 }
3174
3175 return getTagDeclType(NSConstantStringTypeDecl);
3176}
3177
3178void ASTContext::setNSConstantStringType(QualType T) {
3179 const RecordType *Rec = T->getAs<RecordType>();
3180 assert(Rec && "Invalid NSConstantStringType");
3181 NSConstantStringTypeDecl = Rec->getDecl();
3182}
3183
Mike Stump11289f42009-09-09 15:08:12 +00003184QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003185 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003186 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003187 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003188 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003189 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003190
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003191 QualType FieldTypes[] = {
3192 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003193 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003194 getPointerType(UnsignedLongTy),
3195 getConstantArrayType(UnsignedLongTy,
3196 llvm::APInt(32, 5), ArrayType::Normal, 0)
3197 };
Mike Stump11289f42009-09-09 15:08:12 +00003198
Douglas Gregor91f84212008-12-11 16:49:14 +00003199 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003200 FieldDecl *Field = FieldDecl::Create(*this,
3201 ObjCFastEnumerationStateTypeDecl,
3202 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003203 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003204 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003205 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003206 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003207 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003208 }
Mike Stump11289f42009-09-09 15:08:12 +00003209
Douglas Gregord5058122010-02-11 01:19:42 +00003210 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003211 }
Mike Stump11289f42009-09-09 15:08:12 +00003212
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003213 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3214}
3215
Mike Stumpd0153282009-10-20 02:12:22 +00003216QualType ASTContext::getBlockDescriptorType() {
3217 if (BlockDescriptorType)
3218 return getTagDeclType(BlockDescriptorType);
3219
3220 RecordDecl *T;
3221 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003222 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003223 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003224 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003225
3226 QualType FieldTypes[] = {
3227 UnsignedLongTy,
3228 UnsignedLongTy,
3229 };
3230
3231 const char *FieldNames[] = {
3232 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003233 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003234 };
3235
3236 for (size_t i = 0; i < 2; ++i) {
3237 FieldDecl *Field = FieldDecl::Create(*this,
3238 T,
3239 SourceLocation(),
3240 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003241 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003242 /*BitWidth=*/0,
3243 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003244 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003245 T->addDecl(Field);
3246 }
3247
Douglas Gregord5058122010-02-11 01:19:42 +00003248 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003249
3250 BlockDescriptorType = T;
3251
3252 return getTagDeclType(BlockDescriptorType);
3253}
3254
3255void ASTContext::setBlockDescriptorType(QualType T) {
3256 const RecordType *Rec = T->getAs<RecordType>();
3257 assert(Rec && "Invalid BlockDescriptorType");
3258 BlockDescriptorType = Rec->getDecl();
3259}
3260
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003261QualType ASTContext::getBlockDescriptorExtendedType() {
3262 if (BlockDescriptorExtendedType)
3263 return getTagDeclType(BlockDescriptorExtendedType);
3264
3265 RecordDecl *T;
3266 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003267 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003268 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003269 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003270
3271 QualType FieldTypes[] = {
3272 UnsignedLongTy,
3273 UnsignedLongTy,
3274 getPointerType(VoidPtrTy),
3275 getPointerType(VoidPtrTy)
3276 };
3277
3278 const char *FieldNames[] = {
3279 "reserved",
3280 "Size",
3281 "CopyFuncPtr",
3282 "DestroyFuncPtr"
3283 };
3284
3285 for (size_t i = 0; i < 4; ++i) {
3286 FieldDecl *Field = FieldDecl::Create(*this,
3287 T,
3288 SourceLocation(),
3289 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003290 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003291 /*BitWidth=*/0,
3292 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003293 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003294 T->addDecl(Field);
3295 }
3296
Douglas Gregord5058122010-02-11 01:19:42 +00003297 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003298
3299 BlockDescriptorExtendedType = T;
3300
3301 return getTagDeclType(BlockDescriptorExtendedType);
3302}
3303
3304void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3305 const RecordType *Rec = T->getAs<RecordType>();
3306 assert(Rec && "Invalid BlockDescriptorType");
3307 BlockDescriptorExtendedType = Rec->getDecl();
3308}
3309
Mike Stump94967902009-10-21 18:16:27 +00003310bool ASTContext::BlockRequiresCopying(QualType Ty) {
3311 if (Ty->isBlockPointerType())
3312 return true;
3313 if (isObjCNSObjectType(Ty))
3314 return true;
3315 if (Ty->isObjCObjectPointerType())
3316 return true;
Fariborz Jahaniana00076c2010-11-17 00:21:28 +00003317 if (getLangOptions().CPlusPlus) {
3318 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3319 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3320 return RD->hasConstCopyConstructor(*this);
3321
3322 }
3323 }
Mike Stump94967902009-10-21 18:16:27 +00003324 return false;
3325}
3326
Daniel Dunbar56df9772010-08-17 22:39:59 +00003327QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) {
Mike Stump94967902009-10-21 18:16:27 +00003328 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003329 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003330 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003331 // unsigned int __flags;
3332 // unsigned int __size;
Eli Friedman04831922010-08-22 01:00:03 +00003333 // void *__copy_helper; // as needed
3334 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003335 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003336 // } *
3337
Mike Stump94967902009-10-21 18:16:27 +00003338 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3339
3340 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003341 llvm::SmallString<36> Name;
3342 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3343 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003344 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003345 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003346 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003347 T->startDefinition();
3348 QualType Int32Ty = IntTy;
3349 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3350 QualType FieldTypes[] = {
3351 getPointerType(VoidPtrTy),
3352 getPointerType(getTagDeclType(T)),
3353 Int32Ty,
3354 Int32Ty,
3355 getPointerType(VoidPtrTy),
3356 getPointerType(VoidPtrTy),
3357 Ty
3358 };
3359
Daniel Dunbar56df9772010-08-17 22:39:59 +00003360 llvm::StringRef FieldNames[] = {
Mike Stump94967902009-10-21 18:16:27 +00003361 "__isa",
3362 "__forwarding",
3363 "__flags",
3364 "__size",
3365 "__copy_helper",
3366 "__destroy_helper",
3367 DeclName,
3368 };
3369
3370 for (size_t i = 0; i < 7; ++i) {
3371 if (!HasCopyAndDispose && i >=4 && i <= 5)
3372 continue;
3373 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3374 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003375 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003376 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003377 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003378 T->addDecl(Field);
3379 }
3380
Douglas Gregord5058122010-02-11 01:19:42 +00003381 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003382
3383 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003384}
3385
3386
3387QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003388 bool BlockHasCopyDispose,
John McCall87fe5d52010-05-20 01:18:31 +00003389 llvm::SmallVectorImpl<const Expr *> &Layout) {
3390
Mike Stumpd0153282009-10-20 02:12:22 +00003391 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003392 llvm::SmallString<36> Name;
3393 llvm::raw_svector_ostream(Name) << "__block_literal_"
3394 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003395 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003396 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003397 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003398 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003399 QualType FieldTypes[] = {
3400 getPointerType(VoidPtrTy),
3401 IntTy,
3402 IntTy,
3403 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003404 (BlockHasCopyDispose ?
3405 getPointerType(getBlockDescriptorExtendedType()) :
3406 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003407 };
3408
3409 const char *FieldNames[] = {
3410 "__isa",
3411 "__flags",
3412 "__reserved",
3413 "__FuncPtr",
3414 "__descriptor"
3415 };
3416
3417 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003418 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003419 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003420 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003421 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003422 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003423 T->addDecl(Field);
3424 }
3425
John McCall87fe5d52010-05-20 01:18:31 +00003426 for (unsigned i = 0; i < Layout.size(); ++i) {
3427 const Expr *E = Layout[i];
Mike Stump7fe9cc12009-10-21 03:49:08 +00003428
John McCall87fe5d52010-05-20 01:18:31 +00003429 QualType FieldType = E->getType();
3430 IdentifierInfo *FieldName = 0;
3431 if (isa<CXXThisExpr>(E)) {
3432 FieldName = &Idents.get("this");
3433 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3434 const ValueDecl *D = BDRE->getDecl();
3435 FieldName = D->getIdentifier();
3436 if (BDRE->isByRef())
Daniel Dunbar56df9772010-08-17 22:39:59 +00003437 FieldType = BuildByRefType(D->getName(), FieldType);
John McCall87fe5d52010-05-20 01:18:31 +00003438 } else {
3439 // Padding.
3440 assert(isa<ConstantArrayType>(FieldType) &&
3441 isa<DeclRefExpr>(E) &&
3442 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3443 "doesn't match characteristics of padding decl");
3444 }
Mike Stump7fe9cc12009-10-21 03:49:08 +00003445
3446 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCall87fe5d52010-05-20 01:18:31 +00003447 FieldName, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003448 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003449 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003450 T->addDecl(Field);
3451 }
3452
Douglas Gregord5058122010-02-11 01:19:42 +00003453 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003454
3455 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003456}
3457
Douglas Gregor512b0772009-04-23 22:29:11 +00003458void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003459 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003460 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3461 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3462}
3463
Anders Carlsson18acd442007-10-29 06:33:42 +00003464// This returns true if a type has been typedefed to BOOL:
3465// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003466static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003467 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003468 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3469 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003470
Anders Carlssond8499822007-10-29 05:01:08 +00003471 return false;
3472}
3473
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003474/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003475/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003476CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003477 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003478
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003479 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003480 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003481 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003482 // Treat arrays as pointers, since that's how they're passed in.
3483 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003484 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003485 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003486}
3487
3488static inline
3489std::string charUnitsToString(const CharUnits &CU) {
3490 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003491}
3492
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003493/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003494/// declaration.
3495void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3496 std::string& S) {
3497 const BlockDecl *Decl = Expr->getBlockDecl();
3498 QualType BlockTy =
3499 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3500 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003501 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003502 // Compute size of all parameters.
3503 // Start with computing size of a pointer in number of bytes.
3504 // FIXME: There might(should) be a better way of doing this computation!
3505 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003506 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3507 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003508 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003509 E = Decl->param_end(); PI != E; ++PI) {
3510 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003511 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003512 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003513 ParmOffset += sz;
3514 }
3515 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003516 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003517 // Block pointer and offset.
3518 S += "@?0";
3519 ParmOffset = PtrSize;
3520
3521 // Argument types.
3522 ParmOffset = PtrSize;
3523 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3524 Decl->param_end(); PI != E; ++PI) {
3525 ParmVarDecl *PVDecl = *PI;
3526 QualType PType = PVDecl->getOriginalType();
3527 if (const ArrayType *AT =
3528 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3529 // Use array's original type only if it has known number of
3530 // elements.
3531 if (!isa<ConstantArrayType>(AT))
3532 PType = PVDecl->getType();
3533 } else if (PType->isFunctionType())
3534 PType = PVDecl->getType();
3535 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003536 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003537 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003538 }
3539}
3540
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003541/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003542/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003543void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003544 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003545 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003546 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003547 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003548 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003549 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003550 // Compute size of all parameters.
3551 // Start with computing size of a pointer in number of bytes.
3552 // FIXME: There might(should) be a better way of doing this computation!
3553 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003554 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003555 // The first two arguments (self and _cmd) are pointers; account for
3556 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003557 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003558 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003559 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003560 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003561 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003562 assert (sz.isPositive() &&
3563 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003564 ParmOffset += sz;
3565 }
Ken Dyck40775002010-01-11 17:06:35 +00003566 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003567 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003568 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003569
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003570 // Argument types.
3571 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003572 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003573 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003574 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003575 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003576 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003577 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3578 // Use array's original type only if it has known number of
3579 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003580 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003581 PType = PVDecl->getType();
3582 } else if (PType->isFunctionType())
3583 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003584 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003585 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003586 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003587 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003588 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003589 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003590 }
3591}
3592
Daniel Dunbar4932b362008-08-28 04:38:10 +00003593/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003594/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003595/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3596/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003597/// Property attributes are stored as a comma-delimited C string. The simple
3598/// attributes readonly and bycopy are encoded as single characters. The
3599/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3600/// encoded as single characters, followed by an identifier. Property types
3601/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003602/// these attributes are defined by the following enumeration:
3603/// @code
3604/// enum PropertyAttributes {
3605/// kPropertyReadOnly = 'R', // property is read-only.
3606/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3607/// kPropertyByref = '&', // property is a reference to the value last assigned
3608/// kPropertyDynamic = 'D', // property is dynamic
3609/// kPropertyGetter = 'G', // followed by getter selector name
3610/// kPropertySetter = 'S', // followed by setter selector name
3611/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3612/// kPropertyType = 't' // followed by old-style type encoding.
3613/// kPropertyWeak = 'W' // 'weak' property
3614/// kPropertyStrong = 'P' // property GC'able
3615/// kPropertyNonAtomic = 'N' // property non-atomic
3616/// };
3617/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003618void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003619 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003620 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003621 // Collect information from the property implementation decl(s).
3622 bool Dynamic = false;
3623 ObjCPropertyImplDecl *SynthesizePID = 0;
3624
3625 // FIXME: Duplicated code due to poor abstraction.
3626 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003627 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003628 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3629 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003630 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003631 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003632 ObjCPropertyImplDecl *PID = *i;
3633 if (PID->getPropertyDecl() == PD) {
3634 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3635 Dynamic = true;
3636 } else {
3637 SynthesizePID = PID;
3638 }
3639 }
3640 }
3641 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003642 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003643 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003644 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003645 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003646 ObjCPropertyImplDecl *PID = *i;
3647 if (PID->getPropertyDecl() == PD) {
3648 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3649 Dynamic = true;
3650 } else {
3651 SynthesizePID = PID;
3652 }
3653 }
Mike Stump11289f42009-09-09 15:08:12 +00003654 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003655 }
3656 }
3657
3658 // FIXME: This is not very efficient.
3659 S = "T";
3660
3661 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003662 // GCC has some special rules regarding encoding of properties which
3663 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003664 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003665 true /* outermost type */,
3666 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003667
3668 if (PD->isReadOnly()) {
3669 S += ",R";
3670 } else {
3671 switch (PD->getSetterKind()) {
3672 case ObjCPropertyDecl::Assign: break;
3673 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003674 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003675 }
3676 }
3677
3678 // It really isn't clear at all what this means, since properties
3679 // are "dynamic by default".
3680 if (Dynamic)
3681 S += ",D";
3682
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003683 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3684 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003685
Daniel Dunbar4932b362008-08-28 04:38:10 +00003686 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3687 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003688 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003689 }
3690
3691 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3692 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003693 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003694 }
3695
3696 if (SynthesizePID) {
3697 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3698 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003699 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003700 }
3701
3702 // FIXME: OBJCGC: weak & strong
3703}
3704
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003705/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003706/// Another legacy compatibility encoding: 32-bit longs are encoded as
3707/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003708/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3709///
3710void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003711 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003712 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003713 if (BT->getKind() == BuiltinType::ULong &&
3714 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003715 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003716 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003717 if (BT->getKind() == BuiltinType::Long &&
3718 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003719 PointeeTy = IntTy;
3720 }
3721 }
3722}
3723
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003724void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003725 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003726 // We follow the behavior of gcc, expanding structures which are
3727 // directly pointed to, and expanding embedded structures. Note that
3728 // these rules are sufficient to prevent recursive encoding of the
3729 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003730 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003731 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003732}
3733
David Chisnallb190a2c2010-06-04 01:10:52 +00003734static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3735 switch (T->getAs<BuiltinType>()->getKind()) {
3736 default: assert(0 && "Unhandled builtin type kind");
3737 case BuiltinType::Void: return 'v';
3738 case BuiltinType::Bool: return 'B';
3739 case BuiltinType::Char_U:
3740 case BuiltinType::UChar: return 'C';
3741 case BuiltinType::UShort: return 'S';
3742 case BuiltinType::UInt: return 'I';
3743 case BuiltinType::ULong:
3744 return
3745 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3746 case BuiltinType::UInt128: return 'T';
3747 case BuiltinType::ULongLong: return 'Q';
3748 case BuiltinType::Char_S:
3749 case BuiltinType::SChar: return 'c';
3750 case BuiltinType::Short: return 's';
John McCalldad856d2010-06-11 10:11:05 +00003751 case BuiltinType::WChar:
David Chisnallb190a2c2010-06-04 01:10:52 +00003752 case BuiltinType::Int: return 'i';
3753 case BuiltinType::Long:
3754 return
3755 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3756 case BuiltinType::LongLong: return 'q';
3757 case BuiltinType::Int128: return 't';
3758 case BuiltinType::Float: return 'f';
3759 case BuiltinType::Double: return 'd';
Daniel Dunbar7cba5a72010-10-11 21:13:48 +00003760 case BuiltinType::LongDouble: return 'D';
David Chisnallb190a2c2010-06-04 01:10:52 +00003761 }
3762}
3763
Mike Stump11289f42009-09-09 15:08:12 +00003764static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00003765 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003766 const Expr *E = FD->getBitWidth();
3767 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3768 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003769 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00003770 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3771 // The GNU runtime requires more information; bitfields are encoded as b,
3772 // then the offset (in bits) of the first element, then the type of the
3773 // bitfield, then the size in bits. For example, in this structure:
3774 //
3775 // struct
3776 // {
3777 // int integer;
3778 // int flags:2;
3779 // };
3780 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3781 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3782 // information is not especially sensible, but we're stuck with it for
3783 // compatibility with GCC, although providing it breaks anything that
3784 // actually uses runtime introspection and wants to work on both runtimes...
3785 if (!Ctx->getLangOptions().NeXTRuntime) {
3786 const RecordDecl *RD = FD->getParent();
3787 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3788 // FIXME: This same linear search is also used in ExprConstant - it might
3789 // be better if the FieldDecl stored its offset. We'd be increasing the
3790 // size of the object slightly, but saving some time every time it is used.
3791 unsigned i = 0;
3792 for (RecordDecl::field_iterator Field = RD->field_begin(),
3793 FieldEnd = RD->field_end();
3794 Field != FieldEnd; (void)++Field, ++i) {
3795 if (*Field == FD)
3796 break;
3797 }
3798 S += llvm::utostr(RL.getFieldOffset(i));
3799 S += ObjCEncodingForPrimitiveKind(Context, T);
3800 }
3801 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003802 S += llvm::utostr(N);
3803}
3804
Daniel Dunbar07d07852009-10-18 21:17:35 +00003805// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003806void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3807 bool ExpandPointedToStructures,
3808 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003809 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003810 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003811 bool EncodingProperty) {
David Chisnallb190a2c2010-06-04 01:10:52 +00003812 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003813 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003814 return EncodeBitField(this, S, T, FD);
3815 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003816 return;
3817 }
Mike Stump11289f42009-09-09 15:08:12 +00003818
John McCall9dd450b2009-09-21 23:43:11 +00003819 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003820 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003821 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003822 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003823 return;
3824 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003825
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003826 // encoding for pointer or r3eference types.
3827 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003828 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003829 if (PT->isObjCSelType()) {
3830 S += ':';
3831 return;
3832 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003833 PointeeTy = PT->getPointeeType();
3834 }
3835 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3836 PointeeTy = RT->getPointeeType();
3837 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003838 bool isReadOnly = false;
3839 // For historical/compatibility reasons, the read-only qualifier of the
3840 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3841 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003842 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003843 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003844 if (OutermostType && T.isConstQualified()) {
3845 isReadOnly = true;
3846 S += 'r';
3847 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003848 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003849 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003850 while (P->getAs<PointerType>())
3851 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003852 if (P.isConstQualified()) {
3853 isReadOnly = true;
3854 S += 'r';
3855 }
3856 }
3857 if (isReadOnly) {
3858 // Another legacy compatibility encoding. Some ObjC qualifier and type
3859 // combinations need to be rearranged.
3860 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003861 if (llvm::StringRef(S).endswith("nr"))
3862 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003863 }
Mike Stump11289f42009-09-09 15:08:12 +00003864
Anders Carlssond8499822007-10-29 05:01:08 +00003865 if (PointeeTy->isCharType()) {
3866 // char pointer types should be encoded as '*' unless it is a
3867 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003868 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003869 S += '*';
3870 return;
3871 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003872 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003873 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3874 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3875 S += '#';
3876 return;
3877 }
3878 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3879 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3880 S += '@';
3881 return;
3882 }
3883 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003884 }
Anders Carlssond8499822007-10-29 05:01:08 +00003885 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003886 getLegacyIntegralTypeEncoding(PointeeTy);
3887
Mike Stump11289f42009-09-09 15:08:12 +00003888 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003889 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003890 return;
3891 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003892
Chris Lattnere7cabb92009-07-13 00:10:46 +00003893 if (const ArrayType *AT =
3894 // Ignore type qualifiers etc.
3895 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003896 if (isa<IncompleteArrayType>(AT)) {
3897 // Incomplete arrays are encoded as a pointer to the array element.
3898 S += '^';
3899
Mike Stump11289f42009-09-09 15:08:12 +00003900 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003901 false, ExpandStructures, FD);
3902 } else {
3903 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003904
Anders Carlssond05f44b2009-02-22 01:38:57 +00003905 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3906 S += llvm::utostr(CAT->getSize().getZExtValue());
3907 else {
3908 //Variable length arrays are encoded as a regular array with 0 elements.
3909 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3910 S += '0';
3911 }
Mike Stump11289f42009-09-09 15:08:12 +00003912
3913 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003914 false, ExpandStructures, FD);
3915 S += ']';
3916 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003917 return;
3918 }
Mike Stump11289f42009-09-09 15:08:12 +00003919
John McCall9dd450b2009-09-21 23:43:11 +00003920 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003921 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003922 return;
3923 }
Mike Stump11289f42009-09-09 15:08:12 +00003924
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003925 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003926 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003927 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003928 // Anonymous structures print as '?'
3929 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3930 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003931 if (ClassTemplateSpecializationDecl *Spec
3932 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3933 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3934 std::string TemplateArgsStr
3935 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003936 TemplateArgs.data(),
3937 TemplateArgs.size(),
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003938 (*this).PrintingPolicy);
3939
3940 S += TemplateArgsStr;
3941 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003942 } else {
3943 S += '?';
3944 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003945 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003946 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003947 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3948 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003949 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003950 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003951 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003952 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003953 S += '"';
3954 }
Mike Stump11289f42009-09-09 15:08:12 +00003955
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003956 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003957 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003958 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003959 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003960 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003961 QualType qt = Field->getType();
3962 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003963 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003964 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003965 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003966 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003967 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003968 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003969 return;
3970 }
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00003971
Chris Lattnere7cabb92009-07-13 00:10:46 +00003972 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003973 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003974 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003975 else
3976 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003977 return;
3978 }
Mike Stump11289f42009-09-09 15:08:12 +00003979
Chris Lattnere7cabb92009-07-13 00:10:46 +00003980 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003981 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003982 return;
3983 }
Mike Stump11289f42009-09-09 15:08:12 +00003984
John McCall8b07ec22010-05-15 11:32:37 +00003985 // Ignore protocol qualifiers when mangling at this level.
3986 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3987 T = OT->getBaseType();
3988
John McCall8ccfcb52009-09-24 19:53:00 +00003989 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003990 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003991 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003992 S += '{';
3993 const IdentifierInfo *II = OI->getIdentifier();
3994 S += II->getName();
3995 S += '=';
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003996 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
3997 DeepCollectObjCIvars(OI, true, Ivars);
3998 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
3999 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4000 if (Field->isBitField())
4001 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004002 else
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004003 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004004 }
4005 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004006 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004007 }
Mike Stump11289f42009-09-09 15:08:12 +00004008
John McCall9dd450b2009-09-21 23:43:11 +00004009 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004010 if (OPT->isObjCIdType()) {
4011 S += '@';
4012 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004013 }
Mike Stump11289f42009-09-09 15:08:12 +00004014
Steve Narofff0c86112009-10-28 22:03:49 +00004015 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4016 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4017 // Since this is a binary compatibility issue, need to consult with runtime
4018 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004019 S += '#';
4020 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004021 }
Mike Stump11289f42009-09-09 15:08:12 +00004022
Chris Lattnere7cabb92009-07-13 00:10:46 +00004023 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00004024 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00004025 ExpandPointedToStructures,
4026 ExpandStructures, FD);
4027 if (FD || EncodingProperty) {
4028 // Note that we do extended encoding of protocol qualifer list
4029 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004030 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00004031 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4032 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004033 S += '<';
4034 S += (*I)->getNameAsString();
4035 S += '>';
4036 }
4037 S += '"';
4038 }
4039 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004040 }
Mike Stump11289f42009-09-09 15:08:12 +00004041
Chris Lattnere7cabb92009-07-13 00:10:46 +00004042 QualType PointeeTy = OPT->getPointeeType();
4043 if (!EncodingProperty &&
4044 isa<TypedefType>(PointeeTy.getTypePtr())) {
4045 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00004046 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00004047 // {...};
4048 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00004049 getObjCEncodingForTypeImpl(PointeeTy, S,
4050 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00004051 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00004052 return;
4053 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004054
4055 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00004056 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004057 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00004058 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00004059 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4060 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004061 S += '<';
4062 S += (*I)->getNameAsString();
4063 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00004064 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004065 S += '"';
4066 }
4067 return;
4068 }
Mike Stump11289f42009-09-09 15:08:12 +00004069
John McCalla9e6e8d2010-05-17 23:56:34 +00004070 // gcc just blithely ignores member pointers.
4071 // TODO: maybe there should be a mangling for these
4072 if (T->getAs<MemberPointerType>())
4073 return;
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004074
4075 if (T->isVectorType()) {
4076 // This matches gcc's encoding, even though technically it is
4077 // insufficient.
4078 // FIXME. We should do a better job than gcc.
4079 return;
4080 }
4081
Chris Lattnere7cabb92009-07-13 00:10:46 +00004082 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00004083}
4084
Mike Stump11289f42009-09-09 15:08:12 +00004085void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00004086 std::string& S) const {
4087 if (QT & Decl::OBJC_TQ_In)
4088 S += 'n';
4089 if (QT & Decl::OBJC_TQ_Inout)
4090 S += 'N';
4091 if (QT & Decl::OBJC_TQ_Out)
4092 S += 'o';
4093 if (QT & Decl::OBJC_TQ_Bycopy)
4094 S += 'O';
4095 if (QT & Decl::OBJC_TQ_Byref)
4096 S += 'R';
4097 if (QT & Decl::OBJC_TQ_Oneway)
4098 S += 'V';
4099}
4100
Chris Lattnere7cabb92009-07-13 00:10:46 +00004101void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00004102 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004103
Anders Carlsson87c149b2007-10-11 01:00:40 +00004104 BuiltinVaListType = T;
4105}
4106
Chris Lattnere7cabb92009-07-13 00:10:46 +00004107void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004108 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00004109}
4110
Chris Lattnere7cabb92009-07-13 00:10:46 +00004111void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00004112 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00004113}
4114
Chris Lattnere7cabb92009-07-13 00:10:46 +00004115void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004116 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004117}
4118
Chris Lattnere7cabb92009-07-13 00:10:46 +00004119void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004120 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00004121}
4122
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004123void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00004124 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00004125 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004126
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004127 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00004128}
4129
John McCalld28ae272009-12-02 08:04:21 +00004130/// \brief Retrieve the template name that corresponds to a non-empty
4131/// lookup.
John McCallad371252010-01-20 00:46:10 +00004132TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4133 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00004134 unsigned size = End - Begin;
4135 assert(size > 1 && "set is not overloaded!");
4136
4137 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4138 size * sizeof(FunctionTemplateDecl*));
4139 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4140
4141 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00004142 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00004143 NamedDecl *D = *I;
4144 assert(isa<FunctionTemplateDecl>(D) ||
4145 (isa<UsingShadowDecl>(D) &&
4146 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4147 *Storage++ = D;
4148 }
4149
4150 return TemplateName(OT);
4151}
4152
Douglas Gregordc572a32009-03-30 22:58:21 +00004153/// \brief Retrieve the template name that represents a qualified
4154/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00004155TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00004156 bool TemplateKeyword,
4157 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00004158 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00004159 llvm::FoldingSetNodeID ID;
4160 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4161
4162 void *InsertPos = 0;
4163 QualifiedTemplateName *QTN =
4164 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4165 if (!QTN) {
4166 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4167 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4168 }
4169
4170 return TemplateName(QTN);
4171}
4172
4173/// \brief Retrieve the template name that represents a dependent
4174/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00004175TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00004176 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004177 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00004178 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00004179
4180 llvm::FoldingSetNodeID ID;
4181 DependentTemplateName::Profile(ID, NNS, Name);
4182
4183 void *InsertPos = 0;
4184 DependentTemplateName *QTN =
4185 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4186
4187 if (QTN)
4188 return TemplateName(QTN);
4189
4190 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4191 if (CanonNNS == NNS) {
4192 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4193 } else {
4194 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4195 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004196 DependentTemplateName *CheckQTN =
4197 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4198 assert(!CheckQTN && "Dependent type name canonicalization broken");
4199 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004200 }
4201
4202 DependentTemplateNames.InsertNode(QTN, InsertPos);
4203 return TemplateName(QTN);
4204}
4205
Douglas Gregor71395fa2009-11-04 00:56:37 +00004206/// \brief Retrieve the template name that represents a dependent
4207/// template name such as \c MetaFun::template operator+.
4208TemplateName
4209ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4210 OverloadedOperatorKind Operator) {
4211 assert((!NNS || NNS->isDependent()) &&
4212 "Nested name specifier must be dependent");
4213
4214 llvm::FoldingSetNodeID ID;
4215 DependentTemplateName::Profile(ID, NNS, Operator);
4216
4217 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004218 DependentTemplateName *QTN
4219 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004220
4221 if (QTN)
4222 return TemplateName(QTN);
4223
4224 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4225 if (CanonNNS == NNS) {
4226 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4227 } else {
4228 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4229 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004230
4231 DependentTemplateName *CheckQTN
4232 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4233 assert(!CheckQTN && "Dependent template name canonicalization broken");
4234 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004235 }
4236
4237 DependentTemplateNames.InsertNode(QTN, InsertPos);
4238 return TemplateName(QTN);
4239}
4240
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004241/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004242/// TargetInfo, produce the corresponding type. The unsigned @p Type
4243/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004244CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004245 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004246 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004247 case TargetInfo::SignedShort: return ShortTy;
4248 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4249 case TargetInfo::SignedInt: return IntTy;
4250 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4251 case TargetInfo::SignedLong: return LongTy;
4252 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4253 case TargetInfo::SignedLongLong: return LongLongTy;
4254 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4255 }
4256
4257 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004258 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004259}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004260
4261//===----------------------------------------------------------------------===//
4262// Type Predicates.
4263//===----------------------------------------------------------------------===//
4264
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004265/// isObjCNSObjectType - Return true if this is an NSObject object using
4266/// NSObject attribute on a c-style pointer type.
4267/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004268/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004269///
4270bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4271 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4272 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004273 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004274 return true;
4275 }
Mike Stump11289f42009-09-09 15:08:12 +00004276 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004277}
4278
Fariborz Jahanian96207692009-02-18 21:49:28 +00004279/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4280/// garbage collection attribute.
4281///
John McCall8ccfcb52009-09-24 19:53:00 +00004282Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4283 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004284 if (getLangOptions().ObjC1 &&
4285 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00004286 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00004287 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00004288 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004289 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00004290 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00004291 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004292 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004293 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004294 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004295 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00004296 // Non-pointers have none gc'able attribute regardless of the attribute
4297 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00004298 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004299 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004300 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004301 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004302}
4303
Chris Lattner49af6a42008-04-07 06:51:04 +00004304//===----------------------------------------------------------------------===//
4305// Type Compatibility Testing
4306//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004307
Mike Stump11289f42009-09-09 15:08:12 +00004308/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004309/// compatible.
4310static bool areCompatVectorTypes(const VectorType *LHS,
4311 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004312 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004313 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004314 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004315}
4316
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004317bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4318 QualType SecondVec) {
4319 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4320 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4321
4322 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4323 return true;
4324
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004325 // Treat Neon vector types and most AltiVec vector types as if they are the
4326 // equivalent GCC vector types.
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004327 const VectorType *First = FirstVec->getAs<VectorType>();
4328 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004329 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004330 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004331 First->getVectorKind() != VectorType::AltiVecPixel &&
4332 First->getVectorKind() != VectorType::AltiVecBool &&
4333 Second->getVectorKind() != VectorType::AltiVecPixel &&
4334 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004335 return true;
4336
4337 return false;
4338}
4339
Steve Naroff8e6aee52009-07-23 01:01:38 +00004340//===----------------------------------------------------------------------===//
4341// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4342//===----------------------------------------------------------------------===//
4343
4344/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4345/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004346bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4347 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004348 if (lProto == rProto)
4349 return true;
4350 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4351 E = rProto->protocol_end(); PI != E; ++PI)
4352 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4353 return true;
4354 return false;
4355}
4356
Steve Naroff8e6aee52009-07-23 01:01:38 +00004357/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4358/// return true if lhs's protocols conform to rhs's protocol; false
4359/// otherwise.
4360bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4361 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4362 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4363 return false;
4364}
4365
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004366/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4367/// Class<p1, ...>.
4368bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4369 QualType rhs) {
4370 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4371 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4372 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4373
4374 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4375 E = lhsQID->qual_end(); I != E; ++I) {
4376 bool match = false;
4377 ObjCProtocolDecl *lhsProto = *I;
4378 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4379 E = rhsOPT->qual_end(); J != E; ++J) {
4380 ObjCProtocolDecl *rhsProto = *J;
4381 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4382 match = true;
4383 break;
4384 }
4385 }
4386 if (!match)
4387 return false;
4388 }
4389 return true;
4390}
4391
Steve Naroff8e6aee52009-07-23 01:01:38 +00004392/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4393/// ObjCQualifiedIDType.
4394bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4395 bool compare) {
4396 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004397 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004398 lhs->isObjCIdType() || lhs->isObjCClassType())
4399 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004400 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004401 rhs->isObjCIdType() || rhs->isObjCClassType())
4402 return true;
4403
4404 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004405 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004406
Steve Naroff8e6aee52009-07-23 01:01:38 +00004407 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004408
Steve Naroff8e6aee52009-07-23 01:01:38 +00004409 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004410 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004411 // make sure we check the class hierarchy.
4412 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4413 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4414 E = lhsQID->qual_end(); I != E; ++I) {
4415 // when comparing an id<P> on lhs with a static type on rhs,
4416 // see if static class implements all of id's protocols, directly or
4417 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004418 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004419 return false;
4420 }
4421 }
4422 // If there are no qualifiers and no interface, we have an 'id'.
4423 return true;
4424 }
Mike Stump11289f42009-09-09 15:08:12 +00004425 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004426 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4427 E = lhsQID->qual_end(); I != E; ++I) {
4428 ObjCProtocolDecl *lhsProto = *I;
4429 bool match = false;
4430
4431 // when comparing an id<P> on lhs with a static type on rhs,
4432 // see if static class implements all of id's protocols, directly or
4433 // through its super class and categories.
4434 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4435 E = rhsOPT->qual_end(); J != E; ++J) {
4436 ObjCProtocolDecl *rhsProto = *J;
4437 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4438 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4439 match = true;
4440 break;
4441 }
4442 }
Mike Stump11289f42009-09-09 15:08:12 +00004443 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004444 // make sure we check the class hierarchy.
4445 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4446 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4447 E = lhsQID->qual_end(); I != E; ++I) {
4448 // when comparing an id<P> on lhs with a static type on rhs,
4449 // see if static class implements all of id's protocols, directly or
4450 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004451 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004452 match = true;
4453 break;
4454 }
4455 }
4456 }
4457 if (!match)
4458 return false;
4459 }
Mike Stump11289f42009-09-09 15:08:12 +00004460
Steve Naroff8e6aee52009-07-23 01:01:38 +00004461 return true;
4462 }
Mike Stump11289f42009-09-09 15:08:12 +00004463
Steve Naroff8e6aee52009-07-23 01:01:38 +00004464 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4465 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4466
Mike Stump11289f42009-09-09 15:08:12 +00004467 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004468 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004469 // If both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004470 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4471 E = lhsOPT->qual_end(); I != E; ++I) {
4472 ObjCProtocolDecl *lhsProto = *I;
4473 bool match = false;
4474
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004475 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff8e6aee52009-07-23 01:01:38 +00004476 // see if static class implements all of id's protocols, directly or
4477 // through its super class and categories.
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004478 // First, lhs protocols in the qualifier list must be found, direct
4479 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004480 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4481 E = rhsQID->qual_end(); J != E; ++J) {
4482 ObjCProtocolDecl *rhsProto = *J;
4483 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4484 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4485 match = true;
4486 break;
4487 }
4488 }
4489 if (!match)
4490 return false;
4491 }
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004492
4493 // Static class's protocols, or its super class or category protocols
4494 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4495 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4496 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4497 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4498 // This is rather dubious but matches gcc's behavior. If lhs has
4499 // no type qualifier and its class has no static protocol(s)
4500 // assume that it is mismatch.
4501 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4502 return false;
4503 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4504 LHSInheritedProtocols.begin(),
4505 E = LHSInheritedProtocols.end(); I != E; ++I) {
4506 bool match = false;
4507 ObjCProtocolDecl *lhsProto = (*I);
4508 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4509 E = rhsQID->qual_end(); J != E; ++J) {
4510 ObjCProtocolDecl *rhsProto = *J;
4511 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4512 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4513 match = true;
4514 break;
4515 }
4516 }
4517 if (!match)
4518 return false;
4519 }
4520 }
Steve Naroff8e6aee52009-07-23 01:01:38 +00004521 return true;
4522 }
4523 return false;
4524}
4525
Eli Friedman47f77112008-08-22 00:56:42 +00004526/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004527/// compatible for assignment from RHS to LHS. This handles validation of any
4528/// protocol qualifiers on the LHS or RHS.
4529///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004530bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4531 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004532 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4533 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4534
Steve Naroff1329fa02009-07-15 18:40:39 +00004535 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004536 if (LHS->isObjCUnqualifiedIdOrClass() ||
4537 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004538 return true;
4539
John McCall8b07ec22010-05-15 11:32:37 +00004540 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004541 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4542 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004543 false);
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004544
4545 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4546 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4547 QualType(RHSOPT,0));
4548
John McCall8b07ec22010-05-15 11:32:37 +00004549 // If we have 2 user-defined types, fall into that path.
4550 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004551 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004552
Steve Naroff8e6aee52009-07-23 01:01:38 +00004553 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004554}
4555
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004556/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4557/// for providing type-safty for objective-c pointers used to pass/return
4558/// arguments in block literals. When passed as arguments, passing 'A*' where
4559/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4560/// not OK. For the return type, the opposite is not OK.
4561bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4562 const ObjCObjectPointerType *LHSOPT,
4563 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004564 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004565 return true;
4566
4567 if (LHSOPT->isObjCBuiltinType()) {
4568 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4569 }
4570
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004571 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004572 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4573 QualType(RHSOPT,0),
4574 false);
4575
4576 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4577 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4578 if (LHS && RHS) { // We have 2 user-defined types.
4579 if (LHS != RHS) {
4580 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4581 return false;
4582 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4583 return true;
4584 }
4585 else
4586 return true;
4587 }
4588 return false;
4589}
4590
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004591/// getIntersectionOfProtocols - This routine finds the intersection of set
4592/// of protocols inherited from two distinct objective-c pointer objects.
4593/// It is used to build composite qualifier list of the composite type of
4594/// the conditional expression involving two objective-c pointer objects.
4595static
4596void getIntersectionOfProtocols(ASTContext &Context,
4597 const ObjCObjectPointerType *LHSOPT,
4598 const ObjCObjectPointerType *RHSOPT,
4599 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4600
John McCall8b07ec22010-05-15 11:32:37 +00004601 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4602 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4603 assert(LHS->getInterface() && "LHS must have an interface base");
4604 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004605
4606 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4607 unsigned LHSNumProtocols = LHS->getNumProtocols();
4608 if (LHSNumProtocols > 0)
4609 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4610 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004611 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004612 Context.CollectInheritedProtocols(LHS->getInterface(),
4613 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004614 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4615 LHSInheritedProtocols.end());
4616 }
4617
4618 unsigned RHSNumProtocols = RHS->getNumProtocols();
4619 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004620 ObjCProtocolDecl **RHSProtocols =
4621 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004622 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4623 if (InheritedProtocolSet.count(RHSProtocols[i]))
4624 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4625 }
4626 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004627 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004628 Context.CollectInheritedProtocols(RHS->getInterface(),
4629 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004630 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4631 RHSInheritedProtocols.begin(),
4632 E = RHSInheritedProtocols.end(); I != E; ++I)
4633 if (InheritedProtocolSet.count((*I)))
4634 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004635 }
4636}
4637
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004638/// areCommonBaseCompatible - Returns common base class of the two classes if
4639/// one found. Note that this is O'2 algorithm. But it will be called as the
4640/// last type comparison in a ?-exp of ObjC pointer types before a
4641/// warning is issued. So, its invokation is extremely rare.
4642QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004643 const ObjCObjectPointerType *Lptr,
4644 const ObjCObjectPointerType *Rptr) {
4645 const ObjCObjectType *LHS = Lptr->getObjectType();
4646 const ObjCObjectType *RHS = Rptr->getObjectType();
4647 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4648 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4649 if (!LDecl || !RDecl)
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004650 return QualType();
4651
John McCall8b07ec22010-05-15 11:32:37 +00004652 while ((LDecl = LDecl->getSuperClass())) {
4653 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004654 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004655 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4656 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4657
4658 QualType Result = QualType(LHS, 0);
4659 if (!Protocols.empty())
4660 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4661 Result = getObjCObjectPointerType(Result);
4662 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004663 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004664 }
4665
4666 return QualType();
4667}
4668
John McCall8b07ec22010-05-15 11:32:37 +00004669bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4670 const ObjCObjectType *RHS) {
4671 assert(LHS->getInterface() && "LHS is not an interface type");
4672 assert(RHS->getInterface() && "RHS is not an interface type");
4673
Chris Lattner49af6a42008-04-07 06:51:04 +00004674 // Verify that the base decls are compatible: the RHS must be a subclass of
4675 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00004676 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00004677 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004678
Chris Lattner49af6a42008-04-07 06:51:04 +00004679 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4680 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004681 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004682 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004683
Chris Lattner49af6a42008-04-07 06:51:04 +00004684 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4685 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004686 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004687 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004688
John McCall8b07ec22010-05-15 11:32:37 +00004689 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4690 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004691 LHSPI != LHSPE; LHSPI++) {
4692 bool RHSImplementsProtocol = false;
4693
4694 // If the RHS doesn't implement the protocol on the left, the types
4695 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00004696 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4697 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004698 RHSPI != RHSPE; RHSPI++) {
4699 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004700 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004701 break;
4702 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004703 }
4704 // FIXME: For better diagnostics, consider passing back the protocol name.
4705 if (!RHSImplementsProtocol)
4706 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004707 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004708 // The RHS implements all protocols listed on the LHS.
4709 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004710}
4711
Steve Naroffb7605152009-02-12 17:52:19 +00004712bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4713 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004714 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4715 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004716
Steve Naroff7cae42b2009-07-10 23:34:53 +00004717 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004718 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004719
4720 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4721 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004722}
4723
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00004724bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
4725 return canAssignObjCInterfaces(
4726 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
4727 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
4728}
4729
Mike Stump11289f42009-09-09 15:08:12 +00004730/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004731/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004732/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004733/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004734bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
4735 bool CompareUnqualified) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004736 if (getLangOptions().CPlusPlus)
4737 return hasSameType(LHS, RHS);
4738
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004739 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman47f77112008-08-22 00:56:42 +00004740}
4741
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004742bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4743 return !mergeTypes(LHS, RHS, true).isNull();
4744}
4745
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004746/// mergeTransparentUnionType - if T is a transparent union type and a member
4747/// of T is compatible with SubType, return the merged type, else return
4748/// QualType()
4749QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
4750 bool OfBlockPointer,
4751 bool Unqualified) {
4752 if (const RecordType *UT = T->getAsUnionType()) {
4753 RecordDecl *UD = UT->getDecl();
4754 if (UD->hasAttr<TransparentUnionAttr>()) {
4755 for (RecordDecl::field_iterator it = UD->field_begin(),
4756 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbourne19b961d2010-12-02 21:00:06 +00004757 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004758 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
4759 if (!MT.isNull())
4760 return MT;
4761 }
4762 }
4763 }
4764
4765 return QualType();
4766}
4767
4768/// mergeFunctionArgumentTypes - merge two types which appear as function
4769/// argument types
4770QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
4771 bool OfBlockPointer,
4772 bool Unqualified) {
4773 // GNU extension: two types are compatible if they appear as a function
4774 // argument, one of the types is a transparent union type and the other
4775 // type is compatible with a union member
4776 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
4777 Unqualified);
4778 if (!lmerge.isNull())
4779 return lmerge;
4780
4781 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
4782 Unqualified);
4783 if (!rmerge.isNull())
4784 return rmerge;
4785
4786 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
4787}
4788
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004789QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004790 bool OfBlockPointer,
4791 bool Unqualified) {
John McCall9dd450b2009-09-21 23:43:11 +00004792 const FunctionType *lbase = lhs->getAs<FunctionType>();
4793 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004794 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4795 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004796 bool allLTypes = true;
4797 bool allRTypes = true;
4798
4799 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004800 QualType retType;
4801 if (OfBlockPointer)
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004802 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true,
4803 Unqualified);
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004804 else
John McCall8c6b56f2010-12-15 01:06:38 +00004805 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
4806 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00004807 if (retType.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004808
4809 if (Unqualified)
4810 retType = retType.getUnqualifiedType();
4811
4812 CanQualType LRetType = getCanonicalType(lbase->getResultType());
4813 CanQualType RRetType = getCanonicalType(rbase->getResultType());
4814 if (Unqualified) {
4815 LRetType = LRetType.getUnqualifiedType();
4816 RRetType = RRetType.getUnqualifiedType();
4817 }
4818
4819 if (getCanonicalType(retType) != LRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00004820 allLTypes = false;
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004821 if (getCanonicalType(retType) != RRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00004822 allRTypes = false;
John McCall8c6b56f2010-12-15 01:06:38 +00004823
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004824 // FIXME: double check this
4825 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4826 // rbase->getRegParmAttr() != 0 &&
4827 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004828 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4829 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
John McCall8c6b56f2010-12-15 01:06:38 +00004830
Douglas Gregor8c940862010-01-18 17:14:39 +00004831 // Compatible functions must have compatible calling conventions
John McCall8c6b56f2010-12-15 01:06:38 +00004832 if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
Douglas Gregor8c940862010-01-18 17:14:39 +00004833 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004834
John McCall8c6b56f2010-12-15 01:06:38 +00004835 // Regparm is part of the calling convention.
4836 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
4837 return QualType();
4838
4839 // It's noreturn if either type is.
4840 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
4841 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4842 if (NoReturn != lbaseInfo.getNoReturn())
4843 allLTypes = false;
4844 if (NoReturn != rbaseInfo.getNoReturn())
4845 allRTypes = false;
4846
4847 FunctionType::ExtInfo einfo(NoReturn,
4848 lbaseInfo.getRegParm(),
4849 lbaseInfo.getCC());
John McCalldb40c7f2010-12-14 08:05:40 +00004850
Eli Friedman47f77112008-08-22 00:56:42 +00004851 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004852 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4853 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004854 unsigned lproto_nargs = lproto->getNumArgs();
4855 unsigned rproto_nargs = rproto->getNumArgs();
4856
4857 // Compatible functions must have the same number of arguments
4858 if (lproto_nargs != rproto_nargs)
4859 return QualType();
4860
4861 // Variadic and non-variadic functions aren't compatible
4862 if (lproto->isVariadic() != rproto->isVariadic())
4863 return QualType();
4864
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004865 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4866 return QualType();
4867
Eli Friedman47f77112008-08-22 00:56:42 +00004868 // Check argument compatibility
4869 llvm::SmallVector<QualType, 10> types;
4870 for (unsigned i = 0; i < lproto_nargs; i++) {
4871 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4872 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004873 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
4874 OfBlockPointer,
4875 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00004876 if (argtype.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004877
4878 if (Unqualified)
4879 argtype = argtype.getUnqualifiedType();
4880
Eli Friedman47f77112008-08-22 00:56:42 +00004881 types.push_back(argtype);
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004882 if (Unqualified) {
4883 largtype = largtype.getUnqualifiedType();
4884 rargtype = rargtype.getUnqualifiedType();
4885 }
4886
Chris Lattner465fa322008-10-05 17:34:18 +00004887 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4888 allLTypes = false;
4889 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4890 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004891 }
4892 if (allLTypes) return lhs;
4893 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00004894
4895 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
4896 EPI.ExtInfo = einfo;
4897 return getFunctionType(retType, types.begin(), types.size(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00004898 }
4899
4900 if (lproto) allRTypes = false;
4901 if (rproto) allLTypes = false;
4902
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004903 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004904 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004905 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004906 if (proto->isVariadic()) return QualType();
4907 // Check that the types are compatible with the types that
4908 // would result from default argument promotions (C99 6.7.5.3p15).
4909 // The only types actually affected are promotable integer
4910 // types and floats, which would be passed as a different
4911 // type depending on whether the prototype is visible.
4912 unsigned proto_nargs = proto->getNumArgs();
4913 for (unsigned i = 0; i < proto_nargs; ++i) {
4914 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004915
4916 // Look at the promotion type of enum types, since that is the type used
4917 // to pass enum values.
4918 if (const EnumType *Enum = argTy->getAs<EnumType>())
4919 argTy = Enum->getDecl()->getPromotionType();
4920
Eli Friedman47f77112008-08-22 00:56:42 +00004921 if (argTy->isPromotableIntegerType() ||
4922 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4923 return QualType();
4924 }
4925
4926 if (allLTypes) return lhs;
4927 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00004928
4929 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
4930 EPI.ExtInfo = einfo;
Eli Friedman47f77112008-08-22 00:56:42 +00004931 return getFunctionType(retType, proto->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00004932 proto->getNumArgs(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00004933 }
4934
4935 if (allLTypes) return lhs;
4936 if (allRTypes) return rhs;
John McCall8c6b56f2010-12-15 01:06:38 +00004937 return getFunctionNoProtoType(retType, einfo);
Eli Friedman47f77112008-08-22 00:56:42 +00004938}
4939
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004940QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004941 bool OfBlockPointer,
4942 bool Unqualified) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004943 // C++ [expr]: If an expression initially has the type "reference to T", the
4944 // type is adjusted to "T" prior to any further analysis, the expression
4945 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004946 // expression is an lvalue unless the reference is an rvalue reference and
4947 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004948 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4949 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004950
4951 if (Unqualified) {
4952 LHS = LHS.getUnqualifiedType();
4953 RHS = RHS.getUnqualifiedType();
4954 }
Douglas Gregor21e771e2010-02-03 21:02:30 +00004955
Eli Friedman47f77112008-08-22 00:56:42 +00004956 QualType LHSCan = getCanonicalType(LHS),
4957 RHSCan = getCanonicalType(RHS);
4958
4959 // If two types are identical, they are compatible.
4960 if (LHSCan == RHSCan)
4961 return LHS;
4962
John McCall8ccfcb52009-09-24 19:53:00 +00004963 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004964 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4965 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004966 if (LQuals != RQuals) {
4967 // If any of these qualifiers are different, we have a type
4968 // mismatch.
4969 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4970 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4971 return QualType();
4972
4973 // Exactly one GC qualifier difference is allowed: __strong is
4974 // okay if the other type has no GC qualifier but is an Objective
4975 // C object pointer (i.e. implicitly strong by default). We fix
4976 // this by pretending that the unqualified type was actually
4977 // qualified __strong.
4978 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4979 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4980 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4981
4982 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4983 return QualType();
4984
4985 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4986 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4987 }
4988 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4989 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4990 }
Eli Friedman47f77112008-08-22 00:56:42 +00004991 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004992 }
4993
4994 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004995
Eli Friedmandcca6332009-06-01 01:22:52 +00004996 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4997 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004998
Chris Lattnerfd652912008-01-14 05:45:46 +00004999 // We want to consider the two function types to be the same for these
5000 // comparisons, just force one to the other.
5001 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5002 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00005003
5004 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00005005 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5006 LHSClass = Type::ConstantArray;
5007 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5008 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00005009
John McCall8b07ec22010-05-15 11:32:37 +00005010 // ObjCInterfaces are just specialized ObjCObjects.
5011 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5012 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5013
Nate Begemance4d7fc2008-04-18 23:10:10 +00005014 // Canonicalize ExtVector -> Vector.
5015 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5016 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00005017
Chris Lattner95554662008-04-07 05:43:21 +00005018 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005019 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00005020 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00005021 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00005022 // Compatibility is based on the underlying type, not the promotion
5023 // type.
John McCall9dd450b2009-09-21 23:43:11 +00005024 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005025 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5026 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005027 }
John McCall9dd450b2009-09-21 23:43:11 +00005028 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005029 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5030 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005031 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005032
Eli Friedman47f77112008-08-22 00:56:42 +00005033 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005034 }
Eli Friedman47f77112008-08-22 00:56:42 +00005035
Steve Naroffc6edcbd2008-01-09 22:43:08 +00005036 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005037 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005038#define TYPE(Class, Base)
5039#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00005040#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005041#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5042#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5043#include "clang/AST/TypeNodes.def"
5044 assert(false && "Non-canonical and dependent types shouldn't get here");
5045 return QualType();
5046
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005047 case Type::LValueReference:
5048 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005049 case Type::MemberPointer:
5050 assert(false && "C++ should never be in mergeTypes");
5051 return QualType();
5052
John McCall8b07ec22010-05-15 11:32:37 +00005053 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005054 case Type::IncompleteArray:
5055 case Type::VariableArray:
5056 case Type::FunctionProto:
5057 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005058 assert(false && "Types are eliminated above");
5059 return QualType();
5060
Chris Lattnerfd652912008-01-14 05:45:46 +00005061 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00005062 {
5063 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005064 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5065 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005066 if (Unqualified) {
5067 LHSPointee = LHSPointee.getUnqualifiedType();
5068 RHSPointee = RHSPointee.getUnqualifiedType();
5069 }
5070 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5071 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005072 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00005073 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005074 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00005075 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005076 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005077 return getPointerType(ResultType);
5078 }
Steve Naroff68e167d2008-12-10 17:49:55 +00005079 case Type::BlockPointer:
5080 {
5081 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005082 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5083 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005084 if (Unqualified) {
5085 LHSPointee = LHSPointee.getUnqualifiedType();
5086 RHSPointee = RHSPointee.getUnqualifiedType();
5087 }
5088 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5089 Unqualified);
Steve Naroff68e167d2008-12-10 17:49:55 +00005090 if (ResultType.isNull()) return QualType();
5091 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5092 return LHS;
5093 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5094 return RHS;
5095 return getBlockPointerType(ResultType);
5096 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005097 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00005098 {
5099 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5100 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5101 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5102 return QualType();
5103
5104 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5105 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005106 if (Unqualified) {
5107 LHSElem = LHSElem.getUnqualifiedType();
5108 RHSElem = RHSElem.getUnqualifiedType();
5109 }
5110
5111 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005112 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00005113 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5114 return LHS;
5115 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5116 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00005117 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5118 ArrayType::ArraySizeModifier(), 0);
5119 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5120 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005121 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5122 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00005123 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5124 return LHS;
5125 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5126 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005127 if (LVAT) {
5128 // FIXME: This isn't correct! But tricky to implement because
5129 // the array's size has to be the size of LHS, but the type
5130 // has to be different.
5131 return LHS;
5132 }
5133 if (RVAT) {
5134 // FIXME: This isn't correct! But tricky to implement because
5135 // the array's size has to be the size of RHS, but the type
5136 // has to be different.
5137 return RHS;
5138 }
Eli Friedman3e62c212008-08-22 01:48:21 +00005139 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5140 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00005141 return getIncompleteArrayType(ResultType,
5142 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005143 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005144 case Type::FunctionNoProto:
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005145 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005146 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005147 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00005148 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00005149 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005150 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00005151 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00005152 case Type::Complex:
5153 // Distinct complex types are incompatible.
5154 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005155 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00005156 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00005157 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5158 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00005159 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00005160 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00005161 case Type::ObjCObject: {
5162 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00005163 // FIXME: This should be type compatibility, e.g. whether
5164 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00005165 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5166 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5167 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00005168 return LHS;
5169
Eli Friedman47f77112008-08-22 00:56:42 +00005170 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00005171 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00005172 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005173 if (OfBlockPointer) {
5174 if (canAssignObjCInterfacesInBlockPointer(
5175 LHS->getAs<ObjCObjectPointerType>(),
5176 RHS->getAs<ObjCObjectPointerType>()))
5177 return LHS;
5178 return QualType();
5179 }
John McCall9dd450b2009-09-21 23:43:11 +00005180 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5181 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00005182 return LHS;
5183
Steve Naroffc68cfcf2008-12-10 22:14:21 +00005184 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005185 }
Steve Naroff32e44c02007-10-15 20:41:53 +00005186 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005187
5188 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005189}
Ted Kremenekfc581a92007-10-31 17:10:13 +00005190
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005191/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5192/// 'RHS' attributes and returns the merged version; including for function
5193/// return types.
5194QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5195 QualType LHSCan = getCanonicalType(LHS),
5196 RHSCan = getCanonicalType(RHS);
5197 // If two types are identical, they are compatible.
5198 if (LHSCan == RHSCan)
5199 return LHS;
5200 if (RHSCan->isFunctionType()) {
5201 if (!LHSCan->isFunctionType())
5202 return QualType();
5203 QualType OldReturnType =
5204 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5205 QualType NewReturnType =
5206 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5207 QualType ResReturnType =
5208 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5209 if (ResReturnType.isNull())
5210 return QualType();
5211 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5212 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5213 // In either case, use OldReturnType to build the new function type.
5214 const FunctionType *F = LHS->getAs<FunctionType>();
5215 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
John McCalldb40c7f2010-12-14 08:05:40 +00005216 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5217 EPI.ExtInfo = getFunctionExtInfo(LHS);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005218 QualType ResultType
5219 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005220 FPT->getNumArgs(), EPI);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005221 return ResultType;
5222 }
5223 }
5224 return QualType();
5225 }
5226
5227 // If the qualifiers are different, the types can still be merged.
5228 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5229 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5230 if (LQuals != RQuals) {
5231 // If any of these qualifiers are different, we have a type mismatch.
5232 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5233 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5234 return QualType();
5235
5236 // Exactly one GC qualifier difference is allowed: __strong is
5237 // okay if the other type has no GC qualifier but is an Objective
5238 // C object pointer (i.e. implicitly strong by default). We fix
5239 // this by pretending that the unqualified type was actually
5240 // qualified __strong.
5241 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5242 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5243 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5244
5245 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5246 return QualType();
5247
5248 if (GC_L == Qualifiers::Strong)
5249 return LHS;
5250 if (GC_R == Qualifiers::Strong)
5251 return RHS;
5252 return QualType();
5253 }
5254
5255 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5256 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5257 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5258 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5259 if (ResQT == LHSBaseQT)
5260 return LHS;
5261 if (ResQT == RHSBaseQT)
5262 return RHS;
5263 }
5264 return QualType();
5265}
5266
Chris Lattner4ba0cef2008-04-07 07:01:58 +00005267//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005268// Integer Predicates
5269//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00005270
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005271unsigned ASTContext::getIntWidth(QualType T) {
John McCall56774992009-12-09 09:09:27 +00005272 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00005273 T = ET->getDecl()->getIntegerType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00005274 if (T->isBooleanType())
5275 return 1;
Eli Friedman1efaaea2009-02-13 02:31:07 +00005276 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005277 return (unsigned)getTypeSize(T);
5278}
5279
5280QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00005281 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00005282
5283 // Turn <4 x signed int> -> <4 x unsigned int>
5284 if (const VectorType *VTy = T->getAs<VectorType>())
5285 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsonaeb56442010-11-10 21:56:12 +00005286 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattnerec3a1562009-10-17 20:33:28 +00005287
5288 // For enums, we return the unsigned version of the base type.
5289 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005290 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00005291
5292 const BuiltinType *BTy = T->getAs<BuiltinType>();
5293 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005294 switch (BTy->getKind()) {
5295 case BuiltinType::Char_S:
5296 case BuiltinType::SChar:
5297 return UnsignedCharTy;
5298 case BuiltinType::Short:
5299 return UnsignedShortTy;
5300 case BuiltinType::Int:
5301 return UnsignedIntTy;
5302 case BuiltinType::Long:
5303 return UnsignedLongTy;
5304 case BuiltinType::LongLong:
5305 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00005306 case BuiltinType::Int128:
5307 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005308 default:
5309 assert(0 && "Unexpected signed integer type");
5310 return QualType();
5311 }
5312}
5313
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005314ExternalASTSource::~ExternalASTSource() { }
5315
5316void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00005317
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005318ASTMutationListener::~ASTMutationListener() { }
5319
Chris Lattnerecd79c62009-06-14 00:45:47 +00005320
5321//===----------------------------------------------------------------------===//
5322// Builtin Type Computation
5323//===----------------------------------------------------------------------===//
5324
5325/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattnerdc226c22010-10-01 22:42:38 +00005326/// pointer over the consumed characters. This returns the resultant type. If
5327/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5328/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5329/// a vector of "i*".
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005330///
5331/// RequiresICE is filled in on return to indicate whether the value is required
5332/// to be an Integer Constant Expression.
Mike Stump11289f42009-09-09 15:08:12 +00005333static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00005334 ASTContext::GetBuiltinTypeError &Error,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005335 bool &RequiresICE,
Chris Lattnerdc226c22010-10-01 22:42:38 +00005336 bool AllowTypeModifiers) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005337 // Modifiers.
5338 int HowLong = 0;
5339 bool Signed = false, Unsigned = false;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005340 RequiresICE = false;
Chris Lattner84733392010-10-01 07:13:18 +00005341
Chris Lattnerdc226c22010-10-01 22:42:38 +00005342 // Read the prefixed modifiers first.
Chris Lattnerecd79c62009-06-14 00:45:47 +00005343 bool Done = false;
5344 while (!Done) {
5345 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00005346 default: Done = true; --Str; break;
Chris Lattner84733392010-10-01 07:13:18 +00005347 case 'I':
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005348 RequiresICE = true;
Chris Lattner84733392010-10-01 07:13:18 +00005349 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005350 case 'S':
5351 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5352 assert(!Signed && "Can't use 'S' modifier multiple times!");
5353 Signed = true;
5354 break;
5355 case 'U':
5356 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5357 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5358 Unsigned = true;
5359 break;
5360 case 'L':
5361 assert(HowLong <= 2 && "Can't have LLLL modifier");
5362 ++HowLong;
5363 break;
5364 }
5365 }
5366
5367 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005368
Chris Lattnerecd79c62009-06-14 00:45:47 +00005369 // Read the base type.
5370 switch (*Str++) {
5371 default: assert(0 && "Unknown builtin type letter!");
5372 case 'v':
5373 assert(HowLong == 0 && !Signed && !Unsigned &&
5374 "Bad modifiers used with 'v'!");
5375 Type = Context.VoidTy;
5376 break;
5377 case 'f':
5378 assert(HowLong == 0 && !Signed && !Unsigned &&
5379 "Bad modifiers used with 'f'!");
5380 Type = Context.FloatTy;
5381 break;
5382 case 'd':
5383 assert(HowLong < 2 && !Signed && !Unsigned &&
5384 "Bad modifiers used with 'd'!");
5385 if (HowLong)
5386 Type = Context.LongDoubleTy;
5387 else
5388 Type = Context.DoubleTy;
5389 break;
5390 case 's':
5391 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5392 if (Unsigned)
5393 Type = Context.UnsignedShortTy;
5394 else
5395 Type = Context.ShortTy;
5396 break;
5397 case 'i':
5398 if (HowLong == 3)
5399 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5400 else if (HowLong == 2)
5401 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5402 else if (HowLong == 1)
5403 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5404 else
5405 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5406 break;
5407 case 'c':
5408 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5409 if (Signed)
5410 Type = Context.SignedCharTy;
5411 else if (Unsigned)
5412 Type = Context.UnsignedCharTy;
5413 else
5414 Type = Context.CharTy;
5415 break;
5416 case 'b': // boolean
5417 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5418 Type = Context.BoolTy;
5419 break;
5420 case 'z': // size_t.
5421 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5422 Type = Context.getSizeType();
5423 break;
5424 case 'F':
5425 Type = Context.getCFConstantStringType();
5426 break;
Fariborz Jahaniand11da7e2010-11-09 21:38:20 +00005427 case 'G':
5428 Type = Context.getObjCIdType();
5429 break;
5430 case 'H':
5431 Type = Context.getObjCSelType();
5432 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005433 case 'a':
5434 Type = Context.getBuiltinVaListType();
5435 assert(!Type.isNull() && "builtin va list type not initialized!");
5436 break;
5437 case 'A':
5438 // This is a "reference" to a va_list; however, what exactly
5439 // this means depends on how va_list is defined. There are two
5440 // different kinds of va_list: ones passed by value, and ones
5441 // passed by reference. An example of a by-value va_list is
5442 // x86, where va_list is a char*. An example of by-ref va_list
5443 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5444 // we want this argument to be a char*&; for x86-64, we want
5445 // it to be a __va_list_tag*.
5446 Type = Context.getBuiltinVaListType();
5447 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005448 if (Type->isArrayType())
Chris Lattnerecd79c62009-06-14 00:45:47 +00005449 Type = Context.getArrayDecayedType(Type);
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005450 else
Chris Lattnerecd79c62009-06-14 00:45:47 +00005451 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005452 break;
5453 case 'V': {
5454 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005455 unsigned NumElements = strtoul(Str, &End, 10);
5456 assert(End != Str && "Missing vector size");
Chris Lattnerecd79c62009-06-14 00:45:47 +00005457 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005458
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005459 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5460 RequiresICE, false);
5461 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattnerdc226c22010-10-01 22:42:38 +00005462
5463 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner37141f42010-06-23 06:00:24 +00005464 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00005465 VectorType::GenericVector);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005466 break;
5467 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005468 case 'X': {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005469 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5470 false);
5471 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005472 Type = Context.getComplexType(ElementType);
5473 break;
5474 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005475 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005476 Type = Context.getFILEType();
5477 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005478 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005479 return QualType();
5480 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005481 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005482 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005483 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005484 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005485 else
5486 Type = Context.getjmp_bufType();
5487
Mike Stump2adb4da2009-07-28 23:47:15 +00005488 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005489 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005490 return QualType();
5491 }
5492 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005493 }
Mike Stump11289f42009-09-09 15:08:12 +00005494
Chris Lattnerdc226c22010-10-01 22:42:38 +00005495 // If there are modifiers and if we're allowed to parse them, go for it.
5496 Done = !AllowTypeModifiers;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005497 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005498 switch (char c = *Str++) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005499 default: Done = true; --Str; break;
5500 case '*':
5501 case '&': {
5502 // Both pointers and references can have their pointee types
5503 // qualified with an address space.
5504 char *End;
5505 unsigned AddrSpace = strtoul(Str, &End, 10);
5506 if (End != Str && AddrSpace != 0) {
5507 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5508 Str = End;
5509 }
5510 if (c == '*')
5511 Type = Context.getPointerType(Type);
5512 else
5513 Type = Context.getLValueReferenceType(Type);
5514 break;
5515 }
5516 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5517 case 'C':
5518 Type = Type.withConst();
5519 break;
5520 case 'D':
5521 Type = Context.getVolatileType(Type);
5522 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005523 }
5524 }
Chris Lattner84733392010-10-01 07:13:18 +00005525
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005526 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner84733392010-10-01 07:13:18 +00005527 "Integer constant 'I' type must be an integer");
Mike Stump11289f42009-09-09 15:08:12 +00005528
Chris Lattnerecd79c62009-06-14 00:45:47 +00005529 return Type;
5530}
5531
5532/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattnerdc226c22010-10-01 22:42:38 +00005533QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005534 GetBuiltinTypeError &Error,
5535 unsigned *IntegerConstantArgs) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005536 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump11289f42009-09-09 15:08:12 +00005537
Chris Lattnerecd79c62009-06-14 00:45:47 +00005538 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005539
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005540 bool RequiresICE = false;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005541 Error = GE_None;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005542 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5543 RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005544 if (Error != GE_None)
5545 return QualType();
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005546
5547 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5548
Chris Lattnerecd79c62009-06-14 00:45:47 +00005549 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005550 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005551 if (Error != GE_None)
5552 return QualType();
5553
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005554 // If this argument is required to be an IntegerConstantExpression and the
5555 // caller cares, fill in the bitmask we return.
5556 if (RequiresICE && IntegerConstantArgs)
5557 *IntegerConstantArgs |= 1 << ArgTypes.size();
5558
Chris Lattnerecd79c62009-06-14 00:45:47 +00005559 // Do array -> pointer decay. The builtin should use the decayed type.
5560 if (Ty->isArrayType())
5561 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005562
Chris Lattnerecd79c62009-06-14 00:45:47 +00005563 ArgTypes.push_back(Ty);
5564 }
5565
5566 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5567 "'.' should only occur at end of builtin type list!");
5568
5569 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
5570 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
5571 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005572
John McCalldb40c7f2010-12-14 08:05:40 +00005573 FunctionProtoType::ExtProtoInfo EPI;
5574 EPI.Variadic = (TypeStr[0] == '.');
Douglas Gregor36c569f2010-02-21 22:15:06 +00005575 // FIXME: Should we create noreturn types?
John McCalldb40c7f2010-12-14 08:05:40 +00005576
5577 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005578}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005579
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005580GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5581 GVALinkage External = GVA_StrongExternal;
5582
5583 Linkage L = FD->getLinkage();
5584 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5585 FD->getType()->getLinkage() == UniqueExternalLinkage)
5586 L = UniqueExternalLinkage;
5587
5588 switch (L) {
5589 case NoLinkage:
5590 case InternalLinkage:
5591 case UniqueExternalLinkage:
5592 return GVA_Internal;
5593
5594 case ExternalLinkage:
5595 switch (FD->getTemplateSpecializationKind()) {
5596 case TSK_Undeclared:
5597 case TSK_ExplicitSpecialization:
5598 External = GVA_StrongExternal;
5599 break;
5600
5601 case TSK_ExplicitInstantiationDefinition:
5602 return GVA_ExplicitTemplateInstantiation;
5603
5604 case TSK_ExplicitInstantiationDeclaration:
5605 case TSK_ImplicitInstantiation:
5606 External = GVA_TemplateInstantiation;
5607 break;
5608 }
5609 }
5610
5611 if (!FD->isInlined())
5612 return External;
5613
5614 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5615 // GNU or C99 inline semantics. Determine whether this symbol should be
5616 // externally visible.
5617 if (FD->isInlineDefinitionExternallyVisible())
5618 return External;
5619
5620 // C99 inline semantics, where the symbol is not externally visible.
5621 return GVA_C99Inline;
5622 }
5623
5624 // C++0x [temp.explicit]p9:
5625 // [ Note: The intent is that an inline function that is the subject of
5626 // an explicit instantiation declaration will still be implicitly
5627 // instantiated when used so that the body can be considered for
5628 // inlining, but that no out-of-line copy of the inline function would be
5629 // generated in the translation unit. -- end note ]
5630 if (FD->getTemplateSpecializationKind()
5631 == TSK_ExplicitInstantiationDeclaration)
5632 return GVA_C99Inline;
5633
5634 return GVA_CXXInline;
5635}
5636
5637GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
5638 // If this is a static data member, compute the kind of template
5639 // specialization. Otherwise, this variable is not part of a
5640 // template.
5641 TemplateSpecializationKind TSK = TSK_Undeclared;
5642 if (VD->isStaticDataMember())
5643 TSK = VD->getTemplateSpecializationKind();
5644
5645 Linkage L = VD->getLinkage();
5646 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5647 VD->getType()->getLinkage() == UniqueExternalLinkage)
5648 L = UniqueExternalLinkage;
5649
5650 switch (L) {
5651 case NoLinkage:
5652 case InternalLinkage:
5653 case UniqueExternalLinkage:
5654 return GVA_Internal;
5655
5656 case ExternalLinkage:
5657 switch (TSK) {
5658 case TSK_Undeclared:
5659 case TSK_ExplicitSpecialization:
5660 return GVA_StrongExternal;
5661
5662 case TSK_ExplicitInstantiationDeclaration:
5663 llvm_unreachable("Variable should not be instantiated");
5664 // Fall through to treat this like any other instantiation.
5665
5666 case TSK_ExplicitInstantiationDefinition:
5667 return GVA_ExplicitTemplateInstantiation;
5668
5669 case TSK_ImplicitInstantiation:
5670 return GVA_TemplateInstantiation;
5671 }
5672 }
5673
5674 return GVA_StrongExternal;
5675}
5676
Argyrios Kyrtzidisc9049332010-07-29 20:08:05 +00005677bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005678 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
5679 if (!VD->isFileVarDecl())
5680 return false;
5681 } else if (!isa<FunctionDecl>(D))
5682 return false;
5683
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00005684 // Weak references don't produce any output by themselves.
5685 if (D->hasAttr<WeakRefAttr>())
5686 return false;
5687
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005688 // Aliases and used decls are required.
5689 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
5690 return true;
5691
5692 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5693 // Forward declarations aren't required.
5694 if (!FD->isThisDeclarationADefinition())
5695 return false;
5696
5697 // Constructors and destructors are required.
5698 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
5699 return true;
5700
5701 // The key function for a class is required.
5702 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5703 const CXXRecordDecl *RD = MD->getParent();
5704 if (MD->isOutOfLine() && RD->isDynamicClass()) {
5705 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
5706 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
5707 return true;
5708 }
5709 }
5710
5711 GVALinkage Linkage = GetGVALinkageForFunction(FD);
5712
5713 // static, static inline, always_inline, and extern inline functions can
5714 // always be deferred. Normal inline functions can be deferred in C99/C++.
5715 // Implicit template instantiations can also be deferred in C++.
5716 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
5717 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
5718 return false;
5719 return true;
5720 }
5721
5722 const VarDecl *VD = cast<VarDecl>(D);
5723 assert(VD->isFileVarDecl() && "Expected file scoped var");
5724
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00005725 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
5726 return false;
5727
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005728 // Structs that have non-trivial constructors or destructors are required.
5729
5730 // FIXME: Handle references.
5731 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
5732 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +00005733 if (RD->hasDefinition() &&
5734 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005735 return true;
5736 }
5737 }
5738
5739 GVALinkage L = GetGVALinkageForVariable(VD);
5740 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
5741 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
5742 return false;
5743 }
5744
5745 return true;
5746}
Charles Davis53c59df2010-08-16 03:33:14 +00005747
Charles Davis99202b32010-11-09 18:04:24 +00005748CallingConv ASTContext::getDefaultMethodCallConv() {
5749 // Pass through to the C++ ABI object
5750 return ABI->getDefaultMethodCallConv();
5751}
5752
Anders Carlsson60a62632010-11-25 01:51:53 +00005753bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) {
5754 // Pass through to the C++ ABI object
5755 return ABI->isNearlyEmpty(RD);
5756}
5757
Charles Davis53c59df2010-08-16 03:33:14 +00005758CXXABI::~CXXABI() {}