blob: e76238cb221e871c4fa94372adb6ef54caa831c1 [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());
Douglas Gregor61c4d282011-01-05 15:48:55 +000054 ID.AddBoolean(Parm->isParameterPack());
Douglas Gregor3e1274f2010-06-16 21:09:37 +000055
56 TemplateParameterList *Params = Parm->getTemplateParameters();
57 ID.AddInteger(Params->size());
58 for (TemplateParameterList::const_iterator P = Params->begin(),
59 PEnd = Params->end();
60 P != PEnd; ++P) {
61 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
62 ID.AddInteger(0);
63 ID.AddBoolean(TTP->isParameterPack());
64 continue;
65 }
66
67 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
68 ID.AddInteger(1);
Douglas Gregor61c4d282011-01-05 15:48:55 +000069 ID.AddBoolean(NTTP->isParameterPack());
Douglas Gregor3e1274f2010-06-16 21:09:37 +000070 ID.AddPointer(NTTP->getType().getAsOpaquePtr());
71 continue;
72 }
73
74 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
75 ID.AddInteger(2);
76 Profile(ID, TTP);
77 }
78}
79
80TemplateTemplateParmDecl *
81ASTContext::getCanonicalTemplateTemplateParmDecl(
82 TemplateTemplateParmDecl *TTP) {
83 // Check if we already have a canonical template template parameter.
84 llvm::FoldingSetNodeID ID;
85 CanonicalTemplateTemplateParm::Profile(ID, TTP);
86 void *InsertPos = 0;
87 CanonicalTemplateTemplateParm *Canonical
88 = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
89 if (Canonical)
90 return Canonical->getParam();
91
92 // Build a canonical template parameter list.
93 TemplateParameterList *Params = TTP->getTemplateParameters();
94 llvm::SmallVector<NamedDecl *, 4> CanonParams;
95 CanonParams.reserve(Params->size());
96 for (TemplateParameterList::const_iterator P = Params->begin(),
97 PEnd = Params->end();
98 P != PEnd; ++P) {
99 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
100 CanonParams.push_back(
101 TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
102 SourceLocation(), TTP->getDepth(),
103 TTP->getIndex(), 0, false,
104 TTP->isParameterPack()));
105 else if (NonTypeTemplateParmDecl *NTTP
106 = dyn_cast<NonTypeTemplateParmDecl>(*P))
107 CanonParams.push_back(
108 NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
109 SourceLocation(), NTTP->getDepth(),
110 NTTP->getPosition(), 0,
111 getCanonicalType(NTTP->getType()),
Douglas Gregor10738d32010-12-23 23:51:58 +0000112 NTTP->isParameterPack(),
Douglas Gregor3e1274f2010-06-16 21:09:37 +0000113 0));
114 else
115 CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
116 cast<TemplateTemplateParmDecl>(*P)));
117 }
118
119 TemplateTemplateParmDecl *CanonTTP
120 = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
121 SourceLocation(), TTP->getDepth(),
Douglas Gregor61c4d282011-01-05 15:48:55 +0000122 TTP->getPosition(),
123 TTP->isParameterPack(),
124 0,
Douglas Gregor3e1274f2010-06-16 21:09:37 +0000125 TemplateParameterList::Create(*this, SourceLocation(),
126 SourceLocation(),
127 CanonParams.data(),
128 CanonParams.size(),
129 SourceLocation()));
130
131 // Get the new insert position for the node we care about.
132 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
133 assert(Canonical == 0 && "Shouldn't be in the map!");
134 (void)Canonical;
135
136 // Create the canonical template template parameter entry.
137 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
138 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
139 return CanonTTP;
140}
141
Charles Davis071cc7d2010-08-16 03:33:14 +0000142CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
John McCallee79a4c2010-08-21 22:46:04 +0000143 if (!LangOpts.CPlusPlus) return 0;
144
Charles Davis20cf7172010-08-19 02:18:14 +0000145 switch (T.getCXXABI()) {
John McCallee79a4c2010-08-21 22:46:04 +0000146 case CXXABI_ARM:
147 return CreateARMCXXABI(*this);
148 case CXXABI_Itanium:
Charles Davis071cc7d2010-08-16 03:33:14 +0000149 return CreateItaniumCXXABI(*this);
Charles Davis20cf7172010-08-19 02:18:14 +0000150 case CXXABI_Microsoft:
151 return CreateMicrosoftCXXABI(*this);
152 }
John McCallee79a4c2010-08-21 22:46:04 +0000153 return 0;
Charles Davis071cc7d2010-08-16 03:33:14 +0000154}
155
Chris Lattner61710852008-10-05 17:34:18 +0000156ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar444be732009-11-13 05:51:54 +0000157 const TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +0000158 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +0000159 Builtin::Context &builtins,
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000160 unsigned size_reserve) :
John McCallef990012010-06-11 11:07:21 +0000161 TemplateSpecializationTypes(this_()),
162 DependentTemplateSpecializationTypes(this_()),
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +0000163 GlobalNestedNameSpecifier(0), IsInt128Installed(false),
164 CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +0000165 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump083c25e2009-10-22 00:49:09 +0000166 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
John McCallbf1a0282010-06-04 23:28:52 +0000167 NullTypeSourceInfo(QualType()),
Charles Davis071cc7d2010-08-16 03:33:14 +0000168 SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +0000169 Idents(idents), Selectors(sels),
Ted Kremenekac9590e2010-05-10 20:40:08 +0000170 BuiltinInfo(builtins),
171 DeclarationNames(*this),
Argyrios Kyrtzidis9a44b5f2010-10-28 09:29:35 +0000172 ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
Ted Kremenekf057bf72010-06-18 00:31:04 +0000173 LastSDM(0, 0),
174 UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) {
David Chisnall0f436562009-08-17 16:35:33 +0000175 ObjCIdRedefinitionType = QualType();
176 ObjCClassRedefinitionType = QualType();
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000177 ObjCSelRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000178 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +0000179 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +0000180 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +0000181}
182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183ASTContext::~ASTContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000184 // Release the DenseMaps associated with DeclContext objects.
185 // FIXME: Is this the ideal solution?
186 ReleaseDeclContextMaps();
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000187
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000188 // Call all of the deallocation functions.
189 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
190 Deallocations[I].first(Deallocations[I].second);
Douglas Gregor00545312010-05-23 18:26:36 +0000191
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000192 // Release all of the memory associated with overridden C++ methods.
193 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
194 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
195 OM != OMEnd; ++OM)
196 OM->second.Destroy();
Ted Kremenek3478eb62010-02-11 07:12:28 +0000197
Ted Kremenekdcfcfbe2010-06-08 23:00:58 +0000198 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000199 // because they can contain DenseMaps.
200 for (llvm::DenseMap<const ObjCContainerDecl*,
201 const ASTRecordLayout*>::iterator
202 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
203 // Increment in loop to prevent using deallocated memory.
204 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
205 R->Destroy(*this);
206
Ted Kremenekdcfcfbe2010-06-08 23:00:58 +0000207 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
208 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
209 // Increment in loop to prevent using deallocated memory.
210 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
211 R->Destroy(*this);
212 }
Douglas Gregor63200642010-08-30 16:49:28 +0000213
214 for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
215 AEnd = DeclAttrs.end();
216 A != AEnd; ++A)
217 A->second->~AttrVec();
218}
Douglas Gregorab452ba2009-03-26 23:50:42 +0000219
Douglas Gregor00545312010-05-23 18:26:36 +0000220void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
221 Deallocations.push_back(std::make_pair(Callback, Data));
222}
223
Mike Stump1eb44332009-09-09 15:08:12 +0000224void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
226 ExternalSource.reset(Source.take());
227}
228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229void ASTContext::PrintStats() const {
230 fprintf(stderr, "*** AST Context Stats:\n");
231 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000232
Douglas Gregordbe833d2009-05-26 14:40:08 +0000233 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000234#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000235#define ABSTRACT_TYPE(Name, Parent)
236#include "clang/AST/TypeNodes.def"
237 0 // Extra
238 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
241 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000242 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 }
244
Douglas Gregordbe833d2009-05-26 14:40:08 +0000245 unsigned Idx = 0;
246 unsigned TotalBytes = 0;
247#define TYPE(Name, Parent) \
248 if (counts[Idx]) \
249 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
250 TotalBytes += counts[Idx] * sizeof(Name##Type); \
251 ++Idx;
252#define ABSTRACT_TYPE(Name, Parent)
253#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Douglas Gregordbe833d2009-05-26 14:40:08 +0000255 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregored8abf12010-07-08 06:14:04 +0000256
Douglas Gregor4923aa22010-07-02 20:37:36 +0000257 // Implicit special member functions.
Douglas Gregor18274032010-07-03 00:47:00 +0000258 fprintf(stderr, " %u/%u implicit default constructors created\n",
259 NumImplicitDefaultConstructorsDeclared,
260 NumImplicitDefaultConstructors);
Douglas Gregor22584312010-07-02 23:41:54 +0000261 fprintf(stderr, " %u/%u implicit copy constructors created\n",
262 NumImplicitCopyConstructorsDeclared,
263 NumImplicitCopyConstructors);
Douglas Gregora376d102010-07-02 21:50:04 +0000264 fprintf(stderr, " %u/%u implicit copy assignment operators created\n",
265 NumImplicitCopyAssignmentOperatorsDeclared,
266 NumImplicitCopyAssignmentOperators);
Douglas Gregor4923aa22010-07-02 20:37:36 +0000267 fprintf(stderr, " %u/%u implicit destructors created\n",
268 NumImplicitDestructorsDeclared, NumImplicitDestructors);
269
Douglas Gregor2cf26342009-04-09 22:27:44 +0000270 if (ExternalSource.get()) {
271 fprintf(stderr, "\n");
272 ExternalSource->PrintStats();
273 }
Douglas Gregored8abf12010-07-08 06:14:04 +0000274
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000275 BumpAlloc.PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +0000276}
277
278
John McCalle27ec8a2009-10-23 23:03:21 +0000279void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000280 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000281 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000282 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000283}
284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285void ASTContext::InitBuiltinTypes() {
286 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // C99 6.2.5p19.
289 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // C99 6.2.5p2.
292 InitBuiltinType(BoolTy, BuiltinType::Bool);
293 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000294 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 InitBuiltinType(CharTy, BuiltinType::Char_S);
296 else
297 InitBuiltinType(CharTy, BuiltinType::Char_U);
298 // C99 6.2.5p4.
299 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
300 InitBuiltinType(ShortTy, BuiltinType::Short);
301 InitBuiltinType(IntTy, BuiltinType::Int);
302 InitBuiltinType(LongTy, BuiltinType::Long);
303 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // C99 6.2.5p6.
306 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
307 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
308 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
309 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
310 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // C99 6.2.5p10.
313 InitBuiltinType(FloatTy, BuiltinType::Float);
314 InitBuiltinType(DoubleTy, BuiltinType::Double);
315 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000316
Chris Lattner2df9ced2009-04-30 02:43:43 +0000317 // GNU extension, 128-bit integers.
318 InitBuiltinType(Int128Ty, BuiltinType::Int128);
319 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
320
Chris Lattner3f59c972010-12-25 23:25:43 +0000321 if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
322 if (!LangOpts.ShortWChar)
323 InitBuiltinType(WCharTy, BuiltinType::WChar_S);
324 else // -fshort-wchar makes wchar_t be unsigned.
325 InitBuiltinType(WCharTy, BuiltinType::WChar_U);
326 } else // C99
Chris Lattner3a250322009-02-26 23:43:47 +0000327 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000328
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000329 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
330 InitBuiltinType(Char16Ty, BuiltinType::Char16);
331 else // C99
332 Char16Ty = getFromTargetType(Target.getChar16Type());
333
334 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
335 InitBuiltinType(Char32Ty, BuiltinType::Char32);
336 else // C99
337 Char32Ty = getFromTargetType(Target.getChar32Type());
338
Douglas Gregor898574e2008-12-05 23:32:09 +0000339 // Placeholder type for type-dependent expressions whose type is
340 // completely unknown. No code should ever check a type against
341 // DependentTy and users should never see it; however, it is here to
342 // help diagnose failures to properly check for type-dependent
343 // expressions.
344 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000345
John McCall2a984ca2010-10-12 00:20:44 +0000346 // Placeholder type for functions.
347 InitBuiltinType(OverloadTy, BuiltinType::Overload);
348
Mike Stump1eb44332009-09-09 15:08:12 +0000349 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000350 // not yet been deduced.
John McCall2a984ca2010-10-12 00:20:44 +0000351 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // C99 6.2.5p11.
354 FloatComplexTy = getComplexType(FloatTy);
355 DoubleComplexTy = getComplexType(DoubleTy);
356 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000357
Steve Naroff7e219e42007-10-15 14:41:52 +0000358 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Steve Naroffde2e22d2009-07-15 18:40:39 +0000360 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
361 ObjCIdTypedefType = QualType();
362 ObjCClassTypedefType = QualType();
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000363 ObjCSelTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000365 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroffde2e22d2009-07-15 18:40:39 +0000366 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
367 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000368 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff14108da2009-07-10 23:34:53 +0000369
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000370 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000372 // void * type
373 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000374
375 // nullptr type (C++0x 2.14.7)
376 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000377}
378
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000379Diagnostic &ASTContext::getDiagnostics() const {
380 return SourceMgr.getDiagnostics();
381}
382
Douglas Gregor63200642010-08-30 16:49:28 +0000383AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
384 AttrVec *&Result = DeclAttrs[D];
385 if (!Result) {
386 void *Mem = Allocate(sizeof(AttrVec));
387 Result = new (Mem) AttrVec;
388 }
389
390 return *Result;
391}
392
393/// \brief Erase the attributes corresponding to the given declaration.
394void ASTContext::eraseDeclAttrs(const Decl *D) {
395 llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
396 if (Pos != DeclAttrs.end()) {
397 Pos->second->~AttrVec();
398 DeclAttrs.erase(Pos);
399 }
400}
401
402
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000403MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000404ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000405 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000406 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000407 = InstantiatedFromStaticDataMember.find(Var);
408 if (Pos == InstantiatedFromStaticDataMember.end())
409 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Douglas Gregor7caa6822009-07-24 20:34:43 +0000411 return Pos->second;
412}
413
Mike Stump1eb44332009-09-09 15:08:12 +0000414void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000415ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000416 TemplateSpecializationKind TSK,
417 SourceLocation PointOfInstantiation) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000418 assert(Inst->isStaticDataMember() && "Not a static data member");
419 assert(Tmpl->isStaticDataMember() && "Not a static data member");
420 assert(!InstantiatedFromStaticDataMember[Inst] &&
421 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000422 InstantiatedFromStaticDataMember[Inst]
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000423 = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000424}
425
John McCall7ba107a2009-11-18 02:36:19 +0000426NamedDecl *
John McCalled976492009-12-04 22:46:56 +0000427ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCall7ba107a2009-11-18 02:36:19 +0000428 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCalled976492009-12-04 22:46:56 +0000429 = InstantiatedFromUsingDecl.find(UUD);
430 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson0d8df782009-08-29 19:37:28 +0000431 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Anders Carlsson0d8df782009-08-29 19:37:28 +0000433 return Pos->second;
434}
435
436void
John McCalled976492009-12-04 22:46:56 +0000437ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
438 assert((isa<UsingDecl>(Pattern) ||
439 isa<UnresolvedUsingValueDecl>(Pattern) ||
440 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
441 "pattern decl is not a using decl");
442 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
443 InstantiatedFromUsingDecl[Inst] = Pattern;
444}
445
446UsingShadowDecl *
447ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
448 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
449 = InstantiatedFromUsingShadowDecl.find(Inst);
450 if (Pos == InstantiatedFromUsingShadowDecl.end())
451 return 0;
452
453 return Pos->second;
454}
455
456void
457ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
458 UsingShadowDecl *Pattern) {
459 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
460 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +0000461}
462
Anders Carlssond8b285f2009-09-01 04:26:58 +0000463FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
464 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
465 = InstantiatedFromUnnamedFieldDecl.find(Field);
466 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
467 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Anders Carlssond8b285f2009-09-01 04:26:58 +0000469 return Pos->second;
470}
471
472void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
473 FieldDecl *Tmpl) {
474 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
475 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
476 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
477 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Anders Carlssond8b285f2009-09-01 04:26:58 +0000479 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
480}
481
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000482ASTContext::overridden_cxx_method_iterator
483ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
484 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
485 = OverriddenMethods.find(Method);
486 if (Pos == OverriddenMethods.end())
487 return 0;
488
489 return Pos->second.begin();
490}
491
492ASTContext::overridden_cxx_method_iterator
493ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
494 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
495 = OverriddenMethods.find(Method);
496 if (Pos == OverriddenMethods.end())
497 return 0;
498
499 return Pos->second.end();
500}
501
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000502unsigned
503ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
504 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
505 = OverriddenMethods.find(Method);
506 if (Pos == OverriddenMethods.end())
507 return 0;
508
509 return Pos->second.size();
510}
511
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000512void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
513 const CXXMethodDecl *Overridden) {
514 OverriddenMethods[Method].push_back(Overridden);
515}
516
Chris Lattner464175b2007-07-18 17:52:12 +0000517//===----------------------------------------------------------------------===//
518// Type Sizing and Analysis
519//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000520
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000521/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
522/// scalar floating point type.
523const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000524 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000525 assert(BT && "Not a floating point type!");
526 switch (BT->getKind()) {
527 default: assert(0 && "Not a floating point type!");
528 case BuiltinType::Float: return Target.getFloatFormat();
529 case BuiltinType::Double: return Target.getDoubleFormat();
530 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
531 }
532}
533
Ken Dyck8b752f12010-01-27 17:10:57 +0000534/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000535/// specified decl. Note that bitfields do not have a valid alignment, so
536/// this method will assert on them.
Sebastian Redl5d484e82009-11-23 17:18:46 +0000537/// If @p RefAsPointee, references are treated like their underlying type
538/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck8b752f12010-01-27 17:10:57 +0000539CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000540 unsigned Align = Target.getCharWidth();
541
John McCall4081a5c2010-10-08 18:24:19 +0000542 bool UseAlignAttrOnly = false;
543 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
544 Align = AlignFromAttr;
Eli Friedmandcdafb62009-02-22 02:56:25 +0000545
John McCall4081a5c2010-10-08 18:24:19 +0000546 // __attribute__((aligned)) can increase or decrease alignment
547 // *except* on a struct or struct member, where it only increases
548 // alignment unless 'packed' is also specified.
549 //
550 // It is an error for [[align]] to decrease alignment, so we can
551 // ignore that possibility; Sema should diagnose it.
552 if (isa<FieldDecl>(D)) {
553 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
554 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
555 } else {
556 UseAlignAttrOnly = true;
557 }
558 }
559
560 if (UseAlignAttrOnly) {
561 // ignore type of value
562 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneraf707ab2009-01-24 21:53:27 +0000563 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000564 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl5d484e82009-11-23 17:18:46 +0000565 if (RefAsPointee)
566 T = RT->getPointeeType();
567 else
568 T = getPointerType(RT->getPointeeType());
569 }
570 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindola6deecb02010-06-04 23:15:27 +0000571 unsigned MinWidth = Target.getLargeArrayMinWidth();
572 unsigned ArrayAlign = Target.getLargeArrayAlign();
573 if (isa<VariableArrayType>(T) && MinWidth != 0)
574 Align = std::max(Align, ArrayAlign);
575 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
576 unsigned Size = getTypeSize(CT);
577 if (MinWidth != 0 && MinWidth <= Size)
578 Align = std::max(Align, ArrayAlign);
579 }
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000580 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000581 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
582 T = cast<ArrayType>(T)->getElementType();
583
584 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
585 }
Charles Davis05f62472010-02-23 04:52:00 +0000586 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
587 // In the case of a field in a packed struct, we want the minimum
588 // of the alignment of the field and the alignment of the struct.
589 Align = std::min(Align,
590 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
591 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000592 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000593
Ken Dyck8b752f12010-01-27 17:10:57 +0000594 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000595}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000596
John McCallea1471e2010-05-20 01:18:31 +0000597std::pair<CharUnits, CharUnits>
598ASTContext::getTypeInfoInChars(const Type *T) {
599 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
600 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
601 CharUnits::fromQuantity(Info.second / getCharWidth()));
602}
603
604std::pair<CharUnits, CharUnits>
605ASTContext::getTypeInfoInChars(QualType T) {
606 return getTypeInfoInChars(T.getTypePtr());
607}
608
Chris Lattnera7674d82007-07-13 22:13:22 +0000609/// getTypeSize - Return the size of the specified type, in bits. This method
610/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000611///
612/// FIXME: Pointers into different addr spaces could have different sizes and
613/// alignment requirements: getPointerInfo should take an AddrSpace, this
614/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000615std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000616ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000617 uint64_t Width=0;
618 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000619 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000620#define TYPE(Class, Base)
621#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000622#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000623#define DEPENDENT_TYPE(Class, Base) case Type::Class:
624#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000625 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000626 break;
627
Chris Lattner692233e2007-07-13 22:27:08 +0000628 case Type::FunctionNoProto:
629 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000630 // GCC extension: alignof(function) = 32 bits
631 Width = 0;
632 Align = 32;
633 break;
634
Douglas Gregor72564e72009-02-26 23:50:07 +0000635 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000636 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000637 Width = 0;
638 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
639 break;
640
Steve Narofffb22d962007-08-30 01:06:46 +0000641 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000642 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Chris Lattner98be4942008-03-05 18:54:05 +0000644 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000645 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000646 Align = EltInfo.second;
647 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000648 }
Nate Begeman213541a2008-04-18 23:10:10 +0000649 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000650 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000651 const VectorType *VT = cast<VectorType>(T);
652 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
653 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000654 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000655 // If the alignment is not a power of 2, round up to the next power of 2.
656 // This happens for non-power-of-2 length vectors.
Dan Gohman8eefcd32010-04-21 23:32:43 +0000657 if (Align & (Align-1)) {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000658 Align = llvm::NextPowerOf2(Align);
659 Width = llvm::RoundUpToAlignment(Width, Align);
660 }
Chris Lattner030d8842007-07-19 22:06:24 +0000661 break;
662 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000663
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000664 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000665 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000666 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000667 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000668 // GCC extension: alignof(void) = 8 bits.
669 Width = 0;
670 Align = 8;
671 break;
672
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000673 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000674 Width = Target.getBoolWidth();
675 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000676 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000677 case BuiltinType::Char_S:
678 case BuiltinType::Char_U:
679 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000680 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000681 Width = Target.getCharWidth();
682 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000683 break;
Chris Lattner3f59c972010-12-25 23:25:43 +0000684 case BuiltinType::WChar_S:
685 case BuiltinType::WChar_U:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000686 Width = Target.getWCharWidth();
687 Align = Target.getWCharAlign();
688 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000689 case BuiltinType::Char16:
690 Width = Target.getChar16Width();
691 Align = Target.getChar16Align();
692 break;
693 case BuiltinType::Char32:
694 Width = Target.getChar32Width();
695 Align = Target.getChar32Align();
696 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000697 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000698 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000699 Width = Target.getShortWidth();
700 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000701 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000702 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000703 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000704 Width = Target.getIntWidth();
705 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000706 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000707 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000708 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000709 Width = Target.getLongWidth();
710 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000711 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000712 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000713 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000714 Width = Target.getLongLongWidth();
715 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000716 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000717 case BuiltinType::Int128:
718 case BuiltinType::UInt128:
719 Width = 128;
720 Align = 128; // int128_t is 128-bit aligned on all targets.
721 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000722 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000723 Width = Target.getFloatWidth();
724 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000725 break;
726 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000727 Width = Target.getDoubleWidth();
728 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000729 break;
730 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000731 Width = Target.getLongDoubleWidth();
732 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000733 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000734 case BuiltinType::NullPtr:
735 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
736 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000737 break;
Fariborz Jahaniane04f5fc2010-08-02 18:03:20 +0000738 case BuiltinType::ObjCId:
739 case BuiltinType::ObjCClass:
740 case BuiltinType::ObjCSel:
741 Width = Target.getPointerWidth(0);
742 Align = Target.getPointerAlign(0);
743 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000744 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000745 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000746 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000747 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000748 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000749 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000750 case Type::BlockPointer: {
751 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
752 Width = Target.getPointerWidth(AS);
753 Align = Target.getPointerAlign(AS);
754 break;
755 }
Sebastian Redl5d484e82009-11-23 17:18:46 +0000756 case Type::LValueReference:
757 case Type::RValueReference: {
758 // alignof and sizeof should never enter this code path here, so we go
759 // the pointer route.
760 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
761 Width = Target.getPointerWidth(AS);
762 Align = Target.getPointerAlign(AS);
763 break;
764 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000765 case Type::Pointer: {
766 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000767 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000768 Align = Target.getPointerAlign(AS);
769 break;
770 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000771 case Type::MemberPointer: {
Charles Davis071cc7d2010-08-16 03:33:14 +0000772 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000773 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000774 getTypeInfo(getPointerDiffType());
Charles Davis071cc7d2010-08-16 03:33:14 +0000775 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000776 Align = PtrDiffInfo.second;
777 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000778 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000779 case Type::Complex: {
780 // Complex types have the same alignment as their elements, but twice the
781 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000782 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000783 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000784 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000785 Align = EltInfo.second;
786 break;
787 }
John McCallc12c5bb2010-05-15 11:32:37 +0000788 case Type::ObjCObject:
789 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Patel44a3dde2008-06-04 21:54:36 +0000790 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000791 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000792 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
793 Width = Layout.getSize();
794 Align = Layout.getAlignment();
795 break;
796 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000797 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000798 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000799 const TagType *TT = cast<TagType>(T);
800
801 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000802 Width = 1;
803 Align = 1;
804 break;
805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Daniel Dunbar1d751182008-11-08 05:48:37 +0000807 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000808 return getTypeInfo(ET->getDecl()->getIntegerType());
809
Daniel Dunbar1d751182008-11-08 05:48:37 +0000810 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000811 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
812 Width = Layout.getSize();
813 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000814 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000815 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000816
Chris Lattner9fcfe922009-10-22 05:17:15 +0000817 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000818 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
819 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000820
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000821 case Type::Paren:
822 return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
823
Douglas Gregor18857642009-04-30 17:32:17 +0000824 case Type::Typedef: {
825 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregordf1367a2010-08-27 00:11:28 +0000826 std::pair<uint64_t, unsigned> Info
827 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
828 Align = std::max(Typedef->getMaxAlignment(), Info.second);
829 Width = Info.first;
Douglas Gregor7532dc62009-03-30 22:58:21 +0000830 break;
Chris Lattner71763312008-04-06 22:05:18 +0000831 }
Douglas Gregor18857642009-04-30 17:32:17 +0000832
833 case Type::TypeOfExpr:
834 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
835 .getTypePtr());
836
837 case Type::TypeOf:
838 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
839
Anders Carlsson395b4752009-06-24 19:06:50 +0000840 case Type::Decltype:
841 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
842 .getTypePtr());
843
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000844 case Type::Elaborated:
845 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000846
John McCall9d156a72011-01-06 01:58:22 +0000847 case Type::Attributed:
848 return getTypeInfo(
849 cast<AttributedType>(T)->getEquivalentType().getTypePtr());
850
Douglas Gregor18857642009-04-30 17:32:17 +0000851 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000852 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000853 "Cannot request the size of a dependent type");
854 // FIXME: this is likely to be wrong once we support template
855 // aliases, since a template alias could refer to a typedef that
856 // has an __aligned__ attribute on it.
857 return getTypeInfo(getCanonicalType(T));
858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattner464175b2007-07-18 17:52:12 +0000860 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000861 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000862}
863
Ken Dyckbdc601b2009-12-22 14:23:30 +0000864/// getTypeSizeInChars - Return the size of the specified type, in characters.
865/// This method does not work on incomplete types.
866CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000867 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000868}
869CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000870 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000871}
872
Ken Dyck16e20cc2010-01-26 17:25:18 +0000873/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck86fa4312010-01-26 17:22:55 +0000874/// characters. This method does not work on incomplete types.
875CharUnits ASTContext::getTypeAlignInChars(QualType T) {
876 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
877}
878CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
879 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
880}
881
Chris Lattner34ebde42009-01-27 18:08:34 +0000882/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
883/// type for the current target in bits. This can be different than the ABI
884/// alignment in cases where it is beneficial for performance to overalign
885/// a data type.
886unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
887 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000888
889 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000890 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000891 T = CT->getElementType().getTypePtr();
892 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
893 T->isSpecificBuiltinType(BuiltinType::LongLong))
894 return std::max(ABIAlign, (unsigned)getTypeSize(T));
895
Chris Lattner34ebde42009-01-27 18:08:34 +0000896 return ABIAlign;
897}
898
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000899/// ShallowCollectObjCIvars -
900/// Collect all ivars, including those synthesized, in the current class.
901///
902void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000903 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000904 // FIXME. This need be removed but there are two many places which
905 // assume const-ness of ObjCInterfaceDecl
906 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
907 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
908 Iv= Iv->getNextIvar())
909 Ivars.push_back(Iv);
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000910}
911
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000912/// DeepCollectObjCIvars -
913/// This routine first collects all declared, but not synthesized, ivars in
914/// super class and then collects all ivars, including those synthesized for
915/// current class. This routine is used for implementation of current class
916/// when all ivars, declared and synthesized are known.
Fariborz Jahanian98200742009-05-12 18:14:29 +0000917///
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000918void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
919 bool leafClass,
Fariborz Jahanian98200742009-05-12 18:14:29 +0000920 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000921 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
922 DeepCollectObjCIvars(SuperClass, false, Ivars);
923 if (!leafClass) {
924 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
925 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000926 Ivars.push_back(*I);
927 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000928 else
929 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +0000930}
931
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000932/// CollectInheritedProtocols - Collect all protocols in current class and
933/// those inherited by it.
934void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000935 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000936 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000937 // We can use protocol_iterator here instead of
938 // all_referenced_protocol_iterator since we are walking all categories.
939 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
940 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000941 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000942 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000943 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000944 PE = Proto->protocol_end(); P != PE; ++P) {
945 Protocols.insert(*P);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000946 CollectInheritedProtocols(*P, Protocols);
947 }
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000948 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000949
950 // Categories of this Interface.
951 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
952 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
953 CollectInheritedProtocols(CDeclChain, Protocols);
954 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
955 while (SD) {
956 CollectInheritedProtocols(SD, Protocols);
957 SD = SD->getSuperClass();
958 }
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000959 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000960 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000961 PE = OC->protocol_end(); P != PE; ++P) {
962 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000963 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000964 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
965 PE = Proto->protocol_end(); P != PE; ++P)
966 CollectInheritedProtocols(*P, Protocols);
967 }
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000968 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000969 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
970 PE = OP->protocol_end(); P != PE; ++P) {
971 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000972 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000973 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
974 PE = Proto->protocol_end(); P != PE; ++P)
975 CollectInheritedProtocols(*P, Protocols);
976 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000977 }
978}
979
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000980unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
981 unsigned count = 0;
982 // Count ivars declared in class extension.
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000983 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
984 CDecl = CDecl->getNextClassExtension())
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000985 count += CDecl->ivar_size();
986
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000987 // Count ivar defined in this class's implementation. This
988 // includes synthesized ivars.
989 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000990 count += ImplDecl->ivar_size();
991
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000992 return count;
993}
994
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000995/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
996ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
997 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
998 I = ObjCImpls.find(D);
999 if (I != ObjCImpls.end())
1000 return cast<ObjCImplementationDecl>(I->second);
1001 return 0;
1002}
1003/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1004ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
1005 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1006 I = ObjCImpls.find(D);
1007 if (I != ObjCImpls.end())
1008 return cast<ObjCCategoryImplDecl>(I->second);
1009 return 0;
1010}
1011
1012/// \brief Set the implementation of ObjCInterfaceDecl.
1013void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1014 ObjCImplementationDecl *ImplD) {
1015 assert(IFaceD && ImplD && "Passed null params");
1016 ObjCImpls[IFaceD] = ImplD;
1017}
1018/// \brief Set the implementation of ObjCCategoryDecl.
1019void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1020 ObjCCategoryImplDecl *ImplD) {
1021 assert(CatD && ImplD && "Passed null params");
1022 ObjCImpls[CatD] = ImplD;
1023}
1024
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001025/// \brief Get the copy initialization expression of VarDecl,or NULL if
1026/// none exists.
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001027Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001028 assert(VD && "Passed null params");
1029 assert(VD->hasAttr<BlocksAttr>() &&
1030 "getBlockVarCopyInits - not __block var");
Fariborz Jahanian830937b2010-12-02 17:02:11 +00001031 llvm::DenseMap<const VarDecl*, Expr*>::iterator
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001032 I = BlockVarCopyInits.find(VD);
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001033 return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
1034}
1035
1036/// \brief Set the copy inialization expression of a block var decl.
1037void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
1038 assert(VD && Init && "Passed null params");
Fariborz Jahaniand016ec22010-12-06 17:28:17 +00001039 assert(VD->hasAttr<BlocksAttr>() &&
1040 "setBlockVarCopyInits - not __block var");
Fariborz Jahanian1ceee5c2010-12-01 22:29:46 +00001041 BlockVarCopyInits[VD] = Init;
1042}
1043
John McCalla93c9342009-12-07 02:54:59 +00001044/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001045///
John McCalla93c9342009-12-07 02:54:59 +00001046/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001047/// the TypeLoc wrappers.
1048///
1049/// \param T the type that will be the basis for type source info. This type
1050/// should refer to how the declarator was written in source code, not to
1051/// what type semantic analysis resolved the declarator to.
John McCalla93c9342009-12-07 02:54:59 +00001052TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall109de5e2009-10-21 00:23:54 +00001053 unsigned DataSize) {
1054 if (!DataSize)
1055 DataSize = TypeLoc::getFullDataSizeForType(T);
1056 else
1057 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCalla93c9342009-12-07 02:54:59 +00001058 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall109de5e2009-10-21 00:23:54 +00001059
John McCalla93c9342009-12-07 02:54:59 +00001060 TypeSourceInfo *TInfo =
1061 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1062 new (TInfo) TypeSourceInfo(T);
1063 return TInfo;
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001064}
1065
John McCalla93c9342009-12-07 02:54:59 +00001066TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCalla4eb74d2009-10-23 21:14:09 +00001067 SourceLocation L) {
John McCalla93c9342009-12-07 02:54:59 +00001068 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCalla4eb74d2009-10-23 21:14:09 +00001069 DI->getTypeLoc().initialize(L);
1070 return DI;
1071}
1072
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001073const ASTRecordLayout &
1074ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1075 return getObjCLayout(D, 0);
1076}
1077
1078const ASTRecordLayout &
1079ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1080 return getObjCLayout(D->getClassInterface(), D);
1081}
1082
Chris Lattnera7674d82007-07-13 22:13:22 +00001083//===----------------------------------------------------------------------===//
1084// Type creation/memoization methods
1085//===----------------------------------------------------------------------===//
1086
John McCall0953e762009-09-24 19:53:00 +00001087QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1088 unsigned Fast = Quals.getFastQualifiers();
1089 Quals.removeFastQualifiers();
1090
1091 // Check if we've already instantiated this type.
1092 llvm::FoldingSetNodeID ID;
1093 ExtQuals::Profile(ID, TypeNode, Quals);
1094 void *InsertPos = 0;
1095 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1096 assert(EQ->getQualifiers() == Quals);
1097 QualType T = QualType(EQ, Fast);
1098 return T;
1099 }
1100
John McCall49f4e1c2010-12-10 11:01:00 +00001101 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001102 ExtQualNodes.InsertNode(New, InsertPos);
1103 QualType T = QualType(New, Fast);
1104 return T;
1105}
1106
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001107QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001108 QualType CanT = getCanonicalType(T);
1109 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001110 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001111
John McCall0953e762009-09-24 19:53:00 +00001112 // If we are composing extended qualifiers together, merge together
1113 // into one ExtQuals node.
1114 QualifierCollector Quals;
1115 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001116
John McCall0953e762009-09-24 19:53:00 +00001117 // If this type already has an address space specified, it cannot get
1118 // another one.
1119 assert(!Quals.hasAddressSpace() &&
1120 "Type cannot be in multiple addr spaces!");
1121 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001122
John McCall0953e762009-09-24 19:53:00 +00001123 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001124}
1125
Chris Lattnerb7d25532009-02-18 22:53:11 +00001126QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001127 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001128 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001129 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001130 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001131
John McCall7f040a92010-12-24 02:08:15 +00001132 if (const PointerType *ptr = T->getAs<PointerType>()) {
1133 QualType Pointee = ptr->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001134 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001135 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1136 return getPointerType(ResultType);
1137 }
1138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
John McCall0953e762009-09-24 19:53:00 +00001140 // If we are composing extended qualifiers together, merge together
1141 // into one ExtQuals node.
1142 QualifierCollector Quals;
1143 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001144
John McCall0953e762009-09-24 19:53:00 +00001145 // If this type already has an ObjCGC specified, it cannot get
1146 // another one.
1147 assert(!Quals.hasObjCGCAttr() &&
1148 "Type cannot have multiple ObjCGCs!");
1149 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001150
John McCall0953e762009-09-24 19:53:00 +00001151 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001152}
Chris Lattnera7674d82007-07-13 22:13:22 +00001153
John McCalle6a365d2010-12-19 02:44:49 +00001154const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1155 FunctionType::ExtInfo Info) {
1156 if (T->getExtInfo() == Info)
1157 return T;
1158
1159 QualType Result;
1160 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1161 Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1162 } else {
1163 const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1164 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1165 EPI.ExtInfo = Info;
1166 Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1167 FPT->getNumArgs(), EPI);
1168 }
1169
1170 return cast<FunctionType>(Result.getTypePtr());
1171}
1172
Reid Spencer5f016e22007-07-11 17:01:13 +00001173/// getComplexType - Return the uniqued reference to the type for a complex
1174/// number with the specified element type.
1175QualType ASTContext::getComplexType(QualType T) {
1176 // Unique pointers, to guarantee there is only one pointer of a particular
1177 // structure.
1178 llvm::FoldingSetNodeID ID;
1179 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 void *InsertPos = 0;
1182 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1183 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 // If the pointee type isn't canonical, this won't be a canonical type either,
1186 // so fill in the canonical type field.
1187 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001188 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001189 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // Get the new insert position for the node we care about.
1192 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001193 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 }
John McCall6b304a02009-09-24 23:30:46 +00001195 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 Types.push_back(New);
1197 ComplexTypes.InsertNode(New, InsertPos);
1198 return QualType(New, 0);
1199}
1200
Reid Spencer5f016e22007-07-11 17:01:13 +00001201/// getPointerType - Return the uniqued reference to the type for a pointer to
1202/// the specified type.
1203QualType ASTContext::getPointerType(QualType T) {
1204 // Unique pointers, to guarantee there is only one pointer of a particular
1205 // structure.
1206 llvm::FoldingSetNodeID ID;
1207 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 void *InsertPos = 0;
1210 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1211 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 // If the pointee type isn't canonical, this won't be a canonical type either,
1214 // so fill in the canonical type field.
1215 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001216 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001217 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 // Get the new insert position for the node we care about.
1220 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001221 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 }
John McCall6b304a02009-09-24 23:30:46 +00001223 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 Types.push_back(New);
1225 PointerTypes.InsertNode(New, InsertPos);
1226 return QualType(New, 0);
1227}
1228
Mike Stump1eb44332009-09-09 15:08:12 +00001229/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001230/// a pointer to the specified block.
1231QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001232 assert(T->isFunctionType() && "block of function types only");
1233 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001234 // structure.
1235 llvm::FoldingSetNodeID ID;
1236 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Steve Naroff5618bd42008-08-27 16:04:49 +00001238 void *InsertPos = 0;
1239 if (BlockPointerType *PT =
1240 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1241 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
1243 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001244 // type either so fill in the canonical type field.
1245 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001246 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001247 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Steve Naroff5618bd42008-08-27 16:04:49 +00001249 // Get the new insert position for the node we care about.
1250 BlockPointerType *NewIP =
1251 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001252 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001253 }
John McCall6b304a02009-09-24 23:30:46 +00001254 BlockPointerType *New
1255 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001256 Types.push_back(New);
1257 BlockPointerTypes.InsertNode(New, InsertPos);
1258 return QualType(New, 0);
1259}
1260
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001261/// getLValueReferenceType - Return the uniqued reference to the type for an
1262/// lvalue reference to the specified type.
John McCall54e14c42009-10-22 22:37:11 +00001263QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 // Unique pointers, to guarantee there is only one pointer of a particular
1265 // structure.
1266 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001267 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001268
1269 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001270 if (LValueReferenceType *RT =
1271 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001273
John McCall54e14c42009-10-22 22:37:11 +00001274 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1275
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 // If the referencee type isn't canonical, this won't be a canonical type
1277 // either, so fill in the canonical type field.
1278 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001279 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1280 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1281 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001282
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001284 LValueReferenceType *NewIP =
1285 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001286 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 }
1288
John McCall6b304a02009-09-24 23:30:46 +00001289 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001290 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1291 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001293 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001294
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001295 return QualType(New, 0);
1296}
1297
1298/// getRValueReferenceType - Return the uniqued reference to the type for an
1299/// rvalue reference to the specified type.
1300QualType ASTContext::getRValueReferenceType(QualType T) {
1301 // Unique pointers, to guarantee there is only one pointer of a particular
1302 // structure.
1303 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001304 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001305
1306 void *InsertPos = 0;
1307 if (RValueReferenceType *RT =
1308 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1309 return QualType(RT, 0);
1310
John McCall54e14c42009-10-22 22:37:11 +00001311 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1312
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001313 // If the referencee type isn't canonical, this won't be a canonical type
1314 // either, so fill in the canonical type field.
1315 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001316 if (InnerRef || !T.isCanonical()) {
1317 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1318 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001319
1320 // Get the new insert position for the node we care about.
1321 RValueReferenceType *NewIP =
1322 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001323 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001324 }
1325
John McCall6b304a02009-09-24 23:30:46 +00001326 RValueReferenceType *New
1327 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001328 Types.push_back(New);
1329 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 return QualType(New, 0);
1331}
1332
Sebastian Redlf30208a2009-01-24 21:16:55 +00001333/// getMemberPointerType - Return the uniqued reference to the type for a
1334/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001335QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001336 // Unique pointers, to guarantee there is only one pointer of a particular
1337 // structure.
1338 llvm::FoldingSetNodeID ID;
1339 MemberPointerType::Profile(ID, T, Cls);
1340
1341 void *InsertPos = 0;
1342 if (MemberPointerType *PT =
1343 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1344 return QualType(PT, 0);
1345
1346 // If the pointee or class type isn't canonical, this won't be a canonical
1347 // type either, so fill in the canonical type field.
1348 QualType Canonical;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001349 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001350 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1351
1352 // Get the new insert position for the node we care about.
1353 MemberPointerType *NewIP =
1354 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001355 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001356 }
John McCall6b304a02009-09-24 23:30:46 +00001357 MemberPointerType *New
1358 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001359 Types.push_back(New);
1360 MemberPointerTypes.InsertNode(New, InsertPos);
1361 return QualType(New, 0);
1362}
1363
Mike Stump1eb44332009-09-09 15:08:12 +00001364/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001365/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001366QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001367 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001368 ArrayType::ArraySizeModifier ASM,
1369 unsigned EltTypeQuals) {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001370 assert((EltTy->isDependentType() ||
1371 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedman587cbdf2009-05-29 20:17:55 +00001372 "Constant array of VLAs is illegal!");
1373
Chris Lattner38aeec72009-05-13 04:12:56 +00001374 // Convert the array size into a canonical width matching the pointer size for
1375 // the target.
1376 llvm::APInt ArySize(ArySizeIn);
Jay Foad9f71a8f2010-12-07 08:25:34 +00001377 ArySize =
1378 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001381 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001384 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001385 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Reid Spencer5f016e22007-07-11 17:01:13 +00001388 // If the element type isn't canonical, this won't be a canonical type either,
1389 // so fill in the canonical type field.
1390 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001391 if (!EltTy.isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001392 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001393 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001395 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001396 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001397 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 }
Mike Stump1eb44332009-09-09 15:08:12 +00001399
John McCall6b304a02009-09-24 23:30:46 +00001400 ConstantArrayType *New = new(*this,TypeAlignment)
1401 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001402 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 Types.push_back(New);
1404 return QualType(New, 0);
1405}
1406
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00001407/// getIncompleteArrayType - Returns a unique reference to the type for a
1408/// incomplete array of the specified element type.
1409QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) {
1410 QualType ElemTy = getBaseElementType(Ty);
1411 DeclarationName Name;
1412 llvm::SmallVector<QualType, 8> ATypes;
1413 QualType ATy = Ty;
1414 while (const ArrayType *AT = getAsArrayType(ATy)) {
1415 ATypes.push_back(ATy);
1416 ATy = AT->getElementType();
1417 }
1418 for (int i = ATypes.size() - 1; i >= 0; i--) {
1419 if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) {
1420 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star,
1421 0, VAT->getBracketsRange());
1422 }
1423 else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) {
1424 llvm::APSInt ConstVal(CAT->getSize());
1425 ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0);
1426 }
1427 else if (getAsIncompleteArrayType(ATypes[i])) {
1428 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal,
1429 0, SourceRange());
1430 }
1431 else
1432 assert(false && "DependentArrayType is seen");
1433 }
1434 return ElemTy;
1435}
1436
1437/// getVariableArrayDecayedType - Returns a vla type where known sizes
1438/// are replaced with [*]
1439QualType ASTContext::getVariableArrayDecayedType(QualType Ty) {
1440 if (Ty->isPointerType()) {
1441 QualType BaseType = Ty->getAs<PointerType>()->getPointeeType();
1442 if (isa<VariableArrayType>(BaseType)) {
1443 ArrayType *AT = dyn_cast<ArrayType>(BaseType);
1444 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1445 if (VAT->getSizeExpr()) {
1446 Ty = getUnknownSizeVariableArrayType(BaseType);
1447 Ty = getPointerType(Ty);
1448 }
1449 }
1450 }
1451 return Ty;
1452}
1453
1454
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001455/// getVariableArrayType - Returns a non-unique reference to the type for a
1456/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001457QualType ASTContext::getVariableArrayType(QualType EltTy,
1458 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001459 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001460 unsigned EltTypeQuals,
1461 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001462 // Since we don't unique expressions, it isn't possible to unique VLA's
1463 // that have an expression provided for their size.
Douglas Gregor715e9c82010-05-23 16:10:32 +00001464 QualType CanonType;
1465
1466 if (!EltTy.isCanonical()) {
Douglas Gregor715e9c82010-05-23 16:10:32 +00001467 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1468 EltTypeQuals, Brackets);
1469 }
1470
John McCall6b304a02009-09-24 23:30:46 +00001471 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor715e9c82010-05-23 16:10:32 +00001472 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001473
1474 VariableArrayTypes.push_back(New);
1475 Types.push_back(New);
1476 return QualType(New, 0);
1477}
1478
Douglas Gregor898574e2008-12-05 23:32:09 +00001479/// getDependentSizedArrayType - Returns a non-unique reference to
1480/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001481/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001482QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1483 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001484 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001485 unsigned EltTypeQuals,
1486 SourceRange Brackets) {
Douglas Gregorcb78d882009-11-19 18:03:26 +00001487 assert((!NumElts || NumElts->isTypeDependent() ||
1488 NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001489 "Size must be type- or value-dependent!");
1490
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001491 void *InsertPos = 0;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001492 DependentSizedArrayType *Canon = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00001493 llvm::FoldingSetNodeID ID;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001494
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001495 QualType CanonicalEltTy = getCanonicalType(EltTy);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001496 if (NumElts) {
1497 // Dependently-sized array types that do not have a specified
1498 // number of elements will have their sizes deduced from an
1499 // initializer.
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001500 DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM,
Douglas Gregorcb78d882009-11-19 18:03:26 +00001501 EltTypeQuals, NumElts);
1502
1503 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1504 }
1505
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001506 DependentSizedArrayType *New;
1507 if (Canon) {
1508 // We already have a canonical version of this array type; use it as
1509 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001510 New = new (*this, TypeAlignment)
1511 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1512 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001513 } else if (CanonicalEltTy == EltTy) {
1514 // This is a canonical type. Record it.
1515 New = new (*this, TypeAlignment)
1516 DependentSizedArrayType(*this, EltTy, QualType(),
1517 NumElts, ASM, EltTypeQuals, Brackets);
1518
1519 if (NumElts) {
1520#ifndef NDEBUG
1521 DependentSizedArrayType *CanonCheck
1522 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1523 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1524 (void)CanonCheck;
1525#endif
1526 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001527 }
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001528 } else {
1529 QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts,
1530 ASM, EltTypeQuals,
1531 SourceRange());
1532 New = new (*this, TypeAlignment)
1533 DependentSizedArrayType(*this, EltTy, Canon,
1534 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Douglas Gregor898574e2008-12-05 23:32:09 +00001537 Types.push_back(New);
1538 return QualType(New, 0);
1539}
1540
Eli Friedmanc5773c42008-02-15 18:16:39 +00001541QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1542 ArrayType::ArraySizeModifier ASM,
1543 unsigned EltTypeQuals) {
1544 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001545 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001546
1547 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001548 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001549 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1550 return QualType(ATP, 0);
1551
1552 // If the element type isn't canonical, this won't be a canonical type
1553 // either, so fill in the canonical type field.
1554 QualType Canonical;
1555
John McCall467b27b2009-10-22 20:10:53 +00001556 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001557 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001558 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001559
1560 // Get the new insert position for the node we care about.
1561 IncompleteArrayType *NewIP =
1562 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001563 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001564 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001565
John McCall6b304a02009-09-24 23:30:46 +00001566 IncompleteArrayType *New = new (*this, TypeAlignment)
1567 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001568
1569 IncompleteArrayTypes.InsertNode(New, InsertPos);
1570 Types.push_back(New);
1571 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001572}
1573
Steve Naroff73322922007-07-18 18:00:27 +00001574/// getVectorType - Return the unique reference to a vector type of
1575/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001576QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Bob Wilsone86d78c2010-11-10 21:56:12 +00001577 VectorType::VectorKind VecKind) {
Chris Lattner33daae62010-10-01 22:42:38 +00001578 BuiltinType *BaseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Chris Lattner33daae62010-10-01 22:42:38 +00001580 BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1581 assert(BaseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Reid Spencer5f016e22007-07-11 17:01:13 +00001583 // Check if we've already instantiated a vector of this type.
1584 llvm::FoldingSetNodeID ID;
Bob Wilsone86d78c2010-11-10 21:56:12 +00001585 VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
Chris Lattner788b0fd2010-06-23 06:00:24 +00001586
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 void *InsertPos = 0;
1588 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1589 return QualType(VTP, 0);
1590
1591 // If the element type isn't canonical, this won't be a canonical type either,
1592 // so fill in the canonical type field.
1593 QualType Canonical;
Douglas Gregor255210e2010-08-06 10:14:59 +00001594 if (!vecType.isCanonical()) {
Bob Wilson231da7e2010-11-16 00:32:20 +00001595 Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 // Get the new insert position for the node we care about.
1598 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001599 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 }
John McCall6b304a02009-09-24 23:30:46 +00001601 VectorType *New = new (*this, TypeAlignment)
Bob Wilsone86d78c2010-11-10 21:56:12 +00001602 VectorType(vecType, NumElts, Canonical, VecKind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 VectorTypes.InsertNode(New, InsertPos);
1604 Types.push_back(New);
1605 return QualType(New, 0);
1606}
1607
Nate Begeman213541a2008-04-18 23:10:10 +00001608/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001609/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001610QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001611 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Chris Lattnerf52ab252008-04-06 22:59:24 +00001613 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001614 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Steve Naroff73322922007-07-18 18:00:27 +00001616 // Check if we've already instantiated a vector of this type.
1617 llvm::FoldingSetNodeID ID;
Chris Lattner788b0fd2010-06-23 06:00:24 +00001618 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
Bob Wilsone86d78c2010-11-10 21:56:12 +00001619 VectorType::GenericVector);
Steve Naroff73322922007-07-18 18:00:27 +00001620 void *InsertPos = 0;
1621 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1622 return QualType(VTP, 0);
1623
1624 // If the element type isn't canonical, this won't be a canonical type either,
1625 // so fill in the canonical type field.
1626 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001627 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001628 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Steve Naroff73322922007-07-18 18:00:27 +00001630 // Get the new insert position for the node we care about.
1631 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001632 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001633 }
John McCall6b304a02009-09-24 23:30:46 +00001634 ExtVectorType *New = new (*this, TypeAlignment)
1635 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001636 VectorTypes.InsertNode(New, InsertPos);
1637 Types.push_back(New);
1638 return QualType(New, 0);
1639}
1640
Mike Stump1eb44332009-09-09 15:08:12 +00001641QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001642 Expr *SizeExpr,
1643 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001644 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001645 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001646 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001648 void *InsertPos = 0;
1649 DependentSizedExtVectorType *Canon
1650 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1651 DependentSizedExtVectorType *New;
1652 if (Canon) {
1653 // We already have a canonical version of this array type; use it as
1654 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001655 New = new (*this, TypeAlignment)
1656 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1657 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001658 } else {
1659 QualType CanonVecTy = getCanonicalType(vecType);
1660 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001661 New = new (*this, TypeAlignment)
1662 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1663 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001664
1665 DependentSizedExtVectorType *CanonCheck
1666 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1667 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1668 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001669 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1670 } else {
1671 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1672 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001673 New = new (*this, TypeAlignment)
1674 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001675 }
1676 }
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001678 Types.push_back(New);
1679 return QualType(New, 0);
1680}
1681
Douglas Gregor72564e72009-02-26 23:50:07 +00001682/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001683///
Rafael Espindola264ba482010-03-30 20:24:48 +00001684QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1685 const FunctionType::ExtInfo &Info) {
1686 const CallingConv CallConv = Info.getCC();
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 // Unique functions, to guarantee there is only one function of a particular
1688 // structure.
1689 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +00001690 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001693 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001694 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001698 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001699 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindola264ba482010-03-30 20:24:48 +00001700 Canonical =
1701 getFunctionNoProtoType(getCanonicalType(ResultTy),
1702 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001705 FunctionNoProtoType *NewIP =
1706 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001707 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
John McCall6b304a02009-09-24 23:30:46 +00001710 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindola264ba482010-03-30 20:24:48 +00001711 FunctionNoProtoType(ResultTy, Canonical, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001713 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 return QualType(New, 0);
1715}
1716
1717/// getFunctionType - Return a normal function type with a typed argument
1718/// list. isVariadic indicates whether the argument list includes '...'.
John McCalle23cf432010-12-14 08:05:40 +00001719QualType ASTContext::getFunctionType(QualType ResultTy,
1720 const QualType *ArgArray, unsigned NumArgs,
1721 const FunctionProtoType::ExtProtoInfo &EPI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001722 // Unique functions, to guarantee there is only one function of a particular
1723 // structure.
1724 llvm::FoldingSetNodeID ID;
John McCalle23cf432010-12-14 08:05:40 +00001725 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI);
Reid Spencer5f016e22007-07-11 17:01:13 +00001726
1727 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001728 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001729 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001730 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001731
1732 // Determine whether the type being created is already canonical or not.
John McCalle23cf432010-12-14 08:05:40 +00001733 bool isCanonical = !EPI.HasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001734 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001735 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001736 isCanonical = false;
1737
John McCalle23cf432010-12-14 08:05:40 +00001738 const CallingConv CallConv = EPI.ExtInfo.getCC();
1739
Reid Spencer5f016e22007-07-11 17:01:13 +00001740 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001741 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001743 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 llvm::SmallVector<QualType, 16> CanonicalArgs;
1745 CanonicalArgs.reserve(NumArgs);
1746 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001747 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001748
John McCalle23cf432010-12-14 08:05:40 +00001749 FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
1750 if (CanonicalEPI.HasExceptionSpec) {
1751 CanonicalEPI.HasExceptionSpec = false;
1752 CanonicalEPI.HasAnyExceptionSpec = false;
1753 CanonicalEPI.NumExceptions = 0;
1754 }
1755 CanonicalEPI.ExtInfo
1756 = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
1757
Chris Lattnerf52ab252008-04-06 22:59:24 +00001758 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001759 CanonicalArgs.data(), NumArgs,
John McCalle23cf432010-12-14 08:05:40 +00001760 CanonicalEPI);
Sebastian Redl465226e2009-05-27 22:11:52 +00001761
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001763 FunctionProtoType *NewIP =
1764 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Jeffrey Yasskinc6ed7292010-12-23 01:01:28 +00001765 assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001766 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001767
Douglas Gregor72564e72009-02-26 23:50:07 +00001768 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001769 // for two variable size arrays (for parameter and exception types) at the
1770 // end of them.
John McCalle23cf432010-12-14 08:05:40 +00001771 size_t Size = sizeof(FunctionProtoType) +
1772 NumArgs * sizeof(QualType) +
1773 EPI.NumExceptions * sizeof(QualType);
1774 FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
1775 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, EPI);
Reid Spencer5f016e22007-07-11 17:01:13 +00001776 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001777 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001778 return QualType(FTP, 0);
1779}
1780
John McCall3cb0ebd2010-03-10 03:28:59 +00001781#ifndef NDEBUG
1782static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1783 if (!isa<CXXRecordDecl>(D)) return false;
1784 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1785 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1786 return true;
1787 if (RD->getDescribedClassTemplate() &&
1788 !isa<ClassTemplateSpecializationDecl>(RD))
1789 return true;
1790 return false;
1791}
1792#endif
1793
1794/// getInjectedClassNameType - Return the unique reference to the
1795/// injected class name type for the specified templated declaration.
1796QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1797 QualType TST) {
1798 assert(NeedsInjectedClassNameType(Decl));
1799 if (Decl->TypeForDecl) {
1800 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00001801 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCall3cb0ebd2010-03-10 03:28:59 +00001802 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1803 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1804 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1805 } else {
John McCall31f17ec2010-04-27 00:57:59 +00001806 Decl->TypeForDecl =
1807 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCall3cb0ebd2010-03-10 03:28:59 +00001808 Types.push_back(Decl->TypeForDecl);
1809 }
1810 return QualType(Decl->TypeForDecl, 0);
1811}
1812
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001813/// getTypeDeclType - Return the unique reference to the type for the
1814/// specified type declaration.
John McCallbecb8d52010-03-10 06:48:02 +00001815QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001816 assert(Decl && "Passed null for Decl param");
John McCallbecb8d52010-03-10 06:48:02 +00001817 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump1eb44332009-09-09 15:08:12 +00001818
John McCall19c85762010-02-16 03:57:14 +00001819 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001820 return getTypedefType(Typedef);
John McCallbecb8d52010-03-10 06:48:02 +00001821
John McCallbecb8d52010-03-10 06:48:02 +00001822 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1823 "Template type parameter types are always available.");
1824
John McCall19c85762010-02-16 03:57:14 +00001825 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001826 assert(!Record->getPreviousDeclaration() &&
1827 "struct/union has previous declaration");
1828 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001829 return getRecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00001830 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001831 assert(!Enum->getPreviousDeclaration() &&
1832 "enum has previous declaration");
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001833 return getEnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00001834 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00001835 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1836 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stump9fdbab32009-07-31 02:02:20 +00001837 } else
John McCallbecb8d52010-03-10 06:48:02 +00001838 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001839
John McCallbecb8d52010-03-10 06:48:02 +00001840 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001841 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001842}
1843
Reid Spencer5f016e22007-07-11 17:01:13 +00001844/// getTypedefType - Return the unique reference to the type for the
1845/// specified typename decl.
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00001846QualType
1847ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00001850 if (Canonical.isNull())
1851 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001852 Decl->TypeForDecl = new(*this, TypeAlignment)
1853 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 Types.push_back(Decl->TypeForDecl);
1855 return QualType(Decl->TypeForDecl, 0);
1856}
1857
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001858QualType ASTContext::getRecordType(const RecordDecl *Decl) {
1859 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1860
1861 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
1862 if (PrevDecl->TypeForDecl)
1863 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1864
1865 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
1866 Types.push_back(Decl->TypeForDecl);
1867 return QualType(Decl->TypeForDecl, 0);
1868}
1869
1870QualType ASTContext::getEnumType(const EnumDecl *Decl) {
1871 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1872
1873 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
1874 if (PrevDecl->TypeForDecl)
1875 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1876
1877 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
1878 Types.push_back(Decl->TypeForDecl);
1879 return QualType(Decl->TypeForDecl, 0);
1880}
1881
John McCall9d156a72011-01-06 01:58:22 +00001882QualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
1883 QualType modifiedType,
1884 QualType equivalentType) {
1885 llvm::FoldingSetNodeID id;
1886 AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
1887
1888 void *insertPos = 0;
1889 AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
1890 if (type) return QualType(type, 0);
1891
1892 QualType canon = getCanonicalType(equivalentType);
1893 type = new (*this, TypeAlignment)
1894 AttributedType(canon, attrKind, modifiedType, equivalentType);
1895
1896 Types.push_back(type);
1897 AttributedTypes.InsertNode(type, insertPos);
1898
1899 return QualType(type, 0);
1900}
1901
1902
John McCall49a832b2009-10-18 09:09:24 +00001903/// \brief Retrieve a substitution-result type.
1904QualType
1905ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1906 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001907 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001908 && "replacement types must always be canonical");
1909
1910 llvm::FoldingSetNodeID ID;
1911 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1912 void *InsertPos = 0;
1913 SubstTemplateTypeParmType *SubstParm
1914 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1915
1916 if (!SubstParm) {
1917 SubstParm = new (*this, TypeAlignment)
1918 SubstTemplateTypeParmType(Parm, Replacement);
1919 Types.push_back(SubstParm);
1920 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1921 }
1922
1923 return QualType(SubstParm, 0);
1924}
1925
Douglas Gregorfab9d672009-02-05 23:33:38 +00001926/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001927/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001928/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001929QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001930 bool ParameterPack,
Douglas Gregorefed5c82010-06-16 15:23:05 +00001931 IdentifierInfo *Name) {
Douglas Gregorfab9d672009-02-05 23:33:38 +00001932 llvm::FoldingSetNodeID ID;
Douglas Gregorefed5c82010-06-16 15:23:05 +00001933 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001934 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001935 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001936 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1937
1938 if (TypeParm)
1939 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Douglas Gregorefed5c82010-06-16 15:23:05 +00001941 if (Name) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001942 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorefed5c82010-06-16 15:23:05 +00001943 TypeParm = new (*this, TypeAlignment)
1944 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001945
1946 TemplateTypeParmType *TypeCheck
1947 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1948 assert(!TypeCheck && "Template type parameter canonical type broken");
1949 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001950 } else
John McCall6b304a02009-09-24 23:30:46 +00001951 TypeParm = new (*this, TypeAlignment)
1952 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001953
1954 Types.push_back(TypeParm);
1955 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1956
1957 return QualType(TypeParm, 0);
1958}
1959
John McCall3cb0ebd2010-03-10 03:28:59 +00001960TypeSourceInfo *
1961ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1962 SourceLocation NameLoc,
1963 const TemplateArgumentListInfo &Args,
1964 QualType CanonType) {
1965 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1966
1967 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1968 TemplateSpecializationTypeLoc TL
1969 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1970 TL.setTemplateNameLoc(NameLoc);
1971 TL.setLAngleLoc(Args.getLAngleLoc());
1972 TL.setRAngleLoc(Args.getRAngleLoc());
1973 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1974 TL.setArgLocInfo(i, Args[i].getLocInfo());
1975 return DI;
1976}
1977
Mike Stump1eb44332009-09-09 15:08:12 +00001978QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001979ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00001980 const TemplateArgumentListInfo &Args,
John McCall71d74bc2010-06-13 09:25:03 +00001981 QualType Canon) {
John McCalld5532b62009-11-23 01:53:49 +00001982 unsigned NumArgs = Args.size();
1983
John McCall833ca992009-10-29 08:12:44 +00001984 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1985 ArgVec.reserve(NumArgs);
1986 for (unsigned i = 0; i != NumArgs; ++i)
1987 ArgVec.push_back(Args[i].getArgument());
1988
John McCall31f17ec2010-04-27 00:57:59 +00001989 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00001990 Canon);
John McCall833ca992009-10-29 08:12:44 +00001991}
1992
1993QualType
1994ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001995 const TemplateArgument *Args,
1996 unsigned NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00001997 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001998 if (!Canon.isNull())
1999 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002000 else
2001 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregorfc705b82009-02-26 22:19:44 +00002002
Douglas Gregor1275ae02009-07-28 23:00:59 +00002003 // Allocate the (non-canonical) template specialization type, but don't
2004 // try to unique it: these types typically have location information that
2005 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00002006 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00002007 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00002008 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00002009 TemplateSpecializationType *Spec
John McCallef990012010-06-11 11:07:21 +00002010 = new (Mem) TemplateSpecializationType(Template,
John McCall31f17ec2010-04-27 00:57:59 +00002011 Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00002012 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Douglas Gregor55f6b142009-02-09 18:46:07 +00002014 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00002015 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002016}
2017
Mike Stump1eb44332009-09-09 15:08:12 +00002018QualType
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002019ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2020 const TemplateArgument *Args,
2021 unsigned NumArgs) {
2022 // Build the canonical template specialization type.
2023 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2024 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2025 CanonArgs.reserve(NumArgs);
2026 for (unsigned I = 0; I != NumArgs; ++I)
2027 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2028
2029 // Determine whether this canonical template specialization type already
2030 // exists.
2031 llvm::FoldingSetNodeID ID;
2032 TemplateSpecializationType::Profile(ID, CanonTemplate,
2033 CanonArgs.data(), NumArgs, *this);
2034
2035 void *InsertPos = 0;
2036 TemplateSpecializationType *Spec
2037 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2038
2039 if (!Spec) {
2040 // Allocate a new canonical template specialization type.
2041 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2042 sizeof(TemplateArgument) * NumArgs),
2043 TypeAlignment);
2044 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2045 CanonArgs.data(), NumArgs,
2046 QualType());
2047 Types.push_back(Spec);
2048 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2049 }
2050
2051 assert(Spec->isDependentType() &&
2052 "Non-dependent template-id type must have a canonical type");
2053 return QualType(Spec, 0);
2054}
2055
2056QualType
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002057ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2058 NestedNameSpecifier *NNS,
2059 QualType NamedType) {
Douglas Gregore4e5b052009-03-19 00:18:19 +00002060 llvm::FoldingSetNodeID ID;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002061 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002062
2063 void *InsertPos = 0;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002064 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002065 if (T)
2066 return QualType(T, 0);
2067
Douglas Gregor789b1f62010-02-04 18:10:26 +00002068 QualType Canon = NamedType;
2069 if (!Canon.isCanonical()) {
2070 Canon = getCanonicalType(NamedType);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002071 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2072 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregor789b1f62010-02-04 18:10:26 +00002073 (void)CheckT;
2074 }
2075
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002076 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002077 Types.push_back(T);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002078 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002079 return QualType(T, 0);
2080}
2081
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002082QualType
2083ASTContext::getParenType(QualType InnerType) {
2084 llvm::FoldingSetNodeID ID;
2085 ParenType::Profile(ID, InnerType);
2086
2087 void *InsertPos = 0;
2088 ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2089 if (T)
2090 return QualType(T, 0);
2091
2092 QualType Canon = InnerType;
2093 if (!Canon.isCanonical()) {
2094 Canon = getCanonicalType(InnerType);
2095 ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2096 assert(!CheckT && "Paren canonical type broken");
2097 (void)CheckT;
2098 }
2099
2100 T = new (*this) ParenType(InnerType, Canon);
2101 Types.push_back(T);
2102 ParenTypes.InsertNode(T, InsertPos);
2103 return QualType(T, 0);
2104}
2105
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002106QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2107 NestedNameSpecifier *NNS,
2108 const IdentifierInfo *Name,
2109 QualType Canon) {
Douglas Gregord57959a2009-03-27 23:10:48 +00002110 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2111
2112 if (Canon.isNull()) {
2113 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002114 ElaboratedTypeKeyword CanonKeyword = Keyword;
2115 if (Keyword == ETK_None)
2116 CanonKeyword = ETK_Typename;
2117
2118 if (CanonNNS != NNS || CanonKeyword != Keyword)
2119 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002120 }
2121
2122 llvm::FoldingSetNodeID ID;
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002123 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002124
2125 void *InsertPos = 0;
Douglas Gregor4714c122010-03-31 17:34:00 +00002126 DependentNameType *T
2127 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregord57959a2009-03-27 23:10:48 +00002128 if (T)
2129 return QualType(T, 0);
2130
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002131 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregord57959a2009-03-27 23:10:48 +00002132 Types.push_back(T);
Douglas Gregor4714c122010-03-31 17:34:00 +00002133 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002134 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00002135}
2136
Mike Stump1eb44332009-09-09 15:08:12 +00002137QualType
John McCall33500952010-06-11 00:33:02 +00002138ASTContext::getDependentTemplateSpecializationType(
2139 ElaboratedTypeKeyword Keyword,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002140 NestedNameSpecifier *NNS,
John McCall33500952010-06-11 00:33:02 +00002141 const IdentifierInfo *Name,
2142 const TemplateArgumentListInfo &Args) {
2143 // TODO: avoid this copy
2144 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2145 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2146 ArgCopy.push_back(Args[I].getArgument());
2147 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2148 ArgCopy.size(),
2149 ArgCopy.data());
2150}
2151
2152QualType
2153ASTContext::getDependentTemplateSpecializationType(
2154 ElaboratedTypeKeyword Keyword,
2155 NestedNameSpecifier *NNS,
2156 const IdentifierInfo *Name,
2157 unsigned NumArgs,
2158 const TemplateArgument *Args) {
Douglas Gregor17343172009-04-01 00:28:59 +00002159 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2160
Douglas Gregor789b1f62010-02-04 18:10:26 +00002161 llvm::FoldingSetNodeID ID;
John McCall33500952010-06-11 00:33:02 +00002162 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2163 Name, NumArgs, Args);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002164
2165 void *InsertPos = 0;
John McCall33500952010-06-11 00:33:02 +00002166 DependentTemplateSpecializationType *T
2167 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002168 if (T)
2169 return QualType(T, 0);
2170
John McCall33500952010-06-11 00:33:02 +00002171 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002172
John McCall33500952010-06-11 00:33:02 +00002173 ElaboratedTypeKeyword CanonKeyword = Keyword;
2174 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2175
2176 bool AnyNonCanonArgs = false;
2177 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2178 for (unsigned I = 0; I != NumArgs; ++I) {
2179 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2180 if (!CanonArgs[I].structurallyEquals(Args[I]))
2181 AnyNonCanonArgs = true;
Douglas Gregor17343172009-04-01 00:28:59 +00002182 }
2183
John McCall33500952010-06-11 00:33:02 +00002184 QualType Canon;
2185 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2186 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2187 Name, NumArgs,
2188 CanonArgs.data());
2189
2190 // Find the insert position again.
2191 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2192 }
2193
2194 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2195 sizeof(TemplateArgument) * NumArgs),
2196 TypeAlignment);
John McCallef990012010-06-11 11:07:21 +00002197 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCall33500952010-06-11 00:33:02 +00002198 Name, NumArgs, Args, Canon);
Douglas Gregor17343172009-04-01 00:28:59 +00002199 Types.push_back(T);
John McCall33500952010-06-11 00:33:02 +00002200 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002201 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002202}
2203
Douglas Gregor7536dd52010-12-20 02:24:11 +00002204QualType ASTContext::getPackExpansionType(QualType Pattern) {
2205 llvm::FoldingSetNodeID ID;
2206 PackExpansionType::Profile(ID, Pattern);
2207
2208 assert(Pattern->containsUnexpandedParameterPack() &&
2209 "Pack expansions must expand one or more parameter packs");
2210 void *InsertPos = 0;
2211 PackExpansionType *T
2212 = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2213 if (T)
2214 return QualType(T, 0);
2215
2216 QualType Canon;
2217 if (!Pattern.isCanonical()) {
2218 Canon = getPackExpansionType(getCanonicalType(Pattern));
2219
2220 // Find the insert position again.
2221 PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2222 }
2223
2224 T = new (*this) PackExpansionType(Pattern, Canon);
2225 Types.push_back(T);
2226 PackExpansionTypes.InsertNode(T, InsertPos);
2227 return QualType(T, 0);
2228}
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()));
Douglas Gregora7fc9012011-01-05 18:58:31 +00002720
2721 case TemplateArgument::TemplateExpansion:
2722 return TemplateArgument(getCanonicalTemplateName(
2723 Arg.getAsTemplateOrTemplatePattern()),
2724 true);
2725
Douglas Gregor1275ae02009-07-28 23:00:59 +00002726 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002727 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002728 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Douglas Gregor1275ae02009-07-28 23:00:59 +00002730 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00002731 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Douglas Gregor1275ae02009-07-28 23:00:59 +00002733 case TemplateArgument::Pack: {
Douglas Gregor87dd6972010-12-20 16:52:59 +00002734 if (Arg.pack_size() == 0)
2735 return Arg;
2736
Douglas Gregor910f8002010-11-07 23:05:16 +00002737 TemplateArgument *CanonArgs
2738 = new (*this) TemplateArgument[Arg.pack_size()];
Douglas Gregor1275ae02009-07-28 23:00:59 +00002739 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002740 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002741 AEnd = Arg.pack_end();
2742 A != AEnd; (void)++A, ++Idx)
2743 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Douglas Gregor910f8002010-11-07 23:05:16 +00002745 return TemplateArgument(CanonArgs, Arg.pack_size());
Douglas Gregor1275ae02009-07-28 23:00:59 +00002746 }
2747 }
2748
2749 // Silence GCC warning
2750 assert(false && "Unhandled template argument kind");
2751 return TemplateArgument();
2752}
2753
Douglas Gregord57959a2009-03-27 23:10:48 +00002754NestedNameSpecifier *
2755ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002756 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002757 return 0;
2758
2759 switch (NNS->getKind()) {
2760 case NestedNameSpecifier::Identifier:
2761 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002762 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002763 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2764 NNS->getAsIdentifier());
2765
2766 case NestedNameSpecifier::Namespace:
2767 // A namespace is canonical; build a nested-name-specifier with
2768 // this namespace and no prefix.
2769 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2770
2771 case NestedNameSpecifier::TypeSpec:
2772 case NestedNameSpecifier::TypeSpecWithTemplate: {
2773 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Douglas Gregor264bf662010-11-04 00:09:33 +00002774
2775 // If we have some kind of dependent-named type (e.g., "typename T::type"),
2776 // break it apart into its prefix and identifier, then reconsititute those
2777 // as the canonical nested-name-specifier. This is required to canonicalize
2778 // a dependent nested-name-specifier involving typedefs of dependent-name
2779 // types, e.g.,
2780 // typedef typename T::type T1;
2781 // typedef typename T1::type T2;
2782 if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
2783 NestedNameSpecifier *Prefix
2784 = getCanonicalNestedNameSpecifier(DNT->getQualifier());
2785 return NestedNameSpecifier::Create(*this, Prefix,
2786 const_cast<IdentifierInfo *>(DNT->getIdentifier()));
2787 }
2788
Douglas Gregor643f8432010-11-04 00:14:23 +00002789 // Do the same thing as above, but with dependent-named specializations.
Douglas Gregor264bf662010-11-04 00:09:33 +00002790 if (const DependentTemplateSpecializationType *DTST
2791 = T->getAs<DependentTemplateSpecializationType>()) {
2792 NestedNameSpecifier *Prefix
2793 = getCanonicalNestedNameSpecifier(DTST->getQualifier());
2794 TemplateName Name
2795 = getDependentTemplateName(Prefix, DTST->getIdentifier());
2796 T = getTemplateSpecializationType(Name,
2797 DTST->getArgs(), DTST->getNumArgs());
2798 T = getCanonicalType(T);
2799 }
2800
2801 return NestedNameSpecifier::Create(*this, 0, false, T.getTypePtr());
Douglas Gregord57959a2009-03-27 23:10:48 +00002802 }
2803
2804 case NestedNameSpecifier::Global:
2805 // The global specifier is canonical and unique.
2806 return NNS;
2807 }
2808
2809 // Required to silence a GCC warning
2810 return 0;
2811}
2812
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002813
2814const ArrayType *ASTContext::getAsArrayType(QualType T) {
2815 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00002816 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002817 // Handle the common positive case fast.
2818 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2819 return AT;
2820 }
Mike Stump1eb44332009-09-09 15:08:12 +00002821
John McCall0953e762009-09-24 19:53:00 +00002822 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002823 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002824 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002825 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002826
John McCall0953e762009-09-24 19:53:00 +00002827 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002828 // implements C99 6.7.3p8: "If the specification of an array type includes
2829 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002830
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002831 // If we get here, we either have type qualifiers on the type, or we have
2832 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002833 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002834
John McCall0953e762009-09-24 19:53:00 +00002835 QualifierCollector Qs;
John McCall49f4e1c2010-12-10 11:01:00 +00002836 const Type *Ty = Qs.strip(T.getDesugaredType(*this));
Mike Stump1eb44332009-09-09 15:08:12 +00002837
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002838 // If we have a simple case, just return now.
2839 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002840 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002841 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002842
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002843 // Otherwise, we have an array and we have qualifiers on it. Push the
2844 // qualifiers into the array element type and return a new array type.
2845 // Get the canonical version of the element with the extra qualifiers on it.
2846 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002847 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002848
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002849 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2850 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2851 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002852 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002853 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2854 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2855 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002856 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002857
Mike Stump1eb44332009-09-09 15:08:12 +00002858 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002859 = dyn_cast<DependentSizedArrayType>(ATy))
2860 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002861 getDependentSizedArrayType(NewEltTy,
John McCall3fa5cae2010-10-26 07:05:15 +00002862 DSAT->getSizeExpr(),
Douglas Gregor898574e2008-12-05 23:32:09 +00002863 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002864 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002865 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002866
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002867 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002868 return cast<ArrayType>(getVariableArrayType(NewEltTy,
John McCall3fa5cae2010-10-26 07:05:15 +00002869 VAT->getSizeExpr(),
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002870 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002871 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002872 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002873}
2874
Chris Lattnere6327742008-04-02 05:18:44 +00002875/// getArrayDecayedType - Return the properly qualified result of decaying the
2876/// specified array type to a pointer. This operation is non-trivial when
2877/// handling typedefs etc. The canonical type of "T" must be an array type,
2878/// this returns a pointer to a properly qualified element of the array.
2879///
2880/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2881QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002882 // Get the element type with 'getAsArrayType' so that we don't lose any
2883 // typedefs in the element type of the array. This also handles propagation
2884 // of type qualifiers from the array type into the element type if present
2885 // (C99 6.7.3p8).
2886 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2887 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002889 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002890
2891 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002892 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002893}
2894
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002895QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002896 QualifierCollector Qs;
Benjamin Kramer02379412010-04-27 17:12:11 +00002897 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2898 QT = AT->getElementType();
John McCall49f4e1c2010-12-10 11:01:00 +00002899 return Qs.apply(*this, QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002900}
2901
Anders Carlssonfbbce492009-09-25 01:23:32 +00002902QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2903 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002904
Anders Carlssonfbbce492009-09-25 01:23:32 +00002905 if (const ArrayType *AT = getAsArrayType(ElemTy))
2906 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Anders Carlsson6183a992008-12-21 03:44:36 +00002908 return ElemTy;
2909}
2910
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002911/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002912uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002913ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2914 uint64_t ElementCount = 1;
2915 do {
2916 ElementCount *= CA->getSize().getZExtValue();
2917 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2918 } while (CA);
2919 return ElementCount;
2920}
2921
Reid Spencer5f016e22007-07-11 17:01:13 +00002922/// getFloatingRank - Return a relative rank for floating point types.
2923/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002924static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002925 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002926 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002927
John McCall183700f2009-09-21 23:43:11 +00002928 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2929 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002930 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002931 case BuiltinType::Float: return FloatRank;
2932 case BuiltinType::Double: return DoubleRank;
2933 case BuiltinType::LongDouble: return LongDoubleRank;
2934 }
2935}
2936
Mike Stump1eb44332009-09-09 15:08:12 +00002937/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2938/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002939/// 'typeDomain' is a real floating point or complex type.
2940/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002941QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2942 QualType Domain) const {
2943 FloatingRank EltRank = getFloatingRank(Size);
2944 if (Domain->isComplexType()) {
2945 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002946 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002947 case FloatRank: return FloatComplexTy;
2948 case DoubleRank: return DoubleComplexTy;
2949 case LongDoubleRank: return LongDoubleComplexTy;
2950 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002951 }
Chris Lattner1361b112008-04-06 23:58:54 +00002952
2953 assert(Domain->isRealFloatingType() && "Unknown domain!");
2954 switch (EltRank) {
2955 default: assert(0 && "getFloatingRank(): illegal value for rank");
2956 case FloatRank: return FloatTy;
2957 case DoubleRank: return DoubleTy;
2958 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002959 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002960}
2961
Chris Lattner7cfeb082008-04-06 23:55:33 +00002962/// getFloatingTypeOrder - Compare the rank of the two specified floating
2963/// point types, ignoring the domain of the type (i.e. 'double' ==
2964/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002965/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002966int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2967 FloatingRank LHSR = getFloatingRank(LHS);
2968 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Chris Lattnera75cea32008-04-06 23:38:49 +00002970 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002971 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002972 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002973 return 1;
2974 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002975}
2976
Chris Lattnerf52ab252008-04-06 22:59:24 +00002977/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2978/// routine will assert if passed a built-in type that isn't an integer or enum,
2979/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002980unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002981 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002982 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00002983 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00002984
Chris Lattner3f59c972010-12-25 23:25:43 +00002985 if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
2986 T->isSpecificBuiltinType(BuiltinType::WChar_U))
Eli Friedmana3426752009-07-05 23:44:27 +00002987 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2988
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002989 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2990 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2991
2992 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2993 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2994
Chris Lattnerf52ab252008-04-06 22:59:24 +00002995 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002996 default: assert(0 && "getIntegerRank(): not a built-in integer");
2997 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002998 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002999 case BuiltinType::Char_S:
3000 case BuiltinType::Char_U:
3001 case BuiltinType::SChar:
3002 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003003 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003004 case BuiltinType::Short:
3005 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003006 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003007 case BuiltinType::Int:
3008 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003009 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003010 case BuiltinType::Long:
3011 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003012 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00003013 case BuiltinType::LongLong:
3014 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00003015 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00003016 case BuiltinType::Int128:
3017 case BuiltinType::UInt128:
3018 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00003019 }
3020}
3021
Eli Friedman04e83572009-08-20 04:21:42 +00003022/// \brief Whether this is a promotable bitfield reference according
3023/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
3024///
3025/// \returns the type this bit-field will promote to, or NULL if no
3026/// promotion occurs.
3027QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregorceafbde2010-05-24 20:13:53 +00003028 if (E->isTypeDependent() || E->isValueDependent())
3029 return QualType();
3030
Eli Friedman04e83572009-08-20 04:21:42 +00003031 FieldDecl *Field = E->getBitField();
3032 if (!Field)
3033 return QualType();
3034
3035 QualType FT = Field->getType();
3036
3037 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
3038 uint64_t BitWidth = BitWidthAP.getZExtValue();
3039 uint64_t IntSize = getTypeSize(IntTy);
3040 // GCC extension compatibility: if the bit-field size is less than or equal
3041 // to the size of int, it gets promoted no matter what its type is.
3042 // For instance, unsigned long bf : 4 gets promoted to signed int.
3043 if (BitWidth < IntSize)
3044 return IntTy;
3045
3046 if (BitWidth == IntSize)
3047 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
3048
3049 // Types bigger than int are not subject to promotions, and therefore act
3050 // like the base type.
3051 // FIXME: This doesn't quite match what gcc does, but what gcc does here
3052 // is ridiculous.
3053 return QualType();
3054}
3055
Eli Friedmana95d7572009-08-19 07:44:53 +00003056/// getPromotedIntegerType - Returns the type that Promotable will
3057/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3058/// integer type.
3059QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
3060 assert(!Promotable.isNull());
3061 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00003062 if (const EnumType *ET = Promotable->getAs<EnumType>())
3063 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00003064 if (Promotable->isSignedIntegerType())
3065 return IntTy;
3066 uint64_t PromotableSize = getTypeSize(Promotable);
3067 uint64_t IntSize = getTypeSize(IntTy);
3068 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3069 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3070}
3071
Mike Stump1eb44332009-09-09 15:08:12 +00003072/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00003073/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00003074/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003075int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00003076 Type *LHSC = getCanonicalType(LHS).getTypePtr();
3077 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00003078 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003079
Chris Lattnerf52ab252008-04-06 22:59:24 +00003080 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3081 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00003082
Chris Lattner7cfeb082008-04-06 23:55:33 +00003083 unsigned LHSRank = getIntegerRank(LHSC);
3084 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00003085
Chris Lattner7cfeb082008-04-06 23:55:33 +00003086 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3087 if (LHSRank == RHSRank) return 0;
3088 return LHSRank > RHSRank ? 1 : -1;
3089 }
Mike Stump1eb44332009-09-09 15:08:12 +00003090
Chris Lattner7cfeb082008-04-06 23:55:33 +00003091 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3092 if (LHSUnsigned) {
3093 // If the unsigned [LHS] type is larger, return it.
3094 if (LHSRank >= RHSRank)
3095 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00003096
Chris Lattner7cfeb082008-04-06 23:55:33 +00003097 // If the signed type can represent all values of the unsigned type, it
3098 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003099 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003100 return -1;
3101 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00003102
Chris Lattner7cfeb082008-04-06 23:55:33 +00003103 // If the unsigned [RHS] type is larger, return it.
3104 if (RHSRank >= LHSRank)
3105 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Chris Lattner7cfeb082008-04-06 23:55:33 +00003107 // If the signed type can represent all values of the unsigned type, it
3108 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003109 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003110 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00003111}
Anders Carlsson71993dd2007-08-17 05:31:46 +00003112
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003113static RecordDecl *
3114CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
3115 SourceLocation L, IdentifierInfo *Id) {
3116 if (Ctx.getLangOptions().CPlusPlus)
3117 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
3118 else
3119 return RecordDecl::Create(Ctx, TK, DC, L, Id);
3120}
3121
Mike Stump1eb44332009-09-09 15:08:12 +00003122// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00003123QualType ASTContext::getCFConstantStringType() {
3124 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003125 CFConstantStringTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003126 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003127 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00003128 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003129
Anders Carlssonf06273f2007-11-19 00:25:30 +00003130 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00003131
Anders Carlsson71993dd2007-08-17 05:31:46 +00003132 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00003133 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00003134 // int flags;
3135 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00003136 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00003137 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00003138 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00003139 FieldTypes[3] = LongTy;
3140
Anders Carlsson71993dd2007-08-17 05:31:46 +00003141 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00003142 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003143 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00003144 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003145 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003146 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003147 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003148 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003149 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003150 }
3151
Douglas Gregor838db382010-02-11 01:19:42 +00003152 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00003153 }
Mike Stump1eb44332009-09-09 15:08:12 +00003154
Anders Carlsson71993dd2007-08-17 05:31:46 +00003155 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00003156}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003157
Douglas Gregor319ac892009-04-23 22:29:11 +00003158void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003159 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003160 assert(Rec && "Invalid CFConstantStringType");
3161 CFConstantStringTypeDecl = Rec->getDecl();
3162}
3163
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003164// getNSConstantStringType - Return the type used for constant NSStrings.
3165QualType ASTContext::getNSConstantStringType() {
3166 if (!NSConstantStringTypeDecl) {
3167 NSConstantStringTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003168 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003169 &Idents.get("__builtin_NSString"));
3170 NSConstantStringTypeDecl->startDefinition();
3171
3172 QualType FieldTypes[3];
3173
3174 // const int *isa;
3175 FieldTypes[0] = getPointerType(IntTy.withConst());
3176 // const char *str;
3177 FieldTypes[1] = getPointerType(CharTy.withConst());
3178 // unsigned int length;
3179 FieldTypes[2] = UnsignedIntTy;
3180
3181 // Create fields
3182 for (unsigned i = 0; i < 3; ++i) {
3183 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3184 SourceLocation(), 0,
3185 FieldTypes[i], /*TInfo=*/0,
3186 /*BitWidth=*/0,
3187 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003188 Field->setAccess(AS_public);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003189 NSConstantStringTypeDecl->addDecl(Field);
3190 }
3191
3192 NSConstantStringTypeDecl->completeDefinition();
3193 }
3194
3195 return getTagDeclType(NSConstantStringTypeDecl);
3196}
3197
3198void ASTContext::setNSConstantStringType(QualType T) {
3199 const RecordType *Rec = T->getAs<RecordType>();
3200 assert(Rec && "Invalid NSConstantStringType");
3201 NSConstantStringTypeDecl = Rec->getDecl();
3202}
3203
Mike Stump1eb44332009-09-09 15:08:12 +00003204QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003205 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00003206 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003207 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003208 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00003209 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00003210
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003211 QualType FieldTypes[] = {
3212 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00003213 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003214 getPointerType(UnsignedLongTy),
3215 getConstantArrayType(UnsignedLongTy,
3216 llvm::APInt(32, 5), ArrayType::Normal, 0)
3217 };
Mike Stump1eb44332009-09-09 15:08:12 +00003218
Douglas Gregor44b43212008-12-11 16:49:14 +00003219 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003220 FieldDecl *Field = FieldDecl::Create(*this,
3221 ObjCFastEnumerationStateTypeDecl,
3222 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003223 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003224 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003225 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003226 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003227 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003228 }
Mike Stump1eb44332009-09-09 15:08:12 +00003229
Douglas Gregor838db382010-02-11 01:19:42 +00003230 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003231 }
Mike Stump1eb44332009-09-09 15:08:12 +00003232
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003233 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3234}
3235
Mike Stumpadaaad32009-10-20 02:12:22 +00003236QualType ASTContext::getBlockDescriptorType() {
3237 if (BlockDescriptorType)
3238 return getTagDeclType(BlockDescriptorType);
3239
3240 RecordDecl *T;
3241 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003242 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003243 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00003244 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003245
3246 QualType FieldTypes[] = {
3247 UnsignedLongTy,
3248 UnsignedLongTy,
3249 };
3250
3251 const char *FieldNames[] = {
3252 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00003253 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00003254 };
3255
3256 for (size_t i = 0; i < 2; ++i) {
3257 FieldDecl *Field = FieldDecl::Create(*this,
3258 T,
3259 SourceLocation(),
3260 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003261 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00003262 /*BitWidth=*/0,
3263 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003264 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003265 T->addDecl(Field);
3266 }
3267
Douglas Gregor838db382010-02-11 01:19:42 +00003268 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003269
3270 BlockDescriptorType = T;
3271
3272 return getTagDeclType(BlockDescriptorType);
3273}
3274
3275void ASTContext::setBlockDescriptorType(QualType T) {
3276 const RecordType *Rec = T->getAs<RecordType>();
3277 assert(Rec && "Invalid BlockDescriptorType");
3278 BlockDescriptorType = Rec->getDecl();
3279}
3280
Mike Stump083c25e2009-10-22 00:49:09 +00003281QualType ASTContext::getBlockDescriptorExtendedType() {
3282 if (BlockDescriptorExtendedType)
3283 return getTagDeclType(BlockDescriptorExtendedType);
3284
3285 RecordDecl *T;
3286 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003287 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003288 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00003289 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003290
3291 QualType FieldTypes[] = {
3292 UnsignedLongTy,
3293 UnsignedLongTy,
3294 getPointerType(VoidPtrTy),
3295 getPointerType(VoidPtrTy)
3296 };
3297
3298 const char *FieldNames[] = {
3299 "reserved",
3300 "Size",
3301 "CopyFuncPtr",
3302 "DestroyFuncPtr"
3303 };
3304
3305 for (size_t i = 0; i < 4; ++i) {
3306 FieldDecl *Field = FieldDecl::Create(*this,
3307 T,
3308 SourceLocation(),
3309 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003310 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00003311 /*BitWidth=*/0,
3312 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003313 Field->setAccess(AS_public);
Mike Stump083c25e2009-10-22 00:49:09 +00003314 T->addDecl(Field);
3315 }
3316
Douglas Gregor838db382010-02-11 01:19:42 +00003317 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003318
3319 BlockDescriptorExtendedType = T;
3320
3321 return getTagDeclType(BlockDescriptorExtendedType);
3322}
3323
3324void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3325 const RecordType *Rec = T->getAs<RecordType>();
3326 assert(Rec && "Invalid BlockDescriptorType");
3327 BlockDescriptorExtendedType = Rec->getDecl();
3328}
3329
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003330bool ASTContext::BlockRequiresCopying(QualType Ty) {
3331 if (Ty->isBlockPointerType())
3332 return true;
3333 if (isObjCNSObjectType(Ty))
3334 return true;
3335 if (Ty->isObjCObjectPointerType())
3336 return true;
Fariborz Jahaniane38be612010-11-17 00:21:28 +00003337 if (getLangOptions().CPlusPlus) {
3338 if (const RecordType *RT = Ty->getAs<RecordType>()) {
3339 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3340 return RD->hasConstCopyConstructor(*this);
3341
3342 }
3343 }
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003344 return false;
3345}
3346
Daniel Dunbar4087f272010-08-17 22:39:59 +00003347QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003348 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003349 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003350 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003351 // unsigned int __flags;
3352 // unsigned int __size;
Eli Friedmana7e68452010-08-22 01:00:03 +00003353 // void *__copy_helper; // as needed
3354 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003355 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003356 // } *
3357
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003358 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3359
3360 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003361 llvm::SmallString<36> Name;
3362 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3363 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003364 RecordDecl *T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003365 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003366 &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003367 T->startDefinition();
3368 QualType Int32Ty = IntTy;
3369 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3370 QualType FieldTypes[] = {
3371 getPointerType(VoidPtrTy),
3372 getPointerType(getTagDeclType(T)),
3373 Int32Ty,
3374 Int32Ty,
3375 getPointerType(VoidPtrTy),
3376 getPointerType(VoidPtrTy),
3377 Ty
3378 };
3379
Daniel Dunbar4087f272010-08-17 22:39:59 +00003380 llvm::StringRef FieldNames[] = {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003381 "__isa",
3382 "__forwarding",
3383 "__flags",
3384 "__size",
3385 "__copy_helper",
3386 "__destroy_helper",
3387 DeclName,
3388 };
3389
3390 for (size_t i = 0; i < 7; ++i) {
3391 if (!HasCopyAndDispose && i >=4 && i <= 5)
3392 continue;
3393 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3394 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003395 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003396 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003397 Field->setAccess(AS_public);
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003398 T->addDecl(Field);
3399 }
3400
Douglas Gregor838db382010-02-11 01:19:42 +00003401 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003402
3403 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003404}
3405
3406
3407QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00003408 bool BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +00003409 llvm::SmallVectorImpl<const Expr *> &Layout) {
3410
Mike Stumpadaaad32009-10-20 02:12:22 +00003411 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003412 llvm::SmallString<36> Name;
3413 llvm::raw_svector_ostream(Name) << "__block_literal_"
3414 << ++UniqueBlockParmTypeID;
Mike Stumpadaaad32009-10-20 02:12:22 +00003415 RecordDecl *T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003416 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003417 &Idents.get(Name.str()));
John McCall5cfa0112010-02-05 01:33:36 +00003418 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003419 QualType FieldTypes[] = {
3420 getPointerType(VoidPtrTy),
3421 IntTy,
3422 IntTy,
3423 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00003424 (BlockHasCopyDispose ?
3425 getPointerType(getBlockDescriptorExtendedType()) :
3426 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00003427 };
3428
3429 const char *FieldNames[] = {
3430 "__isa",
3431 "__flags",
3432 "__reserved",
3433 "__FuncPtr",
3434 "__descriptor"
3435 };
3436
3437 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00003438 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003439 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003440 FieldTypes[i], /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003441 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003442 Field->setAccess(AS_public);
Mike Stumpea26cb52009-10-21 03:49:08 +00003443 T->addDecl(Field);
3444 }
3445
John McCallea1471e2010-05-20 01:18:31 +00003446 for (unsigned i = 0; i < Layout.size(); ++i) {
3447 const Expr *E = Layout[i];
Mike Stumpea26cb52009-10-21 03:49:08 +00003448
John McCallea1471e2010-05-20 01:18:31 +00003449 QualType FieldType = E->getType();
3450 IdentifierInfo *FieldName = 0;
3451 if (isa<CXXThisExpr>(E)) {
3452 FieldName = &Idents.get("this");
3453 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3454 const ValueDecl *D = BDRE->getDecl();
3455 FieldName = D->getIdentifier();
3456 if (BDRE->isByRef())
Daniel Dunbar4087f272010-08-17 22:39:59 +00003457 FieldType = BuildByRefType(D->getName(), FieldType);
John McCallea1471e2010-05-20 01:18:31 +00003458 } else {
3459 // Padding.
3460 assert(isa<ConstantArrayType>(FieldType) &&
3461 isa<DeclRefExpr>(E) &&
3462 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3463 "doesn't match characteristics of padding decl");
3464 }
Mike Stumpea26cb52009-10-21 03:49:08 +00003465
3466 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallea1471e2010-05-20 01:18:31 +00003467 FieldName, FieldType, /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003468 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003469 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003470 T->addDecl(Field);
3471 }
3472
Douglas Gregor838db382010-02-11 01:19:42 +00003473 T->completeDefinition();
Mike Stumpea26cb52009-10-21 03:49:08 +00003474
3475 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00003476}
3477
Douglas Gregor319ac892009-04-23 22:29:11 +00003478void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003479 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003480 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3481 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3482}
3483
Anders Carlssone8c49532007-10-29 06:33:42 +00003484// This returns true if a type has been typedefed to BOOL:
3485// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003486static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003487 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003488 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3489 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003490
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003491 return false;
3492}
3493
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003494/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003495/// purpose.
Ken Dyckaa8741a2010-01-11 19:19:56 +00003496CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck199c3d62010-01-11 17:06:35 +00003497 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003498
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003499 // Make all integer and enum types at least as large as an int
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003500 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003501 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003502 // Treat arrays as pointers, since that's how they're passed in.
3503 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003504 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003505 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003506}
3507
3508static inline
3509std::string charUnitsToString(const CharUnits &CU) {
3510 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003511}
3512
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003513/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall5e530af2009-11-17 19:33:30 +00003514/// declaration.
3515void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3516 std::string& S) {
3517 const BlockDecl *Decl = Expr->getBlockDecl();
3518 QualType BlockTy =
3519 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3520 // Encode result type.
John McCallc71a4912010-06-04 19:02:56 +00003521 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall5e530af2009-11-17 19:33:30 +00003522 // Compute size of all parameters.
3523 // Start with computing size of a pointer in number of bytes.
3524 // FIXME: There might(should) be a better way of doing this computation!
3525 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003526 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3527 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003528 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall5e530af2009-11-17 19:33:30 +00003529 E = Decl->param_end(); PI != E; ++PI) {
3530 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003531 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003532 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003533 ParmOffset += sz;
3534 }
3535 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003536 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003537 // Block pointer and offset.
3538 S += "@?0";
3539 ParmOffset = PtrSize;
3540
3541 // Argument types.
3542 ParmOffset = PtrSize;
3543 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3544 Decl->param_end(); PI != E; ++PI) {
3545 ParmVarDecl *PVDecl = *PI;
3546 QualType PType = PVDecl->getOriginalType();
3547 if (const ArrayType *AT =
3548 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3549 // Use array's original type only if it has known number of
3550 // elements.
3551 if (!isa<ConstantArrayType>(AT))
3552 PType = PVDecl->getType();
3553 } else if (PType->isFunctionType())
3554 PType = PVDecl->getType();
3555 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003556 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003557 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003558 }
3559}
3560
David Chisnall5389f482010-12-30 14:05:53 +00003561void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
3562 std::string& S) {
3563 // Encode result type.
3564 getObjCEncodingForType(Decl->getResultType(), S);
3565 CharUnits ParmOffset;
3566 // Compute size of all parameters.
3567 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3568 E = Decl->param_end(); PI != E; ++PI) {
3569 QualType PType = (*PI)->getType();
3570 CharUnits sz = getObjCEncodingTypeSize(PType);
3571 assert (sz.isPositive() &&
3572 "getObjCEncodingForMethodDecl - Incomplete param type");
3573 ParmOffset += sz;
3574 }
3575 S += charUnitsToString(ParmOffset);
3576 ParmOffset = CharUnits::Zero();
3577
3578 // Argument types.
3579 for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
3580 E = Decl->param_end(); PI != E; ++PI) {
3581 ParmVarDecl *PVDecl = *PI;
3582 QualType PType = PVDecl->getOriginalType();
3583 if (const ArrayType *AT =
3584 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3585 // Use array's original type only if it has known number of
3586 // elements.
3587 if (!isa<ConstantArrayType>(AT))
3588 PType = PVDecl->getType();
3589 } else if (PType->isFunctionType())
3590 PType = PVDecl->getType();
3591 getObjCEncodingForType(PType, S);
3592 S += charUnitsToString(ParmOffset);
3593 ParmOffset += getObjCEncodingTypeSize(PType);
3594 }
3595}
3596
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003597/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003598/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003599void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003600 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003601 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003602 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003603 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003604 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003605 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003606 // Compute size of all parameters.
3607 // Start with computing size of a pointer in number of bytes.
3608 // FIXME: There might(should) be a better way of doing this computation!
3609 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003610 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003611 // The first two arguments (self and _cmd) are pointers; account for
3612 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003613 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003614 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003615 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003616 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003617 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003618 assert (sz.isPositive() &&
3619 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003620 ParmOffset += sz;
3621 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003622 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003623 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003624 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003625
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003626 // Argument types.
3627 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003628 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003629 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003630 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003631 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003632 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003633 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3634 // Use array's original type only if it has known number of
3635 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003636 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003637 PType = PVDecl->getType();
3638 } else if (PType->isFunctionType())
3639 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003640 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003641 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003642 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003643 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003644 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003645 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003646 }
3647}
3648
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003649/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003650/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003651/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3652/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003653/// Property attributes are stored as a comma-delimited C string. The simple
3654/// attributes readonly and bycopy are encoded as single characters. The
3655/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3656/// encoded as single characters, followed by an identifier. Property types
3657/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003658/// these attributes are defined by the following enumeration:
3659/// @code
3660/// enum PropertyAttributes {
3661/// kPropertyReadOnly = 'R', // property is read-only.
3662/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3663/// kPropertyByref = '&', // property is a reference to the value last assigned
3664/// kPropertyDynamic = 'D', // property is dynamic
3665/// kPropertyGetter = 'G', // followed by getter selector name
3666/// kPropertySetter = 'S', // followed by setter selector name
3667/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3668/// kPropertyType = 't' // followed by old-style type encoding.
3669/// kPropertyWeak = 'W' // 'weak' property
3670/// kPropertyStrong = 'P' // property GC'able
3671/// kPropertyNonAtomic = 'N' // property non-atomic
3672/// };
3673/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003674void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003675 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003676 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003677 // Collect information from the property implementation decl(s).
3678 bool Dynamic = false;
3679 ObjCPropertyImplDecl *SynthesizePID = 0;
3680
3681 // FIXME: Duplicated code due to poor abstraction.
3682 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003683 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003684 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3685 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003686 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003687 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003688 ObjCPropertyImplDecl *PID = *i;
3689 if (PID->getPropertyDecl() == PD) {
3690 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3691 Dynamic = true;
3692 } else {
3693 SynthesizePID = PID;
3694 }
3695 }
3696 }
3697 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003698 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003699 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003700 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003701 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003702 ObjCPropertyImplDecl *PID = *i;
3703 if (PID->getPropertyDecl() == PD) {
3704 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3705 Dynamic = true;
3706 } else {
3707 SynthesizePID = PID;
3708 }
3709 }
Mike Stump1eb44332009-09-09 15:08:12 +00003710 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003711 }
3712 }
3713
3714 // FIXME: This is not very efficient.
3715 S = "T";
3716
3717 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003718 // GCC has some special rules regarding encoding of properties which
3719 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003720 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003721 true /* outermost type */,
3722 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003723
3724 if (PD->isReadOnly()) {
3725 S += ",R";
3726 } else {
3727 switch (PD->getSetterKind()) {
3728 case ObjCPropertyDecl::Assign: break;
3729 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003730 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003731 }
3732 }
3733
3734 // It really isn't clear at all what this means, since properties
3735 // are "dynamic by default".
3736 if (Dynamic)
3737 S += ",D";
3738
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003739 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3740 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003741
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003742 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3743 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003744 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003745 }
3746
3747 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3748 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003749 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003750 }
3751
3752 if (SynthesizePID) {
3753 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3754 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003755 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003756 }
3757
3758 // FIXME: OBJCGC: weak & strong
3759}
3760
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003761/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003762/// Another legacy compatibility encoding: 32-bit longs are encoded as
3763/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003764/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3765///
3766void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003767 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003768 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003769 if (BT->getKind() == BuiltinType::ULong &&
3770 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003771 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003772 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003773 if (BT->getKind() == BuiltinType::Long &&
3774 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003775 PointeeTy = IntTy;
3776 }
3777 }
3778}
3779
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003780void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003781 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003782 // We follow the behavior of gcc, expanding structures which are
3783 // directly pointed to, and expanding embedded structures. Note that
3784 // these rules are sufficient to prevent recursive encoding of the
3785 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003786 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003787 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003788}
3789
David Chisnall64fd7e82010-06-04 01:10:52 +00003790static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3791 switch (T->getAs<BuiltinType>()->getKind()) {
3792 default: assert(0 && "Unhandled builtin type kind");
3793 case BuiltinType::Void: return 'v';
3794 case BuiltinType::Bool: return 'B';
3795 case BuiltinType::Char_U:
3796 case BuiltinType::UChar: return 'C';
3797 case BuiltinType::UShort: return 'S';
3798 case BuiltinType::UInt: return 'I';
3799 case BuiltinType::ULong:
3800 return
3801 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3802 case BuiltinType::UInt128: return 'T';
3803 case BuiltinType::ULongLong: return 'Q';
3804 case BuiltinType::Char_S:
3805 case BuiltinType::SChar: return 'c';
3806 case BuiltinType::Short: return 's';
Chris Lattner3f59c972010-12-25 23:25:43 +00003807 case BuiltinType::WChar_S:
3808 case BuiltinType::WChar_U:
David Chisnall64fd7e82010-06-04 01:10:52 +00003809 case BuiltinType::Int: return 'i';
3810 case BuiltinType::Long:
3811 return
3812 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3813 case BuiltinType::LongLong: return 'q';
3814 case BuiltinType::Int128: return 't';
3815 case BuiltinType::Float: return 'f';
3816 case BuiltinType::Double: return 'd';
Daniel Dunbar3a0be842010-10-11 21:13:48 +00003817 case BuiltinType::LongDouble: return 'D';
David Chisnall64fd7e82010-06-04 01:10:52 +00003818 }
3819}
3820
Mike Stump1eb44332009-09-09 15:08:12 +00003821static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnall64fd7e82010-06-04 01:10:52 +00003822 QualType T, const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003823 const Expr *E = FD->getBitWidth();
3824 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3825 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003826 S += 'b';
David Chisnall64fd7e82010-06-04 01:10:52 +00003827 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3828 // The GNU runtime requires more information; bitfields are encoded as b,
3829 // then the offset (in bits) of the first element, then the type of the
3830 // bitfield, then the size in bits. For example, in this structure:
3831 //
3832 // struct
3833 // {
3834 // int integer;
3835 // int flags:2;
3836 // };
3837 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3838 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3839 // information is not especially sensible, but we're stuck with it for
3840 // compatibility with GCC, although providing it breaks anything that
3841 // actually uses runtime introspection and wants to work on both runtimes...
3842 if (!Ctx->getLangOptions().NeXTRuntime) {
3843 const RecordDecl *RD = FD->getParent();
3844 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3845 // FIXME: This same linear search is also used in ExprConstant - it might
3846 // be better if the FieldDecl stored its offset. We'd be increasing the
3847 // size of the object slightly, but saving some time every time it is used.
3848 unsigned i = 0;
3849 for (RecordDecl::field_iterator Field = RD->field_begin(),
3850 FieldEnd = RD->field_end();
3851 Field != FieldEnd; (void)++Field, ++i) {
3852 if (*Field == FD)
3853 break;
3854 }
3855 S += llvm::utostr(RL.getFieldOffset(i));
David Chisnallc7ff82c2010-12-26 20:12:30 +00003856 if (T->isEnumeralType())
3857 S += 'i';
3858 else
3859 S += ObjCEncodingForPrimitiveKind(Context, T);
David Chisnall64fd7e82010-06-04 01:10:52 +00003860 }
3861 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003862 S += llvm::utostr(N);
3863}
3864
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003865// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003866void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3867 bool ExpandPointedToStructures,
3868 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003869 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003870 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003871 bool EncodingProperty) {
David Chisnall64fd7e82010-06-04 01:10:52 +00003872 if (T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003873 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00003874 return EncodeBitField(this, S, T, FD);
3875 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003876 return;
3877 }
Mike Stump1eb44332009-09-09 15:08:12 +00003878
John McCall183700f2009-09-21 23:43:11 +00003879 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003880 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003881 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003882 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003883 return;
3884 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00003885
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003886 // encoding for pointer or r3eference types.
3887 QualType PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003888 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003889 if (PT->isObjCSelType()) {
3890 S += ':';
3891 return;
3892 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003893 PointeeTy = PT->getPointeeType();
3894 }
3895 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3896 PointeeTy = RT->getPointeeType();
3897 if (!PointeeTy.isNull()) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003898 bool isReadOnly = false;
3899 // For historical/compatibility reasons, the read-only qualifier of the
3900 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3901 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003902 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003903 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003904 if (OutermostType && T.isConstQualified()) {
3905 isReadOnly = true;
3906 S += 'r';
3907 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003908 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003909 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003910 while (P->getAs<PointerType>())
3911 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003912 if (P.isConstQualified()) {
3913 isReadOnly = true;
3914 S += 'r';
3915 }
3916 }
3917 if (isReadOnly) {
3918 // Another legacy compatibility encoding. Some ObjC qualifier and type
3919 // combinations need to be rearranged.
3920 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer02379412010-04-27 17:12:11 +00003921 if (llvm::StringRef(S).endswith("nr"))
3922 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003923 }
Mike Stump1eb44332009-09-09 15:08:12 +00003924
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003925 if (PointeeTy->isCharType()) {
3926 // char pointer types should be encoded as '*' unless it is a
3927 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003928 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003929 S += '*';
3930 return;
3931 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003932 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003933 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3934 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3935 S += '#';
3936 return;
3937 }
3938 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3939 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3940 S += '@';
3941 return;
3942 }
3943 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003944 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003945 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003946 getLegacyIntegralTypeEncoding(PointeeTy);
3947
Mike Stump1eb44332009-09-09 15:08:12 +00003948 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003949 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003950 return;
3951 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003952
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003953 if (const ArrayType *AT =
3954 // Ignore type qualifiers etc.
3955 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003956 if (isa<IncompleteArrayType>(AT)) {
3957 // Incomplete arrays are encoded as a pointer to the array element.
3958 S += '^';
3959
Mike Stump1eb44332009-09-09 15:08:12 +00003960 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003961 false, ExpandStructures, FD);
3962 } else {
3963 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003964
Anders Carlsson559a8332009-02-22 01:38:57 +00003965 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3966 S += llvm::utostr(CAT->getSize().getZExtValue());
3967 else {
3968 //Variable length arrays are encoded as a regular array with 0 elements.
3969 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3970 S += '0';
3971 }
Mike Stump1eb44332009-09-09 15:08:12 +00003972
3973 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003974 false, ExpandStructures, FD);
3975 S += ']';
3976 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003977 return;
3978 }
Mike Stump1eb44332009-09-09 15:08:12 +00003979
John McCall183700f2009-09-21 23:43:11 +00003980 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003981 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003982 return;
3983 }
Mike Stump1eb44332009-09-09 15:08:12 +00003984
Ted Kremenek6217b802009-07-29 21:53:49 +00003985 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003986 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003987 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003988 // Anonymous structures print as '?'
3989 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3990 S += II->getName();
Fariborz Jahanian6fb94392010-05-07 00:28:49 +00003991 if (ClassTemplateSpecializationDecl *Spec
3992 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3993 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3994 std::string TemplateArgsStr
3995 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00003996 TemplateArgs.data(),
3997 TemplateArgs.size(),
Fariborz Jahanian6fb94392010-05-07 00:28:49 +00003998 (*this).PrintingPolicy);
3999
4000 S += TemplateArgsStr;
4001 }
Daniel Dunbar502a4a12008-10-17 06:22:57 +00004002 } else {
4003 S += '?';
4004 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00004005 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00004006 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004007 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
4008 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00004009 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004010 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004011 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00004012 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004013 S += '"';
4014 }
Mike Stump1eb44332009-09-09 15:08:12 +00004015
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004016 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004017 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004018 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004019 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004020 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00004021 QualType qt = Field->getType();
4022 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00004023 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004024 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004025 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00004026 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00004027 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00004028 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004029 return;
4030 }
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00004031
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004032 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00004033 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00004034 EncodeBitField(this, S, T, FD);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00004035 else
4036 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004037 return;
4038 }
Mike Stump1eb44332009-09-09 15:08:12 +00004039
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004040 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00004041 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004042 return;
4043 }
Mike Stump1eb44332009-09-09 15:08:12 +00004044
John McCallc12c5bb2010-05-15 11:32:37 +00004045 // Ignore protocol qualifiers when mangling at this level.
4046 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4047 T = OT->getBaseType();
4048
John McCall0953e762009-09-24 19:53:00 +00004049 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004050 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00004051 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004052 S += '{';
4053 const IdentifierInfo *II = OI->getIdentifier();
4054 S += II->getName();
4055 S += '=';
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00004056 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
4057 DeepCollectObjCIvars(OI, true, Ivars);
4058 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4059 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
4060 if (Field->isBitField())
4061 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004062 else
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00004063 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004064 }
4065 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004066 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00004067 }
Mike Stump1eb44332009-09-09 15:08:12 +00004068
John McCall183700f2009-09-21 23:43:11 +00004069 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00004070 if (OPT->isObjCIdType()) {
4071 S += '@';
4072 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004073 }
Mike Stump1eb44332009-09-09 15:08:12 +00004074
Steve Naroff27d20a22009-10-28 22:03:49 +00004075 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
4076 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
4077 // Since this is a binary compatibility issue, need to consult with runtime
4078 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00004079 S += '#';
4080 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004081 }
Mike Stump1eb44332009-09-09 15:08:12 +00004082
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004083 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004084 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00004085 ExpandPointedToStructures,
4086 ExpandStructures, FD);
4087 if (FD || EncodingProperty) {
4088 // Note that we do extended encoding of protocol qualifer list
4089 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00004090 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00004091 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4092 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00004093 S += '<';
4094 S += (*I)->getNameAsString();
4095 S += '>';
4096 }
4097 S += '"';
4098 }
4099 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004100 }
Mike Stump1eb44332009-09-09 15:08:12 +00004101
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004102 QualType PointeeTy = OPT->getPointeeType();
4103 if (!EncodingProperty &&
4104 isa<TypedefType>(PointeeTy.getTypePtr())) {
4105 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00004106 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004107 // {...};
4108 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00004109 getObjCEncodingForTypeImpl(PointeeTy, S,
4110 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004111 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00004112 return;
4113 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004114
4115 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00004116 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004117 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00004118 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00004119 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4120 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004121 S += '<';
4122 S += (*I)->getNameAsString();
4123 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00004124 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004125 S += '"';
4126 }
4127 return;
4128 }
Mike Stump1eb44332009-09-09 15:08:12 +00004129
John McCall532ec7b2010-05-17 23:56:34 +00004130 // gcc just blithely ignores member pointers.
4131 // TODO: maybe there should be a mangling for these
4132 if (T->getAs<MemberPointerType>())
4133 return;
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00004134
4135 if (T->isVectorType()) {
4136 // This matches gcc's encoding, even though technically it is
4137 // insufficient.
4138 // FIXME. We should do a better job than gcc.
4139 return;
4140 }
4141
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004142 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004143}
4144
Mike Stump1eb44332009-09-09 15:08:12 +00004145void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00004146 std::string& S) const {
4147 if (QT & Decl::OBJC_TQ_In)
4148 S += 'n';
4149 if (QT & Decl::OBJC_TQ_Inout)
4150 S += 'N';
4151 if (QT & Decl::OBJC_TQ_Out)
4152 S += 'o';
4153 if (QT & Decl::OBJC_TQ_Bycopy)
4154 S += 'O';
4155 if (QT & Decl::OBJC_TQ_Byref)
4156 S += 'R';
4157 if (QT & Decl::OBJC_TQ_Oneway)
4158 S += 'V';
4159}
4160
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004161void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004162 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004163
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004164 BuiltinVaListType = T;
4165}
4166
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004167void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004168 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00004169}
4170
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004171void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00004172 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00004173}
4174
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004175void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004176 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00004177}
4178
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004179void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004180 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00004181}
4182
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004183void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00004184 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00004185 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004186
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004187 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00004188}
4189
John McCall0bd6feb2009-12-02 08:04:21 +00004190/// \brief Retrieve the template name that corresponds to a non-empty
4191/// lookup.
John McCalleec51cf2010-01-20 00:46:10 +00004192TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4193 UnresolvedSetIterator End) {
John McCall0bd6feb2009-12-02 08:04:21 +00004194 unsigned size = End - Begin;
4195 assert(size > 1 && "set is not overloaded!");
4196
4197 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4198 size * sizeof(FunctionTemplateDecl*));
4199 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4200
4201 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00004202 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00004203 NamedDecl *D = *I;
4204 assert(isa<FunctionTemplateDecl>(D) ||
4205 (isa<UsingShadowDecl>(D) &&
4206 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4207 *Storage++ = D;
4208 }
4209
4210 return TemplateName(OT);
4211}
4212
Douglas Gregor7532dc62009-03-30 22:58:21 +00004213/// \brief Retrieve the template name that represents a qualified
4214/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00004215TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004216 bool TemplateKeyword,
4217 TemplateDecl *Template) {
Douglas Gregor789b1f62010-02-04 18:10:26 +00004218 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00004219 llvm::FoldingSetNodeID ID;
4220 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4221
4222 void *InsertPos = 0;
4223 QualifiedTemplateName *QTN =
4224 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4225 if (!QTN) {
4226 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4227 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4228 }
4229
4230 return TemplateName(QTN);
4231}
4232
4233/// \brief Retrieve the template name that represents a dependent
4234/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00004235TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004236 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004237 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00004238 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00004239
4240 llvm::FoldingSetNodeID ID;
4241 DependentTemplateName::Profile(ID, NNS, Name);
4242
4243 void *InsertPos = 0;
4244 DependentTemplateName *QTN =
4245 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4246
4247 if (QTN)
4248 return TemplateName(QTN);
4249
4250 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4251 if (CanonNNS == NNS) {
4252 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4253 } else {
4254 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4255 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004256 DependentTemplateName *CheckQTN =
4257 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4258 assert(!CheckQTN && "Dependent type name canonicalization broken");
4259 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00004260 }
4261
4262 DependentTemplateNames.InsertNode(QTN, InsertPos);
4263 return TemplateName(QTN);
4264}
4265
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004266/// \brief Retrieve the template name that represents a dependent
4267/// template name such as \c MetaFun::template operator+.
4268TemplateName
4269ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4270 OverloadedOperatorKind Operator) {
4271 assert((!NNS || NNS->isDependent()) &&
4272 "Nested name specifier must be dependent");
4273
4274 llvm::FoldingSetNodeID ID;
4275 DependentTemplateName::Profile(ID, NNS, Operator);
4276
4277 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00004278 DependentTemplateName *QTN
4279 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004280
4281 if (QTN)
4282 return TemplateName(QTN);
4283
4284 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4285 if (CanonNNS == NNS) {
4286 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4287 } else {
4288 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4289 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004290
4291 DependentTemplateName *CheckQTN
4292 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4293 assert(!CheckQTN && "Dependent template name canonicalization broken");
4294 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004295 }
4296
4297 DependentTemplateNames.InsertNode(QTN, InsertPos);
4298 return TemplateName(QTN);
4299}
4300
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004301/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00004302/// TargetInfo, produce the corresponding type. The unsigned @p Type
4303/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00004304CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004305 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00004306 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004307 case TargetInfo::SignedShort: return ShortTy;
4308 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4309 case TargetInfo::SignedInt: return IntTy;
4310 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4311 case TargetInfo::SignedLong: return LongTy;
4312 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4313 case TargetInfo::SignedLongLong: return LongLongTy;
4314 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4315 }
4316
4317 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00004318 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004319}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00004320
4321//===----------------------------------------------------------------------===//
4322// Type Predicates.
4323//===----------------------------------------------------------------------===//
4324
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004325/// isObjCNSObjectType - Return true if this is an NSObject object using
4326/// NSObject attribute on a c-style pointer type.
4327/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00004328/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004329///
4330bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4331 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4332 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00004333 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004334 return true;
4335 }
Mike Stump1eb44332009-09-09 15:08:12 +00004336 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004337}
4338
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004339/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4340/// garbage collection attribute.
4341///
John McCall0953e762009-09-24 19:53:00 +00004342Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4343 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004344 if (getLangOptions().ObjC1 &&
4345 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00004346 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004347 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00004348 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004349 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00004350 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00004351 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004352 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004353 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00004354 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004355 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00004356 // Non-pointers have none gc'able attribute regardless of the attribute
4357 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00004358 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004359 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004360 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00004361 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004362}
4363
Chris Lattner6ac46a42008-04-07 06:51:04 +00004364//===----------------------------------------------------------------------===//
4365// Type Compatibility Testing
4366//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00004367
Mike Stump1eb44332009-09-09 15:08:12 +00004368/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004369/// compatible.
4370static bool areCompatVectorTypes(const VectorType *LHS,
4371 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00004372 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00004373 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00004374 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00004375}
4376
Douglas Gregor255210e2010-08-06 10:14:59 +00004377bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4378 QualType SecondVec) {
4379 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4380 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4381
4382 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4383 return true;
4384
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004385 // Treat Neon vector types and most AltiVec vector types as if they are the
4386 // equivalent GCC vector types.
Douglas Gregor255210e2010-08-06 10:14:59 +00004387 const VectorType *First = FirstVec->getAs<VectorType>();
4388 const VectorType *Second = SecondVec->getAs<VectorType>();
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004389 if (First->getNumElements() == Second->getNumElements() &&
Douglas Gregor255210e2010-08-06 10:14:59 +00004390 hasSameType(First->getElementType(), Second->getElementType()) &&
Bob Wilsonf69eb7c2010-11-12 17:24:54 +00004391 First->getVectorKind() != VectorType::AltiVecPixel &&
4392 First->getVectorKind() != VectorType::AltiVecBool &&
4393 Second->getVectorKind() != VectorType::AltiVecPixel &&
4394 Second->getVectorKind() != VectorType::AltiVecBool)
Douglas Gregor255210e2010-08-06 10:14:59 +00004395 return true;
4396
4397 return false;
4398}
4399
Steve Naroff4084c302009-07-23 01:01:38 +00004400//===----------------------------------------------------------------------===//
4401// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4402//===----------------------------------------------------------------------===//
4403
4404/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4405/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004406bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4407 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00004408 if (lProto == rProto)
4409 return true;
4410 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4411 E = rProto->protocol_end(); PI != E; ++PI)
4412 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4413 return true;
4414 return false;
4415}
4416
Steve Naroff4084c302009-07-23 01:01:38 +00004417/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4418/// return true if lhs's protocols conform to rhs's protocol; false
4419/// otherwise.
4420bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4421 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4422 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4423 return false;
4424}
4425
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004426/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4427/// Class<p1, ...>.
4428bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4429 QualType rhs) {
4430 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4431 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4432 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4433
4434 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4435 E = lhsQID->qual_end(); I != E; ++I) {
4436 bool match = false;
4437 ObjCProtocolDecl *lhsProto = *I;
4438 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4439 E = rhsOPT->qual_end(); J != E; ++J) {
4440 ObjCProtocolDecl *rhsProto = *J;
4441 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4442 match = true;
4443 break;
4444 }
4445 }
4446 if (!match)
4447 return false;
4448 }
4449 return true;
4450}
4451
Steve Naroff4084c302009-07-23 01:01:38 +00004452/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4453/// ObjCQualifiedIDType.
4454bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4455 bool compare) {
4456 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00004457 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004458 lhs->isObjCIdType() || lhs->isObjCClassType())
4459 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004460 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004461 rhs->isObjCIdType() || rhs->isObjCClassType())
4462 return true;
4463
4464 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00004465 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004466
Steve Naroff4084c302009-07-23 01:01:38 +00004467 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004468
Steve Naroff4084c302009-07-23 01:01:38 +00004469 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004470 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00004471 // make sure we check the class hierarchy.
4472 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4473 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4474 E = lhsQID->qual_end(); I != E; ++I) {
4475 // when comparing an id<P> on lhs with a static type on rhs,
4476 // see if static class implements all of id's protocols, directly or
4477 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004478 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00004479 return false;
4480 }
4481 }
4482 // If there are no qualifiers and no interface, we have an 'id'.
4483 return true;
4484 }
Mike Stump1eb44332009-09-09 15:08:12 +00004485 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004486 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4487 E = lhsQID->qual_end(); I != E; ++I) {
4488 ObjCProtocolDecl *lhsProto = *I;
4489 bool match = false;
4490
4491 // when comparing an id<P> on lhs with a static type on rhs,
4492 // see if static class implements all of id's protocols, directly or
4493 // through its super class and categories.
4494 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4495 E = rhsOPT->qual_end(); J != E; ++J) {
4496 ObjCProtocolDecl *rhsProto = *J;
4497 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4498 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4499 match = true;
4500 break;
4501 }
4502 }
Mike Stump1eb44332009-09-09 15:08:12 +00004503 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004504 // make sure we check the class hierarchy.
4505 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4506 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4507 E = lhsQID->qual_end(); I != E; ++I) {
4508 // when comparing an id<P> on lhs with a static type on rhs,
4509 // see if static class implements all of id's protocols, directly or
4510 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004511 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004512 match = true;
4513 break;
4514 }
4515 }
4516 }
4517 if (!match)
4518 return false;
4519 }
Mike Stump1eb44332009-09-09 15:08:12 +00004520
Steve Naroff4084c302009-07-23 01:01:38 +00004521 return true;
4522 }
Mike Stump1eb44332009-09-09 15:08:12 +00004523
Steve Naroff4084c302009-07-23 01:01:38 +00004524 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4525 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4526
Mike Stump1eb44332009-09-09 15:08:12 +00004527 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004528 lhs->getAsObjCInterfacePointerType()) {
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004529 // If both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004530 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4531 E = lhsOPT->qual_end(); I != E; ++I) {
4532 ObjCProtocolDecl *lhsProto = *I;
4533 bool match = false;
4534
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004535 // when comparing an id<P> on rhs with a static type on lhs,
Steve Naroff4084c302009-07-23 01:01:38 +00004536 // see if static class implements all of id's protocols, directly or
4537 // through its super class and categories.
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004538 // First, lhs protocols in the qualifier list must be found, direct
4539 // or indirect in rhs's qualifier list or it is a mismatch.
Steve Naroff4084c302009-07-23 01:01:38 +00004540 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4541 E = rhsQID->qual_end(); J != E; ++J) {
4542 ObjCProtocolDecl *rhsProto = *J;
4543 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4544 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4545 match = true;
4546 break;
4547 }
4548 }
4549 if (!match)
4550 return false;
4551 }
Fariborz Jahaniande5b17e2010-11-01 20:47:16 +00004552
4553 // Static class's protocols, or its super class or category protocols
4554 // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
4555 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4556 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4557 CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
4558 // This is rather dubious but matches gcc's behavior. If lhs has
4559 // no type qualifier and its class has no static protocol(s)
4560 // assume that it is mismatch.
4561 if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
4562 return false;
4563 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4564 LHSInheritedProtocols.begin(),
4565 E = LHSInheritedProtocols.end(); I != E; ++I) {
4566 bool match = false;
4567 ObjCProtocolDecl *lhsProto = (*I);
4568 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4569 E = rhsQID->qual_end(); J != E; ++J) {
4570 ObjCProtocolDecl *rhsProto = *J;
4571 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4572 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4573 match = true;
4574 break;
4575 }
4576 }
4577 if (!match)
4578 return false;
4579 }
4580 }
Steve Naroff4084c302009-07-23 01:01:38 +00004581 return true;
4582 }
4583 return false;
4584}
4585
Eli Friedman3d815e72008-08-22 00:56:42 +00004586/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004587/// compatible for assignment from RHS to LHS. This handles validation of any
4588/// protocol qualifiers on the LHS or RHS.
4589///
Steve Naroff14108da2009-07-10 23:34:53 +00004590bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4591 const ObjCObjectPointerType *RHSOPT) {
John McCallc12c5bb2010-05-15 11:32:37 +00004592 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4593 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4594
Steve Naroffde2e22d2009-07-15 18:40:39 +00004595 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCallc12c5bb2010-05-15 11:32:37 +00004596 if (LHS->isObjCUnqualifiedIdOrClass() ||
4597 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff14108da2009-07-10 23:34:53 +00004598 return true;
4599
John McCallc12c5bb2010-05-15 11:32:37 +00004600 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump1eb44332009-09-09 15:08:12 +00004601 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4602 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004603 false);
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004604
4605 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4606 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4607 QualType(RHSOPT,0));
4608
John McCallc12c5bb2010-05-15 11:32:37 +00004609 // If we have 2 user-defined types, fall into that path.
4610 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff4084c302009-07-23 01:01:38 +00004611 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004612
Steve Naroff4084c302009-07-23 01:01:38 +00004613 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004614}
4615
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004616/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4617/// for providing type-safty for objective-c pointers used to pass/return
4618/// arguments in block literals. When passed as arguments, passing 'A*' where
4619/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4620/// not OK. For the return type, the opposite is not OK.
4621bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4622 const ObjCObjectPointerType *LHSOPT,
4623 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004624 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004625 return true;
4626
4627 if (LHSOPT->isObjCBuiltinType()) {
4628 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4629 }
4630
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004631 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004632 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4633 QualType(RHSOPT,0),
4634 false);
4635
4636 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4637 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4638 if (LHS && RHS) { // We have 2 user-defined types.
4639 if (LHS != RHS) {
4640 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4641 return false;
4642 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4643 return true;
4644 }
4645 else
4646 return true;
4647 }
4648 return false;
4649}
4650
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004651/// getIntersectionOfProtocols - This routine finds the intersection of set
4652/// of protocols inherited from two distinct objective-c pointer objects.
4653/// It is used to build composite qualifier list of the composite type of
4654/// the conditional expression involving two objective-c pointer objects.
4655static
4656void getIntersectionOfProtocols(ASTContext &Context,
4657 const ObjCObjectPointerType *LHSOPT,
4658 const ObjCObjectPointerType *RHSOPT,
4659 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4660
John McCallc12c5bb2010-05-15 11:32:37 +00004661 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4662 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4663 assert(LHS->getInterface() && "LHS must have an interface base");
4664 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004665
4666 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4667 unsigned LHSNumProtocols = LHS->getNumProtocols();
4668 if (LHSNumProtocols > 0)
4669 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4670 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004671 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004672 Context.CollectInheritedProtocols(LHS->getInterface(),
4673 LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004674 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4675 LHSInheritedProtocols.end());
4676 }
4677
4678 unsigned RHSNumProtocols = RHS->getNumProtocols();
4679 if (RHSNumProtocols > 0) {
Dan Gohmancb421fa2010-04-19 16:39:44 +00004680 ObjCProtocolDecl **RHSProtocols =
4681 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004682 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4683 if (InheritedProtocolSet.count(RHSProtocols[i]))
4684 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4685 }
4686 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004687 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004688 Context.CollectInheritedProtocols(RHS->getInterface(),
4689 RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004690 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4691 RHSInheritedProtocols.begin(),
4692 E = RHSInheritedProtocols.end(); I != E; ++I)
4693 if (InheritedProtocolSet.count((*I)))
4694 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004695 }
4696}
4697
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004698/// areCommonBaseCompatible - Returns common base class of the two classes if
4699/// one found. Note that this is O'2 algorithm. But it will be called as the
4700/// last type comparison in a ?-exp of ObjC pointer types before a
4701/// warning is issued. So, its invokation is extremely rare.
4702QualType ASTContext::areCommonBaseCompatible(
John McCallc12c5bb2010-05-15 11:32:37 +00004703 const ObjCObjectPointerType *Lptr,
4704 const ObjCObjectPointerType *Rptr) {
4705 const ObjCObjectType *LHS = Lptr->getObjectType();
4706 const ObjCObjectType *RHS = Rptr->getObjectType();
4707 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4708 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4709 if (!LDecl || !RDecl)
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004710 return QualType();
4711
John McCallc12c5bb2010-05-15 11:32:37 +00004712 while ((LDecl = LDecl->getSuperClass())) {
4713 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004714 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCallc12c5bb2010-05-15 11:32:37 +00004715 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4716 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4717
4718 QualType Result = QualType(LHS, 0);
4719 if (!Protocols.empty())
4720 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4721 Result = getObjCObjectPointerType(Result);
4722 return Result;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004723 }
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004724 }
4725
4726 return QualType();
4727}
4728
John McCallc12c5bb2010-05-15 11:32:37 +00004729bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4730 const ObjCObjectType *RHS) {
4731 assert(LHS->getInterface() && "LHS is not an interface type");
4732 assert(RHS->getInterface() && "RHS is not an interface type");
4733
Chris Lattner6ac46a42008-04-07 06:51:04 +00004734 // Verify that the base decls are compatible: the RHS must be a subclass of
4735 // the LHS.
John McCallc12c5bb2010-05-15 11:32:37 +00004736 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner6ac46a42008-04-07 06:51:04 +00004737 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004738
Chris Lattner6ac46a42008-04-07 06:51:04 +00004739 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4740 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004741 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004742 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004743
Chris Lattner6ac46a42008-04-07 06:51:04 +00004744 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4745 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004746 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004747 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00004748
John McCallc12c5bb2010-05-15 11:32:37 +00004749 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4750 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004751 LHSPI != LHSPE; LHSPI++) {
4752 bool RHSImplementsProtocol = false;
4753
4754 // If the RHS doesn't implement the protocol on the left, the types
4755 // are incompatible.
John McCallc12c5bb2010-05-15 11:32:37 +00004756 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4757 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00004758 RHSPI != RHSPE; RHSPI++) {
4759 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004760 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00004761 break;
4762 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004763 }
4764 // FIXME: For better diagnostics, consider passing back the protocol name.
4765 if (!RHSImplementsProtocol)
4766 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004767 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004768 // The RHS implements all protocols listed on the LHS.
4769 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004770}
4771
Steve Naroff389bf462009-02-12 17:52:19 +00004772bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4773 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00004774 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4775 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004776
Steve Naroff14108da2009-07-10 23:34:53 +00004777 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00004778 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004779
4780 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4781 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00004782}
4783
Douglas Gregor569c3162010-08-07 11:51:51 +00004784bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
4785 return canAssignObjCInterfaces(
4786 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
4787 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
4788}
4789
Mike Stump1eb44332009-09-09 15:08:12 +00004790/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00004791/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00004792/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00004793/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor447234d2010-07-29 15:18:02 +00004794bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
4795 bool CompareUnqualified) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004796 if (getLangOptions().CPlusPlus)
4797 return hasSameType(LHS, RHS);
4798
Douglas Gregor447234d2010-07-29 15:18:02 +00004799 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman3d815e72008-08-22 00:56:42 +00004800}
4801
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004802bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4803 return !mergeTypes(LHS, RHS, true).isNull();
4804}
4805
Peter Collingbourne48466752010-10-24 18:30:18 +00004806/// mergeTransparentUnionType - if T is a transparent union type and a member
4807/// of T is compatible with SubType, return the merged type, else return
4808/// QualType()
4809QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
4810 bool OfBlockPointer,
4811 bool Unqualified) {
4812 if (const RecordType *UT = T->getAsUnionType()) {
4813 RecordDecl *UD = UT->getDecl();
4814 if (UD->hasAttr<TransparentUnionAttr>()) {
4815 for (RecordDecl::field_iterator it = UD->field_begin(),
4816 itend = UD->field_end(); it != itend; ++it) {
Peter Collingbournef91d7572010-12-02 21:00:06 +00004817 QualType ET = it->getType().getUnqualifiedType();
Peter Collingbourne48466752010-10-24 18:30:18 +00004818 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
4819 if (!MT.isNull())
4820 return MT;
4821 }
4822 }
4823 }
4824
4825 return QualType();
4826}
4827
4828/// mergeFunctionArgumentTypes - merge two types which appear as function
4829/// argument types
4830QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
4831 bool OfBlockPointer,
4832 bool Unqualified) {
4833 // GNU extension: two types are compatible if they appear as a function
4834 // argument, one of the types is a transparent union type and the other
4835 // type is compatible with a union member
4836 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
4837 Unqualified);
4838 if (!lmerge.isNull())
4839 return lmerge;
4840
4841 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
4842 Unqualified);
4843 if (!rmerge.isNull())
4844 return rmerge;
4845
4846 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
4847}
4848
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004849QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor447234d2010-07-29 15:18:02 +00004850 bool OfBlockPointer,
4851 bool Unqualified) {
John McCall183700f2009-09-21 23:43:11 +00004852 const FunctionType *lbase = lhs->getAs<FunctionType>();
4853 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00004854 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4855 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00004856 bool allLTypes = true;
4857 bool allRTypes = true;
4858
4859 // Check return type
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004860 QualType retType;
4861 if (OfBlockPointer)
Douglas Gregor447234d2010-07-29 15:18:02 +00004862 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true,
4863 Unqualified);
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004864 else
John McCall8cc246c2010-12-15 01:06:38 +00004865 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
4866 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004867 if (retType.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004868
4869 if (Unqualified)
4870 retType = retType.getUnqualifiedType();
4871
4872 CanQualType LRetType = getCanonicalType(lbase->getResultType());
4873 CanQualType RRetType = getCanonicalType(rbase->getResultType());
4874 if (Unqualified) {
4875 LRetType = LRetType.getUnqualifiedType();
4876 RRetType = RRetType.getUnqualifiedType();
4877 }
4878
4879 if (getCanonicalType(retType) != LRetType)
Chris Lattner61710852008-10-05 17:34:18 +00004880 allLTypes = false;
Douglas Gregor447234d2010-07-29 15:18:02 +00004881 if (getCanonicalType(retType) != RRetType)
Chris Lattner61710852008-10-05 17:34:18 +00004882 allRTypes = false;
John McCall8cc246c2010-12-15 01:06:38 +00004883
Daniel Dunbar6a15c852010-04-28 16:20:58 +00004884 // FIXME: double check this
4885 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4886 // rbase->getRegParmAttr() != 0 &&
4887 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindola264ba482010-03-30 20:24:48 +00004888 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4889 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
John McCall8cc246c2010-12-15 01:06:38 +00004890
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004891 // Compatible functions must have compatible calling conventions
John McCall8cc246c2010-12-15 01:06:38 +00004892 if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004893 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004894
John McCall8cc246c2010-12-15 01:06:38 +00004895 // Regparm is part of the calling convention.
4896 if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
4897 return QualType();
4898
4899 // It's noreturn if either type is.
4900 // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
4901 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4902 if (NoReturn != lbaseInfo.getNoReturn())
4903 allLTypes = false;
4904 if (NoReturn != rbaseInfo.getNoReturn())
4905 allRTypes = false;
4906
4907 FunctionType::ExtInfo einfo(NoReturn,
4908 lbaseInfo.getRegParm(),
4909 lbaseInfo.getCC());
John McCalle23cf432010-12-14 08:05:40 +00004910
Eli Friedman3d815e72008-08-22 00:56:42 +00004911 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00004912 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4913 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004914 unsigned lproto_nargs = lproto->getNumArgs();
4915 unsigned rproto_nargs = rproto->getNumArgs();
4916
4917 // Compatible functions must have the same number of arguments
4918 if (lproto_nargs != rproto_nargs)
4919 return QualType();
4920
4921 // Variadic and non-variadic functions aren't compatible
4922 if (lproto->isVariadic() != rproto->isVariadic())
4923 return QualType();
4924
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00004925 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4926 return QualType();
4927
Eli Friedman3d815e72008-08-22 00:56:42 +00004928 // Check argument compatibility
4929 llvm::SmallVector<QualType, 10> types;
4930 for (unsigned i = 0; i < lproto_nargs; i++) {
4931 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4932 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbourne48466752010-10-24 18:30:18 +00004933 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
4934 OfBlockPointer,
4935 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004936 if (argtype.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004937
4938 if (Unqualified)
4939 argtype = argtype.getUnqualifiedType();
4940
Eli Friedman3d815e72008-08-22 00:56:42 +00004941 types.push_back(argtype);
Douglas Gregor447234d2010-07-29 15:18:02 +00004942 if (Unqualified) {
4943 largtype = largtype.getUnqualifiedType();
4944 rargtype = rargtype.getUnqualifiedType();
4945 }
4946
Chris Lattner61710852008-10-05 17:34:18 +00004947 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4948 allLTypes = false;
4949 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4950 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00004951 }
4952 if (allLTypes) return lhs;
4953 if (allRTypes) return rhs;
John McCalle23cf432010-12-14 08:05:40 +00004954
4955 FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
4956 EPI.ExtInfo = einfo;
4957 return getFunctionType(retType, types.begin(), types.size(), EPI);
Eli Friedman3d815e72008-08-22 00:56:42 +00004958 }
4959
4960 if (lproto) allRTypes = false;
4961 if (rproto) allLTypes = false;
4962
Douglas Gregor72564e72009-02-26 23:50:07 +00004963 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00004964 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00004965 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004966 if (proto->isVariadic()) return QualType();
4967 // Check that the types are compatible with the types that
4968 // would result from default argument promotions (C99 6.7.5.3p15).
4969 // The only types actually affected are promotable integer
4970 // types and floats, which would be passed as a different
4971 // type depending on whether the prototype is visible.
4972 unsigned proto_nargs = proto->getNumArgs();
4973 for (unsigned i = 0; i < proto_nargs; ++i) {
4974 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00004975
4976 // Look at the promotion type of enum types, since that is the type used
4977 // to pass enum values.
4978 if (const EnumType *Enum = argTy->getAs<EnumType>())
4979 argTy = Enum->getDecl()->getPromotionType();
4980
Eli Friedman3d815e72008-08-22 00:56:42 +00004981 if (argTy->isPromotableIntegerType() ||
4982 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4983 return QualType();
4984 }
4985
4986 if (allLTypes) return lhs;
4987 if (allRTypes) return rhs;
John McCalle23cf432010-12-14 08:05:40 +00004988
4989 FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
4990 EPI.ExtInfo = einfo;
Eli Friedman3d815e72008-08-22 00:56:42 +00004991 return getFunctionType(retType, proto->arg_type_begin(),
John McCalle23cf432010-12-14 08:05:40 +00004992 proto->getNumArgs(), EPI);
Eli Friedman3d815e72008-08-22 00:56:42 +00004993 }
4994
4995 if (allLTypes) return lhs;
4996 if (allRTypes) return rhs;
John McCall8cc246c2010-12-15 01:06:38 +00004997 return getFunctionNoProtoType(retType, einfo);
Eli Friedman3d815e72008-08-22 00:56:42 +00004998}
4999
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005000QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor447234d2010-07-29 15:18:02 +00005001 bool OfBlockPointer,
5002 bool Unqualified) {
Bill Wendling43d69752007-12-03 07:33:35 +00005003 // C++ [expr]: If an expression initially has the type "reference to T", the
5004 // type is adjusted to "T" prior to any further analysis, the expression
5005 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00005006 // expression is an lvalue unless the reference is an rvalue reference and
5007 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00005008 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
5009 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor447234d2010-07-29 15:18:02 +00005010
5011 if (Unqualified) {
5012 LHS = LHS.getUnqualifiedType();
5013 RHS = RHS.getUnqualifiedType();
5014 }
Douglas Gregor0e709ab2010-02-03 21:02:30 +00005015
Eli Friedman3d815e72008-08-22 00:56:42 +00005016 QualType LHSCan = getCanonicalType(LHS),
5017 RHSCan = getCanonicalType(RHS);
5018
5019 // If two types are identical, they are compatible.
5020 if (LHSCan == RHSCan)
5021 return LHS;
5022
John McCall0953e762009-09-24 19:53:00 +00005023 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00005024 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5025 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00005026 if (LQuals != RQuals) {
5027 // If any of these qualifiers are different, we have a type
5028 // mismatch.
5029 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5030 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5031 return QualType();
5032
5033 // Exactly one GC qualifier difference is allowed: __strong is
5034 // okay if the other type has no GC qualifier but is an Objective
5035 // C object pointer (i.e. implicitly strong by default). We fix
5036 // this by pretending that the unqualified type was actually
5037 // qualified __strong.
5038 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5039 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5040 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5041
5042 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5043 return QualType();
5044
5045 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
5046 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
5047 }
5048 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
5049 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
5050 }
Eli Friedman3d815e72008-08-22 00:56:42 +00005051 return QualType();
John McCall0953e762009-09-24 19:53:00 +00005052 }
5053
5054 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00005055
Eli Friedman852d63b2009-06-01 01:22:52 +00005056 Type::TypeClass LHSClass = LHSCan->getTypeClass();
5057 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00005058
Chris Lattner1adb8832008-01-14 05:45:46 +00005059 // We want to consider the two function types to be the same for these
5060 // comparisons, just force one to the other.
5061 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
5062 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00005063
5064 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00005065 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5066 LHSClass = Type::ConstantArray;
5067 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5068 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00005069
John McCallc12c5bb2010-05-15 11:32:37 +00005070 // ObjCInterfaces are just specialized ObjCObjects.
5071 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5072 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5073
Nate Begeman213541a2008-04-18 23:10:10 +00005074 // Canonicalize ExtVector -> Vector.
5075 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5076 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00005077
Chris Lattnera36a61f2008-04-07 05:43:21 +00005078 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00005079 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00005080 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00005081 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00005082 // Compatibility is based on the underlying type, not the promotion
5083 // type.
John McCall183700f2009-09-21 23:43:11 +00005084 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00005085 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
5086 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00005087 }
John McCall183700f2009-09-21 23:43:11 +00005088 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00005089 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
5090 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00005091 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005092
Eli Friedman3d815e72008-08-22 00:56:42 +00005093 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00005094 }
Eli Friedman3d815e72008-08-22 00:56:42 +00005095
Steve Naroff4a746782008-01-09 22:43:08 +00005096 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00005097 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00005098#define TYPE(Class, Base)
5099#define ABSTRACT_TYPE(Class, Base)
John McCallad5e7382010-03-01 23:49:17 +00005100#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +00005101#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5102#define DEPENDENT_TYPE(Class, Base) case Type::Class:
5103#include "clang/AST/TypeNodes.def"
5104 assert(false && "Non-canonical and dependent types shouldn't get here");
5105 return QualType();
5106
Sebastian Redl7c80bd62009-03-16 23:22:08 +00005107 case Type::LValueReference:
5108 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00005109 case Type::MemberPointer:
5110 assert(false && "C++ should never be in mergeTypes");
5111 return QualType();
5112
John McCallc12c5bb2010-05-15 11:32:37 +00005113 case Type::ObjCInterface:
Douglas Gregor72564e72009-02-26 23:50:07 +00005114 case Type::IncompleteArray:
5115 case Type::VariableArray:
5116 case Type::FunctionProto:
5117 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00005118 assert(false && "Types are eliminated above");
5119 return QualType();
5120
Chris Lattner1adb8832008-01-14 05:45:46 +00005121 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00005122 {
5123 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00005124 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
5125 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005126 if (Unqualified) {
5127 LHSPointee = LHSPointee.getUnqualifiedType();
5128 RHSPointee = RHSPointee.getUnqualifiedType();
5129 }
5130 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5131 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005132 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00005133 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005134 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00005135 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005136 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005137 return getPointerType(ResultType);
5138 }
Steve Naroffc0febd52008-12-10 17:49:55 +00005139 case Type::BlockPointer:
5140 {
5141 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00005142 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5143 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005144 if (Unqualified) {
5145 LHSPointee = LHSPointee.getUnqualifiedType();
5146 RHSPointee = RHSPointee.getUnqualifiedType();
5147 }
5148 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5149 Unqualified);
Steve Naroffc0febd52008-12-10 17:49:55 +00005150 if (ResultType.isNull()) return QualType();
5151 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5152 return LHS;
5153 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5154 return RHS;
5155 return getBlockPointerType(ResultType);
5156 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005157 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00005158 {
5159 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5160 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5161 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5162 return QualType();
5163
5164 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5165 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005166 if (Unqualified) {
5167 LHSElem = LHSElem.getUnqualifiedType();
5168 RHSElem = RHSElem.getUnqualifiedType();
5169 }
5170
5171 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005172 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00005173 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5174 return LHS;
5175 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5176 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00005177 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5178 ArrayType::ArraySizeModifier(), 0);
5179 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5180 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005181 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5182 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00005183 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5184 return LHS;
5185 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5186 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005187 if (LVAT) {
5188 // FIXME: This isn't correct! But tricky to implement because
5189 // the array's size has to be the size of LHS, but the type
5190 // has to be different.
5191 return LHS;
5192 }
5193 if (RVAT) {
5194 // FIXME: This isn't correct! But tricky to implement because
5195 // the array's size has to be the size of RHS, but the type
5196 // has to be different.
5197 return RHS;
5198 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00005199 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5200 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005201 return getIncompleteArrayType(ResultType,
5202 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005203 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005204 case Type::FunctionNoProto:
Douglas Gregor447234d2010-07-29 15:18:02 +00005205 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregor72564e72009-02-26 23:50:07 +00005206 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00005207 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00005208 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00005209 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005210 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00005211 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00005212 case Type::Complex:
5213 // Distinct complex types are incompatible.
5214 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005215 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005216 // FIXME: The merged type should be an ExtVector!
John McCall1c471f32010-03-12 23:14:13 +00005217 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5218 RHSCan->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00005219 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00005220 return QualType();
John McCallc12c5bb2010-05-15 11:32:37 +00005221 case Type::ObjCObject: {
5222 // Check if the types are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005223 // FIXME: This should be type compatibility, e.g. whether
5224 // "LHS x; RHS x;" at global scope is legal.
John McCallc12c5bb2010-05-15 11:32:37 +00005225 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5226 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5227 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff5fd659d2009-02-21 16:18:07 +00005228 return LHS;
5229
Eli Friedman3d815e72008-08-22 00:56:42 +00005230 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00005231 }
Steve Naroff14108da2009-07-10 23:34:53 +00005232 case Type::ObjCObjectPointer: {
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005233 if (OfBlockPointer) {
5234 if (canAssignObjCInterfacesInBlockPointer(
5235 LHS->getAs<ObjCObjectPointerType>(),
5236 RHS->getAs<ObjCObjectPointerType>()))
5237 return LHS;
5238 return QualType();
5239 }
John McCall183700f2009-09-21 23:43:11 +00005240 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5241 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00005242 return LHS;
5243
Steve Naroffbc76dd02008-12-10 22:14:21 +00005244 return QualType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005245 }
Steve Naroffec0550f2007-10-15 20:41:53 +00005246 }
Douglas Gregor72564e72009-02-26 23:50:07 +00005247
5248 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00005249}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00005250
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005251/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5252/// 'RHS' attributes and returns the merged version; including for function
5253/// return types.
5254QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5255 QualType LHSCan = getCanonicalType(LHS),
5256 RHSCan = getCanonicalType(RHS);
5257 // If two types are identical, they are compatible.
5258 if (LHSCan == RHSCan)
5259 return LHS;
5260 if (RHSCan->isFunctionType()) {
5261 if (!LHSCan->isFunctionType())
5262 return QualType();
5263 QualType OldReturnType =
5264 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5265 QualType NewReturnType =
5266 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5267 QualType ResReturnType =
5268 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5269 if (ResReturnType.isNull())
5270 return QualType();
5271 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5272 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5273 // In either case, use OldReturnType to build the new function type.
5274 const FunctionType *F = LHS->getAs<FunctionType>();
5275 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
John McCalle23cf432010-12-14 08:05:40 +00005276 FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5277 EPI.ExtInfo = getFunctionExtInfo(LHS);
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005278 QualType ResultType
5279 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
John McCalle23cf432010-12-14 08:05:40 +00005280 FPT->getNumArgs(), EPI);
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005281 return ResultType;
5282 }
5283 }
5284 return QualType();
5285 }
5286
5287 // If the qualifiers are different, the types can still be merged.
5288 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5289 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5290 if (LQuals != RQuals) {
5291 // If any of these qualifiers are different, we have a type mismatch.
5292 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5293 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5294 return QualType();
5295
5296 // Exactly one GC qualifier difference is allowed: __strong is
5297 // okay if the other type has no GC qualifier but is an Objective
5298 // C object pointer (i.e. implicitly strong by default). We fix
5299 // this by pretending that the unqualified type was actually
5300 // qualified __strong.
5301 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5302 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5303 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5304
5305 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5306 return QualType();
5307
5308 if (GC_L == Qualifiers::Strong)
5309 return LHS;
5310 if (GC_R == Qualifiers::Strong)
5311 return RHS;
5312 return QualType();
5313 }
5314
5315 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5316 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5317 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5318 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5319 if (ResQT == LHSBaseQT)
5320 return LHS;
5321 if (ResQT == RHSBaseQT)
5322 return RHS;
5323 }
5324 return QualType();
5325}
5326
Chris Lattner5426bf62008-04-07 07:01:58 +00005327//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00005328// Integer Predicates
5329//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00005330
Eli Friedmanad74a752008-06-28 06:23:08 +00005331unsigned ASTContext::getIntWidth(QualType T) {
John McCall842aef82009-12-09 09:09:27 +00005332 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00005333 T = ET->getDecl()->getIntegerType();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005334 if (T->isBooleanType())
5335 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00005336 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00005337 return (unsigned)getTypeSize(T);
5338}
5339
5340QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregorf6094622010-07-23 15:58:24 +00005341 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00005342
5343 // Turn <4 x signed int> -> <4 x unsigned int>
5344 if (const VectorType *VTy = T->getAs<VectorType>())
5345 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Bob Wilsone86d78c2010-11-10 21:56:12 +00005346 VTy->getNumElements(), VTy->getVectorKind());
Chris Lattner6a2b9262009-10-17 20:33:28 +00005347
5348 // For enums, we return the unsigned version of the base type.
5349 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00005350 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00005351
5352 const BuiltinType *BTy = T->getAs<BuiltinType>();
5353 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00005354 switch (BTy->getKind()) {
5355 case BuiltinType::Char_S:
5356 case BuiltinType::SChar:
5357 return UnsignedCharTy;
5358 case BuiltinType::Short:
5359 return UnsignedShortTy;
5360 case BuiltinType::Int:
5361 return UnsignedIntTy;
5362 case BuiltinType::Long:
5363 return UnsignedLongTy;
5364 case BuiltinType::LongLong:
5365 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00005366 case BuiltinType::Int128:
5367 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00005368 default:
5369 assert(0 && "Unexpected signed integer type");
5370 return QualType();
5371 }
5372}
5373
Douglas Gregor2cf26342009-04-09 22:27:44 +00005374ExternalASTSource::~ExternalASTSource() { }
5375
5376void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00005377
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005378ASTMutationListener::~ASTMutationListener() { }
5379
Chris Lattner86df27b2009-06-14 00:45:47 +00005380
5381//===----------------------------------------------------------------------===//
5382// Builtin Type Computation
5383//===----------------------------------------------------------------------===//
5384
5385/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattner33daae62010-10-01 22:42:38 +00005386/// pointer over the consumed characters. This returns the resultant type. If
5387/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5388/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5389/// a vector of "i*".
Chris Lattner14e0e742010-10-01 22:53:11 +00005390///
5391/// RequiresICE is filled in on return to indicate whether the value is required
5392/// to be an Integer Constant Expression.
Mike Stump1eb44332009-09-09 15:08:12 +00005393static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00005394 ASTContext::GetBuiltinTypeError &Error,
Chris Lattner14e0e742010-10-01 22:53:11 +00005395 bool &RequiresICE,
Chris Lattner33daae62010-10-01 22:42:38 +00005396 bool AllowTypeModifiers) {
Chris Lattner86df27b2009-06-14 00:45:47 +00005397 // Modifiers.
5398 int HowLong = 0;
5399 bool Signed = false, Unsigned = false;
Chris Lattner14e0e742010-10-01 22:53:11 +00005400 RequiresICE = false;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005401
Chris Lattner33daae62010-10-01 22:42:38 +00005402 // Read the prefixed modifiers first.
Chris Lattner86df27b2009-06-14 00:45:47 +00005403 bool Done = false;
5404 while (!Done) {
5405 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00005406 default: Done = true; --Str; break;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005407 case 'I':
Chris Lattner14e0e742010-10-01 22:53:11 +00005408 RequiresICE = true;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005409 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005410 case 'S':
5411 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5412 assert(!Signed && "Can't use 'S' modifier multiple times!");
5413 Signed = true;
5414 break;
5415 case 'U':
5416 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5417 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5418 Unsigned = true;
5419 break;
5420 case 'L':
5421 assert(HowLong <= 2 && "Can't have LLLL modifier");
5422 ++HowLong;
5423 break;
5424 }
5425 }
5426
5427 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00005428
Chris Lattner86df27b2009-06-14 00:45:47 +00005429 // Read the base type.
5430 switch (*Str++) {
5431 default: assert(0 && "Unknown builtin type letter!");
5432 case 'v':
5433 assert(HowLong == 0 && !Signed && !Unsigned &&
5434 "Bad modifiers used with 'v'!");
5435 Type = Context.VoidTy;
5436 break;
5437 case 'f':
5438 assert(HowLong == 0 && !Signed && !Unsigned &&
5439 "Bad modifiers used with 'f'!");
5440 Type = Context.FloatTy;
5441 break;
5442 case 'd':
5443 assert(HowLong < 2 && !Signed && !Unsigned &&
5444 "Bad modifiers used with 'd'!");
5445 if (HowLong)
5446 Type = Context.LongDoubleTy;
5447 else
5448 Type = Context.DoubleTy;
5449 break;
5450 case 's':
5451 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5452 if (Unsigned)
5453 Type = Context.UnsignedShortTy;
5454 else
5455 Type = Context.ShortTy;
5456 break;
5457 case 'i':
5458 if (HowLong == 3)
5459 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5460 else if (HowLong == 2)
5461 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5462 else if (HowLong == 1)
5463 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5464 else
5465 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5466 break;
5467 case 'c':
5468 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5469 if (Signed)
5470 Type = Context.SignedCharTy;
5471 else if (Unsigned)
5472 Type = Context.UnsignedCharTy;
5473 else
5474 Type = Context.CharTy;
5475 break;
5476 case 'b': // boolean
5477 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5478 Type = Context.BoolTy;
5479 break;
5480 case 'z': // size_t.
5481 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5482 Type = Context.getSizeType();
5483 break;
5484 case 'F':
5485 Type = Context.getCFConstantStringType();
5486 break;
Fariborz Jahanianba8bda02010-11-09 21:38:20 +00005487 case 'G':
5488 Type = Context.getObjCIdType();
5489 break;
5490 case 'H':
5491 Type = Context.getObjCSelType();
5492 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005493 case 'a':
5494 Type = Context.getBuiltinVaListType();
5495 assert(!Type.isNull() && "builtin va list type not initialized!");
5496 break;
5497 case 'A':
5498 // This is a "reference" to a va_list; however, what exactly
5499 // this means depends on how va_list is defined. There are two
5500 // different kinds of va_list: ones passed by value, and ones
5501 // passed by reference. An example of a by-value va_list is
5502 // x86, where va_list is a char*. An example of by-ref va_list
5503 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5504 // we want this argument to be a char*&; for x86-64, we want
5505 // it to be a __va_list_tag*.
5506 Type = Context.getBuiltinVaListType();
5507 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattner14e0e742010-10-01 22:53:11 +00005508 if (Type->isArrayType())
Chris Lattner86df27b2009-06-14 00:45:47 +00005509 Type = Context.getArrayDecayedType(Type);
Chris Lattner14e0e742010-10-01 22:53:11 +00005510 else
Chris Lattner86df27b2009-06-14 00:45:47 +00005511 Type = Context.getLValueReferenceType(Type);
Chris Lattner86df27b2009-06-14 00:45:47 +00005512 break;
5513 case 'V': {
5514 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00005515 unsigned NumElements = strtoul(Str, &End, 10);
5516 assert(End != Str && "Missing vector size");
Chris Lattner86df27b2009-06-14 00:45:47 +00005517 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00005518
Chris Lattner14e0e742010-10-01 22:53:11 +00005519 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5520 RequiresICE, false);
5521 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattner33daae62010-10-01 22:42:38 +00005522
5523 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner788b0fd2010-06-23 06:00:24 +00005524 Type = Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00005525 VectorType::GenericVector);
Chris Lattner86df27b2009-06-14 00:45:47 +00005526 break;
5527 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00005528 case 'X': {
Chris Lattner14e0e742010-10-01 22:53:11 +00005529 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5530 false);
5531 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregord3a23b22009-09-28 21:45:01 +00005532 Type = Context.getComplexType(ElementType);
5533 break;
5534 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005535 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00005536 Type = Context.getFILEType();
5537 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005538 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00005539 return QualType();
5540 }
Mike Stumpfd612db2009-07-28 23:47:15 +00005541 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005542 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00005543 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00005544 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00005545 else
5546 Type = Context.getjmp_bufType();
5547
Mike Stumpfd612db2009-07-28 23:47:15 +00005548 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005549 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00005550 return QualType();
5551 }
5552 break;
Mike Stump782fa302009-07-28 02:25:19 +00005553 }
Mike Stump1eb44332009-09-09 15:08:12 +00005554
Chris Lattner33daae62010-10-01 22:42:38 +00005555 // If there are modifiers and if we're allowed to parse them, go for it.
5556 Done = !AllowTypeModifiers;
Chris Lattner86df27b2009-06-14 00:45:47 +00005557 while (!Done) {
John McCall187ab372010-03-12 04:21:28 +00005558 switch (char c = *Str++) {
Chris Lattner33daae62010-10-01 22:42:38 +00005559 default: Done = true; --Str; break;
5560 case '*':
5561 case '&': {
5562 // Both pointers and references can have their pointee types
5563 // qualified with an address space.
5564 char *End;
5565 unsigned AddrSpace = strtoul(Str, &End, 10);
5566 if (End != Str && AddrSpace != 0) {
5567 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5568 Str = End;
5569 }
5570 if (c == '*')
5571 Type = Context.getPointerType(Type);
5572 else
5573 Type = Context.getLValueReferenceType(Type);
5574 break;
5575 }
5576 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5577 case 'C':
5578 Type = Type.withConst();
5579 break;
5580 case 'D':
5581 Type = Context.getVolatileType(Type);
5582 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005583 }
5584 }
Chris Lattner393bd8e2010-10-01 07:13:18 +00005585
Chris Lattner14e0e742010-10-01 22:53:11 +00005586 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner393bd8e2010-10-01 07:13:18 +00005587 "Integer constant 'I' type must be an integer");
Mike Stump1eb44332009-09-09 15:08:12 +00005588
Chris Lattner86df27b2009-06-14 00:45:47 +00005589 return Type;
5590}
5591
5592/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattner33daae62010-10-01 22:42:38 +00005593QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattner14e0e742010-10-01 22:53:11 +00005594 GetBuiltinTypeError &Error,
5595 unsigned *IntegerConstantArgs) {
Chris Lattner33daae62010-10-01 22:42:38 +00005596 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump1eb44332009-09-09 15:08:12 +00005597
Chris Lattner86df27b2009-06-14 00:45:47 +00005598 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00005599
Chris Lattner14e0e742010-10-01 22:53:11 +00005600 bool RequiresICE = false;
Chris Lattner86df27b2009-06-14 00:45:47 +00005601 Error = GE_None;
Chris Lattner14e0e742010-10-01 22:53:11 +00005602 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5603 RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005604 if (Error != GE_None)
5605 return QualType();
Chris Lattner14e0e742010-10-01 22:53:11 +00005606
5607 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5608
Chris Lattner86df27b2009-06-14 00:45:47 +00005609 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattner14e0e742010-10-01 22:53:11 +00005610 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005611 if (Error != GE_None)
5612 return QualType();
5613
Chris Lattner14e0e742010-10-01 22:53:11 +00005614 // If this argument is required to be an IntegerConstantExpression and the
5615 // caller cares, fill in the bitmask we return.
5616 if (RequiresICE && IntegerConstantArgs)
5617 *IntegerConstantArgs |= 1 << ArgTypes.size();
5618
Chris Lattner86df27b2009-06-14 00:45:47 +00005619 // Do array -> pointer decay. The builtin should use the decayed type.
5620 if (Ty->isArrayType())
5621 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00005622
Chris Lattner86df27b2009-06-14 00:45:47 +00005623 ArgTypes.push_back(Ty);
5624 }
5625
5626 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5627 "'.' should only occur at end of builtin type list!");
5628
John McCall00ccbef2010-12-21 00:44:39 +00005629 FunctionType::ExtInfo EI;
5630 if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
5631
5632 bool Variadic = (TypeStr[0] == '.');
5633
5634 // We really shouldn't be making a no-proto type here, especially in C++.
5635 if (ArgTypes.empty() && Variadic)
5636 return getFunctionNoProtoType(ResType, EI);
Douglas Gregorce056bc2010-02-21 22:15:06 +00005637
John McCalle23cf432010-12-14 08:05:40 +00005638 FunctionProtoType::ExtProtoInfo EPI;
John McCall00ccbef2010-12-21 00:44:39 +00005639 EPI.ExtInfo = EI;
5640 EPI.Variadic = Variadic;
John McCalle23cf432010-12-14 08:05:40 +00005641
5642 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
Chris Lattner86df27b2009-06-14 00:45:47 +00005643}
Eli Friedmana95d7572009-08-19 07:44:53 +00005644
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005645GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5646 GVALinkage External = GVA_StrongExternal;
5647
5648 Linkage L = FD->getLinkage();
5649 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5650 FD->getType()->getLinkage() == UniqueExternalLinkage)
5651 L = UniqueExternalLinkage;
5652
5653 switch (L) {
5654 case NoLinkage:
5655 case InternalLinkage:
5656 case UniqueExternalLinkage:
5657 return GVA_Internal;
5658
5659 case ExternalLinkage:
5660 switch (FD->getTemplateSpecializationKind()) {
5661 case TSK_Undeclared:
5662 case TSK_ExplicitSpecialization:
5663 External = GVA_StrongExternal;
5664 break;
5665
5666 case TSK_ExplicitInstantiationDefinition:
5667 return GVA_ExplicitTemplateInstantiation;
5668
5669 case TSK_ExplicitInstantiationDeclaration:
5670 case TSK_ImplicitInstantiation:
5671 External = GVA_TemplateInstantiation;
5672 break;
5673 }
5674 }
5675
5676 if (!FD->isInlined())
5677 return External;
5678
5679 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5680 // GNU or C99 inline semantics. Determine whether this symbol should be
5681 // externally visible.
5682 if (FD->isInlineDefinitionExternallyVisible())
5683 return External;
5684
5685 // C99 inline semantics, where the symbol is not externally visible.
5686 return GVA_C99Inline;
5687 }
5688
5689 // C++0x [temp.explicit]p9:
5690 // [ Note: The intent is that an inline function that is the subject of
5691 // an explicit instantiation declaration will still be implicitly
5692 // instantiated when used so that the body can be considered for
5693 // inlining, but that no out-of-line copy of the inline function would be
5694 // generated in the translation unit. -- end note ]
5695 if (FD->getTemplateSpecializationKind()
5696 == TSK_ExplicitInstantiationDeclaration)
5697 return GVA_C99Inline;
5698
5699 return GVA_CXXInline;
5700}
5701
5702GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
5703 // If this is a static data member, compute the kind of template
5704 // specialization. Otherwise, this variable is not part of a
5705 // template.
5706 TemplateSpecializationKind TSK = TSK_Undeclared;
5707 if (VD->isStaticDataMember())
5708 TSK = VD->getTemplateSpecializationKind();
5709
5710 Linkage L = VD->getLinkage();
5711 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5712 VD->getType()->getLinkage() == UniqueExternalLinkage)
5713 L = UniqueExternalLinkage;
5714
5715 switch (L) {
5716 case NoLinkage:
5717 case InternalLinkage:
5718 case UniqueExternalLinkage:
5719 return GVA_Internal;
5720
5721 case ExternalLinkage:
5722 switch (TSK) {
5723 case TSK_Undeclared:
5724 case TSK_ExplicitSpecialization:
5725 return GVA_StrongExternal;
5726
5727 case TSK_ExplicitInstantiationDeclaration:
5728 llvm_unreachable("Variable should not be instantiated");
5729 // Fall through to treat this like any other instantiation.
5730
5731 case TSK_ExplicitInstantiationDefinition:
5732 return GVA_ExplicitTemplateInstantiation;
5733
5734 case TSK_ImplicitInstantiation:
5735 return GVA_TemplateInstantiation;
5736 }
5737 }
5738
5739 return GVA_StrongExternal;
5740}
5741
Argyrios Kyrtzidis4ac7c0b2010-07-29 20:08:05 +00005742bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005743 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
5744 if (!VD->isFileVarDecl())
5745 return false;
5746 } else if (!isa<FunctionDecl>(D))
5747 return false;
5748
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00005749 // Weak references don't produce any output by themselves.
5750 if (D->hasAttr<WeakRefAttr>())
5751 return false;
5752
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005753 // Aliases and used decls are required.
5754 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
5755 return true;
5756
5757 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5758 // Forward declarations aren't required.
5759 if (!FD->isThisDeclarationADefinition())
5760 return false;
5761
5762 // Constructors and destructors are required.
5763 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
5764 return true;
5765
5766 // The key function for a class is required.
5767 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5768 const CXXRecordDecl *RD = MD->getParent();
5769 if (MD->isOutOfLine() && RD->isDynamicClass()) {
5770 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
5771 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
5772 return true;
5773 }
5774 }
5775
5776 GVALinkage Linkage = GetGVALinkageForFunction(FD);
5777
5778 // static, static inline, always_inline, and extern inline functions can
5779 // always be deferred. Normal inline functions can be deferred in C99/C++.
5780 // Implicit template instantiations can also be deferred in C++.
5781 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
5782 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
5783 return false;
5784 return true;
5785 }
5786
5787 const VarDecl *VD = cast<VarDecl>(D);
5788 assert(VD->isFileVarDecl() && "Expected file scoped var");
5789
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00005790 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
5791 return false;
5792
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005793 // Structs that have non-trivial constructors or destructors are required.
5794
5795 // FIXME: Handle references.
5796 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
5797 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005798 if (RD->hasDefinition() &&
5799 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005800 return true;
5801 }
5802 }
5803
5804 GVALinkage L = GetGVALinkageForVariable(VD);
5805 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
5806 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
5807 return false;
5808 }
5809
5810 return true;
5811}
Charles Davis071cc7d2010-08-16 03:33:14 +00005812
Charles Davisee743f92010-11-09 18:04:24 +00005813CallingConv ASTContext::getDefaultMethodCallConv() {
5814 // Pass through to the C++ ABI object
5815 return ABI->getDefaultMethodCallConv();
5816}
5817
Anders Carlssondae0cb52010-11-25 01:51:53 +00005818bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) {
5819 // Pass through to the C++ ABI object
5820 return ABI->isNearlyEmpty(RD);
5821}
5822
Charles Davis071cc7d2010-08-16 03:33:14 +00005823CXXABI::~CXXABI() {}