blob: b4520609ff78521c512f600ade974b7f1cbca4c6 [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());
Douglas Gregorf5500772011-01-05 15:48:55 +000054 ID.AddBoolean(Parm->isParameterPack());
Douglas Gregor7dbfb462010-06-16 21:09:37 +000055
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);
Douglas Gregorf5500772011-01-05 15:48:55 +000069 ID.AddBoolean(NTTP->isParameterPack());
Douglas Gregor7dbfb462010-06-16 21:09:37 +000070 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(),
Douglas Gregorf5500772011-01-05 15:48:55 +0000122 TTP->getPosition(),
123 TTP->isParameterPack(),
124 0,
Douglas Gregor7dbfb462010-06-16 21:09:37 +0000125 TemplateParameterList::Create(*this, SourceLocation(),
126 SourceLocation(),
127 CanonParams.data(),
128 CanonParams.size(),
129 SourceLocation()));
130
131 // Get the new insert position for the node we care about.
132 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
133 assert(Canonical == 0 && "Shouldn't be in the map!");
134 (void)Canonical;
135
136 // Create the canonical template template parameter entry.
137 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
138 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
139 return CanonTTP;
140}
141
Charles Davis53c59df2010-08-16 03:33:14 +0000142CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
John McCall86353412010-08-21 22:46:04 +0000143 if (!LangOpts.CPlusPlus) return 0;
144
Charles Davis6bcb07a2010-08-19 02:18:14 +0000145 switch (T.getCXXABI()) {
John McCall86353412010-08-21 22:46:04 +0000146 case CXXABI_ARM:
147 return CreateARMCXXABI(*this);
148 case CXXABI_Itanium:
Charles Davis53c59df2010-08-16 03:33:14 +0000149 return CreateItaniumCXXABI(*this);
Charles Davis6bcb07a2010-08-19 02:18:14 +0000150 case CXXABI_Microsoft:
151 return CreateMicrosoftCXXABI(*this);
152 }
John McCall86353412010-08-21 22:46:04 +0000153 return 0;
Charles Davis53c59df2010-08-16 03:33:14 +0000154}
155
Chris Lattner465fa322008-10-05 17:34:18 +0000156ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar1b444192009-11-13 05:51:54 +0000157 const TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +0000158 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +0000159 Builtin::Context &builtins,
Douglas Gregor5b11d492010-07-25 17:53:33 +0000160 unsigned size_reserve) :
John McCall773cc982010-06-11 11:07:21 +0000161 TemplateSpecializationTypes(this_()),
162 DependentTemplateSpecializationTypes(this_()),
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +0000163 GlobalNestedNameSpecifier(0), IsInt128Installed(false),
164 CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +0000165 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +0000166 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
John McCall8cb7bdf2010-06-04 23:28:52 +0000167 NullTypeSourceInfo(QualType()),
Charles Davis53c59df2010-08-16 03:33:14 +0000168 SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000169 Idents(idents), Selectors(sels),
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000170 BuiltinInfo(builtins),
171 DeclarationNames(*this),
Argyrios Kyrtzidis440ea322010-10-28 09:29:35 +0000172 ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
Ted Kremenek520f47b2010-06-18 00:31:04 +0000173 LastSDM(0, 0),
174 UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) {
David Chisnall9f57c292009-08-17 16:35:33 +0000175 ObjCIdRedefinitionType = QualType();
176 ObjCClassRedefinitionType = QualType();
Douglas Gregor5b11d492010-07-25 17:53:33 +0000177 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000178 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +0000179 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000180 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +0000181}
182
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000183ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000184 // Release the DenseMaps associated with DeclContext objects.
185 // FIXME: Is this the ideal solution?
186 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +0000187
Douglas Gregor5b11d492010-07-25 17:53:33 +0000188 // Call all of the deallocation functions.
189 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
190 Deallocations[I].first(Deallocations[I].second);
Douglas Gregor1a809332010-05-23 18:26:36 +0000191
Douglas Gregor832940b2010-03-02 23:58:15 +0000192 // Release all of the memory associated with overridden C++ methods.
193 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
194 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
195 OM != OMEnd; ++OM)
196 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000197
Ted Kremenek076baeb2010-06-08 23:00:58 +0000198 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
Douglas Gregor5b11d492010-07-25 17:53:33 +0000199 // because they can contain DenseMaps.
200 for (llvm::DenseMap<const ObjCContainerDecl*,
201 const ASTRecordLayout*>::iterator
202 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
203 // Increment in loop to prevent using deallocated memory.
204 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
205 R->Destroy(*this);
206
Ted Kremenek076baeb2010-06-08 23:00:58 +0000207 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
208 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
209 // Increment in loop to prevent using deallocated memory.
210 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
211 R->Destroy(*this);
212 }
Douglas Gregor561eceb2010-08-30 16:49:28 +0000213
214 for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
215 AEnd = DeclAttrs.end();
216 A != AEnd; ++A)
217 A->second->~AttrVec();
218}
Douglas Gregorf21eb492009-03-26 23:50:42 +0000219
Douglas Gregor1a809332010-05-23 18:26:36 +0000220void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
221 Deallocations.push_back(std::make_pair(Callback, Data));
222}
223
Mike Stump11289f42009-09-09 15:08:12 +0000224void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
226 ExternalSource.reset(Source.take());
227}
228
Chris Lattner4eb445d2007-01-26 01:27:23 +0000229void ASTContext::PrintStats() const {
230 fprintf(stderr, "*** AST Context Stats:\n");
231 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000232
Douglas Gregora30d0462009-05-26 14:40:08 +0000233 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000234#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000235#define ABSTRACT_TYPE(Name, Parent)
236#include "clang/AST/TypeNodes.def"
237 0 // Extra
238 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000239
Chris Lattner4eb445d2007-01-26 01:27:23 +0000240 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
241 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000242 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000243 }
244
Douglas Gregora30d0462009-05-26 14:40:08 +0000245 unsigned Idx = 0;
246 unsigned TotalBytes = 0;
247#define TYPE(Name, Parent) \
248 if (counts[Idx]) \
249 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
250 TotalBytes += counts[Idx] * sizeof(Name##Type); \
251 ++Idx;
252#define ABSTRACT_TYPE(Name, Parent)
253#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000254
Douglas Gregora30d0462009-05-26 14:40:08 +0000255 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor747eb782010-07-08 06:14:04 +0000256
Douglas Gregor7454c562010-07-02 20:37:36 +0000257 // Implicit special member functions.
Douglas Gregor9672f922010-07-03 00:47:00 +0000258 fprintf(stderr, " %u/%u implicit default constructors created\n",
259 NumImplicitDefaultConstructorsDeclared,
260 NumImplicitDefaultConstructors);
Douglas Gregora6d69502010-07-02 23:41:54 +0000261 fprintf(stderr, " %u/%u implicit copy constructors created\n",
262 NumImplicitCopyConstructorsDeclared,
263 NumImplicitCopyConstructors);
Douglas Gregor330b9cf2010-07-02 21:50:04 +0000264 fprintf(stderr, " %u/%u implicit copy assignment operators created\n",
265 NumImplicitCopyAssignmentOperatorsDeclared,
266 NumImplicitCopyAssignmentOperators);
Douglas Gregor7454c562010-07-02 20:37:36 +0000267 fprintf(stderr, " %u/%u implicit destructors created\n",
268 NumImplicitDestructorsDeclared, NumImplicitDestructors);
269
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000270 if (ExternalSource.get()) {
271 fprintf(stderr, "\n");
272 ExternalSource->PrintStats();
273 }
Douglas Gregor747eb782010-07-08 06:14:04 +0000274
Douglas Gregor5b11d492010-07-25 17:53:33 +0000275 BumpAlloc.PrintStats();
Chris Lattner4eb445d2007-01-26 01:27:23 +0000276}
277
278
John McCall48f2d582009-10-23 23:03:21 +0000279void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000280 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000281 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000282 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000283}
284
Chris Lattner970e54e2006-11-12 00:37:36 +0000285void ASTContext::InitBuiltinTypes() {
286 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000287
Chris Lattner970e54e2006-11-12 00:37:36 +0000288 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000289 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000290
Chris Lattner970e54e2006-11-12 00:37:36 +0000291 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000292 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000293 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000294 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000295 InitBuiltinType(CharTy, BuiltinType::Char_S);
296 else
297 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000298 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000299 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
300 InitBuiltinType(ShortTy, BuiltinType::Short);
301 InitBuiltinType(IntTy, BuiltinType::Int);
302 InitBuiltinType(LongTy, BuiltinType::Long);
303 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattner970e54e2006-11-12 00:37:36 +0000305 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000306 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
307 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
308 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
309 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
310 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000311
Chris Lattner970e54e2006-11-12 00:37:36 +0000312 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000313 InitBuiltinType(FloatTy, BuiltinType::Float);
314 InitBuiltinType(DoubleTy, BuiltinType::Double);
315 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000316
Chris Lattnerf122cef2009-04-30 02:43:43 +0000317 // GNU extension, 128-bit integers.
318 InitBuiltinType(Int128Ty, BuiltinType::Int128);
319 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
320
Chris Lattnerad3467e2010-12-25 23:25:43 +0000321 if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
322 if (!LangOpts.ShortWChar)
323 InitBuiltinType(WCharTy, BuiltinType::WChar_S);
324 else // -fshort-wchar makes wchar_t be unsigned.
325 InitBuiltinType(WCharTy, BuiltinType::WChar_U);
326 } else // C99
Chris Lattner007cb022009-02-26 23:43:47 +0000327 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000328
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000329 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
330 InitBuiltinType(Char16Ty, BuiltinType::Char16);
331 else // C99
332 Char16Ty = getFromTargetType(Target.getChar16Type());
333
334 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
335 InitBuiltinType(Char32Ty, BuiltinType::Char32);
336 else // C99
337 Char32Ty = getFromTargetType(Target.getChar32Type());
338
Douglas Gregor4619e432008-12-05 23:32:09 +0000339 // Placeholder type for type-dependent expressions whose type is
340 // completely unknown. No code should ever check a type against
341 // DependentTy and users should never see it; however, it is here to
342 // help diagnose failures to properly check for type-dependent
343 // expressions.
344 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000345
John McCall36e7fe32010-10-12 00:20:44 +0000346 // Placeholder type for functions.
347 InitBuiltinType(OverloadTy, BuiltinType::Overload);
348
Mike Stump11289f42009-09-09 15:08:12 +0000349 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000350 // not yet been deduced.
John McCall36e7fe32010-10-12 00:20:44 +0000351 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner970e54e2006-11-12 00:37:36 +0000353 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000354 FloatComplexTy = getComplexType(FloatTy);
355 DoubleComplexTy = getComplexType(DoubleTy);
356 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000357
Steve Naroff66e9f332007-10-15 14:41:52 +0000358 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000359
Steve Naroff1329fa02009-07-15 18:40:39 +0000360 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
361 ObjCIdTypedefType = QualType();
362 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000363 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000364
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000365 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000366 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
367 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000368 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000369
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000370 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000371
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000372 // void * type
373 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000374
375 // nullptr type (C++0x 2.14.7)
376 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000377}
378
Argyrios Kyrtzidisca0d0cd2010-09-22 14:32:24 +0000379Diagnostic &ASTContext::getDiagnostics() const {
380 return SourceMgr.getDiagnostics();
381}
382
Douglas Gregor561eceb2010-08-30 16:49:28 +0000383AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
384 AttrVec *&Result = DeclAttrs[D];
385 if (!Result) {
386 void *Mem = Allocate(sizeof(AttrVec));
387 Result = new (Mem) AttrVec;
388 }
389
390 return *Result;
391}
392
393/// \brief Erase the attributes corresponding to the given declaration.
394void ASTContext::eraseDeclAttrs(const Decl *D) {
395 llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
396 if (Pos != DeclAttrs.end()) {
397 Pos->second->~AttrVec();
398 DeclAttrs.erase(Pos);
399 }
400}
401
402
Douglas Gregor86d142a2009-10-08 07:24:58 +0000403MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000404ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000405 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000406 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000407 = InstantiatedFromStaticDataMember.find(Var);
408 if (Pos == InstantiatedFromStaticDataMember.end())
409 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000410
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000411 return Pos->second;
412}
413
Mike Stump11289f42009-09-09 15:08:12 +0000414void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000415ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000416 TemplateSpecializationKind TSK,
417 SourceLocation PointOfInstantiation) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000418 assert(Inst->isStaticDataMember() && "Not a static data member");
419 assert(Tmpl->isStaticDataMember() && "Not a static data member");
420 assert(!InstantiatedFromStaticDataMember[Inst] &&
421 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000422 InstantiatedFromStaticDataMember[Inst]
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000423 = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000424}
425
John McCalle61f2ba2009-11-18 02:36:19 +0000426NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000427ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000428 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000429 = InstantiatedFromUsingDecl.find(UUD);
430 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000431 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000432
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000433 return Pos->second;
434}
435
436void
John McCallb96ec562009-12-04 22:46:56 +0000437ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
438 assert((isa<UsingDecl>(Pattern) ||
439 isa<UnresolvedUsingValueDecl>(Pattern) ||
440 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
441 "pattern decl is not a using decl");
442 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
443 InstantiatedFromUsingDecl[Inst] = Pattern;
444}
445
446UsingShadowDecl *
447ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
448 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
449 = InstantiatedFromUsingShadowDecl.find(Inst);
450 if (Pos == InstantiatedFromUsingShadowDecl.end())
451 return 0;
452
453 return Pos->second;
454}
455
456void
457ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
458 UsingShadowDecl *Pattern) {
459 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
460 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000461}
462
Anders Carlsson5da84842009-09-01 04:26:58 +0000463FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
464 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
465 = InstantiatedFromUnnamedFieldDecl.find(Field);
466 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
467 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000468
Anders Carlsson5da84842009-09-01 04:26:58 +0000469 return Pos->second;
470}
471
472void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
473 FieldDecl *Tmpl) {
474 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
475 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
476 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
477 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000478
Anders Carlsson5da84842009-09-01 04:26:58 +0000479 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
480}
481
Douglas Gregor832940b2010-03-02 23:58:15 +0000482ASTContext::overridden_cxx_method_iterator
483ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
484 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
485 = OverriddenMethods.find(Method);
486 if (Pos == OverriddenMethods.end())
487 return 0;
488
489 return Pos->second.begin();
490}
491
492ASTContext::overridden_cxx_method_iterator
493ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
494 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
495 = OverriddenMethods.find(Method);
496 if (Pos == OverriddenMethods.end())
497 return 0;
498
499 return Pos->second.end();
500}
501
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000502unsigned
503ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
504 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
505 = OverriddenMethods.find(Method);
506 if (Pos == OverriddenMethods.end())
507 return 0;
508
509 return Pos->second.size();
510}
511
Douglas Gregor832940b2010-03-02 23:58:15 +0000512void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
513 const CXXMethodDecl *Overridden) {
514 OverriddenMethods[Method].push_back(Overridden);
515}
516
Chris Lattner53cfe802007-07-18 17:52:12 +0000517//===----------------------------------------------------------------------===//
518// Type Sizing and Analysis
519//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000520
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000521/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
522/// scalar floating point type.
523const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000524 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000525 assert(BT && "Not a floating point type!");
526 switch (BT->getKind()) {
527 default: assert(0 && "Not a floating point type!");
528 case BuiltinType::Float: return Target.getFloatFormat();
529 case BuiltinType::Double: return Target.getDoubleFormat();
530 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
531 }
532}
533
Ken Dyck160146e2010-01-27 17:10:57 +0000534/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000535/// specified decl. Note that bitfields do not have a valid alignment, so
536/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000537/// If @p RefAsPointee, references are treated like their underlying type
538/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000539CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000540 unsigned Align = Target.getCharWidth();
541
John McCall94268702010-10-08 18:24:19 +0000542 bool UseAlignAttrOnly = false;
543 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
544 Align = AlignFromAttr;
Eli Friedman19a546c2009-02-22 02:56:25 +0000545
John McCall94268702010-10-08 18:24:19 +0000546 // __attribute__((aligned)) can increase or decrease alignment
547 // *except* on a struct or struct member, where it only increases
548 // alignment unless 'packed' is also specified.
549 //
550 // It is an error for [[align]] to decrease alignment, so we can
551 // ignore that possibility; Sema should diagnose it.
552 if (isa<FieldDecl>(D)) {
553 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
554 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
555 } else {
556 UseAlignAttrOnly = true;
557 }
558 }
559
560 if (UseAlignAttrOnly) {
561 // ignore type of value
562 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner68061312009-01-24 21:53:27 +0000563 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000564 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000565 if (RefAsPointee)
566 T = RT->getPointeeType();
567 else
568 T = getPointerType(RT->getPointeeType());
569 }
570 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindolae971b9a2010-06-04 23:15:27 +0000571 unsigned MinWidth = Target.getLargeArrayMinWidth();
572 unsigned ArrayAlign = Target.getLargeArrayAlign();
573 if (isa<VariableArrayType>(T) && MinWidth != 0)
574 Align = std::max(Align, ArrayAlign);
575 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
576 unsigned Size = getTypeSize(CT);
577 if (MinWidth != 0 && MinWidth <= Size)
578 Align = std::max(Align, ArrayAlign);
579 }
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000580 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000581 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
582 T = cast<ArrayType>(T)->getElementType();
583
584 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
585 }
Charles Davis3fc51072010-02-23 04:52:00 +0000586 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
587 // In the case of a field in a packed struct, we want the minimum
588 // of the alignment of the field and the alignment of the struct.
589 Align = std::min(Align,
590 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
591 }
Chris Lattner68061312009-01-24 21:53:27 +0000592 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000593
Ken Dyck160146e2010-01-27 17:10:57 +0000594 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000595}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000596
John McCall87fe5d52010-05-20 01:18:31 +0000597std::pair<CharUnits, CharUnits>
598ASTContext::getTypeInfoInChars(const Type *T) {
599 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
600 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
601 CharUnits::fromQuantity(Info.second / getCharWidth()));
602}
603
604std::pair<CharUnits, CharUnits>
605ASTContext::getTypeInfoInChars(QualType T) {
606 return getTypeInfoInChars(T.getTypePtr());
607}
608
Chris Lattner983a8bb2007-07-13 22:13:22 +0000609/// getTypeSize - Return the size of the specified type, in bits. This method
610/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000611///
612/// FIXME: Pointers into different addr spaces could have different sizes and
613/// alignment requirements: getPointerInfo should take an AddrSpace, this
614/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000615std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000616ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000617 uint64_t Width=0;
618 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000619 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000620#define TYPE(Class, Base)
621#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000622#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000623#define DEPENDENT_TYPE(Class, Base) case Type::Class:
624#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000625 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000626 break;
627
Chris Lattner355332d2007-07-13 22:27:08 +0000628 case Type::FunctionNoProto:
629 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000630 // GCC extension: alignof(function) = 32 bits
631 Width = 0;
632 Align = 32;
633 break;
634
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000635 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000636 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000637 Width = 0;
638 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
639 break;
640
Steve Naroff5c131802007-08-30 01:06:46 +0000641 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000642 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000643
Chris Lattner37e05872008-03-05 18:54:05 +0000644 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000645 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000646 Align = EltInfo.second;
647 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000648 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000649 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000650 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000651 const VectorType *VT = cast<VectorType>(T);
652 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
653 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000654 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000655 // If the alignment is not a power of 2, round up to the next power of 2.
656 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000657 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000658 Align = llvm::NextPowerOf2(Align);
659 Width = llvm::RoundUpToAlignment(Width, Align);
660 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000661 break;
662 }
Chris Lattner647fb222007-07-18 18:26:58 +0000663
Chris Lattner7570e9c2008-03-08 08:52:55 +0000664 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000665 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000666 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000667 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000668 // GCC extension: alignof(void) = 8 bits.
669 Width = 0;
670 Align = 8;
671 break;
672
Chris Lattner6a4f7452007-12-19 19:23:28 +0000673 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000674 Width = Target.getBoolWidth();
675 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000676 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000677 case BuiltinType::Char_S:
678 case BuiltinType::Char_U:
679 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000680 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000681 Width = Target.getCharWidth();
682 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000683 break;
Chris Lattnerad3467e2010-12-25 23:25:43 +0000684 case BuiltinType::WChar_S:
685 case BuiltinType::WChar_U:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000686 Width = Target.getWCharWidth();
687 Align = Target.getWCharAlign();
688 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000689 case BuiltinType::Char16:
690 Width = Target.getChar16Width();
691 Align = Target.getChar16Align();
692 break;
693 case BuiltinType::Char32:
694 Width = Target.getChar32Width();
695 Align = Target.getChar32Align();
696 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000697 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000698 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000699 Width = Target.getShortWidth();
700 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000701 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000702 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000703 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000704 Width = Target.getIntWidth();
705 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000706 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000707 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000708 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000709 Width = Target.getLongWidth();
710 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000711 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000712 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000713 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000714 Width = Target.getLongLongWidth();
715 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000716 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000717 case BuiltinType::Int128:
718 case BuiltinType::UInt128:
719 Width = 128;
720 Align = 128; // int128_t is 128-bit aligned on all targets.
721 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000722 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000723 Width = Target.getFloatWidth();
724 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000725 break;
726 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000727 Width = Target.getDoubleWidth();
728 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000729 break;
730 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000731 Width = Target.getLongDoubleWidth();
732 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000733 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000734 case BuiltinType::NullPtr:
735 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
736 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000737 break;
Fariborz Jahanian9c6a39e2010-08-02 18:03:20 +0000738 case BuiltinType::ObjCId:
739 case BuiltinType::ObjCClass:
740 case BuiltinType::ObjCSel:
741 Width = Target.getPointerWidth(0);
742 Align = Target.getPointerAlign(0);
743 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000744 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000745 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000746 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000747 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000748 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000749 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000750 case Type::BlockPointer: {
751 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
752 Width = Target.getPointerWidth(AS);
753 Align = Target.getPointerAlign(AS);
754 break;
755 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000756 case Type::LValueReference:
757 case Type::RValueReference: {
758 // alignof and sizeof should never enter this code path here, so we go
759 // the pointer route.
760 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
761 Width = Target.getPointerWidth(AS);
762 Align = Target.getPointerAlign(AS);
763 break;
764 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000765 case Type::Pointer: {
766 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000767 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000768 Align = Target.getPointerAlign(AS);
769 break;
770 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000771 case Type::MemberPointer: {
Charles Davis53c59df2010-08-16 03:33:14 +0000772 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000773 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000774 getTypeInfo(getPointerDiffType());
Charles Davis53c59df2010-08-16 03:33:14 +0000775 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson32440a02009-05-17 02:06:04 +0000776 Align = PtrDiffInfo.second;
777 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000778 }
Chris Lattner647fb222007-07-18 18:26:58 +0000779 case Type::Complex: {
780 // Complex types have the same alignment as their elements, but twice the
781 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000782 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000783 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000784 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000785 Align = EltInfo.second;
786 break;
787 }
John McCall8b07ec22010-05-15 11:32:37 +0000788 case Type::ObjCObject:
789 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Pateldbb72632008-06-04 21:54:36 +0000790 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000791 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000792 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
793 Width = Layout.getSize();
794 Align = Layout.getAlignment();
795 break;
796 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000797 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000798 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000799 const TagType *TT = cast<TagType>(T);
800
801 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000802 Width = 1;
803 Align = 1;
804 break;
805 }
Mike Stump11289f42009-09-09 15:08:12 +0000806
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000807 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000808 return getTypeInfo(ET->getDecl()->getIntegerType());
809
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000810 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000811 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
812 Width = Layout.getSize();
813 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000814 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000815 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000816
Chris Lattner63d2b362009-10-22 05:17:15 +0000817 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000818 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
819 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000820
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000821 case Type::Paren:
822 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
823
Douglas Gregoref462e62009-04-30 17:32:17 +0000824 case Type::Typedef: {
825 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregorf5bae222010-08-27 00:11:28 +0000826 std::pair<uint64_t, unsigned> Info
827 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
828 Align = std::max(Typedef->getMaxAlignment(), Info.second);
829 Width = Info.first;
Douglas Gregordc572a32009-03-30 22:58:21 +0000830 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000831 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000832
833 case Type::TypeOfExpr:
834 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
835 .getTypePtr());
836
837 case Type::TypeOf:
838 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
839
Anders Carlsson81df7b82009-06-24 19:06:50 +0000840 case Type::Decltype:
841 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
842 .getTypePtr());
843
Abramo Bagnara6150c882010-05-11 21:36:43 +0000844 case Type::Elaborated:
845 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000846
Douglas Gregoref462e62009-04-30 17:32:17 +0000847 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000848 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000849 "Cannot request the size of a dependent type");
850 // FIXME: this is likely to be wrong once we support template
851 // aliases, since a template alias could refer to a typedef that
852 // has an __aligned__ attribute on it.
853 return getTypeInfo(getCanonicalType(T));
854 }
Mike Stump11289f42009-09-09 15:08:12 +0000855
Chris Lattner53cfe802007-07-18 17:52:12 +0000856 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000857 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000858}
859
Ken Dyck8c89d592009-12-22 14:23:30 +0000860/// getTypeSizeInChars - Return the size of the specified type, in characters.
861/// This method does not work on incomplete types.
862CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000863 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000864}
865CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000866 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000867}
868
Ken Dycka6046ab2010-01-26 17:25:18 +0000869/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000870/// characters. This method does not work on incomplete types.
871CharUnits ASTContext::getTypeAlignInChars(QualType T) {
872 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
873}
874CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
875 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
876}
877
Chris Lattnera3402cd2009-01-27 18:08:34 +0000878/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
879/// type for the current target in bits. This can be different than the ABI
880/// alignment in cases where it is beneficial for performance to overalign
881/// a data type.
882unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
883 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000884
885 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000886 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000887 T = CT->getElementType().getTypePtr();
888 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
889 T->isSpecificBuiltinType(BuiltinType::LongLong))
890 return std::max(ABIAlign, (unsigned)getTypeSize(T));
891
Chris Lattnera3402cd2009-01-27 18:08:34 +0000892 return ABIAlign;
893}
894
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000895/// ShallowCollectObjCIvars -
896/// Collect all ivars, including those synthesized, in the current class.
897///
898void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000899 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000900 // FIXME. This need be removed but there are two many places which
901 // assume const-ness of ObjCInterfaceDecl
902 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
903 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
904 Iv= Iv->getNextIvar())
905 Ivars.push_back(Iv);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000906}
907
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000908/// DeepCollectObjCIvars -
909/// This routine first collects all declared, but not synthesized, ivars in
910/// super class and then collects all ivars, including those synthesized for
911/// current class. This routine is used for implementation of current class
912/// when all ivars, declared and synthesized are known.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000913///
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000914void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
915 bool leafClass,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000916 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000917 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
918 DeepCollectObjCIvars(SuperClass, false, Ivars);
919 if (!leafClass) {
920 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
921 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000922 Ivars.push_back(*I);
923 }
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +0000924 else
925 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000926}
927
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000928/// CollectInheritedProtocols - Collect all protocols in current class and
929/// those inherited by it.
930void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000931 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000932 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000933 // We can use protocol_iterator here instead of
934 // all_referenced_protocol_iterator since we are walking all categories.
935 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
936 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000937 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000938 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000939 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000940 PE = Proto->protocol_end(); P != PE; ++P) {
941 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000942 CollectInheritedProtocols(*P, Protocols);
943 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000944 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000945
946 // Categories of this Interface.
947 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
948 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
949 CollectInheritedProtocols(CDeclChain, Protocols);
950 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
951 while (SD) {
952 CollectInheritedProtocols(SD, Protocols);
953 SD = SD->getSuperClass();
954 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000955 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000956 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000957 PE = OC->protocol_end(); P != PE; ++P) {
958 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000959 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000960 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
961 PE = Proto->protocol_end(); P != PE; ++P)
962 CollectInheritedProtocols(*P, Protocols);
963 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000964 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000965 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
966 PE = OP->protocol_end(); P != PE; ++P) {
967 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000968 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000969 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
970 PE = Proto->protocol_end(); P != PE; ++P)
971 CollectInheritedProtocols(*P, Protocols);
972 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000973 }
974}
975
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000976unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
977 unsigned count = 0;
978 // Count ivars declared in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000979 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
980 CDecl = CDecl->getNextClassExtension())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000981 count += CDecl->ivar_size();
982
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000983 // Count ivar defined in this class's implementation. This
984 // includes synthesized ivars.
985 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000986 count += ImplDecl->ivar_size();
987
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000988 return count;
989}
990
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000991/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
992ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
993 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
994 I = ObjCImpls.find(D);
995 if (I != ObjCImpls.end())
996 return cast<ObjCImplementationDecl>(I->second);
997 return 0;
998}
999/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1000ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
1001 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1002 I = ObjCImpls.find(D);
1003 if (I != ObjCImpls.end())
1004 return cast<ObjCCategoryImplDecl>(I->second);
1005 return 0;
1006}
1007
1008/// \brief Set the implementation of ObjCInterfaceDecl.
1009void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1010 ObjCImplementationDecl *ImplD) {
1011 assert(IFaceD && ImplD && "Passed null params");
1012 ObjCImpls[IFaceD] = ImplD;
1013}
1014/// \brief Set the implementation of ObjCCategoryDecl.
1015void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1016 ObjCCategoryImplDecl *ImplD) {
1017 assert(CatD && ImplD && "Passed null params");
1018 ObjCImpls[CatD] = ImplD;
1019}
1020
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001021/// \brief Get the copy initialization expression of VarDecl,or NULL if
1022/// none exists.
Fariborz Jahanian50198092010-12-02 17:02:11 +00001023Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001024 assert(VD && "Passed null params");
1025 assert(VD->hasAttr<BlocksAttr>() &&
1026 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian50198092010-12-02 17:02:11 +00001027 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001028 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001029 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1030}
1031
1032/// \brief Set the copy inialization expression of a block var decl.
1033void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1034 assert(VD && Init && "Passed null params");
Fariborz Jahanian1321cbb2010-12-06 17:28:17 +00001035 assert(VD->hasAttr<BlocksAttr>() &&
1036 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian7cfe7672010-12-01 22:29:46 +00001037 BlockVarCopyInits[VD] = Init;
1038}
1039
John McCallbcd03502009-12-07 02:54:59 +00001040/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001041///
John McCallbcd03502009-12-07 02:54:59 +00001042/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001043/// the TypeLoc wrappers.
1044///
1045/// \param T the type that will be the basis for type source info. This type
1046/// should refer to how the declarator was written in source code, not to
1047/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +00001048TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +00001049 unsigned DataSize) {
1050 if (!DataSize)
1051 DataSize = TypeLoc::getFullDataSizeForType(T);
1052 else
1053 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +00001054 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +00001055
John McCallbcd03502009-12-07 02:54:59 +00001056 TypeSourceInfo *TInfo =
1057 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1058 new (TInfo) TypeSourceInfo(T);
1059 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +00001060}
1061
John McCallbcd03502009-12-07 02:54:59 +00001062TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +00001063 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +00001064 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +00001065 DI->getTypeLoc().initialize(L);
1066 return DI;
1067}
1068
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001069const ASTRecordLayout &
1070ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1071 return getObjCLayout(D, 0);
1072}
1073
1074const ASTRecordLayout &
1075ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1076 return getObjCLayout(D->getClassInterface(), D);
1077}
1078
Chris Lattner983a8bb2007-07-13 22:13:22 +00001079//===----------------------------------------------------------------------===//
1080// Type creation/memoization methods
1081//===----------------------------------------------------------------------===//
1082
John McCall8ccfcb52009-09-24 19:53:00 +00001083QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1084 unsigned Fast = Quals.getFastQualifiers();
1085 Quals.removeFastQualifiers();
1086
1087 // Check if we've already instantiated this type.
1088 llvm::FoldingSetNodeID ID;
1089 ExtQuals::Profile(ID, TypeNode, Quals);
1090 void *InsertPos = 0;
1091 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1092 assert(EQ->getQualifiers() == Quals);
1093 QualType T = QualType(EQ, Fast);
1094 return T;
1095 }
1096
John McCall717d9b02010-12-10 11:01:00 +00001097 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001098 ExtQualNodes.InsertNode(New, InsertPos);
1099 QualType T = QualType(New, Fast);
1100 return T;
1101}
1102
Fariborz Jahanianece85822009-02-17 18:27:45 +00001103QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001104 QualType CanT = getCanonicalType(T);
1105 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001106 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001107
John McCall8ccfcb52009-09-24 19:53:00 +00001108 // If we are composing extended qualifiers together, merge together
1109 // into one ExtQuals node.
1110 QualifierCollector Quals;
1111 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001112
John McCall8ccfcb52009-09-24 19:53:00 +00001113 // If this type already has an address space specified, it cannot get
1114 // another one.
1115 assert(!Quals.hasAddressSpace() &&
1116 "Type cannot be in multiple addr spaces!");
1117 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001118
John McCall8ccfcb52009-09-24 19:53:00 +00001119 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001120}
1121
Chris Lattnerd60183d2009-02-18 22:53:11 +00001122QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001123 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001124 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001125 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001126 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001127
John McCall53fa7142010-12-24 02:08:15 +00001128 if (const PointerType *ptr = T->getAs<PointerType>()) {
1129 QualType Pointee = ptr->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001130 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001131 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1132 return getPointerType(ResultType);
1133 }
1134 }
Mike Stump11289f42009-09-09 15:08:12 +00001135
John McCall8ccfcb52009-09-24 19:53:00 +00001136 // If we are composing extended qualifiers together, merge together
1137 // into one ExtQuals node.
1138 QualifierCollector Quals;
1139 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001140
John McCall8ccfcb52009-09-24 19:53:00 +00001141 // If this type already has an ObjCGC specified, it cannot get
1142 // another one.
1143 assert(!Quals.hasObjCGCAttr() &&
1144 "Type cannot have multiple ObjCGCs!");
1145 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001146
John McCall8ccfcb52009-09-24 19:53:00 +00001147 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001148}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001149
John McCall4f5019e2010-12-19 02:44:49 +00001150const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1151 FunctionType::ExtInfo Info) {
1152 if (T->getExtInfo() == Info)
1153 return T;
1154
1155 QualType Result;
1156 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1157 Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1158 } else {
1159 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1160 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1161 EPI.ExtInfo = Info;
1162 Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1163 FPT->getNumArgs(), EPI);
1164 }
1165
1166 return cast<FunctionType>(Result.getTypePtr());
1167}
1168
Chris Lattnerc6395932007-06-22 20:56:16 +00001169/// getComplexType - Return the uniqued reference to the type for a complex
1170/// number with the specified element type.
1171QualType ASTContext::getComplexType(QualType T) {
1172 // Unique pointers, to guarantee there is only one pointer of a particular
1173 // structure.
1174 llvm::FoldingSetNodeID ID;
1175 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001176
Chris Lattnerc6395932007-06-22 20:56:16 +00001177 void *InsertPos = 0;
1178 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1179 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001180
Chris Lattnerc6395932007-06-22 20:56:16 +00001181 // If the pointee type isn't canonical, this won't be a canonical type either,
1182 // so fill in the canonical type field.
1183 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001184 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001185 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001186
Chris Lattnerc6395932007-06-22 20:56:16 +00001187 // Get the new insert position for the node we care about.
1188 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001189 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001190 }
John McCall90d1c2d2009-09-24 23:30:46 +00001191 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001192 Types.push_back(New);
1193 ComplexTypes.InsertNode(New, InsertPos);
1194 return QualType(New, 0);
1195}
1196
Chris Lattner970e54e2006-11-12 00:37:36 +00001197/// getPointerType - Return the uniqued reference to the type for a pointer to
1198/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001199QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001200 // Unique pointers, to guarantee there is only one pointer of a particular
1201 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001202 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001203 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001204
Chris Lattner67521df2007-01-27 01:29:36 +00001205 void *InsertPos = 0;
1206 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001207 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattner7ccecb92006-11-12 08:50:50 +00001209 // If the pointee type isn't canonical, this won't be a canonical type either,
1210 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001211 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001212 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001213 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001214
Chris Lattner67521df2007-01-27 01:29:36 +00001215 // Get the new insert position for the node we care about.
1216 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001217 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001218 }
John McCall90d1c2d2009-09-24 23:30:46 +00001219 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001220 Types.push_back(New);
1221 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001222 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001223}
1224
Mike Stump11289f42009-09-09 15:08:12 +00001225/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001226/// a pointer to the specified block.
1227QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001228 assert(T->isFunctionType() && "block of function types only");
1229 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001230 // structure.
1231 llvm::FoldingSetNodeID ID;
1232 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001233
Steve Naroffec33ed92008-08-27 16:04:49 +00001234 void *InsertPos = 0;
1235 if (BlockPointerType *PT =
1236 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1237 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001238
1239 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001240 // type either so fill in the canonical type field.
1241 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001242 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001243 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001244
Steve Naroffec33ed92008-08-27 16:04:49 +00001245 // Get the new insert position for the node we care about.
1246 BlockPointerType *NewIP =
1247 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001248 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001249 }
John McCall90d1c2d2009-09-24 23:30:46 +00001250 BlockPointerType *New
1251 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001252 Types.push_back(New);
1253 BlockPointerTypes.InsertNode(New, InsertPos);
1254 return QualType(New, 0);
1255}
1256
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001257/// getLValueReferenceType - Return the uniqued reference to the type for an
1258/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001259QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001260 // Unique pointers, to guarantee there is only one pointer of a particular
1261 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001262 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001263 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001264
1265 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001266 if (LValueReferenceType *RT =
1267 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001268 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001269
John McCallfc93cf92009-10-22 22:37:11 +00001270 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1271
Bill Wendling3708c182007-05-27 10:15:43 +00001272 // If the referencee type isn't canonical, this won't be a canonical type
1273 // either, so fill in the canonical type field.
1274 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001275 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1276 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1277 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001278
Bill Wendling3708c182007-05-27 10:15:43 +00001279 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001280 LValueReferenceType *NewIP =
1281 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001282 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001283 }
1284
John McCall90d1c2d2009-09-24 23:30:46 +00001285 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001286 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1287 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001288 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001289 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001290
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001291 return QualType(New, 0);
1292}
1293
1294/// getRValueReferenceType - Return the uniqued reference to the type for an
1295/// rvalue reference to the specified type.
1296QualType ASTContext::getRValueReferenceType(QualType T) {
1297 // Unique pointers, to guarantee there is only one pointer of a particular
1298 // structure.
1299 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001300 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001301
1302 void *InsertPos = 0;
1303 if (RValueReferenceType *RT =
1304 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1305 return QualType(RT, 0);
1306
John McCallfc93cf92009-10-22 22:37:11 +00001307 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1308
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001309 // If the referencee type isn't canonical, this won't be a canonical type
1310 // either, so fill in the canonical type field.
1311 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001312 if (InnerRef || !T.isCanonical()) {
1313 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1314 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001315
1316 // Get the new insert position for the node we care about.
1317 RValueReferenceType *NewIP =
1318 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001319 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001320 }
1321
John McCall90d1c2d2009-09-24 23:30:46 +00001322 RValueReferenceType *New
1323 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001324 Types.push_back(New);
1325 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001326 return QualType(New, 0);
1327}
1328
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001329/// getMemberPointerType - Return the uniqued reference to the type for a
1330/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001331QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001332 // Unique pointers, to guarantee there is only one pointer of a particular
1333 // structure.
1334 llvm::FoldingSetNodeID ID;
1335 MemberPointerType::Profile(ID, T, Cls);
1336
1337 void *InsertPos = 0;
1338 if (MemberPointerType *PT =
1339 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1340 return QualType(PT, 0);
1341
1342 // If the pointee or class type isn't canonical, this won't be a canonical
1343 // type either, so fill in the canonical type field.
1344 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001345 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001346 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1347
1348 // Get the new insert position for the node we care about.
1349 MemberPointerType *NewIP =
1350 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001351 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001352 }
John McCall90d1c2d2009-09-24 23:30:46 +00001353 MemberPointerType *New
1354 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001355 Types.push_back(New);
1356 MemberPointerTypes.InsertNode(New, InsertPos);
1357 return QualType(New, 0);
1358}
1359
Mike Stump11289f42009-09-09 15:08:12 +00001360/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001361/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001362QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001363 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001364 ArrayType::ArraySizeModifier ASM,
1365 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001366 assert((EltTy->isDependentType() ||
1367 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001368 "Constant array of VLAs is illegal!");
1369
Chris Lattnere2df3f92009-05-13 04:12:56 +00001370 // Convert the array size into a canonical width matching the pointer size for
1371 // the target.
1372 llvm::APInt ArySize(ArySizeIn);
Jay Foad6d4db0c2010-12-07 08:25:34 +00001373 ArySize =
1374 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001375
Chris Lattner23b7eb62007-06-15 23:05:46 +00001376 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001377 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattner36f8e652007-01-27 08:31:04 +00001379 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001380 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001381 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001382 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001383
Chris Lattner7ccecb92006-11-12 08:50:50 +00001384 // If the element type isn't canonical, this won't be a canonical type either,
1385 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001386 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001387 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001388 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001389 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001390 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001391 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001392 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001393 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001394 }
Mike Stump11289f42009-09-09 15:08:12 +00001395
John McCall90d1c2d2009-09-24 23:30:46 +00001396 ConstantArrayType *New = new(*this,TypeAlignment)
1397 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001398 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001399 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001400 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001401}
1402
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00001403/// getIncompleteArrayType - Returns a unique reference to the type for a
1404/// incomplete array of the specified element type.
1405QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) {
1406 QualType ElemTy = getBaseElementType(Ty);
1407 DeclarationName Name;
1408 llvm::SmallVector<QualType, 8> ATypes;
1409 QualType ATy = Ty;
1410 while (const ArrayType *AT = getAsArrayType(ATy)) {
1411 ATypes.push_back(ATy);
1412 ATy = AT->getElementType();
1413 }
1414 for (int i = ATypes.size() - 1; i >= 0; i--) {
1415 if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) {
1416 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star,
1417 0, VAT->getBracketsRange());
1418 }
1419 else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) {
1420 llvm::APSInt ConstVal(CAT->getSize());
1421 ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0);
1422 }
1423 else if (getAsIncompleteArrayType(ATypes[i])) {
1424 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal,
1425 0, SourceRange());
1426 }
1427 else
1428 assert(false && "DependentArrayType is seen");
1429 }
1430 return ElemTy;
1431}
1432
1433/// getVariableArrayDecayedType - Returns a vla type where known sizes
1434/// are replaced with [*]
1435QualType ASTContext::getVariableArrayDecayedType(QualType Ty) {
1436 if (Ty->isPointerType()) {
1437 QualType BaseType = Ty->getAs<PointerType>()->getPointeeType();
1438 if (isa<VariableArrayType>(BaseType)) {
1439 ArrayType *AT = dyn_cast<ArrayType>(BaseType);
1440 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1441 if (VAT->getSizeExpr()) {
1442 Ty = getUnknownSizeVariableArrayType(BaseType);
1443 Ty = getPointerType(Ty);
1444 }
1445 }
1446 }
1447 return Ty;
1448}
1449
1450
Steve Naroffcadebd02007-08-30 18:14:25 +00001451/// getVariableArrayType - Returns a non-unique reference to the type for a
1452/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001453QualType ASTContext::getVariableArrayType(QualType EltTy,
1454 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001455 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001456 unsigned EltTypeQuals,
1457 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001458 // Since we don't unique expressions, it isn't possible to unique VLA's
1459 // that have an expression provided for their size.
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001460 QualType CanonType;
1461
1462 if (!EltTy.isCanonical()) {
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001463 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1464 EltTypeQuals, Brackets);
1465 }
1466
John McCall90d1c2d2009-09-24 23:30:46 +00001467 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001468 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001469
1470 VariableArrayTypes.push_back(New);
1471 Types.push_back(New);
1472 return QualType(New, 0);
1473}
1474
Douglas Gregor4619e432008-12-05 23:32:09 +00001475/// getDependentSizedArrayType - Returns a non-unique reference to
1476/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001477/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001478QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1479 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001480 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001481 unsigned EltTypeQuals,
1482 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001483 assert((!NumElts || NumElts->isTypeDependent() ||
1484 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001485 "Size must be type- or value-dependent!");
1486
Douglas Gregorf3f95522009-07-31 00:23:35 +00001487 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001488 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001489 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001490
Douglas Gregorf86c9392010-10-26 00:51:02 +00001491 QualType CanonicalEltTy = getCanonicalType(EltTy);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001492 if (NumElts) {
1493 // Dependently-sized array types that do not have a specified
1494 // number of elements will have their sizes deduced from an
1495 // initializer.
Douglas Gregorf86c9392010-10-26 00:51:02 +00001496 DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM,
Douglas Gregorad2956c2009-11-19 18:03:26 +00001497 EltTypeQuals, NumElts);
1498
1499 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1500 }
1501
Douglas Gregorf3f95522009-07-31 00:23:35 +00001502 DependentSizedArrayType *New;
1503 if (Canon) {
1504 // We already have a canonical version of this array type; use it as
1505 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001506 New = new (*this, TypeAlignment)
1507 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1508 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf86c9392010-10-26 00:51:02 +00001509 } else if (CanonicalEltTy == EltTy) {
1510 // This is a canonical type. Record it.
1511 New = new (*this, TypeAlignment)
1512 DependentSizedArrayType(*this, EltTy, QualType(),
1513 NumElts, ASM, EltTypeQuals, Brackets);
1514
1515 if (NumElts) {
1516#ifndef NDEBUG
1517 DependentSizedArrayType *CanonCheck
1518 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1519 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1520 (void)CanonCheck;
1521#endif
1522 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001523 }
Douglas Gregorf86c9392010-10-26 00:51:02 +00001524 } else {
1525 QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts,
1526 ASM, EltTypeQuals,
1527 SourceRange());
1528 New = new (*this, TypeAlignment)
1529 DependentSizedArrayType(*this, EltTy, Canon,
1530 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001531 }
Mike Stump11289f42009-09-09 15:08:12 +00001532
Douglas Gregor4619e432008-12-05 23:32:09 +00001533 Types.push_back(New);
1534 return QualType(New, 0);
1535}
1536
Eli Friedmanbd258282008-02-15 18:16:39 +00001537QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1538 ArrayType::ArraySizeModifier ASM,
1539 unsigned EltTypeQuals) {
1540 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001541 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001542
1543 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001544 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001545 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1546 return QualType(ATP, 0);
1547
1548 // If the element type isn't canonical, this won't be a canonical type
1549 // either, so fill in the canonical type field.
1550 QualType Canonical;
1551
John McCallb692a092009-10-22 20:10:53 +00001552 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001553 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001554 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001555
1556 // Get the new insert position for the node we care about.
1557 IncompleteArrayType *NewIP =
1558 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001559 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001560 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001561
John McCall90d1c2d2009-09-24 23:30:46 +00001562 IncompleteArrayType *New = new (*this, TypeAlignment)
1563 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001564
1565 IncompleteArrayTypes.InsertNode(New, InsertPos);
1566 Types.push_back(New);
1567 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001568}
1569
Steve Naroff91fcddb2007-07-18 18:00:27 +00001570/// getVectorType - Return the unique reference to a vector type of
1571/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001572QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001573 VectorType::VectorKind VecKind) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00001574 BuiltinType *BaseType;
Mike Stump11289f42009-09-09 15:08:12 +00001575
Chris Lattnerdc226c22010-10-01 22:42:38 +00001576 BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1577 assert(BaseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001578
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001579 // Check if we've already instantiated a vector of this type.
1580 llvm::FoldingSetNodeID ID;
Bob Wilsonaeb56442010-11-10 21:56:12 +00001581 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner37141f42010-06-23 06:00:24 +00001582
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001583 void *InsertPos = 0;
1584 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1585 return QualType(VTP, 0);
1586
1587 // If the element type isn't canonical, this won't be a canonical type either,
1588 // so fill in the canonical type field.
1589 QualType Canonical;
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00001590 if (!vecType.isCanonical()) {
Bob Wilson77954802010-11-16 00:32:20 +00001591 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump11289f42009-09-09 15:08:12 +00001592
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001593 // Get the new insert position for the node we care about.
1594 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001595 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001596 }
John McCall90d1c2d2009-09-24 23:30:46 +00001597 VectorType *New = new (*this, TypeAlignment)
Bob Wilsonaeb56442010-11-10 21:56:12 +00001598 VectorType(vecType, NumElts, Canonical, VecKind);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001599 VectorTypes.InsertNode(New, InsertPos);
1600 Types.push_back(New);
1601 return QualType(New, 0);
1602}
1603
Nate Begemance4d7fc2008-04-18 23:10:10 +00001604/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001605/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001606QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001607 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001608
Chris Lattner76a00cf2008-04-06 22:59:24 +00001609 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001610 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001611
Steve Naroff91fcddb2007-07-18 18:00:27 +00001612 // Check if we've already instantiated a vector of this type.
1613 llvm::FoldingSetNodeID ID;
Chris Lattner37141f42010-06-23 06:00:24 +00001614 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsonaeb56442010-11-10 21:56:12 +00001615 VectorType::GenericVector);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001616 void *InsertPos = 0;
1617 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1618 return QualType(VTP, 0);
1619
1620 // If the element type isn't canonical, this won't be a canonical type either,
1621 // so fill in the canonical type field.
1622 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001623 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001624 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001625
Steve Naroff91fcddb2007-07-18 18:00:27 +00001626 // Get the new insert position for the node we care about.
1627 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001628 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001629 }
John McCall90d1c2d2009-09-24 23:30:46 +00001630 ExtVectorType *New = new (*this, TypeAlignment)
1631 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001632 VectorTypes.InsertNode(New, InsertPos);
1633 Types.push_back(New);
1634 return QualType(New, 0);
1635}
1636
Mike Stump11289f42009-09-09 15:08:12 +00001637QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001638 Expr *SizeExpr,
1639 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001640 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001641 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001642 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001643
Douglas Gregor352169a2009-07-31 03:54:25 +00001644 void *InsertPos = 0;
1645 DependentSizedExtVectorType *Canon
1646 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1647 DependentSizedExtVectorType *New;
1648 if (Canon) {
1649 // We already have a canonical version of this array type; use it as
1650 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001651 New = new (*this, TypeAlignment)
1652 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1653 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001654 } else {
1655 QualType CanonVecTy = getCanonicalType(vecType);
1656 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001657 New = new (*this, TypeAlignment)
1658 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1659 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001660
1661 DependentSizedExtVectorType *CanonCheck
1662 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1663 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1664 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001665 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1666 } else {
1667 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1668 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001669 New = new (*this, TypeAlignment)
1670 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001671 }
1672 }
Mike Stump11289f42009-09-09 15:08:12 +00001673
Douglas Gregor758a8692009-06-17 21:51:59 +00001674 Types.push_back(New);
1675 return QualType(New, 0);
1676}
1677
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001678/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001679///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001680QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1681 const FunctionType::ExtInfo &Info) {
1682 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001683 // Unique functions, to guarantee there is only one function of a particular
1684 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001685 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001686 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001687
Chris Lattner47955de2007-01-27 08:37:20 +00001688 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001689 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001690 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001691 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001692
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001693 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001694 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001695 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001696 Canonical =
1697 getFunctionNoProtoType(getCanonicalType(ResultTy),
1698 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001699
Chris Lattner47955de2007-01-27 08:37:20 +00001700 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001701 FunctionNoProtoType *NewIP =
1702 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001703 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001704 }
Mike Stump11289f42009-09-09 15:08:12 +00001705
John McCall90d1c2d2009-09-24 23:30:46 +00001706 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001707 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001708 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001709 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001710 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001711}
1712
1713/// getFunctionType - Return a normal function type with a typed argument
1714/// list. isVariadic indicates whether the argument list includes '...'.
John McCalldb40c7f2010-12-14 08:05:40 +00001715QualType ASTContext::getFunctionType(QualType ResultTy,
1716 const QualType *ArgArray, unsigned NumArgs,
1717 const FunctionProtoType::ExtProtoInfo &EPI) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001718 // Unique functions, to guarantee there is only one function of a particular
1719 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001720 llvm::FoldingSetNodeID ID;
John McCalldb40c7f2010-12-14 08:05:40 +00001721 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001722
1723 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001724 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001725 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001726 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001727
1728 // Determine whether the type being created is already canonical or not.
John McCalldb40c7f2010-12-14 08:05:40 +00001729 bool isCanonical = !EPI.HasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001730 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001731 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001732 isCanonical = false;
1733
John McCalldb40c7f2010-12-14 08:05:40 +00001734 const CallingConv CallConv = EPI.ExtInfo.getCC();
1735
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001736 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001737 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001738 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001739 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001740 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001741 CanonicalArgs.reserve(NumArgs);
1742 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001743 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001744
John McCalldb40c7f2010-12-14 08:05:40 +00001745 FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
1746 if (CanonicalEPI.HasExceptionSpec) {
1747 CanonicalEPI.HasExceptionSpec = false;
1748 CanonicalEPI.HasAnyExceptionSpec = false;
1749 CanonicalEPI.NumExceptions = 0;
1750 }
1751 CanonicalEPI.ExtInfo
1752 = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
1753
Chris Lattner76a00cf2008-04-06 22:59:24 +00001754 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001755 CanonicalArgs.data(), NumArgs,
John McCalldb40c7f2010-12-14 08:05:40 +00001756 CanonicalEPI);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001757
Chris Lattnerfd4de792007-01-27 01:15:32 +00001758 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001759 FunctionProtoType *NewIP =
1760 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinb3321532010-12-23 01:01:28 +00001761 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001762 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001763
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001764 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001765 // for two variable size arrays (for parameter and exception types) at the
1766 // end of them.
John McCalldb40c7f2010-12-14 08:05:40 +00001767 size_t Size = sizeof(FunctionProtoType) +
1768 NumArgs * sizeof(QualType) +
1769 EPI.NumExceptions * sizeof(QualType);
1770 FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
1771 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, EPI);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001772 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001773 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001774 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001775}
Chris Lattneref51c202006-11-10 07:17:23 +00001776
John McCalle78aac42010-03-10 03:28:59 +00001777#ifndef NDEBUG
1778static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1779 if (!isa<CXXRecordDecl>(D)) return false;
1780 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1781 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1782 return true;
1783 if (RD->getDescribedClassTemplate() &&
1784 !isa<ClassTemplateSpecializationDecl>(RD))
1785 return true;
1786 return false;
1787}
1788#endif
1789
1790/// getInjectedClassNameType - Return the unique reference to the
1791/// injected class name type for the specified templated declaration.
1792QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1793 QualType TST) {
1794 assert(NeedsInjectedClassNameType(Decl));
1795 if (Decl->TypeForDecl) {
1796 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001797 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCalle78aac42010-03-10 03:28:59 +00001798 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1799 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1800 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1801 } else {
John McCall2408e322010-04-27 00:57:59 +00001802 Decl->TypeForDecl =
1803 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001804 Types.push_back(Decl->TypeForDecl);
1805 }
1806 return QualType(Decl->TypeForDecl, 0);
1807}
1808
Douglas Gregor83a586e2008-04-13 21:07:44 +00001809/// getTypeDeclType - Return the unique reference to the type for the
1810/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001811QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001812 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001813 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001814
John McCall81e38502010-02-16 03:57:14 +00001815 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001816 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001817
John McCall96f0b5f2010-03-10 06:48:02 +00001818 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1819 "Template type parameter types are always available.");
1820
John McCall81e38502010-02-16 03:57:14 +00001821 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001822 assert(!Record->getPreviousDeclaration() &&
1823 "struct/union has previous declaration");
1824 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001825 return getRecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001826 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001827 assert(!Enum->getPreviousDeclaration() &&
1828 "enum has previous declaration");
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001829 return getEnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001830 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001831 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1832 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001833 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001834 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001835
John McCall96f0b5f2010-03-10 06:48:02 +00001836 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001837 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001838}
1839
Chris Lattner32d920b2007-01-26 02:01:53 +00001840/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001841/// specified typename decl.
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001842QualType
1843ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001844 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001845
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001846 if (Canonical.isNull())
1847 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001848 Decl->TypeForDecl = new(*this, TypeAlignment)
1849 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001850 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001851 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001852}
1853
Argyrios Kyrtzidisb5fcdc22010-07-04 21:44:47 +00001854QualType ASTContext::getRecordType(const RecordDecl *Decl) {
1855 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1856
1857 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
1858 if (PrevDecl->TypeForDecl)
1859 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1860
1861 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
1862 Types.push_back(Decl->TypeForDecl);
1863 return QualType(Decl->TypeForDecl, 0);
1864}
1865
1866QualType ASTContext::getEnumType(const EnumDecl *Decl) {
1867 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1868
1869 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
1870 if (PrevDecl->TypeForDecl)
1871 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1872
1873 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
1874 Types.push_back(Decl->TypeForDecl);
1875 return QualType(Decl->TypeForDecl, 0);
1876}
1877
John McCallcebee162009-10-18 09:09:24 +00001878/// \brief Retrieve a substitution-result type.
1879QualType
1880ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1881 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001882 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001883 && "replacement types must always be canonical");
1884
1885 llvm::FoldingSetNodeID ID;
1886 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1887 void *InsertPos = 0;
1888 SubstTemplateTypeParmType *SubstParm
1889 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1890
1891 if (!SubstParm) {
1892 SubstParm = new (*this, TypeAlignment)
1893 SubstTemplateTypeParmType(Parm, Replacement);
1894 Types.push_back(SubstParm);
1895 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1896 }
1897
1898 return QualType(SubstParm, 0);
1899}
1900
Douglas Gregoreff93e02009-02-05 23:33:38 +00001901/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001902/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001903/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001904QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001905 bool ParameterPack,
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001906 IdentifierInfo *Name) {
Douglas Gregoreff93e02009-02-05 23:33:38 +00001907 llvm::FoldingSetNodeID ID;
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001908 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001909 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001910 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001911 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1912
1913 if (TypeParm)
1914 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001915
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001916 if (Name) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00001917 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001918 TypeParm = new (*this, TypeAlignment)
1919 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001920
1921 TemplateTypeParmType *TypeCheck
1922 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1923 assert(!TypeCheck && "Template type parameter canonical type broken");
1924 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001925 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001926 TypeParm = new (*this, TypeAlignment)
1927 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001928
1929 Types.push_back(TypeParm);
1930 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1931
1932 return QualType(TypeParm, 0);
1933}
1934
John McCalle78aac42010-03-10 03:28:59 +00001935TypeSourceInfo *
1936ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1937 SourceLocation NameLoc,
1938 const TemplateArgumentListInfo &Args,
1939 QualType CanonType) {
1940 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1941
1942 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1943 TemplateSpecializationTypeLoc TL
1944 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1945 TL.setTemplateNameLoc(NameLoc);
1946 TL.setLAngleLoc(Args.getLAngleLoc());
1947 TL.setRAngleLoc(Args.getRAngleLoc());
1948 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1949 TL.setArgLocInfo(i, Args[i].getLocInfo());
1950 return DI;
1951}
1952
Mike Stump11289f42009-09-09 15:08:12 +00001953QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001954ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001955 const TemplateArgumentListInfo &Args,
John McCall30576cd2010-06-13 09:25:03 +00001956 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00001957 unsigned NumArgs = Args.size();
1958
John McCall0ad16662009-10-29 08:12:44 +00001959 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1960 ArgVec.reserve(NumArgs);
1961 for (unsigned i = 0; i != NumArgs; ++i)
1962 ArgVec.push_back(Args[i].getArgument());
1963
John McCall2408e322010-04-27 00:57:59 +00001964 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00001965 Canon);
John McCall0ad16662009-10-29 08:12:44 +00001966}
1967
1968QualType
1969ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001970 const TemplateArgument *Args,
1971 unsigned NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00001972 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001973 if (!Canon.isNull())
1974 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001975 else
1976 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregord56a91e2009-02-26 22:19:44 +00001977
Douglas Gregora8e02e72009-07-28 23:00:59 +00001978 // Allocate the (non-canonical) template specialization type, but don't
1979 // try to unique it: these types typically have location information that
1980 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001981 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001982 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001983 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001984 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00001985 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00001986 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001987 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001988
Douglas Gregor8bf42052009-02-09 18:46:07 +00001989 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001990 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001991}
1992
Mike Stump11289f42009-09-09 15:08:12 +00001993QualType
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001994ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
1995 const TemplateArgument *Args,
1996 unsigned NumArgs) {
1997 // Build the canonical template specialization type.
1998 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1999 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2000 CanonArgs.reserve(NumArgs);
2001 for (unsigned I = 0; I != NumArgs; ++I)
2002 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2003
2004 // Determine whether this canonical template specialization type already
2005 // exists.
2006 llvm::FoldingSetNodeID ID;
2007 TemplateSpecializationType::Profile(ID, CanonTemplate,
2008 CanonArgs.data(), NumArgs, *this);
2009
2010 void *InsertPos = 0;
2011 TemplateSpecializationType *Spec
2012 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2013
2014 if (!Spec) {
2015 // Allocate a new canonical template specialization type.
2016 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2017 sizeof(TemplateArgument) * NumArgs),
2018 TypeAlignment);
2019 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2020 CanonArgs.data(), NumArgs,
2021 QualType());
2022 Types.push_back(Spec);
2023 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2024 }
2025
2026 assert(Spec->isDependentType() &&
2027 "Non-dependent template-id type must have a canonical type");
2028 return QualType(Spec, 0);
2029}
2030
2031QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00002032ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2033 NestedNameSpecifier *NNS,
2034 QualType NamedType) {
Douglas Gregor52537682009-03-19 00:18:19 +00002035 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002036 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00002037
2038 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00002039 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002040 if (T)
2041 return QualType(T, 0);
2042
Douglas Gregorc42075a2010-02-04 18:10:26 +00002043 QualType Canon = NamedType;
2044 if (!Canon.isCanonical()) {
2045 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002046 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2047 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00002048 (void)CheckT;
2049 }
2050
Abramo Bagnara6150c882010-05-11 21:36:43 +00002051 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00002052 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00002053 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00002054 return QualType(T, 0);
2055}
2056
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002057QualType
2058ASTContext::getParenType(QualType InnerType) {
2059 llvm::FoldingSetNodeID ID;
2060 ParenType::Profile(ID, InnerType);
2061
2062 void *InsertPos = 0;
2063 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2064 if (T)
2065 return QualType(T, 0);
2066
2067 QualType Canon = InnerType;
2068 if (!Canon.isCanonical()) {
2069 Canon = getCanonicalType(InnerType);
2070 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2071 assert(!CheckT && "Paren canonical type broken");
2072 (void)CheckT;
2073 }
2074
2075 T = new (*this) ParenType(InnerType, Canon);
2076 Types.push_back(T);
2077 ParenTypes.InsertNode(T, InsertPos);
2078 return QualType(T, 0);
2079}
2080
Douglas Gregor02085352010-03-31 20:19:30 +00002081QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2082 NestedNameSpecifier *NNS,
2083 const IdentifierInfo *Name,
2084 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00002085 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2086
2087 if (Canon.isNull()) {
2088 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00002089 ElaboratedTypeKeyword CanonKeyword = Keyword;
2090 if (Keyword == ETK_None)
2091 CanonKeyword = ETK_Typename;
2092
2093 if (CanonNNS != NNS || CanonKeyword != Keyword)
2094 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002095 }
2096
2097 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002098 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00002099
2100 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002101 DependentNameType *T
2102 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00002103 if (T)
2104 return QualType(T, 0);
2105
Douglas Gregor02085352010-03-31 20:19:30 +00002106 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002107 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002108 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002109 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002110}
2111
Mike Stump11289f42009-09-09 15:08:12 +00002112QualType
John McCallc392f372010-06-11 00:33:02 +00002113ASTContext::getDependentTemplateSpecializationType(
2114 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002115 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002116 const IdentifierInfo *Name,
2117 const TemplateArgumentListInfo &Args) {
2118 // TODO: avoid this copy
2119 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2120 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2121 ArgCopy.push_back(Args[I].getArgument());
2122 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2123 ArgCopy.size(),
2124 ArgCopy.data());
2125}
2126
2127QualType
2128ASTContext::getDependentTemplateSpecializationType(
2129 ElaboratedTypeKeyword Keyword,
2130 NestedNameSpecifier *NNS,
2131 const IdentifierInfo *Name,
2132 unsigned NumArgs,
2133 const TemplateArgument *Args) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002134 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2135
Douglas Gregorc42075a2010-02-04 18:10:26 +00002136 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002137 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2138 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002139
2140 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002141 DependentTemplateSpecializationType *T
2142 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002143 if (T)
2144 return QualType(T, 0);
2145
John McCallc392f372010-06-11 00:33:02 +00002146 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002147
John McCallc392f372010-06-11 00:33:02 +00002148 ElaboratedTypeKeyword CanonKeyword = Keyword;
2149 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2150
2151 bool AnyNonCanonArgs = false;
2152 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2153 for (unsigned I = 0; I != NumArgs; ++I) {
2154 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2155 if (!CanonArgs[I].structurallyEquals(Args[I]))
2156 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002157 }
2158
John McCallc392f372010-06-11 00:33:02 +00002159 QualType Canon;
2160 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2161 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2162 Name, NumArgs,
2163 CanonArgs.data());
2164
2165 // Find the insert position again.
2166 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2167 }
2168
2169 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2170 sizeof(TemplateArgument) * NumArgs),
2171 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002172 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002173 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002174 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002175 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002176 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002177}
2178
Douglas Gregord2fa7662010-12-20 02:24:11 +00002179QualType ASTContext::getPackExpansionType(QualType Pattern) {
2180 llvm::FoldingSetNodeID ID;
2181 PackExpansionType::Profile(ID, Pattern);
2182
2183 assert(Pattern->containsUnexpandedParameterPack() &&
2184 "Pack expansions must expand one or more parameter packs");
2185 void *InsertPos = 0;
2186 PackExpansionType *T
2187 = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2188 if (T)
2189 return QualType(T, 0);
2190
2191 QualType Canon;
2192 if (!Pattern.isCanonical()) {
2193 Canon = getPackExpansionType(getCanonicalType(Pattern));
2194
2195 // Find the insert position again.
2196 PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2197 }
2198
2199 T = new (*this) PackExpansionType(Pattern, Canon);
2200 Types.push_back(T);
2201 PackExpansionTypes.InsertNode(T, InsertPos);
2202 return QualType(T, 0);
2203}
2204
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002205/// CmpProtocolNames - Comparison predicate for sorting protocols
2206/// alphabetically.
2207static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2208 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002209 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002210}
2211
John McCall8b07ec22010-05-15 11:32:37 +00002212static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002213 unsigned NumProtocols) {
2214 if (NumProtocols == 0) return true;
2215
2216 for (unsigned i = 1; i != NumProtocols; ++i)
2217 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2218 return false;
2219 return true;
2220}
2221
2222static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002223 unsigned &NumProtocols) {
2224 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002225
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002226 // Sort protocols, keyed by name.
2227 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2228
2229 // Remove duplicates.
2230 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2231 NumProtocols = ProtocolsEnd-Protocols;
2232}
2233
John McCall8b07ec22010-05-15 11:32:37 +00002234QualType ASTContext::getObjCObjectType(QualType BaseType,
2235 ObjCProtocolDecl * const *Protocols,
2236 unsigned NumProtocols) {
2237 // If the base type is an interface and there aren't any protocols
2238 // to add, then the interface type will do just fine.
2239 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2240 return BaseType;
2241
2242 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002243 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002244 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002245 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002246 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2247 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002248
John McCall8b07ec22010-05-15 11:32:37 +00002249 // Build the canonical type, which has the canonical base type and
2250 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002251 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002252 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2253 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2254 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002255 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2256 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002257 unsigned UniqueCount = NumProtocols;
2258
John McCallfc93cf92009-10-22 22:37:11 +00002259 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002260 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2261 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002262 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002263 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2264 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002265 }
2266
2267 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002268 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2269 }
2270
2271 unsigned Size = sizeof(ObjCObjectTypeImpl);
2272 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2273 void *Mem = Allocate(Size, TypeAlignment);
2274 ObjCObjectTypeImpl *T =
2275 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2276
2277 Types.push_back(T);
2278 ObjCObjectTypes.InsertNode(T, InsertPos);
2279 return QualType(T, 0);
2280}
2281
2282/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2283/// the given object type.
2284QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2285 llvm::FoldingSetNodeID ID;
2286 ObjCObjectPointerType::Profile(ID, ObjectT);
2287
2288 void *InsertPos = 0;
2289 if (ObjCObjectPointerType *QT =
2290 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2291 return QualType(QT, 0);
2292
2293 // Find the canonical object type.
2294 QualType Canonical;
2295 if (!ObjectT.isCanonical()) {
2296 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2297
2298 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002299 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2300 }
2301
Douglas Gregorf85bee62010-02-08 22:59:26 +00002302 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002303 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2304 ObjCObjectPointerType *QType =
2305 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002306
Steve Narofffb4330f2009-06-17 22:40:22 +00002307 Types.push_back(QType);
2308 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002309 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002310}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002311
Douglas Gregor1c283312010-08-11 12:19:30 +00002312/// getObjCInterfaceType - Return the unique reference to the type for the
2313/// specified ObjC interface decl. The list of protocols is optional.
2314QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2315 if (Decl->TypeForDecl)
2316 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002317
Douglas Gregor1c283312010-08-11 12:19:30 +00002318 // FIXME: redeclarations?
2319 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2320 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2321 Decl->TypeForDecl = T;
2322 Types.push_back(T);
2323 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002324}
2325
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002326/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2327/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002328/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002329/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002330/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002331QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002332 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002333 if (tofExpr->isTypeDependent()) {
2334 llvm::FoldingSetNodeID ID;
2335 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002336
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002337 void *InsertPos = 0;
2338 DependentTypeOfExprType *Canon
2339 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2340 if (Canon) {
2341 // We already have a "canonical" version of an identical, dependent
2342 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002343 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002344 QualType((TypeOfExprType*)Canon, 0));
2345 }
2346 else {
2347 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002348 Canon
2349 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002350 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2351 toe = Canon;
2352 }
2353 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002354 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002355 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002356 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002357 Types.push_back(toe);
2358 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002359}
2360
Steve Naroffa773cd52007-08-01 18:02:17 +00002361/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2362/// TypeOfType AST's. The only motivation to unique these nodes would be
2363/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002364/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002365/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002366QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002367 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002368 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002369 Types.push_back(tot);
2370 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002371}
2372
Anders Carlssonad6bd352009-06-24 21:24:56 +00002373/// getDecltypeForExpr - Given an expr, will return the decltype for that
2374/// expression, according to the rules in C++0x [dcl.type.simple]p4
2375static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002376 if (e->isTypeDependent())
2377 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002378
Anders Carlssonad6bd352009-06-24 21:24:56 +00002379 // If e is an id expression or a class member access, decltype(e) is defined
2380 // as the type of the entity named by e.
2381 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2382 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2383 return VD->getType();
2384 }
2385 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2386 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2387 return FD->getType();
2388 }
2389 // If e is a function call or an invocation of an overloaded operator,
2390 // (parentheses around e are ignored), decltype(e) is defined as the
2391 // return type of that function.
2392 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2393 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002394
Anders Carlssonad6bd352009-06-24 21:24:56 +00002395 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002396
2397 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002398 // defined as T&, otherwise decltype(e) is defined as T.
John McCall086a4642010-11-24 05:12:34 +00002399 if (e->isLValue())
Anders Carlssonad6bd352009-06-24 21:24:56 +00002400 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002401
Anders Carlssonad6bd352009-06-24 21:24:56 +00002402 return T;
2403}
2404
Anders Carlsson81df7b82009-06-24 19:06:50 +00002405/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2406/// DecltypeType AST's. The only motivation to unique these nodes would be
2407/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002408/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002409/// on canonical type's (which are always unique).
2410QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002411 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002412 if (e->isTypeDependent()) {
2413 llvm::FoldingSetNodeID ID;
2414 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002415
Douglas Gregora21f6c32009-07-30 23:36:40 +00002416 void *InsertPos = 0;
2417 DependentDecltypeType *Canon
2418 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2419 if (Canon) {
2420 // We already have a "canonical" version of an equivalent, dependent
2421 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002422 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002423 QualType((DecltypeType*)Canon, 0));
2424 }
2425 else {
2426 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002427 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002428 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2429 dt = Canon;
2430 }
2431 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002432 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002433 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002434 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002435 Types.push_back(dt);
2436 return QualType(dt, 0);
2437}
2438
Chris Lattnerfb072462007-01-23 05:45:31 +00002439/// getTagDeclType - Return the unique reference to the type for the
2440/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002441QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002442 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002443 // FIXME: What is the design on getTagDeclType when it requires casting
2444 // away const? mutable?
2445 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002446}
2447
Mike Stump11289f42009-09-09 15:08:12 +00002448/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2449/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2450/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002451CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002452 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002453}
Chris Lattnerfb072462007-01-23 05:45:31 +00002454
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002455/// getSignedWCharType - Return the type of "signed wchar_t".
2456/// Used when in C++, as a GCC extension.
2457QualType ASTContext::getSignedWCharType() const {
2458 // FIXME: derive from "Target" ?
2459 return WCharTy;
2460}
2461
2462/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2463/// Used when in C++, as a GCC extension.
2464QualType ASTContext::getUnsignedWCharType() const {
2465 // FIXME: derive from "Target" ?
2466 return UnsignedIntTy;
2467}
2468
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002469/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2470/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2471QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002472 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002473}
2474
Chris Lattnera21ad802008-04-02 05:18:44 +00002475//===----------------------------------------------------------------------===//
2476// Type Operators
2477//===----------------------------------------------------------------------===//
2478
John McCallfc93cf92009-10-22 22:37:11 +00002479CanQualType ASTContext::getCanonicalParamType(QualType T) {
2480 // Push qualifiers into arrays, and then discard any remaining
2481 // qualifiers.
2482 T = getCanonicalType(T);
Fariborz Jahanian8fb87ae2010-09-24 17:30:16 +00002483 T = getVariableArrayDecayedType(T);
John McCallfc93cf92009-10-22 22:37:11 +00002484 const Type *Ty = T.getTypePtr();
John McCallfc93cf92009-10-22 22:37:11 +00002485 QualType Result;
2486 if (isa<ArrayType>(Ty)) {
2487 Result = getArrayDecayedType(QualType(Ty,0));
2488 } else if (isa<FunctionType>(Ty)) {
2489 Result = getPointerType(QualType(Ty, 0));
2490 } else {
2491 Result = QualType(Ty, 0);
2492 }
2493
2494 return CanQualType::CreateUnsafe(Result);
2495}
2496
Chris Lattnered0d0792008-04-06 22:41:35 +00002497/// getCanonicalType - Return the canonical (structural) type corresponding to
2498/// the specified potentially non-canonical type. The non-canonical version
2499/// of a type may have many "decorated" versions of types. Decorators can
2500/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2501/// to be free of any of these, allowing two canonical types to be compared
2502/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002503CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002504 QualifierCollector Quals;
2505 const Type *Ptr = Quals.strip(T);
2506 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002507
John McCall8ccfcb52009-09-24 19:53:00 +00002508 // The canonical internal type will be the canonical type *except*
2509 // that we push type qualifiers down through array types.
2510
2511 // If there are no new qualifiers to push down, stop here.
2512 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002513 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002514
John McCall8ccfcb52009-09-24 19:53:00 +00002515 // If the type qualifiers are on an array type, get the canonical
2516 // type of the array with the qualifiers applied to the element
2517 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002518 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2519 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002520 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002521
Chris Lattner7adf0762008-08-04 07:31:14 +00002522 // Get the canonical version of the element with the extra qualifiers on it.
2523 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002524 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002525 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002526
Chris Lattner7adf0762008-08-04 07:31:14 +00002527 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002528 return CanQualType::CreateUnsafe(
2529 getConstantArrayType(NewEltTy, CAT->getSize(),
2530 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002531 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002532 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002533 return CanQualType::CreateUnsafe(
2534 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002535 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002536
Douglas Gregor4619e432008-12-05 23:32:09 +00002537 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002538 return CanQualType::CreateUnsafe(
2539 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002540 DSAT->getSizeExpr(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002541 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002542 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002543 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002544
Chris Lattner7adf0762008-08-04 07:31:14 +00002545 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002546 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002547 VAT->getSizeExpr(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002548 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002549 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002550 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002551}
2552
Chandler Carruth607f38e2009-12-29 07:16:59 +00002553QualType ASTContext::getUnqualifiedArrayType(QualType T,
2554 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002555 Quals = T.getQualifiers();
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002556 const ArrayType *AT = getAsArrayType(T);
2557 if (!AT) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002558 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002559 }
2560
Chandler Carruth607f38e2009-12-29 07:16:59 +00002561 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002562 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002563 if (Elt == UnqualElt)
2564 return T;
2565
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002566 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002567 return getConstantArrayType(UnqualElt, CAT->getSize(),
2568 CAT->getSizeModifier(), 0);
2569 }
2570
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002571 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002572 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2573 }
2574
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002575 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2576 return getVariableArrayType(UnqualElt,
John McCallc3007a22010-10-26 07:05:15 +00002577 VAT->getSizeExpr(),
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002578 VAT->getSizeModifier(),
2579 VAT->getIndexTypeCVRQualifiers(),
2580 VAT->getBracketsRange());
2581 }
2582
2583 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCallc3007a22010-10-26 07:05:15 +00002584 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr(),
Chandler Carruth607f38e2009-12-29 07:16:59 +00002585 DSAT->getSizeModifier(), 0,
2586 SourceRange());
2587}
2588
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002589/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2590/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2591/// they point to and return true. If T1 and T2 aren't pointer types
2592/// or pointer-to-member types, or if they are not similar at this
2593/// level, returns false and leaves T1 and T2 unchanged. Top-level
2594/// qualifiers on T1 and T2 are ignored. This function will typically
2595/// be called in a loop that successively "unwraps" pointer and
2596/// pointer-to-member types to compare them at each level.
2597bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2598 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2599 *T2PtrType = T2->getAs<PointerType>();
2600 if (T1PtrType && T2PtrType) {
2601 T1 = T1PtrType->getPointeeType();
2602 T2 = T2PtrType->getPointeeType();
2603 return true;
2604 }
2605
2606 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2607 *T2MPType = T2->getAs<MemberPointerType>();
2608 if (T1MPType && T2MPType &&
2609 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2610 QualType(T2MPType->getClass(), 0))) {
2611 T1 = T1MPType->getPointeeType();
2612 T2 = T2MPType->getPointeeType();
2613 return true;
2614 }
2615
2616 if (getLangOptions().ObjC1) {
2617 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2618 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2619 if (T1OPType && T2OPType) {
2620 T1 = T1OPType->getPointeeType();
2621 T2 = T2OPType->getPointeeType();
2622 return true;
2623 }
2624 }
2625
2626 // FIXME: Block pointers, too?
2627
2628 return false;
2629}
2630
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002631DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name,
2632 SourceLocation NameLoc) {
John McCall847e2a12009-11-24 18:42:40 +00002633 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002634 // DNInfo work in progress: CHECKME: what about DNLoc?
2635 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2636
John McCall847e2a12009-11-24 18:42:40 +00002637 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002638 DeclarationName DName;
John McCall847e2a12009-11-24 18:42:40 +00002639 if (DTN->isIdentifier()) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002640 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2641 return DeclarationNameInfo(DName, NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002642 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002643 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2644 // DNInfo work in progress: FIXME: source locations?
2645 DeclarationNameLoc DNLoc;
2646 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2647 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2648 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall847e2a12009-11-24 18:42:40 +00002649 }
2650 }
2651
John McCalld28ae272009-12-02 08:04:21 +00002652 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2653 assert(Storage);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002654 // DNInfo work in progress: CHECKME: what about DNLoc?
2655 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall847e2a12009-11-24 18:42:40 +00002656}
2657
Douglas Gregor6bc50582009-05-07 06:41:52 +00002658TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002659 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2660 if (TemplateTemplateParmDecl *TTP
2661 = dyn_cast<TemplateTemplateParmDecl>(Template))
2662 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2663
2664 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002665 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002666 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002667
John McCalld28ae272009-12-02 08:04:21 +00002668 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002669
Douglas Gregor6bc50582009-05-07 06:41:52 +00002670 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2671 assert(DTN && "Non-dependent template names must refer to template decls.");
2672 return DTN->CanonicalTemplateName;
2673}
2674
Douglas Gregoradee3e32009-11-11 23:06:43 +00002675bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2676 X = getCanonicalTemplateName(X);
2677 Y = getCanonicalTemplateName(Y);
2678 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2679}
2680
Mike Stump11289f42009-09-09 15:08:12 +00002681TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002682ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2683 switch (Arg.getKind()) {
2684 case TemplateArgument::Null:
2685 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002686
Douglas Gregora8e02e72009-07-28 23:00:59 +00002687 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002688 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002689
Douglas Gregora8e02e72009-07-28 23:00:59 +00002690 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002691 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002692
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002693 case TemplateArgument::Template:
2694 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2695
Douglas Gregora8e02e72009-07-28 23:00:59 +00002696 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002697 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002698 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002699
Douglas Gregora8e02e72009-07-28 23:00:59 +00002700 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002701 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002702
Douglas Gregora8e02e72009-07-28 23:00:59 +00002703 case TemplateArgument::Pack: {
Douglas Gregor0192c232010-12-20 16:52:59 +00002704 if (Arg.pack_size() == 0)
2705 return Arg;
2706
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002707 TemplateArgument *CanonArgs
2708 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregora8e02e72009-07-28 23:00:59 +00002709 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002710 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002711 AEnd = Arg.pack_end();
2712 A != AEnd; (void)++A, ++Idx)
2713 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002714
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002715 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregora8e02e72009-07-28 23:00:59 +00002716 }
2717 }
2718
2719 // Silence GCC warning
2720 assert(false && "Unhandled template argument kind");
2721 return TemplateArgument();
2722}
2723
Douglas Gregor333489b2009-03-27 23:10:48 +00002724NestedNameSpecifier *
2725ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002726 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002727 return 0;
2728
2729 switch (NNS->getKind()) {
2730 case NestedNameSpecifier::Identifier:
2731 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002732 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002733 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2734 NNS->getAsIdentifier());
2735
2736 case NestedNameSpecifier::Namespace:
2737 // A namespace is canonical; build a nested-name-specifier with
2738 // this namespace and no prefix.
2739 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2740
2741 case NestedNameSpecifier::TypeSpec:
2742 case NestedNameSpecifier::TypeSpecWithTemplate: {
2743 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor3ade5702010-11-04 00:09:33 +00002744
2745 // If we have some kind of dependent-named type (e.g., "typename T::type"),
2746 // break it apart into its prefix and identifier, then reconsititute those
2747 // as the canonical nested-name-specifier. This is required to canonicalize
2748 // a dependent nested-name-specifier involving typedefs of dependent-name
2749 // types, e.g.,
2750 // typedef typename T::type T1;
2751 // typedef typename T1::type T2;
2752 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
2753 NestedNameSpecifier *Prefix
2754 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
2755 return NestedNameSpecifier::Create(*this, Prefix,
2756 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
2757 }
2758
Douglas Gregorcc10cbf2010-11-04 00:14:23 +00002759 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor3ade5702010-11-04 00:09:33 +00002760 if (const DependentTemplateSpecializationType *DTST
2761 = T->getAs<DependentTemplateSpecializationType>()) {
2762 NestedNameSpecifier *Prefix
2763 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
2764 TemplateName Name
2765 = getDependentTemplateName(Prefix, DTST->getIdentifier());
2766 T = getTemplateSpecializationType(Name,
2767 DTST->getArgs(), DTST->getNumArgs());
2768 T = getCanonicalType(T);
2769 }
2770
2771 return NestedNameSpecifier::Create(*this, 0, false, T.getTypePtr());
Douglas Gregor333489b2009-03-27 23:10:48 +00002772 }
2773
2774 case NestedNameSpecifier::Global:
2775 // The global specifier is canonical and unique.
2776 return NNS;
2777 }
2778
2779 // Required to silence a GCC warning
2780 return 0;
2781}
2782
Chris Lattner7adf0762008-08-04 07:31:14 +00002783
2784const ArrayType *ASTContext::getAsArrayType(QualType T) {
2785 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002786 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002787 // Handle the common positive case fast.
2788 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2789 return AT;
2790 }
Mike Stump11289f42009-09-09 15:08:12 +00002791
John McCall8ccfcb52009-09-24 19:53:00 +00002792 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002793 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002794 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002795 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002796
John McCall8ccfcb52009-09-24 19:53:00 +00002797 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002798 // implements C99 6.7.3p8: "If the specification of an array type includes
2799 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002800
Chris Lattner7adf0762008-08-04 07:31:14 +00002801 // If we get here, we either have type qualifiers on the type, or we have
2802 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002803 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002804
John McCall8ccfcb52009-09-24 19:53:00 +00002805 QualifierCollector Qs;
John McCall717d9b02010-12-10 11:01:00 +00002806 const Type *Ty = Qs.strip(T.getDesugaredType(*this));
Mike Stump11289f42009-09-09 15:08:12 +00002807
Chris Lattner7adf0762008-08-04 07:31:14 +00002808 // If we have a simple case, just return now.
2809 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002810 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002811 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002812
Chris Lattner7adf0762008-08-04 07:31:14 +00002813 // Otherwise, we have an array and we have qualifiers on it. Push the
2814 // qualifiers into the array element type and return a new array type.
2815 // Get the canonical version of the element with the extra qualifiers on it.
2816 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002817 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002818
Chris Lattner7adf0762008-08-04 07:31:14 +00002819 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2820 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2821 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002822 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002823 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2824 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2825 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002826 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002827
Mike Stump11289f42009-09-09 15:08:12 +00002828 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002829 = dyn_cast<DependentSizedArrayType>(ATy))
2830 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002831 getDependentSizedArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002832 DSAT->getSizeExpr(),
Douglas Gregor4619e432008-12-05 23:32:09 +00002833 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002834 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002835 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002836
Chris Lattner7adf0762008-08-04 07:31:14 +00002837 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002838 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCallc3007a22010-10-26 07:05:15 +00002839 VAT->getSizeExpr(),
Chris Lattner7adf0762008-08-04 07:31:14 +00002840 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002841 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002842 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002843}
2844
Chris Lattnera21ad802008-04-02 05:18:44 +00002845/// getArrayDecayedType - Return the properly qualified result of decaying the
2846/// specified array type to a pointer. This operation is non-trivial when
2847/// handling typedefs etc. The canonical type of "T" must be an array type,
2848/// this returns a pointer to a properly qualified element of the array.
2849///
2850/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2851QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002852 // Get the element type with 'getAsArrayType' so that we don't lose any
2853 // typedefs in the element type of the array. This also handles propagation
2854 // of type qualifiers from the array type into the element type if present
2855 // (C99 6.7.3p8).
2856 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2857 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002858
Chris Lattner7adf0762008-08-04 07:31:14 +00002859 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002860
2861 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002862 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002863}
2864
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002865QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002866 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002867 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2868 QT = AT->getElementType();
John McCall717d9b02010-12-10 11:01:00 +00002869 return Qs.apply(*this, QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002870}
2871
Anders Carlsson4bf82142009-09-25 01:23:32 +00002872QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2873 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002874
Anders Carlsson4bf82142009-09-25 01:23:32 +00002875 if (const ArrayType *AT = getAsArrayType(ElemTy))
2876 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002877
Anders Carlssone0808df2008-12-21 03:44:36 +00002878 return ElemTy;
2879}
2880
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002881/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002882uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002883ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2884 uint64_t ElementCount = 1;
2885 do {
2886 ElementCount *= CA->getSize().getZExtValue();
2887 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2888 } while (CA);
2889 return ElementCount;
2890}
2891
Steve Naroff0af91202007-04-27 21:51:21 +00002892/// getFloatingRank - Return a relative rank for floating point types.
2893/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002894static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002895 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002896 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002897
John McCall9dd450b2009-09-21 23:43:11 +00002898 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2899 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002900 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002901 case BuiltinType::Float: return FloatRank;
2902 case BuiltinType::Double: return DoubleRank;
2903 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002904 }
2905}
2906
Mike Stump11289f42009-09-09 15:08:12 +00002907/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2908/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002909/// 'typeDomain' is a real floating point or complex type.
2910/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002911QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2912 QualType Domain) const {
2913 FloatingRank EltRank = getFloatingRank(Size);
2914 if (Domain->isComplexType()) {
2915 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002916 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002917 case FloatRank: return FloatComplexTy;
2918 case DoubleRank: return DoubleComplexTy;
2919 case LongDoubleRank: return LongDoubleComplexTy;
2920 }
Steve Naroff0af91202007-04-27 21:51:21 +00002921 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002922
2923 assert(Domain->isRealFloatingType() && "Unknown domain!");
2924 switch (EltRank) {
2925 default: assert(0 && "getFloatingRank(): illegal value for rank");
2926 case FloatRank: return FloatTy;
2927 case DoubleRank: return DoubleTy;
2928 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002929 }
Steve Naroffe4718892007-04-27 18:30:00 +00002930}
2931
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002932/// getFloatingTypeOrder - Compare the rank of the two specified floating
2933/// point types, ignoring the domain of the type (i.e. 'double' ==
2934/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002935/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002936int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2937 FloatingRank LHSR = getFloatingRank(LHS);
2938 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002939
Chris Lattnerb90739d2008-04-06 23:38:49 +00002940 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002941 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002942 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002943 return 1;
2944 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002945}
2946
Chris Lattner76a00cf2008-04-06 22:59:24 +00002947/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2948/// routine will assert if passed a built-in type that isn't an integer or enum,
2949/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002950unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002951 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002952 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002953 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002954
Chris Lattnerad3467e2010-12-25 23:25:43 +00002955 if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
2956 T->isSpecificBuiltinType(BuiltinType::WChar_U))
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002957 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2958
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002959 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2960 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2961
2962 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2963 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2964
Chris Lattner76a00cf2008-04-06 22:59:24 +00002965 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002966 default: assert(0 && "getIntegerRank(): not a built-in integer");
2967 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002968 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002969 case BuiltinType::Char_S:
2970 case BuiltinType::Char_U:
2971 case BuiltinType::SChar:
2972 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002973 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002974 case BuiltinType::Short:
2975 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002976 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002977 case BuiltinType::Int:
2978 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002979 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002980 case BuiltinType::Long:
2981 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002982 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002983 case BuiltinType::LongLong:
2984 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002985 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002986 case BuiltinType::Int128:
2987 case BuiltinType::UInt128:
2988 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002989 }
2990}
2991
Eli Friedman629ffb92009-08-20 04:21:42 +00002992/// \brief Whether this is a promotable bitfield reference according
2993/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2994///
2995/// \returns the type this bit-field will promote to, or NULL if no
2996/// promotion occurs.
2997QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00002998 if (E->isTypeDependent() || E->isValueDependent())
2999 return QualType();
3000
Eli Friedman629ffb92009-08-20 04:21:42 +00003001 FieldDecl *Field = E->getBitField();
3002 if (!Field)
3003 return QualType();
3004
3005 QualType FT = Field->getType();
3006
3007 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3008 uint64_t BitWidth = BitWidthAP.getZExtValue();
3009 uint64_t IntSize = getTypeSize(IntTy);
3010 // GCC extension compatibility: if the bit-field size is less than or equal
3011 // to the size of int, it gets promoted no matter what its type is.
3012 // For instance, unsigned long bf : 4 gets promoted to signed int.
3013 if (BitWidth < IntSize)
3014 return IntTy;
3015
3016 if (BitWidth == IntSize)
3017 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3018
3019 // Types bigger than int are not subject to promotions, and therefore act
3020 // like the base type.
3021 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3022 // is ridiculous.
3023 return QualType();
3024}
3025
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003026/// getPromotedIntegerType - Returns the type that Promotable will
3027/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3028/// integer type.
3029QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
3030 assert(!Promotable.isNull());
3031 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00003032 if (const EnumType *ET = Promotable->getAs<EnumType>())
3033 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00003034 if (Promotable->isSignedIntegerType())
3035 return IntTy;
3036 uint64_t PromotableSize = getTypeSize(Promotable);
3037 uint64_t IntSize = getTypeSize(IntTy);
3038 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3039 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3040}
3041
Mike Stump11289f42009-09-09 15:08:12 +00003042/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003043/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00003044/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003045int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00003046 Type *LHSC = getCanonicalType(LHS).getTypePtr();
3047 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003048 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00003049
Chris Lattner76a00cf2008-04-06 22:59:24 +00003050 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3051 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00003052
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003053 unsigned LHSRank = getIntegerRank(LHSC);
3054 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00003055
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003056 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3057 if (LHSRank == RHSRank) return 0;
3058 return LHSRank > RHSRank ? 1 : -1;
3059 }
Mike Stump11289f42009-09-09 15:08:12 +00003060
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003061 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3062 if (LHSUnsigned) {
3063 // If the unsigned [LHS] type is larger, return it.
3064 if (LHSRank >= RHSRank)
3065 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00003066
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003067 // If the signed type can represent all values of the unsigned type, it
3068 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003069 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003070 return -1;
3071 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00003072
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003073 // If the unsigned [RHS] type is larger, return it.
3074 if (RHSRank >= LHSRank)
3075 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00003076
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003077 // If the signed type can represent all values of the unsigned type, it
3078 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00003079 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00003080 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00003081}
Anders Carlsson98f07902007-08-17 05:31:46 +00003082
Anders Carlsson6d417272009-11-14 21:45:58 +00003083static RecordDecl *
3084CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
3085 SourceLocation L, IdentifierInfo *Id) {
3086 if (Ctx.getLangOptions().CPlusPlus)
3087 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
3088 else
3089 return RecordDecl::Create(Ctx, TK, DC, L, Id);
3090}
3091
Mike Stump11289f42009-09-09 15:08:12 +00003092// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00003093QualType ASTContext::getCFConstantStringType() {
3094 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00003095 CFConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003096 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003097 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00003098 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00003099
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003100 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00003101
Anders Carlsson98f07902007-08-17 05:31:46 +00003102 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00003103 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00003104 // int flags;
3105 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00003106 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00003107 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00003108 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00003109 FieldTypes[3] = LongTy;
3110
Anders Carlsson98f07902007-08-17 05:31:46 +00003111 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00003112 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003113 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00003114 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003115 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003116 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003117 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003118 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003119 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003120 }
3121
Douglas Gregord5058122010-02-11 01:19:42 +00003122 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00003123 }
Mike Stump11289f42009-09-09 15:08:12 +00003124
Anders Carlsson98f07902007-08-17 05:31:46 +00003125 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00003126}
Anders Carlsson87c149b2007-10-11 01:00:40 +00003127
Douglas Gregor512b0772009-04-23 22:29:11 +00003128void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003129 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003130 assert(Rec && "Invalid CFConstantStringType");
3131 CFConstantStringTypeDecl = Rec->getDecl();
3132}
3133
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003134// getNSConstantStringType - Return the type used for constant NSStrings.
3135QualType ASTContext::getNSConstantStringType() {
3136 if (!NSConstantStringTypeDecl) {
3137 NSConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003138 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003139 &Idents.get("__builtin_NSString"));
3140 NSConstantStringTypeDecl->startDefinition();
3141
3142 QualType FieldTypes[3];
3143
3144 // const int *isa;
3145 FieldTypes[0] = getPointerType(IntTy.withConst());
3146 // const char *str;
3147 FieldTypes[1] = getPointerType(CharTy.withConst());
3148 // unsigned int length;
3149 FieldTypes[2] = UnsignedIntTy;
3150
3151 // Create fields
3152 for (unsigned i = 0; i < 3; ++i) {
3153 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3154 SourceLocation(), 0,
3155 FieldTypes[i], /*TInfo=*/0,
3156 /*BitWidth=*/0,
3157 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003158 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003159 NSConstantStringTypeDecl->addDecl(Field);
3160 }
3161
3162 NSConstantStringTypeDecl->completeDefinition();
3163 }
3164
3165 return getTagDeclType(NSConstantStringTypeDecl);
3166}
3167
3168void ASTContext::setNSConstantStringType(QualType T) {
3169 const RecordType *Rec = T->getAs<RecordType>();
3170 assert(Rec && "Invalid NSConstantStringType");
3171 NSConstantStringTypeDecl = Rec->getDecl();
3172}
3173
Mike Stump11289f42009-09-09 15:08:12 +00003174QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003175 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003176 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003177 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003178 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003179 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003180
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003181 QualType FieldTypes[] = {
3182 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003183 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003184 getPointerType(UnsignedLongTy),
3185 getConstantArrayType(UnsignedLongTy,
3186 llvm::APInt(32, 5), ArrayType::Normal, 0)
3187 };
Mike Stump11289f42009-09-09 15:08:12 +00003188
Douglas Gregor91f84212008-12-11 16:49:14 +00003189 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003190 FieldDecl *Field = FieldDecl::Create(*this,
3191 ObjCFastEnumerationStateTypeDecl,
3192 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003193 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003194 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003195 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003196 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003197 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003198 }
Mike Stump11289f42009-09-09 15:08:12 +00003199
Douglas Gregord5058122010-02-11 01:19:42 +00003200 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003201 }
Mike Stump11289f42009-09-09 15:08:12 +00003202
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003203 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3204}
3205
Mike Stumpd0153282009-10-20 02:12:22 +00003206QualType ASTContext::getBlockDescriptorType() {
3207 if (BlockDescriptorType)
3208 return getTagDeclType(BlockDescriptorType);
3209
3210 RecordDecl *T;
3211 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003212 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003213 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003214 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003215
3216 QualType FieldTypes[] = {
3217 UnsignedLongTy,
3218 UnsignedLongTy,
3219 };
3220
3221 const char *FieldNames[] = {
3222 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003223 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003224 };
3225
3226 for (size_t i = 0; i < 2; ++i) {
3227 FieldDecl *Field = FieldDecl::Create(*this,
3228 T,
3229 SourceLocation(),
3230 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003231 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003232 /*BitWidth=*/0,
3233 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003234 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003235 T->addDecl(Field);
3236 }
3237
Douglas Gregord5058122010-02-11 01:19:42 +00003238 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003239
3240 BlockDescriptorType = T;
3241
3242 return getTagDeclType(BlockDescriptorType);
3243}
3244
3245void ASTContext::setBlockDescriptorType(QualType T) {
3246 const RecordType *Rec = T->getAs<RecordType>();
3247 assert(Rec && "Invalid BlockDescriptorType");
3248 BlockDescriptorType = Rec->getDecl();
3249}
3250
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003251QualType ASTContext::getBlockDescriptorExtendedType() {
3252 if (BlockDescriptorExtendedType)
3253 return getTagDeclType(BlockDescriptorExtendedType);
3254
3255 RecordDecl *T;
3256 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003257 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003258 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003259 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003260
3261 QualType FieldTypes[] = {
3262 UnsignedLongTy,
3263 UnsignedLongTy,
3264 getPointerType(VoidPtrTy),
3265 getPointerType(VoidPtrTy)
3266 };
3267
3268 const char *FieldNames[] = {
3269 "reserved",
3270 "Size",
3271 "CopyFuncPtr",
3272 "DestroyFuncPtr"
3273 };
3274
3275 for (size_t i = 0; i < 4; ++i) {
3276 FieldDecl *Field = FieldDecl::Create(*this,
3277 T,
3278 SourceLocation(),
3279 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003280 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003281 /*BitWidth=*/0,
3282 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003283 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003284 T->addDecl(Field);
3285 }
3286
Douglas Gregord5058122010-02-11 01:19:42 +00003287 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003288
3289 BlockDescriptorExtendedType = T;
3290
3291 return getTagDeclType(BlockDescriptorExtendedType);
3292}
3293
3294void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3295 const RecordType *Rec = T->getAs<RecordType>();
3296 assert(Rec && "Invalid BlockDescriptorType");
3297 BlockDescriptorExtendedType = Rec->getDecl();
3298}
3299
Mike Stump94967902009-10-21 18:16:27 +00003300bool ASTContext::BlockRequiresCopying(QualType Ty) {
3301 if (Ty->isBlockPointerType())
3302 return true;
3303 if (isObjCNSObjectType(Ty))
3304 return true;
3305 if (Ty->isObjCObjectPointerType())
3306 return true;
Fariborz Jahaniana00076c2010-11-17 00:21:28 +00003307 if (getLangOptions().CPlusPlus) {
3308 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3309 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3310 return RD->hasConstCopyConstructor(*this);
3311
3312 }
3313 }
Mike Stump94967902009-10-21 18:16:27 +00003314 return false;
3315}
3316
Daniel Dunbar56df9772010-08-17 22:39:59 +00003317QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) {
Mike Stump94967902009-10-21 18:16:27 +00003318 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003319 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003320 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003321 // unsigned int __flags;
3322 // unsigned int __size;
Eli Friedman04831922010-08-22 01:00:03 +00003323 // void *__copy_helper; // as needed
3324 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003325 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003326 // } *
3327
Mike Stump94967902009-10-21 18:16:27 +00003328 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3329
3330 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003331 llvm::SmallString<36> Name;
3332 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3333 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003334 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003335 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003336 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003337 T->startDefinition();
3338 QualType Int32Ty = IntTy;
3339 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3340 QualType FieldTypes[] = {
3341 getPointerType(VoidPtrTy),
3342 getPointerType(getTagDeclType(T)),
3343 Int32Ty,
3344 Int32Ty,
3345 getPointerType(VoidPtrTy),
3346 getPointerType(VoidPtrTy),
3347 Ty
3348 };
3349
Daniel Dunbar56df9772010-08-17 22:39:59 +00003350 llvm::StringRef FieldNames[] = {
Mike Stump94967902009-10-21 18:16:27 +00003351 "__isa",
3352 "__forwarding",
3353 "__flags",
3354 "__size",
3355 "__copy_helper",
3356 "__destroy_helper",
3357 DeclName,
3358 };
3359
3360 for (size_t i = 0; i < 7; ++i) {
3361 if (!HasCopyAndDispose && i >=4 && i <= 5)
3362 continue;
3363 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3364 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003365 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003366 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003367 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003368 T->addDecl(Field);
3369 }
3370
Douglas Gregord5058122010-02-11 01:19:42 +00003371 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003372
3373 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003374}
3375
3376
3377QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003378 bool BlockHasCopyDispose,
John McCall87fe5d52010-05-20 01:18:31 +00003379 llvm::SmallVectorImpl<const Expr *> &Layout) {
3380
Mike Stumpd0153282009-10-20 02:12:22 +00003381 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003382 llvm::SmallString<36> Name;
3383 llvm::raw_svector_ostream(Name) << "__block_literal_"
3384 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003385 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003386 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003387 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003388 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003389 QualType FieldTypes[] = {
3390 getPointerType(VoidPtrTy),
3391 IntTy,
3392 IntTy,
3393 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003394 (BlockHasCopyDispose ?
3395 getPointerType(getBlockDescriptorExtendedType()) :
3396 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003397 };
3398
3399 const char *FieldNames[] = {
3400 "__isa",
3401 "__flags",
3402 "__reserved",
3403 "__FuncPtr",
3404 "__descriptor"
3405 };
3406
3407 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003408 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003409 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003410 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003411 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003412 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003413 T->addDecl(Field);
3414 }
3415
John McCall87fe5d52010-05-20 01:18:31 +00003416 for (unsigned i = 0; i < Layout.size(); ++i) {
3417 const Expr *E = Layout[i];
Mike Stump7fe9cc12009-10-21 03:49:08 +00003418
John McCall87fe5d52010-05-20 01:18:31 +00003419 QualType FieldType = E->getType();
3420 IdentifierInfo *FieldName = 0;
3421 if (isa<CXXThisExpr>(E)) {
3422 FieldName = &Idents.get("this");
3423 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3424 const ValueDecl *D = BDRE->getDecl();
3425 FieldName = D->getIdentifier();
3426 if (BDRE->isByRef())
Daniel Dunbar56df9772010-08-17 22:39:59 +00003427 FieldType = BuildByRefType(D->getName(), FieldType);
John McCall87fe5d52010-05-20 01:18:31 +00003428 } else {
3429 // Padding.
3430 assert(isa<ConstantArrayType>(FieldType) &&
3431 isa<DeclRefExpr>(E) &&
3432 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3433 "doesn't match characteristics of padding decl");
3434 }
Mike Stump7fe9cc12009-10-21 03:49:08 +00003435
3436 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCall87fe5d52010-05-20 01:18:31 +00003437 FieldName, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003438 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003439 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003440 T->addDecl(Field);
3441 }
3442
Douglas Gregord5058122010-02-11 01:19:42 +00003443 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003444
3445 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003446}
3447
Douglas Gregor512b0772009-04-23 22:29:11 +00003448void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003449 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003450 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3451 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3452}
3453
Anders Carlsson18acd442007-10-29 06:33:42 +00003454// This returns true if a type has been typedefed to BOOL:
3455// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003456static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003457 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003458 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3459 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003460
Anders Carlssond8499822007-10-29 05:01:08 +00003461 return false;
3462}
3463
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003464/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003465/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003466CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003467 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003468
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003469 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003470 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003471 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003472 // Treat arrays as pointers, since that's how they're passed in.
3473 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003474 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003475 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003476}
3477
3478static inline
3479std::string charUnitsToString(const CharUnits &CU) {
3480 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003481}
3482
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003483/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003484/// declaration.
3485void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3486 std::string& S) {
3487 const BlockDecl *Decl = Expr->getBlockDecl();
3488 QualType BlockTy =
3489 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3490 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003491 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003492 // Compute size of all parameters.
3493 // Start with computing size of a pointer in number of bytes.
3494 // FIXME: There might(should) be a better way of doing this computation!
3495 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003496 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3497 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003498 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003499 E = Decl->param_end(); PI != E; ++PI) {
3500 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003501 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003502 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003503 ParmOffset += sz;
3504 }
3505 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003506 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003507 // Block pointer and offset.
3508 S += "@?0";
3509 ParmOffset = PtrSize;
3510
3511 // Argument types.
3512 ParmOffset = PtrSize;
3513 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3514 Decl->param_end(); PI != E; ++PI) {
3515 ParmVarDecl *PVDecl = *PI;
3516 QualType PType = PVDecl->getOriginalType();
3517 if (const ArrayType *AT =
3518 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3519 // Use array's original type only if it has known number of
3520 // elements.
3521 if (!isa<ConstantArrayType>(AT))
3522 PType = PVDecl->getType();
3523 } else if (PType->isFunctionType())
3524 PType = PVDecl->getType();
3525 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003526 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003527 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003528 }
3529}
3530
David Chisnall50e4eba2010-12-30 14:05:53 +00003531void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
3532 std::string& S) {
3533 // Encode result type.
3534 getObjCEncodingForType(Decl->getResultType(), S);
3535 CharUnits ParmOffset;
3536 // Compute size of all parameters.
3537 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3538 E = Decl->param_end(); PI != E; ++PI) {
3539 QualType PType = (*PI)->getType();
3540 CharUnits sz = getObjCEncodingTypeSize(PType);
3541 assert (sz.isPositive() &&
3542 "getObjCEncodingForMethodDecl - Incomplete param type");
3543 ParmOffset += sz;
3544 }
3545 S += charUnitsToString(ParmOffset);
3546 ParmOffset = CharUnits::Zero();
3547
3548 // Argument types.
3549 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3550 E = Decl->param_end(); PI != E; ++PI) {
3551 ParmVarDecl *PVDecl = *PI;
3552 QualType PType = PVDecl->getOriginalType();
3553 if (const ArrayType *AT =
3554 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3555 // Use array's original type only if it has known number of
3556 // elements.
3557 if (!isa<ConstantArrayType>(AT))
3558 PType = PVDecl->getType();
3559 } else if (PType->isFunctionType())
3560 PType = PVDecl->getType();
3561 getObjCEncodingForType(PType, S);
3562 S += charUnitsToString(ParmOffset);
3563 ParmOffset += getObjCEncodingTypeSize(PType);
3564 }
3565}
3566
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003567/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003568/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003569void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003570 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003571 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003572 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003573 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003574 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003575 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003576 // Compute size of all parameters.
3577 // Start with computing size of a pointer in number of bytes.
3578 // FIXME: There might(should) be a better way of doing this computation!
3579 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003580 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003581 // The first two arguments (self and _cmd) are pointers; account for
3582 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003583 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003584 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003585 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003586 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003587 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003588 assert (sz.isPositive() &&
3589 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003590 ParmOffset += sz;
3591 }
Ken Dyck40775002010-01-11 17:06:35 +00003592 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003593 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003594 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003595
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003596 // Argument types.
3597 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003598 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003599 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003600 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003601 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003602 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003603 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3604 // Use array's original type only if it has known number of
3605 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003606 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003607 PType = PVDecl->getType();
3608 } else if (PType->isFunctionType())
3609 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003610 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003611 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003612 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003613 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003614 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003615 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003616 }
3617}
3618
Daniel Dunbar4932b362008-08-28 04:38:10 +00003619/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003620/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003621/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3622/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003623/// Property attributes are stored as a comma-delimited C string. The simple
3624/// attributes readonly and bycopy are encoded as single characters. The
3625/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3626/// encoded as single characters, followed by an identifier. Property types
3627/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003628/// these attributes are defined by the following enumeration:
3629/// @code
3630/// enum PropertyAttributes {
3631/// kPropertyReadOnly = 'R', // property is read-only.
3632/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3633/// kPropertyByref = '&', // property is a reference to the value last assigned
3634/// kPropertyDynamic = 'D', // property is dynamic
3635/// kPropertyGetter = 'G', // followed by getter selector name
3636/// kPropertySetter = 'S', // followed by setter selector name
3637/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3638/// kPropertyType = 't' // followed by old-style type encoding.
3639/// kPropertyWeak = 'W' // 'weak' property
3640/// kPropertyStrong = 'P' // property GC'able
3641/// kPropertyNonAtomic = 'N' // property non-atomic
3642/// };
3643/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003644void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003645 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003646 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003647 // Collect information from the property implementation decl(s).
3648 bool Dynamic = false;
3649 ObjCPropertyImplDecl *SynthesizePID = 0;
3650
3651 // FIXME: Duplicated code due to poor abstraction.
3652 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003653 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003654 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3655 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003656 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003657 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003658 ObjCPropertyImplDecl *PID = *i;
3659 if (PID->getPropertyDecl() == PD) {
3660 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3661 Dynamic = true;
3662 } else {
3663 SynthesizePID = PID;
3664 }
3665 }
3666 }
3667 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003668 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003669 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003670 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003671 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003672 ObjCPropertyImplDecl *PID = *i;
3673 if (PID->getPropertyDecl() == PD) {
3674 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3675 Dynamic = true;
3676 } else {
3677 SynthesizePID = PID;
3678 }
3679 }
Mike Stump11289f42009-09-09 15:08:12 +00003680 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003681 }
3682 }
3683
3684 // FIXME: This is not very efficient.
3685 S = "T";
3686
3687 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003688 // GCC has some special rules regarding encoding of properties which
3689 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003690 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003691 true /* outermost type */,
3692 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003693
3694 if (PD->isReadOnly()) {
3695 S += ",R";
3696 } else {
3697 switch (PD->getSetterKind()) {
3698 case ObjCPropertyDecl::Assign: break;
3699 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003700 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003701 }
3702 }
3703
3704 // It really isn't clear at all what this means, since properties
3705 // are "dynamic by default".
3706 if (Dynamic)
3707 S += ",D";
3708
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003709 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3710 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003711
Daniel Dunbar4932b362008-08-28 04:38:10 +00003712 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3713 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003714 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003715 }
3716
3717 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3718 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003719 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003720 }
3721
3722 if (SynthesizePID) {
3723 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3724 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003725 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003726 }
3727
3728 // FIXME: OBJCGC: weak & strong
3729}
3730
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003731/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003732/// Another legacy compatibility encoding: 32-bit longs are encoded as
3733/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003734/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3735///
3736void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003737 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003738 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003739 if (BT->getKind() == BuiltinType::ULong &&
3740 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003741 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003742 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003743 if (BT->getKind() == BuiltinType::Long &&
3744 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003745 PointeeTy = IntTy;
3746 }
3747 }
3748}
3749
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003750void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003751 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003752 // We follow the behavior of gcc, expanding structures which are
3753 // directly pointed to, and expanding embedded structures. Note that
3754 // these rules are sufficient to prevent recursive encoding of the
3755 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003756 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003757 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003758}
3759
David Chisnallb190a2c2010-06-04 01:10:52 +00003760static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3761 switch (T->getAs<BuiltinType>()->getKind()) {
3762 default: assert(0 && "Unhandled builtin type kind");
3763 case BuiltinType::Void: return 'v';
3764 case BuiltinType::Bool: return 'B';
3765 case BuiltinType::Char_U:
3766 case BuiltinType::UChar: return 'C';
3767 case BuiltinType::UShort: return 'S';
3768 case BuiltinType::UInt: return 'I';
3769 case BuiltinType::ULong:
3770 return
3771 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3772 case BuiltinType::UInt128: return 'T';
3773 case BuiltinType::ULongLong: return 'Q';
3774 case BuiltinType::Char_S:
3775 case BuiltinType::SChar: return 'c';
3776 case BuiltinType::Short: return 's';
Chris Lattnerad3467e2010-12-25 23:25:43 +00003777 case BuiltinType::WChar_S:
3778 case BuiltinType::WChar_U:
David Chisnallb190a2c2010-06-04 01:10:52 +00003779 case BuiltinType::Int: return 'i';
3780 case BuiltinType::Long:
3781 return
3782 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3783 case BuiltinType::LongLong: return 'q';
3784 case BuiltinType::Int128: return 't';
3785 case BuiltinType::Float: return 'f';
3786 case BuiltinType::Double: return 'd';
Daniel Dunbar7cba5a72010-10-11 21:13:48 +00003787 case BuiltinType::LongDouble: return 'D';
David Chisnallb190a2c2010-06-04 01:10:52 +00003788 }
3789}
3790
Mike Stump11289f42009-09-09 15:08:12 +00003791static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00003792 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003793 const Expr *E = FD->getBitWidth();
3794 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3795 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003796 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00003797 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3798 // The GNU runtime requires more information; bitfields are encoded as b,
3799 // then the offset (in bits) of the first element, then the type of the
3800 // bitfield, then the size in bits. For example, in this structure:
3801 //
3802 // struct
3803 // {
3804 // int integer;
3805 // int flags:2;
3806 // };
3807 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3808 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3809 // information is not especially sensible, but we're stuck with it for
3810 // compatibility with GCC, although providing it breaks anything that
3811 // actually uses runtime introspection and wants to work on both runtimes...
3812 if (!Ctx->getLangOptions().NeXTRuntime) {
3813 const RecordDecl *RD = FD->getParent();
3814 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3815 // FIXME: This same linear search is also used in ExprConstant - it might
3816 // be better if the FieldDecl stored its offset. We'd be increasing the
3817 // size of the object slightly, but saving some time every time it is used.
3818 unsigned i = 0;
3819 for (RecordDecl::field_iterator Field = RD->field_begin(),
3820 FieldEnd = RD->field_end();
3821 Field != FieldEnd; (void)++Field, ++i) {
3822 if (*Field == FD)
3823 break;
3824 }
3825 S += llvm::utostr(RL.getFieldOffset(i));
David Chisnall6f0a7d22010-12-26 20:12:30 +00003826 if (T->isEnumeralType())
3827 S += 'i';
3828 else
3829 S += ObjCEncodingForPrimitiveKind(Context, T);
David Chisnallb190a2c2010-06-04 01:10:52 +00003830 }
3831 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003832 S += llvm::utostr(N);
3833}
3834
Daniel Dunbar07d07852009-10-18 21:17:35 +00003835// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003836void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3837 bool ExpandPointedToStructures,
3838 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003839 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003840 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003841 bool EncodingProperty) {
David Chisnallb190a2c2010-06-04 01:10:52 +00003842 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003843 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003844 return EncodeBitField(this, S, T, FD);
3845 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003846 return;
3847 }
Mike Stump11289f42009-09-09 15:08:12 +00003848
John McCall9dd450b2009-09-21 23:43:11 +00003849 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003850 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003851 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003852 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003853 return;
3854 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003855
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003856 // encoding for pointer or r3eference types.
3857 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003858 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003859 if (PT->isObjCSelType()) {
3860 S += ':';
3861 return;
3862 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003863 PointeeTy = PT->getPointeeType();
3864 }
3865 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3866 PointeeTy = RT->getPointeeType();
3867 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003868 bool isReadOnly = false;
3869 // For historical/compatibility reasons, the read-only qualifier of the
3870 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3871 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003872 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003873 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003874 if (OutermostType && T.isConstQualified()) {
3875 isReadOnly = true;
3876 S += 'r';
3877 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003878 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003879 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003880 while (P->getAs<PointerType>())
3881 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003882 if (P.isConstQualified()) {
3883 isReadOnly = true;
3884 S += 'r';
3885 }
3886 }
3887 if (isReadOnly) {
3888 // Another legacy compatibility encoding. Some ObjC qualifier and type
3889 // combinations need to be rearranged.
3890 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003891 if (llvm::StringRef(S).endswith("nr"))
3892 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003893 }
Mike Stump11289f42009-09-09 15:08:12 +00003894
Anders Carlssond8499822007-10-29 05:01:08 +00003895 if (PointeeTy->isCharType()) {
3896 // char pointer types should be encoded as '*' unless it is a
3897 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003898 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003899 S += '*';
3900 return;
3901 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003902 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003903 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3904 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3905 S += '#';
3906 return;
3907 }
3908 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3909 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3910 S += '@';
3911 return;
3912 }
3913 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003914 }
Anders Carlssond8499822007-10-29 05:01:08 +00003915 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003916 getLegacyIntegralTypeEncoding(PointeeTy);
3917
Mike Stump11289f42009-09-09 15:08:12 +00003918 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003919 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003920 return;
3921 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003922
Chris Lattnere7cabb92009-07-13 00:10:46 +00003923 if (const ArrayType *AT =
3924 // Ignore type qualifiers etc.
3925 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003926 if (isa<IncompleteArrayType>(AT)) {
3927 // Incomplete arrays are encoded as a pointer to the array element.
3928 S += '^';
3929
Mike Stump11289f42009-09-09 15:08:12 +00003930 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003931 false, ExpandStructures, FD);
3932 } else {
3933 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003934
Anders Carlssond05f44b2009-02-22 01:38:57 +00003935 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3936 S += llvm::utostr(CAT->getSize().getZExtValue());
3937 else {
3938 //Variable length arrays are encoded as a regular array with 0 elements.
3939 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3940 S += '0';
3941 }
Mike Stump11289f42009-09-09 15:08:12 +00003942
3943 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003944 false, ExpandStructures, FD);
3945 S += ']';
3946 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003947 return;
3948 }
Mike Stump11289f42009-09-09 15:08:12 +00003949
John McCall9dd450b2009-09-21 23:43:11 +00003950 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003951 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003952 return;
3953 }
Mike Stump11289f42009-09-09 15:08:12 +00003954
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003955 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003956 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003957 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003958 // Anonymous structures print as '?'
3959 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3960 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003961 if (ClassTemplateSpecializationDecl *Spec
3962 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3963 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3964 std::string TemplateArgsStr
3965 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003966 TemplateArgs.data(),
3967 TemplateArgs.size(),
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003968 (*this).PrintingPolicy);
3969
3970 S += TemplateArgsStr;
3971 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003972 } else {
3973 S += '?';
3974 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003975 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003976 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003977 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3978 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003979 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003980 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003981 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003982 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003983 S += '"';
3984 }
Mike Stump11289f42009-09-09 15:08:12 +00003985
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003986 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003987 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003988 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003989 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003990 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003991 QualType qt = Field->getType();
3992 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003993 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003994 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003995 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003996 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003997 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003998 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003999 return;
4000 }
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004001
Chris Lattnere7cabb92009-07-13 00:10:46 +00004002 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004003 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00004004 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00004005 else
4006 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004007 return;
4008 }
Mike Stump11289f42009-09-09 15:08:12 +00004009
Chris Lattnere7cabb92009-07-13 00:10:46 +00004010 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00004011 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00004012 return;
4013 }
Mike Stump11289f42009-09-09 15:08:12 +00004014
John McCall8b07ec22010-05-15 11:32:37 +00004015 // Ignore protocol qualifiers when mangling at this level.
4016 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4017 T = OT->getBaseType();
4018
John McCall8ccfcb52009-09-24 19:53:00 +00004019 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004020 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00004021 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004022 S += '{';
4023 const IdentifierInfo *II = OI->getIdentifier();
4024 S += II->getName();
4025 S += '=';
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004026 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
4027 DeepCollectObjCIvars(OI, true, Ivars);
4028 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4029 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4030 if (Field->isBitField())
4031 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004032 else
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00004033 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004034 }
4035 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00004036 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00004037 }
Mike Stump11289f42009-09-09 15:08:12 +00004038
John McCall9dd450b2009-09-21 23:43:11 +00004039 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004040 if (OPT->isObjCIdType()) {
4041 S += '@';
4042 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004043 }
Mike Stump11289f42009-09-09 15:08:12 +00004044
Steve Narofff0c86112009-10-28 22:03:49 +00004045 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4046 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4047 // Since this is a binary compatibility issue, need to consult with runtime
4048 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004049 S += '#';
4050 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004051 }
Mike Stump11289f42009-09-09 15:08:12 +00004052
Chris Lattnere7cabb92009-07-13 00:10:46 +00004053 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00004054 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00004055 ExpandPointedToStructures,
4056 ExpandStructures, FD);
4057 if (FD || EncodingProperty) {
4058 // Note that we do extended encoding of protocol qualifer list
4059 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00004060 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00004061 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4062 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00004063 S += '<';
4064 S += (*I)->getNameAsString();
4065 S += '>';
4066 }
4067 S += '"';
4068 }
4069 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00004070 }
Mike Stump11289f42009-09-09 15:08:12 +00004071
Chris Lattnere7cabb92009-07-13 00:10:46 +00004072 QualType PointeeTy = OPT->getPointeeType();
4073 if (!EncodingProperty &&
4074 isa<TypedefType>(PointeeTy.getTypePtr())) {
4075 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00004076 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00004077 // {...};
4078 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00004079 getObjCEncodingForTypeImpl(PointeeTy, S,
4080 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00004081 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00004082 return;
4083 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004084
4085 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00004086 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004087 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00004088 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00004089 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4090 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00004091 S += '<';
4092 S += (*I)->getNameAsString();
4093 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00004094 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00004095 S += '"';
4096 }
4097 return;
4098 }
Mike Stump11289f42009-09-09 15:08:12 +00004099
John McCalla9e6e8d2010-05-17 23:56:34 +00004100 // gcc just blithely ignores member pointers.
4101 // TODO: maybe there should be a mangling for these
4102 if (T->getAs<MemberPointerType>())
4103 return;
Fariborz Jahaniane0587be2010-10-07 21:25:25 +00004104
4105 if (T->isVectorType()) {
4106 // This matches gcc's encoding, even though technically it is
4107 // insufficient.
4108 // FIXME. We should do a better job than gcc.
4109 return;
4110 }
4111
Chris Lattnere7cabb92009-07-13 00:10:46 +00004112 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00004113}
4114
Mike Stump11289f42009-09-09 15:08:12 +00004115void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00004116 std::string& S) const {
4117 if (QT & Decl::OBJC_TQ_In)
4118 S += 'n';
4119 if (QT & Decl::OBJC_TQ_Inout)
4120 S += 'N';
4121 if (QT & Decl::OBJC_TQ_Out)
4122 S += 'o';
4123 if (QT & Decl::OBJC_TQ_Bycopy)
4124 S += 'O';
4125 if (QT & Decl::OBJC_TQ_Byref)
4126 S += 'R';
4127 if (QT & Decl::OBJC_TQ_Oneway)
4128 S += 'V';
4129}
4130
Chris Lattnere7cabb92009-07-13 00:10:46 +00004131void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00004132 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004133
Anders Carlsson87c149b2007-10-11 01:00:40 +00004134 BuiltinVaListType = T;
4135}
4136
Chris Lattnere7cabb92009-07-13 00:10:46 +00004137void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004138 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00004139}
4140
Chris Lattnere7cabb92009-07-13 00:10:46 +00004141void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00004142 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00004143}
4144
Chris Lattnere7cabb92009-07-13 00:10:46 +00004145void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004146 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00004147}
4148
Chris Lattnere7cabb92009-07-13 00:10:46 +00004149void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004150 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00004151}
4152
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004153void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00004154 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00004155 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00004156
Ted Kremenek1b0ea822008-01-07 19:49:32 +00004157 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00004158}
4159
John McCalld28ae272009-12-02 08:04:21 +00004160/// \brief Retrieve the template name that corresponds to a non-empty
4161/// lookup.
John McCallad371252010-01-20 00:46:10 +00004162TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4163 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00004164 unsigned size = End - Begin;
4165 assert(size > 1 && "set is not overloaded!");
4166
4167 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4168 size * sizeof(FunctionTemplateDecl*));
4169 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4170
4171 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00004172 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00004173 NamedDecl *D = *I;
4174 assert(isa<FunctionTemplateDecl>(D) ||
4175 (isa<UsingShadowDecl>(D) &&
4176 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4177 *Storage++ = D;
4178 }
4179
4180 return TemplateName(OT);
4181}
4182
Douglas Gregordc572a32009-03-30 22:58:21 +00004183/// \brief Retrieve the template name that represents a qualified
4184/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00004185TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00004186 bool TemplateKeyword,
4187 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00004188 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00004189 llvm::FoldingSetNodeID ID;
4190 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4191
4192 void *InsertPos = 0;
4193 QualifiedTemplateName *QTN =
4194 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4195 if (!QTN) {
4196 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4197 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4198 }
4199
4200 return TemplateName(QTN);
4201}
4202
4203/// \brief Retrieve the template name that represents a dependent
4204/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00004205TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00004206 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004207 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00004208 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00004209
4210 llvm::FoldingSetNodeID ID;
4211 DependentTemplateName::Profile(ID, NNS, Name);
4212
4213 void *InsertPos = 0;
4214 DependentTemplateName *QTN =
4215 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4216
4217 if (QTN)
4218 return TemplateName(QTN);
4219
4220 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4221 if (CanonNNS == NNS) {
4222 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4223 } else {
4224 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4225 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004226 DependentTemplateName *CheckQTN =
4227 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4228 assert(!CheckQTN && "Dependent type name canonicalization broken");
4229 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004230 }
4231
4232 DependentTemplateNames.InsertNode(QTN, InsertPos);
4233 return TemplateName(QTN);
4234}
4235
Douglas Gregor71395fa2009-11-04 00:56:37 +00004236/// \brief Retrieve the template name that represents a dependent
4237/// template name such as \c MetaFun::template operator+.
4238TemplateName
4239ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4240 OverloadedOperatorKind Operator) {
4241 assert((!NNS || NNS->isDependent()) &&
4242 "Nested name specifier must be dependent");
4243
4244 llvm::FoldingSetNodeID ID;
4245 DependentTemplateName::Profile(ID, NNS, Operator);
4246
4247 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004248 DependentTemplateName *QTN
4249 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004250
4251 if (QTN)
4252 return TemplateName(QTN);
4253
4254 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4255 if (CanonNNS == NNS) {
4256 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4257 } else {
4258 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4259 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004260
4261 DependentTemplateName *CheckQTN
4262 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4263 assert(!CheckQTN && "Dependent template name canonicalization broken");
4264 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004265 }
4266
4267 DependentTemplateNames.InsertNode(QTN, InsertPos);
4268 return TemplateName(QTN);
4269}
4270
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004271/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004272/// TargetInfo, produce the corresponding type. The unsigned @p Type
4273/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004274CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004275 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004276 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004277 case TargetInfo::SignedShort: return ShortTy;
4278 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4279 case TargetInfo::SignedInt: return IntTy;
4280 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4281 case TargetInfo::SignedLong: return LongTy;
4282 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4283 case TargetInfo::SignedLongLong: return LongLongTy;
4284 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4285 }
4286
4287 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004288 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004289}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004290
4291//===----------------------------------------------------------------------===//
4292// Type Predicates.
4293//===----------------------------------------------------------------------===//
4294
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004295/// isObjCNSObjectType - Return true if this is an NSObject object using
4296/// NSObject attribute on a c-style pointer type.
4297/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004298/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004299///
4300bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4301 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4302 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004303 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004304 return true;
4305 }
Mike Stump11289f42009-09-09 15:08:12 +00004306 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004307}
4308
Fariborz Jahanian96207692009-02-18 21:49:28 +00004309/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4310/// garbage collection attribute.
4311///
John McCall8ccfcb52009-09-24 19:53:00 +00004312Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4313 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004314 if (getLangOptions().ObjC1 &&
4315 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00004316 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00004317 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00004318 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004319 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00004320 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00004321 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004322 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004323 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004324 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004325 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00004326 // Non-pointers have none gc'able attribute regardless of the attribute
4327 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00004328 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004329 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004330 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004331 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004332}
4333
Chris Lattner49af6a42008-04-07 06:51:04 +00004334//===----------------------------------------------------------------------===//
4335// Type Compatibility Testing
4336//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004337
Mike Stump11289f42009-09-09 15:08:12 +00004338/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004339/// compatible.
4340static bool areCompatVectorTypes(const VectorType *LHS,
4341 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004342 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004343 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004344 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004345}
4346
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004347bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4348 QualType SecondVec) {
4349 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4350 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4351
4352 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4353 return true;
4354
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004355 // Treat Neon vector types and most AltiVec vector types as if they are the
4356 // equivalent GCC vector types.
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004357 const VectorType *First = FirstVec->getAs<VectorType>();
4358 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004359 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004360 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsone6aeebb2010-11-12 17:24:54 +00004361 First->getVectorKind() != VectorType::AltiVecPixel &&
4362 First->getVectorKind() != VectorType::AltiVecBool &&
4363 Second->getVectorKind() != VectorType::AltiVecPixel &&
4364 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor59e8b3b2010-08-06 10:14:59 +00004365 return true;
4366
4367 return false;
4368}
4369
Steve Naroff8e6aee52009-07-23 01:01:38 +00004370//===----------------------------------------------------------------------===//
4371// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4372//===----------------------------------------------------------------------===//
4373
4374/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4375/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004376bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4377 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004378 if (lProto == rProto)
4379 return true;
4380 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4381 E = rProto->protocol_end(); PI != E; ++PI)
4382 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4383 return true;
4384 return false;
4385}
4386
Steve Naroff8e6aee52009-07-23 01:01:38 +00004387/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4388/// return true if lhs's protocols conform to rhs's protocol; false
4389/// otherwise.
4390bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4391 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4392 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4393 return false;
4394}
4395
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004396/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4397/// Class<p1, ...>.
4398bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4399 QualType rhs) {
4400 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4401 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4402 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4403
4404 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4405 E = lhsQID->qual_end(); I != E; ++I) {
4406 bool match = false;
4407 ObjCProtocolDecl *lhsProto = *I;
4408 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4409 E = rhsOPT->qual_end(); J != E; ++J) {
4410 ObjCProtocolDecl *rhsProto = *J;
4411 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4412 match = true;
4413 break;
4414 }
4415 }
4416 if (!match)
4417 return false;
4418 }
4419 return true;
4420}
4421
Steve Naroff8e6aee52009-07-23 01:01:38 +00004422/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4423/// ObjCQualifiedIDType.
4424bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4425 bool compare) {
4426 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004427 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004428 lhs->isObjCIdType() || lhs->isObjCClassType())
4429 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004430 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004431 rhs->isObjCIdType() || rhs->isObjCClassType())
4432 return true;
4433
4434 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004435 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004436
Steve Naroff8e6aee52009-07-23 01:01:38 +00004437 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004438
Steve Naroff8e6aee52009-07-23 01:01:38 +00004439 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004440 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004441 // make sure we check the class hierarchy.
4442 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4443 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4444 E = lhsQID->qual_end(); I != E; ++I) {
4445 // when comparing an id<P> on lhs with a static type on rhs,
4446 // see if static class implements all of id's protocols, directly or
4447 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004448 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004449 return false;
4450 }
4451 }
4452 // If there are no qualifiers and no interface, we have an 'id'.
4453 return true;
4454 }
Mike Stump11289f42009-09-09 15:08:12 +00004455 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004456 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4457 E = lhsQID->qual_end(); I != E; ++I) {
4458 ObjCProtocolDecl *lhsProto = *I;
4459 bool match = false;
4460
4461 // when comparing an id<P> on lhs with a static type on rhs,
4462 // see if static class implements all of id's protocols, directly or
4463 // through its super class and categories.
4464 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4465 E = rhsOPT->qual_end(); J != E; ++J) {
4466 ObjCProtocolDecl *rhsProto = *J;
4467 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4468 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4469 match = true;
4470 break;
4471 }
4472 }
Mike Stump11289f42009-09-09 15:08:12 +00004473 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004474 // make sure we check the class hierarchy.
4475 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4476 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4477 E = lhsQID->qual_end(); I != E; ++I) {
4478 // when comparing an id<P> on lhs with a static type on rhs,
4479 // see if static class implements all of id's protocols, directly or
4480 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004481 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004482 match = true;
4483 break;
4484 }
4485 }
4486 }
4487 if (!match)
4488 return false;
4489 }
Mike Stump11289f42009-09-09 15:08:12 +00004490
Steve Naroff8e6aee52009-07-23 01:01:38 +00004491 return true;
4492 }
Mike Stump11289f42009-09-09 15:08:12 +00004493
Steve Naroff8e6aee52009-07-23 01:01:38 +00004494 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4495 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4496
Mike Stump11289f42009-09-09 15:08:12 +00004497 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004498 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004499 // If both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004500 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4501 E = lhsOPT->qual_end(); I != E; ++I) {
4502 ObjCProtocolDecl *lhsProto = *I;
4503 bool match = false;
4504
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004505 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff8e6aee52009-07-23 01:01:38 +00004506 // see if static class implements all of id's protocols, directly or
4507 // through its super class and categories.
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004508 // First, lhs protocols in the qualifier list must be found, direct
4509 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004510 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4511 E = rhsQID->qual_end(); J != E; ++J) {
4512 ObjCProtocolDecl *rhsProto = *J;
4513 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4514 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4515 match = true;
4516 break;
4517 }
4518 }
4519 if (!match)
4520 return false;
4521 }
Fariborz Jahanianeb7714c2010-11-01 20:47:16 +00004522
4523 // Static class's protocols, or its super class or category protocols
4524 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4525 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4526 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4527 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4528 // This is rather dubious but matches gcc's behavior. If lhs has
4529 // no type qualifier and its class has no static protocol(s)
4530 // assume that it is mismatch.
4531 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4532 return false;
4533 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4534 LHSInheritedProtocols.begin(),
4535 E = LHSInheritedProtocols.end(); I != E; ++I) {
4536 bool match = false;
4537 ObjCProtocolDecl *lhsProto = (*I);
4538 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4539 E = rhsQID->qual_end(); J != E; ++J) {
4540 ObjCProtocolDecl *rhsProto = *J;
4541 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4542 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4543 match = true;
4544 break;
4545 }
4546 }
4547 if (!match)
4548 return false;
4549 }
4550 }
Steve Naroff8e6aee52009-07-23 01:01:38 +00004551 return true;
4552 }
4553 return false;
4554}
4555
Eli Friedman47f77112008-08-22 00:56:42 +00004556/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004557/// compatible for assignment from RHS to LHS. This handles validation of any
4558/// protocol qualifiers on the LHS or RHS.
4559///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004560bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4561 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004562 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4563 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4564
Steve Naroff1329fa02009-07-15 18:40:39 +00004565 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004566 if (LHS->isObjCUnqualifiedIdOrClass() ||
4567 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004568 return true;
4569
John McCall8b07ec22010-05-15 11:32:37 +00004570 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004571 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4572 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004573 false);
Fariborz Jahanian3c7ebc32010-07-19 22:02:22 +00004574
4575 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4576 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4577 QualType(RHSOPT,0));
4578
John McCall8b07ec22010-05-15 11:32:37 +00004579 // If we have 2 user-defined types, fall into that path.
4580 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004581 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004582
Steve Naroff8e6aee52009-07-23 01:01:38 +00004583 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004584}
4585
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004586/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4587/// for providing type-safty for objective-c pointers used to pass/return
4588/// arguments in block literals. When passed as arguments, passing 'A*' where
4589/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4590/// not OK. For the return type, the opposite is not OK.
4591bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4592 const ObjCObjectPointerType *LHSOPT,
4593 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004594 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004595 return true;
4596
4597 if (LHSOPT->isObjCBuiltinType()) {
4598 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4599 }
4600
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004601 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004602 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4603 QualType(RHSOPT,0),
4604 false);
4605
4606 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4607 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4608 if (LHS && RHS) { // We have 2 user-defined types.
4609 if (LHS != RHS) {
4610 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4611 return false;
4612 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4613 return true;
4614 }
4615 else
4616 return true;
4617 }
4618 return false;
4619}
4620
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004621/// getIntersectionOfProtocols - This routine finds the intersection of set
4622/// of protocols inherited from two distinct objective-c pointer objects.
4623/// It is used to build composite qualifier list of the composite type of
4624/// the conditional expression involving two objective-c pointer objects.
4625static
4626void getIntersectionOfProtocols(ASTContext &Context,
4627 const ObjCObjectPointerType *LHSOPT,
4628 const ObjCObjectPointerType *RHSOPT,
4629 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4630
John McCall8b07ec22010-05-15 11:32:37 +00004631 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4632 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4633 assert(LHS->getInterface() && "LHS must have an interface base");
4634 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004635
4636 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4637 unsigned LHSNumProtocols = LHS->getNumProtocols();
4638 if (LHSNumProtocols > 0)
4639 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4640 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004641 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004642 Context.CollectInheritedProtocols(LHS->getInterface(),
4643 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004644 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4645 LHSInheritedProtocols.end());
4646 }
4647
4648 unsigned RHSNumProtocols = RHS->getNumProtocols();
4649 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004650 ObjCProtocolDecl **RHSProtocols =
4651 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004652 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4653 if (InheritedProtocolSet.count(RHSProtocols[i]))
4654 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4655 }
4656 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004657 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004658 Context.CollectInheritedProtocols(RHS->getInterface(),
4659 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004660 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4661 RHSInheritedProtocols.begin(),
4662 E = RHSInheritedProtocols.end(); I != E; ++I)
4663 if (InheritedProtocolSet.count((*I)))
4664 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004665 }
4666}
4667
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004668/// areCommonBaseCompatible - Returns common base class of the two classes if
4669/// one found. Note that this is O'2 algorithm. But it will be called as the
4670/// last type comparison in a ?-exp of ObjC pointer types before a
4671/// warning is issued. So, its invokation is extremely rare.
4672QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004673 const ObjCObjectPointerType *Lptr,
4674 const ObjCObjectPointerType *Rptr) {
4675 const ObjCObjectType *LHS = Lptr->getObjectType();
4676 const ObjCObjectType *RHS = Rptr->getObjectType();
4677 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4678 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4679 if (!LDecl || !RDecl)
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004680 return QualType();
4681
John McCall8b07ec22010-05-15 11:32:37 +00004682 while ((LDecl = LDecl->getSuperClass())) {
4683 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004684 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004685 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4686 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4687
4688 QualType Result = QualType(LHS, 0);
4689 if (!Protocols.empty())
4690 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4691 Result = getObjCObjectPointerType(Result);
4692 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004693 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004694 }
4695
4696 return QualType();
4697}
4698
John McCall8b07ec22010-05-15 11:32:37 +00004699bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4700 const ObjCObjectType *RHS) {
4701 assert(LHS->getInterface() && "LHS is not an interface type");
4702 assert(RHS->getInterface() && "RHS is not an interface type");
4703
Chris Lattner49af6a42008-04-07 06:51:04 +00004704 // Verify that the base decls are compatible: the RHS must be a subclass of
4705 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00004706 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00004707 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004708
Chris Lattner49af6a42008-04-07 06:51:04 +00004709 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4710 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004711 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004712 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004713
Chris Lattner49af6a42008-04-07 06:51:04 +00004714 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4715 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004716 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004717 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004718
John McCall8b07ec22010-05-15 11:32:37 +00004719 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4720 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004721 LHSPI != LHSPE; LHSPI++) {
4722 bool RHSImplementsProtocol = false;
4723
4724 // If the RHS doesn't implement the protocol on the left, the types
4725 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00004726 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4727 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004728 RHSPI != RHSPE; RHSPI++) {
4729 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004730 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004731 break;
4732 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004733 }
4734 // FIXME: For better diagnostics, consider passing back the protocol name.
4735 if (!RHSImplementsProtocol)
4736 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004737 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004738 // The RHS implements all protocols listed on the LHS.
4739 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004740}
4741
Steve Naroffb7605152009-02-12 17:52:19 +00004742bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4743 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004744 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4745 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004746
Steve Naroff7cae42b2009-07-10 23:34:53 +00004747 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004748 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004749
4750 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4751 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004752}
4753
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00004754bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
4755 return canAssignObjCInterfaces(
4756 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
4757 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
4758}
4759
Mike Stump11289f42009-09-09 15:08:12 +00004760/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004761/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004762/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004763/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004764bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
4765 bool CompareUnqualified) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004766 if (getLangOptions().CPlusPlus)
4767 return hasSameType(LHS, RHS);
4768
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004769 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman47f77112008-08-22 00:56:42 +00004770}
4771
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004772bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4773 return !mergeTypes(LHS, RHS, true).isNull();
4774}
4775
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004776/// mergeTransparentUnionType - if T is a transparent union type and a member
4777/// of T is compatible with SubType, return the merged type, else return
4778/// QualType()
4779QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
4780 bool OfBlockPointer,
4781 bool Unqualified) {
4782 if (const RecordType *UT = T->getAsUnionType()) {
4783 RecordDecl *UD = UT->getDecl();
4784 if (UD->hasAttr<TransparentUnionAttr>()) {
4785 for (RecordDecl::field_iterator it = UD->field_begin(),
4786 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbourne19b961d2010-12-02 21:00:06 +00004787 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004788 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
4789 if (!MT.isNull())
4790 return MT;
4791 }
4792 }
4793 }
4794
4795 return QualType();
4796}
4797
4798/// mergeFunctionArgumentTypes - merge two types which appear as function
4799/// argument types
4800QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
4801 bool OfBlockPointer,
4802 bool Unqualified) {
4803 // GNU extension: two types are compatible if they appear as a function
4804 // argument, one of the types is a transparent union type and the other
4805 // type is compatible with a union member
4806 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
4807 Unqualified);
4808 if (!lmerge.isNull())
4809 return lmerge;
4810
4811 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
4812 Unqualified);
4813 if (!rmerge.isNull())
4814 return rmerge;
4815
4816 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
4817}
4818
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004819QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004820 bool OfBlockPointer,
4821 bool Unqualified) {
John McCall9dd450b2009-09-21 23:43:11 +00004822 const FunctionType *lbase = lhs->getAs<FunctionType>();
4823 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004824 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4825 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004826 bool allLTypes = true;
4827 bool allRTypes = true;
4828
4829 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004830 QualType retType;
4831 if (OfBlockPointer)
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004832 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true,
4833 Unqualified);
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004834 else
John McCall8c6b56f2010-12-15 01:06:38 +00004835 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
4836 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00004837 if (retType.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004838
4839 if (Unqualified)
4840 retType = retType.getUnqualifiedType();
4841
4842 CanQualType LRetType = getCanonicalType(lbase->getResultType());
4843 CanQualType RRetType = getCanonicalType(rbase->getResultType());
4844 if (Unqualified) {
4845 LRetType = LRetType.getUnqualifiedType();
4846 RRetType = RRetType.getUnqualifiedType();
4847 }
4848
4849 if (getCanonicalType(retType) != LRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00004850 allLTypes = false;
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004851 if (getCanonicalType(retType) != RRetType)
Chris Lattner465fa322008-10-05 17:34:18 +00004852 allRTypes = false;
John McCall8c6b56f2010-12-15 01:06:38 +00004853
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004854 // FIXME: double check this
4855 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4856 // rbase->getRegParmAttr() != 0 &&
4857 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004858 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4859 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
John McCall8c6b56f2010-12-15 01:06:38 +00004860
Douglas Gregor8c940862010-01-18 17:14:39 +00004861 // Compatible functions must have compatible calling conventions
John McCall8c6b56f2010-12-15 01:06:38 +00004862 if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
Douglas Gregor8c940862010-01-18 17:14:39 +00004863 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004864
John McCall8c6b56f2010-12-15 01:06:38 +00004865 // Regparm is part of the calling convention.
4866 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
4867 return QualType();
4868
4869 // It's noreturn if either type is.
4870 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
4871 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4872 if (NoReturn != lbaseInfo.getNoReturn())
4873 allLTypes = false;
4874 if (NoReturn != rbaseInfo.getNoReturn())
4875 allRTypes = false;
4876
4877 FunctionType::ExtInfo einfo(NoReturn,
4878 lbaseInfo.getRegParm(),
4879 lbaseInfo.getCC());
John McCalldb40c7f2010-12-14 08:05:40 +00004880
Eli Friedman47f77112008-08-22 00:56:42 +00004881 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004882 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4883 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004884 unsigned lproto_nargs = lproto->getNumArgs();
4885 unsigned rproto_nargs = rproto->getNumArgs();
4886
4887 // Compatible functions must have the same number of arguments
4888 if (lproto_nargs != rproto_nargs)
4889 return QualType();
4890
4891 // Variadic and non-variadic functions aren't compatible
4892 if (lproto->isVariadic() != rproto->isVariadic())
4893 return QualType();
4894
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004895 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4896 return QualType();
4897
Eli Friedman47f77112008-08-22 00:56:42 +00004898 // Check argument compatibility
4899 llvm::SmallVector<QualType, 10> types;
4900 for (unsigned i = 0; i < lproto_nargs; i++) {
4901 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4902 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbournea99fdcf2010-10-24 18:30:18 +00004903 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
4904 OfBlockPointer,
4905 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00004906 if (argtype.isNull()) return QualType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004907
4908 if (Unqualified)
4909 argtype = argtype.getUnqualifiedType();
4910
Eli Friedman47f77112008-08-22 00:56:42 +00004911 types.push_back(argtype);
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004912 if (Unqualified) {
4913 largtype = largtype.getUnqualifiedType();
4914 rargtype = rargtype.getUnqualifiedType();
4915 }
4916
Chris Lattner465fa322008-10-05 17:34:18 +00004917 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4918 allLTypes = false;
4919 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4920 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004921 }
4922 if (allLTypes) return lhs;
4923 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00004924
4925 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
4926 EPI.ExtInfo = einfo;
4927 return getFunctionType(retType, types.begin(), types.size(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00004928 }
4929
4930 if (lproto) allRTypes = false;
4931 if (rproto) allLTypes = false;
4932
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004933 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004934 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004935 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004936 if (proto->isVariadic()) return QualType();
4937 // Check that the types are compatible with the types that
4938 // would result from default argument promotions (C99 6.7.5.3p15).
4939 // The only types actually affected are promotable integer
4940 // types and floats, which would be passed as a different
4941 // type depending on whether the prototype is visible.
4942 unsigned proto_nargs = proto->getNumArgs();
4943 for (unsigned i = 0; i < proto_nargs; ++i) {
4944 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004945
4946 // Look at the promotion type of enum types, since that is the type used
4947 // to pass enum values.
4948 if (const EnumType *Enum = argTy->getAs<EnumType>())
4949 argTy = Enum->getDecl()->getPromotionType();
4950
Eli Friedman47f77112008-08-22 00:56:42 +00004951 if (argTy->isPromotableIntegerType() ||
4952 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4953 return QualType();
4954 }
4955
4956 if (allLTypes) return lhs;
4957 if (allRTypes) return rhs;
John McCalldb40c7f2010-12-14 08:05:40 +00004958
4959 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
4960 EPI.ExtInfo = einfo;
Eli Friedman47f77112008-08-22 00:56:42 +00004961 return getFunctionType(retType, proto->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00004962 proto->getNumArgs(), EPI);
Eli Friedman47f77112008-08-22 00:56:42 +00004963 }
4964
4965 if (allLTypes) return lhs;
4966 if (allRTypes) return rhs;
John McCall8c6b56f2010-12-15 01:06:38 +00004967 return getFunctionNoProtoType(retType, einfo);
Eli Friedman47f77112008-08-22 00:56:42 +00004968}
4969
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004970QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004971 bool OfBlockPointer,
4972 bool Unqualified) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004973 // C++ [expr]: If an expression initially has the type "reference to T", the
4974 // type is adjusted to "T" prior to any further analysis, the expression
4975 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004976 // expression is an lvalue unless the reference is an rvalue reference and
4977 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004978 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4979 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor17ea3f52010-07-29 15:18:02 +00004980
4981 if (Unqualified) {
4982 LHS = LHS.getUnqualifiedType();
4983 RHS = RHS.getUnqualifiedType();
4984 }
Douglas Gregor21e771e2010-02-03 21:02:30 +00004985
Eli Friedman47f77112008-08-22 00:56:42 +00004986 QualType LHSCan = getCanonicalType(LHS),
4987 RHSCan = getCanonicalType(RHS);
4988
4989 // If two types are identical, they are compatible.
4990 if (LHSCan == RHSCan)
4991 return LHS;
4992
John McCall8ccfcb52009-09-24 19:53:00 +00004993 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004994 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4995 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004996 if (LQuals != RQuals) {
4997 // If any of these qualifiers are different, we have a type
4998 // mismatch.
4999 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5000 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5001 return QualType();
5002
5003 // Exactly one GC qualifier difference is allowed: __strong is
5004 // okay if the other type has no GC qualifier but is an Objective
5005 // C object pointer (i.e. implicitly strong by default). We fix
5006 // this by pretending that the unqualified type was actually
5007 // qualified __strong.
5008 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5009 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5010 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5011
5012 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5013 return QualType();
5014
5015 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
5016 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
5017 }
5018 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
5019 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
5020 }
Eli Friedman47f77112008-08-22 00:56:42 +00005021 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00005022 }
5023
5024 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00005025
Eli Friedmandcca6332009-06-01 01:22:52 +00005026 Type::TypeClass LHSClass = LHSCan->getTypeClass();
5027 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00005028
Chris Lattnerfd652912008-01-14 05:45:46 +00005029 // We want to consider the two function types to be the same for these
5030 // comparisons, just force one to the other.
5031 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5032 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00005033
5034 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00005035 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5036 LHSClass = Type::ConstantArray;
5037 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5038 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00005039
John McCall8b07ec22010-05-15 11:32:37 +00005040 // ObjCInterfaces are just specialized ObjCObjects.
5041 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5042 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5043
Nate Begemance4d7fc2008-04-18 23:10:10 +00005044 // Canonicalize ExtVector -> Vector.
5045 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5046 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00005047
Chris Lattner95554662008-04-07 05:43:21 +00005048 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005049 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00005050 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00005051 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00005052 // Compatibility is based on the underlying type, not the promotion
5053 // type.
John McCall9dd450b2009-09-21 23:43:11 +00005054 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005055 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5056 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005057 }
John McCall9dd450b2009-09-21 23:43:11 +00005058 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00005059 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5060 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00005061 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005062
Eli Friedman47f77112008-08-22 00:56:42 +00005063 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005064 }
Eli Friedman47f77112008-08-22 00:56:42 +00005065
Steve Naroffc6edcbd2008-01-09 22:43:08 +00005066 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00005067 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005068#define TYPE(Class, Base)
5069#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00005070#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005071#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5072#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5073#include "clang/AST/TypeNodes.def"
5074 assert(false && "Non-canonical and dependent types shouldn't get here");
5075 return QualType();
5076
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00005077 case Type::LValueReference:
5078 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005079 case Type::MemberPointer:
5080 assert(false && "C++ should never be in mergeTypes");
5081 return QualType();
5082
John McCall8b07ec22010-05-15 11:32:37 +00005083 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005084 case Type::IncompleteArray:
5085 case Type::VariableArray:
5086 case Type::FunctionProto:
5087 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005088 assert(false && "Types are eliminated above");
5089 return QualType();
5090
Chris Lattnerfd652912008-01-14 05:45:46 +00005091 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00005092 {
5093 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005094 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5095 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005096 if (Unqualified) {
5097 LHSPointee = LHSPointee.getUnqualifiedType();
5098 RHSPointee = RHSPointee.getUnqualifiedType();
5099 }
5100 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5101 Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005102 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00005103 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005104 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00005105 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00005106 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005107 return getPointerType(ResultType);
5108 }
Steve Naroff68e167d2008-12-10 17:49:55 +00005109 case Type::BlockPointer:
5110 {
5111 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00005112 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5113 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005114 if (Unqualified) {
5115 LHSPointee = LHSPointee.getUnqualifiedType();
5116 RHSPointee = RHSPointee.getUnqualifiedType();
5117 }
5118 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5119 Unqualified);
Steve Naroff68e167d2008-12-10 17:49:55 +00005120 if (ResultType.isNull()) return QualType();
5121 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5122 return LHS;
5123 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5124 return RHS;
5125 return getBlockPointerType(ResultType);
5126 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005127 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00005128 {
5129 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5130 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5131 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5132 return QualType();
5133
5134 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5135 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005136 if (Unqualified) {
5137 LHSElem = LHSElem.getUnqualifiedType();
5138 RHSElem = RHSElem.getUnqualifiedType();
5139 }
5140
5141 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman47f77112008-08-22 00:56:42 +00005142 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00005143 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5144 return LHS;
5145 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5146 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00005147 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5148 ArrayType::ArraySizeModifier(), 0);
5149 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5150 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005151 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5152 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00005153 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5154 return LHS;
5155 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5156 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00005157 if (LVAT) {
5158 // FIXME: This isn't correct! But tricky to implement because
5159 // the array's size has to be the size of LHS, but the type
5160 // has to be different.
5161 return LHS;
5162 }
5163 if (RVAT) {
5164 // FIXME: This isn't correct! But tricky to implement because
5165 // the array's size has to be the size of RHS, but the type
5166 // has to be different.
5167 return RHS;
5168 }
Eli Friedman3e62c212008-08-22 01:48:21 +00005169 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5170 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00005171 return getIncompleteArrayType(ResultType,
5172 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00005173 }
Chris Lattnerfd652912008-01-14 05:45:46 +00005174 case Type::FunctionNoProto:
Douglas Gregor17ea3f52010-07-29 15:18:02 +00005175 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005176 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005177 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00005178 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00005179 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005180 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00005181 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00005182 case Type::Complex:
5183 // Distinct complex types are incompatible.
5184 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00005185 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00005186 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00005187 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5188 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00005189 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00005190 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00005191 case Type::ObjCObject: {
5192 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00005193 // FIXME: This should be type compatibility, e.g. whether
5194 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00005195 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5196 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5197 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00005198 return LHS;
5199
Eli Friedman47f77112008-08-22 00:56:42 +00005200 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00005201 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00005202 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005203 if (OfBlockPointer) {
5204 if (canAssignObjCInterfacesInBlockPointer(
5205 LHS->getAs<ObjCObjectPointerType>(),
5206 RHS->getAs<ObjCObjectPointerType>()))
5207 return LHS;
5208 return QualType();
5209 }
John McCall9dd450b2009-09-21 23:43:11 +00005210 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5211 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00005212 return LHS;
5213
Steve Naroffc68cfcf2008-12-10 22:14:21 +00005214 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00005215 }
Steve Naroff32e44c02007-10-15 20:41:53 +00005216 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00005217
5218 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00005219}
Ted Kremenekfc581a92007-10-31 17:10:13 +00005220
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005221/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5222/// 'RHS' attributes and returns the merged version; including for function
5223/// return types.
5224QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5225 QualType LHSCan = getCanonicalType(LHS),
5226 RHSCan = getCanonicalType(RHS);
5227 // If two types are identical, they are compatible.
5228 if (LHSCan == RHSCan)
5229 return LHS;
5230 if (RHSCan->isFunctionType()) {
5231 if (!LHSCan->isFunctionType())
5232 return QualType();
5233 QualType OldReturnType =
5234 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5235 QualType NewReturnType =
5236 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5237 QualType ResReturnType =
5238 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5239 if (ResReturnType.isNull())
5240 return QualType();
5241 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5242 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5243 // In either case, use OldReturnType to build the new function type.
5244 const FunctionType *F = LHS->getAs<FunctionType>();
5245 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
John McCalldb40c7f2010-12-14 08:05:40 +00005246 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5247 EPI.ExtInfo = getFunctionExtInfo(LHS);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005248 QualType ResultType
5249 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
John McCalldb40c7f2010-12-14 08:05:40 +00005250 FPT->getNumArgs(), EPI);
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00005251 return ResultType;
5252 }
5253 }
5254 return QualType();
5255 }
5256
5257 // If the qualifiers are different, the types can still be merged.
5258 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5259 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5260 if (LQuals != RQuals) {
5261 // If any of these qualifiers are different, we have a type mismatch.
5262 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5263 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5264 return QualType();
5265
5266 // Exactly one GC qualifier difference is allowed: __strong is
5267 // okay if the other type has no GC qualifier but is an Objective
5268 // C object pointer (i.e. implicitly strong by default). We fix
5269 // this by pretending that the unqualified type was actually
5270 // qualified __strong.
5271 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5272 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5273 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5274
5275 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5276 return QualType();
5277
5278 if (GC_L == Qualifiers::Strong)
5279 return LHS;
5280 if (GC_R == Qualifiers::Strong)
5281 return RHS;
5282 return QualType();
5283 }
5284
5285 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5286 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5287 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5288 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5289 if (ResQT == LHSBaseQT)
5290 return LHS;
5291 if (ResQT == RHSBaseQT)
5292 return RHS;
5293 }
5294 return QualType();
5295}
5296
Chris Lattner4ba0cef2008-04-07 07:01:58 +00005297//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005298// Integer Predicates
5299//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00005300
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005301unsigned ASTContext::getIntWidth(QualType T) {
John McCall56774992009-12-09 09:09:27 +00005302 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00005303 T = ET->getDecl()->getIntegerType();
Douglas Gregor0bf31402010-10-08 23:50:27 +00005304 if (T->isBooleanType())
5305 return 1;
Eli Friedman1efaaea2009-02-13 02:31:07 +00005306 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005307 return (unsigned)getTypeSize(T);
5308}
5309
5310QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregor5cc2c8b2010-07-23 15:58:24 +00005311 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00005312
5313 // Turn <4 x signed int> -> <4 x unsigned int>
5314 if (const VectorType *VTy = T->getAs<VectorType>())
5315 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsonaeb56442010-11-10 21:56:12 +00005316 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattnerec3a1562009-10-17 20:33:28 +00005317
5318 // For enums, we return the unsigned version of the base type.
5319 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005320 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00005321
5322 const BuiltinType *BTy = T->getAs<BuiltinType>();
5323 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005324 switch (BTy->getKind()) {
5325 case BuiltinType::Char_S:
5326 case BuiltinType::SChar:
5327 return UnsignedCharTy;
5328 case BuiltinType::Short:
5329 return UnsignedShortTy;
5330 case BuiltinType::Int:
5331 return UnsignedIntTy;
5332 case BuiltinType::Long:
5333 return UnsignedLongTy;
5334 case BuiltinType::LongLong:
5335 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00005336 case BuiltinType::Int128:
5337 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00005338 default:
5339 assert(0 && "Unexpected signed integer type");
5340 return QualType();
5341 }
5342}
5343
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005344ExternalASTSource::~ExternalASTSource() { }
5345
5346void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00005347
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005348ASTMutationListener::~ASTMutationListener() { }
5349
Chris Lattnerecd79c62009-06-14 00:45:47 +00005350
5351//===----------------------------------------------------------------------===//
5352// Builtin Type Computation
5353//===----------------------------------------------------------------------===//
5354
5355/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattnerdc226c22010-10-01 22:42:38 +00005356/// pointer over the consumed characters. This returns the resultant type. If
5357/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5358/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5359/// a vector of "i*".
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005360///
5361/// RequiresICE is filled in on return to indicate whether the value is required
5362/// to be an Integer Constant Expression.
Mike Stump11289f42009-09-09 15:08:12 +00005363static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00005364 ASTContext::GetBuiltinTypeError &Error,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005365 bool &RequiresICE,
Chris Lattnerdc226c22010-10-01 22:42:38 +00005366 bool AllowTypeModifiers) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005367 // Modifiers.
5368 int HowLong = 0;
5369 bool Signed = false, Unsigned = false;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005370 RequiresICE = false;
Chris Lattner84733392010-10-01 07:13:18 +00005371
Chris Lattnerdc226c22010-10-01 22:42:38 +00005372 // Read the prefixed modifiers first.
Chris Lattnerecd79c62009-06-14 00:45:47 +00005373 bool Done = false;
5374 while (!Done) {
5375 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00005376 default: Done = true; --Str; break;
Chris Lattner84733392010-10-01 07:13:18 +00005377 case 'I':
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005378 RequiresICE = true;
Chris Lattner84733392010-10-01 07:13:18 +00005379 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005380 case 'S':
5381 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5382 assert(!Signed && "Can't use 'S' modifier multiple times!");
5383 Signed = true;
5384 break;
5385 case 'U':
5386 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5387 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5388 Unsigned = true;
5389 break;
5390 case 'L':
5391 assert(HowLong <= 2 && "Can't have LLLL modifier");
5392 ++HowLong;
5393 break;
5394 }
5395 }
5396
5397 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005398
Chris Lattnerecd79c62009-06-14 00:45:47 +00005399 // Read the base type.
5400 switch (*Str++) {
5401 default: assert(0 && "Unknown builtin type letter!");
5402 case 'v':
5403 assert(HowLong == 0 && !Signed && !Unsigned &&
5404 "Bad modifiers used with 'v'!");
5405 Type = Context.VoidTy;
5406 break;
5407 case 'f':
5408 assert(HowLong == 0 && !Signed && !Unsigned &&
5409 "Bad modifiers used with 'f'!");
5410 Type = Context.FloatTy;
5411 break;
5412 case 'd':
5413 assert(HowLong < 2 && !Signed && !Unsigned &&
5414 "Bad modifiers used with 'd'!");
5415 if (HowLong)
5416 Type = Context.LongDoubleTy;
5417 else
5418 Type = Context.DoubleTy;
5419 break;
5420 case 's':
5421 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5422 if (Unsigned)
5423 Type = Context.UnsignedShortTy;
5424 else
5425 Type = Context.ShortTy;
5426 break;
5427 case 'i':
5428 if (HowLong == 3)
5429 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5430 else if (HowLong == 2)
5431 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5432 else if (HowLong == 1)
5433 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5434 else
5435 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5436 break;
5437 case 'c':
5438 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5439 if (Signed)
5440 Type = Context.SignedCharTy;
5441 else if (Unsigned)
5442 Type = Context.UnsignedCharTy;
5443 else
5444 Type = Context.CharTy;
5445 break;
5446 case 'b': // boolean
5447 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5448 Type = Context.BoolTy;
5449 break;
5450 case 'z': // size_t.
5451 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5452 Type = Context.getSizeType();
5453 break;
5454 case 'F':
5455 Type = Context.getCFConstantStringType();
5456 break;
Fariborz Jahaniand11da7e2010-11-09 21:38:20 +00005457 case 'G':
5458 Type = Context.getObjCIdType();
5459 break;
5460 case 'H':
5461 Type = Context.getObjCSelType();
5462 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005463 case 'a':
5464 Type = Context.getBuiltinVaListType();
5465 assert(!Type.isNull() && "builtin va list type not initialized!");
5466 break;
5467 case 'A':
5468 // This is a "reference" to a va_list; however, what exactly
5469 // this means depends on how va_list is defined. There are two
5470 // different kinds of va_list: ones passed by value, and ones
5471 // passed by reference. An example of a by-value va_list is
5472 // x86, where va_list is a char*. An example of by-ref va_list
5473 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5474 // we want this argument to be a char*&; for x86-64, we want
5475 // it to be a __va_list_tag*.
5476 Type = Context.getBuiltinVaListType();
5477 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005478 if (Type->isArrayType())
Chris Lattnerecd79c62009-06-14 00:45:47 +00005479 Type = Context.getArrayDecayedType(Type);
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005480 else
Chris Lattnerecd79c62009-06-14 00:45:47 +00005481 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005482 break;
5483 case 'V': {
5484 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005485 unsigned NumElements = strtoul(Str, &End, 10);
5486 assert(End != Str && "Missing vector size");
Chris Lattnerecd79c62009-06-14 00:45:47 +00005487 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005488
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005489 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5490 RequiresICE, false);
5491 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattnerdc226c22010-10-01 22:42:38 +00005492
5493 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner37141f42010-06-23 06:00:24 +00005494 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00005495 VectorType::GenericVector);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005496 break;
5497 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005498 case 'X': {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005499 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5500 false);
5501 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005502 Type = Context.getComplexType(ElementType);
5503 break;
5504 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005505 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005506 Type = Context.getFILEType();
5507 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005508 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005509 return QualType();
5510 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005511 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005512 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005513 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005514 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005515 else
5516 Type = Context.getjmp_bufType();
5517
Mike Stump2adb4da2009-07-28 23:47:15 +00005518 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005519 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005520 return QualType();
5521 }
5522 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005523 }
Mike Stump11289f42009-09-09 15:08:12 +00005524
Chris Lattnerdc226c22010-10-01 22:42:38 +00005525 // If there are modifiers and if we're allowed to parse them, go for it.
5526 Done = !AllowTypeModifiers;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005527 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005528 switch (char c = *Str++) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005529 default: Done = true; --Str; break;
5530 case '*':
5531 case '&': {
5532 // Both pointers and references can have their pointee types
5533 // qualified with an address space.
5534 char *End;
5535 unsigned AddrSpace = strtoul(Str, &End, 10);
5536 if (End != Str && AddrSpace != 0) {
5537 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5538 Str = End;
5539 }
5540 if (c == '*')
5541 Type = Context.getPointerType(Type);
5542 else
5543 Type = Context.getLValueReferenceType(Type);
5544 break;
5545 }
5546 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5547 case 'C':
5548 Type = Type.withConst();
5549 break;
5550 case 'D':
5551 Type = Context.getVolatileType(Type);
5552 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005553 }
5554 }
Chris Lattner84733392010-10-01 07:13:18 +00005555
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005556 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner84733392010-10-01 07:13:18 +00005557 "Integer constant 'I' type must be an integer");
Mike Stump11289f42009-09-09 15:08:12 +00005558
Chris Lattnerecd79c62009-06-14 00:45:47 +00005559 return Type;
5560}
5561
5562/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattnerdc226c22010-10-01 22:42:38 +00005563QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005564 GetBuiltinTypeError &Error,
5565 unsigned *IntegerConstantArgs) {
Chris Lattnerdc226c22010-10-01 22:42:38 +00005566 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump11289f42009-09-09 15:08:12 +00005567
Chris Lattnerecd79c62009-06-14 00:45:47 +00005568 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005569
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005570 bool RequiresICE = false;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005571 Error = GE_None;
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005572 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5573 RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005574 if (Error != GE_None)
5575 return QualType();
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005576
5577 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5578
Chris Lattnerecd79c62009-06-14 00:45:47 +00005579 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005580 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005581 if (Error != GE_None)
5582 return QualType();
5583
Chris Lattnerbd6e6932010-10-01 22:53:11 +00005584 // If this argument is required to be an IntegerConstantExpression and the
5585 // caller cares, fill in the bitmask we return.
5586 if (RequiresICE && IntegerConstantArgs)
5587 *IntegerConstantArgs |= 1 << ArgTypes.size();
5588
Chris Lattnerecd79c62009-06-14 00:45:47 +00005589 // Do array -> pointer decay. The builtin should use the decayed type.
5590 if (Ty->isArrayType())
5591 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005592
Chris Lattnerecd79c62009-06-14 00:45:47 +00005593 ArgTypes.push_back(Ty);
5594 }
5595
5596 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5597 "'.' should only occur at end of builtin type list!");
5598
John McCall991eb4b2010-12-21 00:44:39 +00005599 FunctionType::ExtInfo EI;
5600 if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
5601
5602 bool Variadic = (TypeStr[0] == '.');
5603
5604 // We really shouldn't be making a no-proto type here, especially in C++.
5605 if (ArgTypes.empty() && Variadic)
5606 return getFunctionNoProtoType(ResType, EI);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005607
John McCalldb40c7f2010-12-14 08:05:40 +00005608 FunctionProtoType::ExtProtoInfo EPI;
John McCall991eb4b2010-12-21 00:44:39 +00005609 EPI.ExtInfo = EI;
5610 EPI.Variadic = Variadic;
John McCalldb40c7f2010-12-14 08:05:40 +00005611
5612 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005613}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005614
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005615GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5616 GVALinkage External = GVA_StrongExternal;
5617
5618 Linkage L = FD->getLinkage();
5619 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5620 FD->getType()->getLinkage() == UniqueExternalLinkage)
5621 L = UniqueExternalLinkage;
5622
5623 switch (L) {
5624 case NoLinkage:
5625 case InternalLinkage:
5626 case UniqueExternalLinkage:
5627 return GVA_Internal;
5628
5629 case ExternalLinkage:
5630 switch (FD->getTemplateSpecializationKind()) {
5631 case TSK_Undeclared:
5632 case TSK_ExplicitSpecialization:
5633 External = GVA_StrongExternal;
5634 break;
5635
5636 case TSK_ExplicitInstantiationDefinition:
5637 return GVA_ExplicitTemplateInstantiation;
5638
5639 case TSK_ExplicitInstantiationDeclaration:
5640 case TSK_ImplicitInstantiation:
5641 External = GVA_TemplateInstantiation;
5642 break;
5643 }
5644 }
5645
5646 if (!FD->isInlined())
5647 return External;
5648
5649 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5650 // GNU or C99 inline semantics. Determine whether this symbol should be
5651 // externally visible.
5652 if (FD->isInlineDefinitionExternallyVisible())
5653 return External;
5654
5655 // C99 inline semantics, where the symbol is not externally visible.
5656 return GVA_C99Inline;
5657 }
5658
5659 // C++0x [temp.explicit]p9:
5660 // [ Note: The intent is that an inline function that is the subject of
5661 // an explicit instantiation declaration will still be implicitly
5662 // instantiated when used so that the body can be considered for
5663 // inlining, but that no out-of-line copy of the inline function would be
5664 // generated in the translation unit. -- end note ]
5665 if (FD->getTemplateSpecializationKind()
5666 == TSK_ExplicitInstantiationDeclaration)
5667 return GVA_C99Inline;
5668
5669 return GVA_CXXInline;
5670}
5671
5672GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
5673 // If this is a static data member, compute the kind of template
5674 // specialization. Otherwise, this variable is not part of a
5675 // template.
5676 TemplateSpecializationKind TSK = TSK_Undeclared;
5677 if (VD->isStaticDataMember())
5678 TSK = VD->getTemplateSpecializationKind();
5679
5680 Linkage L = VD->getLinkage();
5681 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5682 VD->getType()->getLinkage() == UniqueExternalLinkage)
5683 L = UniqueExternalLinkage;
5684
5685 switch (L) {
5686 case NoLinkage:
5687 case InternalLinkage:
5688 case UniqueExternalLinkage:
5689 return GVA_Internal;
5690
5691 case ExternalLinkage:
5692 switch (TSK) {
5693 case TSK_Undeclared:
5694 case TSK_ExplicitSpecialization:
5695 return GVA_StrongExternal;
5696
5697 case TSK_ExplicitInstantiationDeclaration:
5698 llvm_unreachable("Variable should not be instantiated");
5699 // Fall through to treat this like any other instantiation.
5700
5701 case TSK_ExplicitInstantiationDefinition:
5702 return GVA_ExplicitTemplateInstantiation;
5703
5704 case TSK_ImplicitInstantiation:
5705 return GVA_TemplateInstantiation;
5706 }
5707 }
5708
5709 return GVA_StrongExternal;
5710}
5711
Argyrios Kyrtzidisc9049332010-07-29 20:08:05 +00005712bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005713 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
5714 if (!VD->isFileVarDecl())
5715 return false;
5716 } else if (!isa<FunctionDecl>(D))
5717 return false;
5718
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00005719 // Weak references don't produce any output by themselves.
5720 if (D->hasAttr<WeakRefAttr>())
5721 return false;
5722
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005723 // Aliases and used decls are required.
5724 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
5725 return true;
5726
5727 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5728 // Forward declarations aren't required.
5729 if (!FD->isThisDeclarationADefinition())
5730 return false;
5731
5732 // Constructors and destructors are required.
5733 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
5734 return true;
5735
5736 // The key function for a class is required.
5737 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5738 const CXXRecordDecl *RD = MD->getParent();
5739 if (MD->isOutOfLine() && RD->isDynamicClass()) {
5740 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
5741 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
5742 return true;
5743 }
5744 }
5745
5746 GVALinkage Linkage = GetGVALinkageForFunction(FD);
5747
5748 // static, static inline, always_inline, and extern inline functions can
5749 // always be deferred. Normal inline functions can be deferred in C99/C++.
5750 // Implicit template instantiations can also be deferred in C++.
5751 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
5752 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
5753 return false;
5754 return true;
5755 }
5756
5757 const VarDecl *VD = cast<VarDecl>(D);
5758 assert(VD->isFileVarDecl() && "Expected file scoped var");
5759
Argyrios Kyrtzidis6e03a742010-07-29 20:07:52 +00005760 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
5761 return false;
5762
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005763 // Structs that have non-trivial constructors or destructors are required.
5764
5765 // FIXME: Handle references.
5766 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
5767 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidis1b30d9c2010-08-15 01:15:20 +00005768 if (RD->hasDefinition() &&
5769 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidisc81af032010-07-29 18:15:58 +00005770 return true;
5771 }
5772 }
5773
5774 GVALinkage L = GetGVALinkageForVariable(VD);
5775 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
5776 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
5777 return false;
5778 }
5779
5780 return true;
5781}
Charles Davis53c59df2010-08-16 03:33:14 +00005782
Charles Davis99202b32010-11-09 18:04:24 +00005783CallingConv ASTContext::getDefaultMethodCallConv() {
5784 // Pass through to the C++ ABI object
5785 return ABI->getDefaultMethodCallConv();
5786}
5787
Anders Carlsson60a62632010-11-25 01:51:53 +00005788bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) {
5789 // Pass through to the C++ ABI object
5790 return ABI->isNearlyEmpty(RD);
5791}
5792
Charles Davis53c59df2010-08-16 03:33:14 +00005793CXXABI::~CXXABI() {}