blob: 9a8b153e26fbf18d9710d164557369b91f6fb0fa [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
Douglas Gregoref462e62009-04-30 17:32:17 +0000814 case Type::Typedef: {
815 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregorf5bae222010-08-27 00:11:28 +0000816 std::pair<uint64_t, unsigned> Info
817 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
818 Align = std::max(Typedef->getMaxAlignment(), Info.second);
819 Width = Info.first;
Douglas Gregordc572a32009-03-30 22:58:21 +0000820 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000821 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000822
823 case Type::TypeOfExpr:
824 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
825 .getTypePtr());
826
827 case Type::TypeOf:
828 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
829
Anders Carlsson81df7b82009-06-24 19:06:50 +0000830 case Type::Decltype:
831 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
832 .getTypePtr());
833
Abramo Bagnara6150c882010-05-11 21:36:43 +0000834 case Type::Elaborated:
835 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000836
Douglas Gregoref462e62009-04-30 17:32:17 +0000837 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000838 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000839 "Cannot request the size of a dependent type");
840 // FIXME: this is likely to be wrong once we support template
841 // aliases, since a template alias could refer to a typedef that
842 // has an __aligned__ attribute on it.
843 return getTypeInfo(getCanonicalType(T));
844 }
Mike Stump11289f42009-09-09 15:08:12 +0000845
Chris Lattner53cfe802007-07-18 17:52:12 +0000846 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000847 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000848}
849
Ken Dyck8c89d592009-12-22 14:23:30 +0000850/// getTypeSizeInChars - Return the size of the specified type, in characters.
851/// This method does not work on incomplete types.
852CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000853 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000854}
855CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000856 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000857}
858
Ken Dycka6046ab2010-01-26 17:25:18 +0000859/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000860/// characters. This method does not work on incomplete types.
861CharUnits ASTContext::getTypeAlignInChars(QualType T) {
862 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
863}
864CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
865 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
866}
867
Chris Lattnera3402cd2009-01-27 18:08:34 +0000868/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
869/// type for the current target in bits. This can be different than the ABI
870/// alignment in cases where it is beneficial for performance to overalign
871/// a data type.
872unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
873 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000874
875 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000876 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000877 T = CT->getElementType().getTypePtr();
878 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
879 T->isSpecificBuiltinType(BuiltinType::LongLong))
880 return std::max(ABIAlign, (unsigned)getTypeSize(T));
881
Chris Lattnera3402cd2009-01-27 18:08:34 +0000882 return ABIAlign;
883}
884
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000885/// ShallowCollectObjCIvars -
886/// Collect all ivars, including those synthesized, in the current class.
887///
888void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000889 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000890 // FIXME. This need be removed but there are two many places which
891 // assume const-ness of ObjCInterfaceDecl
892 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
893 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
894 Iv= Iv->getNextIvar())
895 Ivars.push_back(Iv);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000896}
897
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000898/// DeepCollectObjCIvars -
899/// This routine first collects all declared, but not synthesized, ivars in
900/// super class and then collects all ivars, including those synthesized for
901/// current class. This routine is used for implementation of current class
902/// when all ivars, declared and synthesized are known.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000903///
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000904void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
905 bool leafClass,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000906 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000907 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
908 DeepCollectObjCIvars(SuperClass, false, Ivars);
909 if (!leafClass) {
910 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
911 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000912 Ivars.push_back(*I);
913 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000914 else
915 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000916}
917
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000918/// CollectInheritedProtocols - Collect all protocols in current class and
919/// those inherited by it.
920void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000921 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000922 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000923 // We can use protocol_iterator here instead of
924 // all_referenced_protocol_iterator since we are walking all categories.
925 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
926 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000927 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000928 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000929 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000930 PE = Proto->protocol_end(); P != PE; ++P) {
931 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000932 CollectInheritedProtocols(*P, Protocols);
933 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000934 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000935
936 // Categories of this Interface.
937 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
938 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
939 CollectInheritedProtocols(CDeclChain, Protocols);
940 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
941 while (SD) {
942 CollectInheritedProtocols(SD, Protocols);
943 SD = SD->getSuperClass();
944 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000945 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000946 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000947 PE = OC->protocol_end(); P != PE; ++P) {
948 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000949 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000950 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
951 PE = Proto->protocol_end(); P != PE; ++P)
952 CollectInheritedProtocols(*P, Protocols);
953 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000954 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000955 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
956 PE = OP->protocol_end(); P != PE; ++P) {
957 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000958 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000959 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
960 PE = Proto->protocol_end(); P != PE; ++P)
961 CollectInheritedProtocols(*P, Protocols);
962 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000963 }
964}
965
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000966unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
967 unsigned count = 0;
968 // Count ivars declared in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000969 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
970 CDecl = CDecl->getNextClassExtension())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000971 count += CDecl->ivar_size();
972
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000973 // Count ivar defined in this class's implementation. This
974 // includes synthesized ivars.
975 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000976 count += ImplDecl->ivar_size();
977
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000978 return count;
979}
980
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000981/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
982ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
983 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
984 I = ObjCImpls.find(D);
985 if (I != ObjCImpls.end())
986 return cast<ObjCImplementationDecl>(I->second);
987 return 0;
988}
989/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
990ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
991 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
992 I = ObjCImpls.find(D);
993 if (I != ObjCImpls.end())
994 return cast<ObjCCategoryImplDecl>(I->second);
995 return 0;
996}
997
998/// \brief Set the implementation of ObjCInterfaceDecl.
999void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1000 ObjCImplementationDecl *ImplD) {
1001 assert(IFaceD && ImplD && "Passed null params");
1002 ObjCImpls[IFaceD] = ImplD;
1003}
1004/// \brief Set the implementation of ObjCCategoryDecl.
1005void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1006 ObjCCategoryImplDecl *ImplD) {
1007 assert(CatD && ImplD && "Passed null params");
1008 ObjCImpls[CatD] = ImplD;
1009}
1010
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001011/// \brief Get the copy initialization expression of VarDecl,or NULL if
1012/// none exists.
Fariborz Jahanian50198092010-12-02 17:02:11 +00001013Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001014 assert(VD && "Passed null params");
1015 assert(VD->hasAttr<BlocksAttr>() &&
1016 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian50198092010-12-02 17:02:11 +00001017 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001018 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001019 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1020}
1021
1022/// \brief Set the copy inialization expression of a block var decl.
1023void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1024 assert(VD && Init && "Passed null params");
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001025 assert(VD->hasAttr<BlocksAttr>() &&
1026 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001027 BlockVarCopyInits[VD] = Init;
1028}
1029
John McCallbcd03502009-12-07 02:54:59 +00001030/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001031///
John McCallbcd03502009-12-07 02:54:59 +00001032/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001033/// the TypeLoc wrappers.
1034///
1035/// \param T the type that will be the basis for type source info. This type
1036/// should refer to how the declarator was written in source code, not to
1037/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +00001038TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +00001039 unsigned DataSize) {
1040 if (!DataSize)
1041 DataSize = TypeLoc::getFullDataSizeForType(T);
1042 else
1043 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +00001044 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +00001045
John McCallbcd03502009-12-07 02:54:59 +00001046 TypeSourceInfo *TInfo =
1047 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1048 new (TInfo) TypeSourceInfo(T);
1049 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001050}
1051
John McCallbcd03502009-12-07 02:54:59 +00001052TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +00001053 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +00001054 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +00001055 DI->getTypeLoc().initialize(L);
1056 return DI;
1057}
1058
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001059const ASTRecordLayout &
1060ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1061 return getObjCLayout(D, 0);
1062}
1063
1064const ASTRecordLayout &
1065ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1066 return getObjCLayout(D->getClassInterface(), D);
1067}
1068
Chris Lattner983a8bb2007-07-13 22:13:22 +00001069//===----------------------------------------------------------------------===//
1070// Type creation/memoization methods
1071//===----------------------------------------------------------------------===//
1072
John McCall8ccfcb52009-09-24 19:53:00 +00001073QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1074 unsigned Fast = Quals.getFastQualifiers();
1075 Quals.removeFastQualifiers();
1076
1077 // Check if we've already instantiated this type.
1078 llvm::FoldingSetNodeID ID;
1079 ExtQuals::Profile(ID, TypeNode, Quals);
1080 void *InsertPos = 0;
1081 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1082 assert(EQ->getQualifiers() == Quals);
1083 QualType T = QualType(EQ, Fast);
1084 return T;
1085 }
1086
John McCall90d1c2d2009-09-24 23:30:46 +00001087 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001088 ExtQualNodes.InsertNode(New, InsertPos);
1089 QualType T = QualType(New, Fast);
1090 return T;
1091}
1092
1093QualType ASTContext::getVolatileType(QualType T) {
1094 QualType CanT = getCanonicalType(T);
1095 if (CanT.isVolatileQualified()) return T;
1096
1097 QualifierCollector Quals;
1098 const Type *TypeNode = Quals.strip(T);
1099 Quals.addVolatile();
1100
1101 return getExtQualType(TypeNode, Quals);
1102}
1103
Fariborz Jahanianece85822009-02-17 18:27:45 +00001104QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001105 QualType CanT = getCanonicalType(T);
1106 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001107 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001108
John McCall8ccfcb52009-09-24 19:53:00 +00001109 // If we are composing extended qualifiers together, merge together
1110 // into one ExtQuals node.
1111 QualifierCollector Quals;
1112 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001113
John McCall8ccfcb52009-09-24 19:53:00 +00001114 // If this type already has an address space specified, it cannot get
1115 // another one.
1116 assert(!Quals.hasAddressSpace() &&
1117 "Type cannot be in multiple addr spaces!");
1118 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001119
John McCall8ccfcb52009-09-24 19:53:00 +00001120 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001121}
1122
Chris Lattnerd60183d2009-02-18 22:53:11 +00001123QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001124 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001125 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001126 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001127 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001128
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001129 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001130 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001131 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001132 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1133 return getPointerType(ResultType);
1134 }
1135 }
Mike Stump11289f42009-09-09 15:08:12 +00001136
John McCall8ccfcb52009-09-24 19:53:00 +00001137 // If we are composing extended qualifiers together, merge together
1138 // into one ExtQuals node.
1139 QualifierCollector Quals;
1140 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001141
John McCall8ccfcb52009-09-24 19:53:00 +00001142 // If this type already has an ObjCGC specified, it cannot get
1143 // another one.
1144 assert(!Quals.hasObjCGCAttr() &&
1145 "Type cannot have multiple ObjCGCs!");
1146 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001147
John McCall8ccfcb52009-09-24 19:53:00 +00001148 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001149}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001150
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001151static QualType getExtFunctionType(ASTContext& Context, QualType T,
1152 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001153 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001154 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1155 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001156 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001157 if (ResultType == Pointee)
1158 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001159
1160 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001161 } else if (const BlockPointerType *BlockPointer
1162 = T->getAs<BlockPointerType>()) {
1163 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001164 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001165 if (ResultType == Pointee)
1166 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001167
1168 ResultType = Context.getBlockPointerType(ResultType);
Douglas Gregoreece0ea2010-09-01 16:29:03 +00001169 } else if (const MemberPointerType *MemberPointer
1170 = T->getAs<MemberPointerType>()) {
1171 QualType Pointee = MemberPointer->getPointeeType();
1172 ResultType = getExtFunctionType(Context, Pointee, Info);
1173 if (ResultType == Pointee)
1174 return T;
1175
1176 ResultType = Context.getMemberPointerType(ResultType,
1177 MemberPointer->getClass());
Douglas Gregor8c940862010-01-18 17:14:39 +00001178 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001179 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001180 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001181
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001182 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001183 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001184 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001185 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001186 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001187 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001188 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1189 FPT->getNumArgs(), FPT->isVariadic(),
1190 FPT->getTypeQuals(),
1191 FPT->hasExceptionSpec(),
1192 FPT->hasAnyExceptionSpec(),
1193 FPT->getNumExceptions(),
1194 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001195 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001196 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001197 } else
1198 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001199
1200 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1201}
1202
1203QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001204 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001205 return getExtFunctionType(*this, T,
1206 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001207}
1208
1209QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001210 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001211 return getExtFunctionType(*this, T,
1212 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001213}
1214
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001215QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1216 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1217 return getExtFunctionType(*this, T,
1218 Info.withRegParm(RegParm));
1219}
1220
Chris Lattnerc6395932007-06-22 20:56:16 +00001221/// getComplexType - Return the uniqued reference to the type for a complex
1222/// number with the specified element type.
1223QualType ASTContext::getComplexType(QualType T) {
1224 // Unique pointers, to guarantee there is only one pointer of a particular
1225 // structure.
1226 llvm::FoldingSetNodeID ID;
1227 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001228
Chris Lattnerc6395932007-06-22 20:56:16 +00001229 void *InsertPos = 0;
1230 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1231 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001232
Chris Lattnerc6395932007-06-22 20:56:16 +00001233 // If the pointee type isn't canonical, this won't be a canonical type either,
1234 // so fill in the canonical type field.
1235 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001236 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001237 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001238
Chris Lattnerc6395932007-06-22 20:56:16 +00001239 // Get the new insert position for the node we care about.
1240 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001241 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001242 }
John McCall90d1c2d2009-09-24 23:30:46 +00001243 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001244 Types.push_back(New);
1245 ComplexTypes.InsertNode(New, InsertPos);
1246 return QualType(New, 0);
1247}
1248
Chris Lattner970e54e2006-11-12 00:37:36 +00001249/// getPointerType - Return the uniqued reference to the type for a pointer to
1250/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001251QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001252 // Unique pointers, to guarantee there is only one pointer of a particular
1253 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001254 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001255 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001256
Chris Lattner67521df2007-01-27 01:29:36 +00001257 void *InsertPos = 0;
1258 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001259 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001260
Chris Lattner7ccecb92006-11-12 08:50:50 +00001261 // If the pointee type isn't canonical, this won't be a canonical type either,
1262 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001263 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001264 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001265 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chris Lattner67521df2007-01-27 01:29:36 +00001267 // Get the new insert position for the node we care about.
1268 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001269 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001270 }
John McCall90d1c2d2009-09-24 23:30:46 +00001271 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001272 Types.push_back(New);
1273 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001274 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001275}
1276
Mike Stump11289f42009-09-09 15:08:12 +00001277/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001278/// a pointer to the specified block.
1279QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001280 assert(T->isFunctionType() && "block of function types only");
1281 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001282 // structure.
1283 llvm::FoldingSetNodeID ID;
1284 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001285
Steve Naroffec33ed92008-08-27 16:04:49 +00001286 void *InsertPos = 0;
1287 if (BlockPointerType *PT =
1288 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1289 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001290
1291 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001292 // type either so fill in the canonical type field.
1293 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001294 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001295 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001296
Steve Naroffec33ed92008-08-27 16:04:49 +00001297 // Get the new insert position for the node we care about.
1298 BlockPointerType *NewIP =
1299 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001300 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001301 }
John McCall90d1c2d2009-09-24 23:30:46 +00001302 BlockPointerType *New
1303 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001304 Types.push_back(New);
1305 BlockPointerTypes.InsertNode(New, InsertPos);
1306 return QualType(New, 0);
1307}
1308
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001309/// getLValueReferenceType - Return the uniqued reference to the type for an
1310/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001311QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001312 // Unique pointers, to guarantee there is only one pointer of a particular
1313 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001314 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001315 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001316
1317 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001318 if (LValueReferenceType *RT =
1319 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001320 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001321
John McCallfc93cf92009-10-22 22:37:11 +00001322 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1323
Bill Wendling3708c182007-05-27 10:15:43 +00001324 // If the referencee type isn't canonical, this won't be a canonical type
1325 // either, so fill in the canonical type field.
1326 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001327 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1328 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1329 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001330
Bill Wendling3708c182007-05-27 10:15:43 +00001331 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001332 LValueReferenceType *NewIP =
1333 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001334 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001335 }
1336
John McCall90d1c2d2009-09-24 23:30:46 +00001337 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001338 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1339 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001340 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001341 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001342
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001343 return QualType(New, 0);
1344}
1345
1346/// getRValueReferenceType - Return the uniqued reference to the type for an
1347/// rvalue reference to the specified type.
1348QualType ASTContext::getRValueReferenceType(QualType T) {
1349 // Unique pointers, to guarantee there is only one pointer of a particular
1350 // structure.
1351 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001352 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001353
1354 void *InsertPos = 0;
1355 if (RValueReferenceType *RT =
1356 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1357 return QualType(RT, 0);
1358
John McCallfc93cf92009-10-22 22:37:11 +00001359 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1360
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001361 // If the referencee type isn't canonical, this won't be a canonical type
1362 // either, so fill in the canonical type field.
1363 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001364 if (InnerRef || !T.isCanonical()) {
1365 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1366 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001367
1368 // Get the new insert position for the node we care about.
1369 RValueReferenceType *NewIP =
1370 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1371 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1372 }
1373
John McCall90d1c2d2009-09-24 23:30:46 +00001374 RValueReferenceType *New
1375 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001376 Types.push_back(New);
1377 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001378 return QualType(New, 0);
1379}
1380
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001381/// getMemberPointerType - Return the uniqued reference to the type for a
1382/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001383QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001384 // Unique pointers, to guarantee there is only one pointer of a particular
1385 // structure.
1386 llvm::FoldingSetNodeID ID;
1387 MemberPointerType::Profile(ID, T, Cls);
1388
1389 void *InsertPos = 0;
1390 if (MemberPointerType *PT =
1391 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1392 return QualType(PT, 0);
1393
1394 // If the pointee or class type isn't canonical, this won't be a canonical
1395 // type either, so fill in the canonical type field.
1396 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001397 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001398 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1399
1400 // Get the new insert position for the node we care about.
1401 MemberPointerType *NewIP =
1402 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1403 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1404 }
John McCall90d1c2d2009-09-24 23:30:46 +00001405 MemberPointerType *New
1406 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001407 Types.push_back(New);
1408 MemberPointerTypes.InsertNode(New, InsertPos);
1409 return QualType(New, 0);
1410}
1411
Mike Stump11289f42009-09-09 15:08:12 +00001412/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001413/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001414QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001415 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001416 ArrayType::ArraySizeModifier ASM,
1417 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001418 assert((EltTy->isDependentType() ||
1419 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001420 "Constant array of VLAs is illegal!");
1421
Chris Lattnere2df3f92009-05-13 04:12:56 +00001422 // Convert the array size into a canonical width matching the pointer size for
1423 // the target.
1424 llvm::APInt ArySize(ArySizeIn);
1425 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001426
Chris Lattner23b7eb62007-06-15 23:05:46 +00001427 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001428 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001429
Chris Lattner36f8e652007-01-27 08:31:04 +00001430 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001431 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001432 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001433 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001434
Chris Lattner7ccecb92006-11-12 08:50:50 +00001435 // If the element type isn't canonical, this won't be a canonical type either,
1436 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001437 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001438 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001439 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001440 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001441 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001442 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001443 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001444 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001445 }
Mike Stump11289f42009-09-09 15:08:12 +00001446
John McCall90d1c2d2009-09-24 23:30:46 +00001447 ConstantArrayType *New = new(*this,TypeAlignment)
1448 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001449 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001450 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001451 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001452}
1453
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001454/// getIncompleteArrayType - Returns a unique reference to the type for a
1455/// incomplete array of the specified element type.
1456QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) {
1457 QualType ElemTy = getBaseElementType(Ty);
1458 DeclarationName Name;
1459 llvm::SmallVector<QualType, 8> ATypes;
1460 QualType ATy = Ty;
1461 while (const ArrayType *AT = getAsArrayType(ATy)) {
1462 ATypes.push_back(ATy);
1463 ATy = AT->getElementType();
1464 }
1465 for (int i = ATypes.size() - 1; i >= 0; i--) {
1466 if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) {
1467 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star,
1468 0, VAT->getBracketsRange());
1469 }
1470 else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) {
1471 llvm::APSInt ConstVal(CAT->getSize());
1472 ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0);
1473 }
1474 else if (getAsIncompleteArrayType(ATypes[i])) {
1475 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal,
1476 0, SourceRange());
1477 }
1478 else
1479 assert(false && "DependentArrayType is seen");
1480 }
1481 return ElemTy;
1482}
1483
1484/// getVariableArrayDecayedType - Returns a vla type where known sizes
1485/// are replaced with [*]
1486QualType ASTContext::getVariableArrayDecayedType(QualType Ty) {
1487 if (Ty->isPointerType()) {
1488 QualType BaseType = Ty->getAs<PointerType>()->getPointeeType();
1489 if (isa<VariableArrayType>(BaseType)) {
1490 ArrayType *AT = dyn_cast<ArrayType>(BaseType);
1491 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1492 if (VAT->getSizeExpr()) {
1493 Ty = getUnknownSizeVariableArrayType(BaseType);
1494 Ty = getPointerType(Ty);
1495 }
1496 }
1497 }
1498 return Ty;
1499}
1500
1501
Steve Naroffcadebd02007-08-30 18:14:25 +00001502/// getVariableArrayType - Returns a non-unique reference to the type for a
1503/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001504QualType ASTContext::getVariableArrayType(QualType EltTy,
1505 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001506 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001507 unsigned EltTypeQuals,
1508 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001509 // Since we don't unique expressions, it isn't possible to unique VLA's
1510 // that have an expression provided for their size.
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001511 QualType CanonType;
1512
1513 if (!EltTy.isCanonical()) {
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001514 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1515 EltTypeQuals, Brackets);
1516 }
1517
John McCall90d1c2d2009-09-24 23:30:46 +00001518 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001519 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001520
1521 VariableArrayTypes.push_back(New);
1522 Types.push_back(New);
1523 return QualType(New, 0);
1524}
1525
Douglas Gregor4619e432008-12-05 23:32:09 +00001526/// getDependentSizedArrayType - Returns a non-unique reference to
1527/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001528/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001529QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1530 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001531 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001532 unsigned EltTypeQuals,
1533 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001534 assert((!NumElts || NumElts->isTypeDependent() ||
1535 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001536 "Size must be type- or value-dependent!");
1537
Douglas Gregorf3f95522009-07-31 00:23:35 +00001538 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001539 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001540 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001541
Douglas Gregorf86c9392010-10-26 00:51:02 +00001542 QualType CanonicalEltTy = getCanonicalType(EltTy);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001543 if (NumElts) {
1544 // Dependently-sized array types that do not have a specified
1545 // number of elements will have their sizes deduced from an
1546 // initializer.
Douglas Gregorf86c9392010-10-26 00:51:02 +00001547 DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM,
Douglas Gregorad2956c2009-11-19 18:03:26 +00001548 EltTypeQuals, NumElts);
1549
1550 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1551 }
1552
Douglas Gregorf3f95522009-07-31 00:23:35 +00001553 DependentSizedArrayType *New;
1554 if (Canon) {
1555 // We already have a canonical version of this array type; use it as
1556 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001557 New = new (*this, TypeAlignment)
1558 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1559 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf86c9392010-10-26 00:51:02 +00001560 } else if (CanonicalEltTy == EltTy) {
1561 // This is a canonical type. Record it.
1562 New = new (*this, TypeAlignment)
1563 DependentSizedArrayType(*this, EltTy, QualType(),
1564 NumElts, ASM, EltTypeQuals, Brackets);
1565
1566 if (NumElts) {
1567#ifndef NDEBUG
1568 DependentSizedArrayType *CanonCheck
1569 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1570 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1571 (void)CanonCheck;
1572#endif
1573 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001574 }
Douglas Gregorf86c9392010-10-26 00:51:02 +00001575 } else {
1576 QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts,
1577 ASM, EltTypeQuals,
1578 SourceRange());
1579 New = new (*this, TypeAlignment)
1580 DependentSizedArrayType(*this, EltTy, Canon,
1581 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001582 }
Mike Stump11289f42009-09-09 15:08:12 +00001583
Douglas Gregor4619e432008-12-05 23:32:09 +00001584 Types.push_back(New);
1585 return QualType(New, 0);
1586}
1587
Eli Friedmanbd258282008-02-15 18:16:39 +00001588QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1589 ArrayType::ArraySizeModifier ASM,
1590 unsigned EltTypeQuals) {
1591 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001592 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001593
1594 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001595 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001596 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1597 return QualType(ATP, 0);
1598
1599 // If the element type isn't canonical, this won't be a canonical type
1600 // either, so fill in the canonical type field.
1601 QualType Canonical;
1602
John McCallb692a092009-10-22 20:10:53 +00001603 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001604 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001605 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001606
1607 // Get the new insert position for the node we care about.
1608 IncompleteArrayType *NewIP =
1609 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001610 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001611 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001612
John McCall90d1c2d2009-09-24 23:30:46 +00001613 IncompleteArrayType *New = new (*this, TypeAlignment)
1614 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001615
1616 IncompleteArrayTypes.InsertNode(New, InsertPos);
1617 Types.push_back(New);
1618 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001619}
1620
Steve Naroff91fcddb2007-07-18 18:00:27 +00001621/// getVectorType - Return the unique reference to a vector type of
1622/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001623QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001624 VectorType::VectorKind VecKind) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00001625 BuiltinType *BaseType;
Mike Stump11289f42009-09-09 15:08:12 +00001626
Chris Lattnerdc226c22010-10-01 22:42:38 +00001627 BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1628 assert(BaseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001629
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001630 // Check if we've already instantiated a vector of this type.
1631 llvm::FoldingSetNodeID ID;
Bob Wilsonaeb56442010-11-10 21:56:12 +00001632 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner37141f42010-06-23 06:00:24 +00001633
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001634 void *InsertPos = 0;
1635 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1636 return QualType(VTP, 0);
1637
1638 // If the element type isn't canonical, this won't be a canonical type either,
1639 // so fill in the canonical type field.
1640 QualType Canonical;
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00001641 if (!vecType.isCanonical()) {
Bob Wilson77954802010-11-16 00:32:20 +00001642 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump11289f42009-09-09 15:08:12 +00001643
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001644 // Get the new insert position for the node we care about.
1645 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001646 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001647 }
John McCall90d1c2d2009-09-24 23:30:46 +00001648 VectorType *New = new (*this, TypeAlignment)
Bob Wilsonaeb56442010-11-10 21:56:12 +00001649 VectorType(vecType, NumElts, Canonical, VecKind);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001650 VectorTypes.InsertNode(New, InsertPos);
1651 Types.push_back(New);
1652 return QualType(New, 0);
1653}
1654
Nate Begemance4d7fc2008-04-18 23:10:10 +00001655/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001656/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001657QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001658 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001659
Chris Lattner76a00cf2008-04-06 22:59:24 +00001660 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001661 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001662
Steve Naroff91fcddb2007-07-18 18:00:27 +00001663 // Check if we've already instantiated a vector of this type.
1664 llvm::FoldingSetNodeID ID;
Chris Lattner37141f42010-06-23 06:00:24 +00001665 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001666 VectorType::GenericVector);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001667 void *InsertPos = 0;
1668 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1669 return QualType(VTP, 0);
1670
1671 // If the element type isn't canonical, this won't be a canonical type either,
1672 // so fill in the canonical type field.
1673 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001674 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001675 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001676
Steve Naroff91fcddb2007-07-18 18:00:27 +00001677 // Get the new insert position for the node we care about.
1678 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001679 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001680 }
John McCall90d1c2d2009-09-24 23:30:46 +00001681 ExtVectorType *New = new (*this, TypeAlignment)
1682 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001683 VectorTypes.InsertNode(New, InsertPos);
1684 Types.push_back(New);
1685 return QualType(New, 0);
1686}
1687
Mike Stump11289f42009-09-09 15:08:12 +00001688QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001689 Expr *SizeExpr,
1690 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001691 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001692 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001693 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001694
Douglas Gregor352169a2009-07-31 03:54:25 +00001695 void *InsertPos = 0;
1696 DependentSizedExtVectorType *Canon
1697 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1698 DependentSizedExtVectorType *New;
1699 if (Canon) {
1700 // We already have a canonical version of this array type; use it as
1701 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001702 New = new (*this, TypeAlignment)
1703 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1704 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001705 } else {
1706 QualType CanonVecTy = getCanonicalType(vecType);
1707 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001708 New = new (*this, TypeAlignment)
1709 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1710 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001711
1712 DependentSizedExtVectorType *CanonCheck
1713 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1714 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1715 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001716 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1717 } else {
1718 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1719 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001720 New = new (*this, TypeAlignment)
1721 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001722 }
1723 }
Mike Stump11289f42009-09-09 15:08:12 +00001724
Douglas Gregor758a8692009-06-17 21:51:59 +00001725 Types.push_back(New);
1726 return QualType(New, 0);
1727}
1728
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001729/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001730///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001731QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1732 const FunctionType::ExtInfo &Info) {
1733 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001734 // Unique functions, to guarantee there is only one function of a particular
1735 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001736 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001737 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001738
Chris Lattner47955de2007-01-27 08:37:20 +00001739 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001740 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001741 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001742 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001743
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001744 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001745 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001746 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001747 Canonical =
1748 getFunctionNoProtoType(getCanonicalType(ResultTy),
1749 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001750
Chris Lattner47955de2007-01-27 08:37:20 +00001751 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001752 FunctionNoProtoType *NewIP =
1753 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001754 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001755 }
Mike Stump11289f42009-09-09 15:08:12 +00001756
John McCall90d1c2d2009-09-24 23:30:46 +00001757 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001758 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001759 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001760 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001761 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001762}
1763
1764/// getFunctionType - Return a normal function type with a typed argument
1765/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001766QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001767 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001768 unsigned TypeQuals, bool hasExceptionSpec,
1769 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001770 const QualType *ExArray,
1771 const FunctionType::ExtInfo &Info) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00001772
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001773 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001774 // Unique functions, to guarantee there is only one function of a particular
1775 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001776 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001777 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001778 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001779 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001780
1781 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001782 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001783 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001784 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001785
1786 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001787 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001788 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001789 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001790 isCanonical = false;
1791
1792 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001793 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001794 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001795 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001796 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001797 CanonicalArgs.reserve(NumArgs);
1798 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001799 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001800
Chris Lattner76a00cf2008-04-06 22:59:24 +00001801 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001802 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001803 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001804 false, 0, 0,
1805 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001806
Chris Lattnerfd4de792007-01-27 01:15:32 +00001807 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001808 FunctionProtoType *NewIP =
1809 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001810 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001811 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001812
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001813 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001814 // for two variable size arrays (for parameter and exception types) at the
1815 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001816 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001817 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1818 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001819 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001820 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001821 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001822 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001823 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001824 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001825 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001826}
Chris Lattneref51c202006-11-10 07:17:23 +00001827
John McCalle78aac42010-03-10 03:28:59 +00001828#ifndef NDEBUG
1829static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1830 if (!isa<CXXRecordDecl>(D)) return false;
1831 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1832 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1833 return true;
1834 if (RD->getDescribedClassTemplate() &&
1835 !isa<ClassTemplateSpecializationDecl>(RD))
1836 return true;
1837 return false;
1838}
1839#endif
1840
1841/// getInjectedClassNameType - Return the unique reference to the
1842/// injected class name type for the specified templated declaration.
1843QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1844 QualType TST) {
1845 assert(NeedsInjectedClassNameType(Decl));
1846 if (Decl->TypeForDecl) {
1847 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001848 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCalle78aac42010-03-10 03:28:59 +00001849 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1850 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1851 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1852 } else {
John McCall2408e322010-04-27 00:57:59 +00001853 Decl->TypeForDecl =
1854 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001855 Types.push_back(Decl->TypeForDecl);
1856 }
1857 return QualType(Decl->TypeForDecl, 0);
1858}
1859
Douglas Gregor83a586e2008-04-13 21:07:44 +00001860/// getTypeDeclType - Return the unique reference to the type for the
1861/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001862QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001863 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001864 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001865
John McCall81e38502010-02-16 03:57:14 +00001866 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001867 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001868
John McCall96f0b5f2010-03-10 06:48:02 +00001869 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1870 "Template type parameter types are always available.");
1871
John McCall81e38502010-02-16 03:57:14 +00001872 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001873 assert(!Record->getPreviousDeclaration() &&
1874 "struct/union has previous declaration");
1875 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001876 return getRecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001877 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001878 assert(!Enum->getPreviousDeclaration() &&
1879 "enum has previous declaration");
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001880 return getEnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001881 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001882 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1883 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001884 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001885 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001886
John McCall96f0b5f2010-03-10 06:48:02 +00001887 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001888 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001889}
1890
Chris Lattner32d920b2007-01-26 02:01:53 +00001891/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001892/// specified typename decl.
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001893QualType
1894ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001895 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001896
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001897 if (Canonical.isNull())
1898 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001899 Decl->TypeForDecl = new(*this, TypeAlignment)
1900 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001901 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001902 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001903}
1904
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001905QualType ASTContext::getRecordType(const RecordDecl *Decl) {
1906 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1907
1908 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
1909 if (PrevDecl->TypeForDecl)
1910 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1911
1912 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
1913 Types.push_back(Decl->TypeForDecl);
1914 return QualType(Decl->TypeForDecl, 0);
1915}
1916
1917QualType ASTContext::getEnumType(const EnumDecl *Decl) {
1918 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1919
1920 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
1921 if (PrevDecl->TypeForDecl)
1922 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1923
1924 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
1925 Types.push_back(Decl->TypeForDecl);
1926 return QualType(Decl->TypeForDecl, 0);
1927}
1928
John McCallcebee162009-10-18 09:09:24 +00001929/// \brief Retrieve a substitution-result type.
1930QualType
1931ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1932 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001933 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001934 && "replacement types must always be canonical");
1935
1936 llvm::FoldingSetNodeID ID;
1937 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1938 void *InsertPos = 0;
1939 SubstTemplateTypeParmType *SubstParm
1940 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1941
1942 if (!SubstParm) {
1943 SubstParm = new (*this, TypeAlignment)
1944 SubstTemplateTypeParmType(Parm, Replacement);
1945 Types.push_back(SubstParm);
1946 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1947 }
1948
1949 return QualType(SubstParm, 0);
1950}
1951
Douglas Gregoreff93e02009-02-05 23:33:38 +00001952/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001953/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001954/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001955QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001956 bool ParameterPack,
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001957 IdentifierInfo *Name) {
Douglas Gregoreff93e02009-02-05 23:33:38 +00001958 llvm::FoldingSetNodeID ID;
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001959 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001960 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001961 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001962 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1963
1964 if (TypeParm)
1965 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001966
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001967 if (Name) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00001968 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001969 TypeParm = new (*this, TypeAlignment)
1970 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001971
1972 TemplateTypeParmType *TypeCheck
1973 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1974 assert(!TypeCheck && "Template type parameter canonical type broken");
1975 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001976 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001977 TypeParm = new (*this, TypeAlignment)
1978 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001979
1980 Types.push_back(TypeParm);
1981 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1982
1983 return QualType(TypeParm, 0);
1984}
1985
John McCalle78aac42010-03-10 03:28:59 +00001986TypeSourceInfo *
1987ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1988 SourceLocation NameLoc,
1989 const TemplateArgumentListInfo &Args,
1990 QualType CanonType) {
1991 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1992
1993 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1994 TemplateSpecializationTypeLoc TL
1995 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1996 TL.setTemplateNameLoc(NameLoc);
1997 TL.setLAngleLoc(Args.getLAngleLoc());
1998 TL.setRAngleLoc(Args.getRAngleLoc());
1999 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2000 TL.setArgLocInfo(i, Args[i].getLocInfo());
2001 return DI;
2002}
2003
Mike Stump11289f42009-09-09 15:08:12 +00002004QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00002005ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00002006 const TemplateArgumentListInfo &Args,
John McCall30576cd2010-06-13 09:25:03 +00002007 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00002008 unsigned NumArgs = Args.size();
2009
John McCall0ad16662009-10-29 08:12:44 +00002010 llvm::SmallVector<TemplateArgument, 4> ArgVec;
2011 ArgVec.reserve(NumArgs);
2012 for (unsigned i = 0; i != NumArgs; ++i)
2013 ArgVec.push_back(Args[i].getArgument());
2014
John McCall2408e322010-04-27 00:57:59 +00002015 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00002016 Canon);
John McCall0ad16662009-10-29 08:12:44 +00002017}
2018
2019QualType
2020ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00002021 const TemplateArgument *Args,
2022 unsigned NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00002023 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00002024 if (!Canon.isNull())
2025 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002026 else
2027 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregord56a91e2009-02-26 22:19:44 +00002028
Douglas Gregora8e02e72009-07-28 23:00:59 +00002029 // Allocate the (non-canonical) template specialization type, but don't
2030 // try to unique it: these types typically have location information that
2031 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00002032 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00002033 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00002034 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00002035 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00002036 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00002037 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00002038 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00002039
Douglas Gregor8bf42052009-02-09 18:46:07 +00002040 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00002041 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002042}
2043
Mike Stump11289f42009-09-09 15:08:12 +00002044QualType
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00002045ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2046 const TemplateArgument *Args,
2047 unsigned NumArgs) {
2048 // Build the canonical template specialization type.
2049 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2050 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2051 CanonArgs.reserve(NumArgs);
2052 for (unsigned I = 0; I != NumArgs; ++I)
2053 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2054
2055 // Determine whether this canonical template specialization type already
2056 // exists.
2057 llvm::FoldingSetNodeID ID;
2058 TemplateSpecializationType::Profile(ID, CanonTemplate,
2059 CanonArgs.data(), NumArgs, *this);
2060
2061 void *InsertPos = 0;
2062 TemplateSpecializationType *Spec
2063 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2064
2065 if (!Spec) {
2066 // Allocate a new canonical template specialization type.
2067 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2068 sizeof(TemplateArgument) * NumArgs),
2069 TypeAlignment);
2070 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2071 CanonArgs.data(), NumArgs,
2072 QualType());
2073 Types.push_back(Spec);
2074 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2075 }
2076
2077 assert(Spec->isDependentType() &&
2078 "Non-dependent template-id type must have a canonical type");
2079 return QualType(Spec, 0);
2080}
2081
2082QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00002083ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2084 NestedNameSpecifier *NNS,
2085 QualType NamedType) {
Douglas Gregor52537682009-03-19 00:18:19 +00002086 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002087 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00002088
2089 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002090 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002091 if (T)
2092 return QualType(T, 0);
2093
Douglas Gregorc42075a2010-02-04 18:10:26 +00002094 QualType Canon = NamedType;
2095 if (!Canon.isCanonical()) {
2096 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002097 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2098 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00002099 (void)CheckT;
2100 }
2101
Abramo Bagnara6150c882010-05-11 21:36:43 +00002102 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00002103 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002104 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002105 return QualType(T, 0);
2106}
2107
Douglas Gregor02085352010-03-31 20:19:30 +00002108QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2109 NestedNameSpecifier *NNS,
2110 const IdentifierInfo *Name,
2111 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00002112 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2113
2114 if (Canon.isNull()) {
2115 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00002116 ElaboratedTypeKeyword CanonKeyword = Keyword;
2117 if (Keyword == ETK_None)
2118 CanonKeyword = ETK_Typename;
2119
2120 if (CanonNNS != NNS || CanonKeyword != Keyword)
2121 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002122 }
2123
2124 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002125 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002126
2127 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002128 DependentNameType *T
2129 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00002130 if (T)
2131 return QualType(T, 0);
2132
Douglas Gregor02085352010-03-31 20:19:30 +00002133 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002134 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002135 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002136 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002137}
2138
Mike Stump11289f42009-09-09 15:08:12 +00002139QualType
John McCallc392f372010-06-11 00:33:02 +00002140ASTContext::getDependentTemplateSpecializationType(
2141 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002142 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002143 const IdentifierInfo *Name,
2144 const TemplateArgumentListInfo &Args) {
2145 // TODO: avoid this copy
2146 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2147 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2148 ArgCopy.push_back(Args[I].getArgument());
2149 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2150 ArgCopy.size(),
2151 ArgCopy.data());
2152}
2153
2154QualType
2155ASTContext::getDependentTemplateSpecializationType(
2156 ElaboratedTypeKeyword Keyword,
2157 NestedNameSpecifier *NNS,
2158 const IdentifierInfo *Name,
2159 unsigned NumArgs,
2160 const TemplateArgument *Args) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002161 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2162
Douglas Gregorc42075a2010-02-04 18:10:26 +00002163 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002164 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2165 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002166
2167 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002168 DependentTemplateSpecializationType *T
2169 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002170 if (T)
2171 return QualType(T, 0);
2172
John McCallc392f372010-06-11 00:33:02 +00002173 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002174
John McCallc392f372010-06-11 00:33:02 +00002175 ElaboratedTypeKeyword CanonKeyword = Keyword;
2176 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2177
2178 bool AnyNonCanonArgs = false;
2179 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2180 for (unsigned I = 0; I != NumArgs; ++I) {
2181 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2182 if (!CanonArgs[I].structurallyEquals(Args[I]))
2183 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002184 }
2185
John McCallc392f372010-06-11 00:33:02 +00002186 QualType Canon;
2187 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2188 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2189 Name, NumArgs,
2190 CanonArgs.data());
2191
2192 // Find the insert position again.
2193 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2194 }
2195
2196 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2197 sizeof(TemplateArgument) * NumArgs),
2198 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002199 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002200 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002201 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002202 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002203 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002204}
2205
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002206/// CmpProtocolNames - Comparison predicate for sorting protocols
2207/// alphabetically.
2208static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2209 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002210 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002211}
2212
John McCall8b07ec22010-05-15 11:32:37 +00002213static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002214 unsigned NumProtocols) {
2215 if (NumProtocols == 0) return true;
2216
2217 for (unsigned i = 1; i != NumProtocols; ++i)
2218 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2219 return false;
2220 return true;
2221}
2222
2223static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002224 unsigned &NumProtocols) {
2225 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002226
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002227 // Sort protocols, keyed by name.
2228 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2229
2230 // Remove duplicates.
2231 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2232 NumProtocols = ProtocolsEnd-Protocols;
2233}
2234
John McCall8b07ec22010-05-15 11:32:37 +00002235QualType ASTContext::getObjCObjectType(QualType BaseType,
2236 ObjCProtocolDecl * const *Protocols,
2237 unsigned NumProtocols) {
2238 // If the base type is an interface and there aren't any protocols
2239 // to add, then the interface type will do just fine.
2240 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2241 return BaseType;
2242
2243 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002244 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002245 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002246 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002247 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2248 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002249
John McCall8b07ec22010-05-15 11:32:37 +00002250 // Build the canonical type, which has the canonical base type and
2251 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002252 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002253 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2254 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2255 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002256 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2257 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002258 unsigned UniqueCount = NumProtocols;
2259
John McCallfc93cf92009-10-22 22:37:11 +00002260 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002261 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2262 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002263 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002264 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2265 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002266 }
2267
2268 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002269 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2270 }
2271
2272 unsigned Size = sizeof(ObjCObjectTypeImpl);
2273 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2274 void *Mem = Allocate(Size, TypeAlignment);
2275 ObjCObjectTypeImpl *T =
2276 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2277
2278 Types.push_back(T);
2279 ObjCObjectTypes.InsertNode(T, InsertPos);
2280 return QualType(T, 0);
2281}
2282
2283/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2284/// the given object type.
2285QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2286 llvm::FoldingSetNodeID ID;
2287 ObjCObjectPointerType::Profile(ID, ObjectT);
2288
2289 void *InsertPos = 0;
2290 if (ObjCObjectPointerType *QT =
2291 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2292 return QualType(QT, 0);
2293
2294 // Find the canonical object type.
2295 QualType Canonical;
2296 if (!ObjectT.isCanonical()) {
2297 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2298
2299 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002300 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2301 }
2302
Douglas Gregorf85bee62010-02-08 22:59:26 +00002303 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002304 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2305 ObjCObjectPointerType *QType =
2306 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002307
Steve Narofffb4330f2009-06-17 22:40:22 +00002308 Types.push_back(QType);
2309 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002310 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002311}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002312
Douglas Gregor1c283312010-08-11 12:19:30 +00002313/// getObjCInterfaceType - Return the unique reference to the type for the
2314/// specified ObjC interface decl. The list of protocols is optional.
2315QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2316 if (Decl->TypeForDecl)
2317 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002318
Douglas Gregor1c283312010-08-11 12:19:30 +00002319 // FIXME: redeclarations?
2320 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2321 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2322 Decl->TypeForDecl = T;
2323 Types.push_back(T);
2324 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002325}
2326
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002327/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2328/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002329/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002330/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002331/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002332QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002333 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002334 if (tofExpr->isTypeDependent()) {
2335 llvm::FoldingSetNodeID ID;
2336 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002337
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002338 void *InsertPos = 0;
2339 DependentTypeOfExprType *Canon
2340 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2341 if (Canon) {
2342 // We already have a "canonical" version of an identical, dependent
2343 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002344 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002345 QualType((TypeOfExprType*)Canon, 0));
2346 }
2347 else {
2348 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002349 Canon
2350 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002351 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2352 toe = Canon;
2353 }
2354 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002355 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002356 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002357 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002358 Types.push_back(toe);
2359 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002360}
2361
Steve Naroffa773cd52007-08-01 18:02:17 +00002362/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2363/// TypeOfType AST's. The only motivation to unique these nodes would be
2364/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002365/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002366/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002367QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002368 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002369 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002370 Types.push_back(tot);
2371 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002372}
2373
Anders Carlssonad6bd352009-06-24 21:24:56 +00002374/// getDecltypeForExpr - Given an expr, will return the decltype for that
2375/// expression, according to the rules in C++0x [dcl.type.simple]p4
2376static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002377 if (e->isTypeDependent())
2378 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002379
Anders Carlssonad6bd352009-06-24 21:24:56 +00002380 // If e is an id expression or a class member access, decltype(e) is defined
2381 // as the type of the entity named by e.
2382 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2383 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2384 return VD->getType();
2385 }
2386 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2387 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2388 return FD->getType();
2389 }
2390 // If e is a function call or an invocation of an overloaded operator,
2391 // (parentheses around e are ignored), decltype(e) is defined as the
2392 // return type of that function.
2393 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2394 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002395
Anders Carlssonad6bd352009-06-24 21:24:56 +00002396 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002397
2398 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002399 // defined as T&, otherwise decltype(e) is defined as T.
John McCall086a4642010-11-24 05:12:34 +00002400 if (e->isLValue())
Anders Carlssonad6bd352009-06-24 21:24:56 +00002401 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002402
Anders Carlssonad6bd352009-06-24 21:24:56 +00002403 return T;
2404}
2405
Anders Carlsson81df7b82009-06-24 19:06:50 +00002406/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2407/// DecltypeType AST's. The only motivation to unique these nodes would be
2408/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002409/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002410/// on canonical type's (which are always unique).
2411QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002412 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002413 if (e->isTypeDependent()) {
2414 llvm::FoldingSetNodeID ID;
2415 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002416
Douglas Gregora21f6c32009-07-30 23:36:40 +00002417 void *InsertPos = 0;
2418 DependentDecltypeType *Canon
2419 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2420 if (Canon) {
2421 // We already have a "canonical" version of an equivalent, dependent
2422 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002423 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002424 QualType((DecltypeType*)Canon, 0));
2425 }
2426 else {
2427 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002428 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002429 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2430 dt = Canon;
2431 }
2432 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002433 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002434 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002435 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002436 Types.push_back(dt);
2437 return QualType(dt, 0);
2438}
2439
Chris Lattnerfb072462007-01-23 05:45:31 +00002440/// getTagDeclType - Return the unique reference to the type for the
2441/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002442QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002443 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002444 // FIXME: What is the design on getTagDeclType when it requires casting
2445 // away const? mutable?
2446 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002447}
2448
Mike Stump11289f42009-09-09 15:08:12 +00002449/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2450/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2451/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002452CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002453 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002454}
Chris Lattnerfb072462007-01-23 05:45:31 +00002455
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002456/// getSignedWCharType - Return the type of "signed wchar_t".
2457/// Used when in C++, as a GCC extension.
2458QualType ASTContext::getSignedWCharType() const {
2459 // FIXME: derive from "Target" ?
2460 return WCharTy;
2461}
2462
2463/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2464/// Used when in C++, as a GCC extension.
2465QualType ASTContext::getUnsignedWCharType() const {
2466 // FIXME: derive from "Target" ?
2467 return UnsignedIntTy;
2468}
2469
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002470/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2471/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2472QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002473 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002474}
2475
Chris Lattnera21ad802008-04-02 05:18:44 +00002476//===----------------------------------------------------------------------===//
2477// Type Operators
2478//===----------------------------------------------------------------------===//
2479
John McCallfc93cf92009-10-22 22:37:11 +00002480CanQualType ASTContext::getCanonicalParamType(QualType T) {
2481 // Push qualifiers into arrays, and then discard any remaining
2482 // qualifiers.
2483 T = getCanonicalType(T);
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00002484 T = getVariableArrayDecayedType(T);
John McCallfc93cf92009-10-22 22:37:11 +00002485 const Type *Ty = T.getTypePtr();
John McCallfc93cf92009-10-22 22:37:11 +00002486 QualType Result;
2487 if (isa<ArrayType>(Ty)) {
2488 Result = getArrayDecayedType(QualType(Ty,0));
2489 } else if (isa<FunctionType>(Ty)) {
2490 Result = getPointerType(QualType(Ty, 0));
2491 } else {
2492 Result = QualType(Ty, 0);
2493 }
2494
2495 return CanQualType::CreateUnsafe(Result);
2496}
2497
Chris Lattnered0d0792008-04-06 22:41:35 +00002498/// getCanonicalType - Return the canonical (structural) type corresponding to
2499/// the specified potentially non-canonical type. The non-canonical version
2500/// of a type may have many "decorated" versions of types. Decorators can
2501/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2502/// to be free of any of these, allowing two canonical types to be compared
2503/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002504CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002505 QualifierCollector Quals;
2506 const Type *Ptr = Quals.strip(T);
2507 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002508
John McCall8ccfcb52009-09-24 19:53:00 +00002509 // The canonical internal type will be the canonical type *except*
2510 // that we push type qualifiers down through array types.
2511
2512 // If there are no new qualifiers to push down, stop here.
2513 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002514 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002515
John McCall8ccfcb52009-09-24 19:53:00 +00002516 // If the type qualifiers are on an array type, get the canonical
2517 // type of the array with the qualifiers applied to the element
2518 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002519 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2520 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002521 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002522
Chris Lattner7adf0762008-08-04 07:31:14 +00002523 // Get the canonical version of the element with the extra qualifiers on it.
2524 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002525 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002526 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002527
Chris Lattner7adf0762008-08-04 07:31:14 +00002528 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002529 return CanQualType::CreateUnsafe(
2530 getConstantArrayType(NewEltTy, CAT->getSize(),
2531 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002532 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002533 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002534 return CanQualType::CreateUnsafe(
2535 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002536 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002537
Douglas Gregor4619e432008-12-05 23:32:09 +00002538 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002539 return CanQualType::CreateUnsafe(
2540 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002541 DSAT->getSizeExpr(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002542 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002543 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002544 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002545
Chris Lattner7adf0762008-08-04 07:31:14 +00002546 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002547 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002548 VAT->getSizeExpr(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002549 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002550 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002551 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002552}
2553
Chandler Carruth607f38e2009-12-29 07:16:59 +00002554QualType ASTContext::getUnqualifiedArrayType(QualType T,
2555 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002556 Quals = T.getQualifiers();
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002557 const ArrayType *AT = getAsArrayType(T);
2558 if (!AT) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002559 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002560 }
2561
Chandler Carruth607f38e2009-12-29 07:16:59 +00002562 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002563 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002564 if (Elt == UnqualElt)
2565 return T;
2566
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002567 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002568 return getConstantArrayType(UnqualElt, CAT->getSize(),
2569 CAT->getSizeModifier(), 0);
2570 }
2571
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002572 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002573 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2574 }
2575
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002576 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2577 return getVariableArrayType(UnqualElt,
John McCallc3007a22010-10-26 07:05:15 +00002578 VAT->getSizeExpr(),
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002579 VAT->getSizeModifier(),
2580 VAT->getIndexTypeCVRQualifiers(),
2581 VAT->getBracketsRange());
2582 }
2583
2584 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCallc3007a22010-10-26 07:05:15 +00002585 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002586 DSAT->getSizeModifier(), 0,
2587 SourceRange());
2588}
2589
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002590/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2591/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2592/// they point to and return true. If T1 and T2 aren't pointer types
2593/// or pointer-to-member types, or if they are not similar at this
2594/// level, returns false and leaves T1 and T2 unchanged. Top-level
2595/// qualifiers on T1 and T2 are ignored. This function will typically
2596/// be called in a loop that successively "unwraps" pointer and
2597/// pointer-to-member types to compare them at each level.
2598bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2599 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2600 *T2PtrType = T2->getAs<PointerType>();
2601 if (T1PtrType && T2PtrType) {
2602 T1 = T1PtrType->getPointeeType();
2603 T2 = T2PtrType->getPointeeType();
2604 return true;
2605 }
2606
2607 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2608 *T2MPType = T2->getAs<MemberPointerType>();
2609 if (T1MPType && T2MPType &&
2610 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2611 QualType(T2MPType->getClass(), 0))) {
2612 T1 = T1MPType->getPointeeType();
2613 T2 = T2MPType->getPointeeType();
2614 return true;
2615 }
2616
2617 if (getLangOptions().ObjC1) {
2618 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2619 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2620 if (T1OPType && T2OPType) {
2621 T1 = T1OPType->getPointeeType();
2622 T2 = T2OPType->getPointeeType();
2623 return true;
2624 }
2625 }
2626
2627 // FIXME: Block pointers, too?
2628
2629 return false;
2630}
2631
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002632DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name,
2633 SourceLocation NameLoc) {
John McCall847e2a12009-11-24 18:42:40 +00002634 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002635 // DNInfo work in progress: CHECKME: what about DNLoc?
2636 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2637
John McCall847e2a12009-11-24 18:42:40 +00002638 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002639 DeclarationName DName;
John McCall847e2a12009-11-24 18:42:40 +00002640 if (DTN->isIdentifier()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002641 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2642 return DeclarationNameInfo(DName, NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002643 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002644 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2645 // DNInfo work in progress: FIXME: source locations?
2646 DeclarationNameLoc DNLoc;
2647 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2648 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2649 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall847e2a12009-11-24 18:42:40 +00002650 }
2651 }
2652
John McCalld28ae272009-12-02 08:04:21 +00002653 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2654 assert(Storage);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002655 // DNInfo work in progress: CHECKME: what about DNLoc?
2656 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002657}
2658
Douglas Gregor6bc50582009-05-07 06:41:52 +00002659TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002660 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2661 if (TemplateTemplateParmDecl *TTP
2662 = dyn_cast<TemplateTemplateParmDecl>(Template))
2663 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2664
2665 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002666 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002667 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002668
John McCalld28ae272009-12-02 08:04:21 +00002669 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002670
Douglas Gregor6bc50582009-05-07 06:41:52 +00002671 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2672 assert(DTN && "Non-dependent template names must refer to template decls.");
2673 return DTN->CanonicalTemplateName;
2674}
2675
Douglas Gregoradee3e32009-11-11 23:06:43 +00002676bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2677 X = getCanonicalTemplateName(X);
2678 Y = getCanonicalTemplateName(Y);
2679 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2680}
2681
Mike Stump11289f42009-09-09 15:08:12 +00002682TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002683ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2684 switch (Arg.getKind()) {
2685 case TemplateArgument::Null:
2686 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002687
Douglas Gregora8e02e72009-07-28 23:00:59 +00002688 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002689 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002690
Douglas Gregora8e02e72009-07-28 23:00:59 +00002691 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002692 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002693
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002694 case TemplateArgument::Template:
2695 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2696
Douglas Gregora8e02e72009-07-28 23:00:59 +00002697 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002698 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002699 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002700
Douglas Gregora8e02e72009-07-28 23:00:59 +00002701 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002702 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002703
Douglas Gregora8e02e72009-07-28 23:00:59 +00002704 case TemplateArgument::Pack: {
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002705 TemplateArgument *CanonArgs
2706 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregora8e02e72009-07-28 23:00:59 +00002707 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002708 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002709 AEnd = Arg.pack_end();
2710 A != AEnd; (void)++A, ++Idx)
2711 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002712
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002713 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregora8e02e72009-07-28 23:00:59 +00002714 }
2715 }
2716
2717 // Silence GCC warning
2718 assert(false && "Unhandled template argument kind");
2719 return TemplateArgument();
2720}
2721
Douglas Gregor333489b2009-03-27 23:10:48 +00002722NestedNameSpecifier *
2723ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002724 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002725 return 0;
2726
2727 switch (NNS->getKind()) {
2728 case NestedNameSpecifier::Identifier:
2729 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002730 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002731 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2732 NNS->getAsIdentifier());
2733
2734 case NestedNameSpecifier::Namespace:
2735 // A namespace is canonical; build a nested-name-specifier with
2736 // this namespace and no prefix.
2737 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2738
2739 case NestedNameSpecifier::TypeSpec:
2740 case NestedNameSpecifier::TypeSpecWithTemplate: {
2741 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor3ade5702010-11-04 00:09:33 +00002742
2743 // If we have some kind of dependent-named type (e.g., "typename T::type"),
2744 // break it apart into its prefix and identifier, then reconsititute those
2745 // as the canonical nested-name-specifier. This is required to canonicalize
2746 // a dependent nested-name-specifier involving typedefs of dependent-name
2747 // types, e.g.,
2748 // typedef typename T::type T1;
2749 // typedef typename T1::type T2;
2750 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
2751 NestedNameSpecifier *Prefix
2752 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
2753 return NestedNameSpecifier::Create(*this, Prefix,
2754 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
2755 }
2756
Douglas Gregorcc10cbf2010-11-04 00:14:23 +00002757 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor3ade5702010-11-04 00:09:33 +00002758 if (const DependentTemplateSpecializationType *DTST
2759 = T->getAs<DependentTemplateSpecializationType>()) {
2760 NestedNameSpecifier *Prefix
2761 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
2762 TemplateName Name
2763 = getDependentTemplateName(Prefix, DTST->getIdentifier());
2764 T = getTemplateSpecializationType(Name,
2765 DTST->getArgs(), DTST->getNumArgs());
2766 T = getCanonicalType(T);
2767 }
2768
2769 return NestedNameSpecifier::Create(*this, 0, false, T.getTypePtr());
Douglas Gregor333489b2009-03-27 23:10:48 +00002770 }
2771
2772 case NestedNameSpecifier::Global:
2773 // The global specifier is canonical and unique.
2774 return NNS;
2775 }
2776
2777 // Required to silence a GCC warning
2778 return 0;
2779}
2780
Chris Lattner7adf0762008-08-04 07:31:14 +00002781
2782const ArrayType *ASTContext::getAsArrayType(QualType T) {
2783 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002784 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002785 // Handle the common positive case fast.
2786 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2787 return AT;
2788 }
Mike Stump11289f42009-09-09 15:08:12 +00002789
John McCall8ccfcb52009-09-24 19:53:00 +00002790 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002791 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002792 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002793 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002794
John McCall8ccfcb52009-09-24 19:53:00 +00002795 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002796 // implements C99 6.7.3p8: "If the specification of an array type includes
2797 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002798
Chris Lattner7adf0762008-08-04 07:31:14 +00002799 // If we get here, we either have type qualifiers on the type, or we have
2800 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002801 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002802
John McCall8ccfcb52009-09-24 19:53:00 +00002803 QualifierCollector Qs;
2804 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002805
Chris Lattner7adf0762008-08-04 07:31:14 +00002806 // If we have a simple case, just return now.
2807 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002808 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002809 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002810
Chris Lattner7adf0762008-08-04 07:31:14 +00002811 // Otherwise, we have an array and we have qualifiers on it. Push the
2812 // qualifiers into the array element type and return a new array type.
2813 // Get the canonical version of the element with the extra qualifiers on it.
2814 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002815 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002816
Chris Lattner7adf0762008-08-04 07:31:14 +00002817 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2818 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2819 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002820 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002821 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2822 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2823 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002824 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002825
Mike Stump11289f42009-09-09 15:08:12 +00002826 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002827 = dyn_cast<DependentSizedArrayType>(ATy))
2828 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002829 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002830 DSAT->getSizeExpr(),
Douglas Gregor4619e432008-12-05 23:32:09 +00002831 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002832 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002833 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002834
Chris Lattner7adf0762008-08-04 07:31:14 +00002835 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002836 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002837 VAT->getSizeExpr(),
Chris Lattner7adf0762008-08-04 07:31:14 +00002838 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002839 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002840 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002841}
2842
Chris Lattnera21ad802008-04-02 05:18:44 +00002843/// getArrayDecayedType - Return the properly qualified result of decaying the
2844/// specified array type to a pointer. This operation is non-trivial when
2845/// handling typedefs etc. The canonical type of "T" must be an array type,
2846/// this returns a pointer to a properly qualified element of the array.
2847///
2848/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2849QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002850 // Get the element type with 'getAsArrayType' so that we don't lose any
2851 // typedefs in the element type of the array. This also handles propagation
2852 // of type qualifiers from the array type into the element type if present
2853 // (C99 6.7.3p8).
2854 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2855 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002856
Chris Lattner7adf0762008-08-04 07:31:14 +00002857 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002858
2859 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002860 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002861}
2862
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002863QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002864 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002865 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2866 QT = AT->getElementType();
2867 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002868}
2869
Anders Carlsson4bf82142009-09-25 01:23:32 +00002870QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2871 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002872
Anders Carlsson4bf82142009-09-25 01:23:32 +00002873 if (const ArrayType *AT = getAsArrayType(ElemTy))
2874 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002875
Anders Carlssone0808df2008-12-21 03:44:36 +00002876 return ElemTy;
2877}
2878
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002879/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002880uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002881ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2882 uint64_t ElementCount = 1;
2883 do {
2884 ElementCount *= CA->getSize().getZExtValue();
2885 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2886 } while (CA);
2887 return ElementCount;
2888}
2889
Steve Naroff0af91202007-04-27 21:51:21 +00002890/// getFloatingRank - Return a relative rank for floating point types.
2891/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002892static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002893 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002894 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002895
John McCall9dd450b2009-09-21 23:43:11 +00002896 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2897 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002898 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002899 case BuiltinType::Float: return FloatRank;
2900 case BuiltinType::Double: return DoubleRank;
2901 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002902 }
2903}
2904
Mike Stump11289f42009-09-09 15:08:12 +00002905/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2906/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002907/// 'typeDomain' is a real floating point or complex type.
2908/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002909QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2910 QualType Domain) const {
2911 FloatingRank EltRank = getFloatingRank(Size);
2912 if (Domain->isComplexType()) {
2913 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002914 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002915 case FloatRank: return FloatComplexTy;
2916 case DoubleRank: return DoubleComplexTy;
2917 case LongDoubleRank: return LongDoubleComplexTy;
2918 }
Steve Naroff0af91202007-04-27 21:51:21 +00002919 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002920
2921 assert(Domain->isRealFloatingType() && "Unknown domain!");
2922 switch (EltRank) {
2923 default: assert(0 && "getFloatingRank(): illegal value for rank");
2924 case FloatRank: return FloatTy;
2925 case DoubleRank: return DoubleTy;
2926 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002927 }
Steve Naroffe4718892007-04-27 18:30:00 +00002928}
2929
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002930/// getFloatingTypeOrder - Compare the rank of the two specified floating
2931/// point types, ignoring the domain of the type (i.e. 'double' ==
2932/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002933/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002934int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2935 FloatingRank LHSR = getFloatingRank(LHS);
2936 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002937
Chris Lattnerb90739d2008-04-06 23:38:49 +00002938 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002939 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002940 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002941 return 1;
2942 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002943}
2944
Chris Lattner76a00cf2008-04-06 22:59:24 +00002945/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2946/// routine will assert if passed a built-in type that isn't an integer or enum,
2947/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002948unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002949 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002950 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002951 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002952
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002953 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2954 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2955
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002956 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2957 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2958
2959 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2960 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2961
Chris Lattner76a00cf2008-04-06 22:59:24 +00002962 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002963 default: assert(0 && "getIntegerRank(): not a built-in integer");
2964 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002965 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002966 case BuiltinType::Char_S:
2967 case BuiltinType::Char_U:
2968 case BuiltinType::SChar:
2969 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002970 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002971 case BuiltinType::Short:
2972 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002973 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002974 case BuiltinType::Int:
2975 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002976 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002977 case BuiltinType::Long:
2978 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002979 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002980 case BuiltinType::LongLong:
2981 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002982 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002983 case BuiltinType::Int128:
2984 case BuiltinType::UInt128:
2985 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002986 }
2987}
2988
Eli Friedman629ffb92009-08-20 04:21:42 +00002989/// \brief Whether this is a promotable bitfield reference according
2990/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2991///
2992/// \returns the type this bit-field will promote to, or NULL if no
2993/// promotion occurs.
2994QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00002995 if (E->isTypeDependent() || E->isValueDependent())
2996 return QualType();
2997
Eli Friedman629ffb92009-08-20 04:21:42 +00002998 FieldDecl *Field = E->getBitField();
2999 if (!Field)
3000 return QualType();
3001
3002 QualType FT = Field->getType();
3003
3004 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3005 uint64_t BitWidth = BitWidthAP.getZExtValue();
3006 uint64_t IntSize = getTypeSize(IntTy);
3007 // GCC extension compatibility: if the bit-field size is less than or equal
3008 // to the size of int, it gets promoted no matter what its type is.
3009 // For instance, unsigned long bf : 4 gets promoted to signed int.
3010 if (BitWidth < IntSize)
3011 return IntTy;
3012
3013 if (BitWidth == IntSize)
3014 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3015
3016 // Types bigger than int are not subject to promotions, and therefore act
3017 // like the base type.
3018 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3019 // is ridiculous.
3020 return QualType();
3021}
3022
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003023/// getPromotedIntegerType - Returns the type that Promotable will
3024/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3025/// integer type.
3026QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
3027 assert(!Promotable.isNull());
3028 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00003029 if (const EnumType *ET = Promotable->getAs<EnumType>())
3030 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003031 if (Promotable->isSignedIntegerType())
3032 return IntTy;
3033 uint64_t PromotableSize = getTypeSize(Promotable);
3034 uint64_t IntSize = getTypeSize(IntTy);
3035 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3036 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3037}
3038
Mike Stump11289f42009-09-09 15:08:12 +00003039/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003040/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003041/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003042int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00003043 Type *LHSC = getCanonicalType(LHS).getTypePtr();
3044 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003045 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003046
Chris Lattner76a00cf2008-04-06 22:59:24 +00003047 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3048 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00003049
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003050 unsigned LHSRank = getIntegerRank(LHSC);
3051 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00003052
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003053 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3054 if (LHSRank == RHSRank) return 0;
3055 return LHSRank > RHSRank ? 1 : -1;
3056 }
Mike Stump11289f42009-09-09 15:08:12 +00003057
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003058 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3059 if (LHSUnsigned) {
3060 // If the unsigned [LHS] type is larger, return it.
3061 if (LHSRank >= RHSRank)
3062 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00003063
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003064 // If the signed type can represent all values of the unsigned type, it
3065 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003066 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003067 return -1;
3068 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00003069
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003070 // If the unsigned [RHS] type is larger, return it.
3071 if (RHSRank >= LHSRank)
3072 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00003073
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003074 // If the signed type can represent all values of the unsigned type, it
3075 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003076 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003077 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00003078}
Anders Carlsson98f07902007-08-17 05:31:46 +00003079
Anders Carlsson6d417272009-11-14 21:45:58 +00003080static RecordDecl *
3081CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
3082 SourceLocation L, IdentifierInfo *Id) {
3083 if (Ctx.getLangOptions().CPlusPlus)
3084 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
3085 else
3086 return RecordDecl::Create(Ctx, TK, DC, L, Id);
3087}
3088
Mike Stump11289f42009-09-09 15:08:12 +00003089// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00003090QualType ASTContext::getCFConstantStringType() {
3091 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00003092 CFConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003093 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003094 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00003095 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00003096
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003097 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00003098
Anders Carlsson98f07902007-08-17 05:31:46 +00003099 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00003100 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003101 // int flags;
3102 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00003103 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00003104 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00003105 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00003106 FieldTypes[3] = LongTy;
3107
Anders Carlsson98f07902007-08-17 05:31:46 +00003108 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00003109 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003110 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00003111 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003112 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003113 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003114 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003115 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003116 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003117 }
3118
Douglas Gregord5058122010-02-11 01:19:42 +00003119 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00003120 }
Mike Stump11289f42009-09-09 15:08:12 +00003121
Anders Carlsson98f07902007-08-17 05:31:46 +00003122 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00003123}
Anders Carlsson87c149b2007-10-11 01:00:40 +00003124
Douglas Gregor512b0772009-04-23 22:29:11 +00003125void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003126 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003127 assert(Rec && "Invalid CFConstantStringType");
3128 CFConstantStringTypeDecl = Rec->getDecl();
3129}
3130
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003131// getNSConstantStringType - Return the type used for constant NSStrings.
3132QualType ASTContext::getNSConstantStringType() {
3133 if (!NSConstantStringTypeDecl) {
3134 NSConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003135 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003136 &Idents.get("__builtin_NSString"));
3137 NSConstantStringTypeDecl->startDefinition();
3138
3139 QualType FieldTypes[3];
3140
3141 // const int *isa;
3142 FieldTypes[0] = getPointerType(IntTy.withConst());
3143 // const char *str;
3144 FieldTypes[1] = getPointerType(CharTy.withConst());
3145 // unsigned int length;
3146 FieldTypes[2] = UnsignedIntTy;
3147
3148 // Create fields
3149 for (unsigned i = 0; i < 3; ++i) {
3150 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3151 SourceLocation(), 0,
3152 FieldTypes[i], /*TInfo=*/0,
3153 /*BitWidth=*/0,
3154 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003155 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003156 NSConstantStringTypeDecl->addDecl(Field);
3157 }
3158
3159 NSConstantStringTypeDecl->completeDefinition();
3160 }
3161
3162 return getTagDeclType(NSConstantStringTypeDecl);
3163}
3164
3165void ASTContext::setNSConstantStringType(QualType T) {
3166 const RecordType *Rec = T->getAs<RecordType>();
3167 assert(Rec && "Invalid NSConstantStringType");
3168 NSConstantStringTypeDecl = Rec->getDecl();
3169}
3170
Mike Stump11289f42009-09-09 15:08:12 +00003171QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003172 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003173 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003174 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003175 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003176 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003177
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003178 QualType FieldTypes[] = {
3179 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003180 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003181 getPointerType(UnsignedLongTy),
3182 getConstantArrayType(UnsignedLongTy,
3183 llvm::APInt(32, 5), ArrayType::Normal, 0)
3184 };
Mike Stump11289f42009-09-09 15:08:12 +00003185
Douglas Gregor91f84212008-12-11 16:49:14 +00003186 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003187 FieldDecl *Field = FieldDecl::Create(*this,
3188 ObjCFastEnumerationStateTypeDecl,
3189 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003190 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003191 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003192 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003193 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003194 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003195 }
Mike Stump11289f42009-09-09 15:08:12 +00003196
Douglas Gregord5058122010-02-11 01:19:42 +00003197 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003198 }
Mike Stump11289f42009-09-09 15:08:12 +00003199
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003200 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3201}
3202
Mike Stumpd0153282009-10-20 02:12:22 +00003203QualType ASTContext::getBlockDescriptorType() {
3204 if (BlockDescriptorType)
3205 return getTagDeclType(BlockDescriptorType);
3206
3207 RecordDecl *T;
3208 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003209 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003210 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003211 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003212
3213 QualType FieldTypes[] = {
3214 UnsignedLongTy,
3215 UnsignedLongTy,
3216 };
3217
3218 const char *FieldNames[] = {
3219 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003220 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003221 };
3222
3223 for (size_t i = 0; i < 2; ++i) {
3224 FieldDecl *Field = FieldDecl::Create(*this,
3225 T,
3226 SourceLocation(),
3227 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003228 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003229 /*BitWidth=*/0,
3230 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003231 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003232 T->addDecl(Field);
3233 }
3234
Douglas Gregord5058122010-02-11 01:19:42 +00003235 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003236
3237 BlockDescriptorType = T;
3238
3239 return getTagDeclType(BlockDescriptorType);
3240}
3241
3242void ASTContext::setBlockDescriptorType(QualType T) {
3243 const RecordType *Rec = T->getAs<RecordType>();
3244 assert(Rec && "Invalid BlockDescriptorType");
3245 BlockDescriptorType = Rec->getDecl();
3246}
3247
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003248QualType ASTContext::getBlockDescriptorExtendedType() {
3249 if (BlockDescriptorExtendedType)
3250 return getTagDeclType(BlockDescriptorExtendedType);
3251
3252 RecordDecl *T;
3253 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003254 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003255 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003256 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003257
3258 QualType FieldTypes[] = {
3259 UnsignedLongTy,
3260 UnsignedLongTy,
3261 getPointerType(VoidPtrTy),
3262 getPointerType(VoidPtrTy)
3263 };
3264
3265 const char *FieldNames[] = {
3266 "reserved",
3267 "Size",
3268 "CopyFuncPtr",
3269 "DestroyFuncPtr"
3270 };
3271
3272 for (size_t i = 0; i < 4; ++i) {
3273 FieldDecl *Field = FieldDecl::Create(*this,
3274 T,
3275 SourceLocation(),
3276 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003277 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003278 /*BitWidth=*/0,
3279 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003280 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003281 T->addDecl(Field);
3282 }
3283
Douglas Gregord5058122010-02-11 01:19:42 +00003284 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003285
3286 BlockDescriptorExtendedType = T;
3287
3288 return getTagDeclType(BlockDescriptorExtendedType);
3289}
3290
3291void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3292 const RecordType *Rec = T->getAs<RecordType>();
3293 assert(Rec && "Invalid BlockDescriptorType");
3294 BlockDescriptorExtendedType = Rec->getDecl();
3295}
3296
Mike Stump94967902009-10-21 18:16:27 +00003297bool ASTContext::BlockRequiresCopying(QualType Ty) {
3298 if (Ty->isBlockPointerType())
3299 return true;
3300 if (isObjCNSObjectType(Ty))
3301 return true;
3302 if (Ty->isObjCObjectPointerType())
3303 return true;
Fariborz Jahaniana00076c2010-11-17 00:21:28 +00003304 if (getLangOptions().CPlusPlus) {
3305 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3306 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3307 return RD->hasConstCopyConstructor(*this);
3308
3309 }
3310 }
Mike Stump94967902009-10-21 18:16:27 +00003311 return false;
3312}
3313
Daniel Dunbar56df9772010-08-17 22:39:59 +00003314QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) {
Mike Stump94967902009-10-21 18:16:27 +00003315 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003316 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003317 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003318 // unsigned int __flags;
3319 // unsigned int __size;
Eli Friedman04831922010-08-22 01:00:03 +00003320 // void *__copy_helper; // as needed
3321 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003322 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003323 // } *
3324
Mike Stump94967902009-10-21 18:16:27 +00003325 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3326
3327 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003328 llvm::SmallString<36> Name;
3329 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3330 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003331 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003332 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003333 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003334 T->startDefinition();
3335 QualType Int32Ty = IntTy;
3336 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3337 QualType FieldTypes[] = {
3338 getPointerType(VoidPtrTy),
3339 getPointerType(getTagDeclType(T)),
3340 Int32Ty,
3341 Int32Ty,
3342 getPointerType(VoidPtrTy),
3343 getPointerType(VoidPtrTy),
3344 Ty
3345 };
3346
Daniel Dunbar56df9772010-08-17 22:39:59 +00003347 llvm::StringRef FieldNames[] = {
Mike Stump94967902009-10-21 18:16:27 +00003348 "__isa",
3349 "__forwarding",
3350 "__flags",
3351 "__size",
3352 "__copy_helper",
3353 "__destroy_helper",
3354 DeclName,
3355 };
3356
3357 for (size_t i = 0; i < 7; ++i) {
3358 if (!HasCopyAndDispose && i >=4 && i <= 5)
3359 continue;
3360 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3361 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003362 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003363 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003364 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003365 T->addDecl(Field);
3366 }
3367
Douglas Gregord5058122010-02-11 01:19:42 +00003368 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003369
3370 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003371}
3372
3373
3374QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003375 bool BlockHasCopyDispose,
John McCall87fe5d52010-05-20 01:18:31 +00003376 llvm::SmallVectorImpl<const Expr *> &Layout) {
3377
Mike Stumpd0153282009-10-20 02:12:22 +00003378 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003379 llvm::SmallString<36> Name;
3380 llvm::raw_svector_ostream(Name) << "__block_literal_"
3381 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003382 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003383 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003384 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003385 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003386 QualType FieldTypes[] = {
3387 getPointerType(VoidPtrTy),
3388 IntTy,
3389 IntTy,
3390 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003391 (BlockHasCopyDispose ?
3392 getPointerType(getBlockDescriptorExtendedType()) :
3393 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003394 };
3395
3396 const char *FieldNames[] = {
3397 "__isa",
3398 "__flags",
3399 "__reserved",
3400 "__FuncPtr",
3401 "__descriptor"
3402 };
3403
3404 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003405 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003406 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003407 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003408 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003409 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003410 T->addDecl(Field);
3411 }
3412
John McCall87fe5d52010-05-20 01:18:31 +00003413 for (unsigned i = 0; i < Layout.size(); ++i) {
3414 const Expr *E = Layout[i];
Mike Stump7fe9cc12009-10-21 03:49:08 +00003415
John McCall87fe5d52010-05-20 01:18:31 +00003416 QualType FieldType = E->getType();
3417 IdentifierInfo *FieldName = 0;
3418 if (isa<CXXThisExpr>(E)) {
3419 FieldName = &Idents.get("this");
3420 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3421 const ValueDecl *D = BDRE->getDecl();
3422 FieldName = D->getIdentifier();
3423 if (BDRE->isByRef())
Daniel Dunbar56df9772010-08-17 22:39:59 +00003424 FieldType = BuildByRefType(D->getName(), FieldType);
John McCall87fe5d52010-05-20 01:18:31 +00003425 } else {
3426 // Padding.
3427 assert(isa<ConstantArrayType>(FieldType) &&
3428 isa<DeclRefExpr>(E) &&
3429 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3430 "doesn't match characteristics of padding decl");
3431 }
Mike Stump7fe9cc12009-10-21 03:49:08 +00003432
3433 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCall87fe5d52010-05-20 01:18:31 +00003434 FieldName, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003435 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003436 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003437 T->addDecl(Field);
3438 }
3439
Douglas Gregord5058122010-02-11 01:19:42 +00003440 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003441
3442 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003443}
3444
Douglas Gregor512b0772009-04-23 22:29:11 +00003445void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003446 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003447 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3448 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3449}
3450
Anders Carlsson18acd442007-10-29 06:33:42 +00003451// This returns true if a type has been typedefed to BOOL:
3452// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003453static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003454 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003455 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3456 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003457
Anders Carlssond8499822007-10-29 05:01:08 +00003458 return false;
3459}
3460
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003461/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003462/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003463CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003464 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003465
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003466 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003467 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003468 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003469 // Treat arrays as pointers, since that's how they're passed in.
3470 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003471 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003472 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003473}
3474
3475static inline
3476std::string charUnitsToString(const CharUnits &CU) {
3477 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003478}
3479
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003480/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003481/// declaration.
3482void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3483 std::string& S) {
3484 const BlockDecl *Decl = Expr->getBlockDecl();
3485 QualType BlockTy =
3486 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3487 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003488 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003489 // Compute size of all parameters.
3490 // Start with computing size of a pointer in number of bytes.
3491 // FIXME: There might(should) be a better way of doing this computation!
3492 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003493 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3494 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003495 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003496 E = Decl->param_end(); PI != E; ++PI) {
3497 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003498 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003499 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003500 ParmOffset += sz;
3501 }
3502 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003503 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003504 // Block pointer and offset.
3505 S += "@?0";
3506 ParmOffset = PtrSize;
3507
3508 // Argument types.
3509 ParmOffset = PtrSize;
3510 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3511 Decl->param_end(); PI != E; ++PI) {
3512 ParmVarDecl *PVDecl = *PI;
3513 QualType PType = PVDecl->getOriginalType();
3514 if (const ArrayType *AT =
3515 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3516 // Use array's original type only if it has known number of
3517 // elements.
3518 if (!isa<ConstantArrayType>(AT))
3519 PType = PVDecl->getType();
3520 } else if (PType->isFunctionType())
3521 PType = PVDecl->getType();
3522 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003523 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003524 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003525 }
3526}
3527
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003528/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003529/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003530void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003531 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003532 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003533 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003534 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003535 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003536 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003537 // Compute size of all parameters.
3538 // Start with computing size of a pointer in number of bytes.
3539 // FIXME: There might(should) be a better way of doing this computation!
3540 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003541 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003542 // The first two arguments (self and _cmd) are pointers; account for
3543 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003544 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003545 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003546 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003547 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003548 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003549 assert (sz.isPositive() &&
3550 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003551 ParmOffset += sz;
3552 }
Ken Dyck40775002010-01-11 17:06:35 +00003553 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003554 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003555 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003556
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003557 // Argument types.
3558 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003559 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003560 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003561 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003562 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003563 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003564 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3565 // Use array's original type only if it has known number of
3566 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003567 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003568 PType = PVDecl->getType();
3569 } else if (PType->isFunctionType())
3570 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003571 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003572 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003573 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003574 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003575 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003576 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003577 }
3578}
3579
Daniel Dunbar4932b362008-08-28 04:38:10 +00003580/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003581/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003582/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3583/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003584/// Property attributes are stored as a comma-delimited C string. The simple
3585/// attributes readonly and bycopy are encoded as single characters. The
3586/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3587/// encoded as single characters, followed by an identifier. Property types
3588/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003589/// these attributes are defined by the following enumeration:
3590/// @code
3591/// enum PropertyAttributes {
3592/// kPropertyReadOnly = 'R', // property is read-only.
3593/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3594/// kPropertyByref = '&', // property is a reference to the value last assigned
3595/// kPropertyDynamic = 'D', // property is dynamic
3596/// kPropertyGetter = 'G', // followed by getter selector name
3597/// kPropertySetter = 'S', // followed by setter selector name
3598/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3599/// kPropertyType = 't' // followed by old-style type encoding.
3600/// kPropertyWeak = 'W' // 'weak' property
3601/// kPropertyStrong = 'P' // property GC'able
3602/// kPropertyNonAtomic = 'N' // property non-atomic
3603/// };
3604/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003605void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003606 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003607 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003608 // Collect information from the property implementation decl(s).
3609 bool Dynamic = false;
3610 ObjCPropertyImplDecl *SynthesizePID = 0;
3611
3612 // FIXME: Duplicated code due to poor abstraction.
3613 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003614 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003615 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3616 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003617 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003618 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003619 ObjCPropertyImplDecl *PID = *i;
3620 if (PID->getPropertyDecl() == PD) {
3621 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3622 Dynamic = true;
3623 } else {
3624 SynthesizePID = PID;
3625 }
3626 }
3627 }
3628 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003629 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003630 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003631 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003632 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003633 ObjCPropertyImplDecl *PID = *i;
3634 if (PID->getPropertyDecl() == PD) {
3635 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3636 Dynamic = true;
3637 } else {
3638 SynthesizePID = PID;
3639 }
3640 }
Mike Stump11289f42009-09-09 15:08:12 +00003641 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003642 }
3643 }
3644
3645 // FIXME: This is not very efficient.
3646 S = "T";
3647
3648 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003649 // GCC has some special rules regarding encoding of properties which
3650 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003651 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003652 true /* outermost type */,
3653 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003654
3655 if (PD->isReadOnly()) {
3656 S += ",R";
3657 } else {
3658 switch (PD->getSetterKind()) {
3659 case ObjCPropertyDecl::Assign: break;
3660 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003661 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003662 }
3663 }
3664
3665 // It really isn't clear at all what this means, since properties
3666 // are "dynamic by default".
3667 if (Dynamic)
3668 S += ",D";
3669
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003670 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3671 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003672
Daniel Dunbar4932b362008-08-28 04:38:10 +00003673 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3674 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003675 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003676 }
3677
3678 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3679 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003680 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003681 }
3682
3683 if (SynthesizePID) {
3684 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3685 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003686 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003687 }
3688
3689 // FIXME: OBJCGC: weak & strong
3690}
3691
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003692/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003693/// Another legacy compatibility encoding: 32-bit longs are encoded as
3694/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003695/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3696///
3697void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003698 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003699 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003700 if (BT->getKind() == BuiltinType::ULong &&
3701 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003702 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003703 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003704 if (BT->getKind() == BuiltinType::Long &&
3705 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003706 PointeeTy = IntTy;
3707 }
3708 }
3709}
3710
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003711void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003712 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003713 // We follow the behavior of gcc, expanding structures which are
3714 // directly pointed to, and expanding embedded structures. Note that
3715 // these rules are sufficient to prevent recursive encoding of the
3716 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003717 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003718 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003719}
3720
David Chisnallb190a2c2010-06-04 01:10:52 +00003721static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3722 switch (T->getAs<BuiltinType>()->getKind()) {
3723 default: assert(0 && "Unhandled builtin type kind");
3724 case BuiltinType::Void: return 'v';
3725 case BuiltinType::Bool: return 'B';
3726 case BuiltinType::Char_U:
3727 case BuiltinType::UChar: return 'C';
3728 case BuiltinType::UShort: return 'S';
3729 case BuiltinType::UInt: return 'I';
3730 case BuiltinType::ULong:
3731 return
3732 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3733 case BuiltinType::UInt128: return 'T';
3734 case BuiltinType::ULongLong: return 'Q';
3735 case BuiltinType::Char_S:
3736 case BuiltinType::SChar: return 'c';
3737 case BuiltinType::Short: return 's';
John McCalldad856d2010-06-11 10:11:05 +00003738 case BuiltinType::WChar:
David Chisnallb190a2c2010-06-04 01:10:52 +00003739 case BuiltinType::Int: return 'i';
3740 case BuiltinType::Long:
3741 return
3742 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3743 case BuiltinType::LongLong: return 'q';
3744 case BuiltinType::Int128: return 't';
3745 case BuiltinType::Float: return 'f';
3746 case BuiltinType::Double: return 'd';
Daniel Dunbar7cba5a72010-10-11 21:13:48 +00003747 case BuiltinType::LongDouble: return 'D';
David Chisnallb190a2c2010-06-04 01:10:52 +00003748 }
3749}
3750
Mike Stump11289f42009-09-09 15:08:12 +00003751static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00003752 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003753 const Expr *E = FD->getBitWidth();
3754 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3755 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003756 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00003757 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3758 // The GNU runtime requires more information; bitfields are encoded as b,
3759 // then the offset (in bits) of the first element, then the type of the
3760 // bitfield, then the size in bits. For example, in this structure:
3761 //
3762 // struct
3763 // {
3764 // int integer;
3765 // int flags:2;
3766 // };
3767 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3768 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3769 // information is not especially sensible, but we're stuck with it for
3770 // compatibility with GCC, although providing it breaks anything that
3771 // actually uses runtime introspection and wants to work on both runtimes...
3772 if (!Ctx->getLangOptions().NeXTRuntime) {
3773 const RecordDecl *RD = FD->getParent();
3774 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3775 // FIXME: This same linear search is also used in ExprConstant - it might
3776 // be better if the FieldDecl stored its offset. We'd be increasing the
3777 // size of the object slightly, but saving some time every time it is used.
3778 unsigned i = 0;
3779 for (RecordDecl::field_iterator Field = RD->field_begin(),
3780 FieldEnd = RD->field_end();
3781 Field != FieldEnd; (void)++Field, ++i) {
3782 if (*Field == FD)
3783 break;
3784 }
3785 S += llvm::utostr(RL.getFieldOffset(i));
3786 S += ObjCEncodingForPrimitiveKind(Context, T);
3787 }
3788 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003789 S += llvm::utostr(N);
3790}
3791
Daniel Dunbar07d07852009-10-18 21:17:35 +00003792// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003793void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3794 bool ExpandPointedToStructures,
3795 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003796 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003797 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003798 bool EncodingProperty) {
David Chisnallb190a2c2010-06-04 01:10:52 +00003799 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003800 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003801 return EncodeBitField(this, S, T, FD);
3802 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003803 return;
3804 }
Mike Stump11289f42009-09-09 15:08:12 +00003805
John McCall9dd450b2009-09-21 23:43:11 +00003806 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003807 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003808 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003809 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003810 return;
3811 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003812
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003813 // encoding for pointer or r3eference types.
3814 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003815 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003816 if (PT->isObjCSelType()) {
3817 S += ':';
3818 return;
3819 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003820 PointeeTy = PT->getPointeeType();
3821 }
3822 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3823 PointeeTy = RT->getPointeeType();
3824 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003825 bool isReadOnly = false;
3826 // For historical/compatibility reasons, the read-only qualifier of the
3827 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3828 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003829 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003830 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003831 if (OutermostType && T.isConstQualified()) {
3832 isReadOnly = true;
3833 S += 'r';
3834 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003835 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003836 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003837 while (P->getAs<PointerType>())
3838 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003839 if (P.isConstQualified()) {
3840 isReadOnly = true;
3841 S += 'r';
3842 }
3843 }
3844 if (isReadOnly) {
3845 // Another legacy compatibility encoding. Some ObjC qualifier and type
3846 // combinations need to be rearranged.
3847 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003848 if (llvm::StringRef(S).endswith("nr"))
3849 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003850 }
Mike Stump11289f42009-09-09 15:08:12 +00003851
Anders Carlssond8499822007-10-29 05:01:08 +00003852 if (PointeeTy->isCharType()) {
3853 // char pointer types should be encoded as '*' unless it is a
3854 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003855 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003856 S += '*';
3857 return;
3858 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003859 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003860 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3861 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3862 S += '#';
3863 return;
3864 }
3865 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3866 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3867 S += '@';
3868 return;
3869 }
3870 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003871 }
Anders Carlssond8499822007-10-29 05:01:08 +00003872 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003873 getLegacyIntegralTypeEncoding(PointeeTy);
3874
Mike Stump11289f42009-09-09 15:08:12 +00003875 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003876 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003877 return;
3878 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003879
Chris Lattnere7cabb92009-07-13 00:10:46 +00003880 if (const ArrayType *AT =
3881 // Ignore type qualifiers etc.
3882 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003883 if (isa<IncompleteArrayType>(AT)) {
3884 // Incomplete arrays are encoded as a pointer to the array element.
3885 S += '^';
3886
Mike Stump11289f42009-09-09 15:08:12 +00003887 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003888 false, ExpandStructures, FD);
3889 } else {
3890 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003891
Anders Carlssond05f44b2009-02-22 01:38:57 +00003892 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3893 S += llvm::utostr(CAT->getSize().getZExtValue());
3894 else {
3895 //Variable length arrays are encoded as a regular array with 0 elements.
3896 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3897 S += '0';
3898 }
Mike Stump11289f42009-09-09 15:08:12 +00003899
3900 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003901 false, ExpandStructures, FD);
3902 S += ']';
3903 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003904 return;
3905 }
Mike Stump11289f42009-09-09 15:08:12 +00003906
John McCall9dd450b2009-09-21 23:43:11 +00003907 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003908 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003909 return;
3910 }
Mike Stump11289f42009-09-09 15:08:12 +00003911
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003912 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003913 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003914 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003915 // Anonymous structures print as '?'
3916 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3917 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003918 if (ClassTemplateSpecializationDecl *Spec
3919 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3920 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3921 std::string TemplateArgsStr
3922 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003923 TemplateArgs.data(),
3924 TemplateArgs.size(),
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003925 (*this).PrintingPolicy);
3926
3927 S += TemplateArgsStr;
3928 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003929 } else {
3930 S += '?';
3931 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003932 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003933 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003934 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3935 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003936 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003937 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003938 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003939 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003940 S += '"';
3941 }
Mike Stump11289f42009-09-09 15:08:12 +00003942
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003943 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003944 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003945 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003946 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003947 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003948 QualType qt = Field->getType();
3949 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003950 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003951 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003952 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003953 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003954 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003955 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003956 return;
3957 }
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00003958
Chris Lattnere7cabb92009-07-13 00:10:46 +00003959 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003960 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003961 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003962 else
3963 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003964 return;
3965 }
Mike Stump11289f42009-09-09 15:08:12 +00003966
Chris Lattnere7cabb92009-07-13 00:10:46 +00003967 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003968 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003969 return;
3970 }
Mike Stump11289f42009-09-09 15:08:12 +00003971
John McCall8b07ec22010-05-15 11:32:37 +00003972 // Ignore protocol qualifiers when mangling at this level.
3973 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3974 T = OT->getBaseType();
3975
John McCall8ccfcb52009-09-24 19:53:00 +00003976 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003977 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003978 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003979 S += '{';
3980 const IdentifierInfo *II = OI->getIdentifier();
3981 S += II->getName();
3982 S += '=';
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003983 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
3984 DeepCollectObjCIvars(OI, true, Ivars);
3985 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
3986 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
3987 if (Field->isBitField())
3988 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003989 else
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003990 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003991 }
3992 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003993 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003994 }
Mike Stump11289f42009-09-09 15:08:12 +00003995
John McCall9dd450b2009-09-21 23:43:11 +00003996 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003997 if (OPT->isObjCIdType()) {
3998 S += '@';
3999 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004000 }
Mike Stump11289f42009-09-09 15:08:12 +00004001
Steve Narofff0c86112009-10-28 22:03:49 +00004002 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4003 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4004 // Since this is a binary compatibility issue, need to consult with runtime
4005 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004006 S += '#';
4007 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004008 }
Mike Stump11289f42009-09-09 15:08:12 +00004009
Chris Lattnere7cabb92009-07-13 00:10:46 +00004010 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00004011 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00004012 ExpandPointedToStructures,
4013 ExpandStructures, FD);
4014 if (FD || EncodingProperty) {
4015 // Note that we do extended encoding of protocol qualifer list
4016 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004017 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00004018 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4019 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004020 S += '<';
4021 S += (*I)->getNameAsString();
4022 S += '>';
4023 }
4024 S += '"';
4025 }
4026 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004027 }
Mike Stump11289f42009-09-09 15:08:12 +00004028
Chris Lattnere7cabb92009-07-13 00:10:46 +00004029 QualType PointeeTy = OPT->getPointeeType();
4030 if (!EncodingProperty &&
4031 isa<TypedefType>(PointeeTy.getTypePtr())) {
4032 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00004033 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00004034 // {...};
4035 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00004036 getObjCEncodingForTypeImpl(PointeeTy, S,
4037 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00004038 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00004039 return;
4040 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004041
4042 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00004043 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004044 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00004045 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00004046 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4047 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004048 S += '<';
4049 S += (*I)->getNameAsString();
4050 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00004051 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004052 S += '"';
4053 }
4054 return;
4055 }
Mike Stump11289f42009-09-09 15:08:12 +00004056
John McCalla9e6e8d2010-05-17 23:56:34 +00004057 // gcc just blithely ignores member pointers.
4058 // TODO: maybe there should be a mangling for these
4059 if (T->getAs<MemberPointerType>())
4060 return;
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004061
4062 if (T->isVectorType()) {
4063 // This matches gcc's encoding, even though technically it is
4064 // insufficient.
4065 // FIXME. We should do a better job than gcc.
4066 return;
4067 }
4068
Chris Lattnere7cabb92009-07-13 00:10:46 +00004069 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00004070}
4071
Mike Stump11289f42009-09-09 15:08:12 +00004072void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00004073 std::string& S) const {
4074 if (QT & Decl::OBJC_TQ_In)
4075 S += 'n';
4076 if (QT & Decl::OBJC_TQ_Inout)
4077 S += 'N';
4078 if (QT & Decl::OBJC_TQ_Out)
4079 S += 'o';
4080 if (QT & Decl::OBJC_TQ_Bycopy)
4081 S += 'O';
4082 if (QT & Decl::OBJC_TQ_Byref)
4083 S += 'R';
4084 if (QT & Decl::OBJC_TQ_Oneway)
4085 S += 'V';
4086}
4087
Chris Lattnere7cabb92009-07-13 00:10:46 +00004088void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00004089 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004090
Anders Carlsson87c149b2007-10-11 01:00:40 +00004091 BuiltinVaListType = T;
4092}
4093
Chris Lattnere7cabb92009-07-13 00:10:46 +00004094void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004095 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00004096}
4097
Chris Lattnere7cabb92009-07-13 00:10:46 +00004098void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00004099 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00004100}
4101
Chris Lattnere7cabb92009-07-13 00:10:46 +00004102void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004103 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004104}
4105
Chris Lattnere7cabb92009-07-13 00:10:46 +00004106void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004107 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00004108}
4109
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004110void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00004111 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00004112 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004113
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004114 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00004115}
4116
John McCalld28ae272009-12-02 08:04:21 +00004117/// \brief Retrieve the template name that corresponds to a non-empty
4118/// lookup.
John McCallad371252010-01-20 00:46:10 +00004119TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4120 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00004121 unsigned size = End - Begin;
4122 assert(size > 1 && "set is not overloaded!");
4123
4124 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4125 size * sizeof(FunctionTemplateDecl*));
4126 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4127
4128 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00004129 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00004130 NamedDecl *D = *I;
4131 assert(isa<FunctionTemplateDecl>(D) ||
4132 (isa<UsingShadowDecl>(D) &&
4133 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4134 *Storage++ = D;
4135 }
4136
4137 return TemplateName(OT);
4138}
4139
Douglas Gregordc572a32009-03-30 22:58:21 +00004140/// \brief Retrieve the template name that represents a qualified
4141/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00004142TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00004143 bool TemplateKeyword,
4144 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00004145 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00004146 llvm::FoldingSetNodeID ID;
4147 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4148
4149 void *InsertPos = 0;
4150 QualifiedTemplateName *QTN =
4151 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4152 if (!QTN) {
4153 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4154 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4155 }
4156
4157 return TemplateName(QTN);
4158}
4159
4160/// \brief Retrieve the template name that represents a dependent
4161/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00004162TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00004163 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004164 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00004165 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00004166
4167 llvm::FoldingSetNodeID ID;
4168 DependentTemplateName::Profile(ID, NNS, Name);
4169
4170 void *InsertPos = 0;
4171 DependentTemplateName *QTN =
4172 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4173
4174 if (QTN)
4175 return TemplateName(QTN);
4176
4177 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4178 if (CanonNNS == NNS) {
4179 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4180 } else {
4181 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4182 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004183 DependentTemplateName *CheckQTN =
4184 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4185 assert(!CheckQTN && "Dependent type name canonicalization broken");
4186 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004187 }
4188
4189 DependentTemplateNames.InsertNode(QTN, InsertPos);
4190 return TemplateName(QTN);
4191}
4192
Douglas Gregor71395fa2009-11-04 00:56:37 +00004193/// \brief Retrieve the template name that represents a dependent
4194/// template name such as \c MetaFun::template operator+.
4195TemplateName
4196ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4197 OverloadedOperatorKind Operator) {
4198 assert((!NNS || NNS->isDependent()) &&
4199 "Nested name specifier must be dependent");
4200
4201 llvm::FoldingSetNodeID ID;
4202 DependentTemplateName::Profile(ID, NNS, Operator);
4203
4204 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004205 DependentTemplateName *QTN
4206 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004207
4208 if (QTN)
4209 return TemplateName(QTN);
4210
4211 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4212 if (CanonNNS == NNS) {
4213 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4214 } else {
4215 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4216 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004217
4218 DependentTemplateName *CheckQTN
4219 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4220 assert(!CheckQTN && "Dependent template name canonicalization broken");
4221 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004222 }
4223
4224 DependentTemplateNames.InsertNode(QTN, InsertPos);
4225 return TemplateName(QTN);
4226}
4227
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004228/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004229/// TargetInfo, produce the corresponding type. The unsigned @p Type
4230/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004231CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004232 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004233 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004234 case TargetInfo::SignedShort: return ShortTy;
4235 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4236 case TargetInfo::SignedInt: return IntTy;
4237 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4238 case TargetInfo::SignedLong: return LongTy;
4239 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4240 case TargetInfo::SignedLongLong: return LongLongTy;
4241 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4242 }
4243
4244 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004245 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004246}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004247
4248//===----------------------------------------------------------------------===//
4249// Type Predicates.
4250//===----------------------------------------------------------------------===//
4251
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004252/// isObjCNSObjectType - Return true if this is an NSObject object using
4253/// NSObject attribute on a c-style pointer type.
4254/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004255/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004256///
4257bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4258 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4259 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004260 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004261 return true;
4262 }
Mike Stump11289f42009-09-09 15:08:12 +00004263 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004264}
4265
Fariborz Jahanian96207692009-02-18 21:49:28 +00004266/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4267/// garbage collection attribute.
4268///
John McCall8ccfcb52009-09-24 19:53:00 +00004269Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4270 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004271 if (getLangOptions().ObjC1 &&
4272 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00004273 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00004274 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00004275 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004276 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00004277 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00004278 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004279 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004280 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004281 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004282 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00004283 // Non-pointers have none gc'able attribute regardless of the attribute
4284 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00004285 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004286 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004287 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004288 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004289}
4290
Chris Lattner49af6a42008-04-07 06:51:04 +00004291//===----------------------------------------------------------------------===//
4292// Type Compatibility Testing
4293//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004294
Mike Stump11289f42009-09-09 15:08:12 +00004295/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004296/// compatible.
4297static bool areCompatVectorTypes(const VectorType *LHS,
4298 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004299 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004300 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004301 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004302}
4303
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004304bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4305 QualType SecondVec) {
4306 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4307 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4308
4309 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4310 return true;
4311
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004312 // Treat Neon vector types and most AltiVec vector types as if they are the
4313 // equivalent GCC vector types.
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004314 const VectorType *First = FirstVec->getAs<VectorType>();
4315 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004316 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004317 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004318 First->getVectorKind() != VectorType::AltiVecPixel &&
4319 First->getVectorKind() != VectorType::AltiVecBool &&
4320 Second->getVectorKind() != VectorType::AltiVecPixel &&
4321 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004322 return true;
4323
4324 return false;
4325}
4326
Steve Naroff8e6aee52009-07-23 01:01:38 +00004327//===----------------------------------------------------------------------===//
4328// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4329//===----------------------------------------------------------------------===//
4330
4331/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4332/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004333bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4334 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004335 if (lProto == rProto)
4336 return true;
4337 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4338 E = rProto->protocol_end(); PI != E; ++PI)
4339 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4340 return true;
4341 return false;
4342}
4343
Steve Naroff8e6aee52009-07-23 01:01:38 +00004344/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4345/// return true if lhs's protocols conform to rhs's protocol; false
4346/// otherwise.
4347bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4348 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4349 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4350 return false;
4351}
4352
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004353/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4354/// Class<p1, ...>.
4355bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4356 QualType rhs) {
4357 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4358 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4359 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4360
4361 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4362 E = lhsQID->qual_end(); I != E; ++I) {
4363 bool match = false;
4364 ObjCProtocolDecl *lhsProto = *I;
4365 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4366 E = rhsOPT->qual_end(); J != E; ++J) {
4367 ObjCProtocolDecl *rhsProto = *J;
4368 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4369 match = true;
4370 break;
4371 }
4372 }
4373 if (!match)
4374 return false;
4375 }
4376 return true;
4377}
4378
Steve Naroff8e6aee52009-07-23 01:01:38 +00004379/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4380/// ObjCQualifiedIDType.
4381bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4382 bool compare) {
4383 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004384 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004385 lhs->isObjCIdType() || lhs->isObjCClassType())
4386 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004387 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004388 rhs->isObjCIdType() || rhs->isObjCClassType())
4389 return true;
4390
4391 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004392 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004393
Steve Naroff8e6aee52009-07-23 01:01:38 +00004394 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004395
Steve Naroff8e6aee52009-07-23 01:01:38 +00004396 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004397 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004398 // make sure we check the class hierarchy.
4399 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4400 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4401 E = lhsQID->qual_end(); I != E; ++I) {
4402 // when comparing an id<P> on lhs with a static type on rhs,
4403 // see if static class implements all of id's protocols, directly or
4404 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004405 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004406 return false;
4407 }
4408 }
4409 // If there are no qualifiers and no interface, we have an 'id'.
4410 return true;
4411 }
Mike Stump11289f42009-09-09 15:08:12 +00004412 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004413 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4414 E = lhsQID->qual_end(); I != E; ++I) {
4415 ObjCProtocolDecl *lhsProto = *I;
4416 bool match = false;
4417
4418 // when comparing an id<P> on lhs with a static type on rhs,
4419 // see if static class implements all of id's protocols, directly or
4420 // through its super class and categories.
4421 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4422 E = rhsOPT->qual_end(); J != E; ++J) {
4423 ObjCProtocolDecl *rhsProto = *J;
4424 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4425 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4426 match = true;
4427 break;
4428 }
4429 }
Mike Stump11289f42009-09-09 15:08:12 +00004430 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004431 // make sure we check the class hierarchy.
4432 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4433 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4434 E = lhsQID->qual_end(); I != E; ++I) {
4435 // when comparing an id<P> on lhs with a static type on rhs,
4436 // see if static class implements all of id's protocols, directly or
4437 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004438 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004439 match = true;
4440 break;
4441 }
4442 }
4443 }
4444 if (!match)
4445 return false;
4446 }
Mike Stump11289f42009-09-09 15:08:12 +00004447
Steve Naroff8e6aee52009-07-23 01:01:38 +00004448 return true;
4449 }
Mike Stump11289f42009-09-09 15:08:12 +00004450
Steve Naroff8e6aee52009-07-23 01:01:38 +00004451 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4452 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4453
Mike Stump11289f42009-09-09 15:08:12 +00004454 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004455 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004456 // If both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004457 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4458 E = lhsOPT->qual_end(); I != E; ++I) {
4459 ObjCProtocolDecl *lhsProto = *I;
4460 bool match = false;
4461
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004462 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff8e6aee52009-07-23 01:01:38 +00004463 // see if static class implements all of id's protocols, directly or
4464 // through its super class and categories.
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004465 // First, lhs protocols in the qualifier list must be found, direct
4466 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004467 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4468 E = rhsQID->qual_end(); J != E; ++J) {
4469 ObjCProtocolDecl *rhsProto = *J;
4470 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4471 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4472 match = true;
4473 break;
4474 }
4475 }
4476 if (!match)
4477 return false;
4478 }
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004479
4480 // Static class's protocols, or its super class or category protocols
4481 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4482 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4483 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4484 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4485 // This is rather dubious but matches gcc's behavior. If lhs has
4486 // no type qualifier and its class has no static protocol(s)
4487 // assume that it is mismatch.
4488 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4489 return false;
4490 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4491 LHSInheritedProtocols.begin(),
4492 E = LHSInheritedProtocols.end(); I != E; ++I) {
4493 bool match = false;
4494 ObjCProtocolDecl *lhsProto = (*I);
4495 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4496 E = rhsQID->qual_end(); J != E; ++J) {
4497 ObjCProtocolDecl *rhsProto = *J;
4498 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4499 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4500 match = true;
4501 break;
4502 }
4503 }
4504 if (!match)
4505 return false;
4506 }
4507 }
Steve Naroff8e6aee52009-07-23 01:01:38 +00004508 return true;
4509 }
4510 return false;
4511}
4512
Eli Friedman47f77112008-08-22 00:56:42 +00004513/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004514/// compatible for assignment from RHS to LHS. This handles validation of any
4515/// protocol qualifiers on the LHS or RHS.
4516///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004517bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4518 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004519 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4520 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4521
Steve Naroff1329fa02009-07-15 18:40:39 +00004522 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004523 if (LHS->isObjCUnqualifiedIdOrClass() ||
4524 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004525 return true;
4526
John McCall8b07ec22010-05-15 11:32:37 +00004527 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004528 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4529 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004530 false);
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004531
4532 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4533 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4534 QualType(RHSOPT,0));
4535
John McCall8b07ec22010-05-15 11:32:37 +00004536 // If we have 2 user-defined types, fall into that path.
4537 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004538 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004539
Steve Naroff8e6aee52009-07-23 01:01:38 +00004540 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004541}
4542
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004543/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4544/// for providing type-safty for objective-c pointers used to pass/return
4545/// arguments in block literals. When passed as arguments, passing 'A*' where
4546/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4547/// not OK. For the return type, the opposite is not OK.
4548bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4549 const ObjCObjectPointerType *LHSOPT,
4550 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004551 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004552 return true;
4553
4554 if (LHSOPT->isObjCBuiltinType()) {
4555 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4556 }
4557
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004558 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004559 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4560 QualType(RHSOPT,0),
4561 false);
4562
4563 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4564 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4565 if (LHS && RHS) { // We have 2 user-defined types.
4566 if (LHS != RHS) {
4567 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4568 return false;
4569 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4570 return true;
4571 }
4572 else
4573 return true;
4574 }
4575 return false;
4576}
4577
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004578/// getIntersectionOfProtocols - This routine finds the intersection of set
4579/// of protocols inherited from two distinct objective-c pointer objects.
4580/// It is used to build composite qualifier list of the composite type of
4581/// the conditional expression involving two objective-c pointer objects.
4582static
4583void getIntersectionOfProtocols(ASTContext &Context,
4584 const ObjCObjectPointerType *LHSOPT,
4585 const ObjCObjectPointerType *RHSOPT,
4586 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4587
John McCall8b07ec22010-05-15 11:32:37 +00004588 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4589 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4590 assert(LHS->getInterface() && "LHS must have an interface base");
4591 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004592
4593 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4594 unsigned LHSNumProtocols = LHS->getNumProtocols();
4595 if (LHSNumProtocols > 0)
4596 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4597 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004598 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004599 Context.CollectInheritedProtocols(LHS->getInterface(),
4600 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004601 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4602 LHSInheritedProtocols.end());
4603 }
4604
4605 unsigned RHSNumProtocols = RHS->getNumProtocols();
4606 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004607 ObjCProtocolDecl **RHSProtocols =
4608 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004609 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4610 if (InheritedProtocolSet.count(RHSProtocols[i]))
4611 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4612 }
4613 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004614 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004615 Context.CollectInheritedProtocols(RHS->getInterface(),
4616 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004617 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4618 RHSInheritedProtocols.begin(),
4619 E = RHSInheritedProtocols.end(); I != E; ++I)
4620 if (InheritedProtocolSet.count((*I)))
4621 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004622 }
4623}
4624
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004625/// areCommonBaseCompatible - Returns common base class of the two classes if
4626/// one found. Note that this is O'2 algorithm. But it will be called as the
4627/// last type comparison in a ?-exp of ObjC pointer types before a
4628/// warning is issued. So, its invokation is extremely rare.
4629QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004630 const ObjCObjectPointerType *Lptr,
4631 const ObjCObjectPointerType *Rptr) {
4632 const ObjCObjectType *LHS = Lptr->getObjectType();
4633 const ObjCObjectType *RHS = Rptr->getObjectType();
4634 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4635 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4636 if (!LDecl || !RDecl)
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004637 return QualType();
4638
John McCall8b07ec22010-05-15 11:32:37 +00004639 while ((LDecl = LDecl->getSuperClass())) {
4640 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004641 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004642 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4643 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4644
4645 QualType Result = QualType(LHS, 0);
4646 if (!Protocols.empty())
4647 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4648 Result = getObjCObjectPointerType(Result);
4649 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004650 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004651 }
4652
4653 return QualType();
4654}
4655
John McCall8b07ec22010-05-15 11:32:37 +00004656bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4657 const ObjCObjectType *RHS) {
4658 assert(LHS->getInterface() && "LHS is not an interface type");
4659 assert(RHS->getInterface() && "RHS is not an interface type");
4660
Chris Lattner49af6a42008-04-07 06:51:04 +00004661 // Verify that the base decls are compatible: the RHS must be a subclass of
4662 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00004663 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00004664 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004665
Chris Lattner49af6a42008-04-07 06:51:04 +00004666 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4667 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004668 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004669 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004670
Chris Lattner49af6a42008-04-07 06:51:04 +00004671 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4672 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004673 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004674 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004675
John McCall8b07ec22010-05-15 11:32:37 +00004676 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4677 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004678 LHSPI != LHSPE; LHSPI++) {
4679 bool RHSImplementsProtocol = false;
4680
4681 // If the RHS doesn't implement the protocol on the left, the types
4682 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00004683 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4684 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004685 RHSPI != RHSPE; RHSPI++) {
4686 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004687 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004688 break;
4689 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004690 }
4691 // FIXME: For better diagnostics, consider passing back the protocol name.
4692 if (!RHSImplementsProtocol)
4693 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004694 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004695 // The RHS implements all protocols listed on the LHS.
4696 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004697}
4698
Steve Naroffb7605152009-02-12 17:52:19 +00004699bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4700 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004701 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4702 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004703
Steve Naroff7cae42b2009-07-10 23:34:53 +00004704 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004705 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004706
4707 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4708 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004709}
4710
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00004711bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
4712 return canAssignObjCInterfaces(
4713 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
4714 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
4715}
4716
Mike Stump11289f42009-09-09 15:08:12 +00004717/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004718/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004719/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004720/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004721bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
4722 bool CompareUnqualified) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004723 if (getLangOptions().CPlusPlus)
4724 return hasSameType(LHS, RHS);
4725
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004726 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman47f77112008-08-22 00:56:42 +00004727}
4728
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004729bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4730 return !mergeTypes(LHS, RHS, true).isNull();
4731}
4732
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004733/// mergeTransparentUnionType - if T is a transparent union type and a member
4734/// of T is compatible with SubType, return the merged type, else return
4735/// QualType()
4736QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
4737 bool OfBlockPointer,
4738 bool Unqualified) {
4739 if (const RecordType *UT = T->getAsUnionType()) {
4740 RecordDecl *UD = UT->getDecl();
4741 if (UD->hasAttr<TransparentUnionAttr>()) {
4742 for (RecordDecl::field_iterator it = UD->field_begin(),
4743 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbourne19b961d2010-12-02 21:00:06 +00004744 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004745 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
4746 if (!MT.isNull())
4747 return MT;
4748 }
4749 }
4750 }
4751
4752 return QualType();
4753}
4754
4755/// mergeFunctionArgumentTypes - merge two types which appear as function
4756/// argument types
4757QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
4758 bool OfBlockPointer,
4759 bool Unqualified) {
4760 // GNU extension: two types are compatible if they appear as a function
4761 // argument, one of the types is a transparent union type and the other
4762 // type is compatible with a union member
4763 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
4764 Unqualified);
4765 if (!lmerge.isNull())
4766 return lmerge;
4767
4768 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
4769 Unqualified);
4770 if (!rmerge.isNull())
4771 return rmerge;
4772
4773 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
4774}
4775
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004776QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004777 bool OfBlockPointer,
4778 bool Unqualified) {
John McCall9dd450b2009-09-21 23:43:11 +00004779 const FunctionType *lbase = lhs->getAs<FunctionType>();
4780 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004781 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4782 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004783 bool allLTypes = true;
4784 bool allRTypes = true;
4785
4786 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004787 QualType retType;
4788 if (OfBlockPointer)
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004789 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true,
4790 Unqualified);
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004791 else
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004792 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(),
4793 false, Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00004794 if (retType.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004795
4796 if (Unqualified)
4797 retType = retType.getUnqualifiedType();
4798
4799 CanQualType LRetType = getCanonicalType(lbase->getResultType());
4800 CanQualType RRetType = getCanonicalType(rbase->getResultType());
4801 if (Unqualified) {
4802 LRetType = LRetType.getUnqualifiedType();
4803 RRetType = RRetType.getUnqualifiedType();
4804 }
4805
4806 if (getCanonicalType(retType) != LRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00004807 allLTypes = false;
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004808 if (getCanonicalType(retType) != RRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00004809 allRTypes = false;
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004810 // FIXME: double check this
4811 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4812 // rbase->getRegParmAttr() != 0 &&
4813 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004814 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4815 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004816 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4817 lbaseInfo.getRegParm();
4818 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4819 if (NoReturn != lbaseInfo.getNoReturn() ||
4820 RegParm != lbaseInfo.getRegParm())
4821 allLTypes = false;
4822 if (NoReturn != rbaseInfo.getNoReturn() ||
4823 RegParm != rbaseInfo.getRegParm())
4824 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004825 CallingConv lcc = lbaseInfo.getCC();
4826 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004827 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004828 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004829 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004830
Eli Friedman47f77112008-08-22 00:56:42 +00004831 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004832 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4833 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004834 unsigned lproto_nargs = lproto->getNumArgs();
4835 unsigned rproto_nargs = rproto->getNumArgs();
4836
4837 // Compatible functions must have the same number of arguments
4838 if (lproto_nargs != rproto_nargs)
4839 return QualType();
4840
4841 // Variadic and non-variadic functions aren't compatible
4842 if (lproto->isVariadic() != rproto->isVariadic())
4843 return QualType();
4844
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004845 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4846 return QualType();
4847
Eli Friedman47f77112008-08-22 00:56:42 +00004848 // Check argument compatibility
4849 llvm::SmallVector<QualType, 10> types;
4850 for (unsigned i = 0; i < lproto_nargs; i++) {
4851 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4852 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004853 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
4854 OfBlockPointer,
4855 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00004856 if (argtype.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004857
4858 if (Unqualified)
4859 argtype = argtype.getUnqualifiedType();
4860
Eli Friedman47f77112008-08-22 00:56:42 +00004861 types.push_back(argtype);
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004862 if (Unqualified) {
4863 largtype = largtype.getUnqualifiedType();
4864 rargtype = rargtype.getUnqualifiedType();
4865 }
4866
Chris Lattner465fa322008-10-05 17:34:18 +00004867 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4868 allLTypes = false;
4869 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4870 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004871 }
4872 if (allLTypes) return lhs;
4873 if (allRTypes) return rhs;
4874 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004875 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004876 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004877 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004878 }
4879
4880 if (lproto) allRTypes = false;
4881 if (rproto) allLTypes = false;
4882
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004883 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004884 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004885 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004886 if (proto->isVariadic()) return QualType();
4887 // Check that the types are compatible with the types that
4888 // would result from default argument promotions (C99 6.7.5.3p15).
4889 // The only types actually affected are promotable integer
4890 // types and floats, which would be passed as a different
4891 // type depending on whether the prototype is visible.
4892 unsigned proto_nargs = proto->getNumArgs();
4893 for (unsigned i = 0; i < proto_nargs; ++i) {
4894 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004895
4896 // Look at the promotion type of enum types, since that is the type used
4897 // to pass enum values.
4898 if (const EnumType *Enum = argTy->getAs<EnumType>())
4899 argTy = Enum->getDecl()->getPromotionType();
4900
Eli Friedman47f77112008-08-22 00:56:42 +00004901 if (argTy->isPromotableIntegerType() ||
4902 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4903 return QualType();
4904 }
4905
4906 if (allLTypes) return lhs;
4907 if (allRTypes) return rhs;
4908 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004909 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004910 proto->getTypeQuals(),
4911 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004912 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004913 }
4914
4915 if (allLTypes) return lhs;
4916 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004917 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004918 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004919}
4920
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004921QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004922 bool OfBlockPointer,
4923 bool Unqualified) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004924 // C++ [expr]: If an expression initially has the type "reference to T", the
4925 // type is adjusted to "T" prior to any further analysis, the expression
4926 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004927 // expression is an lvalue unless the reference is an rvalue reference and
4928 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004929 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4930 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004931
4932 if (Unqualified) {
4933 LHS = LHS.getUnqualifiedType();
4934 RHS = RHS.getUnqualifiedType();
4935 }
Douglas Gregor21e771e2010-02-03 21:02:30 +00004936
Eli Friedman47f77112008-08-22 00:56:42 +00004937 QualType LHSCan = getCanonicalType(LHS),
4938 RHSCan = getCanonicalType(RHS);
4939
4940 // If two types are identical, they are compatible.
4941 if (LHSCan == RHSCan)
4942 return LHS;
4943
John McCall8ccfcb52009-09-24 19:53:00 +00004944 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004945 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4946 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004947 if (LQuals != RQuals) {
4948 // If any of these qualifiers are different, we have a type
4949 // mismatch.
4950 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4951 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4952 return QualType();
4953
4954 // Exactly one GC qualifier difference is allowed: __strong is
4955 // okay if the other type has no GC qualifier but is an Objective
4956 // C object pointer (i.e. implicitly strong by default). We fix
4957 // this by pretending that the unqualified type was actually
4958 // qualified __strong.
4959 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4960 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4961 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4962
4963 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4964 return QualType();
4965
4966 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4967 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4968 }
4969 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4970 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4971 }
Eli Friedman47f77112008-08-22 00:56:42 +00004972 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004973 }
4974
4975 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004976
Eli Friedmandcca6332009-06-01 01:22:52 +00004977 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4978 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004979
Chris Lattnerfd652912008-01-14 05:45:46 +00004980 // We want to consider the two function types to be the same for these
4981 // comparisons, just force one to the other.
4982 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4983 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004984
4985 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004986 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4987 LHSClass = Type::ConstantArray;
4988 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4989 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004990
John McCall8b07ec22010-05-15 11:32:37 +00004991 // ObjCInterfaces are just specialized ObjCObjects.
4992 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
4993 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
4994
Nate Begemance4d7fc2008-04-18 23:10:10 +00004995 // Canonicalize ExtVector -> Vector.
4996 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4997 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004998
Chris Lattner95554662008-04-07 05:43:21 +00004999 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005000 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00005001 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00005002 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00005003 // Compatibility is based on the underlying type, not the promotion
5004 // type.
John McCall9dd450b2009-09-21 23:43:11 +00005005 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005006 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5007 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005008 }
John McCall9dd450b2009-09-21 23:43:11 +00005009 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005010 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5011 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005012 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005013
Eli Friedman47f77112008-08-22 00:56:42 +00005014 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005015 }
Eli Friedman47f77112008-08-22 00:56:42 +00005016
Steve Naroffc6edcbd2008-01-09 22:43:08 +00005017 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005018 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005019#define TYPE(Class, Base)
5020#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00005021#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005022#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5023#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5024#include "clang/AST/TypeNodes.def"
5025 assert(false && "Non-canonical and dependent types shouldn't get here");
5026 return QualType();
5027
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005028 case Type::LValueReference:
5029 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005030 case Type::MemberPointer:
5031 assert(false && "C++ should never be in mergeTypes");
5032 return QualType();
5033
John McCall8b07ec22010-05-15 11:32:37 +00005034 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005035 case Type::IncompleteArray:
5036 case Type::VariableArray:
5037 case Type::FunctionProto:
5038 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005039 assert(false && "Types are eliminated above");
5040 return QualType();
5041
Chris Lattnerfd652912008-01-14 05:45:46 +00005042 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00005043 {
5044 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005045 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5046 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005047 if (Unqualified) {
5048 LHSPointee = LHSPointee.getUnqualifiedType();
5049 RHSPointee = RHSPointee.getUnqualifiedType();
5050 }
5051 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5052 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005053 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00005054 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005055 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00005056 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005057 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005058 return getPointerType(ResultType);
5059 }
Steve Naroff68e167d2008-12-10 17:49:55 +00005060 case Type::BlockPointer:
5061 {
5062 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005063 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5064 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005065 if (Unqualified) {
5066 LHSPointee = LHSPointee.getUnqualifiedType();
5067 RHSPointee = RHSPointee.getUnqualifiedType();
5068 }
5069 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5070 Unqualified);
Steve Naroff68e167d2008-12-10 17:49:55 +00005071 if (ResultType.isNull()) return QualType();
5072 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5073 return LHS;
5074 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5075 return RHS;
5076 return getBlockPointerType(ResultType);
5077 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005078 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00005079 {
5080 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5081 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5082 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5083 return QualType();
5084
5085 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5086 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005087 if (Unqualified) {
5088 LHSElem = LHSElem.getUnqualifiedType();
5089 RHSElem = RHSElem.getUnqualifiedType();
5090 }
5091
5092 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005093 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00005094 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5095 return LHS;
5096 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5097 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00005098 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5099 ArrayType::ArraySizeModifier(), 0);
5100 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5101 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005102 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5103 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00005104 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5105 return LHS;
5106 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5107 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005108 if (LVAT) {
5109 // FIXME: This isn't correct! But tricky to implement because
5110 // the array's size has to be the size of LHS, but the type
5111 // has to be different.
5112 return LHS;
5113 }
5114 if (RVAT) {
5115 // FIXME: This isn't correct! But tricky to implement because
5116 // the array's size has to be the size of RHS, but the type
5117 // has to be different.
5118 return RHS;
5119 }
Eli Friedman3e62c212008-08-22 01:48:21 +00005120 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5121 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00005122 return getIncompleteArrayType(ResultType,
5123 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005124 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005125 case Type::FunctionNoProto:
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005126 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005127 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005128 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00005129 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00005130 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005131 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00005132 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00005133 case Type::Complex:
5134 // Distinct complex types are incompatible.
5135 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005136 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00005137 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00005138 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5139 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00005140 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00005141 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00005142 case Type::ObjCObject: {
5143 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00005144 // FIXME: This should be type compatibility, e.g. whether
5145 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00005146 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5147 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5148 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00005149 return LHS;
5150
Eli Friedman47f77112008-08-22 00:56:42 +00005151 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00005152 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00005153 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005154 if (OfBlockPointer) {
5155 if (canAssignObjCInterfacesInBlockPointer(
5156 LHS->getAs<ObjCObjectPointerType>(),
5157 RHS->getAs<ObjCObjectPointerType>()))
5158 return LHS;
5159 return QualType();
5160 }
John McCall9dd450b2009-09-21 23:43:11 +00005161 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5162 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00005163 return LHS;
5164
Steve Naroffc68cfcf2008-12-10 22:14:21 +00005165 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005166 }
Steve Naroff32e44c02007-10-15 20:41:53 +00005167 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005168
5169 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005170}
Ted Kremenekfc581a92007-10-31 17:10:13 +00005171
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005172/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5173/// 'RHS' attributes and returns the merged version; including for function
5174/// return types.
5175QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5176 QualType LHSCan = getCanonicalType(LHS),
5177 RHSCan = getCanonicalType(RHS);
5178 // If two types are identical, they are compatible.
5179 if (LHSCan == RHSCan)
5180 return LHS;
5181 if (RHSCan->isFunctionType()) {
5182 if (!LHSCan->isFunctionType())
5183 return QualType();
5184 QualType OldReturnType =
5185 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5186 QualType NewReturnType =
5187 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5188 QualType ResReturnType =
5189 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5190 if (ResReturnType.isNull())
5191 return QualType();
5192 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5193 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5194 // In either case, use OldReturnType to build the new function type.
5195 const FunctionType *F = LHS->getAs<FunctionType>();
5196 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
5197 FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
5198 QualType ResultType
5199 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
5200 FPT->getNumArgs(), FPT->isVariadic(),
5201 FPT->getTypeQuals(),
5202 FPT->hasExceptionSpec(),
5203 FPT->hasAnyExceptionSpec(),
5204 FPT->getNumExceptions(),
5205 FPT->exception_begin(),
5206 Info);
5207 return ResultType;
5208 }
5209 }
5210 return QualType();
5211 }
5212
5213 // If the qualifiers are different, the types can still be merged.
5214 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5215 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5216 if (LQuals != RQuals) {
5217 // If any of these qualifiers are different, we have a type mismatch.
5218 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5219 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5220 return QualType();
5221
5222 // Exactly one GC qualifier difference is allowed: __strong is
5223 // okay if the other type has no GC qualifier but is an Objective
5224 // C object pointer (i.e. implicitly strong by default). We fix
5225 // this by pretending that the unqualified type was actually
5226 // qualified __strong.
5227 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5228 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5229 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5230
5231 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5232 return QualType();
5233
5234 if (GC_L == Qualifiers::Strong)
5235 return LHS;
5236 if (GC_R == Qualifiers::Strong)
5237 return RHS;
5238 return QualType();
5239 }
5240
5241 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5242 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5243 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5244 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5245 if (ResQT == LHSBaseQT)
5246 return LHS;
5247 if (ResQT == RHSBaseQT)
5248 return RHS;
5249 }
5250 return QualType();
5251}
5252
Chris Lattner4ba0cef2008-04-07 07:01:58 +00005253//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005254// Integer Predicates
5255//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00005256
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005257unsigned ASTContext::getIntWidth(QualType T) {
John McCall56774992009-12-09 09:09:27 +00005258 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00005259 T = ET->getDecl()->getIntegerType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00005260 if (T->isBooleanType())
5261 return 1;
Eli Friedman1efaaea2009-02-13 02:31:07 +00005262 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005263 return (unsigned)getTypeSize(T);
5264}
5265
5266QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00005267 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00005268
5269 // Turn <4 x signed int> -> <4 x unsigned int>
5270 if (const VectorType *VTy = T->getAs<VectorType>())
5271 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsonaeb56442010-11-10 21:56:12 +00005272 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattnerec3a1562009-10-17 20:33:28 +00005273
5274 // For enums, we return the unsigned version of the base type.
5275 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005276 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00005277
5278 const BuiltinType *BTy = T->getAs<BuiltinType>();
5279 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005280 switch (BTy->getKind()) {
5281 case BuiltinType::Char_S:
5282 case BuiltinType::SChar:
5283 return UnsignedCharTy;
5284 case BuiltinType::Short:
5285 return UnsignedShortTy;
5286 case BuiltinType::Int:
5287 return UnsignedIntTy;
5288 case BuiltinType::Long:
5289 return UnsignedLongTy;
5290 case BuiltinType::LongLong:
5291 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00005292 case BuiltinType::Int128:
5293 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005294 default:
5295 assert(0 && "Unexpected signed integer type");
5296 return QualType();
5297 }
5298}
5299
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005300ExternalASTSource::~ExternalASTSource() { }
5301
5302void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00005303
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005304ASTMutationListener::~ASTMutationListener() { }
5305
Chris Lattnerecd79c62009-06-14 00:45:47 +00005306
5307//===----------------------------------------------------------------------===//
5308// Builtin Type Computation
5309//===----------------------------------------------------------------------===//
5310
5311/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattnerdc226c22010-10-01 22:42:38 +00005312/// pointer over the consumed characters. This returns the resultant type. If
5313/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5314/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5315/// a vector of "i*".
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005316///
5317/// RequiresICE is filled in on return to indicate whether the value is required
5318/// to be an Integer Constant Expression.
Mike Stump11289f42009-09-09 15:08:12 +00005319static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00005320 ASTContext::GetBuiltinTypeError &Error,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005321 bool &RequiresICE,
Chris Lattnerdc226c22010-10-01 22:42:38 +00005322 bool AllowTypeModifiers) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005323 // Modifiers.
5324 int HowLong = 0;
5325 bool Signed = false, Unsigned = false;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005326 RequiresICE = false;
Chris Lattner84733392010-10-01 07:13:18 +00005327
Chris Lattnerdc226c22010-10-01 22:42:38 +00005328 // Read the prefixed modifiers first.
Chris Lattnerecd79c62009-06-14 00:45:47 +00005329 bool Done = false;
5330 while (!Done) {
5331 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00005332 default: Done = true; --Str; break;
Chris Lattner84733392010-10-01 07:13:18 +00005333 case 'I':
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005334 RequiresICE = true;
Chris Lattner84733392010-10-01 07:13:18 +00005335 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005336 case 'S':
5337 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5338 assert(!Signed && "Can't use 'S' modifier multiple times!");
5339 Signed = true;
5340 break;
5341 case 'U':
5342 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5343 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5344 Unsigned = true;
5345 break;
5346 case 'L':
5347 assert(HowLong <= 2 && "Can't have LLLL modifier");
5348 ++HowLong;
5349 break;
5350 }
5351 }
5352
5353 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005354
Chris Lattnerecd79c62009-06-14 00:45:47 +00005355 // Read the base type.
5356 switch (*Str++) {
5357 default: assert(0 && "Unknown builtin type letter!");
5358 case 'v':
5359 assert(HowLong == 0 && !Signed && !Unsigned &&
5360 "Bad modifiers used with 'v'!");
5361 Type = Context.VoidTy;
5362 break;
5363 case 'f':
5364 assert(HowLong == 0 && !Signed && !Unsigned &&
5365 "Bad modifiers used with 'f'!");
5366 Type = Context.FloatTy;
5367 break;
5368 case 'd':
5369 assert(HowLong < 2 && !Signed && !Unsigned &&
5370 "Bad modifiers used with 'd'!");
5371 if (HowLong)
5372 Type = Context.LongDoubleTy;
5373 else
5374 Type = Context.DoubleTy;
5375 break;
5376 case 's':
5377 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5378 if (Unsigned)
5379 Type = Context.UnsignedShortTy;
5380 else
5381 Type = Context.ShortTy;
5382 break;
5383 case 'i':
5384 if (HowLong == 3)
5385 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5386 else if (HowLong == 2)
5387 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5388 else if (HowLong == 1)
5389 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5390 else
5391 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5392 break;
5393 case 'c':
5394 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5395 if (Signed)
5396 Type = Context.SignedCharTy;
5397 else if (Unsigned)
5398 Type = Context.UnsignedCharTy;
5399 else
5400 Type = Context.CharTy;
5401 break;
5402 case 'b': // boolean
5403 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5404 Type = Context.BoolTy;
5405 break;
5406 case 'z': // size_t.
5407 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5408 Type = Context.getSizeType();
5409 break;
5410 case 'F':
5411 Type = Context.getCFConstantStringType();
5412 break;
Fariborz Jahaniand11da7e2010-11-09 21:38:20 +00005413 case 'G':
5414 Type = Context.getObjCIdType();
5415 break;
5416 case 'H':
5417 Type = Context.getObjCSelType();
5418 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005419 case 'a':
5420 Type = Context.getBuiltinVaListType();
5421 assert(!Type.isNull() && "builtin va list type not initialized!");
5422 break;
5423 case 'A':
5424 // This is a "reference" to a va_list; however, what exactly
5425 // this means depends on how va_list is defined. There are two
5426 // different kinds of va_list: ones passed by value, and ones
5427 // passed by reference. An example of a by-value va_list is
5428 // x86, where va_list is a char*. An example of by-ref va_list
5429 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5430 // we want this argument to be a char*&; for x86-64, we want
5431 // it to be a __va_list_tag*.
5432 Type = Context.getBuiltinVaListType();
5433 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005434 if (Type->isArrayType())
Chris Lattnerecd79c62009-06-14 00:45:47 +00005435 Type = Context.getArrayDecayedType(Type);
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005436 else
Chris Lattnerecd79c62009-06-14 00:45:47 +00005437 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005438 break;
5439 case 'V': {
5440 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005441 unsigned NumElements = strtoul(Str, &End, 10);
5442 assert(End != Str && "Missing vector size");
Chris Lattnerecd79c62009-06-14 00:45:47 +00005443 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005444
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005445 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5446 RequiresICE, false);
5447 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattnerdc226c22010-10-01 22:42:38 +00005448
5449 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner37141f42010-06-23 06:00:24 +00005450 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00005451 VectorType::GenericVector);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005452 break;
5453 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005454 case 'X': {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005455 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5456 false);
5457 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005458 Type = Context.getComplexType(ElementType);
5459 break;
5460 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005461 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005462 Type = Context.getFILEType();
5463 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005464 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005465 return QualType();
5466 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005467 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005468 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005469 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005470 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005471 else
5472 Type = Context.getjmp_bufType();
5473
Mike Stump2adb4da2009-07-28 23:47:15 +00005474 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005475 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005476 return QualType();
5477 }
5478 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005479 }
Mike Stump11289f42009-09-09 15:08:12 +00005480
Chris Lattnerdc226c22010-10-01 22:42:38 +00005481 // If there are modifiers and if we're allowed to parse them, go for it.
5482 Done = !AllowTypeModifiers;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005483 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005484 switch (char c = *Str++) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005485 default: Done = true; --Str; break;
5486 case '*':
5487 case '&': {
5488 // Both pointers and references can have their pointee types
5489 // qualified with an address space.
5490 char *End;
5491 unsigned AddrSpace = strtoul(Str, &End, 10);
5492 if (End != Str && AddrSpace != 0) {
5493 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5494 Str = End;
5495 }
5496 if (c == '*')
5497 Type = Context.getPointerType(Type);
5498 else
5499 Type = Context.getLValueReferenceType(Type);
5500 break;
5501 }
5502 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5503 case 'C':
5504 Type = Type.withConst();
5505 break;
5506 case 'D':
5507 Type = Context.getVolatileType(Type);
5508 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005509 }
5510 }
Chris Lattner84733392010-10-01 07:13:18 +00005511
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005512 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner84733392010-10-01 07:13:18 +00005513 "Integer constant 'I' type must be an integer");
Mike Stump11289f42009-09-09 15:08:12 +00005514
Chris Lattnerecd79c62009-06-14 00:45:47 +00005515 return Type;
5516}
5517
5518/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattnerdc226c22010-10-01 22:42:38 +00005519QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005520 GetBuiltinTypeError &Error,
5521 unsigned *IntegerConstantArgs) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005522 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump11289f42009-09-09 15:08:12 +00005523
Chris Lattnerecd79c62009-06-14 00:45:47 +00005524 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005525
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005526 bool RequiresICE = false;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005527 Error = GE_None;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005528 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5529 RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005530 if (Error != GE_None)
5531 return QualType();
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005532
5533 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5534
Chris Lattnerecd79c62009-06-14 00:45:47 +00005535 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005536 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005537 if (Error != GE_None)
5538 return QualType();
5539
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005540 // If this argument is required to be an IntegerConstantExpression and the
5541 // caller cares, fill in the bitmask we return.
5542 if (RequiresICE && IntegerConstantArgs)
5543 *IntegerConstantArgs |= 1 << ArgTypes.size();
5544
Chris Lattnerecd79c62009-06-14 00:45:47 +00005545 // Do array -> pointer decay. The builtin should use the decayed type.
5546 if (Ty->isArrayType())
5547 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005548
Chris Lattnerecd79c62009-06-14 00:45:47 +00005549 ArgTypes.push_back(Ty);
5550 }
5551
5552 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5553 "'.' should only occur at end of builtin type list!");
5554
5555 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
5556 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
5557 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005558
5559 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00005560 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00005561 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005562 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00005563}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005564
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005565GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5566 GVALinkage External = GVA_StrongExternal;
5567
5568 Linkage L = FD->getLinkage();
5569 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5570 FD->getType()->getLinkage() == UniqueExternalLinkage)
5571 L = UniqueExternalLinkage;
5572
5573 switch (L) {
5574 case NoLinkage:
5575 case InternalLinkage:
5576 case UniqueExternalLinkage:
5577 return GVA_Internal;
5578
5579 case ExternalLinkage:
5580 switch (FD->getTemplateSpecializationKind()) {
5581 case TSK_Undeclared:
5582 case TSK_ExplicitSpecialization:
5583 External = GVA_StrongExternal;
5584 break;
5585
5586 case TSK_ExplicitInstantiationDefinition:
5587 return GVA_ExplicitTemplateInstantiation;
5588
5589 case TSK_ExplicitInstantiationDeclaration:
5590 case TSK_ImplicitInstantiation:
5591 External = GVA_TemplateInstantiation;
5592 break;
5593 }
5594 }
5595
5596 if (!FD->isInlined())
5597 return External;
5598
5599 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5600 // GNU or C99 inline semantics. Determine whether this symbol should be
5601 // externally visible.
5602 if (FD->isInlineDefinitionExternallyVisible())
5603 return External;
5604
5605 // C99 inline semantics, where the symbol is not externally visible.
5606 return GVA_C99Inline;
5607 }
5608
5609 // C++0x [temp.explicit]p9:
5610 // [ Note: The intent is that an inline function that is the subject of
5611 // an explicit instantiation declaration will still be implicitly
5612 // instantiated when used so that the body can be considered for
5613 // inlining, but that no out-of-line copy of the inline function would be
5614 // generated in the translation unit. -- end note ]
5615 if (FD->getTemplateSpecializationKind()
5616 == TSK_ExplicitInstantiationDeclaration)
5617 return GVA_C99Inline;
5618
5619 return GVA_CXXInline;
5620}
5621
5622GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
5623 // If this is a static data member, compute the kind of template
5624 // specialization. Otherwise, this variable is not part of a
5625 // template.
5626 TemplateSpecializationKind TSK = TSK_Undeclared;
5627 if (VD->isStaticDataMember())
5628 TSK = VD->getTemplateSpecializationKind();
5629
5630 Linkage L = VD->getLinkage();
5631 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5632 VD->getType()->getLinkage() == UniqueExternalLinkage)
5633 L = UniqueExternalLinkage;
5634
5635 switch (L) {
5636 case NoLinkage:
5637 case InternalLinkage:
5638 case UniqueExternalLinkage:
5639 return GVA_Internal;
5640
5641 case ExternalLinkage:
5642 switch (TSK) {
5643 case TSK_Undeclared:
5644 case TSK_ExplicitSpecialization:
5645 return GVA_StrongExternal;
5646
5647 case TSK_ExplicitInstantiationDeclaration:
5648 llvm_unreachable("Variable should not be instantiated");
5649 // Fall through to treat this like any other instantiation.
5650
5651 case TSK_ExplicitInstantiationDefinition:
5652 return GVA_ExplicitTemplateInstantiation;
5653
5654 case TSK_ImplicitInstantiation:
5655 return GVA_TemplateInstantiation;
5656 }
5657 }
5658
5659 return GVA_StrongExternal;
5660}
5661
Argyrios Kyrtzidisc9049332010-07-29 20:08:05 +00005662bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005663 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
5664 if (!VD->isFileVarDecl())
5665 return false;
5666 } else if (!isa<FunctionDecl>(D))
5667 return false;
5668
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00005669 // Weak references don't produce any output by themselves.
5670 if (D->hasAttr<WeakRefAttr>())
5671 return false;
5672
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005673 // Aliases and used decls are required.
5674 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
5675 return true;
5676
5677 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5678 // Forward declarations aren't required.
5679 if (!FD->isThisDeclarationADefinition())
5680 return false;
5681
5682 // Constructors and destructors are required.
5683 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
5684 return true;
5685
5686 // The key function for a class is required.
5687 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5688 const CXXRecordDecl *RD = MD->getParent();
5689 if (MD->isOutOfLine() && RD->isDynamicClass()) {
5690 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
5691 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
5692 return true;
5693 }
5694 }
5695
5696 GVALinkage Linkage = GetGVALinkageForFunction(FD);
5697
5698 // static, static inline, always_inline, and extern inline functions can
5699 // always be deferred. Normal inline functions can be deferred in C99/C++.
5700 // Implicit template instantiations can also be deferred in C++.
5701 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
5702 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
5703 return false;
5704 return true;
5705 }
5706
5707 const VarDecl *VD = cast<VarDecl>(D);
5708 assert(VD->isFileVarDecl() && "Expected file scoped var");
5709
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00005710 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
5711 return false;
5712
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005713 // Structs that have non-trivial constructors or destructors are required.
5714
5715 // FIXME: Handle references.
5716 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
5717 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +00005718 if (RD->hasDefinition() &&
5719 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005720 return true;
5721 }
5722 }
5723
5724 GVALinkage L = GetGVALinkageForVariable(VD);
5725 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
5726 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
5727 return false;
5728 }
5729
5730 return true;
5731}
Charles Davis53c59df2010-08-16 03:33:14 +00005732
Charles Davis99202b32010-11-09 18:04:24 +00005733CallingConv ASTContext::getDefaultMethodCallConv() {
5734 // Pass through to the C++ ABI object
5735 return ABI->getDefaultMethodCallConv();
5736}
5737
Anders Carlsson60a62632010-11-25 01:51:53 +00005738bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) {
5739 // Pass through to the C++ ABI object
5740 return ABI->isNearlyEmpty(RD);
5741}
5742
Charles Davis53c59df2010-08-16 03:33:14 +00005743CXXABI::~CXXABI() {}