blob: ecba4a136fc8149caf20e9fde61cbc3479bf9581 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyckbdc601b2009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
John McCallea1471e2010-05-20 01:18:31 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000023#include "clang/AST/ASTMutationListener.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000026#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000028#include "llvm/ADT/SmallString.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000029#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000030#include "llvm/Support/MathExtras.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000031#include "llvm/Support/raw_ostream.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000032#include "CXXABI.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000033
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Douglas Gregor18274032010-07-03 00:47:00 +000036unsigned ASTContext::NumImplicitDefaultConstructors;
37unsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
Douglas Gregor22584312010-07-02 23:41:54 +000038unsigned ASTContext::NumImplicitCopyConstructors;
39unsigned ASTContext::NumImplicitCopyConstructorsDeclared;
Douglas Gregora376d102010-07-02 21:50:04 +000040unsigned ASTContext::NumImplicitCopyAssignmentOperators;
41unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
Douglas Gregor4923aa22010-07-02 20:37:36 +000042unsigned ASTContext::NumImplicitDestructors;
43unsigned ASTContext::NumImplicitDestructorsDeclared;
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045enum FloatingRank {
46 FloatRank, DoubleRank, LongDoubleRank
47};
48
Douglas Gregor3e1274f2010-06-16 21:09:37 +000049void
50ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
51 TemplateTemplateParmDecl *Parm) {
52 ID.AddInteger(Parm->getDepth());
53 ID.AddInteger(Parm->getPosition());
54 // FIXME: Parameter pack
55
56 TemplateParameterList *Params = Parm->getTemplateParameters();
57 ID.AddInteger(Params->size());
58 for (TemplateParameterList::const_iterator P = Params->begin(),
59 PEnd = Params->end();
60 P != PEnd; ++P) {
61 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
62 ID.AddInteger(0);
63 ID.AddBoolean(TTP->isParameterPack());
64 continue;
65 }
66
67 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
68 ID.AddInteger(1);
69 // FIXME: Parameter pack
70 ID.AddPointer(NTTP->getType().getAsOpaquePtr());
71 continue;
72 }
73
74 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
75 ID.AddInteger(2);
76 Profile(ID, TTP);
77 }
78}
79
80TemplateTemplateParmDecl *
81ASTContext::getCanonicalTemplateTemplateParmDecl(
82 TemplateTemplateParmDecl *TTP) {
83 // Check if we already have a canonical template template parameter.
84 llvm::FoldingSetNodeID ID;
85 CanonicalTemplateTemplateParm::Profile(ID, TTP);
86 void *InsertPos = 0;
87 CanonicalTemplateTemplateParm *Canonical
88 = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
89 if (Canonical)
90 return Canonical->getParam();
91
92 // Build a canonical template parameter list.
93 TemplateParameterList *Params = TTP->getTemplateParameters();
94 llvm::SmallVector<NamedDecl *, 4> CanonParams;
95 CanonParams.reserve(Params->size());
96 for (TemplateParameterList::const_iterator P = Params->begin(),
97 PEnd = Params->end();
98 P != PEnd; ++P) {
99 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
100 CanonParams.push_back(
101 TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
102 SourceLocation(), TTP->getDepth(),
103 TTP->getIndex(), 0, false,
104 TTP->isParameterPack()));
105 else if (NonTypeTemplateParmDecl *NTTP
106 = dyn_cast<NonTypeTemplateParmDecl>(*P))
107 CanonParams.push_back(
108 NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
109 SourceLocation(), NTTP->getDepth(),
110 NTTP->getPosition(), 0,
111 getCanonicalType(NTTP->getType()),
112 0));
113 else
114 CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
115 cast<TemplateTemplateParmDecl>(*P)));
116 }
117
118 TemplateTemplateParmDecl *CanonTTP
119 = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
120 SourceLocation(), TTP->getDepth(),
121 TTP->getPosition(), 0,
122 TemplateParameterList::Create(*this, SourceLocation(),
123 SourceLocation(),
124 CanonParams.data(),
125 CanonParams.size(),
126 SourceLocation()));
127
128 // Get the new insert position for the node we care about.
129 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
130 assert(Canonical == 0 && "Shouldn't be in the map!");
131 (void)Canonical;
132
133 // Create the canonical template template parameter entry.
134 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
135 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
136 return CanonTTP;
137}
138
Charles Davis071cc7d2010-08-16 03:33:14 +0000139CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
John McCallee79a4c2010-08-21 22:46:04 +0000140 if (!LangOpts.CPlusPlus) return 0;
141
Charles Davis20cf7172010-08-19 02:18:14 +0000142 switch (T.getCXXABI()) {
John McCallee79a4c2010-08-21 22:46:04 +0000143 case CXXABI_ARM:
144 return CreateARMCXXABI(*this);
145 case CXXABI_Itanium:
Charles Davis071cc7d2010-08-16 03:33:14 +0000146 return CreateItaniumCXXABI(*this);
Charles Davis20cf7172010-08-19 02:18:14 +0000147 case CXXABI_Microsoft:
148 return CreateMicrosoftCXXABI(*this);
149 }
John McCallee79a4c2010-08-21 22:46:04 +0000150 return 0;
Charles Davis071cc7d2010-08-16 03:33:14 +0000151}
152
Chris Lattner61710852008-10-05 17:34:18 +0000153ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar444be732009-11-13 05:51:54 +0000154 const TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +0000155 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +0000156 Builtin::Context &builtins,
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000157 unsigned size_reserve) :
John McCallef990012010-06-11 11:07:21 +0000158 TemplateSpecializationTypes(this_()),
159 DependentTemplateSpecializationTypes(this_()),
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +0000160 GlobalNestedNameSpecifier(0), IsInt128Installed(false),
161 CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +0000162 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump083c25e2009-10-22 00:49:09 +0000163 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
John McCallbf1a0282010-06-04 23:28:52 +0000164 NullTypeSourceInfo(QualType()),
Charles Davis071cc7d2010-08-16 03:33:14 +0000165 SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +0000166 Idents(idents), Selectors(sels),
Ted Kremenekac9590e2010-05-10 20:40:08 +0000167 BuiltinInfo(builtins),
168 DeclarationNames(*this),
Argyrios Kyrtzidis9a44b5f2010-10-28 09:29:35 +0000169 ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
Ted Kremenekf057bf72010-06-18 00:31:04 +0000170 LastSDM(0, 0),
171 UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) {
David Chisnall0f436562009-08-17 16:35:33 +0000172 ObjCIdRedefinitionType = QualType();
173 ObjCClassRedefinitionType = QualType();
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000174 ObjCSelRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000175 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +0000176 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +0000177 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +0000178}
179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180ASTContext::~ASTContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000181 // Release the DenseMaps associated with DeclContext objects.
182 // FIXME: Is this the ideal solution?
183 ReleaseDeclContextMaps();
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000184
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000185 // Call all of the deallocation functions.
186 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
187 Deallocations[I].first(Deallocations[I].second);
Douglas Gregor00545312010-05-23 18:26:36 +0000188
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000189 // Release all of the memory associated with overridden C++ methods.
190 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
191 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
192 OM != OMEnd; ++OM)
193 OM->second.Destroy();
Ted Kremenek3478eb62010-02-11 07:12:28 +0000194
Ted Kremenekdcfcfbe2010-06-08 23:00:58 +0000195 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000196 // because they can contain DenseMaps.
197 for (llvm::DenseMap<const ObjCContainerDecl*,
198 const ASTRecordLayout*>::iterator
199 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
200 // Increment in loop to prevent using deallocated memory.
201 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
202 R->Destroy(*this);
203
Ted Kremenekdcfcfbe2010-06-08 23:00:58 +0000204 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
205 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
206 // Increment in loop to prevent using deallocated memory.
207 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
208 R->Destroy(*this);
209 }
Douglas Gregor63200642010-08-30 16:49:28 +0000210
211 for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
212 AEnd = DeclAttrs.end();
213 A != AEnd; ++A)
214 A->second->~AttrVec();
215}
Douglas Gregorab452ba2009-03-26 23:50:42 +0000216
Douglas Gregor00545312010-05-23 18:26:36 +0000217void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
218 Deallocations.push_back(std::make_pair(Callback, Data));
219}
220
Mike Stump1eb44332009-09-09 15:08:12 +0000221void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
223 ExternalSource.reset(Source.take());
224}
225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226void ASTContext::PrintStats() const {
227 fprintf(stderr, "*** AST Context Stats:\n");
228 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000229
Douglas Gregordbe833d2009-05-26 14:40:08 +0000230 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000231#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000232#define ABSTRACT_TYPE(Name, Parent)
233#include "clang/AST/TypeNodes.def"
234 0 // Extra
235 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
238 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000239 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 }
241
Douglas Gregordbe833d2009-05-26 14:40:08 +0000242 unsigned Idx = 0;
243 unsigned TotalBytes = 0;
244#define TYPE(Name, Parent) \
245 if (counts[Idx]) \
246 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
247 TotalBytes += counts[Idx] * sizeof(Name##Type); \
248 ++Idx;
249#define ABSTRACT_TYPE(Name, Parent)
250#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Douglas Gregordbe833d2009-05-26 14:40:08 +0000252 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregored8abf12010-07-08 06:14:04 +0000253
Douglas Gregor4923aa22010-07-02 20:37:36 +0000254 // Implicit special member functions.
Douglas Gregor18274032010-07-03 00:47:00 +0000255 fprintf(stderr, " %u/%u implicit default constructors created\n",
256 NumImplicitDefaultConstructorsDeclared,
257 NumImplicitDefaultConstructors);
Douglas Gregor22584312010-07-02 23:41:54 +0000258 fprintf(stderr, " %u/%u implicit copy constructors created\n",
259 NumImplicitCopyConstructorsDeclared,
260 NumImplicitCopyConstructors);
Douglas Gregora376d102010-07-02 21:50:04 +0000261 fprintf(stderr, " %u/%u implicit copy assignment operators created\n",
262 NumImplicitCopyAssignmentOperatorsDeclared,
263 NumImplicitCopyAssignmentOperators);
Douglas Gregor4923aa22010-07-02 20:37:36 +0000264 fprintf(stderr, " %u/%u implicit destructors created\n",
265 NumImplicitDestructorsDeclared, NumImplicitDestructors);
266
Douglas Gregor2cf26342009-04-09 22:27:44 +0000267 if (ExternalSource.get()) {
268 fprintf(stderr, "\n");
269 ExternalSource->PrintStats();
270 }
Douglas Gregored8abf12010-07-08 06:14:04 +0000271
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000272 BumpAlloc.PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
275
John McCalle27ec8a2009-10-23 23:03:21 +0000276void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000277 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000278 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000279 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282void ASTContext::InitBuiltinTypes() {
283 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 // C99 6.2.5p19.
286 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // C99 6.2.5p2.
289 InitBuiltinType(BoolTy, BuiltinType::Bool);
290 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000291 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 InitBuiltinType(CharTy, BuiltinType::Char_S);
293 else
294 InitBuiltinType(CharTy, BuiltinType::Char_U);
295 // C99 6.2.5p4.
296 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
297 InitBuiltinType(ShortTy, BuiltinType::Short);
298 InitBuiltinType(IntTy, BuiltinType::Int);
299 InitBuiltinType(LongTy, BuiltinType::Long);
300 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // C99 6.2.5p6.
303 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
304 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
305 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
306 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
307 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 // C99 6.2.5p10.
310 InitBuiltinType(FloatTy, BuiltinType::Float);
311 InitBuiltinType(DoubleTy, BuiltinType::Double);
312 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000313
Chris Lattner2df9ced2009-04-30 02:43:43 +0000314 // GNU extension, 128-bit integers.
315 InitBuiltinType(Int128Ty, BuiltinType::Int128);
316 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
317
Chris Lattner3a250322009-02-26 23:43:47 +0000318 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
319 InitBuiltinType(WCharTy, BuiltinType::WChar);
320 else // C99
321 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000322
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000323 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
324 InitBuiltinType(Char16Ty, BuiltinType::Char16);
325 else // C99
326 Char16Ty = getFromTargetType(Target.getChar16Type());
327
328 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
329 InitBuiltinType(Char32Ty, BuiltinType::Char32);
330 else // C99
331 Char32Ty = getFromTargetType(Target.getChar32Type());
332
Douglas Gregor898574e2008-12-05 23:32:09 +0000333 // Placeholder type for type-dependent expressions whose type is
334 // completely unknown. No code should ever check a type against
335 // DependentTy and users should never see it; however, it is here to
336 // help diagnose failures to properly check for type-dependent
337 // expressions.
338 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000339
John McCall2a984ca2010-10-12 00:20:44 +0000340 // Placeholder type for functions.
341 InitBuiltinType(OverloadTy, BuiltinType::Overload);
342
Mike Stump1eb44332009-09-09 15:08:12 +0000343 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000344 // not yet been deduced.
John McCall2a984ca2010-10-12 00:20:44 +0000345 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 // C99 6.2.5p11.
348 FloatComplexTy = getComplexType(FloatTy);
349 DoubleComplexTy = getComplexType(DoubleTy);
350 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000351
Steve Naroff7e219e42007-10-15 14:41:52 +0000352 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Steve Naroffde2e22d2009-07-15 18:40:39 +0000354 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
355 ObjCIdTypedefType = QualType();
356 ObjCClassTypedefType = QualType();
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000357 ObjCSelTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000359 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroffde2e22d2009-07-15 18:40:39 +0000360 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
361 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000362 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff14108da2009-07-10 23:34:53 +0000363
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000364 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000366 // void * type
367 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000368
369 // nullptr type (C++0x 2.14.7)
370 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000371}
372
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000373Diagnostic &ASTContext::getDiagnostics() const {
374 return SourceMgr.getDiagnostics();
375}
376
Douglas Gregor63200642010-08-30 16:49:28 +0000377AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
378 AttrVec *&Result = DeclAttrs[D];
379 if (!Result) {
380 void *Mem = Allocate(sizeof(AttrVec));
381 Result = new (Mem) AttrVec;
382 }
383
384 return *Result;
385}
386
387/// \brief Erase the attributes corresponding to the given declaration.
388void ASTContext::eraseDeclAttrs(const Decl *D) {
389 llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
390 if (Pos != DeclAttrs.end()) {
391 Pos->second->~AttrVec();
392 DeclAttrs.erase(Pos);
393 }
394}
395
396
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000397MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000398ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000399 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000400 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000401 = InstantiatedFromStaticDataMember.find(Var);
402 if (Pos == InstantiatedFromStaticDataMember.end())
403 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Douglas Gregor7caa6822009-07-24 20:34:43 +0000405 return Pos->second;
406}
407
Mike Stump1eb44332009-09-09 15:08:12 +0000408void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000409ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000410 TemplateSpecializationKind TSK,
411 SourceLocation PointOfInstantiation) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000412 assert(Inst->isStaticDataMember() && "Not a static data member");
413 assert(Tmpl->isStaticDataMember() && "Not a static data member");
414 assert(!InstantiatedFromStaticDataMember[Inst] &&
415 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000416 InstantiatedFromStaticDataMember[Inst]
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000417 = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000418}
419
John McCall7ba107a2009-11-18 02:36:19 +0000420NamedDecl *
John McCalled976492009-12-04 22:46:56 +0000421ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCall7ba107a2009-11-18 02:36:19 +0000422 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCalled976492009-12-04 22:46:56 +0000423 = InstantiatedFromUsingDecl.find(UUD);
424 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson0d8df782009-08-29 19:37:28 +0000425 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlsson0d8df782009-08-29 19:37:28 +0000427 return Pos->second;
428}
429
430void
John McCalled976492009-12-04 22:46:56 +0000431ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
432 assert((isa<UsingDecl>(Pattern) ||
433 isa<UnresolvedUsingValueDecl>(Pattern) ||
434 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
435 "pattern decl is not a using decl");
436 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
437 InstantiatedFromUsingDecl[Inst] = Pattern;
438}
439
440UsingShadowDecl *
441ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
442 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
443 = InstantiatedFromUsingShadowDecl.find(Inst);
444 if (Pos == InstantiatedFromUsingShadowDecl.end())
445 return 0;
446
447 return Pos->second;
448}
449
450void
451ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
452 UsingShadowDecl *Pattern) {
453 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
454 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +0000455}
456
Anders Carlssond8b285f2009-09-01 04:26:58 +0000457FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
458 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
459 = InstantiatedFromUnnamedFieldDecl.find(Field);
460 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
461 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anders Carlssond8b285f2009-09-01 04:26:58 +0000463 return Pos->second;
464}
465
466void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
467 FieldDecl *Tmpl) {
468 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
469 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
470 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
471 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Anders Carlssond8b285f2009-09-01 04:26:58 +0000473 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
474}
475
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000476ASTContext::overridden_cxx_method_iterator
477ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
478 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
479 = OverriddenMethods.find(Method);
480 if (Pos == OverriddenMethods.end())
481 return 0;
482
483 return Pos->second.begin();
484}
485
486ASTContext::overridden_cxx_method_iterator
487ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
488 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
489 = OverriddenMethods.find(Method);
490 if (Pos == OverriddenMethods.end())
491 return 0;
492
493 return Pos->second.end();
494}
495
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000496unsigned
497ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
498 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
499 = OverriddenMethods.find(Method);
500 if (Pos == OverriddenMethods.end())
501 return 0;
502
503 return Pos->second.size();
504}
505
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000506void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
507 const CXXMethodDecl *Overridden) {
508 OverriddenMethods[Method].push_back(Overridden);
509}
510
Chris Lattner464175b2007-07-18 17:52:12 +0000511//===----------------------------------------------------------------------===//
512// Type Sizing and Analysis
513//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000514
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000515/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
516/// scalar floating point type.
517const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000518 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000519 assert(BT && "Not a floating point type!");
520 switch (BT->getKind()) {
521 default: assert(0 && "Not a floating point type!");
522 case BuiltinType::Float: return Target.getFloatFormat();
523 case BuiltinType::Double: return Target.getDoubleFormat();
524 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
525 }
526}
527
Ken Dyck8b752f12010-01-27 17:10:57 +0000528/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000529/// specified decl. Note that bitfields do not have a valid alignment, so
530/// this method will assert on them.
Sebastian Redl5d484e82009-11-23 17:18:46 +0000531/// If @p RefAsPointee, references are treated like their underlying type
532/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck8b752f12010-01-27 17:10:57 +0000533CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000534 unsigned Align = Target.getCharWidth();
535
John McCall4081a5c2010-10-08 18:24:19 +0000536 bool UseAlignAttrOnly = false;
537 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
538 Align = AlignFromAttr;
Eli Friedmandcdafb62009-02-22 02:56:25 +0000539
John McCall4081a5c2010-10-08 18:24:19 +0000540 // __attribute__((aligned)) can increase or decrease alignment
541 // *except* on a struct or struct member, where it only increases
542 // alignment unless 'packed' is also specified.
543 //
544 // It is an error for [[align]] to decrease alignment, so we can
545 // ignore that possibility; Sema should diagnose it.
546 if (isa<FieldDecl>(D)) {
547 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
548 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
549 } else {
550 UseAlignAttrOnly = true;
551 }
552 }
553
554 if (UseAlignAttrOnly) {
555 // ignore type of value
556 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneraf707ab2009-01-24 21:53:27 +0000557 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000558 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl5d484e82009-11-23 17:18:46 +0000559 if (RefAsPointee)
560 T = RT->getPointeeType();
561 else
562 T = getPointerType(RT->getPointeeType());
563 }
564 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindola6deecb02010-06-04 23:15:27 +0000565 unsigned MinWidth = Target.getLargeArrayMinWidth();
566 unsigned ArrayAlign = Target.getLargeArrayAlign();
567 if (isa<VariableArrayType>(T) && MinWidth != 0)
568 Align = std::max(Align, ArrayAlign);
569 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
570 unsigned Size = getTypeSize(CT);
571 if (MinWidth != 0 && MinWidth <= Size)
572 Align = std::max(Align, ArrayAlign);
573 }
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000574 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000575 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
576 T = cast<ArrayType>(T)->getElementType();
577
578 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
579 }
Charles Davis05f62472010-02-23 04:52:00 +0000580 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
581 // In the case of a field in a packed struct, we want the minimum
582 // of the alignment of the field and the alignment of the struct.
583 Align = std::min(Align,
584 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
585 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000586 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000587
Ken Dyck8b752f12010-01-27 17:10:57 +0000588 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000589}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000590
John McCallea1471e2010-05-20 01:18:31 +0000591std::pair<CharUnits, CharUnits>
592ASTContext::getTypeInfoInChars(const Type *T) {
593 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
594 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
595 CharUnits::fromQuantity(Info.second / getCharWidth()));
596}
597
598std::pair<CharUnits, CharUnits>
599ASTContext::getTypeInfoInChars(QualType T) {
600 return getTypeInfoInChars(T.getTypePtr());
601}
602
Chris Lattnera7674d82007-07-13 22:13:22 +0000603/// getTypeSize - Return the size of the specified type, in bits. This method
604/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000605///
606/// FIXME: Pointers into different addr spaces could have different sizes and
607/// alignment requirements: getPointerInfo should take an AddrSpace, this
608/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000609std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000610ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000611 uint64_t Width=0;
612 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000613 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000614#define TYPE(Class, Base)
615#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000616#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000617#define DEPENDENT_TYPE(Class, Base) case Type::Class:
618#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000619 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000620 break;
621
Chris Lattner692233e2007-07-13 22:27:08 +0000622 case Type::FunctionNoProto:
623 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000624 // GCC extension: alignof(function) = 32 bits
625 Width = 0;
626 Align = 32;
627 break;
628
Douglas Gregor72564e72009-02-26 23:50:07 +0000629 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000630 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000631 Width = 0;
632 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
633 break;
634
Steve Narofffb22d962007-08-30 01:06:46 +0000635 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000636 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Chris Lattner98be4942008-03-05 18:54:05 +0000638 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000639 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000640 Align = EltInfo.second;
641 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000642 }
Nate Begeman213541a2008-04-18 23:10:10 +0000643 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000644 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000645 const VectorType *VT = cast<VectorType>(T);
646 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
647 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000648 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000649 // If the alignment is not a power of 2, round up to the next power of 2.
650 // This happens for non-power-of-2 length vectors.
Dan Gohman8eefcd32010-04-21 23:32:43 +0000651 if (Align & (Align-1)) {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000652 Align = llvm::NextPowerOf2(Align);
653 Width = llvm::RoundUpToAlignment(Width, Align);
654 }
Chris Lattner030d8842007-07-19 22:06:24 +0000655 break;
656 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000657
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000658 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000659 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000660 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000661 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000662 // GCC extension: alignof(void) = 8 bits.
663 Width = 0;
664 Align = 8;
665 break;
666
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000667 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000668 Width = Target.getBoolWidth();
669 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000670 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000671 case BuiltinType::Char_S:
672 case BuiltinType::Char_U:
673 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000674 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000675 Width = Target.getCharWidth();
676 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000677 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000678 case BuiltinType::WChar:
679 Width = Target.getWCharWidth();
680 Align = Target.getWCharAlign();
681 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000682 case BuiltinType::Char16:
683 Width = Target.getChar16Width();
684 Align = Target.getChar16Align();
685 break;
686 case BuiltinType::Char32:
687 Width = Target.getChar32Width();
688 Align = Target.getChar32Align();
689 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000690 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000691 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000692 Width = Target.getShortWidth();
693 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000694 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000695 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000696 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000697 Width = Target.getIntWidth();
698 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000699 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000700 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000701 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000702 Width = Target.getLongWidth();
703 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000704 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000705 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000706 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000707 Width = Target.getLongLongWidth();
708 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000709 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000710 case BuiltinType::Int128:
711 case BuiltinType::UInt128:
712 Width = 128;
713 Align = 128; // int128_t is 128-bit aligned on all targets.
714 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000715 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000716 Width = Target.getFloatWidth();
717 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000718 break;
719 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000720 Width = Target.getDoubleWidth();
721 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000722 break;
723 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000724 Width = Target.getLongDoubleWidth();
725 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000726 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000727 case BuiltinType::NullPtr:
728 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
729 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000730 break;
Fariborz Jahaniane04f5fc2010-08-02 18:03:20 +0000731 case BuiltinType::ObjCId:
732 case BuiltinType::ObjCClass:
733 case BuiltinType::ObjCSel:
734 Width = Target.getPointerWidth(0);
735 Align = Target.getPointerAlign(0);
736 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000737 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000738 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000739 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000740 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000741 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000742 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000743 case Type::BlockPointer: {
744 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
745 Width = Target.getPointerWidth(AS);
746 Align = Target.getPointerAlign(AS);
747 break;
748 }
Sebastian Redl5d484e82009-11-23 17:18:46 +0000749 case Type::LValueReference:
750 case Type::RValueReference: {
751 // alignof and sizeof should never enter this code path here, so we go
752 // the pointer route.
753 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
754 Width = Target.getPointerWidth(AS);
755 Align = Target.getPointerAlign(AS);
756 break;
757 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000758 case Type::Pointer: {
759 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000760 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000761 Align = Target.getPointerAlign(AS);
762 break;
763 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000764 case Type::MemberPointer: {
Charles Davis071cc7d2010-08-16 03:33:14 +0000765 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000766 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000767 getTypeInfo(getPointerDiffType());
Charles Davis071cc7d2010-08-16 03:33:14 +0000768 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000769 Align = PtrDiffInfo.second;
770 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000771 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000772 case Type::Complex: {
773 // Complex types have the same alignment as their elements, but twice the
774 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000775 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000776 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000777 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000778 Align = EltInfo.second;
779 break;
780 }
John McCallc12c5bb2010-05-15 11:32:37 +0000781 case Type::ObjCObject:
782 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Patel44a3dde2008-06-04 21:54:36 +0000783 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000784 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000785 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
786 Width = Layout.getSize();
787 Align = Layout.getAlignment();
788 break;
789 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000790 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000791 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000792 const TagType *TT = cast<TagType>(T);
793
794 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000795 Width = 1;
796 Align = 1;
797 break;
798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Daniel Dunbar1d751182008-11-08 05:48:37 +0000800 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000801 return getTypeInfo(ET->getDecl()->getIntegerType());
802
Daniel Dunbar1d751182008-11-08 05:48:37 +0000803 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000804 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
805 Width = Layout.getSize();
806 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000807 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000808 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000809
Chris Lattner9fcfe922009-10-22 05:17:15 +0000810 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000811 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
812 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000813
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000814 case Type::Paren:
815 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
816
Douglas Gregor18857642009-04-30 17:32:17 +0000817 case Type::Typedef: {
818 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregordf1367a2010-08-27 00:11:28 +0000819 std::pair<uint64_t, unsigned> Info
820 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
821 Align = std::max(Typedef->getMaxAlignment(), Info.second);
822 Width = Info.first;
Douglas Gregor7532dc62009-03-30 22:58:21 +0000823 break;
Chris Lattner71763312008-04-06 22:05:18 +0000824 }
Douglas Gregor18857642009-04-30 17:32:17 +0000825
826 case Type::TypeOfExpr:
827 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
828 .getTypePtr());
829
830 case Type::TypeOf:
831 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
832
Anders Carlsson395b4752009-06-24 19:06:50 +0000833 case Type::Decltype:
834 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
835 .getTypePtr());
836
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000837 case Type::Elaborated:
838 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregor18857642009-04-30 17:32:17 +0000840 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000841 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000842 "Cannot request the size of a dependent type");
843 // FIXME: this is likely to be wrong once we support template
844 // aliases, since a template alias could refer to a typedef that
845 // has an __aligned__ attribute on it.
846 return getTypeInfo(getCanonicalType(T));
847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Chris Lattner464175b2007-07-18 17:52:12 +0000849 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000850 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000851}
852
Ken Dyckbdc601b2009-12-22 14:23:30 +0000853/// getTypeSizeInChars - Return the size of the specified type, in characters.
854/// This method does not work on incomplete types.
855CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000856 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000857}
858CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000859 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000860}
861
Ken Dyck16e20cc2010-01-26 17:25:18 +0000862/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck86fa4312010-01-26 17:22:55 +0000863/// characters. This method does not work on incomplete types.
864CharUnits ASTContext::getTypeAlignInChars(QualType T) {
865 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
866}
867CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
868 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
869}
870
Chris Lattner34ebde42009-01-27 18:08:34 +0000871/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
872/// type for the current target in bits. This can be different than the ABI
873/// alignment in cases where it is beneficial for performance to overalign
874/// a data type.
875unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
876 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000877
878 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000879 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000880 T = CT->getElementType().getTypePtr();
881 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
882 T->isSpecificBuiltinType(BuiltinType::LongLong))
883 return std::max(ABIAlign, (unsigned)getTypeSize(T));
884
Chris Lattner34ebde42009-01-27 18:08:34 +0000885 return ABIAlign;
886}
887
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000888/// ShallowCollectObjCIvars -
889/// Collect all ivars, including those synthesized, in the current class.
890///
891void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000892 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000893 // FIXME. This need be removed but there are two many places which
894 // assume const-ness of ObjCInterfaceDecl
895 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
896 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
897 Iv= Iv->getNextIvar())
898 Ivars.push_back(Iv);
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000899}
900
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000901/// DeepCollectObjCIvars -
902/// This routine first collects all declared, but not synthesized, ivars in
903/// super class and then collects all ivars, including those synthesized for
904/// current class. This routine is used for implementation of current class
905/// when all ivars, declared and synthesized are known.
Fariborz Jahanian98200742009-05-12 18:14:29 +0000906///
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000907void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
908 bool leafClass,
Fariborz Jahanian98200742009-05-12 18:14:29 +0000909 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000910 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
911 DeepCollectObjCIvars(SuperClass, false, Ivars);
912 if (!leafClass) {
913 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
914 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000915 Ivars.push_back(*I);
916 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000917 else
918 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +0000919}
920
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000921/// CollectInheritedProtocols - Collect all protocols in current class and
922/// those inherited by it.
923void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000924 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000925 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000926 // We can use protocol_iterator here instead of
927 // all_referenced_protocol_iterator since we are walking all categories.
928 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
929 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000930 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000931 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000932 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000933 PE = Proto->protocol_end(); P != PE; ++P) {
934 Protocols.insert(*P);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000935 CollectInheritedProtocols(*P, Protocols);
936 }
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000937 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000938
939 // Categories of this Interface.
940 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
941 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
942 CollectInheritedProtocols(CDeclChain, Protocols);
943 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
944 while (SD) {
945 CollectInheritedProtocols(SD, Protocols);
946 SD = SD->getSuperClass();
947 }
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000948 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000949 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000950 PE = OC->protocol_end(); P != PE; ++P) {
951 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000952 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000953 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
954 PE = Proto->protocol_end(); P != PE; ++P)
955 CollectInheritedProtocols(*P, Protocols);
956 }
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000957 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000958 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
959 PE = OP->protocol_end(); P != PE; ++P) {
960 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000961 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000962 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
963 PE = Proto->protocol_end(); P != PE; ++P)
964 CollectInheritedProtocols(*P, Protocols);
965 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000966 }
967}
968
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000969unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
970 unsigned count = 0;
971 // Count ivars declared in class extension.
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000972 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
973 CDecl = CDecl->getNextClassExtension())
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000974 count += CDecl->ivar_size();
975
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000976 // Count ivar defined in this class's implementation. This
977 // includes synthesized ivars.
978 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000979 count += ImplDecl->ivar_size();
980
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000981 return count;
982}
983
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000984/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
985ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
986 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
987 I = ObjCImpls.find(D);
988 if (I != ObjCImpls.end())
989 return cast<ObjCImplementationDecl>(I->second);
990 return 0;
991}
992/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
993ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
994 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
995 I = ObjCImpls.find(D);
996 if (I != ObjCImpls.end())
997 return cast<ObjCCategoryImplDecl>(I->second);
998 return 0;
999}
1000
1001/// \brief Set the implementation of ObjCInterfaceDecl.
1002void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1003 ObjCImplementationDecl *ImplD) {
1004 assert(IFaceD && ImplD && "Passed null params");
1005 ObjCImpls[IFaceD] = ImplD;
1006}
1007/// \brief Set the implementation of ObjCCategoryDecl.
1008void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1009 ObjCCategoryImplDecl *ImplD) {
1010 assert(CatD && ImplD && "Passed null params");
1011 ObjCImpls[CatD] = ImplD;
1012}
1013
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001014/// \brief Get the copy initialization expression of VarDecl,or NULL if
1015/// none exists.
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001016Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001017 assert(VD && "Passed null params");
1018 assert(VD->hasAttr<BlocksAttr>() &&
1019 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001020 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001021 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001022 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1023}
1024
1025/// \brief Set the copy inialization expression of a block var decl.
1026void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1027 assert(VD && Init && "Passed null params");
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001028 assert(VD->hasAttr<BlocksAttr>() &&
1029 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001030 BlockVarCopyInits[VD] = Init;
1031}
1032
John McCalla93c9342009-12-07 02:54:59 +00001033/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001034///
John McCalla93c9342009-12-07 02:54:59 +00001035/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001036/// the TypeLoc wrappers.
1037///
1038/// \param T the type that will be the basis for type source info. This type
1039/// should refer to how the declarator was written in source code, not to
1040/// what type semantic analysis resolved the declarator to.
John McCalla93c9342009-12-07 02:54:59 +00001041TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall109de5e2009-10-21 00:23:54 +00001042 unsigned DataSize) {
1043 if (!DataSize)
1044 DataSize = TypeLoc::getFullDataSizeForType(T);
1045 else
1046 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCalla93c9342009-12-07 02:54:59 +00001047 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall109de5e2009-10-21 00:23:54 +00001048
John McCalla93c9342009-12-07 02:54:59 +00001049 TypeSourceInfo *TInfo =
1050 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1051 new (TInfo) TypeSourceInfo(T);
1052 return TInfo;
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001053}
1054
John McCalla93c9342009-12-07 02:54:59 +00001055TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCalla4eb74d2009-10-23 21:14:09 +00001056 SourceLocation L) {
John McCalla93c9342009-12-07 02:54:59 +00001057 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCalla4eb74d2009-10-23 21:14:09 +00001058 DI->getTypeLoc().initialize(L);
1059 return DI;
1060}
1061
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001062const ASTRecordLayout &
1063ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1064 return getObjCLayout(D, 0);
1065}
1066
1067const ASTRecordLayout &
1068ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1069 return getObjCLayout(D->getClassInterface(), D);
1070}
1071
Chris Lattnera7674d82007-07-13 22:13:22 +00001072//===----------------------------------------------------------------------===//
1073// Type creation/memoization methods
1074//===----------------------------------------------------------------------===//
1075
John McCall0953e762009-09-24 19:53:00 +00001076QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1077 unsigned Fast = Quals.getFastQualifiers();
1078 Quals.removeFastQualifiers();
1079
1080 // Check if we've already instantiated this type.
1081 llvm::FoldingSetNodeID ID;
1082 ExtQuals::Profile(ID, TypeNode, Quals);
1083 void *InsertPos = 0;
1084 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1085 assert(EQ->getQualifiers() == Quals);
1086 QualType T = QualType(EQ, Fast);
1087 return T;
1088 }
1089
John McCall49f4e1c2010-12-10 11:01:00 +00001090 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001091 ExtQualNodes.InsertNode(New, InsertPos);
1092 QualType T = QualType(New, Fast);
1093 return T;
1094}
1095
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001096QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001097 QualType CanT = getCanonicalType(T);
1098 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001099 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001100
John McCall0953e762009-09-24 19:53:00 +00001101 // If we are composing extended qualifiers together, merge together
1102 // into one ExtQuals node.
1103 QualifierCollector Quals;
1104 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001105
John McCall0953e762009-09-24 19:53:00 +00001106 // If this type already has an address space specified, it cannot get
1107 // another one.
1108 assert(!Quals.hasAddressSpace() &&
1109 "Type cannot be in multiple addr spaces!");
1110 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001111
John McCall0953e762009-09-24 19:53:00 +00001112 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001113}
1114
Chris Lattnerb7d25532009-02-18 22:53:11 +00001115QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001116 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001117 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001118 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001119 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001121 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001122 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001123 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001124 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1125 return getPointerType(ResultType);
1126 }
1127 }
Mike Stump1eb44332009-09-09 15:08:12 +00001128
John McCall0953e762009-09-24 19:53:00 +00001129 // If we are composing extended qualifiers together, merge together
1130 // into one ExtQuals node.
1131 QualifierCollector Quals;
1132 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001133
John McCall0953e762009-09-24 19:53:00 +00001134 // If this type already has an ObjCGC specified, it cannot get
1135 // another one.
1136 assert(!Quals.hasObjCGCAttr() &&
1137 "Type cannot have multiple ObjCGCs!");
1138 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
John McCall0953e762009-09-24 19:53:00 +00001140 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001141}
Chris Lattnera7674d82007-07-13 22:13:22 +00001142
Rafael Espindola264ba482010-03-30 20:24:48 +00001143static QualType getExtFunctionType(ASTContext& Context, QualType T,
1144 const FunctionType::ExtInfo &Info) {
John McCall0953e762009-09-24 19:53:00 +00001145 QualType ResultType;
Douglas Gregor43c79c22009-12-09 00:47:37 +00001146 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1147 QualType Pointee = Pointer->getPointeeType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001148 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001149 if (ResultType == Pointee)
1150 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001151
1152 ResultType = Context.getPointerType(ResultType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001153 } else if (const ParenType *Paren = T->getAs<ParenType>()) {
1154 QualType Inner = Paren->getInnerType();
1155 ResultType = getExtFunctionType(Context, Inner, Info);
1156 if (ResultType == Inner)
1157 return T;
1158
1159 ResultType = Context.getParenType(ResultType);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001160 } else if (const BlockPointerType *BlockPointer
1161 = T->getAs<BlockPointerType>()) {
1162 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001163 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001164 if (ResultType == Pointee)
1165 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001166
1167 ResultType = Context.getBlockPointerType(ResultType);
Douglas Gregorafac01d2010-09-01 16:29:03 +00001168 } else if (const MemberPointerType *MemberPointer
1169 = T->getAs<MemberPointerType>()) {
1170 QualType Pointee = MemberPointer->getPointeeType();
1171 ResultType = getExtFunctionType(Context, Pointee, Info);
1172 if (ResultType == Pointee)
1173 return T;
1174
1175 ResultType = Context.getMemberPointerType(ResultType,
1176 MemberPointer->getClass());
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001177 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindola264ba482010-03-30 20:24:48 +00001178 if (F->getExtInfo() == Info)
Douglas Gregor43c79c22009-12-09 00:47:37 +00001179 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001180
Douglas Gregor43c79c22009-12-09 00:47:37 +00001181 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001182 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001183 Info);
John McCall0953e762009-09-24 19:53:00 +00001184 } else {
Douglas Gregor43c79c22009-12-09 00:47:37 +00001185 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall0953e762009-09-24 19:53:00 +00001186 ResultType
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001187 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1188 FPT->getNumArgs(), FPT->isVariadic(),
1189 FPT->getTypeQuals(),
1190 FPT->hasExceptionSpec(),
1191 FPT->hasAnyExceptionSpec(),
1192 FPT->getNumExceptions(),
1193 FPT->exception_begin(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001194 Info);
John McCall0953e762009-09-24 19:53:00 +00001195 }
Douglas Gregor43c79c22009-12-09 00:47:37 +00001196 } else
1197 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001198
1199 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1200}
1201
1202QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola425ef722010-03-30 22:15:11 +00001203 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindola264ba482010-03-30 20:24:48 +00001204 return getExtFunctionType(*this, T,
1205 Info.withNoReturn(AddNoReturn));
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001206}
1207
1208QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola425ef722010-03-30 22:15:11 +00001209 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindola264ba482010-03-30 20:24:48 +00001210 return getExtFunctionType(*this, T,
1211 Info.withCallingConv(CallConv));
Mike Stump24556362009-07-25 21:26:53 +00001212}
1213
Rafael Espindola425ef722010-03-30 22:15:11 +00001214QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1215 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1216 return getExtFunctionType(*this, T,
1217 Info.withRegParm(RegParm));
1218}
1219
Reid Spencer5f016e22007-07-11 17:01:13 +00001220/// getComplexType - Return the uniqued reference to the type for a complex
1221/// number with the specified element type.
1222QualType ASTContext::getComplexType(QualType T) {
1223 // Unique pointers, to guarantee there is only one pointer of a particular
1224 // structure.
1225 llvm::FoldingSetNodeID ID;
1226 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 void *InsertPos = 0;
1229 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1230 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // If the pointee type isn't canonical, this won't be a canonical type either,
1233 // so fill in the canonical type field.
1234 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001235 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001236 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 // Get the new insert position for the node we care about.
1239 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001240 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 }
John McCall6b304a02009-09-24 23:30:46 +00001242 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 Types.push_back(New);
1244 ComplexTypes.InsertNode(New, InsertPos);
1245 return QualType(New, 0);
1246}
1247
Reid Spencer5f016e22007-07-11 17:01:13 +00001248/// getPointerType - Return the uniqued reference to the type for a pointer to
1249/// the specified type.
1250QualType ASTContext::getPointerType(QualType T) {
1251 // Unique pointers, to guarantee there is only one pointer of a particular
1252 // structure.
1253 llvm::FoldingSetNodeID ID;
1254 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 void *InsertPos = 0;
1257 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1258 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 // If the pointee type isn't canonical, this won't be a canonical type either,
1261 // so fill in the canonical type field.
1262 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001263 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001264 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 // Get the new insert position for the node we care about.
1267 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001268 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 }
John McCall6b304a02009-09-24 23:30:46 +00001270 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 Types.push_back(New);
1272 PointerTypes.InsertNode(New, InsertPos);
1273 return QualType(New, 0);
1274}
1275
Mike Stump1eb44332009-09-09 15:08:12 +00001276/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001277/// a pointer to the specified block.
1278QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001279 assert(T->isFunctionType() && "block of function types only");
1280 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001281 // structure.
1282 llvm::FoldingSetNodeID ID;
1283 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Steve Naroff5618bd42008-08-27 16:04:49 +00001285 void *InsertPos = 0;
1286 if (BlockPointerType *PT =
1287 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1288 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001289
1290 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001291 // type either so fill in the canonical type field.
1292 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001293 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001294 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Steve Naroff5618bd42008-08-27 16:04:49 +00001296 // Get the new insert position for the node we care about.
1297 BlockPointerType *NewIP =
1298 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001299 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001300 }
John McCall6b304a02009-09-24 23:30:46 +00001301 BlockPointerType *New
1302 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001303 Types.push_back(New);
1304 BlockPointerTypes.InsertNode(New, InsertPos);
1305 return QualType(New, 0);
1306}
1307
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001308/// getLValueReferenceType - Return the uniqued reference to the type for an
1309/// lvalue reference to the specified type.
John McCall54e14c42009-10-22 22:37:11 +00001310QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 // Unique pointers, to guarantee there is only one pointer of a particular
1312 // structure.
1313 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001314 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001315
1316 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001317 if (LValueReferenceType *RT =
1318 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001320
John McCall54e14c42009-10-22 22:37:11 +00001321 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1322
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 // If the referencee type isn't canonical, this won't be a canonical type
1324 // either, so fill in the canonical type field.
1325 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001326 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1327 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1328 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001329
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001331 LValueReferenceType *NewIP =
1332 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001333 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 }
1335
John McCall6b304a02009-09-24 23:30:46 +00001336 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001337 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1338 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001339 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001340 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001341
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001342 return QualType(New, 0);
1343}
1344
1345/// getRValueReferenceType - Return the uniqued reference to the type for an
1346/// rvalue reference to the specified type.
1347QualType ASTContext::getRValueReferenceType(QualType T) {
1348 // Unique pointers, to guarantee there is only one pointer of a particular
1349 // structure.
1350 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001351 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001352
1353 void *InsertPos = 0;
1354 if (RValueReferenceType *RT =
1355 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1356 return QualType(RT, 0);
1357
John McCall54e14c42009-10-22 22:37:11 +00001358 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1359
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001360 // If the referencee type isn't canonical, this won't be a canonical type
1361 // either, so fill in the canonical type field.
1362 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001363 if (InnerRef || !T.isCanonical()) {
1364 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1365 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001366
1367 // Get the new insert position for the node we care about.
1368 RValueReferenceType *NewIP =
1369 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1370 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1371 }
1372
John McCall6b304a02009-09-24 23:30:46 +00001373 RValueReferenceType *New
1374 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001375 Types.push_back(New);
1376 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 return QualType(New, 0);
1378}
1379
Sebastian Redlf30208a2009-01-24 21:16:55 +00001380/// getMemberPointerType - Return the uniqued reference to the type for a
1381/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001382QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001383 // Unique pointers, to guarantee there is only one pointer of a particular
1384 // structure.
1385 llvm::FoldingSetNodeID ID;
1386 MemberPointerType::Profile(ID, T, Cls);
1387
1388 void *InsertPos = 0;
1389 if (MemberPointerType *PT =
1390 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1391 return QualType(PT, 0);
1392
1393 // If the pointee or class type isn't canonical, this won't be a canonical
1394 // type either, so fill in the canonical type field.
1395 QualType Canonical;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001396 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001397 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1398
1399 // Get the new insert position for the node we care about.
1400 MemberPointerType *NewIP =
1401 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1402 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1403 }
John McCall6b304a02009-09-24 23:30:46 +00001404 MemberPointerType *New
1405 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001406 Types.push_back(New);
1407 MemberPointerTypes.InsertNode(New, InsertPos);
1408 return QualType(New, 0);
1409}
1410
Mike Stump1eb44332009-09-09 15:08:12 +00001411/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001412/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001413QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001414 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001415 ArrayType::ArraySizeModifier ASM,
1416 unsigned EltTypeQuals) {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001417 assert((EltTy->isDependentType() ||
1418 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedman587cbdf2009-05-29 20:17:55 +00001419 "Constant array of VLAs is illegal!");
1420
Chris Lattner38aeec72009-05-13 04:12:56 +00001421 // Convert the array size into a canonical width matching the pointer size for
1422 // the target.
1423 llvm::APInt ArySize(ArySizeIn);
Jay Foad9f71a8f2010-12-07 08:25:34 +00001424 ArySize =
1425 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001428 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001431 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001432 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001433 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 // If the element type isn't canonical, this won't be a canonical type either,
1436 // so fill in the canonical type field.
1437 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001438 if (!EltTy.isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001439 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001440 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001442 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001443 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001444 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001445 }
Mike Stump1eb44332009-09-09 15:08:12 +00001446
John McCall6b304a02009-09-24 23:30:46 +00001447 ConstantArrayType *New = new(*this,TypeAlignment)
1448 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001449 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 Types.push_back(New);
1451 return QualType(New, 0);
1452}
1453
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00001454/// getIncompleteArrayType - Returns a unique reference to the type for a
1455/// incomplete array of the specified element type.
1456QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) {
1457 QualType ElemTy = getBaseElementType(Ty);
1458 DeclarationName Name;
1459 llvm::SmallVector<QualType, 8> ATypes;
1460 QualType ATy = Ty;
1461 while (const ArrayType *AT = getAsArrayType(ATy)) {
1462 ATypes.push_back(ATy);
1463 ATy = AT->getElementType();
1464 }
1465 for (int i = ATypes.size() - 1; i >= 0; i--) {
1466 if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) {
1467 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star,
1468 0, VAT->getBracketsRange());
1469 }
1470 else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) {
1471 llvm::APSInt ConstVal(CAT->getSize());
1472 ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0);
1473 }
1474 else if (getAsIncompleteArrayType(ATypes[i])) {
1475 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal,
1476 0, SourceRange());
1477 }
1478 else
1479 assert(false && "DependentArrayType is seen");
1480 }
1481 return ElemTy;
1482}
1483
1484/// getVariableArrayDecayedType - Returns a vla type where known sizes
1485/// are replaced with [*]
1486QualType ASTContext::getVariableArrayDecayedType(QualType Ty) {
1487 if (Ty->isPointerType()) {
1488 QualType BaseType = Ty->getAs<PointerType>()->getPointeeType();
1489 if (isa<VariableArrayType>(BaseType)) {
1490 ArrayType *AT = dyn_cast<ArrayType>(BaseType);
1491 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1492 if (VAT->getSizeExpr()) {
1493 Ty = getUnknownSizeVariableArrayType(BaseType);
1494 Ty = getPointerType(Ty);
1495 }
1496 }
1497 }
1498 return Ty;
1499}
1500
1501
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001502/// getVariableArrayType - Returns a non-unique reference to the type for a
1503/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001504QualType ASTContext::getVariableArrayType(QualType EltTy,
1505 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001506 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001507 unsigned EltTypeQuals,
1508 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001509 // Since we don't unique expressions, it isn't possible to unique VLA's
1510 // that have an expression provided for their size.
Douglas Gregor715e9c82010-05-23 16:10:32 +00001511 QualType CanonType;
1512
1513 if (!EltTy.isCanonical()) {
Douglas Gregor715e9c82010-05-23 16:10:32 +00001514 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1515 EltTypeQuals, Brackets);
1516 }
1517
John McCall6b304a02009-09-24 23:30:46 +00001518 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor715e9c82010-05-23 16:10:32 +00001519 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001520
1521 VariableArrayTypes.push_back(New);
1522 Types.push_back(New);
1523 return QualType(New, 0);
1524}
1525
Douglas Gregor898574e2008-12-05 23:32:09 +00001526/// getDependentSizedArrayType - Returns a non-unique reference to
1527/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001528/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001529QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1530 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001531 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001532 unsigned EltTypeQuals,
1533 SourceRange Brackets) {
Douglas Gregorcb78d882009-11-19 18:03:26 +00001534 assert((!NumElts || NumElts->isTypeDependent() ||
1535 NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001536 "Size must be type- or value-dependent!");
1537
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001538 void *InsertPos = 0;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001539 DependentSizedArrayType *Canon = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00001540 llvm::FoldingSetNodeID ID;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001541
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001542 QualType CanonicalEltTy = getCanonicalType(EltTy);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001543 if (NumElts) {
1544 // Dependently-sized array types that do not have a specified
1545 // number of elements will have their sizes deduced from an
1546 // initializer.
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001547 DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM,
Douglas Gregorcb78d882009-11-19 18:03:26 +00001548 EltTypeQuals, NumElts);
1549
1550 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1551 }
1552
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001553 DependentSizedArrayType *New;
1554 if (Canon) {
1555 // We already have a canonical version of this array type; use it as
1556 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001557 New = new (*this, TypeAlignment)
1558 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1559 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001560 } else if (CanonicalEltTy == EltTy) {
1561 // This is a canonical type. Record it.
1562 New = new (*this, TypeAlignment)
1563 DependentSizedArrayType(*this, EltTy, QualType(),
1564 NumElts, ASM, EltTypeQuals, Brackets);
1565
1566 if (NumElts) {
1567#ifndef NDEBUG
1568 DependentSizedArrayType *CanonCheck
1569 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1570 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1571 (void)CanonCheck;
1572#endif
1573 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001574 }
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001575 } else {
1576 QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts,
1577 ASM, EltTypeQuals,
1578 SourceRange());
1579 New = new (*this, TypeAlignment)
1580 DependentSizedArrayType(*this, EltTy, Canon,
1581 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregor898574e2008-12-05 23:32:09 +00001584 Types.push_back(New);
1585 return QualType(New, 0);
1586}
1587
Eli Friedmanc5773c42008-02-15 18:16:39 +00001588QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1589 ArrayType::ArraySizeModifier ASM,
1590 unsigned EltTypeQuals) {
1591 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001592 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001593
1594 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001595 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001596 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1597 return QualType(ATP, 0);
1598
1599 // If the element type isn't canonical, this won't be a canonical type
1600 // either, so fill in the canonical type field.
1601 QualType Canonical;
1602
John McCall467b27b2009-10-22 20:10:53 +00001603 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001604 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001605 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001606
1607 // Get the new insert position for the node we care about.
1608 IncompleteArrayType *NewIP =
1609 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001610 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001611 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001612
John McCall6b304a02009-09-24 23:30:46 +00001613 IncompleteArrayType *New = new (*this, TypeAlignment)
1614 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001615
1616 IncompleteArrayTypes.InsertNode(New, InsertPos);
1617 Types.push_back(New);
1618 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001619}
1620
Steve Naroff73322922007-07-18 18:00:27 +00001621/// getVectorType - Return the unique reference to a vector type of
1622/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001623QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Bob Wilsone86d78c2010-11-10 21:56:12 +00001624 VectorType::VectorKind VecKind) {
Chris Lattner33daae62010-10-01 22:42:38 +00001625 BuiltinType *BaseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Chris Lattner33daae62010-10-01 22:42:38 +00001627 BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1628 assert(BaseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 // Check if we've already instantiated a vector of this type.
1631 llvm::FoldingSetNodeID ID;
Bob Wilsone86d78c2010-11-10 21:56:12 +00001632 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner788b0fd2010-06-23 06:00:24 +00001633
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 void *InsertPos = 0;
1635 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1636 return QualType(VTP, 0);
1637
1638 // If the element type isn't canonical, this won't be a canonical type either,
1639 // so fill in the canonical type field.
1640 QualType Canonical;
Douglas Gregor255210e2010-08-06 10:14:59 +00001641 if (!vecType.isCanonical()) {
Bob Wilson231da7e2010-11-16 00:32:20 +00001642 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 // Get the new insert position for the node we care about.
1645 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001646 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 }
John McCall6b304a02009-09-24 23:30:46 +00001648 VectorType *New = new (*this, TypeAlignment)
Bob Wilsone86d78c2010-11-10 21:56:12 +00001649 VectorType(vecType, NumElts, Canonical, VecKind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 VectorTypes.InsertNode(New, InsertPos);
1651 Types.push_back(New);
1652 return QualType(New, 0);
1653}
1654
Nate Begeman213541a2008-04-18 23:10:10 +00001655/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001656/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001657QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001658 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Chris Lattnerf52ab252008-04-06 22:59:24 +00001660 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001661 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Steve Naroff73322922007-07-18 18:00:27 +00001663 // Check if we've already instantiated a vector of this type.
1664 llvm::FoldingSetNodeID ID;
Chris Lattner788b0fd2010-06-23 06:00:24 +00001665 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsone86d78c2010-11-10 21:56:12 +00001666 VectorType::GenericVector);
Steve Naroff73322922007-07-18 18:00:27 +00001667 void *InsertPos = 0;
1668 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1669 return QualType(VTP, 0);
1670
1671 // If the element type isn't canonical, this won't be a canonical type either,
1672 // so fill in the canonical type field.
1673 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001674 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001675 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Steve Naroff73322922007-07-18 18:00:27 +00001677 // Get the new insert position for the node we care about.
1678 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001679 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001680 }
John McCall6b304a02009-09-24 23:30:46 +00001681 ExtVectorType *New = new (*this, TypeAlignment)
1682 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001683 VectorTypes.InsertNode(New, InsertPos);
1684 Types.push_back(New);
1685 return QualType(New, 0);
1686}
1687
Mike Stump1eb44332009-09-09 15:08:12 +00001688QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001689 Expr *SizeExpr,
1690 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001691 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001692 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001693 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001695 void *InsertPos = 0;
1696 DependentSizedExtVectorType *Canon
1697 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1698 DependentSizedExtVectorType *New;
1699 if (Canon) {
1700 // We already have a canonical version of this array type; use it as
1701 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001702 New = new (*this, TypeAlignment)
1703 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1704 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001705 } else {
1706 QualType CanonVecTy = getCanonicalType(vecType);
1707 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001708 New = new (*this, TypeAlignment)
1709 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1710 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001711
1712 DependentSizedExtVectorType *CanonCheck
1713 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1714 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1715 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001716 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1717 } else {
1718 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1719 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001720 New = new (*this, TypeAlignment)
1721 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001722 }
1723 }
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001725 Types.push_back(New);
1726 return QualType(New, 0);
1727}
1728
Douglas Gregor72564e72009-02-26 23:50:07 +00001729/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001730///
Rafael Espindola264ba482010-03-30 20:24:48 +00001731QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1732 const FunctionType::ExtInfo &Info) {
1733 const CallingConv CallConv = Info.getCC();
Reid Spencer5f016e22007-07-11 17:01:13 +00001734 // Unique functions, to guarantee there is only one function of a particular
1735 // structure.
1736 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +00001737 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001740 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001741 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001745 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001746 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindola264ba482010-03-30 20:24:48 +00001747 Canonical =
1748 getFunctionNoProtoType(getCanonicalType(ResultTy),
1749 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001752 FunctionNoProtoType *NewIP =
1753 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001754 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 }
Mike Stump1eb44332009-09-09 15:08:12 +00001756
John McCall6b304a02009-09-24 23:30:46 +00001757 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindola264ba482010-03-30 20:24:48 +00001758 FunctionNoProtoType(ResultTy, Canonical, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001759 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001760 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001761 return QualType(New, 0);
1762}
1763
1764/// getFunctionType - Return a normal function type with a typed argument
1765/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001766QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001767 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001768 unsigned TypeQuals, bool hasExceptionSpec,
1769 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001770 const QualType *ExArray,
1771 const FunctionType::ExtInfo &Info) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00001772
Rafael Espindola264ba482010-03-30 20:24:48 +00001773 const CallingConv CallConv= Info.getCC();
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 // Unique functions, to guarantee there is only one function of a particular
1775 // structure.
1776 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001777 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001778 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindola264ba482010-03-30 20:24:48 +00001779 NumExs, ExArray, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001780
1781 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001782 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001783 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001785
1786 // Determine whether the type being created is already canonical or not.
John McCall54e14c42009-10-22 22:37:11 +00001787 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001789 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 isCanonical = false;
1791
1792 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001793 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001795 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 llvm::SmallVector<QualType, 16> CanonicalArgs;
1797 CanonicalArgs.reserve(NumArgs);
1798 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001799 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001800
Chris Lattnerf52ab252008-04-06 22:59:24 +00001801 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001802 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001803 isVariadic, TypeQuals, false,
Rafael Espindola264ba482010-03-30 20:24:48 +00001804 false, 0, 0,
1805 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl465226e2009-05-27 22:11:52 +00001806
Reid Spencer5f016e22007-07-11 17:01:13 +00001807 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001808 FunctionProtoType *NewIP =
1809 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001810 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001812
Douglas Gregor72564e72009-02-26 23:50:07 +00001813 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001814 // for two variable size arrays (for parameter and exception types) at the
1815 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001816 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001817 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1818 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001819 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001820 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001821 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindola264ba482010-03-30 20:24:48 +00001822 ExArray, NumExs, Canonical, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001823 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001824 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001825 return QualType(FTP, 0);
1826}
1827
John McCall3cb0ebd2010-03-10 03:28:59 +00001828#ifndef NDEBUG
1829static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1830 if (!isa<CXXRecordDecl>(D)) return false;
1831 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1832 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1833 return true;
1834 if (RD->getDescribedClassTemplate() &&
1835 !isa<ClassTemplateSpecializationDecl>(RD))
1836 return true;
1837 return false;
1838}
1839#endif
1840
1841/// getInjectedClassNameType - Return the unique reference to the
1842/// injected class name type for the specified templated declaration.
1843QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1844 QualType TST) {
1845 assert(NeedsInjectedClassNameType(Decl));
1846 if (Decl->TypeForDecl) {
1847 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00001848 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCall3cb0ebd2010-03-10 03:28:59 +00001849 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1850 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1851 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1852 } else {
John McCall31f17ec2010-04-27 00:57:59 +00001853 Decl->TypeForDecl =
1854 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCall3cb0ebd2010-03-10 03:28:59 +00001855 Types.push_back(Decl->TypeForDecl);
1856 }
1857 return QualType(Decl->TypeForDecl, 0);
1858}
1859
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001860/// getTypeDeclType - Return the unique reference to the type for the
1861/// specified type declaration.
John McCallbecb8d52010-03-10 06:48:02 +00001862QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001863 assert(Decl && "Passed null for Decl param");
John McCallbecb8d52010-03-10 06:48:02 +00001864 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump1eb44332009-09-09 15:08:12 +00001865
John McCall19c85762010-02-16 03:57:14 +00001866 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001867 return getTypedefType(Typedef);
John McCallbecb8d52010-03-10 06:48:02 +00001868
John McCallbecb8d52010-03-10 06:48:02 +00001869 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1870 "Template type parameter types are always available.");
1871
John McCall19c85762010-02-16 03:57:14 +00001872 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001873 assert(!Record->getPreviousDeclaration() &&
1874 "struct/union has previous declaration");
1875 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001876 return getRecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00001877 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001878 assert(!Enum->getPreviousDeclaration() &&
1879 "enum has previous declaration");
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001880 return getEnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00001881 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00001882 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1883 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stump9fdbab32009-07-31 02:02:20 +00001884 } else
John McCallbecb8d52010-03-10 06:48:02 +00001885 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001886
John McCallbecb8d52010-03-10 06:48:02 +00001887 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001888 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001889}
1890
Reid Spencer5f016e22007-07-11 17:01:13 +00001891/// getTypedefType - Return the unique reference to the type for the
1892/// specified typename decl.
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00001893QualType
1894ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00001897 if (Canonical.isNull())
1898 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001899 Decl->TypeForDecl = new(*this, TypeAlignment)
1900 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001901 Types.push_back(Decl->TypeForDecl);
1902 return QualType(Decl->TypeForDecl, 0);
1903}
1904
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001905QualType ASTContext::getRecordType(const RecordDecl *Decl) {
1906 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1907
1908 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
1909 if (PrevDecl->TypeForDecl)
1910 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1911
1912 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
1913 Types.push_back(Decl->TypeForDecl);
1914 return QualType(Decl->TypeForDecl, 0);
1915}
1916
1917QualType ASTContext::getEnumType(const EnumDecl *Decl) {
1918 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1919
1920 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
1921 if (PrevDecl->TypeForDecl)
1922 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1923
1924 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
1925 Types.push_back(Decl->TypeForDecl);
1926 return QualType(Decl->TypeForDecl, 0);
1927}
1928
John McCall49a832b2009-10-18 09:09:24 +00001929/// \brief Retrieve a substitution-result type.
1930QualType
1931ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1932 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001933 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001934 && "replacement types must always be canonical");
1935
1936 llvm::FoldingSetNodeID ID;
1937 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1938 void *InsertPos = 0;
1939 SubstTemplateTypeParmType *SubstParm
1940 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1941
1942 if (!SubstParm) {
1943 SubstParm = new (*this, TypeAlignment)
1944 SubstTemplateTypeParmType(Parm, Replacement);
1945 Types.push_back(SubstParm);
1946 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1947 }
1948
1949 return QualType(SubstParm, 0);
1950}
1951
Douglas Gregorfab9d672009-02-05 23:33:38 +00001952/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001953/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001954/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001955QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001956 bool ParameterPack,
Douglas Gregorefed5c82010-06-16 15:23:05 +00001957 IdentifierInfo *Name) {
Douglas Gregorfab9d672009-02-05 23:33:38 +00001958 llvm::FoldingSetNodeID ID;
Douglas Gregorefed5c82010-06-16 15:23:05 +00001959 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001960 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001961 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001962 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1963
1964 if (TypeParm)
1965 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Douglas Gregorefed5c82010-06-16 15:23:05 +00001967 if (Name) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001968 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorefed5c82010-06-16 15:23:05 +00001969 TypeParm = new (*this, TypeAlignment)
1970 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001971
1972 TemplateTypeParmType *TypeCheck
1973 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1974 assert(!TypeCheck && "Template type parameter canonical type broken");
1975 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001976 } else
John McCall6b304a02009-09-24 23:30:46 +00001977 TypeParm = new (*this, TypeAlignment)
1978 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001979
1980 Types.push_back(TypeParm);
1981 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1982
1983 return QualType(TypeParm, 0);
1984}
1985
John McCall3cb0ebd2010-03-10 03:28:59 +00001986TypeSourceInfo *
1987ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1988 SourceLocation NameLoc,
1989 const TemplateArgumentListInfo &Args,
1990 QualType CanonType) {
1991 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1992
1993 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1994 TemplateSpecializationTypeLoc TL
1995 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1996 TL.setTemplateNameLoc(NameLoc);
1997 TL.setLAngleLoc(Args.getLAngleLoc());
1998 TL.setRAngleLoc(Args.getRAngleLoc());
1999 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
2000 TL.setArgLocInfo(i, Args[i].getLocInfo());
2001 return DI;
2002}
2003
Mike Stump1eb44332009-09-09 15:08:12 +00002004QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00002005ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00002006 const TemplateArgumentListInfo &Args,
John McCall71d74bc2010-06-13 09:25:03 +00002007 QualType Canon) {
John McCalld5532b62009-11-23 01:53:49 +00002008 unsigned NumArgs = Args.size();
2009
John McCall833ca992009-10-29 08:12:44 +00002010 llvm::SmallVector<TemplateArgument, 4> ArgVec;
2011 ArgVec.reserve(NumArgs);
2012 for (unsigned i = 0; i != NumArgs; ++i)
2013 ArgVec.push_back(Args[i].getArgument());
2014
John McCall31f17ec2010-04-27 00:57:59 +00002015 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00002016 Canon);
John McCall833ca992009-10-29 08:12:44 +00002017}
2018
2019QualType
2020ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002021 const TemplateArgument *Args,
2022 unsigned NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00002023 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00002024 if (!Canon.isNull())
2025 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002026 else
2027 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregorfc705b82009-02-26 22:19:44 +00002028
Douglas Gregor1275ae02009-07-28 23:00:59 +00002029 // Allocate the (non-canonical) template specialization type, but don't
2030 // try to unique it: these types typically have location information that
2031 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00002032 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00002033 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00002034 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00002035 TemplateSpecializationType *Spec
John McCallef990012010-06-11 11:07:21 +00002036 = new (Mem) TemplateSpecializationType(Template,
John McCall31f17ec2010-04-27 00:57:59 +00002037 Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00002038 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00002039
Douglas Gregor55f6b142009-02-09 18:46:07 +00002040 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00002041 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002042}
2043
Mike Stump1eb44332009-09-09 15:08:12 +00002044QualType
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002045ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2046 const TemplateArgument *Args,
2047 unsigned NumArgs) {
2048 // Build the canonical template specialization type.
2049 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2050 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2051 CanonArgs.reserve(NumArgs);
2052 for (unsigned I = 0; I != NumArgs; ++I)
2053 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2054
2055 // Determine whether this canonical template specialization type already
2056 // exists.
2057 llvm::FoldingSetNodeID ID;
2058 TemplateSpecializationType::Profile(ID, CanonTemplate,
2059 CanonArgs.data(), NumArgs, *this);
2060
2061 void *InsertPos = 0;
2062 TemplateSpecializationType *Spec
2063 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2064
2065 if (!Spec) {
2066 // Allocate a new canonical template specialization type.
2067 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2068 sizeof(TemplateArgument) * NumArgs),
2069 TypeAlignment);
2070 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2071 CanonArgs.data(), NumArgs,
2072 QualType());
2073 Types.push_back(Spec);
2074 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2075 }
2076
2077 assert(Spec->isDependentType() &&
2078 "Non-dependent template-id type must have a canonical type");
2079 return QualType(Spec, 0);
2080}
2081
2082QualType
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002083ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2084 NestedNameSpecifier *NNS,
2085 QualType NamedType) {
Douglas Gregore4e5b052009-03-19 00:18:19 +00002086 llvm::FoldingSetNodeID ID;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002087 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002088
2089 void *InsertPos = 0;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002090 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002091 if (T)
2092 return QualType(T, 0);
2093
Douglas Gregor789b1f62010-02-04 18:10:26 +00002094 QualType Canon = NamedType;
2095 if (!Canon.isCanonical()) {
2096 Canon = getCanonicalType(NamedType);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002097 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2098 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregor789b1f62010-02-04 18:10:26 +00002099 (void)CheckT;
2100 }
2101
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002102 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002103 Types.push_back(T);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002104 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002105 return QualType(T, 0);
2106}
2107
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002108QualType
2109ASTContext::getParenType(QualType InnerType) {
2110 llvm::FoldingSetNodeID ID;
2111 ParenType::Profile(ID, InnerType);
2112
2113 void *InsertPos = 0;
2114 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2115 if (T)
2116 return QualType(T, 0);
2117
2118 QualType Canon = InnerType;
2119 if (!Canon.isCanonical()) {
2120 Canon = getCanonicalType(InnerType);
2121 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2122 assert(!CheckT && "Paren canonical type broken");
2123 (void)CheckT;
2124 }
2125
2126 T = new (*this) ParenType(InnerType, Canon);
2127 Types.push_back(T);
2128 ParenTypes.InsertNode(T, InsertPos);
2129 return QualType(T, 0);
2130}
2131
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002132QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2133 NestedNameSpecifier *NNS,
2134 const IdentifierInfo *Name,
2135 QualType Canon) {
Douglas Gregord57959a2009-03-27 23:10:48 +00002136 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2137
2138 if (Canon.isNull()) {
2139 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002140 ElaboratedTypeKeyword CanonKeyword = Keyword;
2141 if (Keyword == ETK_None)
2142 CanonKeyword = ETK_Typename;
2143
2144 if (CanonNNS != NNS || CanonKeyword != Keyword)
2145 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002146 }
2147
2148 llvm::FoldingSetNodeID ID;
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002149 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002150
2151 void *InsertPos = 0;
Douglas Gregor4714c122010-03-31 17:34:00 +00002152 DependentNameType *T
2153 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregord57959a2009-03-27 23:10:48 +00002154 if (T)
2155 return QualType(T, 0);
2156
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002157 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregord57959a2009-03-27 23:10:48 +00002158 Types.push_back(T);
Douglas Gregor4714c122010-03-31 17:34:00 +00002159 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002160 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00002161}
2162
Mike Stump1eb44332009-09-09 15:08:12 +00002163QualType
John McCall33500952010-06-11 00:33:02 +00002164ASTContext::getDependentTemplateSpecializationType(
2165 ElaboratedTypeKeyword Keyword,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002166 NestedNameSpecifier *NNS,
John McCall33500952010-06-11 00:33:02 +00002167 const IdentifierInfo *Name,
2168 const TemplateArgumentListInfo &Args) {
2169 // TODO: avoid this copy
2170 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2171 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2172 ArgCopy.push_back(Args[I].getArgument());
2173 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2174 ArgCopy.size(),
2175 ArgCopy.data());
2176}
2177
2178QualType
2179ASTContext::getDependentTemplateSpecializationType(
2180 ElaboratedTypeKeyword Keyword,
2181 NestedNameSpecifier *NNS,
2182 const IdentifierInfo *Name,
2183 unsigned NumArgs,
2184 const TemplateArgument *Args) {
Douglas Gregor17343172009-04-01 00:28:59 +00002185 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2186
Douglas Gregor789b1f62010-02-04 18:10:26 +00002187 llvm::FoldingSetNodeID ID;
John McCall33500952010-06-11 00:33:02 +00002188 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2189 Name, NumArgs, Args);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002190
2191 void *InsertPos = 0;
John McCall33500952010-06-11 00:33:02 +00002192 DependentTemplateSpecializationType *T
2193 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002194 if (T)
2195 return QualType(T, 0);
2196
John McCall33500952010-06-11 00:33:02 +00002197 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002198
John McCall33500952010-06-11 00:33:02 +00002199 ElaboratedTypeKeyword CanonKeyword = Keyword;
2200 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2201
2202 bool AnyNonCanonArgs = false;
2203 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2204 for (unsigned I = 0; I != NumArgs; ++I) {
2205 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2206 if (!CanonArgs[I].structurallyEquals(Args[I]))
2207 AnyNonCanonArgs = true;
Douglas Gregor17343172009-04-01 00:28:59 +00002208 }
2209
John McCall33500952010-06-11 00:33:02 +00002210 QualType Canon;
2211 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2212 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2213 Name, NumArgs,
2214 CanonArgs.data());
2215
2216 // Find the insert position again.
2217 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2218 }
2219
2220 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2221 sizeof(TemplateArgument) * NumArgs),
2222 TypeAlignment);
John McCallef990012010-06-11 11:07:21 +00002223 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCall33500952010-06-11 00:33:02 +00002224 Name, NumArgs, Args, Canon);
Douglas Gregor17343172009-04-01 00:28:59 +00002225 Types.push_back(T);
John McCall33500952010-06-11 00:33:02 +00002226 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002227 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002228}
2229
Chris Lattner88cb27a2008-04-07 04:56:42 +00002230/// CmpProtocolNames - Comparison predicate for sorting protocols
2231/// alphabetically.
2232static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2233 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002234 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00002235}
2236
John McCallc12c5bb2010-05-15 11:32:37 +00002237static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCall54e14c42009-10-22 22:37:11 +00002238 unsigned NumProtocols) {
2239 if (NumProtocols == 0) return true;
2240
2241 for (unsigned i = 1; i != NumProtocols; ++i)
2242 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2243 return false;
2244 return true;
2245}
2246
2247static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00002248 unsigned &NumProtocols) {
2249 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Chris Lattner88cb27a2008-04-07 04:56:42 +00002251 // Sort protocols, keyed by name.
2252 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2253
2254 // Remove duplicates.
2255 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2256 NumProtocols = ProtocolsEnd-Protocols;
2257}
2258
John McCallc12c5bb2010-05-15 11:32:37 +00002259QualType ASTContext::getObjCObjectType(QualType BaseType,
2260 ObjCProtocolDecl * const *Protocols,
2261 unsigned NumProtocols) {
2262 // If the base type is an interface and there aren't any protocols
2263 // to add, then the interface type will do just fine.
2264 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2265 return BaseType;
2266
2267 // Look in the folding set for an existing type.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002268 llvm::FoldingSetNodeID ID;
John McCallc12c5bb2010-05-15 11:32:37 +00002269 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002270 void *InsertPos = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002271 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2272 return QualType(QT, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002273
John McCallc12c5bb2010-05-15 11:32:37 +00002274 // Build the canonical type, which has the canonical base type and
2275 // a sorted-and-uniqued list of protocols.
John McCall54e14c42009-10-22 22:37:11 +00002276 QualType Canonical;
John McCallc12c5bb2010-05-15 11:32:37 +00002277 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2278 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2279 if (!ProtocolsSorted) {
Benjamin Kramer02379412010-04-27 17:12:11 +00002280 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2281 Protocols + NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002282 unsigned UniqueCount = NumProtocols;
2283
John McCall54e14c42009-10-22 22:37:11 +00002284 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCallc12c5bb2010-05-15 11:32:37 +00002285 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2286 &Sorted[0], UniqueCount);
John McCall54e14c42009-10-22 22:37:11 +00002287 } else {
John McCallc12c5bb2010-05-15 11:32:37 +00002288 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2289 Protocols, NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002290 }
2291
2292 // Regenerate InsertPos.
John McCallc12c5bb2010-05-15 11:32:37 +00002293 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2294 }
2295
2296 unsigned Size = sizeof(ObjCObjectTypeImpl);
2297 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2298 void *Mem = Allocate(Size, TypeAlignment);
2299 ObjCObjectTypeImpl *T =
2300 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2301
2302 Types.push_back(T);
2303 ObjCObjectTypes.InsertNode(T, InsertPos);
2304 return QualType(T, 0);
2305}
2306
2307/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2308/// the given object type.
2309QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2310 llvm::FoldingSetNodeID ID;
2311 ObjCObjectPointerType::Profile(ID, ObjectT);
2312
2313 void *InsertPos = 0;
2314 if (ObjCObjectPointerType *QT =
2315 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2316 return QualType(QT, 0);
2317
2318 // Find the canonical object type.
2319 QualType Canonical;
2320 if (!ObjectT.isCanonical()) {
2321 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2322
2323 // Regenerate InsertPos.
John McCall54e14c42009-10-22 22:37:11 +00002324 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2325 }
2326
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002327 // No match.
John McCallc12c5bb2010-05-15 11:32:37 +00002328 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2329 ObjCObjectPointerType *QType =
2330 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002332 Types.push_back(QType);
2333 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCallc12c5bb2010-05-15 11:32:37 +00002334 return QualType(QType, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002335}
Chris Lattner88cb27a2008-04-07 04:56:42 +00002336
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002337/// getObjCInterfaceType - Return the unique reference to the type for the
2338/// specified ObjC interface decl. The list of protocols is optional.
2339QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2340 if (Decl->TypeForDecl)
2341 return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002343 // FIXME: redeclarations?
2344 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2345 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2346 Decl->TypeForDecl = T;
2347 Types.push_back(T);
2348 return QualType(T, 0);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002349}
2350
Douglas Gregor72564e72009-02-26 23:50:07 +00002351/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2352/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002353/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002354/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002355/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002356QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002357 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002358 if (tofExpr->isTypeDependent()) {
2359 llvm::FoldingSetNodeID ID;
2360 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Douglas Gregorb1975722009-07-30 23:18:24 +00002362 void *InsertPos = 0;
2363 DependentTypeOfExprType *Canon
2364 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2365 if (Canon) {
2366 // We already have a "canonical" version of an identical, dependent
2367 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002368 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002369 QualType((TypeOfExprType*)Canon, 0));
2370 }
2371 else {
2372 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002373 Canon
2374 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002375 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2376 toe = Canon;
2377 }
2378 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002379 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002380 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002381 }
Steve Naroff9752f252007-08-01 18:02:17 +00002382 Types.push_back(toe);
2383 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002384}
2385
Steve Naroff9752f252007-08-01 18:02:17 +00002386/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2387/// TypeOfType AST's. The only motivation to unique these nodes would be
2388/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002389/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002390/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002391QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002392 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002393 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002394 Types.push_back(tot);
2395 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002396}
2397
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002398/// getDecltypeForExpr - Given an expr, will return the decltype for that
2399/// expression, according to the rules in C++0x [dcl.type.simple]p4
2400static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002401 if (e->isTypeDependent())
2402 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002403
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002404 // If e is an id expression or a class member access, decltype(e) is defined
2405 // as the type of the entity named by e.
2406 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2407 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2408 return VD->getType();
2409 }
2410 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2411 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2412 return FD->getType();
2413 }
2414 // If e is a function call or an invocation of an overloaded operator,
2415 // (parentheses around e are ignored), decltype(e) is defined as the
2416 // return type of that function.
2417 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2418 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002420 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002421
2422 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002423 // defined as T&, otherwise decltype(e) is defined as T.
John McCall7eb0a9e2010-11-24 05:12:34 +00002424 if (e->isLValue())
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002425 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002427 return T;
2428}
2429
Anders Carlsson395b4752009-06-24 19:06:50 +00002430/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2431/// DecltypeType AST's. The only motivation to unique these nodes would be
2432/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002433/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002434/// on canonical type's (which are always unique).
2435QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002436 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002437 if (e->isTypeDependent()) {
2438 llvm::FoldingSetNodeID ID;
2439 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002441 void *InsertPos = 0;
2442 DependentDecltypeType *Canon
2443 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2444 if (Canon) {
2445 // We already have a "canonical" version of an equivalent, dependent
2446 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002447 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002448 QualType((DecltypeType*)Canon, 0));
2449 }
2450 else {
2451 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002452 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002453 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2454 dt = Canon;
2455 }
2456 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002457 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002458 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002459 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002460 Types.push_back(dt);
2461 return QualType(dt, 0);
2462}
2463
Reid Spencer5f016e22007-07-11 17:01:13 +00002464/// getTagDeclType - Return the unique reference to the type for the
2465/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002466QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002467 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002468 // FIXME: What is the design on getTagDeclType when it requires casting
2469 // away const? mutable?
2470 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002471}
2472
Mike Stump1eb44332009-09-09 15:08:12 +00002473/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2474/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2475/// needs to agree with the definition in <stddef.h>.
Anders Carlssona3ccda52009-12-12 00:26:23 +00002476CanQualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002477 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002478}
2479
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002480/// getSignedWCharType - Return the type of "signed wchar_t".
2481/// Used when in C++, as a GCC extension.
2482QualType ASTContext::getSignedWCharType() const {
2483 // FIXME: derive from "Target" ?
2484 return WCharTy;
2485}
2486
2487/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2488/// Used when in C++, as a GCC extension.
2489QualType ASTContext::getUnsignedWCharType() const {
2490 // FIXME: derive from "Target" ?
2491 return UnsignedIntTy;
2492}
2493
Chris Lattner8b9023b2007-07-13 03:05:23 +00002494/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2495/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2496QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002497 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002498}
2499
Chris Lattnere6327742008-04-02 05:18:44 +00002500//===----------------------------------------------------------------------===//
2501// Type Operators
2502//===----------------------------------------------------------------------===//
2503
John McCall54e14c42009-10-22 22:37:11 +00002504CanQualType ASTContext::getCanonicalParamType(QualType T) {
2505 // Push qualifiers into arrays, and then discard any remaining
2506 // qualifiers.
2507 T = getCanonicalType(T);
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002508 T = getVariableArrayDecayedType(T);
John McCall54e14c42009-10-22 22:37:11 +00002509 const Type *Ty = T.getTypePtr();
John McCall54e14c42009-10-22 22:37:11 +00002510 QualType Result;
2511 if (isa<ArrayType>(Ty)) {
2512 Result = getArrayDecayedType(QualType(Ty,0));
2513 } else if (isa<FunctionType>(Ty)) {
2514 Result = getPointerType(QualType(Ty, 0));
2515 } else {
2516 Result = QualType(Ty, 0);
2517 }
2518
2519 return CanQualType::CreateUnsafe(Result);
2520}
2521
Chris Lattner77c96472008-04-06 22:41:35 +00002522/// getCanonicalType - Return the canonical (structural) type corresponding to
2523/// the specified potentially non-canonical type. The non-canonical version
2524/// of a type may have many "decorated" versions of types. Decorators can
2525/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2526/// to be free of any of these, allowing two canonical types to be compared
2527/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002528CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002529 QualifierCollector Quals;
2530 const Type *Ptr = Quals.strip(T);
2531 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002532
John McCall0953e762009-09-24 19:53:00 +00002533 // The canonical internal type will be the canonical type *except*
2534 // that we push type qualifiers down through array types.
2535
2536 // If there are no new qualifiers to push down, stop here.
2537 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002538 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002539
John McCall0953e762009-09-24 19:53:00 +00002540 // If the type qualifiers are on an array type, get the canonical
2541 // type of the array with the qualifiers applied to the element
2542 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002543 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2544 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002545 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002547 // Get the canonical version of the element with the extra qualifiers on it.
2548 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002549 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002550 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002552 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002553 return CanQualType::CreateUnsafe(
2554 getConstantArrayType(NewEltTy, CAT->getSize(),
2555 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002556 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002557 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002558 return CanQualType::CreateUnsafe(
2559 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002560 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002561
Douglas Gregor898574e2008-12-05 23:32:09 +00002562 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002563 return CanQualType::CreateUnsafe(
2564 getDependentSizedArrayType(NewEltTy,
John McCall3fa5cae2010-10-26 07:05:15 +00002565 DSAT->getSizeExpr(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002566 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002567 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor87a924e2009-10-30 22:56:57 +00002568 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor898574e2008-12-05 23:32:09 +00002569
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002570 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002571 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
John McCall3fa5cae2010-10-26 07:05:15 +00002572 VAT->getSizeExpr(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002573 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002574 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002575 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002576}
2577
Chandler Carruth28e318c2009-12-29 07:16:59 +00002578QualType ASTContext::getUnqualifiedArrayType(QualType T,
2579 Qualifiers &Quals) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002580 Quals = T.getQualifiers();
Douglas Gregor9dadd942010-05-17 18:45:21 +00002581 const ArrayType *AT = getAsArrayType(T);
2582 if (!AT) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002583 return T.getUnqualifiedType();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002584 }
2585
Chandler Carruth28e318c2009-12-29 07:16:59 +00002586 QualType Elt = AT->getElementType();
Zhongxing Xuc1ae0a82010-01-05 08:15:06 +00002587 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002588 if (Elt == UnqualElt)
2589 return T;
2590
Douglas Gregor9dadd942010-05-17 18:45:21 +00002591 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth28e318c2009-12-29 07:16:59 +00002592 return getConstantArrayType(UnqualElt, CAT->getSize(),
2593 CAT->getSizeModifier(), 0);
2594 }
2595
Douglas Gregor9dadd942010-05-17 18:45:21 +00002596 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth28e318c2009-12-29 07:16:59 +00002597 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2598 }
2599
Douglas Gregor9dadd942010-05-17 18:45:21 +00002600 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2601 return getVariableArrayType(UnqualElt,
John McCall3fa5cae2010-10-26 07:05:15 +00002602 VAT->getSizeExpr(),
Douglas Gregor9dadd942010-05-17 18:45:21 +00002603 VAT->getSizeModifier(),
2604 VAT->getIndexTypeCVRQualifiers(),
2605 VAT->getBracketsRange());
2606 }
2607
2608 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
John McCall3fa5cae2010-10-26 07:05:15 +00002609 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr(),
Chandler Carruth28e318c2009-12-29 07:16:59 +00002610 DSAT->getSizeModifier(), 0,
2611 SourceRange());
2612}
2613
Douglas Gregor5a57efd2010-06-09 03:53:18 +00002614/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2615/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2616/// they point to and return true. If T1 and T2 aren't pointer types
2617/// or pointer-to-member types, or if they are not similar at this
2618/// level, returns false and leaves T1 and T2 unchanged. Top-level
2619/// qualifiers on T1 and T2 are ignored. This function will typically
2620/// be called in a loop that successively "unwraps" pointer and
2621/// pointer-to-member types to compare them at each level.
2622bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2623 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2624 *T2PtrType = T2->getAs<PointerType>();
2625 if (T1PtrType && T2PtrType) {
2626 T1 = T1PtrType->getPointeeType();
2627 T2 = T2PtrType->getPointeeType();
2628 return true;
2629 }
2630
2631 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2632 *T2MPType = T2->getAs<MemberPointerType>();
2633 if (T1MPType && T2MPType &&
2634 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2635 QualType(T2MPType->getClass(), 0))) {
2636 T1 = T1MPType->getPointeeType();
2637 T2 = T2MPType->getPointeeType();
2638 return true;
2639 }
2640
2641 if (getLangOptions().ObjC1) {
2642 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2643 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2644 if (T1OPType && T2OPType) {
2645 T1 = T1OPType->getPointeeType();
2646 T2 = T2OPType->getPointeeType();
2647 return true;
2648 }
2649 }
2650
2651 // FIXME: Block pointers, too?
2652
2653 return false;
2654}
2655
Abramo Bagnara25777432010-08-11 22:01:17 +00002656DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name,
2657 SourceLocation NameLoc) {
John McCall80ad16f2009-11-24 18:42:40 +00002658 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnara25777432010-08-11 22:01:17 +00002659 // DNInfo work in progress: CHECKME: what about DNLoc?
2660 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2661
John McCall80ad16f2009-11-24 18:42:40 +00002662 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002663 DeclarationName DName;
John McCall80ad16f2009-11-24 18:42:40 +00002664 if (DTN->isIdentifier()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002665 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2666 return DeclarationNameInfo(DName, NameLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002667 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002668 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2669 // DNInfo work in progress: FIXME: source locations?
2670 DeclarationNameLoc DNLoc;
2671 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2672 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2673 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002674 }
2675 }
2676
John McCall0bd6feb2009-12-02 08:04:21 +00002677 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2678 assert(Storage);
Abramo Bagnara25777432010-08-11 22:01:17 +00002679 // DNInfo work in progress: CHECKME: what about DNLoc?
2680 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002681}
2682
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002683TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor3e1274f2010-06-16 21:09:37 +00002684 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2685 if (TemplateTemplateParmDecl *TTP
2686 = dyn_cast<TemplateTemplateParmDecl>(Template))
2687 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2688
2689 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002690 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor3e1274f2010-06-16 21:09:37 +00002691 }
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002692
John McCall0bd6feb2009-12-02 08:04:21 +00002693 assert(!Name.getAsOverloadedTemplate());
Mike Stump1eb44332009-09-09 15:08:12 +00002694
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002695 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2696 assert(DTN && "Non-dependent template names must refer to template decls.");
2697 return DTN->CanonicalTemplateName;
2698}
2699
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002700bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2701 X = getCanonicalTemplateName(X);
2702 Y = getCanonicalTemplateName(Y);
2703 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2704}
2705
Mike Stump1eb44332009-09-09 15:08:12 +00002706TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002707ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2708 switch (Arg.getKind()) {
2709 case TemplateArgument::Null:
2710 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Douglas Gregor1275ae02009-07-28 23:00:59 +00002712 case TemplateArgument::Expression:
Douglas Gregor1275ae02009-07-28 23:00:59 +00002713 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Douglas Gregor1275ae02009-07-28 23:00:59 +00002715 case TemplateArgument::Declaration:
John McCall833ca992009-10-29 08:12:44 +00002716 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Douglas Gregor788cd062009-11-11 01:00:40 +00002718 case TemplateArgument::Template:
2719 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2720
Douglas Gregor1275ae02009-07-28 23:00:59 +00002721 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002722 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002723 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Douglas Gregor1275ae02009-07-28 23:00:59 +00002725 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00002726 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Douglas Gregor1275ae02009-07-28 23:00:59 +00002728 case TemplateArgument::Pack: {
Douglas Gregor910f8002010-11-07 23:05:16 +00002729 TemplateArgument *CanonArgs
2730 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregor1275ae02009-07-28 23:00:59 +00002731 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002732 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002733 AEnd = Arg.pack_end();
2734 A != AEnd; (void)++A, ++Idx)
2735 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Douglas Gregor910f8002010-11-07 23:05:16 +00002737 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregor1275ae02009-07-28 23:00:59 +00002738 }
2739 }
2740
2741 // Silence GCC warning
2742 assert(false && "Unhandled template argument kind");
2743 return TemplateArgument();
2744}
2745
Douglas Gregord57959a2009-03-27 23:10:48 +00002746NestedNameSpecifier *
2747ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002748 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002749 return 0;
2750
2751 switch (NNS->getKind()) {
2752 case NestedNameSpecifier::Identifier:
2753 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002754 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002755 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2756 NNS->getAsIdentifier());
2757
2758 case NestedNameSpecifier::Namespace:
2759 // A namespace is canonical; build a nested-name-specifier with
2760 // this namespace and no prefix.
2761 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2762
2763 case NestedNameSpecifier::TypeSpec:
2764 case NestedNameSpecifier::TypeSpecWithTemplate: {
2765 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor264bf662010-11-04 00:09:33 +00002766
2767 // If we have some kind of dependent-named type (e.g., "typename T::type"),
2768 // break it apart into its prefix and identifier, then reconsititute those
2769 // as the canonical nested-name-specifier. This is required to canonicalize
2770 // a dependent nested-name-specifier involving typedefs of dependent-name
2771 // types, e.g.,
2772 // typedef typename T::type T1;
2773 // typedef typename T1::type T2;
2774 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
2775 NestedNameSpecifier *Prefix
2776 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
2777 return NestedNameSpecifier::Create(*this, Prefix,
2778 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
2779 }
2780
Douglas Gregor643f8432010-11-04 00:14:23 +00002781 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor264bf662010-11-04 00:09:33 +00002782 if (const DependentTemplateSpecializationType *DTST
2783 = T->getAs<DependentTemplateSpecializationType>()) {
2784 NestedNameSpecifier *Prefix
2785 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
2786 TemplateName Name
2787 = getDependentTemplateName(Prefix, DTST->getIdentifier());
2788 T = getTemplateSpecializationType(Name,
2789 DTST->getArgs(), DTST->getNumArgs());
2790 T = getCanonicalType(T);
2791 }
2792
2793 return NestedNameSpecifier::Create(*this, 0, false, T.getTypePtr());
Douglas Gregord57959a2009-03-27 23:10:48 +00002794 }
2795
2796 case NestedNameSpecifier::Global:
2797 // The global specifier is canonical and unique.
2798 return NNS;
2799 }
2800
2801 // Required to silence a GCC warning
2802 return 0;
2803}
2804
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002805
2806const ArrayType *ASTContext::getAsArrayType(QualType T) {
2807 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00002808 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002809 // Handle the common positive case fast.
2810 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2811 return AT;
2812 }
Mike Stump1eb44332009-09-09 15:08:12 +00002813
John McCall0953e762009-09-24 19:53:00 +00002814 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002815 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002816 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002817 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002818
John McCall0953e762009-09-24 19:53:00 +00002819 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002820 // implements C99 6.7.3p8: "If the specification of an array type includes
2821 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002822
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002823 // If we get here, we either have type qualifiers on the type, or we have
2824 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002825 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002826
John McCall0953e762009-09-24 19:53:00 +00002827 QualifierCollector Qs;
John McCall49f4e1c2010-12-10 11:01:00 +00002828 const Type *Ty = Qs.strip(T.getDesugaredType(*this));
Mike Stump1eb44332009-09-09 15:08:12 +00002829
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002830 // If we have a simple case, just return now.
2831 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002832 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002833 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002834
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002835 // Otherwise, we have an array and we have qualifiers on it. Push the
2836 // qualifiers into the array element type and return a new array type.
2837 // Get the canonical version of the element with the extra qualifiers on it.
2838 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002839 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002841 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2842 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2843 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002844 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002845 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2846 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2847 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002848 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002849
Mike Stump1eb44332009-09-09 15:08:12 +00002850 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002851 = dyn_cast<DependentSizedArrayType>(ATy))
2852 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002853 getDependentSizedArrayType(NewEltTy,
John McCall3fa5cae2010-10-26 07:05:15 +00002854 DSAT->getSizeExpr(),
Douglas Gregor898574e2008-12-05 23:32:09 +00002855 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002856 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002857 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002858
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002859 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002860 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCall3fa5cae2010-10-26 07:05:15 +00002861 VAT->getSizeExpr(),
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002862 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002863 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002864 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002865}
2866
Chris Lattnere6327742008-04-02 05:18:44 +00002867/// getArrayDecayedType - Return the properly qualified result of decaying the
2868/// specified array type to a pointer. This operation is non-trivial when
2869/// handling typedefs etc. The canonical type of "T" must be an array type,
2870/// this returns a pointer to a properly qualified element of the array.
2871///
2872/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2873QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002874 // Get the element type with 'getAsArrayType' so that we don't lose any
2875 // typedefs in the element type of the array. This also handles propagation
2876 // of type qualifiers from the array type into the element type if present
2877 // (C99 6.7.3p8).
2878 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2879 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002881 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002882
2883 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002884 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002885}
2886
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002887QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002888 QualifierCollector Qs;
Benjamin Kramer02379412010-04-27 17:12:11 +00002889 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2890 QT = AT->getElementType();
John McCall49f4e1c2010-12-10 11:01:00 +00002891 return Qs.apply(*this, QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002892}
2893
Anders Carlssonfbbce492009-09-25 01:23:32 +00002894QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2895 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Anders Carlssonfbbce492009-09-25 01:23:32 +00002897 if (const ArrayType *AT = getAsArrayType(ElemTy))
2898 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002899
Anders Carlsson6183a992008-12-21 03:44:36 +00002900 return ElemTy;
2901}
2902
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002903/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002904uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002905ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2906 uint64_t ElementCount = 1;
2907 do {
2908 ElementCount *= CA->getSize().getZExtValue();
2909 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2910 } while (CA);
2911 return ElementCount;
2912}
2913
Reid Spencer5f016e22007-07-11 17:01:13 +00002914/// getFloatingRank - Return a relative rank for floating point types.
2915/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002916static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002917 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002918 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002919
John McCall183700f2009-09-21 23:43:11 +00002920 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2921 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002922 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002923 case BuiltinType::Float: return FloatRank;
2924 case BuiltinType::Double: return DoubleRank;
2925 case BuiltinType::LongDouble: return LongDoubleRank;
2926 }
2927}
2928
Mike Stump1eb44332009-09-09 15:08:12 +00002929/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2930/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002931/// 'typeDomain' is a real floating point or complex type.
2932/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002933QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2934 QualType Domain) const {
2935 FloatingRank EltRank = getFloatingRank(Size);
2936 if (Domain->isComplexType()) {
2937 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002938 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002939 case FloatRank: return FloatComplexTy;
2940 case DoubleRank: return DoubleComplexTy;
2941 case LongDoubleRank: return LongDoubleComplexTy;
2942 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002943 }
Chris Lattner1361b112008-04-06 23:58:54 +00002944
2945 assert(Domain->isRealFloatingType() && "Unknown domain!");
2946 switch (EltRank) {
2947 default: assert(0 && "getFloatingRank(): illegal value for rank");
2948 case FloatRank: return FloatTy;
2949 case DoubleRank: return DoubleTy;
2950 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002951 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002952}
2953
Chris Lattner7cfeb082008-04-06 23:55:33 +00002954/// getFloatingTypeOrder - Compare the rank of the two specified floating
2955/// point types, ignoring the domain of the type (i.e. 'double' ==
2956/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002957/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002958int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2959 FloatingRank LHSR = getFloatingRank(LHS);
2960 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002961
Chris Lattnera75cea32008-04-06 23:38:49 +00002962 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002963 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002964 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002965 return 1;
2966 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002967}
2968
Chris Lattnerf52ab252008-04-06 22:59:24 +00002969/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2970/// routine will assert if passed a built-in type that isn't an integer or enum,
2971/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002972unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002973 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002974 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00002975 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00002976
Eli Friedmana3426752009-07-05 23:44:27 +00002977 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2978 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2979
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002980 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2981 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2982
2983 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2984 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2985
Chris Lattnerf52ab252008-04-06 22:59:24 +00002986 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002987 default: assert(0 && "getIntegerRank(): not a built-in integer");
2988 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002989 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002990 case BuiltinType::Char_S:
2991 case BuiltinType::Char_U:
2992 case BuiltinType::SChar:
2993 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002994 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002995 case BuiltinType::Short:
2996 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002997 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002998 case BuiltinType::Int:
2999 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003000 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003001 case BuiltinType::Long:
3002 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003003 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003004 case BuiltinType::LongLong:
3005 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003006 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00003007 case BuiltinType::Int128:
3008 case BuiltinType::UInt128:
3009 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00003010 }
3011}
3012
Eli Friedman04e83572009-08-20 04:21:42 +00003013/// \brief Whether this is a promotable bitfield reference according
3014/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
3015///
3016/// \returns the type this bit-field will promote to, or NULL if no
3017/// promotion occurs.
3018QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregorceafbde2010-05-24 20:13:53 +00003019 if (E->isTypeDependent() || E->isValueDependent())
3020 return QualType();
3021
Eli Friedman04e83572009-08-20 04:21:42 +00003022 FieldDecl *Field = E->getBitField();
3023 if (!Field)
3024 return QualType();
3025
3026 QualType FT = Field->getType();
3027
3028 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3029 uint64_t BitWidth = BitWidthAP.getZExtValue();
3030 uint64_t IntSize = getTypeSize(IntTy);
3031 // GCC extension compatibility: if the bit-field size is less than or equal
3032 // to the size of int, it gets promoted no matter what its type is.
3033 // For instance, unsigned long bf : 4 gets promoted to signed int.
3034 if (BitWidth < IntSize)
3035 return IntTy;
3036
3037 if (BitWidth == IntSize)
3038 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3039
3040 // Types bigger than int are not subject to promotions, and therefore act
3041 // like the base type.
3042 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3043 // is ridiculous.
3044 return QualType();
3045}
3046
Eli Friedmana95d7572009-08-19 07:44:53 +00003047/// getPromotedIntegerType - Returns the type that Promotable will
3048/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3049/// integer type.
3050QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
3051 assert(!Promotable.isNull());
3052 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00003053 if (const EnumType *ET = Promotable->getAs<EnumType>())
3054 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00003055 if (Promotable->isSignedIntegerType())
3056 return IntTy;
3057 uint64_t PromotableSize = getTypeSize(Promotable);
3058 uint64_t IntSize = getTypeSize(IntTy);
3059 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3060 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3061}
3062
Mike Stump1eb44332009-09-09 15:08:12 +00003063/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00003064/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00003065/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003066int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00003067 Type *LHSC = getCanonicalType(LHS).getTypePtr();
3068 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00003069 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003070
Chris Lattnerf52ab252008-04-06 22:59:24 +00003071 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3072 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00003073
Chris Lattner7cfeb082008-04-06 23:55:33 +00003074 unsigned LHSRank = getIntegerRank(LHSC);
3075 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00003076
Chris Lattner7cfeb082008-04-06 23:55:33 +00003077 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3078 if (LHSRank == RHSRank) return 0;
3079 return LHSRank > RHSRank ? 1 : -1;
3080 }
Mike Stump1eb44332009-09-09 15:08:12 +00003081
Chris Lattner7cfeb082008-04-06 23:55:33 +00003082 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3083 if (LHSUnsigned) {
3084 // If the unsigned [LHS] type is larger, return it.
3085 if (LHSRank >= RHSRank)
3086 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00003087
Chris Lattner7cfeb082008-04-06 23:55:33 +00003088 // If the signed type can represent all values of the unsigned type, it
3089 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003090 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003091 return -1;
3092 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00003093
Chris Lattner7cfeb082008-04-06 23:55:33 +00003094 // If the unsigned [RHS] type is larger, return it.
3095 if (RHSRank >= LHSRank)
3096 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00003097
Chris Lattner7cfeb082008-04-06 23:55:33 +00003098 // If the signed type can represent all values of the unsigned type, it
3099 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003100 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003101 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00003102}
Anders Carlsson71993dd2007-08-17 05:31:46 +00003103
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003104static RecordDecl *
3105CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
3106 SourceLocation L, IdentifierInfo *Id) {
3107 if (Ctx.getLangOptions().CPlusPlus)
3108 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
3109 else
3110 return RecordDecl::Create(Ctx, TK, DC, L, Id);
3111}
3112
Mike Stump1eb44332009-09-09 15:08:12 +00003113// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00003114QualType ASTContext::getCFConstantStringType() {
3115 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003116 CFConstantStringTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003117 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003118 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00003119 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003120
Anders Carlssonf06273f2007-11-19 00:25:30 +00003121 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Anders Carlsson71993dd2007-08-17 05:31:46 +00003123 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00003124 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00003125 // int flags;
3126 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00003127 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00003128 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00003129 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00003130 FieldTypes[3] = LongTy;
3131
Anders Carlsson71993dd2007-08-17 05:31:46 +00003132 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00003133 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003134 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00003135 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003136 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003137 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003138 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003139 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003140 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003141 }
3142
Douglas Gregor838db382010-02-11 01:19:42 +00003143 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00003144 }
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Anders Carlsson71993dd2007-08-17 05:31:46 +00003146 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00003147}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003148
Douglas Gregor319ac892009-04-23 22:29:11 +00003149void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003150 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003151 assert(Rec && "Invalid CFConstantStringType");
3152 CFConstantStringTypeDecl = Rec->getDecl();
3153}
3154
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003155// getNSConstantStringType - Return the type used for constant NSStrings.
3156QualType ASTContext::getNSConstantStringType() {
3157 if (!NSConstantStringTypeDecl) {
3158 NSConstantStringTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003159 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003160 &Idents.get("__builtin_NSString"));
3161 NSConstantStringTypeDecl->startDefinition();
3162
3163 QualType FieldTypes[3];
3164
3165 // const int *isa;
3166 FieldTypes[0] = getPointerType(IntTy.withConst());
3167 // const char *str;
3168 FieldTypes[1] = getPointerType(CharTy.withConst());
3169 // unsigned int length;
3170 FieldTypes[2] = UnsignedIntTy;
3171
3172 // Create fields
3173 for (unsigned i = 0; i < 3; ++i) {
3174 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3175 SourceLocation(), 0,
3176 FieldTypes[i], /*TInfo=*/0,
3177 /*BitWidth=*/0,
3178 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003179 Field->setAccess(AS_public);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003180 NSConstantStringTypeDecl->addDecl(Field);
3181 }
3182
3183 NSConstantStringTypeDecl->completeDefinition();
3184 }
3185
3186 return getTagDeclType(NSConstantStringTypeDecl);
3187}
3188
3189void ASTContext::setNSConstantStringType(QualType T) {
3190 const RecordType *Rec = T->getAs<RecordType>();
3191 assert(Rec && "Invalid NSConstantStringType");
3192 NSConstantStringTypeDecl = Rec->getDecl();
3193}
3194
Mike Stump1eb44332009-09-09 15:08:12 +00003195QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003196 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00003197 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003198 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003199 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00003200 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00003201
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003202 QualType FieldTypes[] = {
3203 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00003204 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003205 getPointerType(UnsignedLongTy),
3206 getConstantArrayType(UnsignedLongTy,
3207 llvm::APInt(32, 5), ArrayType::Normal, 0)
3208 };
Mike Stump1eb44332009-09-09 15:08:12 +00003209
Douglas Gregor44b43212008-12-11 16:49:14 +00003210 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003211 FieldDecl *Field = FieldDecl::Create(*this,
3212 ObjCFastEnumerationStateTypeDecl,
3213 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003214 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003215 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003216 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003217 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003218 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003219 }
Mike Stump1eb44332009-09-09 15:08:12 +00003220
Douglas Gregor838db382010-02-11 01:19:42 +00003221 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003222 }
Mike Stump1eb44332009-09-09 15:08:12 +00003223
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003224 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3225}
3226
Mike Stumpadaaad32009-10-20 02:12:22 +00003227QualType ASTContext::getBlockDescriptorType() {
3228 if (BlockDescriptorType)
3229 return getTagDeclType(BlockDescriptorType);
3230
3231 RecordDecl *T;
3232 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003233 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003234 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00003235 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003236
3237 QualType FieldTypes[] = {
3238 UnsignedLongTy,
3239 UnsignedLongTy,
3240 };
3241
3242 const char *FieldNames[] = {
3243 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00003244 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00003245 };
3246
3247 for (size_t i = 0; i < 2; ++i) {
3248 FieldDecl *Field = FieldDecl::Create(*this,
3249 T,
3250 SourceLocation(),
3251 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003252 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00003253 /*BitWidth=*/0,
3254 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003255 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003256 T->addDecl(Field);
3257 }
3258
Douglas Gregor838db382010-02-11 01:19:42 +00003259 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003260
3261 BlockDescriptorType = T;
3262
3263 return getTagDeclType(BlockDescriptorType);
3264}
3265
3266void ASTContext::setBlockDescriptorType(QualType T) {
3267 const RecordType *Rec = T->getAs<RecordType>();
3268 assert(Rec && "Invalid BlockDescriptorType");
3269 BlockDescriptorType = Rec->getDecl();
3270}
3271
Mike Stump083c25e2009-10-22 00:49:09 +00003272QualType ASTContext::getBlockDescriptorExtendedType() {
3273 if (BlockDescriptorExtendedType)
3274 return getTagDeclType(BlockDescriptorExtendedType);
3275
3276 RecordDecl *T;
3277 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003278 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003279 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00003280 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003281
3282 QualType FieldTypes[] = {
3283 UnsignedLongTy,
3284 UnsignedLongTy,
3285 getPointerType(VoidPtrTy),
3286 getPointerType(VoidPtrTy)
3287 };
3288
3289 const char *FieldNames[] = {
3290 "reserved",
3291 "Size",
3292 "CopyFuncPtr",
3293 "DestroyFuncPtr"
3294 };
3295
3296 for (size_t i = 0; i < 4; ++i) {
3297 FieldDecl *Field = FieldDecl::Create(*this,
3298 T,
3299 SourceLocation(),
3300 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003301 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00003302 /*BitWidth=*/0,
3303 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003304 Field->setAccess(AS_public);
Mike Stump083c25e2009-10-22 00:49:09 +00003305 T->addDecl(Field);
3306 }
3307
Douglas Gregor838db382010-02-11 01:19:42 +00003308 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003309
3310 BlockDescriptorExtendedType = T;
3311
3312 return getTagDeclType(BlockDescriptorExtendedType);
3313}
3314
3315void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3316 const RecordType *Rec = T->getAs<RecordType>();
3317 assert(Rec && "Invalid BlockDescriptorType");
3318 BlockDescriptorExtendedType = Rec->getDecl();
3319}
3320
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003321bool ASTContext::BlockRequiresCopying(QualType Ty) {
3322 if (Ty->isBlockPointerType())
3323 return true;
3324 if (isObjCNSObjectType(Ty))
3325 return true;
3326 if (Ty->isObjCObjectPointerType())
3327 return true;
Fariborz Jahaniane38be612010-11-17 00:21:28 +00003328 if (getLangOptions().CPlusPlus) {
3329 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3330 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3331 return RD->hasConstCopyConstructor(*this);
3332
3333 }
3334 }
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003335 return false;
3336}
3337
Daniel Dunbar4087f272010-08-17 22:39:59 +00003338QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003339 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003340 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003341 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003342 // unsigned int __flags;
3343 // unsigned int __size;
Eli Friedmana7e68452010-08-22 01:00:03 +00003344 // void *__copy_helper; // as needed
3345 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003346 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003347 // } *
3348
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003349 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3350
3351 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003352 llvm::SmallString<36> Name;
3353 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3354 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003355 RecordDecl *T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003356 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003357 &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003358 T->startDefinition();
3359 QualType Int32Ty = IntTy;
3360 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3361 QualType FieldTypes[] = {
3362 getPointerType(VoidPtrTy),
3363 getPointerType(getTagDeclType(T)),
3364 Int32Ty,
3365 Int32Ty,
3366 getPointerType(VoidPtrTy),
3367 getPointerType(VoidPtrTy),
3368 Ty
3369 };
3370
Daniel Dunbar4087f272010-08-17 22:39:59 +00003371 llvm::StringRef FieldNames[] = {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003372 "__isa",
3373 "__forwarding",
3374 "__flags",
3375 "__size",
3376 "__copy_helper",
3377 "__destroy_helper",
3378 DeclName,
3379 };
3380
3381 for (size_t i = 0; i < 7; ++i) {
3382 if (!HasCopyAndDispose && i >=4 && i <= 5)
3383 continue;
3384 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3385 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003386 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003387 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003388 Field->setAccess(AS_public);
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003389 T->addDecl(Field);
3390 }
3391
Douglas Gregor838db382010-02-11 01:19:42 +00003392 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003393
3394 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003395}
3396
3397
3398QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00003399 bool BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +00003400 llvm::SmallVectorImpl<const Expr *> &Layout) {
3401
Mike Stumpadaaad32009-10-20 02:12:22 +00003402 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003403 llvm::SmallString<36> Name;
3404 llvm::raw_svector_ostream(Name) << "__block_literal_"
3405 << ++UniqueBlockParmTypeID;
Mike Stumpadaaad32009-10-20 02:12:22 +00003406 RecordDecl *T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003407 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003408 &Idents.get(Name.str()));
John McCall5cfa0112010-02-05 01:33:36 +00003409 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003410 QualType FieldTypes[] = {
3411 getPointerType(VoidPtrTy),
3412 IntTy,
3413 IntTy,
3414 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00003415 (BlockHasCopyDispose ?
3416 getPointerType(getBlockDescriptorExtendedType()) :
3417 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00003418 };
3419
3420 const char *FieldNames[] = {
3421 "__isa",
3422 "__flags",
3423 "__reserved",
3424 "__FuncPtr",
3425 "__descriptor"
3426 };
3427
3428 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00003429 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003430 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003431 FieldTypes[i], /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003432 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003433 Field->setAccess(AS_public);
Mike Stumpea26cb52009-10-21 03:49:08 +00003434 T->addDecl(Field);
3435 }
3436
John McCallea1471e2010-05-20 01:18:31 +00003437 for (unsigned i = 0; i < Layout.size(); ++i) {
3438 const Expr *E = Layout[i];
Mike Stumpea26cb52009-10-21 03:49:08 +00003439
John McCallea1471e2010-05-20 01:18:31 +00003440 QualType FieldType = E->getType();
3441 IdentifierInfo *FieldName = 0;
3442 if (isa<CXXThisExpr>(E)) {
3443 FieldName = &Idents.get("this");
3444 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3445 const ValueDecl *D = BDRE->getDecl();
3446 FieldName = D->getIdentifier();
3447 if (BDRE->isByRef())
Daniel Dunbar4087f272010-08-17 22:39:59 +00003448 FieldType = BuildByRefType(D->getName(), FieldType);
John McCallea1471e2010-05-20 01:18:31 +00003449 } else {
3450 // Padding.
3451 assert(isa<ConstantArrayType>(FieldType) &&
3452 isa<DeclRefExpr>(E) &&
3453 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3454 "doesn't match characteristics of padding decl");
3455 }
Mike Stumpea26cb52009-10-21 03:49:08 +00003456
3457 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallea1471e2010-05-20 01:18:31 +00003458 FieldName, FieldType, /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003459 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003460 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003461 T->addDecl(Field);
3462 }
3463
Douglas Gregor838db382010-02-11 01:19:42 +00003464 T->completeDefinition();
Mike Stumpea26cb52009-10-21 03:49:08 +00003465
3466 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00003467}
3468
Douglas Gregor319ac892009-04-23 22:29:11 +00003469void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003470 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003471 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3472 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3473}
3474
Anders Carlssone8c49532007-10-29 06:33:42 +00003475// This returns true if a type has been typedefed to BOOL:
3476// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003477static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003478 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003479 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3480 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003481
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003482 return false;
3483}
3484
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003485/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003486/// purpose.
Ken Dyckaa8741a2010-01-11 19:19:56 +00003487CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck199c3d62010-01-11 17:06:35 +00003488 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003489
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003490 // Make all integer and enum types at least as large as an int
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003491 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003492 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003493 // Treat arrays as pointers, since that's how they're passed in.
3494 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003495 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003496 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003497}
3498
3499static inline
3500std::string charUnitsToString(const CharUnits &CU) {
3501 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003502}
3503
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003504/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall5e530af2009-11-17 19:33:30 +00003505/// declaration.
3506void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3507 std::string& S) {
3508 const BlockDecl *Decl = Expr->getBlockDecl();
3509 QualType BlockTy =
3510 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3511 // Encode result type.
John McCallc71a4912010-06-04 19:02:56 +00003512 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall5e530af2009-11-17 19:33:30 +00003513 // Compute size of all parameters.
3514 // Start with computing size of a pointer in number of bytes.
3515 // FIXME: There might(should) be a better way of doing this computation!
3516 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003517 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3518 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003519 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall5e530af2009-11-17 19:33:30 +00003520 E = Decl->param_end(); PI != E; ++PI) {
3521 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003522 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003523 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003524 ParmOffset += sz;
3525 }
3526 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003527 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003528 // Block pointer and offset.
3529 S += "@?0";
3530 ParmOffset = PtrSize;
3531
3532 // Argument types.
3533 ParmOffset = PtrSize;
3534 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3535 Decl->param_end(); PI != E; ++PI) {
3536 ParmVarDecl *PVDecl = *PI;
3537 QualType PType = PVDecl->getOriginalType();
3538 if (const ArrayType *AT =
3539 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3540 // Use array's original type only if it has known number of
3541 // elements.
3542 if (!isa<ConstantArrayType>(AT))
3543 PType = PVDecl->getType();
3544 } else if (PType->isFunctionType())
3545 PType = PVDecl->getType();
3546 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003547 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003548 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003549 }
3550}
3551
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003552/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003553/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003554void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003555 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003556 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003557 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003558 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003559 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003560 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003561 // Compute size of all parameters.
3562 // Start with computing size of a pointer in number of bytes.
3563 // FIXME: There might(should) be a better way of doing this computation!
3564 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003565 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003566 // The first two arguments (self and _cmd) are pointers; account for
3567 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003568 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003569 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003570 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003571 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003572 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003573 assert (sz.isPositive() &&
3574 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003575 ParmOffset += sz;
3576 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003577 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003578 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003579 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003580
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003581 // Argument types.
3582 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003583 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003584 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003585 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003586 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003587 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003588 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3589 // Use array's original type only if it has known number of
3590 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003591 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003592 PType = PVDecl->getType();
3593 } else if (PType->isFunctionType())
3594 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003595 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003596 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003597 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003598 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003599 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003600 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003601 }
3602}
3603
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003604/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003605/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003606/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3607/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003608/// Property attributes are stored as a comma-delimited C string. The simple
3609/// attributes readonly and bycopy are encoded as single characters. The
3610/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3611/// encoded as single characters, followed by an identifier. Property types
3612/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003613/// these attributes are defined by the following enumeration:
3614/// @code
3615/// enum PropertyAttributes {
3616/// kPropertyReadOnly = 'R', // property is read-only.
3617/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3618/// kPropertyByref = '&', // property is a reference to the value last assigned
3619/// kPropertyDynamic = 'D', // property is dynamic
3620/// kPropertyGetter = 'G', // followed by getter selector name
3621/// kPropertySetter = 'S', // followed by setter selector name
3622/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3623/// kPropertyType = 't' // followed by old-style type encoding.
3624/// kPropertyWeak = 'W' // 'weak' property
3625/// kPropertyStrong = 'P' // property GC'able
3626/// kPropertyNonAtomic = 'N' // property non-atomic
3627/// };
3628/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003629void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003630 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003631 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003632 // Collect information from the property implementation decl(s).
3633 bool Dynamic = false;
3634 ObjCPropertyImplDecl *SynthesizePID = 0;
3635
3636 // FIXME: Duplicated code due to poor abstraction.
3637 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003638 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003639 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3640 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003641 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003642 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003643 ObjCPropertyImplDecl *PID = *i;
3644 if (PID->getPropertyDecl() == PD) {
3645 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3646 Dynamic = true;
3647 } else {
3648 SynthesizePID = PID;
3649 }
3650 }
3651 }
3652 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003653 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003654 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003655 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003656 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003657 ObjCPropertyImplDecl *PID = *i;
3658 if (PID->getPropertyDecl() == PD) {
3659 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3660 Dynamic = true;
3661 } else {
3662 SynthesizePID = PID;
3663 }
3664 }
Mike Stump1eb44332009-09-09 15:08:12 +00003665 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003666 }
3667 }
3668
3669 // FIXME: This is not very efficient.
3670 S = "T";
3671
3672 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003673 // GCC has some special rules regarding encoding of properties which
3674 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003675 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003676 true /* outermost type */,
3677 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003678
3679 if (PD->isReadOnly()) {
3680 S += ",R";
3681 } else {
3682 switch (PD->getSetterKind()) {
3683 case ObjCPropertyDecl::Assign: break;
3684 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003685 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003686 }
3687 }
3688
3689 // It really isn't clear at all what this means, since properties
3690 // are "dynamic by default".
3691 if (Dynamic)
3692 S += ",D";
3693
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003694 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3695 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003696
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003697 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3698 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003699 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003700 }
3701
3702 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3703 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003704 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003705 }
3706
3707 if (SynthesizePID) {
3708 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3709 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003710 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003711 }
3712
3713 // FIXME: OBJCGC: weak & strong
3714}
3715
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003716/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003717/// Another legacy compatibility encoding: 32-bit longs are encoded as
3718/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003719/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3720///
3721void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003722 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003723 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003724 if (BT->getKind() == BuiltinType::ULong &&
3725 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003726 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003727 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003728 if (BT->getKind() == BuiltinType::Long &&
3729 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003730 PointeeTy = IntTy;
3731 }
3732 }
3733}
3734
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003735void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003736 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003737 // We follow the behavior of gcc, expanding structures which are
3738 // directly pointed to, and expanding embedded structures. Note that
3739 // these rules are sufficient to prevent recursive encoding of the
3740 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003741 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003742 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003743}
3744
David Chisnall64fd7e82010-06-04 01:10:52 +00003745static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3746 switch (T->getAs<BuiltinType>()->getKind()) {
3747 default: assert(0 && "Unhandled builtin type kind");
3748 case BuiltinType::Void: return 'v';
3749 case BuiltinType::Bool: return 'B';
3750 case BuiltinType::Char_U:
3751 case BuiltinType::UChar: return 'C';
3752 case BuiltinType::UShort: return 'S';
3753 case BuiltinType::UInt: return 'I';
3754 case BuiltinType::ULong:
3755 return
3756 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3757 case BuiltinType::UInt128: return 'T';
3758 case BuiltinType::ULongLong: return 'Q';
3759 case BuiltinType::Char_S:
3760 case BuiltinType::SChar: return 'c';
3761 case BuiltinType::Short: return 's';
John McCall24da7092010-06-11 10:11:05 +00003762 case BuiltinType::WChar:
David Chisnall64fd7e82010-06-04 01:10:52 +00003763 case BuiltinType::Int: return 'i';
3764 case BuiltinType::Long:
3765 return
3766 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3767 case BuiltinType::LongLong: return 'q';
3768 case BuiltinType::Int128: return 't';
3769 case BuiltinType::Float: return 'f';
3770 case BuiltinType::Double: return 'd';
Daniel Dunbar3a0be842010-10-11 21:13:48 +00003771 case BuiltinType::LongDouble: return 'D';
David Chisnall64fd7e82010-06-04 01:10:52 +00003772 }
3773}
3774
Mike Stump1eb44332009-09-09 15:08:12 +00003775static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnall64fd7e82010-06-04 01:10:52 +00003776 QualType T, const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003777 const Expr *E = FD->getBitWidth();
3778 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3779 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003780 S += 'b';
David Chisnall64fd7e82010-06-04 01:10:52 +00003781 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3782 // The GNU runtime requires more information; bitfields are encoded as b,
3783 // then the offset (in bits) of the first element, then the type of the
3784 // bitfield, then the size in bits. For example, in this structure:
3785 //
3786 // struct
3787 // {
3788 // int integer;
3789 // int flags:2;
3790 // };
3791 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3792 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3793 // information is not especially sensible, but we're stuck with it for
3794 // compatibility with GCC, although providing it breaks anything that
3795 // actually uses runtime introspection and wants to work on both runtimes...
3796 if (!Ctx->getLangOptions().NeXTRuntime) {
3797 const RecordDecl *RD = FD->getParent();
3798 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3799 // FIXME: This same linear search is also used in ExprConstant - it might
3800 // be better if the FieldDecl stored its offset. We'd be increasing the
3801 // size of the object slightly, but saving some time every time it is used.
3802 unsigned i = 0;
3803 for (RecordDecl::field_iterator Field = RD->field_begin(),
3804 FieldEnd = RD->field_end();
3805 Field != FieldEnd; (void)++Field, ++i) {
3806 if (*Field == FD)
3807 break;
3808 }
3809 S += llvm::utostr(RL.getFieldOffset(i));
3810 S += ObjCEncodingForPrimitiveKind(Context, T);
3811 }
3812 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003813 S += llvm::utostr(N);
3814}
3815
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003816// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003817void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3818 bool ExpandPointedToStructures,
3819 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003820 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003821 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003822 bool EncodingProperty) {
David Chisnall64fd7e82010-06-04 01:10:52 +00003823 if (T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003824 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00003825 return EncodeBitField(this, S, T, FD);
3826 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003827 return;
3828 }
Mike Stump1eb44332009-09-09 15:08:12 +00003829
John McCall183700f2009-09-21 23:43:11 +00003830 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003831 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003832 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003833 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003834 return;
3835 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00003836
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003837 // encoding for pointer or r3eference types.
3838 QualType PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003839 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003840 if (PT->isObjCSelType()) {
3841 S += ':';
3842 return;
3843 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003844 PointeeTy = PT->getPointeeType();
3845 }
3846 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3847 PointeeTy = RT->getPointeeType();
3848 if (!PointeeTy.isNull()) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003849 bool isReadOnly = false;
3850 // For historical/compatibility reasons, the read-only qualifier of the
3851 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3852 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003853 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003854 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003855 if (OutermostType && T.isConstQualified()) {
3856 isReadOnly = true;
3857 S += 'r';
3858 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003859 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003860 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003861 while (P->getAs<PointerType>())
3862 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003863 if (P.isConstQualified()) {
3864 isReadOnly = true;
3865 S += 'r';
3866 }
3867 }
3868 if (isReadOnly) {
3869 // Another legacy compatibility encoding. Some ObjC qualifier and type
3870 // combinations need to be rearranged.
3871 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer02379412010-04-27 17:12:11 +00003872 if (llvm::StringRef(S).endswith("nr"))
3873 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003874 }
Mike Stump1eb44332009-09-09 15:08:12 +00003875
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003876 if (PointeeTy->isCharType()) {
3877 // char pointer types should be encoded as '*' unless it is a
3878 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003879 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003880 S += '*';
3881 return;
3882 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003883 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003884 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3885 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3886 S += '#';
3887 return;
3888 }
3889 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3890 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3891 S += '@';
3892 return;
3893 }
3894 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003895 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003896 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003897 getLegacyIntegralTypeEncoding(PointeeTy);
3898
Mike Stump1eb44332009-09-09 15:08:12 +00003899 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003900 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003901 return;
3902 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003903
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003904 if (const ArrayType *AT =
3905 // Ignore type qualifiers etc.
3906 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003907 if (isa<IncompleteArrayType>(AT)) {
3908 // Incomplete arrays are encoded as a pointer to the array element.
3909 S += '^';
3910
Mike Stump1eb44332009-09-09 15:08:12 +00003911 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003912 false, ExpandStructures, FD);
3913 } else {
3914 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003915
Anders Carlsson559a8332009-02-22 01:38:57 +00003916 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3917 S += llvm::utostr(CAT->getSize().getZExtValue());
3918 else {
3919 //Variable length arrays are encoded as a regular array with 0 elements.
3920 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3921 S += '0';
3922 }
Mike Stump1eb44332009-09-09 15:08:12 +00003923
3924 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003925 false, ExpandStructures, FD);
3926 S += ']';
3927 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003928 return;
3929 }
Mike Stump1eb44332009-09-09 15:08:12 +00003930
John McCall183700f2009-09-21 23:43:11 +00003931 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003932 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003933 return;
3934 }
Mike Stump1eb44332009-09-09 15:08:12 +00003935
Ted Kremenek6217b802009-07-29 21:53:49 +00003936 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003937 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003938 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003939 // Anonymous structures print as '?'
3940 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3941 S += II->getName();
Fariborz Jahanian6fb94392010-05-07 00:28:49 +00003942 if (ClassTemplateSpecializationDecl *Spec
3943 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3944 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3945 std::string TemplateArgsStr
3946 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00003947 TemplateArgs.data(),
3948 TemplateArgs.size(),
Fariborz Jahanian6fb94392010-05-07 00:28:49 +00003949 (*this).PrintingPolicy);
3950
3951 S += TemplateArgsStr;
3952 }
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003953 } else {
3954 S += '?';
3955 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003956 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003957 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003958 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3959 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003960 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003961 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003962 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003963 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003964 S += '"';
3965 }
Mike Stump1eb44332009-09-09 15:08:12 +00003966
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003967 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003968 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003969 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003970 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003971 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003972 QualType qt = Field->getType();
3973 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003974 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003975 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003976 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003977 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003978 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003979 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003980 return;
3981 }
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00003982
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003983 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003984 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00003985 EncodeBitField(this, S, T, FD);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003986 else
3987 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003988 return;
3989 }
Mike Stump1eb44332009-09-09 15:08:12 +00003990
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003991 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003992 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003993 return;
3994 }
Mike Stump1eb44332009-09-09 15:08:12 +00003995
John McCallc12c5bb2010-05-15 11:32:37 +00003996 // Ignore protocol qualifiers when mangling at this level.
3997 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3998 T = OT->getBaseType();
3999
John McCall0953e762009-09-24 19:53:00 +00004000 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004001 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00004002 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004003 S += '{';
4004 const IdentifierInfo *II = OI->getIdentifier();
4005 S += II->getName();
4006 S += '=';
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00004007 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
4008 DeepCollectObjCIvars(OI, true, Ivars);
4009 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4010 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4011 if (Field->isBitField())
4012 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004013 else
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00004014 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004015 }
4016 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004017 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004018 }
Mike Stump1eb44332009-09-09 15:08:12 +00004019
John McCall183700f2009-09-21 23:43:11 +00004020 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00004021 if (OPT->isObjCIdType()) {
4022 S += '@';
4023 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004024 }
Mike Stump1eb44332009-09-09 15:08:12 +00004025
Steve Naroff27d20a22009-10-28 22:03:49 +00004026 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4027 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4028 // Since this is a binary compatibility issue, need to consult with runtime
4029 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00004030 S += '#';
4031 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004032 }
Mike Stump1eb44332009-09-09 15:08:12 +00004033
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004034 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004035 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00004036 ExpandPointedToStructures,
4037 ExpandStructures, FD);
4038 if (FD || EncodingProperty) {
4039 // Note that we do extended encoding of protocol qualifer list
4040 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00004041 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00004042 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4043 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00004044 S += '<';
4045 S += (*I)->getNameAsString();
4046 S += '>';
4047 }
4048 S += '"';
4049 }
4050 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004051 }
Mike Stump1eb44332009-09-09 15:08:12 +00004052
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004053 QualType PointeeTy = OPT->getPointeeType();
4054 if (!EncodingProperty &&
4055 isa<TypedefType>(PointeeTy.getTypePtr())) {
4056 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00004057 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004058 // {...};
4059 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00004060 getObjCEncodingForTypeImpl(PointeeTy, S,
4061 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004062 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00004063 return;
4064 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004065
4066 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00004067 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004068 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00004069 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00004070 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4071 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004072 S += '<';
4073 S += (*I)->getNameAsString();
4074 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00004075 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004076 S += '"';
4077 }
4078 return;
4079 }
Mike Stump1eb44332009-09-09 15:08:12 +00004080
John McCall532ec7b2010-05-17 23:56:34 +00004081 // gcc just blithely ignores member pointers.
4082 // TODO: maybe there should be a mangling for these
4083 if (T->getAs<MemberPointerType>())
4084 return;
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00004085
4086 if (T->isVectorType()) {
4087 // This matches gcc's encoding, even though technically it is
4088 // insufficient.
4089 // FIXME. We should do a better job than gcc.
4090 return;
4091 }
4092
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004093 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004094}
4095
Mike Stump1eb44332009-09-09 15:08:12 +00004096void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00004097 std::string& S) const {
4098 if (QT & Decl::OBJC_TQ_In)
4099 S += 'n';
4100 if (QT & Decl::OBJC_TQ_Inout)
4101 S += 'N';
4102 if (QT & Decl::OBJC_TQ_Out)
4103 S += 'o';
4104 if (QT & Decl::OBJC_TQ_Bycopy)
4105 S += 'O';
4106 if (QT & Decl::OBJC_TQ_Byref)
4107 S += 'R';
4108 if (QT & Decl::OBJC_TQ_Oneway)
4109 S += 'V';
4110}
4111
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004112void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004113 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004114
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004115 BuiltinVaListType = T;
4116}
4117
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004118void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004119 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00004120}
4121
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004122void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00004123 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00004124}
4125
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004126void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004127 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00004128}
4129
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004130void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004131 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00004132}
4133
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004134void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00004135 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00004136 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004137
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004138 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00004139}
4140
John McCall0bd6feb2009-12-02 08:04:21 +00004141/// \brief Retrieve the template name that corresponds to a non-empty
4142/// lookup.
John McCalleec51cf2010-01-20 00:46:10 +00004143TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4144 UnresolvedSetIterator End) {
John McCall0bd6feb2009-12-02 08:04:21 +00004145 unsigned size = End - Begin;
4146 assert(size > 1 && "set is not overloaded!");
4147
4148 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4149 size * sizeof(FunctionTemplateDecl*));
4150 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4151
4152 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00004153 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00004154 NamedDecl *D = *I;
4155 assert(isa<FunctionTemplateDecl>(D) ||
4156 (isa<UsingShadowDecl>(D) &&
4157 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4158 *Storage++ = D;
4159 }
4160
4161 return TemplateName(OT);
4162}
4163
Douglas Gregor7532dc62009-03-30 22:58:21 +00004164/// \brief Retrieve the template name that represents a qualified
4165/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00004166TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004167 bool TemplateKeyword,
4168 TemplateDecl *Template) {
Douglas Gregor789b1f62010-02-04 18:10:26 +00004169 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00004170 llvm::FoldingSetNodeID ID;
4171 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4172
4173 void *InsertPos = 0;
4174 QualifiedTemplateName *QTN =
4175 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4176 if (!QTN) {
4177 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4178 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4179 }
4180
4181 return TemplateName(QTN);
4182}
4183
4184/// \brief Retrieve the template name that represents a dependent
4185/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00004186TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004187 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004188 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00004189 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00004190
4191 llvm::FoldingSetNodeID ID;
4192 DependentTemplateName::Profile(ID, NNS, Name);
4193
4194 void *InsertPos = 0;
4195 DependentTemplateName *QTN =
4196 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4197
4198 if (QTN)
4199 return TemplateName(QTN);
4200
4201 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4202 if (CanonNNS == NNS) {
4203 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4204 } else {
4205 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4206 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004207 DependentTemplateName *CheckQTN =
4208 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4209 assert(!CheckQTN && "Dependent type name canonicalization broken");
4210 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00004211 }
4212
4213 DependentTemplateNames.InsertNode(QTN, InsertPos);
4214 return TemplateName(QTN);
4215}
4216
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004217/// \brief Retrieve the template name that represents a dependent
4218/// template name such as \c MetaFun::template operator+.
4219TemplateName
4220ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4221 OverloadedOperatorKind Operator) {
4222 assert((!NNS || NNS->isDependent()) &&
4223 "Nested name specifier must be dependent");
4224
4225 llvm::FoldingSetNodeID ID;
4226 DependentTemplateName::Profile(ID, NNS, Operator);
4227
4228 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00004229 DependentTemplateName *QTN
4230 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004231
4232 if (QTN)
4233 return TemplateName(QTN);
4234
4235 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4236 if (CanonNNS == NNS) {
4237 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4238 } else {
4239 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4240 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004241
4242 DependentTemplateName *CheckQTN
4243 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4244 assert(!CheckQTN && "Dependent template name canonicalization broken");
4245 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004246 }
4247
4248 DependentTemplateNames.InsertNode(QTN, InsertPos);
4249 return TemplateName(QTN);
4250}
4251
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004252/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00004253/// TargetInfo, produce the corresponding type. The unsigned @p Type
4254/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00004255CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004256 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00004257 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004258 case TargetInfo::SignedShort: return ShortTy;
4259 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4260 case TargetInfo::SignedInt: return IntTy;
4261 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4262 case TargetInfo::SignedLong: return LongTy;
4263 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4264 case TargetInfo::SignedLongLong: return LongLongTy;
4265 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4266 }
4267
4268 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00004269 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004270}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00004271
4272//===----------------------------------------------------------------------===//
4273// Type Predicates.
4274//===----------------------------------------------------------------------===//
4275
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004276/// isObjCNSObjectType - Return true if this is an NSObject object using
4277/// NSObject attribute on a c-style pointer type.
4278/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00004279/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004280///
4281bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4282 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4283 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00004284 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004285 return true;
4286 }
Mike Stump1eb44332009-09-09 15:08:12 +00004287 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004288}
4289
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004290/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4291/// garbage collection attribute.
4292///
John McCall0953e762009-09-24 19:53:00 +00004293Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4294 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004295 if (getLangOptions().ObjC1 &&
4296 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00004297 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004298 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00004299 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004300 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00004301 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00004302 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004303 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004304 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00004305 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004306 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00004307 // Non-pointers have none gc'able attribute regardless of the attribute
4308 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00004309 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004310 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004311 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00004312 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004313}
4314
Chris Lattner6ac46a42008-04-07 06:51:04 +00004315//===----------------------------------------------------------------------===//
4316// Type Compatibility Testing
4317//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00004318
Mike Stump1eb44332009-09-09 15:08:12 +00004319/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004320/// compatible.
4321static bool areCompatVectorTypes(const VectorType *LHS,
4322 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00004323 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00004324 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00004325 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00004326}
4327
Douglas Gregor255210e2010-08-06 10:14:59 +00004328bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4329 QualType SecondVec) {
4330 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4331 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4332
4333 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4334 return true;
4335
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004336 // Treat Neon vector types and most AltiVec vector types as if they are the
4337 // equivalent GCC vector types.
Douglas Gregor255210e2010-08-06 10:14:59 +00004338 const VectorType *First = FirstVec->getAs<VectorType>();
4339 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004340 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor255210e2010-08-06 10:14:59 +00004341 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004342 First->getVectorKind() != VectorType::AltiVecPixel &&
4343 First->getVectorKind() != VectorType::AltiVecBool &&
4344 Second->getVectorKind() != VectorType::AltiVecPixel &&
4345 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor255210e2010-08-06 10:14:59 +00004346 return true;
4347
4348 return false;
4349}
4350
Steve Naroff4084c302009-07-23 01:01:38 +00004351//===----------------------------------------------------------------------===//
4352// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4353//===----------------------------------------------------------------------===//
4354
4355/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4356/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004357bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4358 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00004359 if (lProto == rProto)
4360 return true;
4361 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4362 E = rProto->protocol_end(); PI != E; ++PI)
4363 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4364 return true;
4365 return false;
4366}
4367
Steve Naroff4084c302009-07-23 01:01:38 +00004368/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4369/// return true if lhs's protocols conform to rhs's protocol; false
4370/// otherwise.
4371bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4372 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4373 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4374 return false;
4375}
4376
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004377/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4378/// Class<p1, ...>.
4379bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4380 QualType rhs) {
4381 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4382 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4383 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4384
4385 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4386 E = lhsQID->qual_end(); I != E; ++I) {
4387 bool match = false;
4388 ObjCProtocolDecl *lhsProto = *I;
4389 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4390 E = rhsOPT->qual_end(); J != E; ++J) {
4391 ObjCProtocolDecl *rhsProto = *J;
4392 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4393 match = true;
4394 break;
4395 }
4396 }
4397 if (!match)
4398 return false;
4399 }
4400 return true;
4401}
4402
Steve Naroff4084c302009-07-23 01:01:38 +00004403/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4404/// ObjCQualifiedIDType.
4405bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4406 bool compare) {
4407 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00004408 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004409 lhs->isObjCIdType() || lhs->isObjCClassType())
4410 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004411 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004412 rhs->isObjCIdType() || rhs->isObjCClassType())
4413 return true;
4414
4415 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00004416 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004417
Steve Naroff4084c302009-07-23 01:01:38 +00004418 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004419
Steve Naroff4084c302009-07-23 01:01:38 +00004420 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004421 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00004422 // make sure we check the class hierarchy.
4423 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4424 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4425 E = lhsQID->qual_end(); I != E; ++I) {
4426 // when comparing an id<P> on lhs with a static type on rhs,
4427 // see if static class implements all of id's protocols, directly or
4428 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004429 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00004430 return false;
4431 }
4432 }
4433 // If there are no qualifiers and no interface, we have an 'id'.
4434 return true;
4435 }
Mike Stump1eb44332009-09-09 15:08:12 +00004436 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004437 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4438 E = lhsQID->qual_end(); I != E; ++I) {
4439 ObjCProtocolDecl *lhsProto = *I;
4440 bool match = false;
4441
4442 // when comparing an id<P> on lhs with a static type on rhs,
4443 // see if static class implements all of id's protocols, directly or
4444 // through its super class and categories.
4445 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4446 E = rhsOPT->qual_end(); J != E; ++J) {
4447 ObjCProtocolDecl *rhsProto = *J;
4448 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4449 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4450 match = true;
4451 break;
4452 }
4453 }
Mike Stump1eb44332009-09-09 15:08:12 +00004454 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004455 // make sure we check the class hierarchy.
4456 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4457 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4458 E = lhsQID->qual_end(); I != E; ++I) {
4459 // when comparing an id<P> on lhs with a static type on rhs,
4460 // see if static class implements all of id's protocols, directly or
4461 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004462 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004463 match = true;
4464 break;
4465 }
4466 }
4467 }
4468 if (!match)
4469 return false;
4470 }
Mike Stump1eb44332009-09-09 15:08:12 +00004471
Steve Naroff4084c302009-07-23 01:01:38 +00004472 return true;
4473 }
Mike Stump1eb44332009-09-09 15:08:12 +00004474
Steve Naroff4084c302009-07-23 01:01:38 +00004475 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4476 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4477
Mike Stump1eb44332009-09-09 15:08:12 +00004478 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004479 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004480 // If both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004481 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4482 E = lhsOPT->qual_end(); I != E; ++I) {
4483 ObjCProtocolDecl *lhsProto = *I;
4484 bool match = false;
4485
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004486 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff4084c302009-07-23 01:01:38 +00004487 // see if static class implements all of id's protocols, directly or
4488 // through its super class and categories.
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004489 // First, lhs protocols in the qualifier list must be found, direct
4490 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff4084c302009-07-23 01:01:38 +00004491 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4492 E = rhsQID->qual_end(); J != E; ++J) {
4493 ObjCProtocolDecl *rhsProto = *J;
4494 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4495 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4496 match = true;
4497 break;
4498 }
4499 }
4500 if (!match)
4501 return false;
4502 }
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004503
4504 // Static class's protocols, or its super class or category protocols
4505 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4506 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4507 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4508 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4509 // This is rather dubious but matches gcc's behavior. If lhs has
4510 // no type qualifier and its class has no static protocol(s)
4511 // assume that it is mismatch.
4512 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4513 return false;
4514 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4515 LHSInheritedProtocols.begin(),
4516 E = LHSInheritedProtocols.end(); I != E; ++I) {
4517 bool match = false;
4518 ObjCProtocolDecl *lhsProto = (*I);
4519 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4520 E = rhsQID->qual_end(); J != E; ++J) {
4521 ObjCProtocolDecl *rhsProto = *J;
4522 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4523 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4524 match = true;
4525 break;
4526 }
4527 }
4528 if (!match)
4529 return false;
4530 }
4531 }
Steve Naroff4084c302009-07-23 01:01:38 +00004532 return true;
4533 }
4534 return false;
4535}
4536
Eli Friedman3d815e72008-08-22 00:56:42 +00004537/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004538/// compatible for assignment from RHS to LHS. This handles validation of any
4539/// protocol qualifiers on the LHS or RHS.
4540///
Steve Naroff14108da2009-07-10 23:34:53 +00004541bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4542 const ObjCObjectPointerType *RHSOPT) {
John McCallc12c5bb2010-05-15 11:32:37 +00004543 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4544 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4545
Steve Naroffde2e22d2009-07-15 18:40:39 +00004546 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCallc12c5bb2010-05-15 11:32:37 +00004547 if (LHS->isObjCUnqualifiedIdOrClass() ||
4548 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff14108da2009-07-10 23:34:53 +00004549 return true;
4550
John McCallc12c5bb2010-05-15 11:32:37 +00004551 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump1eb44332009-09-09 15:08:12 +00004552 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4553 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004554 false);
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004555
4556 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4557 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4558 QualType(RHSOPT,0));
4559
John McCallc12c5bb2010-05-15 11:32:37 +00004560 // If we have 2 user-defined types, fall into that path.
4561 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff4084c302009-07-23 01:01:38 +00004562 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004563
Steve Naroff4084c302009-07-23 01:01:38 +00004564 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004565}
4566
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004567/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4568/// for providing type-safty for objective-c pointers used to pass/return
4569/// arguments in block literals. When passed as arguments, passing 'A*' where
4570/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4571/// not OK. For the return type, the opposite is not OK.
4572bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4573 const ObjCObjectPointerType *LHSOPT,
4574 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004575 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004576 return true;
4577
4578 if (LHSOPT->isObjCBuiltinType()) {
4579 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4580 }
4581
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004582 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004583 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4584 QualType(RHSOPT,0),
4585 false);
4586
4587 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4588 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4589 if (LHS && RHS) { // We have 2 user-defined types.
4590 if (LHS != RHS) {
4591 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4592 return false;
4593 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4594 return true;
4595 }
4596 else
4597 return true;
4598 }
4599 return false;
4600}
4601
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004602/// getIntersectionOfProtocols - This routine finds the intersection of set
4603/// of protocols inherited from two distinct objective-c pointer objects.
4604/// It is used to build composite qualifier list of the composite type of
4605/// the conditional expression involving two objective-c pointer objects.
4606static
4607void getIntersectionOfProtocols(ASTContext &Context,
4608 const ObjCObjectPointerType *LHSOPT,
4609 const ObjCObjectPointerType *RHSOPT,
4610 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4611
John McCallc12c5bb2010-05-15 11:32:37 +00004612 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4613 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4614 assert(LHS->getInterface() && "LHS must have an interface base");
4615 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004616
4617 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4618 unsigned LHSNumProtocols = LHS->getNumProtocols();
4619 if (LHSNumProtocols > 0)
4620 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4621 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004622 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004623 Context.CollectInheritedProtocols(LHS->getInterface(),
4624 LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004625 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4626 LHSInheritedProtocols.end());
4627 }
4628
4629 unsigned RHSNumProtocols = RHS->getNumProtocols();
4630 if (RHSNumProtocols > 0) {
Dan Gohmancb421fa2010-04-19 16:39:44 +00004631 ObjCProtocolDecl **RHSProtocols =
4632 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004633 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4634 if (InheritedProtocolSet.count(RHSProtocols[i]))
4635 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4636 }
4637 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004638 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004639 Context.CollectInheritedProtocols(RHS->getInterface(),
4640 RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004641 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4642 RHSInheritedProtocols.begin(),
4643 E = RHSInheritedProtocols.end(); I != E; ++I)
4644 if (InheritedProtocolSet.count((*I)))
4645 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004646 }
4647}
4648
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004649/// areCommonBaseCompatible - Returns common base class of the two classes if
4650/// one found. Note that this is O'2 algorithm. But it will be called as the
4651/// last type comparison in a ?-exp of ObjC pointer types before a
4652/// warning is issued. So, its invokation is extremely rare.
4653QualType ASTContext::areCommonBaseCompatible(
John McCallc12c5bb2010-05-15 11:32:37 +00004654 const ObjCObjectPointerType *Lptr,
4655 const ObjCObjectPointerType *Rptr) {
4656 const ObjCObjectType *LHS = Lptr->getObjectType();
4657 const ObjCObjectType *RHS = Rptr->getObjectType();
4658 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4659 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4660 if (!LDecl || !RDecl)
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004661 return QualType();
4662
John McCallc12c5bb2010-05-15 11:32:37 +00004663 while ((LDecl = LDecl->getSuperClass())) {
4664 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004665 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCallc12c5bb2010-05-15 11:32:37 +00004666 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4667 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4668
4669 QualType Result = QualType(LHS, 0);
4670 if (!Protocols.empty())
4671 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4672 Result = getObjCObjectPointerType(Result);
4673 return Result;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004674 }
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004675 }
4676
4677 return QualType();
4678}
4679
John McCallc12c5bb2010-05-15 11:32:37 +00004680bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4681 const ObjCObjectType *RHS) {
4682 assert(LHS->getInterface() && "LHS is not an interface type");
4683 assert(RHS->getInterface() && "RHS is not an interface type");
4684
Chris Lattner6ac46a42008-04-07 06:51:04 +00004685 // Verify that the base decls are compatible: the RHS must be a subclass of
4686 // the LHS.
John McCallc12c5bb2010-05-15 11:32:37 +00004687 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner6ac46a42008-04-07 06:51:04 +00004688 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004689
Chris Lattner6ac46a42008-04-07 06:51:04 +00004690 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4691 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004692 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004693 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004694
Chris Lattner6ac46a42008-04-07 06:51:04 +00004695 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4696 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004697 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004698 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00004699
John McCallc12c5bb2010-05-15 11:32:37 +00004700 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4701 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004702 LHSPI != LHSPE; LHSPI++) {
4703 bool RHSImplementsProtocol = false;
4704
4705 // If the RHS doesn't implement the protocol on the left, the types
4706 // are incompatible.
John McCallc12c5bb2010-05-15 11:32:37 +00004707 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4708 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00004709 RHSPI != RHSPE; RHSPI++) {
4710 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004711 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00004712 break;
4713 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004714 }
4715 // FIXME: For better diagnostics, consider passing back the protocol name.
4716 if (!RHSImplementsProtocol)
4717 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004718 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004719 // The RHS implements all protocols listed on the LHS.
4720 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004721}
4722
Steve Naroff389bf462009-02-12 17:52:19 +00004723bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4724 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00004725 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4726 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004727
Steve Naroff14108da2009-07-10 23:34:53 +00004728 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00004729 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004730
4731 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4732 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00004733}
4734
Douglas Gregor569c3162010-08-07 11:51:51 +00004735bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
4736 return canAssignObjCInterfaces(
4737 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
4738 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
4739}
4740
Mike Stump1eb44332009-09-09 15:08:12 +00004741/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00004742/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00004743/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00004744/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor447234d2010-07-29 15:18:02 +00004745bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
4746 bool CompareUnqualified) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004747 if (getLangOptions().CPlusPlus)
4748 return hasSameType(LHS, RHS);
4749
Douglas Gregor447234d2010-07-29 15:18:02 +00004750 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman3d815e72008-08-22 00:56:42 +00004751}
4752
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004753bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4754 return !mergeTypes(LHS, RHS, true).isNull();
4755}
4756
Peter Collingbourne48466752010-10-24 18:30:18 +00004757/// mergeTransparentUnionType - if T is a transparent union type and a member
4758/// of T is compatible with SubType, return the merged type, else return
4759/// QualType()
4760QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
4761 bool OfBlockPointer,
4762 bool Unqualified) {
4763 if (const RecordType *UT = T->getAsUnionType()) {
4764 RecordDecl *UD = UT->getDecl();
4765 if (UD->hasAttr<TransparentUnionAttr>()) {
4766 for (RecordDecl::field_iterator it = UD->field_begin(),
4767 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbournef91d7572010-12-02 21:00:06 +00004768 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbourne48466752010-10-24 18:30:18 +00004769 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
4770 if (!MT.isNull())
4771 return MT;
4772 }
4773 }
4774 }
4775
4776 return QualType();
4777}
4778
4779/// mergeFunctionArgumentTypes - merge two types which appear as function
4780/// argument types
4781QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
4782 bool OfBlockPointer,
4783 bool Unqualified) {
4784 // GNU extension: two types are compatible if they appear as a function
4785 // argument, one of the types is a transparent union type and the other
4786 // type is compatible with a union member
4787 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
4788 Unqualified);
4789 if (!lmerge.isNull())
4790 return lmerge;
4791
4792 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
4793 Unqualified);
4794 if (!rmerge.isNull())
4795 return rmerge;
4796
4797 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
4798}
4799
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004800QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor447234d2010-07-29 15:18:02 +00004801 bool OfBlockPointer,
4802 bool Unqualified) {
John McCall183700f2009-09-21 23:43:11 +00004803 const FunctionType *lbase = lhs->getAs<FunctionType>();
4804 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00004805 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4806 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00004807 bool allLTypes = true;
4808 bool allRTypes = true;
4809
4810 // Check return type
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004811 QualType retType;
4812 if (OfBlockPointer)
Douglas Gregor447234d2010-07-29 15:18:02 +00004813 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true,
4814 Unqualified);
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004815 else
Douglas Gregor447234d2010-07-29 15:18:02 +00004816 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(),
4817 false, Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004818 if (retType.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004819
4820 if (Unqualified)
4821 retType = retType.getUnqualifiedType();
4822
4823 CanQualType LRetType = getCanonicalType(lbase->getResultType());
4824 CanQualType RRetType = getCanonicalType(rbase->getResultType());
4825 if (Unqualified) {
4826 LRetType = LRetType.getUnqualifiedType();
4827 RRetType = RRetType.getUnqualifiedType();
4828 }
4829
4830 if (getCanonicalType(retType) != LRetType)
Chris Lattner61710852008-10-05 17:34:18 +00004831 allLTypes = false;
Douglas Gregor447234d2010-07-29 15:18:02 +00004832 if (getCanonicalType(retType) != RRetType)
Chris Lattner61710852008-10-05 17:34:18 +00004833 allRTypes = false;
Daniel Dunbar6a15c852010-04-28 16:20:58 +00004834 // FIXME: double check this
4835 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4836 // rbase->getRegParmAttr() != 0 &&
4837 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindola264ba482010-03-30 20:24:48 +00004838 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4839 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbar6a15c852010-04-28 16:20:58 +00004840 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4841 lbaseInfo.getRegParm();
4842 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4843 if (NoReturn != lbaseInfo.getNoReturn() ||
4844 RegParm != lbaseInfo.getRegParm())
4845 allLTypes = false;
4846 if (NoReturn != rbaseInfo.getNoReturn() ||
4847 RegParm != rbaseInfo.getRegParm())
4848 allRTypes = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00004849 CallingConv lcc = lbaseInfo.getCC();
4850 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004851 // Compatible functions must have compatible calling conventions
John McCall04a67a62010-02-05 21:31:56 +00004852 if (!isSameCallConv(lcc, rcc))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004853 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004854
Eli Friedman3d815e72008-08-22 00:56:42 +00004855 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00004856 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4857 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004858 unsigned lproto_nargs = lproto->getNumArgs();
4859 unsigned rproto_nargs = rproto->getNumArgs();
4860
4861 // Compatible functions must have the same number of arguments
4862 if (lproto_nargs != rproto_nargs)
4863 return QualType();
4864
4865 // Variadic and non-variadic functions aren't compatible
4866 if (lproto->isVariadic() != rproto->isVariadic())
4867 return QualType();
4868
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00004869 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4870 return QualType();
4871
Eli Friedman3d815e72008-08-22 00:56:42 +00004872 // Check argument compatibility
4873 llvm::SmallVector<QualType, 10> types;
4874 for (unsigned i = 0; i < lproto_nargs; i++) {
4875 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4876 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbourne48466752010-10-24 18:30:18 +00004877 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
4878 OfBlockPointer,
4879 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004880 if (argtype.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004881
4882 if (Unqualified)
4883 argtype = argtype.getUnqualifiedType();
4884
Eli Friedman3d815e72008-08-22 00:56:42 +00004885 types.push_back(argtype);
Douglas Gregor447234d2010-07-29 15:18:02 +00004886 if (Unqualified) {
4887 largtype = largtype.getUnqualifiedType();
4888 rargtype = rargtype.getUnqualifiedType();
4889 }
4890
Chris Lattner61710852008-10-05 17:34:18 +00004891 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4892 allLTypes = false;
4893 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4894 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00004895 }
4896 if (allLTypes) return lhs;
4897 if (allRTypes) return rhs;
4898 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00004899 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindola264ba482010-03-30 20:24:48 +00004900 false, false, 0, 0,
Rafael Espindola425ef722010-03-30 22:15:11 +00004901 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman3d815e72008-08-22 00:56:42 +00004902 }
4903
4904 if (lproto) allRTypes = false;
4905 if (rproto) allLTypes = false;
4906
Douglas Gregor72564e72009-02-26 23:50:07 +00004907 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00004908 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00004909 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004910 if (proto->isVariadic()) return QualType();
4911 // Check that the types are compatible with the types that
4912 // would result from default argument promotions (C99 6.7.5.3p15).
4913 // The only types actually affected are promotable integer
4914 // types and floats, which would be passed as a different
4915 // type depending on whether the prototype is visible.
4916 unsigned proto_nargs = proto->getNumArgs();
4917 for (unsigned i = 0; i < proto_nargs; ++i) {
4918 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00004919
4920 // Look at the promotion type of enum types, since that is the type used
4921 // to pass enum values.
4922 if (const EnumType *Enum = argTy->getAs<EnumType>())
4923 argTy = Enum->getDecl()->getPromotionType();
4924
Eli Friedman3d815e72008-08-22 00:56:42 +00004925 if (argTy->isPromotableIntegerType() ||
4926 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4927 return QualType();
4928 }
4929
4930 if (allLTypes) return lhs;
4931 if (allRTypes) return rhs;
4932 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00004933 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindola264ba482010-03-30 20:24:48 +00004934 proto->getTypeQuals(),
4935 false, false, 0, 0,
Rafael Espindola425ef722010-03-30 22:15:11 +00004936 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman3d815e72008-08-22 00:56:42 +00004937 }
4938
4939 if (allLTypes) return lhs;
4940 if (allRTypes) return rhs;
Rafael Espindola425ef722010-03-30 22:15:11 +00004941 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindola264ba482010-03-30 20:24:48 +00004942 return getFunctionNoProtoType(retType, Info);
Eli Friedman3d815e72008-08-22 00:56:42 +00004943}
4944
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004945QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor447234d2010-07-29 15:18:02 +00004946 bool OfBlockPointer,
4947 bool Unqualified) {
Bill Wendling43d69752007-12-03 07:33:35 +00004948 // C++ [expr]: If an expression initially has the type "reference to T", the
4949 // type is adjusted to "T" prior to any further analysis, the expression
4950 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004951 // expression is an lvalue unless the reference is an rvalue reference and
4952 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004953 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4954 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor447234d2010-07-29 15:18:02 +00004955
4956 if (Unqualified) {
4957 LHS = LHS.getUnqualifiedType();
4958 RHS = RHS.getUnqualifiedType();
4959 }
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004960
Eli Friedman3d815e72008-08-22 00:56:42 +00004961 QualType LHSCan = getCanonicalType(LHS),
4962 RHSCan = getCanonicalType(RHS);
4963
4964 // If two types are identical, they are compatible.
4965 if (LHSCan == RHSCan)
4966 return LHS;
4967
John McCall0953e762009-09-24 19:53:00 +00004968 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00004969 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4970 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00004971 if (LQuals != RQuals) {
4972 // If any of these qualifiers are different, we have a type
4973 // mismatch.
4974 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4975 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4976 return QualType();
4977
4978 // Exactly one GC qualifier difference is allowed: __strong is
4979 // okay if the other type has no GC qualifier but is an Objective
4980 // C object pointer (i.e. implicitly strong by default). We fix
4981 // this by pretending that the unqualified type was actually
4982 // qualified __strong.
4983 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4984 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4985 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4986
4987 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4988 return QualType();
4989
4990 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4991 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4992 }
4993 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4994 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4995 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004996 return QualType();
John McCall0953e762009-09-24 19:53:00 +00004997 }
4998
4999 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00005000
Eli Friedman852d63b2009-06-01 01:22:52 +00005001 Type::TypeClass LHSClass = LHSCan->getTypeClass();
5002 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00005003
Chris Lattner1adb8832008-01-14 05:45:46 +00005004 // We want to consider the two function types to be the same for these
5005 // comparisons, just force one to the other.
5006 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5007 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00005008
5009 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00005010 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5011 LHSClass = Type::ConstantArray;
5012 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5013 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00005014
John McCallc12c5bb2010-05-15 11:32:37 +00005015 // ObjCInterfaces are just specialized ObjCObjects.
5016 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5017 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5018
Nate Begeman213541a2008-04-18 23:10:10 +00005019 // Canonicalize ExtVector -> Vector.
5020 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5021 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00005022
Chris Lattnera36a61f2008-04-07 05:43:21 +00005023 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00005024 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00005025 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00005026 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00005027 // Compatibility is based on the underlying type, not the promotion
5028 // type.
John McCall183700f2009-09-21 23:43:11 +00005029 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00005030 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5031 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00005032 }
John McCall183700f2009-09-21 23:43:11 +00005033 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00005034 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5035 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00005036 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005037
Eli Friedman3d815e72008-08-22 00:56:42 +00005038 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00005039 }
Eli Friedman3d815e72008-08-22 00:56:42 +00005040
Steve Naroff4a746782008-01-09 22:43:08 +00005041 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00005042 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00005043#define TYPE(Class, Base)
5044#define ABSTRACT_TYPE(Class, Base)
John McCallad5e7382010-03-01 23:49:17 +00005045#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +00005046#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5047#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5048#include "clang/AST/TypeNodes.def"
5049 assert(false && "Non-canonical and dependent types shouldn't get here");
5050 return QualType();
5051
Sebastian Redl7c80bd62009-03-16 23:22:08 +00005052 case Type::LValueReference:
5053 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00005054 case Type::MemberPointer:
5055 assert(false && "C++ should never be in mergeTypes");
5056 return QualType();
5057
John McCallc12c5bb2010-05-15 11:32:37 +00005058 case Type::ObjCInterface:
Douglas Gregor72564e72009-02-26 23:50:07 +00005059 case Type::IncompleteArray:
5060 case Type::VariableArray:
5061 case Type::FunctionProto:
5062 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00005063 assert(false && "Types are eliminated above");
5064 return QualType();
5065
Chris Lattner1adb8832008-01-14 05:45:46 +00005066 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00005067 {
5068 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00005069 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5070 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005071 if (Unqualified) {
5072 LHSPointee = LHSPointee.getUnqualifiedType();
5073 RHSPointee = RHSPointee.getUnqualifiedType();
5074 }
5075 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5076 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005077 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00005078 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005079 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00005080 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005081 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005082 return getPointerType(ResultType);
5083 }
Steve Naroffc0febd52008-12-10 17:49:55 +00005084 case Type::BlockPointer:
5085 {
5086 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00005087 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5088 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005089 if (Unqualified) {
5090 LHSPointee = LHSPointee.getUnqualifiedType();
5091 RHSPointee = RHSPointee.getUnqualifiedType();
5092 }
5093 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5094 Unqualified);
Steve Naroffc0febd52008-12-10 17:49:55 +00005095 if (ResultType.isNull()) return QualType();
5096 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5097 return LHS;
5098 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5099 return RHS;
5100 return getBlockPointerType(ResultType);
5101 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005102 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00005103 {
5104 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5105 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5106 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5107 return QualType();
5108
5109 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5110 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005111 if (Unqualified) {
5112 LHSElem = LHSElem.getUnqualifiedType();
5113 RHSElem = RHSElem.getUnqualifiedType();
5114 }
5115
5116 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005117 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00005118 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5119 return LHS;
5120 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5121 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00005122 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5123 ArrayType::ArraySizeModifier(), 0);
5124 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5125 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005126 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5127 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00005128 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5129 return LHS;
5130 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5131 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005132 if (LVAT) {
5133 // FIXME: This isn't correct! But tricky to implement because
5134 // the array's size has to be the size of LHS, but the type
5135 // has to be different.
5136 return LHS;
5137 }
5138 if (RVAT) {
5139 // FIXME: This isn't correct! But tricky to implement because
5140 // the array's size has to be the size of RHS, but the type
5141 // has to be different.
5142 return RHS;
5143 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00005144 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5145 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005146 return getIncompleteArrayType(ResultType,
5147 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005148 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005149 case Type::FunctionNoProto:
Douglas Gregor447234d2010-07-29 15:18:02 +00005150 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregor72564e72009-02-26 23:50:07 +00005151 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00005152 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00005153 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00005154 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005155 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00005156 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00005157 case Type::Complex:
5158 // Distinct complex types are incompatible.
5159 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005160 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005161 // FIXME: The merged type should be an ExtVector!
John McCall1c471f32010-03-12 23:14:13 +00005162 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5163 RHSCan->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00005164 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00005165 return QualType();
John McCallc12c5bb2010-05-15 11:32:37 +00005166 case Type::ObjCObject: {
5167 // Check if the types are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005168 // FIXME: This should be type compatibility, e.g. whether
5169 // "LHS x; RHS x;" at global scope is legal.
John McCallc12c5bb2010-05-15 11:32:37 +00005170 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5171 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5172 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff5fd659d2009-02-21 16:18:07 +00005173 return LHS;
5174
Eli Friedman3d815e72008-08-22 00:56:42 +00005175 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00005176 }
Steve Naroff14108da2009-07-10 23:34:53 +00005177 case Type::ObjCObjectPointer: {
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005178 if (OfBlockPointer) {
5179 if (canAssignObjCInterfacesInBlockPointer(
5180 LHS->getAs<ObjCObjectPointerType>(),
5181 RHS->getAs<ObjCObjectPointerType>()))
5182 return LHS;
5183 return QualType();
5184 }
John McCall183700f2009-09-21 23:43:11 +00005185 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5186 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00005187 return LHS;
5188
Steve Naroffbc76dd02008-12-10 22:14:21 +00005189 return QualType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005190 }
Steve Naroffec0550f2007-10-15 20:41:53 +00005191 }
Douglas Gregor72564e72009-02-26 23:50:07 +00005192
5193 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00005194}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00005195
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005196/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5197/// 'RHS' attributes and returns the merged version; including for function
5198/// return types.
5199QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5200 QualType LHSCan = getCanonicalType(LHS),
5201 RHSCan = getCanonicalType(RHS);
5202 // If two types are identical, they are compatible.
5203 if (LHSCan == RHSCan)
5204 return LHS;
5205 if (RHSCan->isFunctionType()) {
5206 if (!LHSCan->isFunctionType())
5207 return QualType();
5208 QualType OldReturnType =
5209 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5210 QualType NewReturnType =
5211 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5212 QualType ResReturnType =
5213 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5214 if (ResReturnType.isNull())
5215 return QualType();
5216 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5217 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5218 // In either case, use OldReturnType to build the new function type.
5219 const FunctionType *F = LHS->getAs<FunctionType>();
5220 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
5221 FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
5222 QualType ResultType
5223 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
5224 FPT->getNumArgs(), FPT->isVariadic(),
5225 FPT->getTypeQuals(),
5226 FPT->hasExceptionSpec(),
5227 FPT->hasAnyExceptionSpec(),
5228 FPT->getNumExceptions(),
5229 FPT->exception_begin(),
5230 Info);
5231 return ResultType;
5232 }
5233 }
5234 return QualType();
5235 }
5236
5237 // If the qualifiers are different, the types can still be merged.
5238 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5239 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5240 if (LQuals != RQuals) {
5241 // If any of these qualifiers are different, we have a type mismatch.
5242 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5243 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5244 return QualType();
5245
5246 // Exactly one GC qualifier difference is allowed: __strong is
5247 // okay if the other type has no GC qualifier but is an Objective
5248 // C object pointer (i.e. implicitly strong by default). We fix
5249 // this by pretending that the unqualified type was actually
5250 // qualified __strong.
5251 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5252 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5253 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5254
5255 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5256 return QualType();
5257
5258 if (GC_L == Qualifiers::Strong)
5259 return LHS;
5260 if (GC_R == Qualifiers::Strong)
5261 return RHS;
5262 return QualType();
5263 }
5264
5265 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5266 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5267 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5268 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5269 if (ResQT == LHSBaseQT)
5270 return LHS;
5271 if (ResQT == RHSBaseQT)
5272 return RHS;
5273 }
5274 return QualType();
5275}
5276
Chris Lattner5426bf62008-04-07 07:01:58 +00005277//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00005278// Integer Predicates
5279//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00005280
Eli Friedmanad74a752008-06-28 06:23:08 +00005281unsigned ASTContext::getIntWidth(QualType T) {
John McCall842aef82009-12-09 09:09:27 +00005282 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00005283 T = ET->getDecl()->getIntegerType();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005284 if (T->isBooleanType())
5285 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00005286 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00005287 return (unsigned)getTypeSize(T);
5288}
5289
5290QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregorf6094622010-07-23 15:58:24 +00005291 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00005292
5293 // Turn <4 x signed int> -> <4 x unsigned int>
5294 if (const VectorType *VTy = T->getAs<VectorType>())
5295 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsone86d78c2010-11-10 21:56:12 +00005296 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattner6a2b9262009-10-17 20:33:28 +00005297
5298 // For enums, we return the unsigned version of the base type.
5299 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00005300 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00005301
5302 const BuiltinType *BTy = T->getAs<BuiltinType>();
5303 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00005304 switch (BTy->getKind()) {
5305 case BuiltinType::Char_S:
5306 case BuiltinType::SChar:
5307 return UnsignedCharTy;
5308 case BuiltinType::Short:
5309 return UnsignedShortTy;
5310 case BuiltinType::Int:
5311 return UnsignedIntTy;
5312 case BuiltinType::Long:
5313 return UnsignedLongTy;
5314 case BuiltinType::LongLong:
5315 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00005316 case BuiltinType::Int128:
5317 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00005318 default:
5319 assert(0 && "Unexpected signed integer type");
5320 return QualType();
5321 }
5322}
5323
Douglas Gregor2cf26342009-04-09 22:27:44 +00005324ExternalASTSource::~ExternalASTSource() { }
5325
5326void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00005327
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005328ASTMutationListener::~ASTMutationListener() { }
5329
Chris Lattner86df27b2009-06-14 00:45:47 +00005330
5331//===----------------------------------------------------------------------===//
5332// Builtin Type Computation
5333//===----------------------------------------------------------------------===//
5334
5335/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattner33daae62010-10-01 22:42:38 +00005336/// pointer over the consumed characters. This returns the resultant type. If
5337/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5338/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5339/// a vector of "i*".
Chris Lattner14e0e742010-10-01 22:53:11 +00005340///
5341/// RequiresICE is filled in on return to indicate whether the value is required
5342/// to be an Integer Constant Expression.
Mike Stump1eb44332009-09-09 15:08:12 +00005343static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00005344 ASTContext::GetBuiltinTypeError &Error,
Chris Lattner14e0e742010-10-01 22:53:11 +00005345 bool &RequiresICE,
Chris Lattner33daae62010-10-01 22:42:38 +00005346 bool AllowTypeModifiers) {
Chris Lattner86df27b2009-06-14 00:45:47 +00005347 // Modifiers.
5348 int HowLong = 0;
5349 bool Signed = false, Unsigned = false;
Chris Lattner14e0e742010-10-01 22:53:11 +00005350 RequiresICE = false;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005351
Chris Lattner33daae62010-10-01 22:42:38 +00005352 // Read the prefixed modifiers first.
Chris Lattner86df27b2009-06-14 00:45:47 +00005353 bool Done = false;
5354 while (!Done) {
5355 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00005356 default: Done = true; --Str; break;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005357 case 'I':
Chris Lattner14e0e742010-10-01 22:53:11 +00005358 RequiresICE = true;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005359 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005360 case 'S':
5361 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5362 assert(!Signed && "Can't use 'S' modifier multiple times!");
5363 Signed = true;
5364 break;
5365 case 'U':
5366 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5367 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5368 Unsigned = true;
5369 break;
5370 case 'L':
5371 assert(HowLong <= 2 && "Can't have LLLL modifier");
5372 ++HowLong;
5373 break;
5374 }
5375 }
5376
5377 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00005378
Chris Lattner86df27b2009-06-14 00:45:47 +00005379 // Read the base type.
5380 switch (*Str++) {
5381 default: assert(0 && "Unknown builtin type letter!");
5382 case 'v':
5383 assert(HowLong == 0 && !Signed && !Unsigned &&
5384 "Bad modifiers used with 'v'!");
5385 Type = Context.VoidTy;
5386 break;
5387 case 'f':
5388 assert(HowLong == 0 && !Signed && !Unsigned &&
5389 "Bad modifiers used with 'f'!");
5390 Type = Context.FloatTy;
5391 break;
5392 case 'd':
5393 assert(HowLong < 2 && !Signed && !Unsigned &&
5394 "Bad modifiers used with 'd'!");
5395 if (HowLong)
5396 Type = Context.LongDoubleTy;
5397 else
5398 Type = Context.DoubleTy;
5399 break;
5400 case 's':
5401 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5402 if (Unsigned)
5403 Type = Context.UnsignedShortTy;
5404 else
5405 Type = Context.ShortTy;
5406 break;
5407 case 'i':
5408 if (HowLong == 3)
5409 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5410 else if (HowLong == 2)
5411 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5412 else if (HowLong == 1)
5413 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5414 else
5415 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5416 break;
5417 case 'c':
5418 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5419 if (Signed)
5420 Type = Context.SignedCharTy;
5421 else if (Unsigned)
5422 Type = Context.UnsignedCharTy;
5423 else
5424 Type = Context.CharTy;
5425 break;
5426 case 'b': // boolean
5427 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5428 Type = Context.BoolTy;
5429 break;
5430 case 'z': // size_t.
5431 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5432 Type = Context.getSizeType();
5433 break;
5434 case 'F':
5435 Type = Context.getCFConstantStringType();
5436 break;
Fariborz Jahanianba8bda02010-11-09 21:38:20 +00005437 case 'G':
5438 Type = Context.getObjCIdType();
5439 break;
5440 case 'H':
5441 Type = Context.getObjCSelType();
5442 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005443 case 'a':
5444 Type = Context.getBuiltinVaListType();
5445 assert(!Type.isNull() && "builtin va list type not initialized!");
5446 break;
5447 case 'A':
5448 // This is a "reference" to a va_list; however, what exactly
5449 // this means depends on how va_list is defined. There are two
5450 // different kinds of va_list: ones passed by value, and ones
5451 // passed by reference. An example of a by-value va_list is
5452 // x86, where va_list is a char*. An example of by-ref va_list
5453 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5454 // we want this argument to be a char*&; for x86-64, we want
5455 // it to be a __va_list_tag*.
5456 Type = Context.getBuiltinVaListType();
5457 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattner14e0e742010-10-01 22:53:11 +00005458 if (Type->isArrayType())
Chris Lattner86df27b2009-06-14 00:45:47 +00005459 Type = Context.getArrayDecayedType(Type);
Chris Lattner14e0e742010-10-01 22:53:11 +00005460 else
Chris Lattner86df27b2009-06-14 00:45:47 +00005461 Type = Context.getLValueReferenceType(Type);
Chris Lattner86df27b2009-06-14 00:45:47 +00005462 break;
5463 case 'V': {
5464 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00005465 unsigned NumElements = strtoul(Str, &End, 10);
5466 assert(End != Str && "Missing vector size");
Chris Lattner86df27b2009-06-14 00:45:47 +00005467 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00005468
Chris Lattner14e0e742010-10-01 22:53:11 +00005469 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5470 RequiresICE, false);
5471 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattner33daae62010-10-01 22:42:38 +00005472
5473 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner788b0fd2010-06-23 06:00:24 +00005474 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00005475 VectorType::GenericVector);
Chris Lattner86df27b2009-06-14 00:45:47 +00005476 break;
5477 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00005478 case 'X': {
Chris Lattner14e0e742010-10-01 22:53:11 +00005479 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5480 false);
5481 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregord3a23b22009-09-28 21:45:01 +00005482 Type = Context.getComplexType(ElementType);
5483 break;
5484 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005485 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00005486 Type = Context.getFILEType();
5487 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005488 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00005489 return QualType();
5490 }
Mike Stumpfd612db2009-07-28 23:47:15 +00005491 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005492 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00005493 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00005494 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00005495 else
5496 Type = Context.getjmp_bufType();
5497
Mike Stumpfd612db2009-07-28 23:47:15 +00005498 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005499 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00005500 return QualType();
5501 }
5502 break;
Mike Stump782fa302009-07-28 02:25:19 +00005503 }
Mike Stump1eb44332009-09-09 15:08:12 +00005504
Chris Lattner33daae62010-10-01 22:42:38 +00005505 // If there are modifiers and if we're allowed to parse them, go for it.
5506 Done = !AllowTypeModifiers;
Chris Lattner86df27b2009-06-14 00:45:47 +00005507 while (!Done) {
John McCall187ab372010-03-12 04:21:28 +00005508 switch (char c = *Str++) {
Chris Lattner33daae62010-10-01 22:42:38 +00005509 default: Done = true; --Str; break;
5510 case '*':
5511 case '&': {
5512 // Both pointers and references can have their pointee types
5513 // qualified with an address space.
5514 char *End;
5515 unsigned AddrSpace = strtoul(Str, &End, 10);
5516 if (End != Str && AddrSpace != 0) {
5517 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5518 Str = End;
5519 }
5520 if (c == '*')
5521 Type = Context.getPointerType(Type);
5522 else
5523 Type = Context.getLValueReferenceType(Type);
5524 break;
5525 }
5526 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5527 case 'C':
5528 Type = Type.withConst();
5529 break;
5530 case 'D':
5531 Type = Context.getVolatileType(Type);
5532 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005533 }
5534 }
Chris Lattner393bd8e2010-10-01 07:13:18 +00005535
Chris Lattner14e0e742010-10-01 22:53:11 +00005536 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner393bd8e2010-10-01 07:13:18 +00005537 "Integer constant 'I' type must be an integer");
Mike Stump1eb44332009-09-09 15:08:12 +00005538
Chris Lattner86df27b2009-06-14 00:45:47 +00005539 return Type;
5540}
5541
5542/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattner33daae62010-10-01 22:42:38 +00005543QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattner14e0e742010-10-01 22:53:11 +00005544 GetBuiltinTypeError &Error,
5545 unsigned *IntegerConstantArgs) {
Chris Lattner33daae62010-10-01 22:42:38 +00005546 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump1eb44332009-09-09 15:08:12 +00005547
Chris Lattner86df27b2009-06-14 00:45:47 +00005548 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00005549
Chris Lattner14e0e742010-10-01 22:53:11 +00005550 bool RequiresICE = false;
Chris Lattner86df27b2009-06-14 00:45:47 +00005551 Error = GE_None;
Chris Lattner14e0e742010-10-01 22:53:11 +00005552 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5553 RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005554 if (Error != GE_None)
5555 return QualType();
Chris Lattner14e0e742010-10-01 22:53:11 +00005556
5557 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5558
Chris Lattner86df27b2009-06-14 00:45:47 +00005559 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattner14e0e742010-10-01 22:53:11 +00005560 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005561 if (Error != GE_None)
5562 return QualType();
5563
Chris Lattner14e0e742010-10-01 22:53:11 +00005564 // If this argument is required to be an IntegerConstantExpression and the
5565 // caller cares, fill in the bitmask we return.
5566 if (RequiresICE && IntegerConstantArgs)
5567 *IntegerConstantArgs |= 1 << ArgTypes.size();
5568
Chris Lattner86df27b2009-06-14 00:45:47 +00005569 // Do array -> pointer decay. The builtin should use the decayed type.
5570 if (Ty->isArrayType())
5571 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00005572
Chris Lattner86df27b2009-06-14 00:45:47 +00005573 ArgTypes.push_back(Ty);
5574 }
5575
5576 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5577 "'.' should only occur at end of builtin type list!");
5578
5579 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
5580 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
5581 return getFunctionNoProtoType(ResType);
Douglas Gregorce056bc2010-02-21 22:15:06 +00005582
5583 // FIXME: Should we create noreturn types?
Chris Lattner86df27b2009-06-14 00:45:47 +00005584 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00005585 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindola264ba482010-03-30 20:24:48 +00005586 FunctionType::ExtInfo());
Chris Lattner86df27b2009-06-14 00:45:47 +00005587}
Eli Friedmana95d7572009-08-19 07:44:53 +00005588
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005589GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5590 GVALinkage External = GVA_StrongExternal;
5591
5592 Linkage L = FD->getLinkage();
5593 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5594 FD->getType()->getLinkage() == UniqueExternalLinkage)
5595 L = UniqueExternalLinkage;
5596
5597 switch (L) {
5598 case NoLinkage:
5599 case InternalLinkage:
5600 case UniqueExternalLinkage:
5601 return GVA_Internal;
5602
5603 case ExternalLinkage:
5604 switch (FD->getTemplateSpecializationKind()) {
5605 case TSK_Undeclared:
5606 case TSK_ExplicitSpecialization:
5607 External = GVA_StrongExternal;
5608 break;
5609
5610 case TSK_ExplicitInstantiationDefinition:
5611 return GVA_ExplicitTemplateInstantiation;
5612
5613 case TSK_ExplicitInstantiationDeclaration:
5614 case TSK_ImplicitInstantiation:
5615 External = GVA_TemplateInstantiation;
5616 break;
5617 }
5618 }
5619
5620 if (!FD->isInlined())
5621 return External;
5622
5623 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5624 // GNU or C99 inline semantics. Determine whether this symbol should be
5625 // externally visible.
5626 if (FD->isInlineDefinitionExternallyVisible())
5627 return External;
5628
5629 // C99 inline semantics, where the symbol is not externally visible.
5630 return GVA_C99Inline;
5631 }
5632
5633 // C++0x [temp.explicit]p9:
5634 // [ Note: The intent is that an inline function that is the subject of
5635 // an explicit instantiation declaration will still be implicitly
5636 // instantiated when used so that the body can be considered for
5637 // inlining, but that no out-of-line copy of the inline function would be
5638 // generated in the translation unit. -- end note ]
5639 if (FD->getTemplateSpecializationKind()
5640 == TSK_ExplicitInstantiationDeclaration)
5641 return GVA_C99Inline;
5642
5643 return GVA_CXXInline;
5644}
5645
5646GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
5647 // If this is a static data member, compute the kind of template
5648 // specialization. Otherwise, this variable is not part of a
5649 // template.
5650 TemplateSpecializationKind TSK = TSK_Undeclared;
5651 if (VD->isStaticDataMember())
5652 TSK = VD->getTemplateSpecializationKind();
5653
5654 Linkage L = VD->getLinkage();
5655 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5656 VD->getType()->getLinkage() == UniqueExternalLinkage)
5657 L = UniqueExternalLinkage;
5658
5659 switch (L) {
5660 case NoLinkage:
5661 case InternalLinkage:
5662 case UniqueExternalLinkage:
5663 return GVA_Internal;
5664
5665 case ExternalLinkage:
5666 switch (TSK) {
5667 case TSK_Undeclared:
5668 case TSK_ExplicitSpecialization:
5669 return GVA_StrongExternal;
5670
5671 case TSK_ExplicitInstantiationDeclaration:
5672 llvm_unreachable("Variable should not be instantiated");
5673 // Fall through to treat this like any other instantiation.
5674
5675 case TSK_ExplicitInstantiationDefinition:
5676 return GVA_ExplicitTemplateInstantiation;
5677
5678 case TSK_ImplicitInstantiation:
5679 return GVA_TemplateInstantiation;
5680 }
5681 }
5682
5683 return GVA_StrongExternal;
5684}
5685
Argyrios Kyrtzidis4ac7c0b2010-07-29 20:08:05 +00005686bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005687 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
5688 if (!VD->isFileVarDecl())
5689 return false;
5690 } else if (!isa<FunctionDecl>(D))
5691 return false;
5692
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00005693 // Weak references don't produce any output by themselves.
5694 if (D->hasAttr<WeakRefAttr>())
5695 return false;
5696
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005697 // Aliases and used decls are required.
5698 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
5699 return true;
5700
5701 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5702 // Forward declarations aren't required.
5703 if (!FD->isThisDeclarationADefinition())
5704 return false;
5705
5706 // Constructors and destructors are required.
5707 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
5708 return true;
5709
5710 // The key function for a class is required.
5711 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5712 const CXXRecordDecl *RD = MD->getParent();
5713 if (MD->isOutOfLine() && RD->isDynamicClass()) {
5714 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
5715 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
5716 return true;
5717 }
5718 }
5719
5720 GVALinkage Linkage = GetGVALinkageForFunction(FD);
5721
5722 // static, static inline, always_inline, and extern inline functions can
5723 // always be deferred. Normal inline functions can be deferred in C99/C++.
5724 // Implicit template instantiations can also be deferred in C++.
5725 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
5726 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
5727 return false;
5728 return true;
5729 }
5730
5731 const VarDecl *VD = cast<VarDecl>(D);
5732 assert(VD->isFileVarDecl() && "Expected file scoped var");
5733
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00005734 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
5735 return false;
5736
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005737 // Structs that have non-trivial constructors or destructors are required.
5738
5739 // FIXME: Handle references.
5740 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
5741 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005742 if (RD->hasDefinition() &&
5743 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005744 return true;
5745 }
5746 }
5747
5748 GVALinkage L = GetGVALinkageForVariable(VD);
5749 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
5750 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
5751 return false;
5752 }
5753
5754 return true;
5755}
Charles Davis071cc7d2010-08-16 03:33:14 +00005756
Charles Davisee743f92010-11-09 18:04:24 +00005757CallingConv ASTContext::getDefaultMethodCallConv() {
5758 // Pass through to the C++ ABI object
5759 return ABI->getDefaultMethodCallConv();
5760}
5761
Anders Carlssondae0cb52010-11-25 01:51:53 +00005762bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) {
5763 // Pass through to the C++ ABI object
5764 return ABI->isNearlyEmpty(RD);
5765}
5766
Charles Davis071cc7d2010-08-16 03:33:14 +00005767CXXABI::~CXXABI() {}