blob: 149ecbc5998d2012080a0df973c18550517fcd06 [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()),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000112 NTTP->isParameterPack(),
Douglas Gregor7dbfb462010-06-16 21:09:37 +0000113 0));
114 else
115 CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
116 cast<TemplateTemplateParmDecl>(*P)));
117 }
118
119 TemplateTemplateParmDecl *CanonTTP
120 = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
121 SourceLocation(), TTP->getDepth(),
122 TTP->getPosition(), 0,
123 TemplateParameterList::Create(*this, SourceLocation(),
124 SourceLocation(),
125 CanonParams.data(),
126 CanonParams.size(),
127 SourceLocation()));
128
129 // Get the new insert position for the node we care about.
130 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
131 assert(Canonical == 0 && "Shouldn't be in the map!");
132 (void)Canonical;
133
134 // Create the canonical template template parameter entry.
135 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
136 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
137 return CanonTTP;
138}
139
Charles Davis53c59df2010-08-16 03:33:14 +0000140CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
John McCall86353412010-08-21 22:46:04 +0000141 if (!LangOpts.CPlusPlus) return 0;
142
Charles Davis6bcb07a2010-08-19 02:18:14 +0000143 switch (T.getCXXABI()) {
John McCall86353412010-08-21 22:46:04 +0000144 case CXXABI_ARM:
145 return CreateARMCXXABI(*this);
146 case CXXABI_Itanium:
Charles Davis53c59df2010-08-16 03:33:14 +0000147 return CreateItaniumCXXABI(*this);
Charles Davis6bcb07a2010-08-19 02:18:14 +0000148 case CXXABI_Microsoft:
149 return CreateMicrosoftCXXABI(*this);
150 }
John McCall86353412010-08-21 22:46:04 +0000151 return 0;
Charles Davis53c59df2010-08-16 03:33:14 +0000152}
153
Chris Lattner465fa322008-10-05 17:34:18 +0000154ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar1b444192009-11-13 05:51:54 +0000155 const TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +0000156 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +0000157 Builtin::Context &builtins,
Douglas Gregor5b11d492010-07-25 17:53:33 +0000158 unsigned size_reserve) :
John McCall773cc982010-06-11 11:07:21 +0000159 TemplateSpecializationTypes(this_()),
160 DependentTemplateSpecializationTypes(this_()),
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +0000161 GlobalNestedNameSpecifier(0), IsInt128Installed(false),
162 CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +0000163 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +0000164 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
John McCall8cb7bdf2010-06-04 23:28:52 +0000165 NullTypeSourceInfo(QualType()),
Charles Davis53c59df2010-08-16 03:33:14 +0000166 SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000167 Idents(idents), Selectors(sels),
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000168 BuiltinInfo(builtins),
169 DeclarationNames(*this),
Argyrios Kyrtzidis440ea322010-10-28 09:29:35 +0000170 ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
Ted Kremenek520f47b2010-06-18 00:31:04 +0000171 LastSDM(0, 0),
172 UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) {
David Chisnall9f57c292009-08-17 16:35:33 +0000173 ObjCIdRedefinitionType = QualType();
174 ObjCClassRedefinitionType = QualType();
Douglas Gregor5b11d492010-07-25 17:53:33 +0000175 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000176 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +0000177 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000178 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +0000179}
180
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000181ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000182 // Release the DenseMaps associated with DeclContext objects.
183 // FIXME: Is this the ideal solution?
184 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +0000185
Douglas Gregor5b11d492010-07-25 17:53:33 +0000186 // Call all of the deallocation functions.
187 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
188 Deallocations[I].first(Deallocations[I].second);
Douglas Gregor1a809332010-05-23 18:26:36 +0000189
Douglas Gregor832940b2010-03-02 23:58:15 +0000190 // Release all of the memory associated with overridden C++ methods.
191 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
192 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
193 OM != OMEnd; ++OM)
194 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000195
Ted Kremenek076baeb2010-06-08 23:00:58 +0000196 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
Douglas Gregor5b11d492010-07-25 17:53:33 +0000197 // because they can contain DenseMaps.
198 for (llvm::DenseMap<const ObjCContainerDecl*,
199 const ASTRecordLayout*>::iterator
200 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
201 // Increment in loop to prevent using deallocated memory.
202 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
203 R->Destroy(*this);
204
Ted Kremenek076baeb2010-06-08 23:00:58 +0000205 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
206 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
207 // Increment in loop to prevent using deallocated memory.
208 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
209 R->Destroy(*this);
210 }
Douglas Gregor561eceb2010-08-30 16:49:28 +0000211
212 for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
213 AEnd = DeclAttrs.end();
214 A != AEnd; ++A)
215 A->second->~AttrVec();
216}
Douglas Gregorf21eb492009-03-26 23:50:42 +0000217
Douglas Gregor1a809332010-05-23 18:26:36 +0000218void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
219 Deallocations.push_back(std::make_pair(Callback, Data));
220}
221
Mike Stump11289f42009-09-09 15:08:12 +0000222void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
224 ExternalSource.reset(Source.take());
225}
226
Chris Lattner4eb445d2007-01-26 01:27:23 +0000227void ASTContext::PrintStats() const {
228 fprintf(stderr, "*** AST Context Stats:\n");
229 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000230
Douglas Gregora30d0462009-05-26 14:40:08 +0000231 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000232#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000233#define ABSTRACT_TYPE(Name, Parent)
234#include "clang/AST/TypeNodes.def"
235 0 // Extra
236 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000237
Chris Lattner4eb445d2007-01-26 01:27:23 +0000238 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
239 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000240 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000241 }
242
Douglas Gregora30d0462009-05-26 14:40:08 +0000243 unsigned Idx = 0;
244 unsigned TotalBytes = 0;
245#define TYPE(Name, Parent) \
246 if (counts[Idx]) \
247 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
248 TotalBytes += counts[Idx] * sizeof(Name##Type); \
249 ++Idx;
250#define ABSTRACT_TYPE(Name, Parent)
251#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000252
Douglas Gregora30d0462009-05-26 14:40:08 +0000253 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor747eb782010-07-08 06:14:04 +0000254
Douglas Gregor7454c562010-07-02 20:37:36 +0000255 // Implicit special member functions.
Douglas Gregor9672f922010-07-03 00:47:00 +0000256 fprintf(stderr, " %u/%u implicit default constructors created\n",
257 NumImplicitDefaultConstructorsDeclared,
258 NumImplicitDefaultConstructors);
Douglas Gregora6d69502010-07-02 23:41:54 +0000259 fprintf(stderr, " %u/%u implicit copy constructors created\n",
260 NumImplicitCopyConstructorsDeclared,
261 NumImplicitCopyConstructors);
Douglas Gregor330b9cf2010-07-02 21:50:04 +0000262 fprintf(stderr, " %u/%u implicit copy assignment operators created\n",
263 NumImplicitCopyAssignmentOperatorsDeclared,
264 NumImplicitCopyAssignmentOperators);
Douglas Gregor7454c562010-07-02 20:37:36 +0000265 fprintf(stderr, " %u/%u implicit destructors created\n",
266 NumImplicitDestructorsDeclared, NumImplicitDestructors);
267
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000268 if (ExternalSource.get()) {
269 fprintf(stderr, "\n");
270 ExternalSource->PrintStats();
271 }
Douglas Gregor747eb782010-07-08 06:14:04 +0000272
Douglas Gregor5b11d492010-07-25 17:53:33 +0000273 BumpAlloc.PrintStats();
Chris Lattner4eb445d2007-01-26 01:27:23 +0000274}
275
276
John McCall48f2d582009-10-23 23:03:21 +0000277void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000278 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000279 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000280 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000281}
282
Chris Lattner970e54e2006-11-12 00:37:36 +0000283void ASTContext::InitBuiltinTypes() {
284 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattner970e54e2006-11-12 00:37:36 +0000286 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000287 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner970e54e2006-11-12 00:37:36 +0000289 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000290 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000291 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000292 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000293 InitBuiltinType(CharTy, BuiltinType::Char_S);
294 else
295 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000296 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000297 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
298 InitBuiltinType(ShortTy, BuiltinType::Short);
299 InitBuiltinType(IntTy, BuiltinType::Int);
300 InitBuiltinType(LongTy, BuiltinType::Long);
301 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000302
Chris Lattner970e54e2006-11-12 00:37:36 +0000303 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000304 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
305 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
306 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
307 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
308 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattner970e54e2006-11-12 00:37:36 +0000310 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000311 InitBuiltinType(FloatTy, BuiltinType::Float);
312 InitBuiltinType(DoubleTy, BuiltinType::Double);
313 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000314
Chris Lattnerf122cef2009-04-30 02:43:43 +0000315 // GNU extension, 128-bit integers.
316 InitBuiltinType(Int128Ty, BuiltinType::Int128);
317 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
318
Chris Lattnerad3467e2010-12-25 23:25:43 +0000319 if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
320 if (!LangOpts.ShortWChar)
321 InitBuiltinType(WCharTy, BuiltinType::WChar_S);
322 else // -fshort-wchar makes wchar_t be unsigned.
323 InitBuiltinType(WCharTy, BuiltinType::WChar_U);
324 } else // C99
Chris Lattner007cb022009-02-26 23:43:47 +0000325 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000326
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000327 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
328 InitBuiltinType(Char16Ty, BuiltinType::Char16);
329 else // C99
330 Char16Ty = getFromTargetType(Target.getChar16Type());
331
332 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
333 InitBuiltinType(Char32Ty, BuiltinType::Char32);
334 else // C99
335 Char32Ty = getFromTargetType(Target.getChar32Type());
336
Douglas Gregor4619e432008-12-05 23:32:09 +0000337 // Placeholder type for type-dependent expressions whose type is
338 // completely unknown. No code should ever check a type against
339 // DependentTy and users should never see it; however, it is here to
340 // help diagnose failures to properly check for type-dependent
341 // expressions.
342 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000343
John McCall36e7fe32010-10-12 00:20:44 +0000344 // Placeholder type for functions.
345 InitBuiltinType(OverloadTy, BuiltinType::Overload);
346
Mike Stump11289f42009-09-09 15:08:12 +0000347 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000348 // not yet been deduced.
John McCall36e7fe32010-10-12 00:20:44 +0000349 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000350
Chris Lattner970e54e2006-11-12 00:37:36 +0000351 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000352 FloatComplexTy = getComplexType(FloatTy);
353 DoubleComplexTy = getComplexType(DoubleTy);
354 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000355
Steve Naroff66e9f332007-10-15 14:41:52 +0000356 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000357
Steve Naroff1329fa02009-07-15 18:40:39 +0000358 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
359 ObjCIdTypedefType = QualType();
360 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000361 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000362
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000363 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000364 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
365 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000366 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000367
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000368 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000369
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000370 // void * type
371 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000372
373 // nullptr type (C++0x 2.14.7)
374 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000375}
376
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000377Diagnostic &ASTContext::getDiagnostics() const {
378 return SourceMgr.getDiagnostics();
379}
380
Douglas Gregor561eceb2010-08-30 16:49:28 +0000381AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
382 AttrVec *&Result = DeclAttrs[D];
383 if (!Result) {
384 void *Mem = Allocate(sizeof(AttrVec));
385 Result = new (Mem) AttrVec;
386 }
387
388 return *Result;
389}
390
391/// \brief Erase the attributes corresponding to the given declaration.
392void ASTContext::eraseDeclAttrs(const Decl *D) {
393 llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
394 if (Pos != DeclAttrs.end()) {
395 Pos->second->~AttrVec();
396 DeclAttrs.erase(Pos);
397 }
398}
399
400
Douglas Gregor86d142a2009-10-08 07:24:58 +0000401MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000402ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000403 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000404 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000405 = InstantiatedFromStaticDataMember.find(Var);
406 if (Pos == InstantiatedFromStaticDataMember.end())
407 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000409 return Pos->second;
410}
411
Mike Stump11289f42009-09-09 15:08:12 +0000412void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000413ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000414 TemplateSpecializationKind TSK,
415 SourceLocation PointOfInstantiation) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000416 assert(Inst->isStaticDataMember() && "Not a static data member");
417 assert(Tmpl->isStaticDataMember() && "Not a static data member");
418 assert(!InstantiatedFromStaticDataMember[Inst] &&
419 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000420 InstantiatedFromStaticDataMember[Inst]
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000421 = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000422}
423
John McCalle61f2ba2009-11-18 02:36:19 +0000424NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000425ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000426 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000427 = InstantiatedFromUsingDecl.find(UUD);
428 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000429 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000430
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000431 return Pos->second;
432}
433
434void
John McCallb96ec562009-12-04 22:46:56 +0000435ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
436 assert((isa<UsingDecl>(Pattern) ||
437 isa<UnresolvedUsingValueDecl>(Pattern) ||
438 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
439 "pattern decl is not a using decl");
440 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
441 InstantiatedFromUsingDecl[Inst] = Pattern;
442}
443
444UsingShadowDecl *
445ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
446 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
447 = InstantiatedFromUsingShadowDecl.find(Inst);
448 if (Pos == InstantiatedFromUsingShadowDecl.end())
449 return 0;
450
451 return Pos->second;
452}
453
454void
455ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
456 UsingShadowDecl *Pattern) {
457 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
458 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000459}
460
Anders Carlsson5da84842009-09-01 04:26:58 +0000461FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
462 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
463 = InstantiatedFromUnnamedFieldDecl.find(Field);
464 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
465 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000466
Anders Carlsson5da84842009-09-01 04:26:58 +0000467 return Pos->second;
468}
469
470void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
471 FieldDecl *Tmpl) {
472 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
473 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
474 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
475 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000476
Anders Carlsson5da84842009-09-01 04:26:58 +0000477 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
478}
479
Douglas Gregor832940b2010-03-02 23:58:15 +0000480ASTContext::overridden_cxx_method_iterator
481ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
482 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
483 = OverriddenMethods.find(Method);
484 if (Pos == OverriddenMethods.end())
485 return 0;
486
487 return Pos->second.begin();
488}
489
490ASTContext::overridden_cxx_method_iterator
491ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
492 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
493 = OverriddenMethods.find(Method);
494 if (Pos == OverriddenMethods.end())
495 return 0;
496
497 return Pos->second.end();
498}
499
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000500unsigned
501ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
502 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
503 = OverriddenMethods.find(Method);
504 if (Pos == OverriddenMethods.end())
505 return 0;
506
507 return Pos->second.size();
508}
509
Douglas Gregor832940b2010-03-02 23:58:15 +0000510void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
511 const CXXMethodDecl *Overridden) {
512 OverriddenMethods[Method].push_back(Overridden);
513}
514
Chris Lattner53cfe802007-07-18 17:52:12 +0000515//===----------------------------------------------------------------------===//
516// Type Sizing and Analysis
517//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000518
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000519/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
520/// scalar floating point type.
521const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000522 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000523 assert(BT && "Not a floating point type!");
524 switch (BT->getKind()) {
525 default: assert(0 && "Not a floating point type!");
526 case BuiltinType::Float: return Target.getFloatFormat();
527 case BuiltinType::Double: return Target.getDoubleFormat();
528 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
529 }
530}
531
Ken Dyck160146e2010-01-27 17:10:57 +0000532/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000533/// specified decl. Note that bitfields do not have a valid alignment, so
534/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000535/// If @p RefAsPointee, references are treated like their underlying type
536/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000537CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000538 unsigned Align = Target.getCharWidth();
539
John McCall94268702010-10-08 18:24:19 +0000540 bool UseAlignAttrOnly = false;
541 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
542 Align = AlignFromAttr;
Eli Friedman19a546c2009-02-22 02:56:25 +0000543
John McCall94268702010-10-08 18:24:19 +0000544 // __attribute__((aligned)) can increase or decrease alignment
545 // *except* on a struct or struct member, where it only increases
546 // alignment unless 'packed' is also specified.
547 //
548 // It is an error for [[align]] to decrease alignment, so we can
549 // ignore that possibility; Sema should diagnose it.
550 if (isa<FieldDecl>(D)) {
551 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
552 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
553 } else {
554 UseAlignAttrOnly = true;
555 }
556 }
557
558 if (UseAlignAttrOnly) {
559 // ignore type of value
560 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner68061312009-01-24 21:53:27 +0000561 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000562 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000563 if (RefAsPointee)
564 T = RT->getPointeeType();
565 else
566 T = getPointerType(RT->getPointeeType());
567 }
568 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindolae971b9a2010-06-04 23:15:27 +0000569 unsigned MinWidth = Target.getLargeArrayMinWidth();
570 unsigned ArrayAlign = Target.getLargeArrayAlign();
571 if (isa<VariableArrayType>(T) && MinWidth != 0)
572 Align = std::max(Align, ArrayAlign);
573 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
574 unsigned Size = getTypeSize(CT);
575 if (MinWidth != 0 && MinWidth <= Size)
576 Align = std::max(Align, ArrayAlign);
577 }
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000578 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000579 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
580 T = cast<ArrayType>(T)->getElementType();
581
582 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
583 }
Charles Davis3fc51072010-02-23 04:52:00 +0000584 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
585 // In the case of a field in a packed struct, we want the minimum
586 // of the alignment of the field and the alignment of the struct.
587 Align = std::min(Align,
588 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
589 }
Chris Lattner68061312009-01-24 21:53:27 +0000590 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000591
Ken Dyck160146e2010-01-27 17:10:57 +0000592 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000593}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000594
John McCall87fe5d52010-05-20 01:18:31 +0000595std::pair<CharUnits, CharUnits>
596ASTContext::getTypeInfoInChars(const Type *T) {
597 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
598 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
599 CharUnits::fromQuantity(Info.second / getCharWidth()));
600}
601
602std::pair<CharUnits, CharUnits>
603ASTContext::getTypeInfoInChars(QualType T) {
604 return getTypeInfoInChars(T.getTypePtr());
605}
606
Chris Lattner983a8bb2007-07-13 22:13:22 +0000607/// getTypeSize - Return the size of the specified type, in bits. This method
608/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000609///
610/// FIXME: Pointers into different addr spaces could have different sizes and
611/// alignment requirements: getPointerInfo should take an AddrSpace, this
612/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000613std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000614ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000615 uint64_t Width=0;
616 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000617 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000618#define TYPE(Class, Base)
619#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000620#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000621#define DEPENDENT_TYPE(Class, Base) case Type::Class:
622#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000623 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000624 break;
625
Chris Lattner355332d2007-07-13 22:27:08 +0000626 case Type::FunctionNoProto:
627 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000628 // GCC extension: alignof(function) = 32 bits
629 Width = 0;
630 Align = 32;
631 break;
632
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000633 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000634 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000635 Width = 0;
636 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
637 break;
638
Steve Naroff5c131802007-08-30 01:06:46 +0000639 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000640 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattner37e05872008-03-05 18:54:05 +0000642 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000643 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000644 Align = EltInfo.second;
645 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000646 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000647 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000648 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000649 const VectorType *VT = cast<VectorType>(T);
650 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
651 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000652 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000653 // If the alignment is not a power of 2, round up to the next power of 2.
654 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000655 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000656 Align = llvm::NextPowerOf2(Align);
657 Width = llvm::RoundUpToAlignment(Width, Align);
658 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000659 break;
660 }
Chris Lattner647fb222007-07-18 18:26:58 +0000661
Chris Lattner7570e9c2008-03-08 08:52:55 +0000662 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000663 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000664 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000665 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000666 // GCC extension: alignof(void) = 8 bits.
667 Width = 0;
668 Align = 8;
669 break;
670
Chris Lattner6a4f7452007-12-19 19:23:28 +0000671 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000672 Width = Target.getBoolWidth();
673 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000674 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000675 case BuiltinType::Char_S:
676 case BuiltinType::Char_U:
677 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000678 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000679 Width = Target.getCharWidth();
680 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000681 break;
Chris Lattnerad3467e2010-12-25 23:25:43 +0000682 case BuiltinType::WChar_S:
683 case BuiltinType::WChar_U:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000684 Width = Target.getWCharWidth();
685 Align = Target.getWCharAlign();
686 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000687 case BuiltinType::Char16:
688 Width = Target.getChar16Width();
689 Align = Target.getChar16Align();
690 break;
691 case BuiltinType::Char32:
692 Width = Target.getChar32Width();
693 Align = Target.getChar32Align();
694 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000695 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000696 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000697 Width = Target.getShortWidth();
698 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000699 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000700 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000701 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000702 Width = Target.getIntWidth();
703 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000704 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000705 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000706 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000707 Width = Target.getLongWidth();
708 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000709 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000710 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000711 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000712 Width = Target.getLongLongWidth();
713 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000714 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000715 case BuiltinType::Int128:
716 case BuiltinType::UInt128:
717 Width = 128;
718 Align = 128; // int128_t is 128-bit aligned on all targets.
719 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000720 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000721 Width = Target.getFloatWidth();
722 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000723 break;
724 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000725 Width = Target.getDoubleWidth();
726 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000727 break;
728 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000729 Width = Target.getLongDoubleWidth();
730 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000731 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000732 case BuiltinType::NullPtr:
733 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
734 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000735 break;
Fariborz Jahanian9c6a39e2010-08-02 18:03:20 +0000736 case BuiltinType::ObjCId:
737 case BuiltinType::ObjCClass:
738 case BuiltinType::ObjCSel:
739 Width = Target.getPointerWidth(0);
740 Align = Target.getPointerAlign(0);
741 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000742 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000743 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000744 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000745 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000746 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000747 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000748 case Type::BlockPointer: {
749 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
750 Width = Target.getPointerWidth(AS);
751 Align = Target.getPointerAlign(AS);
752 break;
753 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000754 case Type::LValueReference:
755 case Type::RValueReference: {
756 // alignof and sizeof should never enter this code path here, so we go
757 // the pointer route.
758 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
759 Width = Target.getPointerWidth(AS);
760 Align = Target.getPointerAlign(AS);
761 break;
762 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000763 case Type::Pointer: {
764 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000765 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000766 Align = Target.getPointerAlign(AS);
767 break;
768 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000769 case Type::MemberPointer: {
Charles Davis53c59df2010-08-16 03:33:14 +0000770 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000771 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000772 getTypeInfo(getPointerDiffType());
Charles Davis53c59df2010-08-16 03:33:14 +0000773 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson32440a02009-05-17 02:06:04 +0000774 Align = PtrDiffInfo.second;
775 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000776 }
Chris Lattner647fb222007-07-18 18:26:58 +0000777 case Type::Complex: {
778 // Complex types have the same alignment as their elements, but twice the
779 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000780 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000781 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000782 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000783 Align = EltInfo.second;
784 break;
785 }
John McCall8b07ec22010-05-15 11:32:37 +0000786 case Type::ObjCObject:
787 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Pateldbb72632008-06-04 21:54:36 +0000788 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000789 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000790 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
791 Width = Layout.getSize();
792 Align = Layout.getAlignment();
793 break;
794 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000795 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000796 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000797 const TagType *TT = cast<TagType>(T);
798
799 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000800 Width = 1;
801 Align = 1;
802 break;
803 }
Mike Stump11289f42009-09-09 15:08:12 +0000804
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000805 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000806 return getTypeInfo(ET->getDecl()->getIntegerType());
807
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000808 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000809 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
810 Width = Layout.getSize();
811 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000812 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000813 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000814
Chris Lattner63d2b362009-10-22 05:17:15 +0000815 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000816 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
817 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000818
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000819 case Type::Paren:
820 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
821
Douglas Gregoref462e62009-04-30 17:32:17 +0000822 case Type::Typedef: {
823 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregorf5bae222010-08-27 00:11:28 +0000824 std::pair<uint64_t, unsigned> Info
825 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
826 Align = std::max(Typedef->getMaxAlignment(), Info.second);
827 Width = Info.first;
Douglas Gregordc572a32009-03-30 22:58:21 +0000828 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000829 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000830
831 case Type::TypeOfExpr:
832 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
833 .getTypePtr());
834
835 case Type::TypeOf:
836 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
837
Anders Carlsson81df7b82009-06-24 19:06:50 +0000838 case Type::Decltype:
839 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
840 .getTypePtr());
841
Abramo Bagnara6150c882010-05-11 21:36:43 +0000842 case Type::Elaborated:
843 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000844
Douglas Gregoref462e62009-04-30 17:32:17 +0000845 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000846 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000847 "Cannot request the size of a dependent type");
848 // FIXME: this is likely to be wrong once we support template
849 // aliases, since a template alias could refer to a typedef that
850 // has an __aligned__ attribute on it.
851 return getTypeInfo(getCanonicalType(T));
852 }
Mike Stump11289f42009-09-09 15:08:12 +0000853
Chris Lattner53cfe802007-07-18 17:52:12 +0000854 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000855 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000856}
857
Ken Dyck8c89d592009-12-22 14:23:30 +0000858/// getTypeSizeInChars - Return the size of the specified type, in characters.
859/// This method does not work on incomplete types.
860CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000861 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000862}
863CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000864 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000865}
866
Ken Dycka6046ab2010-01-26 17:25:18 +0000867/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000868/// characters. This method does not work on incomplete types.
869CharUnits ASTContext::getTypeAlignInChars(QualType T) {
870 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
871}
872CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
873 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
874}
875
Chris Lattnera3402cd2009-01-27 18:08:34 +0000876/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
877/// type for the current target in bits. This can be different than the ABI
878/// alignment in cases where it is beneficial for performance to overalign
879/// a data type.
880unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
881 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000882
883 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000884 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000885 T = CT->getElementType().getTypePtr();
886 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
887 T->isSpecificBuiltinType(BuiltinType::LongLong))
888 return std::max(ABIAlign, (unsigned)getTypeSize(T));
889
Chris Lattnera3402cd2009-01-27 18:08:34 +0000890 return ABIAlign;
891}
892
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000893/// ShallowCollectObjCIvars -
894/// Collect all ivars, including those synthesized, in the current class.
895///
896void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000897 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000898 // FIXME. This need be removed but there are two many places which
899 // assume const-ness of ObjCInterfaceDecl
900 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
901 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
902 Iv= Iv->getNextIvar())
903 Ivars.push_back(Iv);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000904}
905
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000906/// DeepCollectObjCIvars -
907/// This routine first collects all declared, but not synthesized, ivars in
908/// super class and then collects all ivars, including those synthesized for
909/// current class. This routine is used for implementation of current class
910/// when all ivars, declared and synthesized are known.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000911///
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000912void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
913 bool leafClass,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000914 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000915 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
916 DeepCollectObjCIvars(SuperClass, false, Ivars);
917 if (!leafClass) {
918 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
919 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000920 Ivars.push_back(*I);
921 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000922 else
923 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000924}
925
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000926/// CollectInheritedProtocols - Collect all protocols in current class and
927/// those inherited by it.
928void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000929 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000930 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000931 // We can use protocol_iterator here instead of
932 // all_referenced_protocol_iterator since we are walking all categories.
933 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
934 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000935 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000936 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000937 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000938 PE = Proto->protocol_end(); P != PE; ++P) {
939 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000940 CollectInheritedProtocols(*P, Protocols);
941 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000942 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000943
944 // Categories of this Interface.
945 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
946 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
947 CollectInheritedProtocols(CDeclChain, Protocols);
948 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
949 while (SD) {
950 CollectInheritedProtocols(SD, Protocols);
951 SD = SD->getSuperClass();
952 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000953 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000954 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000955 PE = OC->protocol_end(); P != PE; ++P) {
956 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000957 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000958 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
959 PE = Proto->protocol_end(); P != PE; ++P)
960 CollectInheritedProtocols(*P, Protocols);
961 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000962 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000963 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
964 PE = OP->protocol_end(); P != PE; ++P) {
965 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000966 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000967 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
968 PE = Proto->protocol_end(); P != PE; ++P)
969 CollectInheritedProtocols(*P, Protocols);
970 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000971 }
972}
973
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000974unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
975 unsigned count = 0;
976 // Count ivars declared in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000977 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
978 CDecl = CDecl->getNextClassExtension())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000979 count += CDecl->ivar_size();
980
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000981 // Count ivar defined in this class's implementation. This
982 // includes synthesized ivars.
983 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000984 count += ImplDecl->ivar_size();
985
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000986 return count;
987}
988
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000989/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
990ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
991 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
992 I = ObjCImpls.find(D);
993 if (I != ObjCImpls.end())
994 return cast<ObjCImplementationDecl>(I->second);
995 return 0;
996}
997/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
998ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
999 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1000 I = ObjCImpls.find(D);
1001 if (I != ObjCImpls.end())
1002 return cast<ObjCCategoryImplDecl>(I->second);
1003 return 0;
1004}
1005
1006/// \brief Set the implementation of ObjCInterfaceDecl.
1007void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1008 ObjCImplementationDecl *ImplD) {
1009 assert(IFaceD && ImplD && "Passed null params");
1010 ObjCImpls[IFaceD] = ImplD;
1011}
1012/// \brief Set the implementation of ObjCCategoryDecl.
1013void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1014 ObjCCategoryImplDecl *ImplD) {
1015 assert(CatD && ImplD && "Passed null params");
1016 ObjCImpls[CatD] = ImplD;
1017}
1018
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001019/// \brief Get the copy initialization expression of VarDecl,or NULL if
1020/// none exists.
Fariborz Jahanian50198092010-12-02 17:02:11 +00001021Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001022 assert(VD && "Passed null params");
1023 assert(VD->hasAttr<BlocksAttr>() &&
1024 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian50198092010-12-02 17:02:11 +00001025 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001026 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001027 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1028}
1029
1030/// \brief Set the copy inialization expression of a block var decl.
1031void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1032 assert(VD && Init && "Passed null params");
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001033 assert(VD->hasAttr<BlocksAttr>() &&
1034 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001035 BlockVarCopyInits[VD] = Init;
1036}
1037
John McCallbcd03502009-12-07 02:54:59 +00001038/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001039///
John McCallbcd03502009-12-07 02:54:59 +00001040/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001041/// the TypeLoc wrappers.
1042///
1043/// \param T the type that will be the basis for type source info. This type
1044/// should refer to how the declarator was written in source code, not to
1045/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +00001046TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +00001047 unsigned DataSize) {
1048 if (!DataSize)
1049 DataSize = TypeLoc::getFullDataSizeForType(T);
1050 else
1051 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +00001052 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +00001053
John McCallbcd03502009-12-07 02:54:59 +00001054 TypeSourceInfo *TInfo =
1055 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1056 new (TInfo) TypeSourceInfo(T);
1057 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001058}
1059
John McCallbcd03502009-12-07 02:54:59 +00001060TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +00001061 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +00001062 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +00001063 DI->getTypeLoc().initialize(L);
1064 return DI;
1065}
1066
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001067const ASTRecordLayout &
1068ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1069 return getObjCLayout(D, 0);
1070}
1071
1072const ASTRecordLayout &
1073ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1074 return getObjCLayout(D->getClassInterface(), D);
1075}
1076
Chris Lattner983a8bb2007-07-13 22:13:22 +00001077//===----------------------------------------------------------------------===//
1078// Type creation/memoization methods
1079//===----------------------------------------------------------------------===//
1080
John McCall8ccfcb52009-09-24 19:53:00 +00001081QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1082 unsigned Fast = Quals.getFastQualifiers();
1083 Quals.removeFastQualifiers();
1084
1085 // Check if we've already instantiated this type.
1086 llvm::FoldingSetNodeID ID;
1087 ExtQuals::Profile(ID, TypeNode, Quals);
1088 void *InsertPos = 0;
1089 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1090 assert(EQ->getQualifiers() == Quals);
1091 QualType T = QualType(EQ, Fast);
1092 return T;
1093 }
1094
John McCall717d9b02010-12-10 11:01:00 +00001095 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001096 ExtQualNodes.InsertNode(New, InsertPos);
1097 QualType T = QualType(New, Fast);
1098 return T;
1099}
1100
Fariborz Jahanianece85822009-02-17 18:27:45 +00001101QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001102 QualType CanT = getCanonicalType(T);
1103 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001104 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001105
John McCall8ccfcb52009-09-24 19:53:00 +00001106 // If we are composing extended qualifiers together, merge together
1107 // into one ExtQuals node.
1108 QualifierCollector Quals;
1109 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001110
John McCall8ccfcb52009-09-24 19:53:00 +00001111 // If this type already has an address space specified, it cannot get
1112 // another one.
1113 assert(!Quals.hasAddressSpace() &&
1114 "Type cannot be in multiple addr spaces!");
1115 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001116
John McCall8ccfcb52009-09-24 19:53:00 +00001117 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001118}
1119
Chris Lattnerd60183d2009-02-18 22:53:11 +00001120QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001121 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001122 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001123 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001124 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001125
John McCall53fa7142010-12-24 02:08:15 +00001126 if (const PointerType *ptr = T->getAs<PointerType>()) {
1127 QualType Pointee = ptr->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001128 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001129 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1130 return getPointerType(ResultType);
1131 }
1132 }
Mike Stump11289f42009-09-09 15:08:12 +00001133
John McCall8ccfcb52009-09-24 19:53:00 +00001134 // If we are composing extended qualifiers together, merge together
1135 // into one ExtQuals node.
1136 QualifierCollector Quals;
1137 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001138
John McCall8ccfcb52009-09-24 19:53:00 +00001139 // If this type already has an ObjCGC specified, it cannot get
1140 // another one.
1141 assert(!Quals.hasObjCGCAttr() &&
1142 "Type cannot have multiple ObjCGCs!");
1143 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001144
John McCall8ccfcb52009-09-24 19:53:00 +00001145 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001146}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001147
John McCall4f5019e2010-12-19 02:44:49 +00001148const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1149 FunctionType::ExtInfo Info) {
1150 if (T->getExtInfo() == Info)
1151 return T;
1152
1153 QualType Result;
1154 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1155 Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1156 } else {
1157 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1158 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1159 EPI.ExtInfo = Info;
1160 Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1161 FPT->getNumArgs(), EPI);
1162 }
1163
1164 return cast<FunctionType>(Result.getTypePtr());
1165}
1166
Chris Lattnerc6395932007-06-22 20:56:16 +00001167/// getComplexType - Return the uniqued reference to the type for a complex
1168/// number with the specified element type.
1169QualType ASTContext::getComplexType(QualType T) {
1170 // Unique pointers, to guarantee there is only one pointer of a particular
1171 // structure.
1172 llvm::FoldingSetNodeID ID;
1173 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattnerc6395932007-06-22 20:56:16 +00001175 void *InsertPos = 0;
1176 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1177 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Chris Lattnerc6395932007-06-22 20:56:16 +00001179 // If the pointee type isn't canonical, this won't be a canonical type either,
1180 // so fill in the canonical type field.
1181 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001182 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001183 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001184
Chris Lattnerc6395932007-06-22 20:56:16 +00001185 // Get the new insert position for the node we care about.
1186 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001187 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001188 }
John McCall90d1c2d2009-09-24 23:30:46 +00001189 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001190 Types.push_back(New);
1191 ComplexTypes.InsertNode(New, InsertPos);
1192 return QualType(New, 0);
1193}
1194
Chris Lattner970e54e2006-11-12 00:37:36 +00001195/// getPointerType - Return the uniqued reference to the type for a pointer to
1196/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001197QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001198 // Unique pointers, to guarantee there is only one pointer of a particular
1199 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001200 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001201 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001202
Chris Lattner67521df2007-01-27 01:29:36 +00001203 void *InsertPos = 0;
1204 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001205 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001206
Chris Lattner7ccecb92006-11-12 08:50:50 +00001207 // If the pointee type isn't canonical, this won't be a canonical type either,
1208 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001209 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001210 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001211 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001212
Chris Lattner67521df2007-01-27 01:29:36 +00001213 // Get the new insert position for the node we care about.
1214 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001215 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001216 }
John McCall90d1c2d2009-09-24 23:30:46 +00001217 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001218 Types.push_back(New);
1219 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001220 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001221}
1222
Mike Stump11289f42009-09-09 15:08:12 +00001223/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001224/// a pointer to the specified block.
1225QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001226 assert(T->isFunctionType() && "block of function types only");
1227 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001228 // structure.
1229 llvm::FoldingSetNodeID ID;
1230 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001231
Steve Naroffec33ed92008-08-27 16:04:49 +00001232 void *InsertPos = 0;
1233 if (BlockPointerType *PT =
1234 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1235 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001236
1237 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001238 // type either so fill in the canonical type field.
1239 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001240 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001241 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001242
Steve Naroffec33ed92008-08-27 16:04:49 +00001243 // Get the new insert position for the node we care about.
1244 BlockPointerType *NewIP =
1245 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001246 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001247 }
John McCall90d1c2d2009-09-24 23:30:46 +00001248 BlockPointerType *New
1249 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001250 Types.push_back(New);
1251 BlockPointerTypes.InsertNode(New, InsertPos);
1252 return QualType(New, 0);
1253}
1254
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001255/// getLValueReferenceType - Return the uniqued reference to the type for an
1256/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001257QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001258 // Unique pointers, to guarantee there is only one pointer of a particular
1259 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001260 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001261 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001262
1263 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001264 if (LValueReferenceType *RT =
1265 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001266 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001267
John McCallfc93cf92009-10-22 22:37:11 +00001268 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1269
Bill Wendling3708c182007-05-27 10:15:43 +00001270 // If the referencee type isn't canonical, this won't be a canonical type
1271 // either, so fill in the canonical type field.
1272 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001273 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1274 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1275 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001276
Bill Wendling3708c182007-05-27 10:15:43 +00001277 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001278 LValueReferenceType *NewIP =
1279 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001280 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001281 }
1282
John McCall90d1c2d2009-09-24 23:30:46 +00001283 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001284 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1285 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001286 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001287 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001288
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001289 return QualType(New, 0);
1290}
1291
1292/// getRValueReferenceType - Return the uniqued reference to the type for an
1293/// rvalue reference to the specified type.
1294QualType ASTContext::getRValueReferenceType(QualType T) {
1295 // Unique pointers, to guarantee there is only one pointer of a particular
1296 // structure.
1297 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001298 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001299
1300 void *InsertPos = 0;
1301 if (RValueReferenceType *RT =
1302 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1303 return QualType(RT, 0);
1304
John McCallfc93cf92009-10-22 22:37:11 +00001305 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1306
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001307 // If the referencee type isn't canonical, this won't be a canonical type
1308 // either, so fill in the canonical type field.
1309 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001310 if (InnerRef || !T.isCanonical()) {
1311 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1312 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001313
1314 // Get the new insert position for the node we care about.
1315 RValueReferenceType *NewIP =
1316 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001317 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001318 }
1319
John McCall90d1c2d2009-09-24 23:30:46 +00001320 RValueReferenceType *New
1321 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001322 Types.push_back(New);
1323 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001324 return QualType(New, 0);
1325}
1326
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001327/// getMemberPointerType - Return the uniqued reference to the type for a
1328/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001329QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001330 // Unique pointers, to guarantee there is only one pointer of a particular
1331 // structure.
1332 llvm::FoldingSetNodeID ID;
1333 MemberPointerType::Profile(ID, T, Cls);
1334
1335 void *InsertPos = 0;
1336 if (MemberPointerType *PT =
1337 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1338 return QualType(PT, 0);
1339
1340 // If the pointee or class type isn't canonical, this won't be a canonical
1341 // type either, so fill in the canonical type field.
1342 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001343 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001344 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1345
1346 // Get the new insert position for the node we care about.
1347 MemberPointerType *NewIP =
1348 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001349 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001350 }
John McCall90d1c2d2009-09-24 23:30:46 +00001351 MemberPointerType *New
1352 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001353 Types.push_back(New);
1354 MemberPointerTypes.InsertNode(New, InsertPos);
1355 return QualType(New, 0);
1356}
1357
Mike Stump11289f42009-09-09 15:08:12 +00001358/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001359/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001360QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001361 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001362 ArrayType::ArraySizeModifier ASM,
1363 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001364 assert((EltTy->isDependentType() ||
1365 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001366 "Constant array of VLAs is illegal!");
1367
Chris Lattnere2df3f92009-05-13 04:12:56 +00001368 // Convert the array size into a canonical width matching the pointer size for
1369 // the target.
1370 llvm::APInt ArySize(ArySizeIn);
Jay Foad6d4db0c2010-12-07 08:25:34 +00001371 ArySize =
1372 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001373
Chris Lattner23b7eb62007-06-15 23:05:46 +00001374 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001375 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattner36f8e652007-01-27 08:31:04 +00001377 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001378 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001379 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001380 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001381
Chris Lattner7ccecb92006-11-12 08:50:50 +00001382 // If the element type isn't canonical, this won't be a canonical type either,
1383 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001384 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001385 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001386 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001387 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001388 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001389 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001390 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001391 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
John McCall90d1c2d2009-09-24 23:30:46 +00001394 ConstantArrayType *New = new(*this,TypeAlignment)
1395 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001396 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001397 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001398 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001399}
1400
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001401/// getIncompleteArrayType - Returns a unique reference to the type for a
1402/// incomplete array of the specified element type.
1403QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) {
1404 QualType ElemTy = getBaseElementType(Ty);
1405 DeclarationName Name;
1406 llvm::SmallVector<QualType, 8> ATypes;
1407 QualType ATy = Ty;
1408 while (const ArrayType *AT = getAsArrayType(ATy)) {
1409 ATypes.push_back(ATy);
1410 ATy = AT->getElementType();
1411 }
1412 for (int i = ATypes.size() - 1; i >= 0; i--) {
1413 if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) {
1414 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star,
1415 0, VAT->getBracketsRange());
1416 }
1417 else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) {
1418 llvm::APSInt ConstVal(CAT->getSize());
1419 ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0);
1420 }
1421 else if (getAsIncompleteArrayType(ATypes[i])) {
1422 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal,
1423 0, SourceRange());
1424 }
1425 else
1426 assert(false && "DependentArrayType is seen");
1427 }
1428 return ElemTy;
1429}
1430
1431/// getVariableArrayDecayedType - Returns a vla type where known sizes
1432/// are replaced with [*]
1433QualType ASTContext::getVariableArrayDecayedType(QualType Ty) {
1434 if (Ty->isPointerType()) {
1435 QualType BaseType = Ty->getAs<PointerType>()->getPointeeType();
1436 if (isa<VariableArrayType>(BaseType)) {
1437 ArrayType *AT = dyn_cast<ArrayType>(BaseType);
1438 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1439 if (VAT->getSizeExpr()) {
1440 Ty = getUnknownSizeVariableArrayType(BaseType);
1441 Ty = getPointerType(Ty);
1442 }
1443 }
1444 }
1445 return Ty;
1446}
1447
1448
Steve Naroffcadebd02007-08-30 18:14:25 +00001449/// getVariableArrayType - Returns a non-unique reference to the type for a
1450/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001451QualType ASTContext::getVariableArrayType(QualType EltTy,
1452 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001453 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001454 unsigned EltTypeQuals,
1455 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001456 // Since we don't unique expressions, it isn't possible to unique VLA's
1457 // that have an expression provided for their size.
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001458 QualType CanonType;
1459
1460 if (!EltTy.isCanonical()) {
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001461 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1462 EltTypeQuals, Brackets);
1463 }
1464
John McCall90d1c2d2009-09-24 23:30:46 +00001465 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001466 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001467
1468 VariableArrayTypes.push_back(New);
1469 Types.push_back(New);
1470 return QualType(New, 0);
1471}
1472
Douglas Gregor4619e432008-12-05 23:32:09 +00001473/// getDependentSizedArrayType - Returns a non-unique reference to
1474/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001475/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001476QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1477 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001478 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001479 unsigned EltTypeQuals,
1480 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001481 assert((!NumElts || NumElts->isTypeDependent() ||
1482 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001483 "Size must be type- or value-dependent!");
1484
Douglas Gregorf3f95522009-07-31 00:23:35 +00001485 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001486 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001487 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001488
Douglas Gregorf86c9392010-10-26 00:51:02 +00001489 QualType CanonicalEltTy = getCanonicalType(EltTy);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001490 if (NumElts) {
1491 // Dependently-sized array types that do not have a specified
1492 // number of elements will have their sizes deduced from an
1493 // initializer.
Douglas Gregorf86c9392010-10-26 00:51:02 +00001494 DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM,
Douglas Gregorad2956c2009-11-19 18:03:26 +00001495 EltTypeQuals, NumElts);
1496
1497 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1498 }
1499
Douglas Gregorf3f95522009-07-31 00:23:35 +00001500 DependentSizedArrayType *New;
1501 if (Canon) {
1502 // We already have a canonical version of this array type; use it as
1503 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001504 New = new (*this, TypeAlignment)
1505 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1506 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf86c9392010-10-26 00:51:02 +00001507 } else if (CanonicalEltTy == EltTy) {
1508 // This is a canonical type. Record it.
1509 New = new (*this, TypeAlignment)
1510 DependentSizedArrayType(*this, EltTy, QualType(),
1511 NumElts, ASM, EltTypeQuals, Brackets);
1512
1513 if (NumElts) {
1514#ifndef NDEBUG
1515 DependentSizedArrayType *CanonCheck
1516 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1517 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1518 (void)CanonCheck;
1519#endif
1520 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001521 }
Douglas Gregorf86c9392010-10-26 00:51:02 +00001522 } else {
1523 QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts,
1524 ASM, EltTypeQuals,
1525 SourceRange());
1526 New = new (*this, TypeAlignment)
1527 DependentSizedArrayType(*this, EltTy, Canon,
1528 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001529 }
Mike Stump11289f42009-09-09 15:08:12 +00001530
Douglas Gregor4619e432008-12-05 23:32:09 +00001531 Types.push_back(New);
1532 return QualType(New, 0);
1533}
1534
Eli Friedmanbd258282008-02-15 18:16:39 +00001535QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1536 ArrayType::ArraySizeModifier ASM,
1537 unsigned EltTypeQuals) {
1538 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001539 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001540
1541 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001542 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001543 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1544 return QualType(ATP, 0);
1545
1546 // If the element type isn't canonical, this won't be a canonical type
1547 // either, so fill in the canonical type field.
1548 QualType Canonical;
1549
John McCallb692a092009-10-22 20:10:53 +00001550 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001551 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001552 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001553
1554 // Get the new insert position for the node we care about.
1555 IncompleteArrayType *NewIP =
1556 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001557 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001558 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001559
John McCall90d1c2d2009-09-24 23:30:46 +00001560 IncompleteArrayType *New = new (*this, TypeAlignment)
1561 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001562
1563 IncompleteArrayTypes.InsertNode(New, InsertPos);
1564 Types.push_back(New);
1565 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001566}
1567
Steve Naroff91fcddb2007-07-18 18:00:27 +00001568/// getVectorType - Return the unique reference to a vector type of
1569/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001570QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001571 VectorType::VectorKind VecKind) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00001572 BuiltinType *BaseType;
Mike Stump11289f42009-09-09 15:08:12 +00001573
Chris Lattnerdc226c22010-10-01 22:42:38 +00001574 BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1575 assert(BaseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001576
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001577 // Check if we've already instantiated a vector of this type.
1578 llvm::FoldingSetNodeID ID;
Bob Wilsonaeb56442010-11-10 21:56:12 +00001579 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner37141f42010-06-23 06:00:24 +00001580
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001581 void *InsertPos = 0;
1582 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1583 return QualType(VTP, 0);
1584
1585 // If the element type isn't canonical, this won't be a canonical type either,
1586 // so fill in the canonical type field.
1587 QualType Canonical;
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00001588 if (!vecType.isCanonical()) {
Bob Wilson77954802010-11-16 00:32:20 +00001589 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump11289f42009-09-09 15:08:12 +00001590
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001591 // Get the new insert position for the node we care about.
1592 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001593 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001594 }
John McCall90d1c2d2009-09-24 23:30:46 +00001595 VectorType *New = new (*this, TypeAlignment)
Bob Wilsonaeb56442010-11-10 21:56:12 +00001596 VectorType(vecType, NumElts, Canonical, VecKind);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001597 VectorTypes.InsertNode(New, InsertPos);
1598 Types.push_back(New);
1599 return QualType(New, 0);
1600}
1601
Nate Begemance4d7fc2008-04-18 23:10:10 +00001602/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001603/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001604QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001605 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001606
Chris Lattner76a00cf2008-04-06 22:59:24 +00001607 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001608 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001609
Steve Naroff91fcddb2007-07-18 18:00:27 +00001610 // Check if we've already instantiated a vector of this type.
1611 llvm::FoldingSetNodeID ID;
Chris Lattner37141f42010-06-23 06:00:24 +00001612 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001613 VectorType::GenericVector);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001614 void *InsertPos = 0;
1615 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1616 return QualType(VTP, 0);
1617
1618 // If the element type isn't canonical, this won't be a canonical type either,
1619 // so fill in the canonical type field.
1620 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001621 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001622 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001623
Steve Naroff91fcddb2007-07-18 18:00:27 +00001624 // Get the new insert position for the node we care about.
1625 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001626 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001627 }
John McCall90d1c2d2009-09-24 23:30:46 +00001628 ExtVectorType *New = new (*this, TypeAlignment)
1629 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001630 VectorTypes.InsertNode(New, InsertPos);
1631 Types.push_back(New);
1632 return QualType(New, 0);
1633}
1634
Mike Stump11289f42009-09-09 15:08:12 +00001635QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001636 Expr *SizeExpr,
1637 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001638 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001639 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001640 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001641
Douglas Gregor352169a2009-07-31 03:54:25 +00001642 void *InsertPos = 0;
1643 DependentSizedExtVectorType *Canon
1644 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1645 DependentSizedExtVectorType *New;
1646 if (Canon) {
1647 // We already have a canonical version of this array type; use it as
1648 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001649 New = new (*this, TypeAlignment)
1650 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1651 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001652 } else {
1653 QualType CanonVecTy = getCanonicalType(vecType);
1654 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001655 New = new (*this, TypeAlignment)
1656 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1657 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001658
1659 DependentSizedExtVectorType *CanonCheck
1660 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1661 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1662 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001663 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1664 } else {
1665 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1666 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001667 New = new (*this, TypeAlignment)
1668 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001669 }
1670 }
Mike Stump11289f42009-09-09 15:08:12 +00001671
Douglas Gregor758a8692009-06-17 21:51:59 +00001672 Types.push_back(New);
1673 return QualType(New, 0);
1674}
1675
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001676/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001677///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001678QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1679 const FunctionType::ExtInfo &Info) {
1680 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001681 // Unique functions, to guarantee there is only one function of a particular
1682 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001683 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001684 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001685
Chris Lattner47955de2007-01-27 08:37:20 +00001686 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001687 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001688 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001689 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001690
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001691 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001692 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001693 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001694 Canonical =
1695 getFunctionNoProtoType(getCanonicalType(ResultTy),
1696 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001697
Chris Lattner47955de2007-01-27 08:37:20 +00001698 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001699 FunctionNoProtoType *NewIP =
1700 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001701 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001702 }
Mike Stump11289f42009-09-09 15:08:12 +00001703
John McCall90d1c2d2009-09-24 23:30:46 +00001704 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001705 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001706 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001707 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001708 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001709}
1710
1711/// getFunctionType - Return a normal function type with a typed argument
1712/// list. isVariadic indicates whether the argument list includes '...'.
John McCalldb40c7f2010-12-14 08:05:40 +00001713QualType ASTContext::getFunctionType(QualType ResultTy,
1714 const QualType *ArgArray, unsigned NumArgs,
1715 const FunctionProtoType::ExtProtoInfo &EPI) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001716 // Unique functions, to guarantee there is only one function of a particular
1717 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001718 llvm::FoldingSetNodeID ID;
John McCalldb40c7f2010-12-14 08:05:40 +00001719 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001720
1721 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001722 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001723 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001724 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001725
1726 // Determine whether the type being created is already canonical or not.
John McCalldb40c7f2010-12-14 08:05:40 +00001727 bool isCanonical = !EPI.HasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001728 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001729 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001730 isCanonical = false;
1731
John McCalldb40c7f2010-12-14 08:05:40 +00001732 const CallingConv CallConv = EPI.ExtInfo.getCC();
1733
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001734 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001735 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001736 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001737 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001738 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001739 CanonicalArgs.reserve(NumArgs);
1740 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001741 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001742
John McCalldb40c7f2010-12-14 08:05:40 +00001743 FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
1744 if (CanonicalEPI.HasExceptionSpec) {
1745 CanonicalEPI.HasExceptionSpec = false;
1746 CanonicalEPI.HasAnyExceptionSpec = false;
1747 CanonicalEPI.NumExceptions = 0;
1748 }
1749 CanonicalEPI.ExtInfo
1750 = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
1751
Chris Lattner76a00cf2008-04-06 22:59:24 +00001752 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001753 CanonicalArgs.data(), NumArgs,
John McCalldb40c7f2010-12-14 08:05:40 +00001754 CanonicalEPI);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001755
Chris Lattnerfd4de792007-01-27 01:15:32 +00001756 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001757 FunctionProtoType *NewIP =
1758 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001759 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001760 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001761
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001762 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001763 // for two variable size arrays (for parameter and exception types) at the
1764 // end of them.
John McCalldb40c7f2010-12-14 08:05:40 +00001765 size_t Size = sizeof(FunctionProtoType) +
1766 NumArgs * sizeof(QualType) +
1767 EPI.NumExceptions * sizeof(QualType);
1768 FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
1769 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, EPI);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001770 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001771 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001772 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001773}
Chris Lattneref51c202006-11-10 07:17:23 +00001774
John McCalle78aac42010-03-10 03:28:59 +00001775#ifndef NDEBUG
1776static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1777 if (!isa<CXXRecordDecl>(D)) return false;
1778 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1779 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1780 return true;
1781 if (RD->getDescribedClassTemplate() &&
1782 !isa<ClassTemplateSpecializationDecl>(RD))
1783 return true;
1784 return false;
1785}
1786#endif
1787
1788/// getInjectedClassNameType - Return the unique reference to the
1789/// injected class name type for the specified templated declaration.
1790QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1791 QualType TST) {
1792 assert(NeedsInjectedClassNameType(Decl));
1793 if (Decl->TypeForDecl) {
1794 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001795 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCalle78aac42010-03-10 03:28:59 +00001796 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1797 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1798 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1799 } else {
John McCall2408e322010-04-27 00:57:59 +00001800 Decl->TypeForDecl =
1801 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001802 Types.push_back(Decl->TypeForDecl);
1803 }
1804 return QualType(Decl->TypeForDecl, 0);
1805}
1806
Douglas Gregor83a586e2008-04-13 21:07:44 +00001807/// getTypeDeclType - Return the unique reference to the type for the
1808/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001809QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001810 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001811 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001812
John McCall81e38502010-02-16 03:57:14 +00001813 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001814 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001815
John McCall96f0b5f2010-03-10 06:48:02 +00001816 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1817 "Template type parameter types are always available.");
1818
John McCall81e38502010-02-16 03:57:14 +00001819 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001820 assert(!Record->getPreviousDeclaration() &&
1821 "struct/union has previous declaration");
1822 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001823 return getRecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001824 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001825 assert(!Enum->getPreviousDeclaration() &&
1826 "enum has previous declaration");
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001827 return getEnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001828 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001829 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1830 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001831 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001832 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001833
John McCall96f0b5f2010-03-10 06:48:02 +00001834 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001835 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001836}
1837
Chris Lattner32d920b2007-01-26 02:01:53 +00001838/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001839/// specified typename decl.
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001840QualType
1841ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001842 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001843
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001844 if (Canonical.isNull())
1845 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001846 Decl->TypeForDecl = new(*this, TypeAlignment)
1847 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001848 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001849 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001850}
1851
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001852QualType ASTContext::getRecordType(const RecordDecl *Decl) {
1853 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1854
1855 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
1856 if (PrevDecl->TypeForDecl)
1857 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1858
1859 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
1860 Types.push_back(Decl->TypeForDecl);
1861 return QualType(Decl->TypeForDecl, 0);
1862}
1863
1864QualType ASTContext::getEnumType(const EnumDecl *Decl) {
1865 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1866
1867 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
1868 if (PrevDecl->TypeForDecl)
1869 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1870
1871 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
1872 Types.push_back(Decl->TypeForDecl);
1873 return QualType(Decl->TypeForDecl, 0);
1874}
1875
John McCallcebee162009-10-18 09:09:24 +00001876/// \brief Retrieve a substitution-result type.
1877QualType
1878ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1879 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001880 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001881 && "replacement types must always be canonical");
1882
1883 llvm::FoldingSetNodeID ID;
1884 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1885 void *InsertPos = 0;
1886 SubstTemplateTypeParmType *SubstParm
1887 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1888
1889 if (!SubstParm) {
1890 SubstParm = new (*this, TypeAlignment)
1891 SubstTemplateTypeParmType(Parm, Replacement);
1892 Types.push_back(SubstParm);
1893 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1894 }
1895
1896 return QualType(SubstParm, 0);
1897}
1898
Douglas Gregoreff93e02009-02-05 23:33:38 +00001899/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001900/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001901/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001902QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001903 bool ParameterPack,
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001904 IdentifierInfo *Name) {
Douglas Gregoreff93e02009-02-05 23:33:38 +00001905 llvm::FoldingSetNodeID ID;
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001906 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001907 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001908 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001909 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1910
1911 if (TypeParm)
1912 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001913
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001914 if (Name) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00001915 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001916 TypeParm = new (*this, TypeAlignment)
1917 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001918
1919 TemplateTypeParmType *TypeCheck
1920 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1921 assert(!TypeCheck && "Template type parameter canonical type broken");
1922 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001923 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001924 TypeParm = new (*this, TypeAlignment)
1925 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001926
1927 Types.push_back(TypeParm);
1928 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1929
1930 return QualType(TypeParm, 0);
1931}
1932
John McCalle78aac42010-03-10 03:28:59 +00001933TypeSourceInfo *
1934ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1935 SourceLocation NameLoc,
1936 const TemplateArgumentListInfo &Args,
1937 QualType CanonType) {
1938 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1939
1940 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1941 TemplateSpecializationTypeLoc TL
1942 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1943 TL.setTemplateNameLoc(NameLoc);
1944 TL.setLAngleLoc(Args.getLAngleLoc());
1945 TL.setRAngleLoc(Args.getRAngleLoc());
1946 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1947 TL.setArgLocInfo(i, Args[i].getLocInfo());
1948 return DI;
1949}
1950
Mike Stump11289f42009-09-09 15:08:12 +00001951QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001952ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001953 const TemplateArgumentListInfo &Args,
John McCall30576cd2010-06-13 09:25:03 +00001954 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00001955 unsigned NumArgs = Args.size();
1956
John McCall0ad16662009-10-29 08:12:44 +00001957 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1958 ArgVec.reserve(NumArgs);
1959 for (unsigned i = 0; i != NumArgs; ++i)
1960 ArgVec.push_back(Args[i].getArgument());
1961
John McCall2408e322010-04-27 00:57:59 +00001962 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00001963 Canon);
John McCall0ad16662009-10-29 08:12:44 +00001964}
1965
1966QualType
1967ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001968 const TemplateArgument *Args,
1969 unsigned NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00001970 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001971 if (!Canon.isNull())
1972 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001973 else
1974 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregord56a91e2009-02-26 22:19:44 +00001975
Douglas Gregora8e02e72009-07-28 23:00:59 +00001976 // Allocate the (non-canonical) template specialization type, but don't
1977 // try to unique it: these types typically have location information that
1978 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001979 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001980 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001981 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001982 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00001983 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00001984 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001985 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001986
Douglas Gregor8bf42052009-02-09 18:46:07 +00001987 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001988 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001989}
1990
Mike Stump11289f42009-09-09 15:08:12 +00001991QualType
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001992ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
1993 const TemplateArgument *Args,
1994 unsigned NumArgs) {
1995 // Build the canonical template specialization type.
1996 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1997 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1998 CanonArgs.reserve(NumArgs);
1999 for (unsigned I = 0; I != NumArgs; ++I)
2000 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2001
2002 // Determine whether this canonical template specialization type already
2003 // exists.
2004 llvm::FoldingSetNodeID ID;
2005 TemplateSpecializationType::Profile(ID, CanonTemplate,
2006 CanonArgs.data(), NumArgs, *this);
2007
2008 void *InsertPos = 0;
2009 TemplateSpecializationType *Spec
2010 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2011
2012 if (!Spec) {
2013 // Allocate a new canonical template specialization type.
2014 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2015 sizeof(TemplateArgument) * NumArgs),
2016 TypeAlignment);
2017 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2018 CanonArgs.data(), NumArgs,
2019 QualType());
2020 Types.push_back(Spec);
2021 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2022 }
2023
2024 assert(Spec->isDependentType() &&
2025 "Non-dependent template-id type must have a canonical type");
2026 return QualType(Spec, 0);
2027}
2028
2029QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00002030ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2031 NestedNameSpecifier *NNS,
2032 QualType NamedType) {
Douglas Gregor52537682009-03-19 00:18:19 +00002033 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002034 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00002035
2036 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002037 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002038 if (T)
2039 return QualType(T, 0);
2040
Douglas Gregorc42075a2010-02-04 18:10:26 +00002041 QualType Canon = NamedType;
2042 if (!Canon.isCanonical()) {
2043 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002044 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2045 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00002046 (void)CheckT;
2047 }
2048
Abramo Bagnara6150c882010-05-11 21:36:43 +00002049 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00002050 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002051 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002052 return QualType(T, 0);
2053}
2054
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002055QualType
2056ASTContext::getParenType(QualType InnerType) {
2057 llvm::FoldingSetNodeID ID;
2058 ParenType::Profile(ID, InnerType);
2059
2060 void *InsertPos = 0;
2061 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2062 if (T)
2063 return QualType(T, 0);
2064
2065 QualType Canon = InnerType;
2066 if (!Canon.isCanonical()) {
2067 Canon = getCanonicalType(InnerType);
2068 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2069 assert(!CheckT && "Paren canonical type broken");
2070 (void)CheckT;
2071 }
2072
2073 T = new (*this) ParenType(InnerType, Canon);
2074 Types.push_back(T);
2075 ParenTypes.InsertNode(T, InsertPos);
2076 return QualType(T, 0);
2077}
2078
Douglas Gregor02085352010-03-31 20:19:30 +00002079QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2080 NestedNameSpecifier *NNS,
2081 const IdentifierInfo *Name,
2082 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00002083 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2084
2085 if (Canon.isNull()) {
2086 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00002087 ElaboratedTypeKeyword CanonKeyword = Keyword;
2088 if (Keyword == ETK_None)
2089 CanonKeyword = ETK_Typename;
2090
2091 if (CanonNNS != NNS || CanonKeyword != Keyword)
2092 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002093 }
2094
2095 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002096 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002097
2098 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002099 DependentNameType *T
2100 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00002101 if (T)
2102 return QualType(T, 0);
2103
Douglas Gregor02085352010-03-31 20:19:30 +00002104 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002105 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002106 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002107 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002108}
2109
Mike Stump11289f42009-09-09 15:08:12 +00002110QualType
John McCallc392f372010-06-11 00:33:02 +00002111ASTContext::getDependentTemplateSpecializationType(
2112 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002113 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002114 const IdentifierInfo *Name,
2115 const TemplateArgumentListInfo &Args) {
2116 // TODO: avoid this copy
2117 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2118 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2119 ArgCopy.push_back(Args[I].getArgument());
2120 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2121 ArgCopy.size(),
2122 ArgCopy.data());
2123}
2124
2125QualType
2126ASTContext::getDependentTemplateSpecializationType(
2127 ElaboratedTypeKeyword Keyword,
2128 NestedNameSpecifier *NNS,
2129 const IdentifierInfo *Name,
2130 unsigned NumArgs,
2131 const TemplateArgument *Args) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002132 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2133
Douglas Gregorc42075a2010-02-04 18:10:26 +00002134 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002135 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2136 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002137
2138 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002139 DependentTemplateSpecializationType *T
2140 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002141 if (T)
2142 return QualType(T, 0);
2143
John McCallc392f372010-06-11 00:33:02 +00002144 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002145
John McCallc392f372010-06-11 00:33:02 +00002146 ElaboratedTypeKeyword CanonKeyword = Keyword;
2147 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2148
2149 bool AnyNonCanonArgs = false;
2150 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2151 for (unsigned I = 0; I != NumArgs; ++I) {
2152 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2153 if (!CanonArgs[I].structurallyEquals(Args[I]))
2154 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002155 }
2156
John McCallc392f372010-06-11 00:33:02 +00002157 QualType Canon;
2158 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2159 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2160 Name, NumArgs,
2161 CanonArgs.data());
2162
2163 // Find the insert position again.
2164 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2165 }
2166
2167 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2168 sizeof(TemplateArgument) * NumArgs),
2169 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002170 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002171 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002172 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002173 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002174 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002175}
2176
Douglas Gregord2fa7662010-12-20 02:24:11 +00002177QualType ASTContext::getPackExpansionType(QualType Pattern) {
2178 llvm::FoldingSetNodeID ID;
2179 PackExpansionType::Profile(ID, Pattern);
2180
2181 assert(Pattern->containsUnexpandedParameterPack() &&
2182 "Pack expansions must expand one or more parameter packs");
2183 void *InsertPos = 0;
2184 PackExpansionType *T
2185 = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2186 if (T)
2187 return QualType(T, 0);
2188
2189 QualType Canon;
2190 if (!Pattern.isCanonical()) {
2191 Canon = getPackExpansionType(getCanonicalType(Pattern));
2192
2193 // Find the insert position again.
2194 PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2195 }
2196
2197 T = new (*this) PackExpansionType(Pattern, Canon);
2198 Types.push_back(T);
2199 PackExpansionTypes.InsertNode(T, InsertPos);
2200 return QualType(T, 0);
2201}
2202
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002203/// CmpProtocolNames - Comparison predicate for sorting protocols
2204/// alphabetically.
2205static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2206 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002207 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002208}
2209
John McCall8b07ec22010-05-15 11:32:37 +00002210static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002211 unsigned NumProtocols) {
2212 if (NumProtocols == 0) return true;
2213
2214 for (unsigned i = 1; i != NumProtocols; ++i)
2215 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2216 return false;
2217 return true;
2218}
2219
2220static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002221 unsigned &NumProtocols) {
2222 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002223
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002224 // Sort protocols, keyed by name.
2225 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2226
2227 // Remove duplicates.
2228 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2229 NumProtocols = ProtocolsEnd-Protocols;
2230}
2231
John McCall8b07ec22010-05-15 11:32:37 +00002232QualType ASTContext::getObjCObjectType(QualType BaseType,
2233 ObjCProtocolDecl * const *Protocols,
2234 unsigned NumProtocols) {
2235 // If the base type is an interface and there aren't any protocols
2236 // to add, then the interface type will do just fine.
2237 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2238 return BaseType;
2239
2240 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002241 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002242 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002243 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002244 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2245 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002246
John McCall8b07ec22010-05-15 11:32:37 +00002247 // Build the canonical type, which has the canonical base type and
2248 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002249 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002250 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2251 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2252 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002253 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2254 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002255 unsigned UniqueCount = NumProtocols;
2256
John McCallfc93cf92009-10-22 22:37:11 +00002257 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002258 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2259 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002260 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002261 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2262 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002263 }
2264
2265 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002266 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2267 }
2268
2269 unsigned Size = sizeof(ObjCObjectTypeImpl);
2270 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2271 void *Mem = Allocate(Size, TypeAlignment);
2272 ObjCObjectTypeImpl *T =
2273 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2274
2275 Types.push_back(T);
2276 ObjCObjectTypes.InsertNode(T, InsertPos);
2277 return QualType(T, 0);
2278}
2279
2280/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2281/// the given object type.
2282QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2283 llvm::FoldingSetNodeID ID;
2284 ObjCObjectPointerType::Profile(ID, ObjectT);
2285
2286 void *InsertPos = 0;
2287 if (ObjCObjectPointerType *QT =
2288 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2289 return QualType(QT, 0);
2290
2291 // Find the canonical object type.
2292 QualType Canonical;
2293 if (!ObjectT.isCanonical()) {
2294 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2295
2296 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002297 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2298 }
2299
Douglas Gregorf85bee62010-02-08 22:59:26 +00002300 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002301 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2302 ObjCObjectPointerType *QType =
2303 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002304
Steve Narofffb4330f2009-06-17 22:40:22 +00002305 Types.push_back(QType);
2306 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002307 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002308}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002309
Douglas Gregor1c283312010-08-11 12:19:30 +00002310/// getObjCInterfaceType - Return the unique reference to the type for the
2311/// specified ObjC interface decl. The list of protocols is optional.
2312QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2313 if (Decl->TypeForDecl)
2314 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002315
Douglas Gregor1c283312010-08-11 12:19:30 +00002316 // FIXME: redeclarations?
2317 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2318 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2319 Decl->TypeForDecl = T;
2320 Types.push_back(T);
2321 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002322}
2323
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002324/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2325/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002326/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002327/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002328/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002329QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002330 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002331 if (tofExpr->isTypeDependent()) {
2332 llvm::FoldingSetNodeID ID;
2333 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002334
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002335 void *InsertPos = 0;
2336 DependentTypeOfExprType *Canon
2337 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2338 if (Canon) {
2339 // We already have a "canonical" version of an identical, dependent
2340 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002341 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002342 QualType((TypeOfExprType*)Canon, 0));
2343 }
2344 else {
2345 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002346 Canon
2347 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002348 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2349 toe = Canon;
2350 }
2351 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002352 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002353 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002354 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002355 Types.push_back(toe);
2356 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002357}
2358
Steve Naroffa773cd52007-08-01 18:02:17 +00002359/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2360/// TypeOfType AST's. The only motivation to unique these nodes would be
2361/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002362/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002363/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002364QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002365 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002366 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002367 Types.push_back(tot);
2368 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002369}
2370
Anders Carlssonad6bd352009-06-24 21:24:56 +00002371/// getDecltypeForExpr - Given an expr, will return the decltype for that
2372/// expression, according to the rules in C++0x [dcl.type.simple]p4
2373static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002374 if (e->isTypeDependent())
2375 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002376
Anders Carlssonad6bd352009-06-24 21:24:56 +00002377 // If e is an id expression or a class member access, decltype(e) is defined
2378 // as the type of the entity named by e.
2379 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2380 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2381 return VD->getType();
2382 }
2383 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2384 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2385 return FD->getType();
2386 }
2387 // If e is a function call or an invocation of an overloaded operator,
2388 // (parentheses around e are ignored), decltype(e) is defined as the
2389 // return type of that function.
2390 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2391 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002392
Anders Carlssonad6bd352009-06-24 21:24:56 +00002393 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002394
2395 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002396 // defined as T&, otherwise decltype(e) is defined as T.
John McCall086a4642010-11-24 05:12:34 +00002397 if (e->isLValue())
Anders Carlssonad6bd352009-06-24 21:24:56 +00002398 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002399
Anders Carlssonad6bd352009-06-24 21:24:56 +00002400 return T;
2401}
2402
Anders Carlsson81df7b82009-06-24 19:06:50 +00002403/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2404/// DecltypeType AST's. The only motivation to unique these nodes would be
2405/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002406/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002407/// on canonical type's (which are always unique).
2408QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002409 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002410 if (e->isTypeDependent()) {
2411 llvm::FoldingSetNodeID ID;
2412 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002413
Douglas Gregora21f6c32009-07-30 23:36:40 +00002414 void *InsertPos = 0;
2415 DependentDecltypeType *Canon
2416 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2417 if (Canon) {
2418 // We already have a "canonical" version of an equivalent, dependent
2419 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002420 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002421 QualType((DecltypeType*)Canon, 0));
2422 }
2423 else {
2424 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002425 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002426 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2427 dt = Canon;
2428 }
2429 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002430 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002431 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002432 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002433 Types.push_back(dt);
2434 return QualType(dt, 0);
2435}
2436
Chris Lattnerfb072462007-01-23 05:45:31 +00002437/// getTagDeclType - Return the unique reference to the type for the
2438/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002439QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002440 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002441 // FIXME: What is the design on getTagDeclType when it requires casting
2442 // away const? mutable?
2443 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002444}
2445
Mike Stump11289f42009-09-09 15:08:12 +00002446/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2447/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2448/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002449CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002450 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002451}
Chris Lattnerfb072462007-01-23 05:45:31 +00002452
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002453/// getSignedWCharType - Return the type of "signed wchar_t".
2454/// Used when in C++, as a GCC extension.
2455QualType ASTContext::getSignedWCharType() const {
2456 // FIXME: derive from "Target" ?
2457 return WCharTy;
2458}
2459
2460/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2461/// Used when in C++, as a GCC extension.
2462QualType ASTContext::getUnsignedWCharType() const {
2463 // FIXME: derive from "Target" ?
2464 return UnsignedIntTy;
2465}
2466
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002467/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2468/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2469QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002470 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002471}
2472
Chris Lattnera21ad802008-04-02 05:18:44 +00002473//===----------------------------------------------------------------------===//
2474// Type Operators
2475//===----------------------------------------------------------------------===//
2476
John McCallfc93cf92009-10-22 22:37:11 +00002477CanQualType ASTContext::getCanonicalParamType(QualType T) {
2478 // Push qualifiers into arrays, and then discard any remaining
2479 // qualifiers.
2480 T = getCanonicalType(T);
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00002481 T = getVariableArrayDecayedType(T);
John McCallfc93cf92009-10-22 22:37:11 +00002482 const Type *Ty = T.getTypePtr();
John McCallfc93cf92009-10-22 22:37:11 +00002483 QualType Result;
2484 if (isa<ArrayType>(Ty)) {
2485 Result = getArrayDecayedType(QualType(Ty,0));
2486 } else if (isa<FunctionType>(Ty)) {
2487 Result = getPointerType(QualType(Ty, 0));
2488 } else {
2489 Result = QualType(Ty, 0);
2490 }
2491
2492 return CanQualType::CreateUnsafe(Result);
2493}
2494
Chris Lattnered0d0792008-04-06 22:41:35 +00002495/// getCanonicalType - Return the canonical (structural) type corresponding to
2496/// the specified potentially non-canonical type. The non-canonical version
2497/// of a type may have many "decorated" versions of types. Decorators can
2498/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2499/// to be free of any of these, allowing two canonical types to be compared
2500/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002501CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002502 QualifierCollector Quals;
2503 const Type *Ptr = Quals.strip(T);
2504 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002505
John McCall8ccfcb52009-09-24 19:53:00 +00002506 // The canonical internal type will be the canonical type *except*
2507 // that we push type qualifiers down through array types.
2508
2509 // If there are no new qualifiers to push down, stop here.
2510 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002511 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002512
John McCall8ccfcb52009-09-24 19:53:00 +00002513 // If the type qualifiers are on an array type, get the canonical
2514 // type of the array with the qualifiers applied to the element
2515 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002516 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2517 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002518 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002519
Chris Lattner7adf0762008-08-04 07:31:14 +00002520 // Get the canonical version of the element with the extra qualifiers on it.
2521 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002522 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002523 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002524
Chris Lattner7adf0762008-08-04 07:31:14 +00002525 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002526 return CanQualType::CreateUnsafe(
2527 getConstantArrayType(NewEltTy, CAT->getSize(),
2528 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002529 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002530 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002531 return CanQualType::CreateUnsafe(
2532 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002533 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002534
Douglas Gregor4619e432008-12-05 23:32:09 +00002535 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002536 return CanQualType::CreateUnsafe(
2537 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002538 DSAT->getSizeExpr(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002539 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002540 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002541 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002542
Chris Lattner7adf0762008-08-04 07:31:14 +00002543 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002544 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002545 VAT->getSizeExpr(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002546 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002547 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002548 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002549}
2550
Chandler Carruth607f38e2009-12-29 07:16:59 +00002551QualType ASTContext::getUnqualifiedArrayType(QualType T,
2552 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002553 Quals = T.getQualifiers();
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002554 const ArrayType *AT = getAsArrayType(T);
2555 if (!AT) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002556 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002557 }
2558
Chandler Carruth607f38e2009-12-29 07:16:59 +00002559 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002560 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002561 if (Elt == UnqualElt)
2562 return T;
2563
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002564 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002565 return getConstantArrayType(UnqualElt, CAT->getSize(),
2566 CAT->getSizeModifier(), 0);
2567 }
2568
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002569 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002570 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2571 }
2572
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002573 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2574 return getVariableArrayType(UnqualElt,
John McCallc3007a22010-10-26 07:05:15 +00002575 VAT->getSizeExpr(),
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002576 VAT->getSizeModifier(),
2577 VAT->getIndexTypeCVRQualifiers(),
2578 VAT->getBracketsRange());
2579 }
2580
2581 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCallc3007a22010-10-26 07:05:15 +00002582 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002583 DSAT->getSizeModifier(), 0,
2584 SourceRange());
2585}
2586
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002587/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2588/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2589/// they point to and return true. If T1 and T2 aren't pointer types
2590/// or pointer-to-member types, or if they are not similar at this
2591/// level, returns false and leaves T1 and T2 unchanged. Top-level
2592/// qualifiers on T1 and T2 are ignored. This function will typically
2593/// be called in a loop that successively "unwraps" pointer and
2594/// pointer-to-member types to compare them at each level.
2595bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2596 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2597 *T2PtrType = T2->getAs<PointerType>();
2598 if (T1PtrType && T2PtrType) {
2599 T1 = T1PtrType->getPointeeType();
2600 T2 = T2PtrType->getPointeeType();
2601 return true;
2602 }
2603
2604 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2605 *T2MPType = T2->getAs<MemberPointerType>();
2606 if (T1MPType && T2MPType &&
2607 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2608 QualType(T2MPType->getClass(), 0))) {
2609 T1 = T1MPType->getPointeeType();
2610 T2 = T2MPType->getPointeeType();
2611 return true;
2612 }
2613
2614 if (getLangOptions().ObjC1) {
2615 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2616 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2617 if (T1OPType && T2OPType) {
2618 T1 = T1OPType->getPointeeType();
2619 T2 = T2OPType->getPointeeType();
2620 return true;
2621 }
2622 }
2623
2624 // FIXME: Block pointers, too?
2625
2626 return false;
2627}
2628
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002629DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name,
2630 SourceLocation NameLoc) {
John McCall847e2a12009-11-24 18:42:40 +00002631 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002632 // DNInfo work in progress: CHECKME: what about DNLoc?
2633 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2634
John McCall847e2a12009-11-24 18:42:40 +00002635 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002636 DeclarationName DName;
John McCall847e2a12009-11-24 18:42:40 +00002637 if (DTN->isIdentifier()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002638 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2639 return DeclarationNameInfo(DName, NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002640 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002641 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2642 // DNInfo work in progress: FIXME: source locations?
2643 DeclarationNameLoc DNLoc;
2644 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2645 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2646 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall847e2a12009-11-24 18:42:40 +00002647 }
2648 }
2649
John McCalld28ae272009-12-02 08:04:21 +00002650 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2651 assert(Storage);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002652 // DNInfo work in progress: CHECKME: what about DNLoc?
2653 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002654}
2655
Douglas Gregor6bc50582009-05-07 06:41:52 +00002656TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002657 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2658 if (TemplateTemplateParmDecl *TTP
2659 = dyn_cast<TemplateTemplateParmDecl>(Template))
2660 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2661
2662 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002663 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002664 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002665
John McCalld28ae272009-12-02 08:04:21 +00002666 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002667
Douglas Gregor6bc50582009-05-07 06:41:52 +00002668 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2669 assert(DTN && "Non-dependent template names must refer to template decls.");
2670 return DTN->CanonicalTemplateName;
2671}
2672
Douglas Gregoradee3e32009-11-11 23:06:43 +00002673bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2674 X = getCanonicalTemplateName(X);
2675 Y = getCanonicalTemplateName(Y);
2676 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2677}
2678
Mike Stump11289f42009-09-09 15:08:12 +00002679TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002680ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2681 switch (Arg.getKind()) {
2682 case TemplateArgument::Null:
2683 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002684
Douglas Gregora8e02e72009-07-28 23:00:59 +00002685 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002686 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002687
Douglas Gregora8e02e72009-07-28 23:00:59 +00002688 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002689 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002690
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002691 case TemplateArgument::Template:
2692 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2693
Douglas Gregora8e02e72009-07-28 23:00:59 +00002694 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002695 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002696 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002697
Douglas Gregora8e02e72009-07-28 23:00:59 +00002698 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002699 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002700
Douglas Gregora8e02e72009-07-28 23:00:59 +00002701 case TemplateArgument::Pack: {
Douglas Gregor0192c232010-12-20 16:52:59 +00002702 if (Arg.pack_size() == 0)
2703 return Arg;
2704
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;
John McCall717d9b02010-12-10 11:01:00 +00002804 const Type *Ty = Qs.strip(T.getDesugaredType(*this));
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();
John McCall717d9b02010-12-10 11:01:00 +00002867 return Qs.apply(*this, 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
Chris Lattnerad3467e2010-12-25 23:25:43 +00002953 if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
2954 T->isSpecificBuiltinType(BuiltinType::WChar_U))
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002955 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2956
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002957 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2958 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2959
2960 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2961 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2962
Chris Lattner76a00cf2008-04-06 22:59:24 +00002963 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002964 default: assert(0 && "getIntegerRank(): not a built-in integer");
2965 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002966 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002967 case BuiltinType::Char_S:
2968 case BuiltinType::Char_U:
2969 case BuiltinType::SChar:
2970 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002971 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002972 case BuiltinType::Short:
2973 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002974 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002975 case BuiltinType::Int:
2976 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002977 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002978 case BuiltinType::Long:
2979 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002980 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002981 case BuiltinType::LongLong:
2982 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002983 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002984 case BuiltinType::Int128:
2985 case BuiltinType::UInt128:
2986 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002987 }
2988}
2989
Eli Friedman629ffb92009-08-20 04:21:42 +00002990/// \brief Whether this is a promotable bitfield reference according
2991/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2992///
2993/// \returns the type this bit-field will promote to, or NULL if no
2994/// promotion occurs.
2995QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00002996 if (E->isTypeDependent() || E->isValueDependent())
2997 return QualType();
2998
Eli Friedman629ffb92009-08-20 04:21:42 +00002999 FieldDecl *Field = E->getBitField();
3000 if (!Field)
3001 return QualType();
3002
3003 QualType FT = Field->getType();
3004
3005 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3006 uint64_t BitWidth = BitWidthAP.getZExtValue();
3007 uint64_t IntSize = getTypeSize(IntTy);
3008 // GCC extension compatibility: if the bit-field size is less than or equal
3009 // to the size of int, it gets promoted no matter what its type is.
3010 // For instance, unsigned long bf : 4 gets promoted to signed int.
3011 if (BitWidth < IntSize)
3012 return IntTy;
3013
3014 if (BitWidth == IntSize)
3015 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3016
3017 // Types bigger than int are not subject to promotions, and therefore act
3018 // like the base type.
3019 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3020 // is ridiculous.
3021 return QualType();
3022}
3023
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003024/// getPromotedIntegerType - Returns the type that Promotable will
3025/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3026/// integer type.
3027QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
3028 assert(!Promotable.isNull());
3029 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00003030 if (const EnumType *ET = Promotable->getAs<EnumType>())
3031 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003032 if (Promotable->isSignedIntegerType())
3033 return IntTy;
3034 uint64_t PromotableSize = getTypeSize(Promotable);
3035 uint64_t IntSize = getTypeSize(IntTy);
3036 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3037 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3038}
3039
Mike Stump11289f42009-09-09 15:08:12 +00003040/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003041/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003042/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003043int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00003044 Type *LHSC = getCanonicalType(LHS).getTypePtr();
3045 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003046 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003047
Chris Lattner76a00cf2008-04-06 22:59:24 +00003048 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3049 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00003050
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003051 unsigned LHSRank = getIntegerRank(LHSC);
3052 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00003053
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003054 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3055 if (LHSRank == RHSRank) return 0;
3056 return LHSRank > RHSRank ? 1 : -1;
3057 }
Mike Stump11289f42009-09-09 15:08:12 +00003058
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003059 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3060 if (LHSUnsigned) {
3061 // If the unsigned [LHS] type is larger, return it.
3062 if (LHSRank >= RHSRank)
3063 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00003064
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003065 // If the signed type can represent all values of the unsigned type, it
3066 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003067 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003068 return -1;
3069 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00003070
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003071 // If the unsigned [RHS] type is larger, return it.
3072 if (RHSRank >= LHSRank)
3073 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00003074
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003075 // If the signed type can represent all values of the unsigned type, it
3076 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003077 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003078 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00003079}
Anders Carlsson98f07902007-08-17 05:31:46 +00003080
Anders Carlsson6d417272009-11-14 21:45:58 +00003081static RecordDecl *
3082CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
3083 SourceLocation L, IdentifierInfo *Id) {
3084 if (Ctx.getLangOptions().CPlusPlus)
3085 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
3086 else
3087 return RecordDecl::Create(Ctx, TK, DC, L, Id);
3088}
3089
Mike Stump11289f42009-09-09 15:08:12 +00003090// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00003091QualType ASTContext::getCFConstantStringType() {
3092 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00003093 CFConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003094 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003095 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00003096 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00003097
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003098 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00003099
Anders Carlsson98f07902007-08-17 05:31:46 +00003100 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00003101 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003102 // int flags;
3103 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00003104 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00003105 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00003106 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00003107 FieldTypes[3] = LongTy;
3108
Anders Carlsson98f07902007-08-17 05:31:46 +00003109 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00003110 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003111 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00003112 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003113 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003114 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003115 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003116 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003117 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003118 }
3119
Douglas Gregord5058122010-02-11 01:19:42 +00003120 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00003121 }
Mike Stump11289f42009-09-09 15:08:12 +00003122
Anders Carlsson98f07902007-08-17 05:31:46 +00003123 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00003124}
Anders Carlsson87c149b2007-10-11 01:00:40 +00003125
Douglas Gregor512b0772009-04-23 22:29:11 +00003126void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003127 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003128 assert(Rec && "Invalid CFConstantStringType");
3129 CFConstantStringTypeDecl = Rec->getDecl();
3130}
3131
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003132// getNSConstantStringType - Return the type used for constant NSStrings.
3133QualType ASTContext::getNSConstantStringType() {
3134 if (!NSConstantStringTypeDecl) {
3135 NSConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003136 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003137 &Idents.get("__builtin_NSString"));
3138 NSConstantStringTypeDecl->startDefinition();
3139
3140 QualType FieldTypes[3];
3141
3142 // const int *isa;
3143 FieldTypes[0] = getPointerType(IntTy.withConst());
3144 // const char *str;
3145 FieldTypes[1] = getPointerType(CharTy.withConst());
3146 // unsigned int length;
3147 FieldTypes[2] = UnsignedIntTy;
3148
3149 // Create fields
3150 for (unsigned i = 0; i < 3; ++i) {
3151 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3152 SourceLocation(), 0,
3153 FieldTypes[i], /*TInfo=*/0,
3154 /*BitWidth=*/0,
3155 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003156 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003157 NSConstantStringTypeDecl->addDecl(Field);
3158 }
3159
3160 NSConstantStringTypeDecl->completeDefinition();
3161 }
3162
3163 return getTagDeclType(NSConstantStringTypeDecl);
3164}
3165
3166void ASTContext::setNSConstantStringType(QualType T) {
3167 const RecordType *Rec = T->getAs<RecordType>();
3168 assert(Rec && "Invalid NSConstantStringType");
3169 NSConstantStringTypeDecl = Rec->getDecl();
3170}
3171
Mike Stump11289f42009-09-09 15:08:12 +00003172QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003173 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003174 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003175 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003176 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003177 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003178
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003179 QualType FieldTypes[] = {
3180 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003181 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003182 getPointerType(UnsignedLongTy),
3183 getConstantArrayType(UnsignedLongTy,
3184 llvm::APInt(32, 5), ArrayType::Normal, 0)
3185 };
Mike Stump11289f42009-09-09 15:08:12 +00003186
Douglas Gregor91f84212008-12-11 16:49:14 +00003187 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003188 FieldDecl *Field = FieldDecl::Create(*this,
3189 ObjCFastEnumerationStateTypeDecl,
3190 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003191 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003192 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003193 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003194 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003195 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003196 }
Mike Stump11289f42009-09-09 15:08:12 +00003197
Douglas Gregord5058122010-02-11 01:19:42 +00003198 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003199 }
Mike Stump11289f42009-09-09 15:08:12 +00003200
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003201 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3202}
3203
Mike Stumpd0153282009-10-20 02:12:22 +00003204QualType ASTContext::getBlockDescriptorType() {
3205 if (BlockDescriptorType)
3206 return getTagDeclType(BlockDescriptorType);
3207
3208 RecordDecl *T;
3209 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003210 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003211 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003212 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003213
3214 QualType FieldTypes[] = {
3215 UnsignedLongTy,
3216 UnsignedLongTy,
3217 };
3218
3219 const char *FieldNames[] = {
3220 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003221 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003222 };
3223
3224 for (size_t i = 0; i < 2; ++i) {
3225 FieldDecl *Field = FieldDecl::Create(*this,
3226 T,
3227 SourceLocation(),
3228 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003229 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003230 /*BitWidth=*/0,
3231 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003232 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003233 T->addDecl(Field);
3234 }
3235
Douglas Gregord5058122010-02-11 01:19:42 +00003236 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003237
3238 BlockDescriptorType = T;
3239
3240 return getTagDeclType(BlockDescriptorType);
3241}
3242
3243void ASTContext::setBlockDescriptorType(QualType T) {
3244 const RecordType *Rec = T->getAs<RecordType>();
3245 assert(Rec && "Invalid BlockDescriptorType");
3246 BlockDescriptorType = Rec->getDecl();
3247}
3248
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003249QualType ASTContext::getBlockDescriptorExtendedType() {
3250 if (BlockDescriptorExtendedType)
3251 return getTagDeclType(BlockDescriptorExtendedType);
3252
3253 RecordDecl *T;
3254 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003255 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003256 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003257 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003258
3259 QualType FieldTypes[] = {
3260 UnsignedLongTy,
3261 UnsignedLongTy,
3262 getPointerType(VoidPtrTy),
3263 getPointerType(VoidPtrTy)
3264 };
3265
3266 const char *FieldNames[] = {
3267 "reserved",
3268 "Size",
3269 "CopyFuncPtr",
3270 "DestroyFuncPtr"
3271 };
3272
3273 for (size_t i = 0; i < 4; ++i) {
3274 FieldDecl *Field = FieldDecl::Create(*this,
3275 T,
3276 SourceLocation(),
3277 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003278 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003279 /*BitWidth=*/0,
3280 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003281 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003282 T->addDecl(Field);
3283 }
3284
Douglas Gregord5058122010-02-11 01:19:42 +00003285 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003286
3287 BlockDescriptorExtendedType = T;
3288
3289 return getTagDeclType(BlockDescriptorExtendedType);
3290}
3291
3292void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3293 const RecordType *Rec = T->getAs<RecordType>();
3294 assert(Rec && "Invalid BlockDescriptorType");
3295 BlockDescriptorExtendedType = Rec->getDecl();
3296}
3297
Mike Stump94967902009-10-21 18:16:27 +00003298bool ASTContext::BlockRequiresCopying(QualType Ty) {
3299 if (Ty->isBlockPointerType())
3300 return true;
3301 if (isObjCNSObjectType(Ty))
3302 return true;
3303 if (Ty->isObjCObjectPointerType())
3304 return true;
Fariborz Jahaniana00076c2010-11-17 00:21:28 +00003305 if (getLangOptions().CPlusPlus) {
3306 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3307 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3308 return RD->hasConstCopyConstructor(*this);
3309
3310 }
3311 }
Mike Stump94967902009-10-21 18:16:27 +00003312 return false;
3313}
3314
Daniel Dunbar56df9772010-08-17 22:39:59 +00003315QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) {
Mike Stump94967902009-10-21 18:16:27 +00003316 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003317 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003318 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003319 // unsigned int __flags;
3320 // unsigned int __size;
Eli Friedman04831922010-08-22 01:00:03 +00003321 // void *__copy_helper; // as needed
3322 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003323 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003324 // } *
3325
Mike Stump94967902009-10-21 18:16:27 +00003326 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3327
3328 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003329 llvm::SmallString<36> Name;
3330 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3331 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003332 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003333 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003334 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003335 T->startDefinition();
3336 QualType Int32Ty = IntTy;
3337 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3338 QualType FieldTypes[] = {
3339 getPointerType(VoidPtrTy),
3340 getPointerType(getTagDeclType(T)),
3341 Int32Ty,
3342 Int32Ty,
3343 getPointerType(VoidPtrTy),
3344 getPointerType(VoidPtrTy),
3345 Ty
3346 };
3347
Daniel Dunbar56df9772010-08-17 22:39:59 +00003348 llvm::StringRef FieldNames[] = {
Mike Stump94967902009-10-21 18:16:27 +00003349 "__isa",
3350 "__forwarding",
3351 "__flags",
3352 "__size",
3353 "__copy_helper",
3354 "__destroy_helper",
3355 DeclName,
3356 };
3357
3358 for (size_t i = 0; i < 7; ++i) {
3359 if (!HasCopyAndDispose && i >=4 && i <= 5)
3360 continue;
3361 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3362 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003363 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003364 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003365 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003366 T->addDecl(Field);
3367 }
3368
Douglas Gregord5058122010-02-11 01:19:42 +00003369 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003370
3371 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003372}
3373
3374
3375QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003376 bool BlockHasCopyDispose,
John McCall87fe5d52010-05-20 01:18:31 +00003377 llvm::SmallVectorImpl<const Expr *> &Layout) {
3378
Mike Stumpd0153282009-10-20 02:12:22 +00003379 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003380 llvm::SmallString<36> Name;
3381 llvm::raw_svector_ostream(Name) << "__block_literal_"
3382 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003383 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003384 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003385 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003386 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003387 QualType FieldTypes[] = {
3388 getPointerType(VoidPtrTy),
3389 IntTy,
3390 IntTy,
3391 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003392 (BlockHasCopyDispose ?
3393 getPointerType(getBlockDescriptorExtendedType()) :
3394 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003395 };
3396
3397 const char *FieldNames[] = {
3398 "__isa",
3399 "__flags",
3400 "__reserved",
3401 "__FuncPtr",
3402 "__descriptor"
3403 };
3404
3405 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003406 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003407 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003408 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003409 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003410 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003411 T->addDecl(Field);
3412 }
3413
John McCall87fe5d52010-05-20 01:18:31 +00003414 for (unsigned i = 0; i < Layout.size(); ++i) {
3415 const Expr *E = Layout[i];
Mike Stump7fe9cc12009-10-21 03:49:08 +00003416
John McCall87fe5d52010-05-20 01:18:31 +00003417 QualType FieldType = E->getType();
3418 IdentifierInfo *FieldName = 0;
3419 if (isa<CXXThisExpr>(E)) {
3420 FieldName = &Idents.get("this");
3421 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3422 const ValueDecl *D = BDRE->getDecl();
3423 FieldName = D->getIdentifier();
3424 if (BDRE->isByRef())
Daniel Dunbar56df9772010-08-17 22:39:59 +00003425 FieldType = BuildByRefType(D->getName(), FieldType);
John McCall87fe5d52010-05-20 01:18:31 +00003426 } else {
3427 // Padding.
3428 assert(isa<ConstantArrayType>(FieldType) &&
3429 isa<DeclRefExpr>(E) &&
3430 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3431 "doesn't match characteristics of padding decl");
3432 }
Mike Stump7fe9cc12009-10-21 03:49:08 +00003433
3434 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCall87fe5d52010-05-20 01:18:31 +00003435 FieldName, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003436 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003437 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003438 T->addDecl(Field);
3439 }
3440
Douglas Gregord5058122010-02-11 01:19:42 +00003441 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003442
3443 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003444}
3445
Douglas Gregor512b0772009-04-23 22:29:11 +00003446void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003447 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003448 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3449 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3450}
3451
Anders Carlsson18acd442007-10-29 06:33:42 +00003452// This returns true if a type has been typedefed to BOOL:
3453// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003454static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003455 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003456 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3457 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003458
Anders Carlssond8499822007-10-29 05:01:08 +00003459 return false;
3460}
3461
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003462/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003463/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003464CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003465 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003466
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003467 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003468 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003469 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003470 // Treat arrays as pointers, since that's how they're passed in.
3471 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003472 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003473 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003474}
3475
3476static inline
3477std::string charUnitsToString(const CharUnits &CU) {
3478 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003479}
3480
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003481/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003482/// declaration.
3483void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3484 std::string& S) {
3485 const BlockDecl *Decl = Expr->getBlockDecl();
3486 QualType BlockTy =
3487 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3488 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003489 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003490 // Compute size of all parameters.
3491 // Start with computing size of a pointer in number of bytes.
3492 // FIXME: There might(should) be a better way of doing this computation!
3493 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003494 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3495 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003496 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003497 E = Decl->param_end(); PI != E; ++PI) {
3498 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003499 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003500 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003501 ParmOffset += sz;
3502 }
3503 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003504 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003505 // Block pointer and offset.
3506 S += "@?0";
3507 ParmOffset = PtrSize;
3508
3509 // Argument types.
3510 ParmOffset = PtrSize;
3511 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3512 Decl->param_end(); PI != E; ++PI) {
3513 ParmVarDecl *PVDecl = *PI;
3514 QualType PType = PVDecl->getOriginalType();
3515 if (const ArrayType *AT =
3516 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3517 // Use array's original type only if it has known number of
3518 // elements.
3519 if (!isa<ConstantArrayType>(AT))
3520 PType = PVDecl->getType();
3521 } else if (PType->isFunctionType())
3522 PType = PVDecl->getType();
3523 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003524 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003525 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003526 }
3527}
3528
David Chisnall50e4eba2010-12-30 14:05:53 +00003529void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
3530 std::string& S) {
3531 // Encode result type.
3532 getObjCEncodingForType(Decl->getResultType(), S);
3533 CharUnits ParmOffset;
3534 // Compute size of all parameters.
3535 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3536 E = Decl->param_end(); PI != E; ++PI) {
3537 QualType PType = (*PI)->getType();
3538 CharUnits sz = getObjCEncodingTypeSize(PType);
3539 assert (sz.isPositive() &&
3540 "getObjCEncodingForMethodDecl - Incomplete param type");
3541 ParmOffset += sz;
3542 }
3543 S += charUnitsToString(ParmOffset);
3544 ParmOffset = CharUnits::Zero();
3545
3546 // Argument types.
3547 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3548 E = Decl->param_end(); PI != E; ++PI) {
3549 ParmVarDecl *PVDecl = *PI;
3550 QualType PType = PVDecl->getOriginalType();
3551 if (const ArrayType *AT =
3552 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3553 // Use array's original type only if it has known number of
3554 // elements.
3555 if (!isa<ConstantArrayType>(AT))
3556 PType = PVDecl->getType();
3557 } else if (PType->isFunctionType())
3558 PType = PVDecl->getType();
3559 getObjCEncodingForType(PType, S);
3560 S += charUnitsToString(ParmOffset);
3561 ParmOffset += getObjCEncodingTypeSize(PType);
3562 }
3563}
3564
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003565/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003566/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003567void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003568 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003569 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003570 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003571 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003572 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003573 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003574 // Compute size of all parameters.
3575 // Start with computing size of a pointer in number of bytes.
3576 // FIXME: There might(should) be a better way of doing this computation!
3577 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003578 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003579 // The first two arguments (self and _cmd) are pointers; account for
3580 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003581 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003582 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003583 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003584 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003585 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003586 assert (sz.isPositive() &&
3587 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003588 ParmOffset += sz;
3589 }
Ken Dyck40775002010-01-11 17:06:35 +00003590 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003591 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003592 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003593
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003594 // Argument types.
3595 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003596 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003597 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003598 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003599 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003600 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003601 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3602 // Use array's original type only if it has known number of
3603 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003604 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003605 PType = PVDecl->getType();
3606 } else if (PType->isFunctionType())
3607 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003608 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003609 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003610 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003611 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003612 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003613 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003614 }
3615}
3616
Daniel Dunbar4932b362008-08-28 04:38:10 +00003617/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003618/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003619/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3620/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003621/// Property attributes are stored as a comma-delimited C string. The simple
3622/// attributes readonly and bycopy are encoded as single characters. The
3623/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3624/// encoded as single characters, followed by an identifier. Property types
3625/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003626/// these attributes are defined by the following enumeration:
3627/// @code
3628/// enum PropertyAttributes {
3629/// kPropertyReadOnly = 'R', // property is read-only.
3630/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3631/// kPropertyByref = '&', // property is a reference to the value last assigned
3632/// kPropertyDynamic = 'D', // property is dynamic
3633/// kPropertyGetter = 'G', // followed by getter selector name
3634/// kPropertySetter = 'S', // followed by setter selector name
3635/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3636/// kPropertyType = 't' // followed by old-style type encoding.
3637/// kPropertyWeak = 'W' // 'weak' property
3638/// kPropertyStrong = 'P' // property GC'able
3639/// kPropertyNonAtomic = 'N' // property non-atomic
3640/// };
3641/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003642void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003643 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003644 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003645 // Collect information from the property implementation decl(s).
3646 bool Dynamic = false;
3647 ObjCPropertyImplDecl *SynthesizePID = 0;
3648
3649 // FIXME: Duplicated code due to poor abstraction.
3650 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003651 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003652 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3653 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003654 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003655 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003656 ObjCPropertyImplDecl *PID = *i;
3657 if (PID->getPropertyDecl() == PD) {
3658 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3659 Dynamic = true;
3660 } else {
3661 SynthesizePID = PID;
3662 }
3663 }
3664 }
3665 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003666 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003667 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003668 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003669 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003670 ObjCPropertyImplDecl *PID = *i;
3671 if (PID->getPropertyDecl() == PD) {
3672 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3673 Dynamic = true;
3674 } else {
3675 SynthesizePID = PID;
3676 }
3677 }
Mike Stump11289f42009-09-09 15:08:12 +00003678 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003679 }
3680 }
3681
3682 // FIXME: This is not very efficient.
3683 S = "T";
3684
3685 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003686 // GCC has some special rules regarding encoding of properties which
3687 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003688 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003689 true /* outermost type */,
3690 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003691
3692 if (PD->isReadOnly()) {
3693 S += ",R";
3694 } else {
3695 switch (PD->getSetterKind()) {
3696 case ObjCPropertyDecl::Assign: break;
3697 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003698 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003699 }
3700 }
3701
3702 // It really isn't clear at all what this means, since properties
3703 // are "dynamic by default".
3704 if (Dynamic)
3705 S += ",D";
3706
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003707 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3708 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003709
Daniel Dunbar4932b362008-08-28 04:38:10 +00003710 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3711 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003712 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003713 }
3714
3715 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3716 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003717 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003718 }
3719
3720 if (SynthesizePID) {
3721 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3722 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003723 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003724 }
3725
3726 // FIXME: OBJCGC: weak & strong
3727}
3728
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003729/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003730/// Another legacy compatibility encoding: 32-bit longs are encoded as
3731/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003732/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3733///
3734void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003735 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003736 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003737 if (BT->getKind() == BuiltinType::ULong &&
3738 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003739 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003740 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003741 if (BT->getKind() == BuiltinType::Long &&
3742 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003743 PointeeTy = IntTy;
3744 }
3745 }
3746}
3747
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003748void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003749 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003750 // We follow the behavior of gcc, expanding structures which are
3751 // directly pointed to, and expanding embedded structures. Note that
3752 // these rules are sufficient to prevent recursive encoding of the
3753 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003754 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003755 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003756}
3757
David Chisnallb190a2c2010-06-04 01:10:52 +00003758static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3759 switch (T->getAs<BuiltinType>()->getKind()) {
3760 default: assert(0 && "Unhandled builtin type kind");
3761 case BuiltinType::Void: return 'v';
3762 case BuiltinType::Bool: return 'B';
3763 case BuiltinType::Char_U:
3764 case BuiltinType::UChar: return 'C';
3765 case BuiltinType::UShort: return 'S';
3766 case BuiltinType::UInt: return 'I';
3767 case BuiltinType::ULong:
3768 return
3769 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3770 case BuiltinType::UInt128: return 'T';
3771 case BuiltinType::ULongLong: return 'Q';
3772 case BuiltinType::Char_S:
3773 case BuiltinType::SChar: return 'c';
3774 case BuiltinType::Short: return 's';
Chris Lattnerad3467e2010-12-25 23:25:43 +00003775 case BuiltinType::WChar_S:
3776 case BuiltinType::WChar_U:
David Chisnallb190a2c2010-06-04 01:10:52 +00003777 case BuiltinType::Int: return 'i';
3778 case BuiltinType::Long:
3779 return
3780 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3781 case BuiltinType::LongLong: return 'q';
3782 case BuiltinType::Int128: return 't';
3783 case BuiltinType::Float: return 'f';
3784 case BuiltinType::Double: return 'd';
Daniel Dunbar7cba5a72010-10-11 21:13:48 +00003785 case BuiltinType::LongDouble: return 'D';
David Chisnallb190a2c2010-06-04 01:10:52 +00003786 }
3787}
3788
Mike Stump11289f42009-09-09 15:08:12 +00003789static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00003790 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003791 const Expr *E = FD->getBitWidth();
3792 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3793 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003794 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00003795 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3796 // The GNU runtime requires more information; bitfields are encoded as b,
3797 // then the offset (in bits) of the first element, then the type of the
3798 // bitfield, then the size in bits. For example, in this structure:
3799 //
3800 // struct
3801 // {
3802 // int integer;
3803 // int flags:2;
3804 // };
3805 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3806 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3807 // information is not especially sensible, but we're stuck with it for
3808 // compatibility with GCC, although providing it breaks anything that
3809 // actually uses runtime introspection and wants to work on both runtimes...
3810 if (!Ctx->getLangOptions().NeXTRuntime) {
3811 const RecordDecl *RD = FD->getParent();
3812 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3813 // FIXME: This same linear search is also used in ExprConstant - it might
3814 // be better if the FieldDecl stored its offset. We'd be increasing the
3815 // size of the object slightly, but saving some time every time it is used.
3816 unsigned i = 0;
3817 for (RecordDecl::field_iterator Field = RD->field_begin(),
3818 FieldEnd = RD->field_end();
3819 Field != FieldEnd; (void)++Field, ++i) {
3820 if (*Field == FD)
3821 break;
3822 }
3823 S += llvm::utostr(RL.getFieldOffset(i));
David Chisnall6f0a7d22010-12-26 20:12:30 +00003824 if (T->isEnumeralType())
3825 S += 'i';
3826 else
3827 S += ObjCEncodingForPrimitiveKind(Context, T);
David Chisnallb190a2c2010-06-04 01:10:52 +00003828 }
3829 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003830 S += llvm::utostr(N);
3831}
3832
Daniel Dunbar07d07852009-10-18 21:17:35 +00003833// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003834void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3835 bool ExpandPointedToStructures,
3836 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003837 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003838 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003839 bool EncodingProperty) {
David Chisnallb190a2c2010-06-04 01:10:52 +00003840 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003841 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003842 return EncodeBitField(this, S, T, FD);
3843 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003844 return;
3845 }
Mike Stump11289f42009-09-09 15:08:12 +00003846
John McCall9dd450b2009-09-21 23:43:11 +00003847 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003848 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003849 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003850 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003851 return;
3852 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003853
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003854 // encoding for pointer or r3eference types.
3855 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003856 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003857 if (PT->isObjCSelType()) {
3858 S += ':';
3859 return;
3860 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003861 PointeeTy = PT->getPointeeType();
3862 }
3863 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3864 PointeeTy = RT->getPointeeType();
3865 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003866 bool isReadOnly = false;
3867 // For historical/compatibility reasons, the read-only qualifier of the
3868 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3869 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003870 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003871 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003872 if (OutermostType && T.isConstQualified()) {
3873 isReadOnly = true;
3874 S += 'r';
3875 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003876 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003877 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003878 while (P->getAs<PointerType>())
3879 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003880 if (P.isConstQualified()) {
3881 isReadOnly = true;
3882 S += 'r';
3883 }
3884 }
3885 if (isReadOnly) {
3886 // Another legacy compatibility encoding. Some ObjC qualifier and type
3887 // combinations need to be rearranged.
3888 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003889 if (llvm::StringRef(S).endswith("nr"))
3890 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003891 }
Mike Stump11289f42009-09-09 15:08:12 +00003892
Anders Carlssond8499822007-10-29 05:01:08 +00003893 if (PointeeTy->isCharType()) {
3894 // char pointer types should be encoded as '*' unless it is a
3895 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003896 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003897 S += '*';
3898 return;
3899 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003900 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003901 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3902 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3903 S += '#';
3904 return;
3905 }
3906 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3907 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3908 S += '@';
3909 return;
3910 }
3911 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003912 }
Anders Carlssond8499822007-10-29 05:01:08 +00003913 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003914 getLegacyIntegralTypeEncoding(PointeeTy);
3915
Mike Stump11289f42009-09-09 15:08:12 +00003916 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003917 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003918 return;
3919 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003920
Chris Lattnere7cabb92009-07-13 00:10:46 +00003921 if (const ArrayType *AT =
3922 // Ignore type qualifiers etc.
3923 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003924 if (isa<IncompleteArrayType>(AT)) {
3925 // Incomplete arrays are encoded as a pointer to the array element.
3926 S += '^';
3927
Mike Stump11289f42009-09-09 15:08:12 +00003928 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003929 false, ExpandStructures, FD);
3930 } else {
3931 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003932
Anders Carlssond05f44b2009-02-22 01:38:57 +00003933 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3934 S += llvm::utostr(CAT->getSize().getZExtValue());
3935 else {
3936 //Variable length arrays are encoded as a regular array with 0 elements.
3937 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3938 S += '0';
3939 }
Mike Stump11289f42009-09-09 15:08:12 +00003940
3941 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003942 false, ExpandStructures, FD);
3943 S += ']';
3944 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003945 return;
3946 }
Mike Stump11289f42009-09-09 15:08:12 +00003947
John McCall9dd450b2009-09-21 23:43:11 +00003948 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003949 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003950 return;
3951 }
Mike Stump11289f42009-09-09 15:08:12 +00003952
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003953 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003954 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003955 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003956 // Anonymous structures print as '?'
3957 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3958 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003959 if (ClassTemplateSpecializationDecl *Spec
3960 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3961 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3962 std::string TemplateArgsStr
3963 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003964 TemplateArgs.data(),
3965 TemplateArgs.size(),
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003966 (*this).PrintingPolicy);
3967
3968 S += TemplateArgsStr;
3969 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003970 } else {
3971 S += '?';
3972 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003973 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003974 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003975 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3976 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003977 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003978 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003979 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003980 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003981 S += '"';
3982 }
Mike Stump11289f42009-09-09 15:08:12 +00003983
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003984 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003985 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003986 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003987 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003988 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003989 QualType qt = Field->getType();
3990 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003991 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003992 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003993 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003994 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003995 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003996 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003997 return;
3998 }
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00003999
Chris Lattnere7cabb92009-07-13 00:10:46 +00004000 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004001 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00004002 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004003 else
4004 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004005 return;
4006 }
Mike Stump11289f42009-09-09 15:08:12 +00004007
Chris Lattnere7cabb92009-07-13 00:10:46 +00004008 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00004009 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00004010 return;
4011 }
Mike Stump11289f42009-09-09 15:08:12 +00004012
John McCall8b07ec22010-05-15 11:32:37 +00004013 // Ignore protocol qualifiers when mangling at this level.
4014 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4015 T = OT->getBaseType();
4016
John McCall8ccfcb52009-09-24 19:53:00 +00004017 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004018 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00004019 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004020 S += '{';
4021 const IdentifierInfo *II = OI->getIdentifier();
4022 S += II->getName();
4023 S += '=';
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004024 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
4025 DeepCollectObjCIvars(OI, true, Ivars);
4026 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4027 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4028 if (Field->isBitField())
4029 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004030 else
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004031 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004032 }
4033 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004034 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004035 }
Mike Stump11289f42009-09-09 15:08:12 +00004036
John McCall9dd450b2009-09-21 23:43:11 +00004037 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004038 if (OPT->isObjCIdType()) {
4039 S += '@';
4040 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004041 }
Mike Stump11289f42009-09-09 15:08:12 +00004042
Steve Narofff0c86112009-10-28 22:03:49 +00004043 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4044 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4045 // Since this is a binary compatibility issue, need to consult with runtime
4046 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004047 S += '#';
4048 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004049 }
Mike Stump11289f42009-09-09 15:08:12 +00004050
Chris Lattnere7cabb92009-07-13 00:10:46 +00004051 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00004052 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00004053 ExpandPointedToStructures,
4054 ExpandStructures, FD);
4055 if (FD || EncodingProperty) {
4056 // Note that we do extended encoding of protocol qualifer list
4057 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004058 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00004059 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4060 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004061 S += '<';
4062 S += (*I)->getNameAsString();
4063 S += '>';
4064 }
4065 S += '"';
4066 }
4067 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004068 }
Mike Stump11289f42009-09-09 15:08:12 +00004069
Chris Lattnere7cabb92009-07-13 00:10:46 +00004070 QualType PointeeTy = OPT->getPointeeType();
4071 if (!EncodingProperty &&
4072 isa<TypedefType>(PointeeTy.getTypePtr())) {
4073 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00004074 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00004075 // {...};
4076 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00004077 getObjCEncodingForTypeImpl(PointeeTy, S,
4078 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00004079 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00004080 return;
4081 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004082
4083 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00004084 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004085 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00004086 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00004087 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4088 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004089 S += '<';
4090 S += (*I)->getNameAsString();
4091 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00004092 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004093 S += '"';
4094 }
4095 return;
4096 }
Mike Stump11289f42009-09-09 15:08:12 +00004097
John McCalla9e6e8d2010-05-17 23:56:34 +00004098 // gcc just blithely ignores member pointers.
4099 // TODO: maybe there should be a mangling for these
4100 if (T->getAs<MemberPointerType>())
4101 return;
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004102
4103 if (T->isVectorType()) {
4104 // This matches gcc's encoding, even though technically it is
4105 // insufficient.
4106 // FIXME. We should do a better job than gcc.
4107 return;
4108 }
4109
Chris Lattnere7cabb92009-07-13 00:10:46 +00004110 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00004111}
4112
Mike Stump11289f42009-09-09 15:08:12 +00004113void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00004114 std::string& S) const {
4115 if (QT & Decl::OBJC_TQ_In)
4116 S += 'n';
4117 if (QT & Decl::OBJC_TQ_Inout)
4118 S += 'N';
4119 if (QT & Decl::OBJC_TQ_Out)
4120 S += 'o';
4121 if (QT & Decl::OBJC_TQ_Bycopy)
4122 S += 'O';
4123 if (QT & Decl::OBJC_TQ_Byref)
4124 S += 'R';
4125 if (QT & Decl::OBJC_TQ_Oneway)
4126 S += 'V';
4127}
4128
Chris Lattnere7cabb92009-07-13 00:10:46 +00004129void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00004130 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004131
Anders Carlsson87c149b2007-10-11 01:00:40 +00004132 BuiltinVaListType = T;
4133}
4134
Chris Lattnere7cabb92009-07-13 00:10:46 +00004135void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004136 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00004137}
4138
Chris Lattnere7cabb92009-07-13 00:10:46 +00004139void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00004140 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00004141}
4142
Chris Lattnere7cabb92009-07-13 00:10:46 +00004143void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004144 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004145}
4146
Chris Lattnere7cabb92009-07-13 00:10:46 +00004147void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004148 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00004149}
4150
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004151void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00004152 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00004153 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004154
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004155 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00004156}
4157
John McCalld28ae272009-12-02 08:04:21 +00004158/// \brief Retrieve the template name that corresponds to a non-empty
4159/// lookup.
John McCallad371252010-01-20 00:46:10 +00004160TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4161 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00004162 unsigned size = End - Begin;
4163 assert(size > 1 && "set is not overloaded!");
4164
4165 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4166 size * sizeof(FunctionTemplateDecl*));
4167 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4168
4169 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00004170 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00004171 NamedDecl *D = *I;
4172 assert(isa<FunctionTemplateDecl>(D) ||
4173 (isa<UsingShadowDecl>(D) &&
4174 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4175 *Storage++ = D;
4176 }
4177
4178 return TemplateName(OT);
4179}
4180
Douglas Gregordc572a32009-03-30 22:58:21 +00004181/// \brief Retrieve the template name that represents a qualified
4182/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00004183TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00004184 bool TemplateKeyword,
4185 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00004186 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00004187 llvm::FoldingSetNodeID ID;
4188 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4189
4190 void *InsertPos = 0;
4191 QualifiedTemplateName *QTN =
4192 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4193 if (!QTN) {
4194 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4195 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4196 }
4197
4198 return TemplateName(QTN);
4199}
4200
4201/// \brief Retrieve the template name that represents a dependent
4202/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00004203TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00004204 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004205 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00004206 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00004207
4208 llvm::FoldingSetNodeID ID;
4209 DependentTemplateName::Profile(ID, NNS, Name);
4210
4211 void *InsertPos = 0;
4212 DependentTemplateName *QTN =
4213 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4214
4215 if (QTN)
4216 return TemplateName(QTN);
4217
4218 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4219 if (CanonNNS == NNS) {
4220 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4221 } else {
4222 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4223 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004224 DependentTemplateName *CheckQTN =
4225 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4226 assert(!CheckQTN && "Dependent type name canonicalization broken");
4227 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004228 }
4229
4230 DependentTemplateNames.InsertNode(QTN, InsertPos);
4231 return TemplateName(QTN);
4232}
4233
Douglas Gregor71395fa2009-11-04 00:56:37 +00004234/// \brief Retrieve the template name that represents a dependent
4235/// template name such as \c MetaFun::template operator+.
4236TemplateName
4237ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4238 OverloadedOperatorKind Operator) {
4239 assert((!NNS || NNS->isDependent()) &&
4240 "Nested name specifier must be dependent");
4241
4242 llvm::FoldingSetNodeID ID;
4243 DependentTemplateName::Profile(ID, NNS, Operator);
4244
4245 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004246 DependentTemplateName *QTN
4247 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004248
4249 if (QTN)
4250 return TemplateName(QTN);
4251
4252 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4253 if (CanonNNS == NNS) {
4254 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4255 } else {
4256 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4257 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004258
4259 DependentTemplateName *CheckQTN
4260 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4261 assert(!CheckQTN && "Dependent template name canonicalization broken");
4262 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004263 }
4264
4265 DependentTemplateNames.InsertNode(QTN, InsertPos);
4266 return TemplateName(QTN);
4267}
4268
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004269/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004270/// TargetInfo, produce the corresponding type. The unsigned @p Type
4271/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004272CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004273 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004274 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004275 case TargetInfo::SignedShort: return ShortTy;
4276 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4277 case TargetInfo::SignedInt: return IntTy;
4278 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4279 case TargetInfo::SignedLong: return LongTy;
4280 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4281 case TargetInfo::SignedLongLong: return LongLongTy;
4282 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4283 }
4284
4285 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004286 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004287}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004288
4289//===----------------------------------------------------------------------===//
4290// Type Predicates.
4291//===----------------------------------------------------------------------===//
4292
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004293/// isObjCNSObjectType - Return true if this is an NSObject object using
4294/// NSObject attribute on a c-style pointer type.
4295/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004296/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004297///
4298bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4299 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4300 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004301 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004302 return true;
4303 }
Mike Stump11289f42009-09-09 15:08:12 +00004304 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004305}
4306
Fariborz Jahanian96207692009-02-18 21:49:28 +00004307/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4308/// garbage collection attribute.
4309///
John McCall8ccfcb52009-09-24 19:53:00 +00004310Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4311 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004312 if (getLangOptions().ObjC1 &&
4313 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00004314 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00004315 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00004316 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004317 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00004318 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00004319 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004320 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004321 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004322 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004323 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00004324 // Non-pointers have none gc'able attribute regardless of the attribute
4325 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00004326 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004327 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004328 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004329 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004330}
4331
Chris Lattner49af6a42008-04-07 06:51:04 +00004332//===----------------------------------------------------------------------===//
4333// Type Compatibility Testing
4334//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004335
Mike Stump11289f42009-09-09 15:08:12 +00004336/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004337/// compatible.
4338static bool areCompatVectorTypes(const VectorType *LHS,
4339 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004340 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004341 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004342 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004343}
4344
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004345bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4346 QualType SecondVec) {
4347 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4348 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4349
4350 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4351 return true;
4352
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004353 // Treat Neon vector types and most AltiVec vector types as if they are the
4354 // equivalent GCC vector types.
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004355 const VectorType *First = FirstVec->getAs<VectorType>();
4356 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004357 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004358 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004359 First->getVectorKind() != VectorType::AltiVecPixel &&
4360 First->getVectorKind() != VectorType::AltiVecBool &&
4361 Second->getVectorKind() != VectorType::AltiVecPixel &&
4362 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004363 return true;
4364
4365 return false;
4366}
4367
Steve Naroff8e6aee52009-07-23 01:01:38 +00004368//===----------------------------------------------------------------------===//
4369// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4370//===----------------------------------------------------------------------===//
4371
4372/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4373/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004374bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4375 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004376 if (lProto == rProto)
4377 return true;
4378 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4379 E = rProto->protocol_end(); PI != E; ++PI)
4380 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4381 return true;
4382 return false;
4383}
4384
Steve Naroff8e6aee52009-07-23 01:01:38 +00004385/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4386/// return true if lhs's protocols conform to rhs's protocol; false
4387/// otherwise.
4388bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4389 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4390 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4391 return false;
4392}
4393
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004394/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4395/// Class<p1, ...>.
4396bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4397 QualType rhs) {
4398 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4399 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4400 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4401
4402 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4403 E = lhsQID->qual_end(); I != E; ++I) {
4404 bool match = false;
4405 ObjCProtocolDecl *lhsProto = *I;
4406 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4407 E = rhsOPT->qual_end(); J != E; ++J) {
4408 ObjCProtocolDecl *rhsProto = *J;
4409 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4410 match = true;
4411 break;
4412 }
4413 }
4414 if (!match)
4415 return false;
4416 }
4417 return true;
4418}
4419
Steve Naroff8e6aee52009-07-23 01:01:38 +00004420/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4421/// ObjCQualifiedIDType.
4422bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4423 bool compare) {
4424 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004425 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004426 lhs->isObjCIdType() || lhs->isObjCClassType())
4427 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004428 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004429 rhs->isObjCIdType() || rhs->isObjCClassType())
4430 return true;
4431
4432 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004433 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004434
Steve Naroff8e6aee52009-07-23 01:01:38 +00004435 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004436
Steve Naroff8e6aee52009-07-23 01:01:38 +00004437 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004438 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004439 // make sure we check the class hierarchy.
4440 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4441 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4442 E = lhsQID->qual_end(); I != E; ++I) {
4443 // when comparing an id<P> on lhs with a static type on rhs,
4444 // see if static class implements all of id's protocols, directly or
4445 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004446 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004447 return false;
4448 }
4449 }
4450 // If there are no qualifiers and no interface, we have an 'id'.
4451 return true;
4452 }
Mike Stump11289f42009-09-09 15:08:12 +00004453 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004454 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4455 E = lhsQID->qual_end(); I != E; ++I) {
4456 ObjCProtocolDecl *lhsProto = *I;
4457 bool match = false;
4458
4459 // when comparing an id<P> on lhs with a static type on rhs,
4460 // see if static class implements all of id's protocols, directly or
4461 // through its super class and categories.
4462 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4463 E = rhsOPT->qual_end(); J != E; ++J) {
4464 ObjCProtocolDecl *rhsProto = *J;
4465 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4466 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4467 match = true;
4468 break;
4469 }
4470 }
Mike Stump11289f42009-09-09 15:08:12 +00004471 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004472 // make sure we check the class hierarchy.
4473 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4474 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4475 E = lhsQID->qual_end(); I != E; ++I) {
4476 // when comparing an id<P> on lhs with a static type on rhs,
4477 // see if static class implements all of id's protocols, directly or
4478 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004479 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004480 match = true;
4481 break;
4482 }
4483 }
4484 }
4485 if (!match)
4486 return false;
4487 }
Mike Stump11289f42009-09-09 15:08:12 +00004488
Steve Naroff8e6aee52009-07-23 01:01:38 +00004489 return true;
4490 }
Mike Stump11289f42009-09-09 15:08:12 +00004491
Steve Naroff8e6aee52009-07-23 01:01:38 +00004492 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4493 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4494
Mike Stump11289f42009-09-09 15:08:12 +00004495 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004496 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004497 // If both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004498 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4499 E = lhsOPT->qual_end(); I != E; ++I) {
4500 ObjCProtocolDecl *lhsProto = *I;
4501 bool match = false;
4502
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004503 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff8e6aee52009-07-23 01:01:38 +00004504 // see if static class implements all of id's protocols, directly or
4505 // through its super class and categories.
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004506 // First, lhs protocols in the qualifier list must be found, direct
4507 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004508 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4509 E = rhsQID->qual_end(); J != E; ++J) {
4510 ObjCProtocolDecl *rhsProto = *J;
4511 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4512 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4513 match = true;
4514 break;
4515 }
4516 }
4517 if (!match)
4518 return false;
4519 }
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004520
4521 // Static class's protocols, or its super class or category protocols
4522 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4523 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4524 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4525 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4526 // This is rather dubious but matches gcc's behavior. If lhs has
4527 // no type qualifier and its class has no static protocol(s)
4528 // assume that it is mismatch.
4529 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4530 return false;
4531 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4532 LHSInheritedProtocols.begin(),
4533 E = LHSInheritedProtocols.end(); I != E; ++I) {
4534 bool match = false;
4535 ObjCProtocolDecl *lhsProto = (*I);
4536 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4537 E = rhsQID->qual_end(); J != E; ++J) {
4538 ObjCProtocolDecl *rhsProto = *J;
4539 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4540 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4541 match = true;
4542 break;
4543 }
4544 }
4545 if (!match)
4546 return false;
4547 }
4548 }
Steve Naroff8e6aee52009-07-23 01:01:38 +00004549 return true;
4550 }
4551 return false;
4552}
4553
Eli Friedman47f77112008-08-22 00:56:42 +00004554/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004555/// compatible for assignment from RHS to LHS. This handles validation of any
4556/// protocol qualifiers on the LHS or RHS.
4557///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004558bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4559 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004560 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4561 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4562
Steve Naroff1329fa02009-07-15 18:40:39 +00004563 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004564 if (LHS->isObjCUnqualifiedIdOrClass() ||
4565 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004566 return true;
4567
John McCall8b07ec22010-05-15 11:32:37 +00004568 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004569 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4570 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004571 false);
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004572
4573 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4574 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4575 QualType(RHSOPT,0));
4576
John McCall8b07ec22010-05-15 11:32:37 +00004577 // If we have 2 user-defined types, fall into that path.
4578 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004579 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004580
Steve Naroff8e6aee52009-07-23 01:01:38 +00004581 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004582}
4583
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004584/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4585/// for providing type-safty for objective-c pointers used to pass/return
4586/// arguments in block literals. When passed as arguments, passing 'A*' where
4587/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4588/// not OK. For the return type, the opposite is not OK.
4589bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4590 const ObjCObjectPointerType *LHSOPT,
4591 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004592 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004593 return true;
4594
4595 if (LHSOPT->isObjCBuiltinType()) {
4596 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4597 }
4598
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004599 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004600 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4601 QualType(RHSOPT,0),
4602 false);
4603
4604 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4605 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4606 if (LHS && RHS) { // We have 2 user-defined types.
4607 if (LHS != RHS) {
4608 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4609 return false;
4610 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4611 return true;
4612 }
4613 else
4614 return true;
4615 }
4616 return false;
4617}
4618
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004619/// getIntersectionOfProtocols - This routine finds the intersection of set
4620/// of protocols inherited from two distinct objective-c pointer objects.
4621/// It is used to build composite qualifier list of the composite type of
4622/// the conditional expression involving two objective-c pointer objects.
4623static
4624void getIntersectionOfProtocols(ASTContext &Context,
4625 const ObjCObjectPointerType *LHSOPT,
4626 const ObjCObjectPointerType *RHSOPT,
4627 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4628
John McCall8b07ec22010-05-15 11:32:37 +00004629 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4630 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4631 assert(LHS->getInterface() && "LHS must have an interface base");
4632 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004633
4634 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4635 unsigned LHSNumProtocols = LHS->getNumProtocols();
4636 if (LHSNumProtocols > 0)
4637 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4638 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004639 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004640 Context.CollectInheritedProtocols(LHS->getInterface(),
4641 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004642 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4643 LHSInheritedProtocols.end());
4644 }
4645
4646 unsigned RHSNumProtocols = RHS->getNumProtocols();
4647 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004648 ObjCProtocolDecl **RHSProtocols =
4649 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004650 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4651 if (InheritedProtocolSet.count(RHSProtocols[i]))
4652 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4653 }
4654 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004655 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004656 Context.CollectInheritedProtocols(RHS->getInterface(),
4657 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004658 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4659 RHSInheritedProtocols.begin(),
4660 E = RHSInheritedProtocols.end(); I != E; ++I)
4661 if (InheritedProtocolSet.count((*I)))
4662 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004663 }
4664}
4665
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004666/// areCommonBaseCompatible - Returns common base class of the two classes if
4667/// one found. Note that this is O'2 algorithm. But it will be called as the
4668/// last type comparison in a ?-exp of ObjC pointer types before a
4669/// warning is issued. So, its invokation is extremely rare.
4670QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004671 const ObjCObjectPointerType *Lptr,
4672 const ObjCObjectPointerType *Rptr) {
4673 const ObjCObjectType *LHS = Lptr->getObjectType();
4674 const ObjCObjectType *RHS = Rptr->getObjectType();
4675 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4676 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4677 if (!LDecl || !RDecl)
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004678 return QualType();
4679
John McCall8b07ec22010-05-15 11:32:37 +00004680 while ((LDecl = LDecl->getSuperClass())) {
4681 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004682 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004683 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4684 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4685
4686 QualType Result = QualType(LHS, 0);
4687 if (!Protocols.empty())
4688 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4689 Result = getObjCObjectPointerType(Result);
4690 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004691 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004692 }
4693
4694 return QualType();
4695}
4696
John McCall8b07ec22010-05-15 11:32:37 +00004697bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4698 const ObjCObjectType *RHS) {
4699 assert(LHS->getInterface() && "LHS is not an interface type");
4700 assert(RHS->getInterface() && "RHS is not an interface type");
4701
Chris Lattner49af6a42008-04-07 06:51:04 +00004702 // Verify that the base decls are compatible: the RHS must be a subclass of
4703 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00004704 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00004705 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004706
Chris Lattner49af6a42008-04-07 06:51:04 +00004707 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4708 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004709 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004710 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004711
Chris Lattner49af6a42008-04-07 06:51:04 +00004712 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4713 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004714 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004715 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004716
John McCall8b07ec22010-05-15 11:32:37 +00004717 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4718 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004719 LHSPI != LHSPE; LHSPI++) {
4720 bool RHSImplementsProtocol = false;
4721
4722 // If the RHS doesn't implement the protocol on the left, the types
4723 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00004724 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4725 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004726 RHSPI != RHSPE; RHSPI++) {
4727 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004728 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004729 break;
4730 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004731 }
4732 // FIXME: For better diagnostics, consider passing back the protocol name.
4733 if (!RHSImplementsProtocol)
4734 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004735 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004736 // The RHS implements all protocols listed on the LHS.
4737 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004738}
4739
Steve Naroffb7605152009-02-12 17:52:19 +00004740bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4741 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004742 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4743 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004744
Steve Naroff7cae42b2009-07-10 23:34:53 +00004745 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004746 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004747
4748 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4749 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004750}
4751
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00004752bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
4753 return canAssignObjCInterfaces(
4754 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
4755 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
4756}
4757
Mike Stump11289f42009-09-09 15:08:12 +00004758/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004759/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004760/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004761/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004762bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
4763 bool CompareUnqualified) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004764 if (getLangOptions().CPlusPlus)
4765 return hasSameType(LHS, RHS);
4766
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004767 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman47f77112008-08-22 00:56:42 +00004768}
4769
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004770bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4771 return !mergeTypes(LHS, RHS, true).isNull();
4772}
4773
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004774/// mergeTransparentUnionType - if T is a transparent union type and a member
4775/// of T is compatible with SubType, return the merged type, else return
4776/// QualType()
4777QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
4778 bool OfBlockPointer,
4779 bool Unqualified) {
4780 if (const RecordType *UT = T->getAsUnionType()) {
4781 RecordDecl *UD = UT->getDecl();
4782 if (UD->hasAttr<TransparentUnionAttr>()) {
4783 for (RecordDecl::field_iterator it = UD->field_begin(),
4784 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbourne19b961d2010-12-02 21:00:06 +00004785 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004786 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
4787 if (!MT.isNull())
4788 return MT;
4789 }
4790 }
4791 }
4792
4793 return QualType();
4794}
4795
4796/// mergeFunctionArgumentTypes - merge two types which appear as function
4797/// argument types
4798QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
4799 bool OfBlockPointer,
4800 bool Unqualified) {
4801 // GNU extension: two types are compatible if they appear as a function
4802 // argument, one of the types is a transparent union type and the other
4803 // type is compatible with a union member
4804 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
4805 Unqualified);
4806 if (!lmerge.isNull())
4807 return lmerge;
4808
4809 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
4810 Unqualified);
4811 if (!rmerge.isNull())
4812 return rmerge;
4813
4814 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
4815}
4816
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004817QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004818 bool OfBlockPointer,
4819 bool Unqualified) {
John McCall9dd450b2009-09-21 23:43:11 +00004820 const FunctionType *lbase = lhs->getAs<FunctionType>();
4821 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004822 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4823 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004824 bool allLTypes = true;
4825 bool allRTypes = true;
4826
4827 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004828 QualType retType;
4829 if (OfBlockPointer)
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004830 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true,
4831 Unqualified);
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004832 else
John McCall8c6b56f2010-12-15 01:06:38 +00004833 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
4834 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00004835 if (retType.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004836
4837 if (Unqualified)
4838 retType = retType.getUnqualifiedType();
4839
4840 CanQualType LRetType = getCanonicalType(lbase->getResultType());
4841 CanQualType RRetType = getCanonicalType(rbase->getResultType());
4842 if (Unqualified) {
4843 LRetType = LRetType.getUnqualifiedType();
4844 RRetType = RRetType.getUnqualifiedType();
4845 }
4846
4847 if (getCanonicalType(retType) != LRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00004848 allLTypes = false;
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004849 if (getCanonicalType(retType) != RRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00004850 allRTypes = false;
John McCall8c6b56f2010-12-15 01:06:38 +00004851
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004852 // FIXME: double check this
4853 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4854 // rbase->getRegParmAttr() != 0 &&
4855 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004856 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4857 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
John McCall8c6b56f2010-12-15 01:06:38 +00004858
Douglas Gregor8c940862010-01-18 17:14:39 +00004859 // Compatible functions must have compatible calling conventions
John McCall8c6b56f2010-12-15 01:06:38 +00004860 if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
Douglas Gregor8c940862010-01-18 17:14:39 +00004861 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004862
John McCall8c6b56f2010-12-15 01:06:38 +00004863 // Regparm is part of the calling convention.
4864 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
4865 return QualType();
4866
4867 // It's noreturn if either type is.
4868 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
4869 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4870 if (NoReturn != lbaseInfo.getNoReturn())
4871 allLTypes = false;
4872 if (NoReturn != rbaseInfo.getNoReturn())
4873 allRTypes = false;
4874
4875 FunctionType::ExtInfo einfo(NoReturn,
4876 lbaseInfo.getRegParm(),
4877 lbaseInfo.getCC());
John McCalldb40c7f2010-12-14 08:05:40 +00004878
Eli Friedman47f77112008-08-22 00:56:42 +00004879 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004880 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4881 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004882 unsigned lproto_nargs = lproto->getNumArgs();
4883 unsigned rproto_nargs = rproto->getNumArgs();
4884
4885 // Compatible functions must have the same number of arguments
4886 if (lproto_nargs != rproto_nargs)
4887 return QualType();
4888
4889 // Variadic and non-variadic functions aren't compatible
4890 if (lproto->isVariadic() != rproto->isVariadic())
4891 return QualType();
4892
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004893 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4894 return QualType();
4895
Eli Friedman47f77112008-08-22 00:56:42 +00004896 // Check argument compatibility
4897 llvm::SmallVector<QualType, 10> types;
4898 for (unsigned i = 0; i < lproto_nargs; i++) {
4899 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4900 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004901 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
4902 OfBlockPointer,
4903 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00004904 if (argtype.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004905
4906 if (Unqualified)
4907 argtype = argtype.getUnqualifiedType();
4908
Eli Friedman47f77112008-08-22 00:56:42 +00004909 types.push_back(argtype);
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004910 if (Unqualified) {
4911 largtype = largtype.getUnqualifiedType();
4912 rargtype = rargtype.getUnqualifiedType();
4913 }
4914
Chris Lattner465fa322008-10-05 17:34:18 +00004915 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4916 allLTypes = false;
4917 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4918 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004919 }
4920 if (allLTypes) return lhs;
4921 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00004922
4923 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
4924 EPI.ExtInfo = einfo;
4925 return getFunctionType(retType, types.begin(), types.size(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00004926 }
4927
4928 if (lproto) allRTypes = false;
4929 if (rproto) allLTypes = false;
4930
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004931 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004932 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004933 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004934 if (proto->isVariadic()) return QualType();
4935 // Check that the types are compatible with the types that
4936 // would result from default argument promotions (C99 6.7.5.3p15).
4937 // The only types actually affected are promotable integer
4938 // types and floats, which would be passed as a different
4939 // type depending on whether the prototype is visible.
4940 unsigned proto_nargs = proto->getNumArgs();
4941 for (unsigned i = 0; i < proto_nargs; ++i) {
4942 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004943
4944 // Look at the promotion type of enum types, since that is the type used
4945 // to pass enum values.
4946 if (const EnumType *Enum = argTy->getAs<EnumType>())
4947 argTy = Enum->getDecl()->getPromotionType();
4948
Eli Friedman47f77112008-08-22 00:56:42 +00004949 if (argTy->isPromotableIntegerType() ||
4950 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4951 return QualType();
4952 }
4953
4954 if (allLTypes) return lhs;
4955 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00004956
4957 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
4958 EPI.ExtInfo = einfo;
Eli Friedman47f77112008-08-22 00:56:42 +00004959 return getFunctionType(retType, proto->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00004960 proto->getNumArgs(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00004961 }
4962
4963 if (allLTypes) return lhs;
4964 if (allRTypes) return rhs;
John McCall8c6b56f2010-12-15 01:06:38 +00004965 return getFunctionNoProtoType(retType, einfo);
Eli Friedman47f77112008-08-22 00:56:42 +00004966}
4967
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004968QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004969 bool OfBlockPointer,
4970 bool Unqualified) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004971 // C++ [expr]: If an expression initially has the type "reference to T", the
4972 // type is adjusted to "T" prior to any further analysis, the expression
4973 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004974 // expression is an lvalue unless the reference is an rvalue reference and
4975 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004976 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4977 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004978
4979 if (Unqualified) {
4980 LHS = LHS.getUnqualifiedType();
4981 RHS = RHS.getUnqualifiedType();
4982 }
Douglas Gregor21e771e2010-02-03 21:02:30 +00004983
Eli Friedman47f77112008-08-22 00:56:42 +00004984 QualType LHSCan = getCanonicalType(LHS),
4985 RHSCan = getCanonicalType(RHS);
4986
4987 // If two types are identical, they are compatible.
4988 if (LHSCan == RHSCan)
4989 return LHS;
4990
John McCall8ccfcb52009-09-24 19:53:00 +00004991 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004992 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4993 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004994 if (LQuals != RQuals) {
4995 // If any of these qualifiers are different, we have a type
4996 // mismatch.
4997 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4998 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4999 return QualType();
5000
5001 // Exactly one GC qualifier difference is allowed: __strong is
5002 // okay if the other type has no GC qualifier but is an Objective
5003 // C object pointer (i.e. implicitly strong by default). We fix
5004 // this by pretending that the unqualified type was actually
5005 // qualified __strong.
5006 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5007 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5008 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5009
5010 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5011 return QualType();
5012
5013 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
5014 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
5015 }
5016 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
5017 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
5018 }
Eli Friedman47f77112008-08-22 00:56:42 +00005019 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00005020 }
5021
5022 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00005023
Eli Friedmandcca6332009-06-01 01:22:52 +00005024 Type::TypeClass LHSClass = LHSCan->getTypeClass();
5025 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00005026
Chris Lattnerfd652912008-01-14 05:45:46 +00005027 // We want to consider the two function types to be the same for these
5028 // comparisons, just force one to the other.
5029 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5030 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00005031
5032 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00005033 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5034 LHSClass = Type::ConstantArray;
5035 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5036 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00005037
John McCall8b07ec22010-05-15 11:32:37 +00005038 // ObjCInterfaces are just specialized ObjCObjects.
5039 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5040 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5041
Nate Begemance4d7fc2008-04-18 23:10:10 +00005042 // Canonicalize ExtVector -> Vector.
5043 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5044 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00005045
Chris Lattner95554662008-04-07 05:43:21 +00005046 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005047 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00005048 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00005049 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00005050 // Compatibility is based on the underlying type, not the promotion
5051 // type.
John McCall9dd450b2009-09-21 23:43:11 +00005052 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005053 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5054 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005055 }
John McCall9dd450b2009-09-21 23:43:11 +00005056 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005057 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5058 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005059 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005060
Eli Friedman47f77112008-08-22 00:56:42 +00005061 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005062 }
Eli Friedman47f77112008-08-22 00:56:42 +00005063
Steve Naroffc6edcbd2008-01-09 22:43:08 +00005064 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005065 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005066#define TYPE(Class, Base)
5067#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00005068#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005069#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5070#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5071#include "clang/AST/TypeNodes.def"
5072 assert(false && "Non-canonical and dependent types shouldn't get here");
5073 return QualType();
5074
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005075 case Type::LValueReference:
5076 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005077 case Type::MemberPointer:
5078 assert(false && "C++ should never be in mergeTypes");
5079 return QualType();
5080
John McCall8b07ec22010-05-15 11:32:37 +00005081 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005082 case Type::IncompleteArray:
5083 case Type::VariableArray:
5084 case Type::FunctionProto:
5085 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005086 assert(false && "Types are eliminated above");
5087 return QualType();
5088
Chris Lattnerfd652912008-01-14 05:45:46 +00005089 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00005090 {
5091 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005092 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5093 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005094 if (Unqualified) {
5095 LHSPointee = LHSPointee.getUnqualifiedType();
5096 RHSPointee = RHSPointee.getUnqualifiedType();
5097 }
5098 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5099 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005100 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00005101 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005102 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00005103 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005104 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005105 return getPointerType(ResultType);
5106 }
Steve Naroff68e167d2008-12-10 17:49:55 +00005107 case Type::BlockPointer:
5108 {
5109 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005110 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5111 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005112 if (Unqualified) {
5113 LHSPointee = LHSPointee.getUnqualifiedType();
5114 RHSPointee = RHSPointee.getUnqualifiedType();
5115 }
5116 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5117 Unqualified);
Steve Naroff68e167d2008-12-10 17:49:55 +00005118 if (ResultType.isNull()) return QualType();
5119 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5120 return LHS;
5121 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5122 return RHS;
5123 return getBlockPointerType(ResultType);
5124 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005125 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00005126 {
5127 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5128 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5129 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5130 return QualType();
5131
5132 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5133 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005134 if (Unqualified) {
5135 LHSElem = LHSElem.getUnqualifiedType();
5136 RHSElem = RHSElem.getUnqualifiedType();
5137 }
5138
5139 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005140 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00005141 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5142 return LHS;
5143 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5144 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00005145 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5146 ArrayType::ArraySizeModifier(), 0);
5147 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5148 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005149 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5150 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00005151 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5152 return LHS;
5153 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5154 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005155 if (LVAT) {
5156 // FIXME: This isn't correct! But tricky to implement because
5157 // the array's size has to be the size of LHS, but the type
5158 // has to be different.
5159 return LHS;
5160 }
5161 if (RVAT) {
5162 // FIXME: This isn't correct! But tricky to implement because
5163 // the array's size has to be the size of RHS, but the type
5164 // has to be different.
5165 return RHS;
5166 }
Eli Friedman3e62c212008-08-22 01:48:21 +00005167 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5168 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00005169 return getIncompleteArrayType(ResultType,
5170 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005171 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005172 case Type::FunctionNoProto:
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005173 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005174 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005175 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00005176 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00005177 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005178 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00005179 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00005180 case Type::Complex:
5181 // Distinct complex types are incompatible.
5182 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005183 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00005184 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00005185 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5186 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00005187 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00005188 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00005189 case Type::ObjCObject: {
5190 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00005191 // FIXME: This should be type compatibility, e.g. whether
5192 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00005193 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5194 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5195 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00005196 return LHS;
5197
Eli Friedman47f77112008-08-22 00:56:42 +00005198 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00005199 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00005200 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005201 if (OfBlockPointer) {
5202 if (canAssignObjCInterfacesInBlockPointer(
5203 LHS->getAs<ObjCObjectPointerType>(),
5204 RHS->getAs<ObjCObjectPointerType>()))
5205 return LHS;
5206 return QualType();
5207 }
John McCall9dd450b2009-09-21 23:43:11 +00005208 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5209 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00005210 return LHS;
5211
Steve Naroffc68cfcf2008-12-10 22:14:21 +00005212 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005213 }
Steve Naroff32e44c02007-10-15 20:41:53 +00005214 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005215
5216 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005217}
Ted Kremenekfc581a92007-10-31 17:10:13 +00005218
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005219/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5220/// 'RHS' attributes and returns the merged version; including for function
5221/// return types.
5222QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5223 QualType LHSCan = getCanonicalType(LHS),
5224 RHSCan = getCanonicalType(RHS);
5225 // If two types are identical, they are compatible.
5226 if (LHSCan == RHSCan)
5227 return LHS;
5228 if (RHSCan->isFunctionType()) {
5229 if (!LHSCan->isFunctionType())
5230 return QualType();
5231 QualType OldReturnType =
5232 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5233 QualType NewReturnType =
5234 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5235 QualType ResReturnType =
5236 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5237 if (ResReturnType.isNull())
5238 return QualType();
5239 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5240 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5241 // In either case, use OldReturnType to build the new function type.
5242 const FunctionType *F = LHS->getAs<FunctionType>();
5243 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
John McCalldb40c7f2010-12-14 08:05:40 +00005244 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5245 EPI.ExtInfo = getFunctionExtInfo(LHS);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005246 QualType ResultType
5247 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005248 FPT->getNumArgs(), EPI);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005249 return ResultType;
5250 }
5251 }
5252 return QualType();
5253 }
5254
5255 // If the qualifiers are different, the types can still be merged.
5256 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5257 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5258 if (LQuals != RQuals) {
5259 // If any of these qualifiers are different, we have a type mismatch.
5260 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5261 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5262 return QualType();
5263
5264 // Exactly one GC qualifier difference is allowed: __strong is
5265 // okay if the other type has no GC qualifier but is an Objective
5266 // C object pointer (i.e. implicitly strong by default). We fix
5267 // this by pretending that the unqualified type was actually
5268 // qualified __strong.
5269 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5270 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5271 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5272
5273 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5274 return QualType();
5275
5276 if (GC_L == Qualifiers::Strong)
5277 return LHS;
5278 if (GC_R == Qualifiers::Strong)
5279 return RHS;
5280 return QualType();
5281 }
5282
5283 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5284 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5285 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5286 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5287 if (ResQT == LHSBaseQT)
5288 return LHS;
5289 if (ResQT == RHSBaseQT)
5290 return RHS;
5291 }
5292 return QualType();
5293}
5294
Chris Lattner4ba0cef2008-04-07 07:01:58 +00005295//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005296// Integer Predicates
5297//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00005298
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005299unsigned ASTContext::getIntWidth(QualType T) {
John McCall56774992009-12-09 09:09:27 +00005300 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00005301 T = ET->getDecl()->getIntegerType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00005302 if (T->isBooleanType())
5303 return 1;
Eli Friedman1efaaea2009-02-13 02:31:07 +00005304 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005305 return (unsigned)getTypeSize(T);
5306}
5307
5308QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00005309 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00005310
5311 // Turn <4 x signed int> -> <4 x unsigned int>
5312 if (const VectorType *VTy = T->getAs<VectorType>())
5313 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsonaeb56442010-11-10 21:56:12 +00005314 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattnerec3a1562009-10-17 20:33:28 +00005315
5316 // For enums, we return the unsigned version of the base type.
5317 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005318 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00005319
5320 const BuiltinType *BTy = T->getAs<BuiltinType>();
5321 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005322 switch (BTy->getKind()) {
5323 case BuiltinType::Char_S:
5324 case BuiltinType::SChar:
5325 return UnsignedCharTy;
5326 case BuiltinType::Short:
5327 return UnsignedShortTy;
5328 case BuiltinType::Int:
5329 return UnsignedIntTy;
5330 case BuiltinType::Long:
5331 return UnsignedLongTy;
5332 case BuiltinType::LongLong:
5333 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00005334 case BuiltinType::Int128:
5335 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005336 default:
5337 assert(0 && "Unexpected signed integer type");
5338 return QualType();
5339 }
5340}
5341
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005342ExternalASTSource::~ExternalASTSource() { }
5343
5344void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00005345
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005346ASTMutationListener::~ASTMutationListener() { }
5347
Chris Lattnerecd79c62009-06-14 00:45:47 +00005348
5349//===----------------------------------------------------------------------===//
5350// Builtin Type Computation
5351//===----------------------------------------------------------------------===//
5352
5353/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattnerdc226c22010-10-01 22:42:38 +00005354/// pointer over the consumed characters. This returns the resultant type. If
5355/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5356/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5357/// a vector of "i*".
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005358///
5359/// RequiresICE is filled in on return to indicate whether the value is required
5360/// to be an Integer Constant Expression.
Mike Stump11289f42009-09-09 15:08:12 +00005361static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00005362 ASTContext::GetBuiltinTypeError &Error,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005363 bool &RequiresICE,
Chris Lattnerdc226c22010-10-01 22:42:38 +00005364 bool AllowTypeModifiers) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005365 // Modifiers.
5366 int HowLong = 0;
5367 bool Signed = false, Unsigned = false;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005368 RequiresICE = false;
Chris Lattner84733392010-10-01 07:13:18 +00005369
Chris Lattnerdc226c22010-10-01 22:42:38 +00005370 // Read the prefixed modifiers first.
Chris Lattnerecd79c62009-06-14 00:45:47 +00005371 bool Done = false;
5372 while (!Done) {
5373 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00005374 default: Done = true; --Str; break;
Chris Lattner84733392010-10-01 07:13:18 +00005375 case 'I':
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005376 RequiresICE = true;
Chris Lattner84733392010-10-01 07:13:18 +00005377 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005378 case 'S':
5379 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5380 assert(!Signed && "Can't use 'S' modifier multiple times!");
5381 Signed = true;
5382 break;
5383 case 'U':
5384 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5385 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5386 Unsigned = true;
5387 break;
5388 case 'L':
5389 assert(HowLong <= 2 && "Can't have LLLL modifier");
5390 ++HowLong;
5391 break;
5392 }
5393 }
5394
5395 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005396
Chris Lattnerecd79c62009-06-14 00:45:47 +00005397 // Read the base type.
5398 switch (*Str++) {
5399 default: assert(0 && "Unknown builtin type letter!");
5400 case 'v':
5401 assert(HowLong == 0 && !Signed && !Unsigned &&
5402 "Bad modifiers used with 'v'!");
5403 Type = Context.VoidTy;
5404 break;
5405 case 'f':
5406 assert(HowLong == 0 && !Signed && !Unsigned &&
5407 "Bad modifiers used with 'f'!");
5408 Type = Context.FloatTy;
5409 break;
5410 case 'd':
5411 assert(HowLong < 2 && !Signed && !Unsigned &&
5412 "Bad modifiers used with 'd'!");
5413 if (HowLong)
5414 Type = Context.LongDoubleTy;
5415 else
5416 Type = Context.DoubleTy;
5417 break;
5418 case 's':
5419 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5420 if (Unsigned)
5421 Type = Context.UnsignedShortTy;
5422 else
5423 Type = Context.ShortTy;
5424 break;
5425 case 'i':
5426 if (HowLong == 3)
5427 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5428 else if (HowLong == 2)
5429 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5430 else if (HowLong == 1)
5431 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5432 else
5433 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5434 break;
5435 case 'c':
5436 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5437 if (Signed)
5438 Type = Context.SignedCharTy;
5439 else if (Unsigned)
5440 Type = Context.UnsignedCharTy;
5441 else
5442 Type = Context.CharTy;
5443 break;
5444 case 'b': // boolean
5445 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5446 Type = Context.BoolTy;
5447 break;
5448 case 'z': // size_t.
5449 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5450 Type = Context.getSizeType();
5451 break;
5452 case 'F':
5453 Type = Context.getCFConstantStringType();
5454 break;
Fariborz Jahaniand11da7e2010-11-09 21:38:20 +00005455 case 'G':
5456 Type = Context.getObjCIdType();
5457 break;
5458 case 'H':
5459 Type = Context.getObjCSelType();
5460 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005461 case 'a':
5462 Type = Context.getBuiltinVaListType();
5463 assert(!Type.isNull() && "builtin va list type not initialized!");
5464 break;
5465 case 'A':
5466 // This is a "reference" to a va_list; however, what exactly
5467 // this means depends on how va_list is defined. There are two
5468 // different kinds of va_list: ones passed by value, and ones
5469 // passed by reference. An example of a by-value va_list is
5470 // x86, where va_list is a char*. An example of by-ref va_list
5471 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5472 // we want this argument to be a char*&; for x86-64, we want
5473 // it to be a __va_list_tag*.
5474 Type = Context.getBuiltinVaListType();
5475 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005476 if (Type->isArrayType())
Chris Lattnerecd79c62009-06-14 00:45:47 +00005477 Type = Context.getArrayDecayedType(Type);
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005478 else
Chris Lattnerecd79c62009-06-14 00:45:47 +00005479 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005480 break;
5481 case 'V': {
5482 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005483 unsigned NumElements = strtoul(Str, &End, 10);
5484 assert(End != Str && "Missing vector size");
Chris Lattnerecd79c62009-06-14 00:45:47 +00005485 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005486
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005487 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5488 RequiresICE, false);
5489 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattnerdc226c22010-10-01 22:42:38 +00005490
5491 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner37141f42010-06-23 06:00:24 +00005492 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00005493 VectorType::GenericVector);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005494 break;
5495 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005496 case 'X': {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005497 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5498 false);
5499 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005500 Type = Context.getComplexType(ElementType);
5501 break;
5502 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005503 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005504 Type = Context.getFILEType();
5505 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005506 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005507 return QualType();
5508 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005509 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005510 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005511 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005512 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005513 else
5514 Type = Context.getjmp_bufType();
5515
Mike Stump2adb4da2009-07-28 23:47:15 +00005516 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005517 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005518 return QualType();
5519 }
5520 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005521 }
Mike Stump11289f42009-09-09 15:08:12 +00005522
Chris Lattnerdc226c22010-10-01 22:42:38 +00005523 // If there are modifiers and if we're allowed to parse them, go for it.
5524 Done = !AllowTypeModifiers;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005525 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005526 switch (char c = *Str++) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005527 default: Done = true; --Str; break;
5528 case '*':
5529 case '&': {
5530 // Both pointers and references can have their pointee types
5531 // qualified with an address space.
5532 char *End;
5533 unsigned AddrSpace = strtoul(Str, &End, 10);
5534 if (End != Str && AddrSpace != 0) {
5535 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5536 Str = End;
5537 }
5538 if (c == '*')
5539 Type = Context.getPointerType(Type);
5540 else
5541 Type = Context.getLValueReferenceType(Type);
5542 break;
5543 }
5544 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5545 case 'C':
5546 Type = Type.withConst();
5547 break;
5548 case 'D':
5549 Type = Context.getVolatileType(Type);
5550 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005551 }
5552 }
Chris Lattner84733392010-10-01 07:13:18 +00005553
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005554 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner84733392010-10-01 07:13:18 +00005555 "Integer constant 'I' type must be an integer");
Mike Stump11289f42009-09-09 15:08:12 +00005556
Chris Lattnerecd79c62009-06-14 00:45:47 +00005557 return Type;
5558}
5559
5560/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattnerdc226c22010-10-01 22:42:38 +00005561QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005562 GetBuiltinTypeError &Error,
5563 unsigned *IntegerConstantArgs) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005564 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump11289f42009-09-09 15:08:12 +00005565
Chris Lattnerecd79c62009-06-14 00:45:47 +00005566 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005567
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005568 bool RequiresICE = false;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005569 Error = GE_None;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005570 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5571 RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005572 if (Error != GE_None)
5573 return QualType();
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005574
5575 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5576
Chris Lattnerecd79c62009-06-14 00:45:47 +00005577 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005578 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005579 if (Error != GE_None)
5580 return QualType();
5581
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005582 // If this argument is required to be an IntegerConstantExpression and the
5583 // caller cares, fill in the bitmask we return.
5584 if (RequiresICE && IntegerConstantArgs)
5585 *IntegerConstantArgs |= 1 << ArgTypes.size();
5586
Chris Lattnerecd79c62009-06-14 00:45:47 +00005587 // Do array -> pointer decay. The builtin should use the decayed type.
5588 if (Ty->isArrayType())
5589 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005590
Chris Lattnerecd79c62009-06-14 00:45:47 +00005591 ArgTypes.push_back(Ty);
5592 }
5593
5594 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5595 "'.' should only occur at end of builtin type list!");
5596
John McCall991eb4b2010-12-21 00:44:39 +00005597 FunctionType::ExtInfo EI;
5598 if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
5599
5600 bool Variadic = (TypeStr[0] == '.');
5601
5602 // We really shouldn't be making a no-proto type here, especially in C++.
5603 if (ArgTypes.empty() && Variadic)
5604 return getFunctionNoProtoType(ResType, EI);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005605
John McCalldb40c7f2010-12-14 08:05:40 +00005606 FunctionProtoType::ExtProtoInfo EPI;
John McCall991eb4b2010-12-21 00:44:39 +00005607 EPI.ExtInfo = EI;
5608 EPI.Variadic = Variadic;
John McCalldb40c7f2010-12-14 08:05:40 +00005609
5610 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005611}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005612
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005613GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5614 GVALinkage External = GVA_StrongExternal;
5615
5616 Linkage L = FD->getLinkage();
5617 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5618 FD->getType()->getLinkage() == UniqueExternalLinkage)
5619 L = UniqueExternalLinkage;
5620
5621 switch (L) {
5622 case NoLinkage:
5623 case InternalLinkage:
5624 case UniqueExternalLinkage:
5625 return GVA_Internal;
5626
5627 case ExternalLinkage:
5628 switch (FD->getTemplateSpecializationKind()) {
5629 case TSK_Undeclared:
5630 case TSK_ExplicitSpecialization:
5631 External = GVA_StrongExternal;
5632 break;
5633
5634 case TSK_ExplicitInstantiationDefinition:
5635 return GVA_ExplicitTemplateInstantiation;
5636
5637 case TSK_ExplicitInstantiationDeclaration:
5638 case TSK_ImplicitInstantiation:
5639 External = GVA_TemplateInstantiation;
5640 break;
5641 }
5642 }
5643
5644 if (!FD->isInlined())
5645 return External;
5646
5647 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5648 // GNU or C99 inline semantics. Determine whether this symbol should be
5649 // externally visible.
5650 if (FD->isInlineDefinitionExternallyVisible())
5651 return External;
5652
5653 // C99 inline semantics, where the symbol is not externally visible.
5654 return GVA_C99Inline;
5655 }
5656
5657 // C++0x [temp.explicit]p9:
5658 // [ Note: The intent is that an inline function that is the subject of
5659 // an explicit instantiation declaration will still be implicitly
5660 // instantiated when used so that the body can be considered for
5661 // inlining, but that no out-of-line copy of the inline function would be
5662 // generated in the translation unit. -- end note ]
5663 if (FD->getTemplateSpecializationKind()
5664 == TSK_ExplicitInstantiationDeclaration)
5665 return GVA_C99Inline;
5666
5667 return GVA_CXXInline;
5668}
5669
5670GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
5671 // If this is a static data member, compute the kind of template
5672 // specialization. Otherwise, this variable is not part of a
5673 // template.
5674 TemplateSpecializationKind TSK = TSK_Undeclared;
5675 if (VD->isStaticDataMember())
5676 TSK = VD->getTemplateSpecializationKind();
5677
5678 Linkage L = VD->getLinkage();
5679 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5680 VD->getType()->getLinkage() == UniqueExternalLinkage)
5681 L = UniqueExternalLinkage;
5682
5683 switch (L) {
5684 case NoLinkage:
5685 case InternalLinkage:
5686 case UniqueExternalLinkage:
5687 return GVA_Internal;
5688
5689 case ExternalLinkage:
5690 switch (TSK) {
5691 case TSK_Undeclared:
5692 case TSK_ExplicitSpecialization:
5693 return GVA_StrongExternal;
5694
5695 case TSK_ExplicitInstantiationDeclaration:
5696 llvm_unreachable("Variable should not be instantiated");
5697 // Fall through to treat this like any other instantiation.
5698
5699 case TSK_ExplicitInstantiationDefinition:
5700 return GVA_ExplicitTemplateInstantiation;
5701
5702 case TSK_ImplicitInstantiation:
5703 return GVA_TemplateInstantiation;
5704 }
5705 }
5706
5707 return GVA_StrongExternal;
5708}
5709
Argyrios Kyrtzidisc9049332010-07-29 20:08:05 +00005710bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005711 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
5712 if (!VD->isFileVarDecl())
5713 return false;
5714 } else if (!isa<FunctionDecl>(D))
5715 return false;
5716
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00005717 // Weak references don't produce any output by themselves.
5718 if (D->hasAttr<WeakRefAttr>())
5719 return false;
5720
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005721 // Aliases and used decls are required.
5722 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
5723 return true;
5724
5725 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5726 // Forward declarations aren't required.
5727 if (!FD->isThisDeclarationADefinition())
5728 return false;
5729
5730 // Constructors and destructors are required.
5731 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
5732 return true;
5733
5734 // The key function for a class is required.
5735 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5736 const CXXRecordDecl *RD = MD->getParent();
5737 if (MD->isOutOfLine() && RD->isDynamicClass()) {
5738 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
5739 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
5740 return true;
5741 }
5742 }
5743
5744 GVALinkage Linkage = GetGVALinkageForFunction(FD);
5745
5746 // static, static inline, always_inline, and extern inline functions can
5747 // always be deferred. Normal inline functions can be deferred in C99/C++.
5748 // Implicit template instantiations can also be deferred in C++.
5749 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
5750 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
5751 return false;
5752 return true;
5753 }
5754
5755 const VarDecl *VD = cast<VarDecl>(D);
5756 assert(VD->isFileVarDecl() && "Expected file scoped var");
5757
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00005758 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
5759 return false;
5760
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005761 // Structs that have non-trivial constructors or destructors are required.
5762
5763 // FIXME: Handle references.
5764 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
5765 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +00005766 if (RD->hasDefinition() &&
5767 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005768 return true;
5769 }
5770 }
5771
5772 GVALinkage L = GetGVALinkageForVariable(VD);
5773 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
5774 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
5775 return false;
5776 }
5777
5778 return true;
5779}
Charles Davis53c59df2010-08-16 03:33:14 +00005780
Charles Davis99202b32010-11-09 18:04:24 +00005781CallingConv ASTContext::getDefaultMethodCallConv() {
5782 // Pass through to the C++ ABI object
5783 return ABI->getDefaultMethodCallConv();
5784}
5785
Anders Carlsson60a62632010-11-25 01:51:53 +00005786bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) {
5787 // Pass through to the C++ ABI object
5788 return ABI->isNearlyEmpty(RD);
5789}
5790
Charles Davis53c59df2010-08-16 03:33:14 +00005791CXXABI::~CXXABI() {}