blob: d7ebedf72a036b57d40d8ae70b0b666c058f3499 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyckbdc601b2009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
John McCallea1471e2010-05-20 01:18:31 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000023#include "clang/AST/ASTMutationListener.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000026#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "clang/Basic/TargetInfo.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000028#include "llvm/ADT/SmallString.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000029#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000030#include "llvm/Support/MathExtras.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000031#include "llvm/Support/raw_ostream.h"
Charles Davis071cc7d2010-08-16 03:33:14 +000032#include "CXXABI.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000033
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Douglas Gregor18274032010-07-03 00:47:00 +000036unsigned ASTContext::NumImplicitDefaultConstructors;
37unsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
Douglas Gregor22584312010-07-02 23:41:54 +000038unsigned ASTContext::NumImplicitCopyConstructors;
39unsigned ASTContext::NumImplicitCopyConstructorsDeclared;
Douglas Gregora376d102010-07-02 21:50:04 +000040unsigned ASTContext::NumImplicitCopyAssignmentOperators;
41unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
Douglas Gregor4923aa22010-07-02 20:37:36 +000042unsigned ASTContext::NumImplicitDestructors;
43unsigned ASTContext::NumImplicitDestructorsDeclared;
44
Reid Spencer5f016e22007-07-11 17:01:13 +000045enum FloatingRank {
46 FloatRank, DoubleRank, LongDoubleRank
47};
48
Douglas Gregor3e1274f2010-06-16 21:09:37 +000049void
50ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
51 TemplateTemplateParmDecl *Parm) {
52 ID.AddInteger(Parm->getDepth());
53 ID.AddInteger(Parm->getPosition());
54 // FIXME: Parameter pack
55
56 TemplateParameterList *Params = Parm->getTemplateParameters();
57 ID.AddInteger(Params->size());
58 for (TemplateParameterList::const_iterator P = Params->begin(),
59 PEnd = Params->end();
60 P != PEnd; ++P) {
61 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
62 ID.AddInteger(0);
63 ID.AddBoolean(TTP->isParameterPack());
64 continue;
65 }
66
67 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
68 ID.AddInteger(1);
69 // FIXME: Parameter pack
70 ID.AddPointer(NTTP->getType().getAsOpaquePtr());
71 continue;
72 }
73
74 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
75 ID.AddInteger(2);
76 Profile(ID, TTP);
77 }
78}
79
80TemplateTemplateParmDecl *
81ASTContext::getCanonicalTemplateTemplateParmDecl(
82 TemplateTemplateParmDecl *TTP) {
83 // Check if we already have a canonical template template parameter.
84 llvm::FoldingSetNodeID ID;
85 CanonicalTemplateTemplateParm::Profile(ID, TTP);
86 void *InsertPos = 0;
87 CanonicalTemplateTemplateParm *Canonical
88 = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
89 if (Canonical)
90 return Canonical->getParam();
91
92 // Build a canonical template parameter list.
93 TemplateParameterList *Params = TTP->getTemplateParameters();
94 llvm::SmallVector<NamedDecl *, 4> CanonParams;
95 CanonParams.reserve(Params->size());
96 for (TemplateParameterList::const_iterator P = Params->begin(),
97 PEnd = Params->end();
98 P != PEnd; ++P) {
99 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
100 CanonParams.push_back(
101 TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
102 SourceLocation(), TTP->getDepth(),
103 TTP->getIndex(), 0, false,
104 TTP->isParameterPack()));
105 else if (NonTypeTemplateParmDecl *NTTP
106 = dyn_cast<NonTypeTemplateParmDecl>(*P))
107 CanonParams.push_back(
108 NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
109 SourceLocation(), NTTP->getDepth(),
110 NTTP->getPosition(), 0,
111 getCanonicalType(NTTP->getType()),
112 0));
113 else
114 CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
115 cast<TemplateTemplateParmDecl>(*P)));
116 }
117
118 TemplateTemplateParmDecl *CanonTTP
119 = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
120 SourceLocation(), TTP->getDepth(),
121 TTP->getPosition(), 0,
122 TemplateParameterList::Create(*this, SourceLocation(),
123 SourceLocation(),
124 CanonParams.data(),
125 CanonParams.size(),
126 SourceLocation()));
127
128 // Get the new insert position for the node we care about.
129 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
130 assert(Canonical == 0 && "Shouldn't be in the map!");
131 (void)Canonical;
132
133 // Create the canonical template template parameter entry.
134 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
135 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
136 return CanonTTP;
137}
138
Charles Davis071cc7d2010-08-16 03:33:14 +0000139CXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
John McCallee79a4c2010-08-21 22:46:04 +0000140 if (!LangOpts.CPlusPlus) return 0;
141
Charles Davis20cf7172010-08-19 02:18:14 +0000142 switch (T.getCXXABI()) {
John McCallee79a4c2010-08-21 22:46:04 +0000143 case CXXABI_ARM:
144 return CreateARMCXXABI(*this);
145 case CXXABI_Itanium:
Charles Davis071cc7d2010-08-16 03:33:14 +0000146 return CreateItaniumCXXABI(*this);
Charles Davis20cf7172010-08-19 02:18:14 +0000147 case CXXABI_Microsoft:
148 return CreateMicrosoftCXXABI(*this);
149 }
John McCallee79a4c2010-08-21 22:46:04 +0000150 return 0;
Charles Davis071cc7d2010-08-16 03:33:14 +0000151}
152
Chris Lattner61710852008-10-05 17:34:18 +0000153ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar444be732009-11-13 05:51:54 +0000154 const TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +0000155 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +0000156 Builtin::Context &builtins,
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000157 unsigned size_reserve) :
John McCallef990012010-06-11 11:07:21 +0000158 TemplateSpecializationTypes(this_()),
159 DependentTemplateSpecializationTypes(this_()),
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +0000160 GlobalNestedNameSpecifier(0), IsInt128Installed(false),
161 CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +0000162 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump083c25e2009-10-22 00:49:09 +0000163 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
John McCallbf1a0282010-06-04 23:28:52 +0000164 NullTypeSourceInfo(QualType()),
Charles Davis071cc7d2010-08-16 03:33:14 +0000165 SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +0000166 Idents(idents), Selectors(sels),
Ted Kremenekac9590e2010-05-10 20:40:08 +0000167 BuiltinInfo(builtins),
168 DeclarationNames(*this),
169 ExternalSource(0), PrintingPolicy(LOpts),
Ted Kremenekf057bf72010-06-18 00:31:04 +0000170 LastSDM(0, 0),
171 UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) {
David Chisnall0f436562009-08-17 16:35:33 +0000172 ObjCIdRedefinitionType = QualType();
173 ObjCClassRedefinitionType = QualType();
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000174 ObjCSelRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000175 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +0000176 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +0000177 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +0000178}
179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180ASTContext::~ASTContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +0000181 // Release the DenseMaps associated with DeclContext objects.
182 // FIXME: Is this the ideal solution?
183 ReleaseDeclContextMaps();
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000184
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000185 // Call all of the deallocation functions.
186 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
187 Deallocations[I].first(Deallocations[I].second);
Douglas Gregor00545312010-05-23 18:26:36 +0000188
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000189 // Release all of the memory associated with overridden C++ methods.
190 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
191 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
192 OM != OMEnd; ++OM)
193 OM->second.Destroy();
Ted Kremenek3478eb62010-02-11 07:12:28 +0000194
Ted Kremenekdcfcfbe2010-06-08 23:00:58 +0000195 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000196 // because they can contain DenseMaps.
197 for (llvm::DenseMap<const ObjCContainerDecl*,
198 const ASTRecordLayout*>::iterator
199 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
200 // Increment in loop to prevent using deallocated memory.
201 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
202 R->Destroy(*this);
203
Ted Kremenekdcfcfbe2010-06-08 23:00:58 +0000204 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
205 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
206 // Increment in loop to prevent using deallocated memory.
207 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
208 R->Destroy(*this);
209 }
Douglas Gregor63200642010-08-30 16:49:28 +0000210
211 for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
212 AEnd = DeclAttrs.end();
213 A != AEnd; ++A)
214 A->second->~AttrVec();
215}
Douglas Gregorab452ba2009-03-26 23:50:42 +0000216
Douglas Gregor00545312010-05-23 18:26:36 +0000217void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
218 Deallocations.push_back(std::make_pair(Callback, Data));
219}
220
Mike Stump1eb44332009-09-09 15:08:12 +0000221void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
223 ExternalSource.reset(Source.take());
224}
225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226void ASTContext::PrintStats() const {
227 fprintf(stderr, "*** AST Context Stats:\n");
228 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000229
Douglas Gregordbe833d2009-05-26 14:40:08 +0000230 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000231#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000232#define ABSTRACT_TYPE(Name, Parent)
233#include "clang/AST/TypeNodes.def"
234 0 // Extra
235 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
238 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000239 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 }
241
Douglas Gregordbe833d2009-05-26 14:40:08 +0000242 unsigned Idx = 0;
243 unsigned TotalBytes = 0;
244#define TYPE(Name, Parent) \
245 if (counts[Idx]) \
246 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
247 TotalBytes += counts[Idx] * sizeof(Name##Type); \
248 ++Idx;
249#define ABSTRACT_TYPE(Name, Parent)
250#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Douglas Gregordbe833d2009-05-26 14:40:08 +0000252 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregored8abf12010-07-08 06:14:04 +0000253
Douglas Gregor4923aa22010-07-02 20:37:36 +0000254 // Implicit special member functions.
Douglas Gregor18274032010-07-03 00:47:00 +0000255 fprintf(stderr, " %u/%u implicit default constructors created\n",
256 NumImplicitDefaultConstructorsDeclared,
257 NumImplicitDefaultConstructors);
Douglas Gregor22584312010-07-02 23:41:54 +0000258 fprintf(stderr, " %u/%u implicit copy constructors created\n",
259 NumImplicitCopyConstructorsDeclared,
260 NumImplicitCopyConstructors);
Douglas Gregora376d102010-07-02 21:50:04 +0000261 fprintf(stderr, " %u/%u implicit copy assignment operators created\n",
262 NumImplicitCopyAssignmentOperatorsDeclared,
263 NumImplicitCopyAssignmentOperators);
Douglas Gregor4923aa22010-07-02 20:37:36 +0000264 fprintf(stderr, " %u/%u implicit destructors created\n",
265 NumImplicitDestructorsDeclared, NumImplicitDestructors);
266
Douglas Gregor2cf26342009-04-09 22:27:44 +0000267 if (ExternalSource.get()) {
268 fprintf(stderr, "\n");
269 ExternalSource->PrintStats();
270 }
Douglas Gregored8abf12010-07-08 06:14:04 +0000271
Douglas Gregor63fe86b2010-07-25 17:53:33 +0000272 BumpAlloc.PrintStats();
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
275
John McCalle27ec8a2009-10-23 23:03:21 +0000276void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000277 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000278 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000279 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282void ASTContext::InitBuiltinTypes() {
283 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 // C99 6.2.5p19.
286 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // C99 6.2.5p2.
289 InitBuiltinType(BoolTy, BuiltinType::Bool);
290 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000291 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 InitBuiltinType(CharTy, BuiltinType::Char_S);
293 else
294 InitBuiltinType(CharTy, BuiltinType::Char_U);
295 // C99 6.2.5p4.
296 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
297 InitBuiltinType(ShortTy, BuiltinType::Short);
298 InitBuiltinType(IntTy, BuiltinType::Int);
299 InitBuiltinType(LongTy, BuiltinType::Long);
300 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // C99 6.2.5p6.
303 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
304 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
305 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
306 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
307 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 // C99 6.2.5p10.
310 InitBuiltinType(FloatTy, BuiltinType::Float);
311 InitBuiltinType(DoubleTy, BuiltinType::Double);
312 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000313
Chris Lattner2df9ced2009-04-30 02:43:43 +0000314 // GNU extension, 128-bit integers.
315 InitBuiltinType(Int128Ty, BuiltinType::Int128);
316 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
317
Chris Lattner3a250322009-02-26 23:43:47 +0000318 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
319 InitBuiltinType(WCharTy, BuiltinType::WChar);
320 else // C99
321 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000322
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000323 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
324 InitBuiltinType(Char16Ty, BuiltinType::Char16);
325 else // C99
326 Char16Ty = getFromTargetType(Target.getChar16Type());
327
328 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
329 InitBuiltinType(Char32Ty, BuiltinType::Char32);
330 else // C99
331 Char32Ty = getFromTargetType(Target.getChar32Type());
332
Douglas Gregor898574e2008-12-05 23:32:09 +0000333 // Placeholder type for type-dependent expressions whose type is
334 // completely unknown. No code should ever check a type against
335 // DependentTy and users should never see it; however, it is here to
336 // help diagnose failures to properly check for type-dependent
337 // expressions.
338 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000339
John McCall2a984ca2010-10-12 00:20:44 +0000340 // Placeholder type for functions.
341 InitBuiltinType(OverloadTy, BuiltinType::Overload);
342
Mike Stump1eb44332009-09-09 15:08:12 +0000343 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000344 // not yet been deduced.
John McCall2a984ca2010-10-12 00:20:44 +0000345 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 // C99 6.2.5p11.
348 FloatComplexTy = getComplexType(FloatTy);
349 DoubleComplexTy = getComplexType(DoubleTy);
350 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000351
Steve Naroff7e219e42007-10-15 14:41:52 +0000352 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Steve Naroffde2e22d2009-07-15 18:40:39 +0000354 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
355 ObjCIdTypedefType = QualType();
356 ObjCClassTypedefType = QualType();
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000357 ObjCSelTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000359 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroffde2e22d2009-07-15 18:40:39 +0000360 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
361 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000362 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff14108da2009-07-10 23:34:53 +0000363
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000364 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000366 // void * type
367 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000368
369 // nullptr type (C++0x 2.14.7)
370 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000371}
372
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000373Diagnostic &ASTContext::getDiagnostics() const {
374 return SourceMgr.getDiagnostics();
375}
376
Douglas Gregor63200642010-08-30 16:49:28 +0000377AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
378 AttrVec *&Result = DeclAttrs[D];
379 if (!Result) {
380 void *Mem = Allocate(sizeof(AttrVec));
381 Result = new (Mem) AttrVec;
382 }
383
384 return *Result;
385}
386
387/// \brief Erase the attributes corresponding to the given declaration.
388void ASTContext::eraseDeclAttrs(const Decl *D) {
389 llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
390 if (Pos != DeclAttrs.end()) {
391 Pos->second->~AttrVec();
392 DeclAttrs.erase(Pos);
393 }
394}
395
396
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000397MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000398ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000399 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000400 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000401 = InstantiatedFromStaticDataMember.find(Var);
402 if (Pos == InstantiatedFromStaticDataMember.end())
403 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Douglas Gregor7caa6822009-07-24 20:34:43 +0000405 return Pos->second;
406}
407
Mike Stump1eb44332009-09-09 15:08:12 +0000408void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000409ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000410 TemplateSpecializationKind TSK,
411 SourceLocation PointOfInstantiation) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000412 assert(Inst->isStaticDataMember() && "Not a static data member");
413 assert(Tmpl->isStaticDataMember() && "Not a static data member");
414 assert(!InstantiatedFromStaticDataMember[Inst] &&
415 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000416 InstantiatedFromStaticDataMember[Inst]
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000417 = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000418}
419
John McCall7ba107a2009-11-18 02:36:19 +0000420NamedDecl *
John McCalled976492009-12-04 22:46:56 +0000421ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCall7ba107a2009-11-18 02:36:19 +0000422 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCalled976492009-12-04 22:46:56 +0000423 = InstantiatedFromUsingDecl.find(UUD);
424 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson0d8df782009-08-29 19:37:28 +0000425 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlsson0d8df782009-08-29 19:37:28 +0000427 return Pos->second;
428}
429
430void
John McCalled976492009-12-04 22:46:56 +0000431ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
432 assert((isa<UsingDecl>(Pattern) ||
433 isa<UnresolvedUsingValueDecl>(Pattern) ||
434 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
435 "pattern decl is not a using decl");
436 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
437 InstantiatedFromUsingDecl[Inst] = Pattern;
438}
439
440UsingShadowDecl *
441ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
442 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
443 = InstantiatedFromUsingShadowDecl.find(Inst);
444 if (Pos == InstantiatedFromUsingShadowDecl.end())
445 return 0;
446
447 return Pos->second;
448}
449
450void
451ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
452 UsingShadowDecl *Pattern) {
453 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
454 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +0000455}
456
Anders Carlssond8b285f2009-09-01 04:26:58 +0000457FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
458 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
459 = InstantiatedFromUnnamedFieldDecl.find(Field);
460 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
461 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anders Carlssond8b285f2009-09-01 04:26:58 +0000463 return Pos->second;
464}
465
466void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
467 FieldDecl *Tmpl) {
468 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
469 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
470 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
471 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Anders Carlssond8b285f2009-09-01 04:26:58 +0000473 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
474}
475
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000476ASTContext::overridden_cxx_method_iterator
477ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
478 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
479 = OverriddenMethods.find(Method);
480 if (Pos == OverriddenMethods.end())
481 return 0;
482
483 return Pos->second.begin();
484}
485
486ASTContext::overridden_cxx_method_iterator
487ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
488 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
489 = OverriddenMethods.find(Method);
490 if (Pos == OverriddenMethods.end())
491 return 0;
492
493 return Pos->second.end();
494}
495
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000496unsigned
497ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
498 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
499 = OverriddenMethods.find(Method);
500 if (Pos == OverriddenMethods.end())
501 return 0;
502
503 return Pos->second.size();
504}
505
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000506void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
507 const CXXMethodDecl *Overridden) {
508 OverriddenMethods[Method].push_back(Overridden);
509}
510
Chris Lattner464175b2007-07-18 17:52:12 +0000511//===----------------------------------------------------------------------===//
512// Type Sizing and Analysis
513//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000514
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000515/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
516/// scalar floating point type.
517const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000518 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000519 assert(BT && "Not a floating point type!");
520 switch (BT->getKind()) {
521 default: assert(0 && "Not a floating point type!");
522 case BuiltinType::Float: return Target.getFloatFormat();
523 case BuiltinType::Double: return Target.getDoubleFormat();
524 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
525 }
526}
527
Ken Dyck8b752f12010-01-27 17:10:57 +0000528/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000529/// specified decl. Note that bitfields do not have a valid alignment, so
530/// this method will assert on them.
Sebastian Redl5d484e82009-11-23 17:18:46 +0000531/// If @p RefAsPointee, references are treated like their underlying type
532/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck8b752f12010-01-27 17:10:57 +0000533CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000534 unsigned Align = Target.getCharWidth();
535
John McCall4081a5c2010-10-08 18:24:19 +0000536 bool UseAlignAttrOnly = false;
537 if (unsigned AlignFromAttr = D->getMaxAlignment()) {
538 Align = AlignFromAttr;
Eli Friedmandcdafb62009-02-22 02:56:25 +0000539
John McCall4081a5c2010-10-08 18:24:19 +0000540 // __attribute__((aligned)) can increase or decrease alignment
541 // *except* on a struct or struct member, where it only increases
542 // alignment unless 'packed' is also specified.
543 //
544 // It is an error for [[align]] to decrease alignment, so we can
545 // ignore that possibility; Sema should diagnose it.
546 if (isa<FieldDecl>(D)) {
547 UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
548 cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
549 } else {
550 UseAlignAttrOnly = true;
551 }
552 }
553
554 if (UseAlignAttrOnly) {
555 // ignore type of value
556 } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneraf707ab2009-01-24 21:53:27 +0000557 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000558 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl5d484e82009-11-23 17:18:46 +0000559 if (RefAsPointee)
560 T = RT->getPointeeType();
561 else
562 T = getPointerType(RT->getPointeeType());
563 }
564 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindola6deecb02010-06-04 23:15:27 +0000565 unsigned MinWidth = Target.getLargeArrayMinWidth();
566 unsigned ArrayAlign = Target.getLargeArrayAlign();
567 if (isa<VariableArrayType>(T) && MinWidth != 0)
568 Align = std::max(Align, ArrayAlign);
569 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
570 unsigned Size = getTypeSize(CT);
571 if (MinWidth != 0 && MinWidth <= Size)
572 Align = std::max(Align, ArrayAlign);
573 }
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000574 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000575 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
576 T = cast<ArrayType>(T)->getElementType();
577
578 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
579 }
Charles Davis05f62472010-02-23 04:52:00 +0000580 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
581 // In the case of a field in a packed struct, we want the minimum
582 // of the alignment of the field and the alignment of the struct.
583 Align = std::min(Align,
584 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
585 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000586 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000587
Ken Dyck8b752f12010-01-27 17:10:57 +0000588 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000589}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000590
John McCallea1471e2010-05-20 01:18:31 +0000591std::pair<CharUnits, CharUnits>
592ASTContext::getTypeInfoInChars(const Type *T) {
593 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
594 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
595 CharUnits::fromQuantity(Info.second / getCharWidth()));
596}
597
598std::pair<CharUnits, CharUnits>
599ASTContext::getTypeInfoInChars(QualType T) {
600 return getTypeInfoInChars(T.getTypePtr());
601}
602
Chris Lattnera7674d82007-07-13 22:13:22 +0000603/// getTypeSize - Return the size of the specified type, in bits. This method
604/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000605///
606/// FIXME: Pointers into different addr spaces could have different sizes and
607/// alignment requirements: getPointerInfo should take an AddrSpace, this
608/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000609std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000610ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000611 uint64_t Width=0;
612 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000613 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000614#define TYPE(Class, Base)
615#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000616#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000617#define DEPENDENT_TYPE(Class, Base) case Type::Class:
618#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000619 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000620 break;
621
Chris Lattner692233e2007-07-13 22:27:08 +0000622 case Type::FunctionNoProto:
623 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000624 // GCC extension: alignof(function) = 32 bits
625 Width = 0;
626 Align = 32;
627 break;
628
Douglas Gregor72564e72009-02-26 23:50:07 +0000629 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000630 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000631 Width = 0;
632 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
633 break;
634
Steve Narofffb22d962007-08-30 01:06:46 +0000635 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000636 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Chris Lattner98be4942008-03-05 18:54:05 +0000638 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000639 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000640 Align = EltInfo.second;
641 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000642 }
Nate Begeman213541a2008-04-18 23:10:10 +0000643 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000644 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000645 const VectorType *VT = cast<VectorType>(T);
646 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
647 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000648 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000649 // If the alignment is not a power of 2, round up to the next power of 2.
650 // This happens for non-power-of-2 length vectors.
Dan Gohman8eefcd32010-04-21 23:32:43 +0000651 if (Align & (Align-1)) {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000652 Align = llvm::NextPowerOf2(Align);
653 Width = llvm::RoundUpToAlignment(Width, Align);
654 }
Chris Lattner030d8842007-07-19 22:06:24 +0000655 break;
656 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000657
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000658 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000659 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000660 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000661 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000662 // GCC extension: alignof(void) = 8 bits.
663 Width = 0;
664 Align = 8;
665 break;
666
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000667 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000668 Width = Target.getBoolWidth();
669 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000670 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000671 case BuiltinType::Char_S:
672 case BuiltinType::Char_U:
673 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000674 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000675 Width = Target.getCharWidth();
676 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000677 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000678 case BuiltinType::WChar:
679 Width = Target.getWCharWidth();
680 Align = Target.getWCharAlign();
681 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000682 case BuiltinType::Char16:
683 Width = Target.getChar16Width();
684 Align = Target.getChar16Align();
685 break;
686 case BuiltinType::Char32:
687 Width = Target.getChar32Width();
688 Align = Target.getChar32Align();
689 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000690 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000691 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000692 Width = Target.getShortWidth();
693 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000694 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000695 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000696 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000697 Width = Target.getIntWidth();
698 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000699 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000700 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000701 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000702 Width = Target.getLongWidth();
703 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000704 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000705 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000706 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000707 Width = Target.getLongLongWidth();
708 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000709 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000710 case BuiltinType::Int128:
711 case BuiltinType::UInt128:
712 Width = 128;
713 Align = 128; // int128_t is 128-bit aligned on all targets.
714 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000715 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000716 Width = Target.getFloatWidth();
717 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000718 break;
719 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000720 Width = Target.getDoubleWidth();
721 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000722 break;
723 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000724 Width = Target.getLongDoubleWidth();
725 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000726 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000727 case BuiltinType::NullPtr:
728 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
729 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000730 break;
Fariborz Jahaniane04f5fc2010-08-02 18:03:20 +0000731 case BuiltinType::ObjCId:
732 case BuiltinType::ObjCClass:
733 case BuiltinType::ObjCSel:
734 Width = Target.getPointerWidth(0);
735 Align = Target.getPointerAlign(0);
736 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000737 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000738 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000739 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000740 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000741 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000742 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000743 case Type::BlockPointer: {
744 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
745 Width = Target.getPointerWidth(AS);
746 Align = Target.getPointerAlign(AS);
747 break;
748 }
Sebastian Redl5d484e82009-11-23 17:18:46 +0000749 case Type::LValueReference:
750 case Type::RValueReference: {
751 // alignof and sizeof should never enter this code path here, so we go
752 // the pointer route.
753 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
754 Width = Target.getPointerWidth(AS);
755 Align = Target.getPointerAlign(AS);
756 break;
757 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000758 case Type::Pointer: {
759 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000760 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000761 Align = Target.getPointerAlign(AS);
762 break;
763 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000764 case Type::MemberPointer: {
Charles Davis071cc7d2010-08-16 03:33:14 +0000765 const MemberPointerType *MPT = cast<MemberPointerType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000766 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000767 getTypeInfo(getPointerDiffType());
Charles Davis071cc7d2010-08-16 03:33:14 +0000768 Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000769 Align = PtrDiffInfo.second;
770 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000771 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000772 case Type::Complex: {
773 // Complex types have the same alignment as their elements, but twice the
774 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000775 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000776 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000777 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000778 Align = EltInfo.second;
779 break;
780 }
John McCallc12c5bb2010-05-15 11:32:37 +0000781 case Type::ObjCObject:
782 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Patel44a3dde2008-06-04 21:54:36 +0000783 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000784 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000785 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
786 Width = Layout.getSize();
787 Align = Layout.getAlignment();
788 break;
789 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000790 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000791 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000792 const TagType *TT = cast<TagType>(T);
793
794 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000795 Width = 1;
796 Align = 1;
797 break;
798 }
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Daniel Dunbar1d751182008-11-08 05:48:37 +0000800 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000801 return getTypeInfo(ET->getDecl()->getIntegerType());
802
Daniel Dunbar1d751182008-11-08 05:48:37 +0000803 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000804 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
805 Width = Layout.getSize();
806 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000807 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000808 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000809
Chris Lattner9fcfe922009-10-22 05:17:15 +0000810 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000811 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
812 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000813
Douglas Gregor18857642009-04-30 17:32:17 +0000814 case Type::Typedef: {
815 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Douglas Gregordf1367a2010-08-27 00:11:28 +0000816 std::pair<uint64_t, unsigned> Info
817 = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
818 Align = std::max(Typedef->getMaxAlignment(), Info.second);
819 Width = Info.first;
Douglas Gregor7532dc62009-03-30 22:58:21 +0000820 break;
Chris Lattner71763312008-04-06 22:05:18 +0000821 }
Douglas Gregor18857642009-04-30 17:32:17 +0000822
823 case Type::TypeOfExpr:
824 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
825 .getTypePtr());
826
827 case Type::TypeOf:
828 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
829
Anders Carlsson395b4752009-06-24 19:06:50 +0000830 case Type::Decltype:
831 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
832 .getTypePtr());
833
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000834 case Type::Elaborated:
835 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Douglas Gregor18857642009-04-30 17:32:17 +0000837 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000838 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000839 "Cannot request the size of a dependent type");
840 // FIXME: this is likely to be wrong once we support template
841 // aliases, since a template alias could refer to a typedef that
842 // has an __aligned__ attribute on it.
843 return getTypeInfo(getCanonicalType(T));
844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Chris Lattner464175b2007-07-18 17:52:12 +0000846 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000847 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000848}
849
Ken Dyckbdc601b2009-12-22 14:23:30 +0000850/// getTypeSizeInChars - Return the size of the specified type, in characters.
851/// This method does not work on incomplete types.
852CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000853 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000854}
855CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000856 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000857}
858
Ken Dyck16e20cc2010-01-26 17:25:18 +0000859/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck86fa4312010-01-26 17:22:55 +0000860/// characters. This method does not work on incomplete types.
861CharUnits ASTContext::getTypeAlignInChars(QualType T) {
862 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
863}
864CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
865 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
866}
867
Chris Lattner34ebde42009-01-27 18:08:34 +0000868/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
869/// type for the current target in bits. This can be different than the ABI
870/// alignment in cases where it is beneficial for performance to overalign
871/// a data type.
872unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
873 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000874
875 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000876 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000877 T = CT->getElementType().getTypePtr();
878 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
879 T->isSpecificBuiltinType(BuiltinType::LongLong))
880 return std::max(ABIAlign, (unsigned)getTypeSize(T));
881
Chris Lattner34ebde42009-01-27 18:08:34 +0000882 return ABIAlign;
883}
884
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000885/// ShallowCollectObjCIvars -
886/// Collect all ivars, including those synthesized, in the current class.
887///
888void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000889 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000890 // FIXME. This need be removed but there are two many places which
891 // assume const-ness of ObjCInterfaceDecl
892 ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
893 for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
894 Iv= Iv->getNextIvar())
895 Ivars.push_back(Iv);
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000896}
897
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000898/// DeepCollectObjCIvars -
899/// This routine first collects all declared, but not synthesized, ivars in
900/// super class and then collects all ivars, including those synthesized for
901/// current class. This routine is used for implementation of current class
902/// when all ivars, declared and synthesized are known.
Fariborz Jahanian98200742009-05-12 18:14:29 +0000903///
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000904void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
905 bool leafClass,
Fariborz Jahanian98200742009-05-12 18:14:29 +0000906 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000907 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
908 DeepCollectObjCIvars(SuperClass, false, Ivars);
909 if (!leafClass) {
910 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
911 E = OI->ivar_end(); I != E; ++I)
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000912 Ivars.push_back(*I);
913 }
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +0000914 else
915 ShallowCollectObjCIvars(OI, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +0000916}
917
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000918/// CollectInheritedProtocols - Collect all protocols in current class and
919/// those inherited by it.
920void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000921 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000922 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000923 // We can use protocol_iterator here instead of
924 // all_referenced_protocol_iterator since we are walking all categories.
925 for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
926 PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000927 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000928 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000929 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000930 PE = Proto->protocol_end(); P != PE; ++P) {
931 Protocols.insert(*P);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000932 CollectInheritedProtocols(*P, Protocols);
933 }
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000934 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000935
936 // Categories of this Interface.
937 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
938 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
939 CollectInheritedProtocols(CDeclChain, Protocols);
940 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
941 while (SD) {
942 CollectInheritedProtocols(SD, Protocols);
943 SD = SD->getSuperClass();
944 }
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000945 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000946 for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000947 PE = OC->protocol_end(); P != PE; ++P) {
948 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000949 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000950 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
951 PE = Proto->protocol_end(); P != PE; ++P)
952 CollectInheritedProtocols(*P, Protocols);
953 }
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000954 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000955 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
956 PE = OP->protocol_end(); P != PE; ++P) {
957 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000958 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000959 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
960 PE = Proto->protocol_end(); P != PE; ++P)
961 CollectInheritedProtocols(*P, Protocols);
962 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000963 }
964}
965
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000966unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
967 unsigned count = 0;
968 // Count ivars declared in class extension.
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000969 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
970 CDecl = CDecl->getNextClassExtension())
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000971 count += CDecl->ivar_size();
972
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000973 // Count ivar defined in this class's implementation. This
974 // includes synthesized ivars.
975 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramerb170ca52010-04-27 17:47:25 +0000976 count += ImplDecl->ivar_size();
977
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000978 return count;
979}
980
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000981/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
982ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
983 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
984 I = ObjCImpls.find(D);
985 if (I != ObjCImpls.end())
986 return cast<ObjCImplementationDecl>(I->second);
987 return 0;
988}
989/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
990ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
991 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
992 I = ObjCImpls.find(D);
993 if (I != ObjCImpls.end())
994 return cast<ObjCCategoryImplDecl>(I->second);
995 return 0;
996}
997
998/// \brief Set the implementation of ObjCInterfaceDecl.
999void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1000 ObjCImplementationDecl *ImplD) {
1001 assert(IFaceD && ImplD && "Passed null params");
1002 ObjCImpls[IFaceD] = ImplD;
1003}
1004/// \brief Set the implementation of ObjCCategoryDecl.
1005void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1006 ObjCCategoryImplDecl *ImplD) {
1007 assert(CatD && ImplD && "Passed null params");
1008 ObjCImpls[CatD] = ImplD;
1009}
1010
John McCalla93c9342009-12-07 02:54:59 +00001011/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001012///
John McCalla93c9342009-12-07 02:54:59 +00001013/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001014/// the TypeLoc wrappers.
1015///
1016/// \param T the type that will be the basis for type source info. This type
1017/// should refer to how the declarator was written in source code, not to
1018/// what type semantic analysis resolved the declarator to.
John McCalla93c9342009-12-07 02:54:59 +00001019TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall109de5e2009-10-21 00:23:54 +00001020 unsigned DataSize) {
1021 if (!DataSize)
1022 DataSize = TypeLoc::getFullDataSizeForType(T);
1023 else
1024 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCalla93c9342009-12-07 02:54:59 +00001025 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall109de5e2009-10-21 00:23:54 +00001026
John McCalla93c9342009-12-07 02:54:59 +00001027 TypeSourceInfo *TInfo =
1028 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1029 new (TInfo) TypeSourceInfo(T);
1030 return TInfo;
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001031}
1032
John McCalla93c9342009-12-07 02:54:59 +00001033TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCalla4eb74d2009-10-23 21:14:09 +00001034 SourceLocation L) {
John McCalla93c9342009-12-07 02:54:59 +00001035 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCalla4eb74d2009-10-23 21:14:09 +00001036 DI->getTypeLoc().initialize(L);
1037 return DI;
1038}
1039
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001040const ASTRecordLayout &
1041ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1042 return getObjCLayout(D, 0);
1043}
1044
1045const ASTRecordLayout &
1046ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1047 return getObjCLayout(D->getClassInterface(), D);
1048}
1049
Chris Lattnera7674d82007-07-13 22:13:22 +00001050//===----------------------------------------------------------------------===//
1051// Type creation/memoization methods
1052//===----------------------------------------------------------------------===//
1053
John McCall0953e762009-09-24 19:53:00 +00001054QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1055 unsigned Fast = Quals.getFastQualifiers();
1056 Quals.removeFastQualifiers();
1057
1058 // Check if we've already instantiated this type.
1059 llvm::FoldingSetNodeID ID;
1060 ExtQuals::Profile(ID, TypeNode, Quals);
1061 void *InsertPos = 0;
1062 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1063 assert(EQ->getQualifiers() == Quals);
1064 QualType T = QualType(EQ, Fast);
1065 return T;
1066 }
1067
John McCall6b304a02009-09-24 23:30:46 +00001068 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001069 ExtQualNodes.InsertNode(New, InsertPos);
1070 QualType T = QualType(New, Fast);
1071 return T;
1072}
1073
1074QualType ASTContext::getVolatileType(QualType T) {
1075 QualType CanT = getCanonicalType(T);
1076 if (CanT.isVolatileQualified()) return T;
1077
1078 QualifierCollector Quals;
1079 const Type *TypeNode = Quals.strip(T);
1080 Quals.addVolatile();
1081
1082 return getExtQualType(TypeNode, Quals);
1083}
1084
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001085QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001086 QualType CanT = getCanonicalType(T);
1087 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001088 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001089
John McCall0953e762009-09-24 19:53:00 +00001090 // If we are composing extended qualifiers together, merge together
1091 // into one ExtQuals node.
1092 QualifierCollector Quals;
1093 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001094
John McCall0953e762009-09-24 19:53:00 +00001095 // If this type already has an address space specified, it cannot get
1096 // another one.
1097 assert(!Quals.hasAddressSpace() &&
1098 "Type cannot be in multiple addr spaces!");
1099 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001100
John McCall0953e762009-09-24 19:53:00 +00001101 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001102}
1103
Chris Lattnerb7d25532009-02-18 22:53:11 +00001104QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001105 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001106 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001107 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001108 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001110 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001111 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001112 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001113 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1114 return getPointerType(ResultType);
1115 }
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
John McCall0953e762009-09-24 19:53:00 +00001118 // If we are composing extended qualifiers together, merge together
1119 // into one ExtQuals node.
1120 QualifierCollector Quals;
1121 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001122
John McCall0953e762009-09-24 19:53:00 +00001123 // If this type already has an ObjCGC specified, it cannot get
1124 // another one.
1125 assert(!Quals.hasObjCGCAttr() &&
1126 "Type cannot have multiple ObjCGCs!");
1127 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001128
John McCall0953e762009-09-24 19:53:00 +00001129 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001130}
Chris Lattnera7674d82007-07-13 22:13:22 +00001131
Rafael Espindola264ba482010-03-30 20:24:48 +00001132static QualType getExtFunctionType(ASTContext& Context, QualType T,
1133 const FunctionType::ExtInfo &Info) {
John McCall0953e762009-09-24 19:53:00 +00001134 QualType ResultType;
Douglas Gregor43c79c22009-12-09 00:47:37 +00001135 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1136 QualType Pointee = Pointer->getPointeeType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001137 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001138 if (ResultType == Pointee)
1139 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001140
1141 ResultType = Context.getPointerType(ResultType);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001142 } else if (const BlockPointerType *BlockPointer
1143 = T->getAs<BlockPointerType>()) {
1144 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001145 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001146 if (ResultType == Pointee)
1147 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001148
1149 ResultType = Context.getBlockPointerType(ResultType);
Douglas Gregorafac01d2010-09-01 16:29:03 +00001150 } else if (const MemberPointerType *MemberPointer
1151 = T->getAs<MemberPointerType>()) {
1152 QualType Pointee = MemberPointer->getPointeeType();
1153 ResultType = getExtFunctionType(Context, Pointee, Info);
1154 if (ResultType == Pointee)
1155 return T;
1156
1157 ResultType = Context.getMemberPointerType(ResultType,
1158 MemberPointer->getClass());
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001159 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindola264ba482010-03-30 20:24:48 +00001160 if (F->getExtInfo() == Info)
Douglas Gregor43c79c22009-12-09 00:47:37 +00001161 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001162
Douglas Gregor43c79c22009-12-09 00:47:37 +00001163 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001164 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001165 Info);
John McCall0953e762009-09-24 19:53:00 +00001166 } else {
Douglas Gregor43c79c22009-12-09 00:47:37 +00001167 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall0953e762009-09-24 19:53:00 +00001168 ResultType
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001169 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1170 FPT->getNumArgs(), FPT->isVariadic(),
1171 FPT->getTypeQuals(),
1172 FPT->hasExceptionSpec(),
1173 FPT->hasAnyExceptionSpec(),
1174 FPT->getNumExceptions(),
1175 FPT->exception_begin(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001176 Info);
John McCall0953e762009-09-24 19:53:00 +00001177 }
Douglas Gregor43c79c22009-12-09 00:47:37 +00001178 } else
1179 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001180
1181 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1182}
1183
1184QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola425ef722010-03-30 22:15:11 +00001185 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindola264ba482010-03-30 20:24:48 +00001186 return getExtFunctionType(*this, T,
1187 Info.withNoReturn(AddNoReturn));
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001188}
1189
1190QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola425ef722010-03-30 22:15:11 +00001191 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindola264ba482010-03-30 20:24:48 +00001192 return getExtFunctionType(*this, T,
1193 Info.withCallingConv(CallConv));
Mike Stump24556362009-07-25 21:26:53 +00001194}
1195
Rafael Espindola425ef722010-03-30 22:15:11 +00001196QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1197 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1198 return getExtFunctionType(*this, T,
1199 Info.withRegParm(RegParm));
1200}
1201
Reid Spencer5f016e22007-07-11 17:01:13 +00001202/// getComplexType - Return the uniqued reference to the type for a complex
1203/// number with the specified element type.
1204QualType ASTContext::getComplexType(QualType T) {
1205 // Unique pointers, to guarantee there is only one pointer of a particular
1206 // structure.
1207 llvm::FoldingSetNodeID ID;
1208 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 void *InsertPos = 0;
1211 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1212 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 // If the pointee type isn't canonical, this won't be a canonical type either,
1215 // so fill in the canonical type field.
1216 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001217 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001218 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 // Get the new insert position for the node we care about.
1221 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001222 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 }
John McCall6b304a02009-09-24 23:30:46 +00001224 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 Types.push_back(New);
1226 ComplexTypes.InsertNode(New, InsertPos);
1227 return QualType(New, 0);
1228}
1229
Reid Spencer5f016e22007-07-11 17:01:13 +00001230/// getPointerType - Return the uniqued reference to the type for a pointer to
1231/// the specified type.
1232QualType ASTContext::getPointerType(QualType T) {
1233 // Unique pointers, to guarantee there is only one pointer of a particular
1234 // structure.
1235 llvm::FoldingSetNodeID ID;
1236 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 void *InsertPos = 0;
1239 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1240 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 // If the pointee type isn't canonical, this won't be a canonical type either,
1243 // so fill in the canonical type field.
1244 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001245 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001246 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 // Get the new insert position for the node we care about.
1249 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001250 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 }
John McCall6b304a02009-09-24 23:30:46 +00001252 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 Types.push_back(New);
1254 PointerTypes.InsertNode(New, InsertPos);
1255 return QualType(New, 0);
1256}
1257
Mike Stump1eb44332009-09-09 15:08:12 +00001258/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001259/// a pointer to the specified block.
1260QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001261 assert(T->isFunctionType() && "block of function types only");
1262 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001263 // structure.
1264 llvm::FoldingSetNodeID ID;
1265 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Steve Naroff5618bd42008-08-27 16:04:49 +00001267 void *InsertPos = 0;
1268 if (BlockPointerType *PT =
1269 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1270 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001271
1272 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001273 // type either so fill in the canonical type field.
1274 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001275 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001276 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Steve Naroff5618bd42008-08-27 16:04:49 +00001278 // Get the new insert position for the node we care about.
1279 BlockPointerType *NewIP =
1280 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001281 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001282 }
John McCall6b304a02009-09-24 23:30:46 +00001283 BlockPointerType *New
1284 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001285 Types.push_back(New);
1286 BlockPointerTypes.InsertNode(New, InsertPos);
1287 return QualType(New, 0);
1288}
1289
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001290/// getLValueReferenceType - Return the uniqued reference to the type for an
1291/// lvalue reference to the specified type.
John McCall54e14c42009-10-22 22:37:11 +00001292QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 // Unique pointers, to guarantee there is only one pointer of a particular
1294 // structure.
1295 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001296 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001297
1298 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001299 if (LValueReferenceType *RT =
1300 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001302
John McCall54e14c42009-10-22 22:37:11 +00001303 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1304
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 // If the referencee type isn't canonical, this won't be a canonical type
1306 // either, so fill in the canonical type field.
1307 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001308 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1309 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1310 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001311
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001313 LValueReferenceType *NewIP =
1314 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001315 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 }
1317
John McCall6b304a02009-09-24 23:30:46 +00001318 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001319 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1320 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001322 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001323
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001324 return QualType(New, 0);
1325}
1326
1327/// getRValueReferenceType - Return the uniqued reference to the type for an
1328/// rvalue reference to the specified type.
1329QualType ASTContext::getRValueReferenceType(QualType T) {
1330 // Unique pointers, to guarantee there is only one pointer of a particular
1331 // structure.
1332 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001333 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001334
1335 void *InsertPos = 0;
1336 if (RValueReferenceType *RT =
1337 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1338 return QualType(RT, 0);
1339
John McCall54e14c42009-10-22 22:37:11 +00001340 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1341
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001342 // If the referencee type isn't canonical, this won't be a canonical type
1343 // either, so fill in the canonical type field.
1344 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001345 if (InnerRef || !T.isCanonical()) {
1346 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1347 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001348
1349 // Get the new insert position for the node we care about.
1350 RValueReferenceType *NewIP =
1351 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1352 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1353 }
1354
John McCall6b304a02009-09-24 23:30:46 +00001355 RValueReferenceType *New
1356 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001357 Types.push_back(New);
1358 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 return QualType(New, 0);
1360}
1361
Sebastian Redlf30208a2009-01-24 21:16:55 +00001362/// getMemberPointerType - Return the uniqued reference to the type for a
1363/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001364QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001365 // Unique pointers, to guarantee there is only one pointer of a particular
1366 // structure.
1367 llvm::FoldingSetNodeID ID;
1368 MemberPointerType::Profile(ID, T, Cls);
1369
1370 void *InsertPos = 0;
1371 if (MemberPointerType *PT =
1372 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1373 return QualType(PT, 0);
1374
1375 // If the pointee or class type isn't canonical, this won't be a canonical
1376 // type either, so fill in the canonical type field.
1377 QualType Canonical;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001378 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001379 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1380
1381 // Get the new insert position for the node we care about.
1382 MemberPointerType *NewIP =
1383 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1384 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1385 }
John McCall6b304a02009-09-24 23:30:46 +00001386 MemberPointerType *New
1387 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001388 Types.push_back(New);
1389 MemberPointerTypes.InsertNode(New, InsertPos);
1390 return QualType(New, 0);
1391}
1392
Mike Stump1eb44332009-09-09 15:08:12 +00001393/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001394/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001395QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001396 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001397 ArrayType::ArraySizeModifier ASM,
1398 unsigned EltTypeQuals) {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001399 assert((EltTy->isDependentType() ||
1400 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedman587cbdf2009-05-29 20:17:55 +00001401 "Constant array of VLAs is illegal!");
1402
Chris Lattner38aeec72009-05-13 04:12:56 +00001403 // Convert the array size into a canonical width matching the pointer size for
1404 // the target.
1405 llvm::APInt ArySize(ArySizeIn);
1406 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001409 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001412 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001413 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Reid Spencer5f016e22007-07-11 17:01:13 +00001416 // If the element type isn't canonical, this won't be a canonical type either,
1417 // so fill in the canonical type field.
1418 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001419 if (!EltTy.isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001420 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001421 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001422 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001423 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001424 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001425 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001426 }
Mike Stump1eb44332009-09-09 15:08:12 +00001427
John McCall6b304a02009-09-24 23:30:46 +00001428 ConstantArrayType *New = new(*this,TypeAlignment)
1429 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001430 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 Types.push_back(New);
1432 return QualType(New, 0);
1433}
1434
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00001435/// getIncompleteArrayType - Returns a unique reference to the type for a
1436/// incomplete array of the specified element type.
1437QualType ASTContext::getUnknownSizeVariableArrayType(QualType Ty) {
1438 QualType ElemTy = getBaseElementType(Ty);
1439 DeclarationName Name;
1440 llvm::SmallVector<QualType, 8> ATypes;
1441 QualType ATy = Ty;
1442 while (const ArrayType *AT = getAsArrayType(ATy)) {
1443 ATypes.push_back(ATy);
1444 ATy = AT->getElementType();
1445 }
1446 for (int i = ATypes.size() - 1; i >= 0; i--) {
1447 if (const VariableArrayType *VAT = getAsVariableArrayType(ATypes[i])) {
1448 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Star,
1449 0, VAT->getBracketsRange());
1450 }
1451 else if (const ConstantArrayType *CAT = getAsConstantArrayType(ATypes[i])) {
1452 llvm::APSInt ConstVal(CAT->getSize());
1453 ElemTy = getConstantArrayType(ElemTy, ConstVal, ArrayType::Normal, 0);
1454 }
1455 else if (getAsIncompleteArrayType(ATypes[i])) {
1456 ElemTy = getVariableArrayType(ElemTy, /*ArraySize*/0, ArrayType::Normal,
1457 0, SourceRange());
1458 }
1459 else
1460 assert(false && "DependentArrayType is seen");
1461 }
1462 return ElemTy;
1463}
1464
1465/// getVariableArrayDecayedType - Returns a vla type where known sizes
1466/// are replaced with [*]
1467QualType ASTContext::getVariableArrayDecayedType(QualType Ty) {
1468 if (Ty->isPointerType()) {
1469 QualType BaseType = Ty->getAs<PointerType>()->getPointeeType();
1470 if (isa<VariableArrayType>(BaseType)) {
1471 ArrayType *AT = dyn_cast<ArrayType>(BaseType);
1472 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1473 if (VAT->getSizeExpr()) {
1474 Ty = getUnknownSizeVariableArrayType(BaseType);
1475 Ty = getPointerType(Ty);
1476 }
1477 }
1478 }
1479 return Ty;
1480}
1481
1482
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001483/// getVariableArrayType - Returns a non-unique reference to the type for a
1484/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001485QualType ASTContext::getVariableArrayType(QualType EltTy,
1486 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001487 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001488 unsigned EltTypeQuals,
1489 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001490 // Since we don't unique expressions, it isn't possible to unique VLA's
1491 // that have an expression provided for their size.
Douglas Gregor715e9c82010-05-23 16:10:32 +00001492 QualType CanonType;
1493
1494 if (!EltTy.isCanonical()) {
1495 if (NumElts)
1496 NumElts->Retain();
1497 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1498 EltTypeQuals, Brackets);
1499 }
1500
John McCall6b304a02009-09-24 23:30:46 +00001501 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor715e9c82010-05-23 16:10:32 +00001502 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001503
1504 VariableArrayTypes.push_back(New);
1505 Types.push_back(New);
1506 return QualType(New, 0);
1507}
1508
Douglas Gregor898574e2008-12-05 23:32:09 +00001509/// getDependentSizedArrayType - Returns a non-unique reference to
1510/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001511/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001512QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1513 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001514 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001515 unsigned EltTypeQuals,
1516 SourceRange Brackets) {
Douglas Gregorcb78d882009-11-19 18:03:26 +00001517 assert((!NumElts || NumElts->isTypeDependent() ||
1518 NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001519 "Size must be type- or value-dependent!");
1520
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001521 void *InsertPos = 0;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001522 DependentSizedArrayType *Canon = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00001523 llvm::FoldingSetNodeID ID;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001524
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001525 QualType CanonicalEltTy = getCanonicalType(EltTy);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001526 if (NumElts) {
1527 // Dependently-sized array types that do not have a specified
1528 // number of elements will have their sizes deduced from an
1529 // initializer.
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001530 DependentSizedArrayType::Profile(ID, *this, CanonicalEltTy, ASM,
Douglas Gregorcb78d882009-11-19 18:03:26 +00001531 EltTypeQuals, NumElts);
1532
1533 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1534 }
1535
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001536 DependentSizedArrayType *New;
1537 if (Canon) {
1538 // We already have a canonical version of this array type; use it as
1539 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001540 New = new (*this, TypeAlignment)
1541 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1542 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001543 } else if (CanonicalEltTy == EltTy) {
1544 // This is a canonical type. Record it.
1545 New = new (*this, TypeAlignment)
1546 DependentSizedArrayType(*this, EltTy, QualType(),
1547 NumElts, ASM, EltTypeQuals, Brackets);
1548
1549 if (NumElts) {
1550#ifndef NDEBUG
1551 DependentSizedArrayType *CanonCheck
1552 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1553 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1554 (void)CanonCheck;
1555#endif
1556 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001557 }
Douglas Gregor32adc8b2010-10-26 00:51:02 +00001558 } else {
1559 QualType Canon = getDependentSizedArrayType(CanonicalEltTy, NumElts,
1560 ASM, EltTypeQuals,
1561 SourceRange());
1562 New = new (*this, TypeAlignment)
1563 DependentSizedArrayType(*this, EltTy, Canon,
1564 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001565 }
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Douglas Gregor898574e2008-12-05 23:32:09 +00001567 Types.push_back(New);
1568 return QualType(New, 0);
1569}
1570
Eli Friedmanc5773c42008-02-15 18:16:39 +00001571QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1572 ArrayType::ArraySizeModifier ASM,
1573 unsigned EltTypeQuals) {
1574 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001575 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001576
1577 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001578 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001579 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1580 return QualType(ATP, 0);
1581
1582 // If the element type isn't canonical, this won't be a canonical type
1583 // either, so fill in the canonical type field.
1584 QualType Canonical;
1585
John McCall467b27b2009-10-22 20:10:53 +00001586 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001587 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001588 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001589
1590 // Get the new insert position for the node we care about.
1591 IncompleteArrayType *NewIP =
1592 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001593 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001594 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001595
John McCall6b304a02009-09-24 23:30:46 +00001596 IncompleteArrayType *New = new (*this, TypeAlignment)
1597 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001598
1599 IncompleteArrayTypes.InsertNode(New, InsertPos);
1600 Types.push_back(New);
1601 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001602}
1603
Steve Naroff73322922007-07-18 18:00:27 +00001604/// getVectorType - Return the unique reference to a vector type of
1605/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001606QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Chris Lattner33daae62010-10-01 22:42:38 +00001607 VectorType::AltiVecSpecific AltiVecSpec) {
1608 BuiltinType *BaseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Chris Lattner33daae62010-10-01 22:42:38 +00001610 BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1611 assert(BaseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Reid Spencer5f016e22007-07-11 17:01:13 +00001613 // Check if we've already instantiated a vector of this type.
1614 llvm::FoldingSetNodeID ID;
Chris Lattner788b0fd2010-06-23 06:00:24 +00001615 VectorType::Profile(ID, vecType, NumElts, Type::Vector, AltiVecSpec);
1616
Reid Spencer5f016e22007-07-11 17:01:13 +00001617 void *InsertPos = 0;
1618 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1619 return QualType(VTP, 0);
1620
1621 // If the element type isn't canonical, this won't be a canonical type either,
1622 // so fill in the canonical type field.
1623 QualType Canonical;
Douglas Gregor255210e2010-08-06 10:14:59 +00001624 if (!vecType.isCanonical()) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00001625 Canonical = getVectorType(getCanonicalType(vecType), NumElts,
1626 VectorType::NotAltiVec);
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 // Get the new insert position for the node we care about.
1629 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001630 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 }
John McCall6b304a02009-09-24 23:30:46 +00001632 VectorType *New = new (*this, TypeAlignment)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001633 VectorType(vecType, NumElts, Canonical, AltiVecSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 VectorTypes.InsertNode(New, InsertPos);
1635 Types.push_back(New);
1636 return QualType(New, 0);
1637}
1638
Nate Begeman213541a2008-04-18 23:10:10 +00001639/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001640/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001641QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001642 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Chris Lattnerf52ab252008-04-06 22:59:24 +00001644 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001645 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Steve Naroff73322922007-07-18 18:00:27 +00001647 // Check if we've already instantiated a vector of this type.
1648 llvm::FoldingSetNodeID ID;
Chris Lattner788b0fd2010-06-23 06:00:24 +00001649 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
1650 VectorType::NotAltiVec);
Steve Naroff73322922007-07-18 18:00:27 +00001651 void *InsertPos = 0;
1652 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1653 return QualType(VTP, 0);
1654
1655 // If the element type isn't canonical, this won't be a canonical type either,
1656 // so fill in the canonical type field.
1657 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001658 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001659 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Steve Naroff73322922007-07-18 18:00:27 +00001661 // Get the new insert position for the node we care about.
1662 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001663 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001664 }
John McCall6b304a02009-09-24 23:30:46 +00001665 ExtVectorType *New = new (*this, TypeAlignment)
1666 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001667 VectorTypes.InsertNode(New, InsertPos);
1668 Types.push_back(New);
1669 return QualType(New, 0);
1670}
1671
Mike Stump1eb44332009-09-09 15:08:12 +00001672QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001673 Expr *SizeExpr,
1674 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001675 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001676 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001677 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001679 void *InsertPos = 0;
1680 DependentSizedExtVectorType *Canon
1681 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1682 DependentSizedExtVectorType *New;
1683 if (Canon) {
1684 // We already have a canonical version of this array type; use it as
1685 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001686 New = new (*this, TypeAlignment)
1687 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1688 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001689 } else {
1690 QualType CanonVecTy = getCanonicalType(vecType);
1691 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001692 New = new (*this, TypeAlignment)
1693 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1694 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001695
1696 DependentSizedExtVectorType *CanonCheck
1697 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1698 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1699 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001700 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1701 } else {
1702 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1703 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001704 New = new (*this, TypeAlignment)
1705 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001706 }
1707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001709 Types.push_back(New);
1710 return QualType(New, 0);
1711}
1712
Douglas Gregor72564e72009-02-26 23:50:07 +00001713/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001714///
Rafael Espindola264ba482010-03-30 20:24:48 +00001715QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1716 const FunctionType::ExtInfo &Info) {
1717 const CallingConv CallConv = Info.getCC();
Reid Spencer5f016e22007-07-11 17:01:13 +00001718 // Unique functions, to guarantee there is only one function of a particular
1719 // structure.
1720 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +00001721 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Reid Spencer5f016e22007-07-11 17:01:13 +00001723 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001724 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001725 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001726 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001729 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001730 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindola264ba482010-03-30 20:24:48 +00001731 Canonical =
1732 getFunctionNoProtoType(getCanonicalType(ResultTy),
1733 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001736 FunctionNoProtoType *NewIP =
1737 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001738 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 }
Mike Stump1eb44332009-09-09 15:08:12 +00001740
John McCall6b304a02009-09-24 23:30:46 +00001741 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindola264ba482010-03-30 20:24:48 +00001742 FunctionNoProtoType(ResultTy, Canonical, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001744 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 return QualType(New, 0);
1746}
1747
1748/// getFunctionType - Return a normal function type with a typed argument
1749/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001750QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001751 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001752 unsigned TypeQuals, bool hasExceptionSpec,
1753 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001754 const QualType *ExArray,
1755 const FunctionType::ExtInfo &Info) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00001756
Rafael Espindola264ba482010-03-30 20:24:48 +00001757 const CallingConv CallConv= Info.getCC();
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 // Unique functions, to guarantee there is only one function of a particular
1759 // structure.
1760 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001761 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001762 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindola264ba482010-03-30 20:24:48 +00001763 NumExs, ExArray, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001764
1765 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001766 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001767 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001769
1770 // Determine whether the type being created is already canonical or not.
John McCall54e14c42009-10-22 22:37:11 +00001771 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001773 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 isCanonical = false;
1775
1776 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001777 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001778 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001779 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001780 llvm::SmallVector<QualType, 16> CanonicalArgs;
1781 CanonicalArgs.reserve(NumArgs);
1782 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001783 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001784
Chris Lattnerf52ab252008-04-06 22:59:24 +00001785 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001786 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001787 isVariadic, TypeQuals, false,
Rafael Espindola264ba482010-03-30 20:24:48 +00001788 false, 0, 0,
1789 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl465226e2009-05-27 22:11:52 +00001790
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001792 FunctionProtoType *NewIP =
1793 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001794 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001796
Douglas Gregor72564e72009-02-26 23:50:07 +00001797 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001798 // for two variable size arrays (for parameter and exception types) at the
1799 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001800 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001801 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1802 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001803 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001804 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001805 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindola264ba482010-03-30 20:24:48 +00001806 ExArray, NumExs, Canonical, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001807 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001808 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001809 return QualType(FTP, 0);
1810}
1811
John McCall3cb0ebd2010-03-10 03:28:59 +00001812#ifndef NDEBUG
1813static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1814 if (!isa<CXXRecordDecl>(D)) return false;
1815 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1816 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1817 return true;
1818 if (RD->getDescribedClassTemplate() &&
1819 !isa<ClassTemplateSpecializationDecl>(RD))
1820 return true;
1821 return false;
1822}
1823#endif
1824
1825/// getInjectedClassNameType - Return the unique reference to the
1826/// injected class name type for the specified templated declaration.
1827QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1828 QualType TST) {
1829 assert(NeedsInjectedClassNameType(Decl));
1830 if (Decl->TypeForDecl) {
1831 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00001832 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCall3cb0ebd2010-03-10 03:28:59 +00001833 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1834 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1835 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1836 } else {
John McCall31f17ec2010-04-27 00:57:59 +00001837 Decl->TypeForDecl =
1838 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCall3cb0ebd2010-03-10 03:28:59 +00001839 Types.push_back(Decl->TypeForDecl);
1840 }
1841 return QualType(Decl->TypeForDecl, 0);
1842}
1843
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001844/// getTypeDeclType - Return the unique reference to the type for the
1845/// specified type declaration.
John McCallbecb8d52010-03-10 06:48:02 +00001846QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001847 assert(Decl && "Passed null for Decl param");
John McCallbecb8d52010-03-10 06:48:02 +00001848 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump1eb44332009-09-09 15:08:12 +00001849
John McCall19c85762010-02-16 03:57:14 +00001850 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001851 return getTypedefType(Typedef);
John McCallbecb8d52010-03-10 06:48:02 +00001852
John McCallbecb8d52010-03-10 06:48:02 +00001853 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1854 "Template type parameter types are always available.");
1855
John McCall19c85762010-02-16 03:57:14 +00001856 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001857 assert(!Record->getPreviousDeclaration() &&
1858 "struct/union has previous declaration");
1859 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001860 return getRecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00001861 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001862 assert(!Enum->getPreviousDeclaration() &&
1863 "enum has previous declaration");
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001864 return getEnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00001865 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00001866 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1867 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stump9fdbab32009-07-31 02:02:20 +00001868 } else
John McCallbecb8d52010-03-10 06:48:02 +00001869 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001870
John McCallbecb8d52010-03-10 06:48:02 +00001871 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001872 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001873}
1874
Reid Spencer5f016e22007-07-11 17:01:13 +00001875/// getTypedefType - Return the unique reference to the type for the
1876/// specified typename decl.
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00001877QualType
1878ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00001881 if (Canonical.isNull())
1882 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001883 Decl->TypeForDecl = new(*this, TypeAlignment)
1884 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 Types.push_back(Decl->TypeForDecl);
1886 return QualType(Decl->TypeForDecl, 0);
1887}
1888
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001889QualType ASTContext::getRecordType(const RecordDecl *Decl) {
1890 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1891
1892 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
1893 if (PrevDecl->TypeForDecl)
1894 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1895
1896 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
1897 Types.push_back(Decl->TypeForDecl);
1898 return QualType(Decl->TypeForDecl, 0);
1899}
1900
1901QualType ASTContext::getEnumType(const EnumDecl *Decl) {
1902 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1903
1904 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
1905 if (PrevDecl->TypeForDecl)
1906 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1907
1908 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
1909 Types.push_back(Decl->TypeForDecl);
1910 return QualType(Decl->TypeForDecl, 0);
1911}
1912
John McCall49a832b2009-10-18 09:09:24 +00001913/// \brief Retrieve a substitution-result type.
1914QualType
1915ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1916 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001917 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001918 && "replacement types must always be canonical");
1919
1920 llvm::FoldingSetNodeID ID;
1921 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1922 void *InsertPos = 0;
1923 SubstTemplateTypeParmType *SubstParm
1924 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1925
1926 if (!SubstParm) {
1927 SubstParm = new (*this, TypeAlignment)
1928 SubstTemplateTypeParmType(Parm, Replacement);
1929 Types.push_back(SubstParm);
1930 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1931 }
1932
1933 return QualType(SubstParm, 0);
1934}
1935
Douglas Gregorfab9d672009-02-05 23:33:38 +00001936/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001937/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001938/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001939QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001940 bool ParameterPack,
Douglas Gregorefed5c82010-06-16 15:23:05 +00001941 IdentifierInfo *Name) {
Douglas Gregorfab9d672009-02-05 23:33:38 +00001942 llvm::FoldingSetNodeID ID;
Douglas Gregorefed5c82010-06-16 15:23:05 +00001943 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001944 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001945 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001946 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1947
1948 if (TypeParm)
1949 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Douglas Gregorefed5c82010-06-16 15:23:05 +00001951 if (Name) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001952 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorefed5c82010-06-16 15:23:05 +00001953 TypeParm = new (*this, TypeAlignment)
1954 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001955
1956 TemplateTypeParmType *TypeCheck
1957 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1958 assert(!TypeCheck && "Template type parameter canonical type broken");
1959 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001960 } else
John McCall6b304a02009-09-24 23:30:46 +00001961 TypeParm = new (*this, TypeAlignment)
1962 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001963
1964 Types.push_back(TypeParm);
1965 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1966
1967 return QualType(TypeParm, 0);
1968}
1969
John McCall3cb0ebd2010-03-10 03:28:59 +00001970TypeSourceInfo *
1971ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1972 SourceLocation NameLoc,
1973 const TemplateArgumentListInfo &Args,
1974 QualType CanonType) {
1975 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1976
1977 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1978 TemplateSpecializationTypeLoc TL
1979 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1980 TL.setTemplateNameLoc(NameLoc);
1981 TL.setLAngleLoc(Args.getLAngleLoc());
1982 TL.setRAngleLoc(Args.getRAngleLoc());
1983 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1984 TL.setArgLocInfo(i, Args[i].getLocInfo());
1985 return DI;
1986}
1987
Mike Stump1eb44332009-09-09 15:08:12 +00001988QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001989ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00001990 const TemplateArgumentListInfo &Args,
John McCall71d74bc2010-06-13 09:25:03 +00001991 QualType Canon) {
John McCalld5532b62009-11-23 01:53:49 +00001992 unsigned NumArgs = Args.size();
1993
John McCall833ca992009-10-29 08:12:44 +00001994 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1995 ArgVec.reserve(NumArgs);
1996 for (unsigned i = 0; i != NumArgs; ++i)
1997 ArgVec.push_back(Args[i].getArgument());
1998
John McCall31f17ec2010-04-27 00:57:59 +00001999 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00002000 Canon);
John McCall833ca992009-10-29 08:12:44 +00002001}
2002
2003QualType
2004ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002005 const TemplateArgument *Args,
2006 unsigned NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00002007 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00002008 if (!Canon.isNull())
2009 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002010 else
2011 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregorfc705b82009-02-26 22:19:44 +00002012
Douglas Gregor1275ae02009-07-28 23:00:59 +00002013 // Allocate the (non-canonical) template specialization type, but don't
2014 // try to unique it: these types typically have location information that
2015 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00002016 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00002017 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00002018 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00002019 TemplateSpecializationType *Spec
John McCallef990012010-06-11 11:07:21 +00002020 = new (Mem) TemplateSpecializationType(Template,
John McCall31f17ec2010-04-27 00:57:59 +00002021 Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00002022 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Douglas Gregor55f6b142009-02-09 18:46:07 +00002024 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00002025 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002026}
2027
Mike Stump1eb44332009-09-09 15:08:12 +00002028QualType
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002029ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2030 const TemplateArgument *Args,
2031 unsigned NumArgs) {
2032 // Build the canonical template specialization type.
2033 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2034 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2035 CanonArgs.reserve(NumArgs);
2036 for (unsigned I = 0; I != NumArgs; ++I)
2037 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2038
2039 // Determine whether this canonical template specialization type already
2040 // exists.
2041 llvm::FoldingSetNodeID ID;
2042 TemplateSpecializationType::Profile(ID, CanonTemplate,
2043 CanonArgs.data(), NumArgs, *this);
2044
2045 void *InsertPos = 0;
2046 TemplateSpecializationType *Spec
2047 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2048
2049 if (!Spec) {
2050 // Allocate a new canonical template specialization type.
2051 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2052 sizeof(TemplateArgument) * NumArgs),
2053 TypeAlignment);
2054 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2055 CanonArgs.data(), NumArgs,
2056 QualType());
2057 Types.push_back(Spec);
2058 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2059 }
2060
2061 assert(Spec->isDependentType() &&
2062 "Non-dependent template-id type must have a canonical type");
2063 return QualType(Spec, 0);
2064}
2065
2066QualType
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002067ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2068 NestedNameSpecifier *NNS,
2069 QualType NamedType) {
Douglas Gregore4e5b052009-03-19 00:18:19 +00002070 llvm::FoldingSetNodeID ID;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002071 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002072
2073 void *InsertPos = 0;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002074 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002075 if (T)
2076 return QualType(T, 0);
2077
Douglas Gregor789b1f62010-02-04 18:10:26 +00002078 QualType Canon = NamedType;
2079 if (!Canon.isCanonical()) {
2080 Canon = getCanonicalType(NamedType);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002081 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2082 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregor789b1f62010-02-04 18:10:26 +00002083 (void)CheckT;
2084 }
2085
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002086 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002087 Types.push_back(T);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002088 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002089 return QualType(T, 0);
2090}
2091
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002092QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2093 NestedNameSpecifier *NNS,
2094 const IdentifierInfo *Name,
2095 QualType Canon) {
Douglas Gregord57959a2009-03-27 23:10:48 +00002096 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2097
2098 if (Canon.isNull()) {
2099 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002100 ElaboratedTypeKeyword CanonKeyword = Keyword;
2101 if (Keyword == ETK_None)
2102 CanonKeyword = ETK_Typename;
2103
2104 if (CanonNNS != NNS || CanonKeyword != Keyword)
2105 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002106 }
2107
2108 llvm::FoldingSetNodeID ID;
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002109 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002110
2111 void *InsertPos = 0;
Douglas Gregor4714c122010-03-31 17:34:00 +00002112 DependentNameType *T
2113 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregord57959a2009-03-27 23:10:48 +00002114 if (T)
2115 return QualType(T, 0);
2116
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002117 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregord57959a2009-03-27 23:10:48 +00002118 Types.push_back(T);
Douglas Gregor4714c122010-03-31 17:34:00 +00002119 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002120 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00002121}
2122
Mike Stump1eb44332009-09-09 15:08:12 +00002123QualType
John McCall33500952010-06-11 00:33:02 +00002124ASTContext::getDependentTemplateSpecializationType(
2125 ElaboratedTypeKeyword Keyword,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002126 NestedNameSpecifier *NNS,
John McCall33500952010-06-11 00:33:02 +00002127 const IdentifierInfo *Name,
2128 const TemplateArgumentListInfo &Args) {
2129 // TODO: avoid this copy
2130 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2131 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2132 ArgCopy.push_back(Args[I].getArgument());
2133 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2134 ArgCopy.size(),
2135 ArgCopy.data());
2136}
2137
2138QualType
2139ASTContext::getDependentTemplateSpecializationType(
2140 ElaboratedTypeKeyword Keyword,
2141 NestedNameSpecifier *NNS,
2142 const IdentifierInfo *Name,
2143 unsigned NumArgs,
2144 const TemplateArgument *Args) {
Douglas Gregor17343172009-04-01 00:28:59 +00002145 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2146
Douglas Gregor789b1f62010-02-04 18:10:26 +00002147 llvm::FoldingSetNodeID ID;
John McCall33500952010-06-11 00:33:02 +00002148 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2149 Name, NumArgs, Args);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002150
2151 void *InsertPos = 0;
John McCall33500952010-06-11 00:33:02 +00002152 DependentTemplateSpecializationType *T
2153 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002154 if (T)
2155 return QualType(T, 0);
2156
John McCall33500952010-06-11 00:33:02 +00002157 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002158
John McCall33500952010-06-11 00:33:02 +00002159 ElaboratedTypeKeyword CanonKeyword = Keyword;
2160 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2161
2162 bool AnyNonCanonArgs = false;
2163 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2164 for (unsigned I = 0; I != NumArgs; ++I) {
2165 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2166 if (!CanonArgs[I].structurallyEquals(Args[I]))
2167 AnyNonCanonArgs = true;
Douglas Gregor17343172009-04-01 00:28:59 +00002168 }
2169
John McCall33500952010-06-11 00:33:02 +00002170 QualType Canon;
2171 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2172 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2173 Name, NumArgs,
2174 CanonArgs.data());
2175
2176 // Find the insert position again.
2177 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2178 }
2179
2180 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2181 sizeof(TemplateArgument) * NumArgs),
2182 TypeAlignment);
John McCallef990012010-06-11 11:07:21 +00002183 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCall33500952010-06-11 00:33:02 +00002184 Name, NumArgs, Args, Canon);
Douglas Gregor17343172009-04-01 00:28:59 +00002185 Types.push_back(T);
John McCall33500952010-06-11 00:33:02 +00002186 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002187 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002188}
2189
Chris Lattner88cb27a2008-04-07 04:56:42 +00002190/// CmpProtocolNames - Comparison predicate for sorting protocols
2191/// alphabetically.
2192static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2193 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002194 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00002195}
2196
John McCallc12c5bb2010-05-15 11:32:37 +00002197static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCall54e14c42009-10-22 22:37:11 +00002198 unsigned NumProtocols) {
2199 if (NumProtocols == 0) return true;
2200
2201 for (unsigned i = 1; i != NumProtocols; ++i)
2202 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2203 return false;
2204 return true;
2205}
2206
2207static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00002208 unsigned &NumProtocols) {
2209 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Chris Lattner88cb27a2008-04-07 04:56:42 +00002211 // Sort protocols, keyed by name.
2212 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2213
2214 // Remove duplicates.
2215 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2216 NumProtocols = ProtocolsEnd-Protocols;
2217}
2218
John McCallc12c5bb2010-05-15 11:32:37 +00002219QualType ASTContext::getObjCObjectType(QualType BaseType,
2220 ObjCProtocolDecl * const *Protocols,
2221 unsigned NumProtocols) {
2222 // If the base type is an interface and there aren't any protocols
2223 // to add, then the interface type will do just fine.
2224 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2225 return BaseType;
2226
2227 // Look in the folding set for an existing type.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002228 llvm::FoldingSetNodeID ID;
John McCallc12c5bb2010-05-15 11:32:37 +00002229 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002230 void *InsertPos = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002231 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2232 return QualType(QT, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002233
John McCallc12c5bb2010-05-15 11:32:37 +00002234 // Build the canonical type, which has the canonical base type and
2235 // a sorted-and-uniqued list of protocols.
John McCall54e14c42009-10-22 22:37:11 +00002236 QualType Canonical;
John McCallc12c5bb2010-05-15 11:32:37 +00002237 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2238 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2239 if (!ProtocolsSorted) {
Benjamin Kramer02379412010-04-27 17:12:11 +00002240 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2241 Protocols + NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002242 unsigned UniqueCount = NumProtocols;
2243
John McCall54e14c42009-10-22 22:37:11 +00002244 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCallc12c5bb2010-05-15 11:32:37 +00002245 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2246 &Sorted[0], UniqueCount);
John McCall54e14c42009-10-22 22:37:11 +00002247 } else {
John McCallc12c5bb2010-05-15 11:32:37 +00002248 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2249 Protocols, NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002250 }
2251
2252 // Regenerate InsertPos.
John McCallc12c5bb2010-05-15 11:32:37 +00002253 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2254 }
2255
2256 unsigned Size = sizeof(ObjCObjectTypeImpl);
2257 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2258 void *Mem = Allocate(Size, TypeAlignment);
2259 ObjCObjectTypeImpl *T =
2260 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2261
2262 Types.push_back(T);
2263 ObjCObjectTypes.InsertNode(T, InsertPos);
2264 return QualType(T, 0);
2265}
2266
2267/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2268/// the given object type.
2269QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2270 llvm::FoldingSetNodeID ID;
2271 ObjCObjectPointerType::Profile(ID, ObjectT);
2272
2273 void *InsertPos = 0;
2274 if (ObjCObjectPointerType *QT =
2275 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2276 return QualType(QT, 0);
2277
2278 // Find the canonical object type.
2279 QualType Canonical;
2280 if (!ObjectT.isCanonical()) {
2281 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2282
2283 // Regenerate InsertPos.
John McCall54e14c42009-10-22 22:37:11 +00002284 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2285 }
2286
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002287 // No match.
John McCallc12c5bb2010-05-15 11:32:37 +00002288 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2289 ObjCObjectPointerType *QType =
2290 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump1eb44332009-09-09 15:08:12 +00002291
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002292 Types.push_back(QType);
2293 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCallc12c5bb2010-05-15 11:32:37 +00002294 return QualType(QType, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002295}
Chris Lattner88cb27a2008-04-07 04:56:42 +00002296
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002297/// getObjCInterfaceType - Return the unique reference to the type for the
2298/// specified ObjC interface decl. The list of protocols is optional.
2299QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2300 if (Decl->TypeForDecl)
2301 return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002303 // FIXME: redeclarations?
2304 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2305 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2306 Decl->TypeForDecl = T;
2307 Types.push_back(T);
2308 return QualType(T, 0);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002309}
2310
Douglas Gregor72564e72009-02-26 23:50:07 +00002311/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2312/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002313/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002314/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002315/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002316QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002317 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002318 if (tofExpr->isTypeDependent()) {
2319 llvm::FoldingSetNodeID ID;
2320 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002321
Douglas Gregorb1975722009-07-30 23:18:24 +00002322 void *InsertPos = 0;
2323 DependentTypeOfExprType *Canon
2324 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2325 if (Canon) {
2326 // We already have a "canonical" version of an identical, dependent
2327 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002328 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002329 QualType((TypeOfExprType*)Canon, 0));
2330 }
2331 else {
2332 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002333 Canon
2334 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002335 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2336 toe = Canon;
2337 }
2338 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002339 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002340 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002341 }
Steve Naroff9752f252007-08-01 18:02:17 +00002342 Types.push_back(toe);
2343 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002344}
2345
Steve Naroff9752f252007-08-01 18:02:17 +00002346/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2347/// TypeOfType AST's. The only motivation to unique these nodes would be
2348/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002349/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002350/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002351QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002352 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002353 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002354 Types.push_back(tot);
2355 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002356}
2357
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002358/// getDecltypeForExpr - Given an expr, will return the decltype for that
2359/// expression, according to the rules in C++0x [dcl.type.simple]p4
2360static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002361 if (e->isTypeDependent())
2362 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002364 // If e is an id expression or a class member access, decltype(e) is defined
2365 // as the type of the entity named by e.
2366 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2367 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2368 return VD->getType();
2369 }
2370 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2371 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2372 return FD->getType();
2373 }
2374 // If e is a function call or an invocation of an overloaded operator,
2375 // (parentheses around e are ignored), decltype(e) is defined as the
2376 // return type of that function.
2377 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2378 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002379
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002380 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002381
2382 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002383 // defined as T&, otherwise decltype(e) is defined as T.
2384 if (e->isLvalue(Context) == Expr::LV_Valid)
2385 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002386
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002387 return T;
2388}
2389
Anders Carlsson395b4752009-06-24 19:06:50 +00002390/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2391/// DecltypeType AST's. The only motivation to unique these nodes would be
2392/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002393/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002394/// on canonical type's (which are always unique).
2395QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002396 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002397 if (e->isTypeDependent()) {
2398 llvm::FoldingSetNodeID ID;
2399 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002400
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002401 void *InsertPos = 0;
2402 DependentDecltypeType *Canon
2403 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2404 if (Canon) {
2405 // We already have a "canonical" version of an equivalent, dependent
2406 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002407 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002408 QualType((DecltypeType*)Canon, 0));
2409 }
2410 else {
2411 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002412 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002413 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2414 dt = Canon;
2415 }
2416 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002417 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002418 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002419 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002420 Types.push_back(dt);
2421 return QualType(dt, 0);
2422}
2423
Reid Spencer5f016e22007-07-11 17:01:13 +00002424/// getTagDeclType - Return the unique reference to the type for the
2425/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002426QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002427 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002428 // FIXME: What is the design on getTagDeclType when it requires casting
2429 // away const? mutable?
2430 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002431}
2432
Mike Stump1eb44332009-09-09 15:08:12 +00002433/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2434/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2435/// needs to agree with the definition in <stddef.h>.
Anders Carlssona3ccda52009-12-12 00:26:23 +00002436CanQualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002437 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002438}
2439
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002440/// getSignedWCharType - Return the type of "signed wchar_t".
2441/// Used when in C++, as a GCC extension.
2442QualType ASTContext::getSignedWCharType() const {
2443 // FIXME: derive from "Target" ?
2444 return WCharTy;
2445}
2446
2447/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2448/// Used when in C++, as a GCC extension.
2449QualType ASTContext::getUnsignedWCharType() const {
2450 // FIXME: derive from "Target" ?
2451 return UnsignedIntTy;
2452}
2453
Chris Lattner8b9023b2007-07-13 03:05:23 +00002454/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2455/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2456QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002457 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002458}
2459
Chris Lattnere6327742008-04-02 05:18:44 +00002460//===----------------------------------------------------------------------===//
2461// Type Operators
2462//===----------------------------------------------------------------------===//
2463
John McCall54e14c42009-10-22 22:37:11 +00002464CanQualType ASTContext::getCanonicalParamType(QualType T) {
2465 // Push qualifiers into arrays, and then discard any remaining
2466 // qualifiers.
2467 T = getCanonicalType(T);
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002468 T = getVariableArrayDecayedType(T);
John McCall54e14c42009-10-22 22:37:11 +00002469 const Type *Ty = T.getTypePtr();
John McCall54e14c42009-10-22 22:37:11 +00002470 QualType Result;
2471 if (isa<ArrayType>(Ty)) {
2472 Result = getArrayDecayedType(QualType(Ty,0));
2473 } else if (isa<FunctionType>(Ty)) {
2474 Result = getPointerType(QualType(Ty, 0));
2475 } else {
2476 Result = QualType(Ty, 0);
2477 }
2478
2479 return CanQualType::CreateUnsafe(Result);
2480}
2481
Chris Lattner77c96472008-04-06 22:41:35 +00002482/// getCanonicalType - Return the canonical (structural) type corresponding to
2483/// the specified potentially non-canonical type. The non-canonical version
2484/// of a type may have many "decorated" versions of types. Decorators can
2485/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2486/// to be free of any of these, allowing two canonical types to be compared
2487/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002488CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002489 QualifierCollector Quals;
2490 const Type *Ptr = Quals.strip(T);
2491 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002492
John McCall0953e762009-09-24 19:53:00 +00002493 // The canonical internal type will be the canonical type *except*
2494 // that we push type qualifiers down through array types.
2495
2496 // If there are no new qualifiers to push down, stop here.
2497 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002498 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002499
John McCall0953e762009-09-24 19:53:00 +00002500 // If the type qualifiers are on an array type, get the canonical
2501 // type of the array with the qualifiers applied to the element
2502 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002503 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2504 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002505 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002506
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002507 // Get the canonical version of the element with the extra qualifiers on it.
2508 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002509 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002510 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002512 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002513 return CanQualType::CreateUnsafe(
2514 getConstantArrayType(NewEltTy, CAT->getSize(),
2515 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002516 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002517 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002518 return CanQualType::CreateUnsafe(
2519 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002520 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002521
Douglas Gregor898574e2008-12-05 23:32:09 +00002522 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002523 return CanQualType::CreateUnsafe(
2524 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002525 DSAT->getSizeExpr() ?
2526 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002527 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002528 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor87a924e2009-10-30 22:56:57 +00002529 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor898574e2008-12-05 23:32:09 +00002530
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002531 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002532 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002533 VAT->getSizeExpr() ?
2534 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002535 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002536 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002537 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002538}
2539
Chandler Carruth28e318c2009-12-29 07:16:59 +00002540QualType ASTContext::getUnqualifiedArrayType(QualType T,
2541 Qualifiers &Quals) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002542 Quals = T.getQualifiers();
Douglas Gregor9dadd942010-05-17 18:45:21 +00002543 const ArrayType *AT = getAsArrayType(T);
2544 if (!AT) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002545 return T.getUnqualifiedType();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002546 }
2547
Chandler Carruth28e318c2009-12-29 07:16:59 +00002548 QualType Elt = AT->getElementType();
Zhongxing Xuc1ae0a82010-01-05 08:15:06 +00002549 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002550 if (Elt == UnqualElt)
2551 return T;
2552
Douglas Gregor9dadd942010-05-17 18:45:21 +00002553 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth28e318c2009-12-29 07:16:59 +00002554 return getConstantArrayType(UnqualElt, CAT->getSize(),
2555 CAT->getSizeModifier(), 0);
2556 }
2557
Douglas Gregor9dadd942010-05-17 18:45:21 +00002558 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth28e318c2009-12-29 07:16:59 +00002559 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2560 }
2561
Douglas Gregor9dadd942010-05-17 18:45:21 +00002562 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2563 return getVariableArrayType(UnqualElt,
2564 VAT->getSizeExpr() ?
2565 VAT->getSizeExpr()->Retain() : 0,
2566 VAT->getSizeModifier(),
2567 VAT->getIndexTypeCVRQualifiers(),
2568 VAT->getBracketsRange());
2569 }
2570
2571 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002572 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2573 DSAT->getSizeModifier(), 0,
2574 SourceRange());
2575}
2576
Douglas Gregor5a57efd2010-06-09 03:53:18 +00002577/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2578/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2579/// they point to and return true. If T1 and T2 aren't pointer types
2580/// or pointer-to-member types, or if they are not similar at this
2581/// level, returns false and leaves T1 and T2 unchanged. Top-level
2582/// qualifiers on T1 and T2 are ignored. This function will typically
2583/// be called in a loop that successively "unwraps" pointer and
2584/// pointer-to-member types to compare them at each level.
2585bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2586 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2587 *T2PtrType = T2->getAs<PointerType>();
2588 if (T1PtrType && T2PtrType) {
2589 T1 = T1PtrType->getPointeeType();
2590 T2 = T2PtrType->getPointeeType();
2591 return true;
2592 }
2593
2594 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2595 *T2MPType = T2->getAs<MemberPointerType>();
2596 if (T1MPType && T2MPType &&
2597 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2598 QualType(T2MPType->getClass(), 0))) {
2599 T1 = T1MPType->getPointeeType();
2600 T2 = T2MPType->getPointeeType();
2601 return true;
2602 }
2603
2604 if (getLangOptions().ObjC1) {
2605 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2606 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2607 if (T1OPType && T2OPType) {
2608 T1 = T1OPType->getPointeeType();
2609 T2 = T2OPType->getPointeeType();
2610 return true;
2611 }
2612 }
2613
2614 // FIXME: Block pointers, too?
2615
2616 return false;
2617}
2618
Abramo Bagnara25777432010-08-11 22:01:17 +00002619DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name,
2620 SourceLocation NameLoc) {
John McCall80ad16f2009-11-24 18:42:40 +00002621 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnara25777432010-08-11 22:01:17 +00002622 // DNInfo work in progress: CHECKME: what about DNLoc?
2623 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2624
John McCall80ad16f2009-11-24 18:42:40 +00002625 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002626 DeclarationName DName;
John McCall80ad16f2009-11-24 18:42:40 +00002627 if (DTN->isIdentifier()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002628 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2629 return DeclarationNameInfo(DName, NameLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002630 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002631 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2632 // DNInfo work in progress: FIXME: source locations?
2633 DeclarationNameLoc DNLoc;
2634 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2635 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2636 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002637 }
2638 }
2639
John McCall0bd6feb2009-12-02 08:04:21 +00002640 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2641 assert(Storage);
Abramo Bagnara25777432010-08-11 22:01:17 +00002642 // DNInfo work in progress: CHECKME: what about DNLoc?
2643 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002644}
2645
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002646TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor3e1274f2010-06-16 21:09:37 +00002647 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2648 if (TemplateTemplateParmDecl *TTP
2649 = dyn_cast<TemplateTemplateParmDecl>(Template))
2650 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2651
2652 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002653 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor3e1274f2010-06-16 21:09:37 +00002654 }
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002655
John McCall0bd6feb2009-12-02 08:04:21 +00002656 assert(!Name.getAsOverloadedTemplate());
Mike Stump1eb44332009-09-09 15:08:12 +00002657
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002658 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2659 assert(DTN && "Non-dependent template names must refer to template decls.");
2660 return DTN->CanonicalTemplateName;
2661}
2662
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002663bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2664 X = getCanonicalTemplateName(X);
2665 Y = getCanonicalTemplateName(Y);
2666 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2667}
2668
Mike Stump1eb44332009-09-09 15:08:12 +00002669TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002670ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2671 switch (Arg.getKind()) {
2672 case TemplateArgument::Null:
2673 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Douglas Gregor1275ae02009-07-28 23:00:59 +00002675 case TemplateArgument::Expression:
Douglas Gregor1275ae02009-07-28 23:00:59 +00002676 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002677
Douglas Gregor1275ae02009-07-28 23:00:59 +00002678 case TemplateArgument::Declaration:
John McCall833ca992009-10-29 08:12:44 +00002679 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Douglas Gregor788cd062009-11-11 01:00:40 +00002681 case TemplateArgument::Template:
2682 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2683
Douglas Gregor1275ae02009-07-28 23:00:59 +00002684 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002685 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002686 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002687
Douglas Gregor1275ae02009-07-28 23:00:59 +00002688 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00002689 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002690
Douglas Gregor1275ae02009-07-28 23:00:59 +00002691 case TemplateArgument::Pack: {
2692 // FIXME: Allocate in ASTContext
2693 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2694 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002695 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002696 AEnd = Arg.pack_end();
2697 A != AEnd; (void)++A, ++Idx)
2698 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002699
Douglas Gregor1275ae02009-07-28 23:00:59 +00002700 TemplateArgument Result;
2701 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2702 return Result;
2703 }
2704 }
2705
2706 // Silence GCC warning
2707 assert(false && "Unhandled template argument kind");
2708 return TemplateArgument();
2709}
2710
Douglas Gregord57959a2009-03-27 23:10:48 +00002711NestedNameSpecifier *
2712ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002713 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002714 return 0;
2715
2716 switch (NNS->getKind()) {
2717 case NestedNameSpecifier::Identifier:
2718 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002719 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002720 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2721 NNS->getAsIdentifier());
2722
2723 case NestedNameSpecifier::Namespace:
2724 // A namespace is canonical; build a nested-name-specifier with
2725 // this namespace and no prefix.
2726 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2727
2728 case NestedNameSpecifier::TypeSpec:
2729 case NestedNameSpecifier::TypeSpecWithTemplate: {
2730 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002731 return NestedNameSpecifier::Create(*this, 0,
2732 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002733 T.getTypePtr());
2734 }
2735
2736 case NestedNameSpecifier::Global:
2737 // The global specifier is canonical and unique.
2738 return NNS;
2739 }
2740
2741 // Required to silence a GCC warning
2742 return 0;
2743}
2744
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002745
2746const ArrayType *ASTContext::getAsArrayType(QualType T) {
2747 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00002748 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002749 // Handle the common positive case fast.
2750 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2751 return AT;
2752 }
Mike Stump1eb44332009-09-09 15:08:12 +00002753
John McCall0953e762009-09-24 19:53:00 +00002754 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002755 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002756 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002757 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002758
John McCall0953e762009-09-24 19:53:00 +00002759 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002760 // implements C99 6.7.3p8: "If the specification of an array type includes
2761 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002762
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002763 // If we get here, we either have type qualifiers on the type, or we have
2764 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002765 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002766
John McCall0953e762009-09-24 19:53:00 +00002767 QualifierCollector Qs;
2768 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002769
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002770 // If we have a simple case, just return now.
2771 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002772 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002773 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002775 // Otherwise, we have an array and we have qualifiers on it. Push the
2776 // qualifiers into the array element type and return a new array type.
2777 // Get the canonical version of the element with the extra qualifiers on it.
2778 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002779 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002780
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002781 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2782 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2783 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002784 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002785 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2786 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2787 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002788 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002789
Mike Stump1eb44332009-09-09 15:08:12 +00002790 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002791 = dyn_cast<DependentSizedArrayType>(ATy))
2792 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002793 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002794 DSAT->getSizeExpr() ?
2795 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002796 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002797 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002798 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002799
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002800 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002801 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002802 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002803 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002804 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002805 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002806 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002807}
2808
Chris Lattnere6327742008-04-02 05:18:44 +00002809/// getArrayDecayedType - Return the properly qualified result of decaying the
2810/// specified array type to a pointer. This operation is non-trivial when
2811/// handling typedefs etc. The canonical type of "T" must be an array type,
2812/// this returns a pointer to a properly qualified element of the array.
2813///
2814/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2815QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002816 // Get the element type with 'getAsArrayType' so that we don't lose any
2817 // typedefs in the element type of the array. This also handles propagation
2818 // of type qualifiers from the array type into the element type if present
2819 // (C99 6.7.3p8).
2820 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2821 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002822
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002823 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002824
2825 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002826 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002827}
2828
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002829QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002830 QualifierCollector Qs;
Benjamin Kramer02379412010-04-27 17:12:11 +00002831 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2832 QT = AT->getElementType();
2833 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002834}
2835
Anders Carlssonfbbce492009-09-25 01:23:32 +00002836QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2837 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Anders Carlssonfbbce492009-09-25 01:23:32 +00002839 if (const ArrayType *AT = getAsArrayType(ElemTy))
2840 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002841
Anders Carlsson6183a992008-12-21 03:44:36 +00002842 return ElemTy;
2843}
2844
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002845/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002846uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002847ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2848 uint64_t ElementCount = 1;
2849 do {
2850 ElementCount *= CA->getSize().getZExtValue();
2851 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2852 } while (CA);
2853 return ElementCount;
2854}
2855
Reid Spencer5f016e22007-07-11 17:01:13 +00002856/// getFloatingRank - Return a relative rank for floating point types.
2857/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002858static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002859 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002860 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002861
John McCall183700f2009-09-21 23:43:11 +00002862 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2863 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002864 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002865 case BuiltinType::Float: return FloatRank;
2866 case BuiltinType::Double: return DoubleRank;
2867 case BuiltinType::LongDouble: return LongDoubleRank;
2868 }
2869}
2870
Mike Stump1eb44332009-09-09 15:08:12 +00002871/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2872/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002873/// 'typeDomain' is a real floating point or complex type.
2874/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002875QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2876 QualType Domain) const {
2877 FloatingRank EltRank = getFloatingRank(Size);
2878 if (Domain->isComplexType()) {
2879 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002880 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002881 case FloatRank: return FloatComplexTy;
2882 case DoubleRank: return DoubleComplexTy;
2883 case LongDoubleRank: return LongDoubleComplexTy;
2884 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002885 }
Chris Lattner1361b112008-04-06 23:58:54 +00002886
2887 assert(Domain->isRealFloatingType() && "Unknown domain!");
2888 switch (EltRank) {
2889 default: assert(0 && "getFloatingRank(): illegal value for rank");
2890 case FloatRank: return FloatTy;
2891 case DoubleRank: return DoubleTy;
2892 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002893 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002894}
2895
Chris Lattner7cfeb082008-04-06 23:55:33 +00002896/// getFloatingTypeOrder - Compare the rank of the two specified floating
2897/// point types, ignoring the domain of the type (i.e. 'double' ==
2898/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002899/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002900int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2901 FloatingRank LHSR = getFloatingRank(LHS);
2902 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Chris Lattnera75cea32008-04-06 23:38:49 +00002904 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002905 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002906 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002907 return 1;
2908 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002909}
2910
Chris Lattnerf52ab252008-04-06 22:59:24 +00002911/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2912/// routine will assert if passed a built-in type that isn't an integer or enum,
2913/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002914unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002915 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002916 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00002917 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00002918
Eli Friedmana3426752009-07-05 23:44:27 +00002919 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2920 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2921
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002922 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2923 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2924
2925 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2926 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2927
Chris Lattnerf52ab252008-04-06 22:59:24 +00002928 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002929 default: assert(0 && "getIntegerRank(): not a built-in integer");
2930 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002931 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002932 case BuiltinType::Char_S:
2933 case BuiltinType::Char_U:
2934 case BuiltinType::SChar:
2935 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002936 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002937 case BuiltinType::Short:
2938 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002939 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002940 case BuiltinType::Int:
2941 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002942 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002943 case BuiltinType::Long:
2944 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002945 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002946 case BuiltinType::LongLong:
2947 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002948 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002949 case BuiltinType::Int128:
2950 case BuiltinType::UInt128:
2951 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002952 }
2953}
2954
Eli Friedman04e83572009-08-20 04:21:42 +00002955/// \brief Whether this is a promotable bitfield reference according
2956/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2957///
2958/// \returns the type this bit-field will promote to, or NULL if no
2959/// promotion occurs.
2960QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregorceafbde2010-05-24 20:13:53 +00002961 if (E->isTypeDependent() || E->isValueDependent())
2962 return QualType();
2963
Eli Friedman04e83572009-08-20 04:21:42 +00002964 FieldDecl *Field = E->getBitField();
2965 if (!Field)
2966 return QualType();
2967
2968 QualType FT = Field->getType();
2969
2970 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2971 uint64_t BitWidth = BitWidthAP.getZExtValue();
2972 uint64_t IntSize = getTypeSize(IntTy);
2973 // GCC extension compatibility: if the bit-field size is less than or equal
2974 // to the size of int, it gets promoted no matter what its type is.
2975 // For instance, unsigned long bf : 4 gets promoted to signed int.
2976 if (BitWidth < IntSize)
2977 return IntTy;
2978
2979 if (BitWidth == IntSize)
2980 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2981
2982 // Types bigger than int are not subject to promotions, and therefore act
2983 // like the base type.
2984 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2985 // is ridiculous.
2986 return QualType();
2987}
2988
Eli Friedmana95d7572009-08-19 07:44:53 +00002989/// getPromotedIntegerType - Returns the type that Promotable will
2990/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2991/// integer type.
2992QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2993 assert(!Promotable.isNull());
2994 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00002995 if (const EnumType *ET = Promotable->getAs<EnumType>())
2996 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00002997 if (Promotable->isSignedIntegerType())
2998 return IntTy;
2999 uint64_t PromotableSize = getTypeSize(Promotable);
3000 uint64_t IntSize = getTypeSize(IntTy);
3001 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3002 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3003}
3004
Mike Stump1eb44332009-09-09 15:08:12 +00003005/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00003006/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00003007/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003008int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00003009 Type *LHSC = getCanonicalType(LHS).getTypePtr();
3010 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00003011 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003012
Chris Lattnerf52ab252008-04-06 22:59:24 +00003013 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3014 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Chris Lattner7cfeb082008-04-06 23:55:33 +00003016 unsigned LHSRank = getIntegerRank(LHSC);
3017 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Chris Lattner7cfeb082008-04-06 23:55:33 +00003019 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3020 if (LHSRank == RHSRank) return 0;
3021 return LHSRank > RHSRank ? 1 : -1;
3022 }
Mike Stump1eb44332009-09-09 15:08:12 +00003023
Chris Lattner7cfeb082008-04-06 23:55:33 +00003024 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3025 if (LHSUnsigned) {
3026 // If the unsigned [LHS] type is larger, return it.
3027 if (LHSRank >= RHSRank)
3028 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00003029
Chris Lattner7cfeb082008-04-06 23:55:33 +00003030 // If the signed type can represent all values of the unsigned type, it
3031 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003032 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003033 return -1;
3034 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00003035
Chris Lattner7cfeb082008-04-06 23:55:33 +00003036 // If the unsigned [RHS] type is larger, return it.
3037 if (RHSRank >= LHSRank)
3038 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00003039
Chris Lattner7cfeb082008-04-06 23:55:33 +00003040 // If the signed type can represent all values of the unsigned type, it
3041 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003042 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003043 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00003044}
Anders Carlsson71993dd2007-08-17 05:31:46 +00003045
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003046static RecordDecl *
3047CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
3048 SourceLocation L, IdentifierInfo *Id) {
3049 if (Ctx.getLangOptions().CPlusPlus)
3050 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
3051 else
3052 return RecordDecl::Create(Ctx, TK, DC, L, Id);
3053}
3054
Mike Stump1eb44332009-09-09 15:08:12 +00003055// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00003056QualType ASTContext::getCFConstantStringType() {
3057 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003058 CFConstantStringTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003059 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003060 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00003061 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003062
Anders Carlssonf06273f2007-11-19 00:25:30 +00003063 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00003064
Anders Carlsson71993dd2007-08-17 05:31:46 +00003065 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00003066 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00003067 // int flags;
3068 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00003069 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00003070 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00003071 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00003072 FieldTypes[3] = LongTy;
3073
Anders Carlsson71993dd2007-08-17 05:31:46 +00003074 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00003075 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003076 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00003077 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003078 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003079 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003080 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003081 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003082 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003083 }
3084
Douglas Gregor838db382010-02-11 01:19:42 +00003085 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00003086 }
Mike Stump1eb44332009-09-09 15:08:12 +00003087
Anders Carlsson71993dd2007-08-17 05:31:46 +00003088 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00003089}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003090
Douglas Gregor319ac892009-04-23 22:29:11 +00003091void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003092 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003093 assert(Rec && "Invalid CFConstantStringType");
3094 CFConstantStringTypeDecl = Rec->getDecl();
3095}
3096
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003097// getNSConstantStringType - Return the type used for constant NSStrings.
3098QualType ASTContext::getNSConstantStringType() {
3099 if (!NSConstantStringTypeDecl) {
3100 NSConstantStringTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003101 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003102 &Idents.get("__builtin_NSString"));
3103 NSConstantStringTypeDecl->startDefinition();
3104
3105 QualType FieldTypes[3];
3106
3107 // const int *isa;
3108 FieldTypes[0] = getPointerType(IntTy.withConst());
3109 // const char *str;
3110 FieldTypes[1] = getPointerType(CharTy.withConst());
3111 // unsigned int length;
3112 FieldTypes[2] = UnsignedIntTy;
3113
3114 // Create fields
3115 for (unsigned i = 0; i < 3; ++i) {
3116 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3117 SourceLocation(), 0,
3118 FieldTypes[i], /*TInfo=*/0,
3119 /*BitWidth=*/0,
3120 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003121 Field->setAccess(AS_public);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003122 NSConstantStringTypeDecl->addDecl(Field);
3123 }
3124
3125 NSConstantStringTypeDecl->completeDefinition();
3126 }
3127
3128 return getTagDeclType(NSConstantStringTypeDecl);
3129}
3130
3131void ASTContext::setNSConstantStringType(QualType T) {
3132 const RecordType *Rec = T->getAs<RecordType>();
3133 assert(Rec && "Invalid NSConstantStringType");
3134 NSConstantStringTypeDecl = Rec->getDecl();
3135}
3136
Mike Stump1eb44332009-09-09 15:08:12 +00003137QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003138 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00003139 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003140 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003141 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00003142 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00003143
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003144 QualType FieldTypes[] = {
3145 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00003146 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003147 getPointerType(UnsignedLongTy),
3148 getConstantArrayType(UnsignedLongTy,
3149 llvm::APInt(32, 5), ArrayType::Normal, 0)
3150 };
Mike Stump1eb44332009-09-09 15:08:12 +00003151
Douglas Gregor44b43212008-12-11 16:49:14 +00003152 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003153 FieldDecl *Field = FieldDecl::Create(*this,
3154 ObjCFastEnumerationStateTypeDecl,
3155 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003156 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003157 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003158 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003159 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003160 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003161 }
Mike Stump1eb44332009-09-09 15:08:12 +00003162
Douglas Gregor838db382010-02-11 01:19:42 +00003163 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003164 }
Mike Stump1eb44332009-09-09 15:08:12 +00003165
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003166 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3167}
3168
Mike Stumpadaaad32009-10-20 02:12:22 +00003169QualType ASTContext::getBlockDescriptorType() {
3170 if (BlockDescriptorType)
3171 return getTagDeclType(BlockDescriptorType);
3172
3173 RecordDecl *T;
3174 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003175 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003176 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00003177 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003178
3179 QualType FieldTypes[] = {
3180 UnsignedLongTy,
3181 UnsignedLongTy,
3182 };
3183
3184 const char *FieldNames[] = {
3185 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00003186 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00003187 };
3188
3189 for (size_t i = 0; i < 2; ++i) {
3190 FieldDecl *Field = FieldDecl::Create(*this,
3191 T,
3192 SourceLocation(),
3193 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003194 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00003195 /*BitWidth=*/0,
3196 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003197 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003198 T->addDecl(Field);
3199 }
3200
Douglas Gregor838db382010-02-11 01:19:42 +00003201 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003202
3203 BlockDescriptorType = T;
3204
3205 return getTagDeclType(BlockDescriptorType);
3206}
3207
3208void ASTContext::setBlockDescriptorType(QualType T) {
3209 const RecordType *Rec = T->getAs<RecordType>();
3210 assert(Rec && "Invalid BlockDescriptorType");
3211 BlockDescriptorType = Rec->getDecl();
3212}
3213
Mike Stump083c25e2009-10-22 00:49:09 +00003214QualType ASTContext::getBlockDescriptorExtendedType() {
3215 if (BlockDescriptorExtendedType)
3216 return getTagDeclType(BlockDescriptorExtendedType);
3217
3218 RecordDecl *T;
3219 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003220 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003221 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00003222 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003223
3224 QualType FieldTypes[] = {
3225 UnsignedLongTy,
3226 UnsignedLongTy,
3227 getPointerType(VoidPtrTy),
3228 getPointerType(VoidPtrTy)
3229 };
3230
3231 const char *FieldNames[] = {
3232 "reserved",
3233 "Size",
3234 "CopyFuncPtr",
3235 "DestroyFuncPtr"
3236 };
3237
3238 for (size_t i = 0; i < 4; ++i) {
3239 FieldDecl *Field = FieldDecl::Create(*this,
3240 T,
3241 SourceLocation(),
3242 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003243 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00003244 /*BitWidth=*/0,
3245 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003246 Field->setAccess(AS_public);
Mike Stump083c25e2009-10-22 00:49:09 +00003247 T->addDecl(Field);
3248 }
3249
Douglas Gregor838db382010-02-11 01:19:42 +00003250 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003251
3252 BlockDescriptorExtendedType = T;
3253
3254 return getTagDeclType(BlockDescriptorExtendedType);
3255}
3256
3257void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3258 const RecordType *Rec = T->getAs<RecordType>();
3259 assert(Rec && "Invalid BlockDescriptorType");
3260 BlockDescriptorExtendedType = Rec->getDecl();
3261}
3262
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003263bool ASTContext::BlockRequiresCopying(QualType Ty) {
3264 if (Ty->isBlockPointerType())
3265 return true;
3266 if (isObjCNSObjectType(Ty))
3267 return true;
3268 if (Ty->isObjCObjectPointerType())
3269 return true;
3270 return false;
3271}
3272
Daniel Dunbar4087f272010-08-17 22:39:59 +00003273QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003274 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003275 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003276 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003277 // unsigned int __flags;
3278 // unsigned int __size;
Eli Friedmana7e68452010-08-22 01:00:03 +00003279 // void *__copy_helper; // as needed
3280 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003281 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003282 // } *
3283
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003284 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3285
3286 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003287 llvm::SmallString<36> Name;
3288 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3289 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003290 RecordDecl *T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003291 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003292 &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003293 T->startDefinition();
3294 QualType Int32Ty = IntTy;
3295 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3296 QualType FieldTypes[] = {
3297 getPointerType(VoidPtrTy),
3298 getPointerType(getTagDeclType(T)),
3299 Int32Ty,
3300 Int32Ty,
3301 getPointerType(VoidPtrTy),
3302 getPointerType(VoidPtrTy),
3303 Ty
3304 };
3305
Daniel Dunbar4087f272010-08-17 22:39:59 +00003306 llvm::StringRef FieldNames[] = {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003307 "__isa",
3308 "__forwarding",
3309 "__flags",
3310 "__size",
3311 "__copy_helper",
3312 "__destroy_helper",
3313 DeclName,
3314 };
3315
3316 for (size_t i = 0; i < 7; ++i) {
3317 if (!HasCopyAndDispose && i >=4 && i <= 5)
3318 continue;
3319 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3320 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003321 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003322 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003323 Field->setAccess(AS_public);
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003324 T->addDecl(Field);
3325 }
3326
Douglas Gregor838db382010-02-11 01:19:42 +00003327 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003328
3329 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003330}
3331
3332
3333QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00003334 bool BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +00003335 llvm::SmallVectorImpl<const Expr *> &Layout) {
3336
Mike Stumpadaaad32009-10-20 02:12:22 +00003337 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003338 llvm::SmallString<36> Name;
3339 llvm::raw_svector_ostream(Name) << "__block_literal_"
3340 << ++UniqueBlockParmTypeID;
Mike Stumpadaaad32009-10-20 02:12:22 +00003341 RecordDecl *T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003342 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003343 &Idents.get(Name.str()));
John McCall5cfa0112010-02-05 01:33:36 +00003344 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003345 QualType FieldTypes[] = {
3346 getPointerType(VoidPtrTy),
3347 IntTy,
3348 IntTy,
3349 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00003350 (BlockHasCopyDispose ?
3351 getPointerType(getBlockDescriptorExtendedType()) :
3352 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00003353 };
3354
3355 const char *FieldNames[] = {
3356 "__isa",
3357 "__flags",
3358 "__reserved",
3359 "__FuncPtr",
3360 "__descriptor"
3361 };
3362
3363 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00003364 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003365 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003366 FieldTypes[i], /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003367 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003368 Field->setAccess(AS_public);
Mike Stumpea26cb52009-10-21 03:49:08 +00003369 T->addDecl(Field);
3370 }
3371
John McCallea1471e2010-05-20 01:18:31 +00003372 for (unsigned i = 0; i < Layout.size(); ++i) {
3373 const Expr *E = Layout[i];
Mike Stumpea26cb52009-10-21 03:49:08 +00003374
John McCallea1471e2010-05-20 01:18:31 +00003375 QualType FieldType = E->getType();
3376 IdentifierInfo *FieldName = 0;
3377 if (isa<CXXThisExpr>(E)) {
3378 FieldName = &Idents.get("this");
3379 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3380 const ValueDecl *D = BDRE->getDecl();
3381 FieldName = D->getIdentifier();
3382 if (BDRE->isByRef())
Daniel Dunbar4087f272010-08-17 22:39:59 +00003383 FieldType = BuildByRefType(D->getName(), FieldType);
John McCallea1471e2010-05-20 01:18:31 +00003384 } else {
3385 // Padding.
3386 assert(isa<ConstantArrayType>(FieldType) &&
3387 isa<DeclRefExpr>(E) &&
3388 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3389 "doesn't match characteristics of padding decl");
3390 }
Mike Stumpea26cb52009-10-21 03:49:08 +00003391
3392 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallea1471e2010-05-20 01:18:31 +00003393 FieldName, FieldType, /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003394 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003395 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003396 T->addDecl(Field);
3397 }
3398
Douglas Gregor838db382010-02-11 01:19:42 +00003399 T->completeDefinition();
Mike Stumpea26cb52009-10-21 03:49:08 +00003400
3401 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00003402}
3403
Douglas Gregor319ac892009-04-23 22:29:11 +00003404void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003405 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003406 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3407 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3408}
3409
Anders Carlssone8c49532007-10-29 06:33:42 +00003410// This returns true if a type has been typedefed to BOOL:
3411// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003412static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003413 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003414 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3415 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003416
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003417 return false;
3418}
3419
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003420/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003421/// purpose.
Ken Dyckaa8741a2010-01-11 19:19:56 +00003422CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck199c3d62010-01-11 17:06:35 +00003423 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003425 // Make all integer and enum types at least as large as an int
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003426 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003427 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003428 // Treat arrays as pointers, since that's how they're passed in.
3429 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003430 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003431 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003432}
3433
3434static inline
3435std::string charUnitsToString(const CharUnits &CU) {
3436 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003437}
3438
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003439/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall5e530af2009-11-17 19:33:30 +00003440/// declaration.
3441void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3442 std::string& S) {
3443 const BlockDecl *Decl = Expr->getBlockDecl();
3444 QualType BlockTy =
3445 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3446 // Encode result type.
John McCallc71a4912010-06-04 19:02:56 +00003447 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall5e530af2009-11-17 19:33:30 +00003448 // Compute size of all parameters.
3449 // Start with computing size of a pointer in number of bytes.
3450 // FIXME: There might(should) be a better way of doing this computation!
3451 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003452 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3453 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003454 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall5e530af2009-11-17 19:33:30 +00003455 E = Decl->param_end(); PI != E; ++PI) {
3456 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003457 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003458 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003459 ParmOffset += sz;
3460 }
3461 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003462 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003463 // Block pointer and offset.
3464 S += "@?0";
3465 ParmOffset = PtrSize;
3466
3467 // Argument types.
3468 ParmOffset = PtrSize;
3469 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3470 Decl->param_end(); PI != E; ++PI) {
3471 ParmVarDecl *PVDecl = *PI;
3472 QualType PType = PVDecl->getOriginalType();
3473 if (const ArrayType *AT =
3474 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3475 // Use array's original type only if it has known number of
3476 // elements.
3477 if (!isa<ConstantArrayType>(AT))
3478 PType = PVDecl->getType();
3479 } else if (PType->isFunctionType())
3480 PType = PVDecl->getType();
3481 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003482 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003483 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003484 }
3485}
3486
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003487/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003488/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003489void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003490 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003491 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003492 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003493 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003494 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003495 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003496 // Compute size of all parameters.
3497 // Start with computing size of a pointer in number of bytes.
3498 // FIXME: There might(should) be a better way of doing this computation!
3499 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003500 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003501 // The first two arguments (self and _cmd) are pointers; account for
3502 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003503 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003504 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003505 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003506 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003507 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003508 assert (sz.isPositive() &&
3509 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003510 ParmOffset += sz;
3511 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003512 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003513 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003514 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003515
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003516 // Argument types.
3517 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003518 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003519 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003520 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003521 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003522 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003523 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3524 // Use array's original type only if it has known number of
3525 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003526 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003527 PType = PVDecl->getType();
3528 } else if (PType->isFunctionType())
3529 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003530 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003531 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003532 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003533 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003534 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003535 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003536 }
3537}
3538
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003539/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003540/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003541/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3542/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003543/// Property attributes are stored as a comma-delimited C string. The simple
3544/// attributes readonly and bycopy are encoded as single characters. The
3545/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3546/// encoded as single characters, followed by an identifier. Property types
3547/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003548/// these attributes are defined by the following enumeration:
3549/// @code
3550/// enum PropertyAttributes {
3551/// kPropertyReadOnly = 'R', // property is read-only.
3552/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3553/// kPropertyByref = '&', // property is a reference to the value last assigned
3554/// kPropertyDynamic = 'D', // property is dynamic
3555/// kPropertyGetter = 'G', // followed by getter selector name
3556/// kPropertySetter = 'S', // followed by setter selector name
3557/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3558/// kPropertyType = 't' // followed by old-style type encoding.
3559/// kPropertyWeak = 'W' // 'weak' property
3560/// kPropertyStrong = 'P' // property GC'able
3561/// kPropertyNonAtomic = 'N' // property non-atomic
3562/// };
3563/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003564void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003565 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003566 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003567 // Collect information from the property implementation decl(s).
3568 bool Dynamic = false;
3569 ObjCPropertyImplDecl *SynthesizePID = 0;
3570
3571 // FIXME: Duplicated code due to poor abstraction.
3572 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003573 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003574 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3575 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003576 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003577 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003578 ObjCPropertyImplDecl *PID = *i;
3579 if (PID->getPropertyDecl() == PD) {
3580 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3581 Dynamic = true;
3582 } else {
3583 SynthesizePID = PID;
3584 }
3585 }
3586 }
3587 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003588 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003589 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003590 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003591 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003592 ObjCPropertyImplDecl *PID = *i;
3593 if (PID->getPropertyDecl() == PD) {
3594 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3595 Dynamic = true;
3596 } else {
3597 SynthesizePID = PID;
3598 }
3599 }
Mike Stump1eb44332009-09-09 15:08:12 +00003600 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003601 }
3602 }
3603
3604 // FIXME: This is not very efficient.
3605 S = "T";
3606
3607 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003608 // GCC has some special rules regarding encoding of properties which
3609 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003610 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003611 true /* outermost type */,
3612 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003613
3614 if (PD->isReadOnly()) {
3615 S += ",R";
3616 } else {
3617 switch (PD->getSetterKind()) {
3618 case ObjCPropertyDecl::Assign: break;
3619 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003620 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003621 }
3622 }
3623
3624 // It really isn't clear at all what this means, since properties
3625 // are "dynamic by default".
3626 if (Dynamic)
3627 S += ",D";
3628
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003629 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3630 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003631
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003632 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3633 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003634 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003635 }
3636
3637 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3638 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003639 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003640 }
3641
3642 if (SynthesizePID) {
3643 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3644 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003645 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003646 }
3647
3648 // FIXME: OBJCGC: weak & strong
3649}
3650
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003651/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003652/// Another legacy compatibility encoding: 32-bit longs are encoded as
3653/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003654/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3655///
3656void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003657 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003658 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003659 if (BT->getKind() == BuiltinType::ULong &&
3660 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003661 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003662 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003663 if (BT->getKind() == BuiltinType::Long &&
3664 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003665 PointeeTy = IntTy;
3666 }
3667 }
3668}
3669
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003670void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003671 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003672 // We follow the behavior of gcc, expanding structures which are
3673 // directly pointed to, and expanding embedded structures. Note that
3674 // these rules are sufficient to prevent recursive encoding of the
3675 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003676 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003677 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003678}
3679
David Chisnall64fd7e82010-06-04 01:10:52 +00003680static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3681 switch (T->getAs<BuiltinType>()->getKind()) {
3682 default: assert(0 && "Unhandled builtin type kind");
3683 case BuiltinType::Void: return 'v';
3684 case BuiltinType::Bool: return 'B';
3685 case BuiltinType::Char_U:
3686 case BuiltinType::UChar: return 'C';
3687 case BuiltinType::UShort: return 'S';
3688 case BuiltinType::UInt: return 'I';
3689 case BuiltinType::ULong:
3690 return
3691 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3692 case BuiltinType::UInt128: return 'T';
3693 case BuiltinType::ULongLong: return 'Q';
3694 case BuiltinType::Char_S:
3695 case BuiltinType::SChar: return 'c';
3696 case BuiltinType::Short: return 's';
John McCall24da7092010-06-11 10:11:05 +00003697 case BuiltinType::WChar:
David Chisnall64fd7e82010-06-04 01:10:52 +00003698 case BuiltinType::Int: return 'i';
3699 case BuiltinType::Long:
3700 return
3701 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3702 case BuiltinType::LongLong: return 'q';
3703 case BuiltinType::Int128: return 't';
3704 case BuiltinType::Float: return 'f';
3705 case BuiltinType::Double: return 'd';
Daniel Dunbar3a0be842010-10-11 21:13:48 +00003706 case BuiltinType::LongDouble: return 'D';
David Chisnall64fd7e82010-06-04 01:10:52 +00003707 }
3708}
3709
Mike Stump1eb44332009-09-09 15:08:12 +00003710static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnall64fd7e82010-06-04 01:10:52 +00003711 QualType T, const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003712 const Expr *E = FD->getBitWidth();
3713 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3714 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003715 S += 'b';
David Chisnall64fd7e82010-06-04 01:10:52 +00003716 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3717 // The GNU runtime requires more information; bitfields are encoded as b,
3718 // then the offset (in bits) of the first element, then the type of the
3719 // bitfield, then the size in bits. For example, in this structure:
3720 //
3721 // struct
3722 // {
3723 // int integer;
3724 // int flags:2;
3725 // };
3726 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3727 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3728 // information is not especially sensible, but we're stuck with it for
3729 // compatibility with GCC, although providing it breaks anything that
3730 // actually uses runtime introspection and wants to work on both runtimes...
3731 if (!Ctx->getLangOptions().NeXTRuntime) {
3732 const RecordDecl *RD = FD->getParent();
3733 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3734 // FIXME: This same linear search is also used in ExprConstant - it might
3735 // be better if the FieldDecl stored its offset. We'd be increasing the
3736 // size of the object slightly, but saving some time every time it is used.
3737 unsigned i = 0;
3738 for (RecordDecl::field_iterator Field = RD->field_begin(),
3739 FieldEnd = RD->field_end();
3740 Field != FieldEnd; (void)++Field, ++i) {
3741 if (*Field == FD)
3742 break;
3743 }
3744 S += llvm::utostr(RL.getFieldOffset(i));
3745 S += ObjCEncodingForPrimitiveKind(Context, T);
3746 }
3747 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003748 S += llvm::utostr(N);
3749}
3750
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003751// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003752void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3753 bool ExpandPointedToStructures,
3754 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003755 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003756 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003757 bool EncodingProperty) {
David Chisnall64fd7e82010-06-04 01:10:52 +00003758 if (T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003759 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00003760 return EncodeBitField(this, S, T, FD);
3761 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003762 return;
3763 }
Mike Stump1eb44332009-09-09 15:08:12 +00003764
John McCall183700f2009-09-21 23:43:11 +00003765 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003766 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003767 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003768 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003769 return;
3770 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00003771
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003772 // encoding for pointer or r3eference types.
3773 QualType PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003774 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003775 if (PT->isObjCSelType()) {
3776 S += ':';
3777 return;
3778 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003779 PointeeTy = PT->getPointeeType();
3780 }
3781 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3782 PointeeTy = RT->getPointeeType();
3783 if (!PointeeTy.isNull()) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003784 bool isReadOnly = false;
3785 // For historical/compatibility reasons, the read-only qualifier of the
3786 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3787 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003788 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003789 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003790 if (OutermostType && T.isConstQualified()) {
3791 isReadOnly = true;
3792 S += 'r';
3793 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003794 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003795 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003796 while (P->getAs<PointerType>())
3797 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003798 if (P.isConstQualified()) {
3799 isReadOnly = true;
3800 S += 'r';
3801 }
3802 }
3803 if (isReadOnly) {
3804 // Another legacy compatibility encoding. Some ObjC qualifier and type
3805 // combinations need to be rearranged.
3806 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer02379412010-04-27 17:12:11 +00003807 if (llvm::StringRef(S).endswith("nr"))
3808 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003809 }
Mike Stump1eb44332009-09-09 15:08:12 +00003810
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003811 if (PointeeTy->isCharType()) {
3812 // char pointer types should be encoded as '*' unless it is a
3813 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003814 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003815 S += '*';
3816 return;
3817 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003818 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003819 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3820 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3821 S += '#';
3822 return;
3823 }
3824 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3825 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3826 S += '@';
3827 return;
3828 }
3829 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003830 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003831 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003832 getLegacyIntegralTypeEncoding(PointeeTy);
3833
Mike Stump1eb44332009-09-09 15:08:12 +00003834 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003835 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003836 return;
3837 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003838
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003839 if (const ArrayType *AT =
3840 // Ignore type qualifiers etc.
3841 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003842 if (isa<IncompleteArrayType>(AT)) {
3843 // Incomplete arrays are encoded as a pointer to the array element.
3844 S += '^';
3845
Mike Stump1eb44332009-09-09 15:08:12 +00003846 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003847 false, ExpandStructures, FD);
3848 } else {
3849 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003850
Anders Carlsson559a8332009-02-22 01:38:57 +00003851 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3852 S += llvm::utostr(CAT->getSize().getZExtValue());
3853 else {
3854 //Variable length arrays are encoded as a regular array with 0 elements.
3855 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3856 S += '0';
3857 }
Mike Stump1eb44332009-09-09 15:08:12 +00003858
3859 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003860 false, ExpandStructures, FD);
3861 S += ']';
3862 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003863 return;
3864 }
Mike Stump1eb44332009-09-09 15:08:12 +00003865
John McCall183700f2009-09-21 23:43:11 +00003866 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003867 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003868 return;
3869 }
Mike Stump1eb44332009-09-09 15:08:12 +00003870
Ted Kremenek6217b802009-07-29 21:53:49 +00003871 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003872 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003873 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003874 // Anonymous structures print as '?'
3875 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3876 S += II->getName();
Fariborz Jahanian6fb94392010-05-07 00:28:49 +00003877 if (ClassTemplateSpecializationDecl *Spec
3878 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3879 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3880 std::string TemplateArgsStr
3881 = TemplateSpecializationType::PrintTemplateArgumentList(
3882 TemplateArgs.getFlatArgumentList(),
3883 TemplateArgs.flat_size(),
3884 (*this).PrintingPolicy);
3885
3886 S += TemplateArgsStr;
3887 }
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003888 } else {
3889 S += '?';
3890 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003891 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003892 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003893 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3894 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003895 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003896 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003897 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003898 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003899 S += '"';
3900 }
Mike Stump1eb44332009-09-09 15:08:12 +00003901
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003902 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003903 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003904 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003905 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003906 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003907 QualType qt = Field->getType();
3908 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003909 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003910 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003911 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003912 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003913 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003914 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003915 return;
3916 }
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00003917
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003918 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003919 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00003920 EncodeBitField(this, S, T, FD);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003921 else
3922 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003923 return;
3924 }
Mike Stump1eb44332009-09-09 15:08:12 +00003925
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003926 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003927 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003928 return;
3929 }
Mike Stump1eb44332009-09-09 15:08:12 +00003930
John McCallc12c5bb2010-05-15 11:32:37 +00003931 // Ignore protocol qualifiers when mangling at this level.
3932 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3933 T = OT->getBaseType();
3934
John McCall0953e762009-09-24 19:53:00 +00003935 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003936 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003937 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003938 S += '{';
3939 const IdentifierInfo *II = OI->getIdentifier();
3940 S += II->getName();
3941 S += '=';
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003942 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
3943 DeepCollectObjCIvars(OI, true, Ivars);
3944 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
3945 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
3946 if (Field->isBitField())
3947 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003948 else
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003949 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003950 }
3951 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003952 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003953 }
Mike Stump1eb44332009-09-09 15:08:12 +00003954
John McCall183700f2009-09-21 23:43:11 +00003955 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003956 if (OPT->isObjCIdType()) {
3957 S += '@';
3958 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003959 }
Mike Stump1eb44332009-09-09 15:08:12 +00003960
Steve Naroff27d20a22009-10-28 22:03:49 +00003961 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3962 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3963 // Since this is a binary compatibility issue, need to consult with runtime
3964 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00003965 S += '#';
3966 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003967 }
Mike Stump1eb44332009-09-09 15:08:12 +00003968
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003969 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003970 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003971 ExpandPointedToStructures,
3972 ExpandStructures, FD);
3973 if (FD || EncodingProperty) {
3974 // Note that we do extended encoding of protocol qualifer list
3975 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003976 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003977 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3978 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003979 S += '<';
3980 S += (*I)->getNameAsString();
3981 S += '>';
3982 }
3983 S += '"';
3984 }
3985 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003986 }
Mike Stump1eb44332009-09-09 15:08:12 +00003987
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003988 QualType PointeeTy = OPT->getPointeeType();
3989 if (!EncodingProperty &&
3990 isa<TypedefType>(PointeeTy.getTypePtr())) {
3991 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003992 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003993 // {...};
3994 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003995 getObjCEncodingForTypeImpl(PointeeTy, S,
3996 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003997 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003998 return;
3999 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004000
4001 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00004002 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004003 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00004004 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00004005 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4006 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004007 S += '<';
4008 S += (*I)->getNameAsString();
4009 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00004010 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004011 S += '"';
4012 }
4013 return;
4014 }
Mike Stump1eb44332009-09-09 15:08:12 +00004015
John McCall532ec7b2010-05-17 23:56:34 +00004016 // gcc just blithely ignores member pointers.
4017 // TODO: maybe there should be a mangling for these
4018 if (T->getAs<MemberPointerType>())
4019 return;
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00004020
4021 if (T->isVectorType()) {
4022 // This matches gcc's encoding, even though technically it is
4023 // insufficient.
4024 // FIXME. We should do a better job than gcc.
4025 return;
4026 }
4027
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004028 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004029}
4030
Mike Stump1eb44332009-09-09 15:08:12 +00004031void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00004032 std::string& S) const {
4033 if (QT & Decl::OBJC_TQ_In)
4034 S += 'n';
4035 if (QT & Decl::OBJC_TQ_Inout)
4036 S += 'N';
4037 if (QT & Decl::OBJC_TQ_Out)
4038 S += 'o';
4039 if (QT & Decl::OBJC_TQ_Bycopy)
4040 S += 'O';
4041 if (QT & Decl::OBJC_TQ_Byref)
4042 S += 'R';
4043 if (QT & Decl::OBJC_TQ_Oneway)
4044 S += 'V';
4045}
4046
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004047void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004048 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004049
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004050 BuiltinVaListType = T;
4051}
4052
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004053void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004054 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00004055}
4056
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004057void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00004058 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00004059}
4060
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004061void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004062 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00004063}
4064
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004065void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004066 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00004067}
4068
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004069void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00004070 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00004071 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004072
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004073 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00004074}
4075
John McCall0bd6feb2009-12-02 08:04:21 +00004076/// \brief Retrieve the template name that corresponds to a non-empty
4077/// lookup.
John McCalleec51cf2010-01-20 00:46:10 +00004078TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4079 UnresolvedSetIterator End) {
John McCall0bd6feb2009-12-02 08:04:21 +00004080 unsigned size = End - Begin;
4081 assert(size > 1 && "set is not overloaded!");
4082
4083 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4084 size * sizeof(FunctionTemplateDecl*));
4085 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4086
4087 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00004088 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00004089 NamedDecl *D = *I;
4090 assert(isa<FunctionTemplateDecl>(D) ||
4091 (isa<UsingShadowDecl>(D) &&
4092 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4093 *Storage++ = D;
4094 }
4095
4096 return TemplateName(OT);
4097}
4098
Douglas Gregor7532dc62009-03-30 22:58:21 +00004099/// \brief Retrieve the template name that represents a qualified
4100/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00004101TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004102 bool TemplateKeyword,
4103 TemplateDecl *Template) {
Douglas Gregor789b1f62010-02-04 18:10:26 +00004104 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00004105 llvm::FoldingSetNodeID ID;
4106 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4107
4108 void *InsertPos = 0;
4109 QualifiedTemplateName *QTN =
4110 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4111 if (!QTN) {
4112 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4113 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4114 }
4115
4116 return TemplateName(QTN);
4117}
4118
4119/// \brief Retrieve the template name that represents a dependent
4120/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00004121TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004122 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004123 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00004124 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00004125
4126 llvm::FoldingSetNodeID ID;
4127 DependentTemplateName::Profile(ID, NNS, Name);
4128
4129 void *InsertPos = 0;
4130 DependentTemplateName *QTN =
4131 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4132
4133 if (QTN)
4134 return TemplateName(QTN);
4135
4136 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4137 if (CanonNNS == NNS) {
4138 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4139 } else {
4140 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4141 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004142 DependentTemplateName *CheckQTN =
4143 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4144 assert(!CheckQTN && "Dependent type name canonicalization broken");
4145 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00004146 }
4147
4148 DependentTemplateNames.InsertNode(QTN, InsertPos);
4149 return TemplateName(QTN);
4150}
4151
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004152/// \brief Retrieve the template name that represents a dependent
4153/// template name such as \c MetaFun::template operator+.
4154TemplateName
4155ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4156 OverloadedOperatorKind Operator) {
4157 assert((!NNS || NNS->isDependent()) &&
4158 "Nested name specifier must be dependent");
4159
4160 llvm::FoldingSetNodeID ID;
4161 DependentTemplateName::Profile(ID, NNS, Operator);
4162
4163 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00004164 DependentTemplateName *QTN
4165 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004166
4167 if (QTN)
4168 return TemplateName(QTN);
4169
4170 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4171 if (CanonNNS == NNS) {
4172 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4173 } else {
4174 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4175 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004176
4177 DependentTemplateName *CheckQTN
4178 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4179 assert(!CheckQTN && "Dependent template name canonicalization broken");
4180 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004181 }
4182
4183 DependentTemplateNames.InsertNode(QTN, InsertPos);
4184 return TemplateName(QTN);
4185}
4186
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004187/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00004188/// TargetInfo, produce the corresponding type. The unsigned @p Type
4189/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00004190CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004191 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00004192 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004193 case TargetInfo::SignedShort: return ShortTy;
4194 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4195 case TargetInfo::SignedInt: return IntTy;
4196 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4197 case TargetInfo::SignedLong: return LongTy;
4198 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4199 case TargetInfo::SignedLongLong: return LongLongTy;
4200 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4201 }
4202
4203 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00004204 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004205}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00004206
4207//===----------------------------------------------------------------------===//
4208// Type Predicates.
4209//===----------------------------------------------------------------------===//
4210
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004211/// isObjCNSObjectType - Return true if this is an NSObject object using
4212/// NSObject attribute on a c-style pointer type.
4213/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00004214/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004215///
4216bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4217 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4218 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00004219 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004220 return true;
4221 }
Mike Stump1eb44332009-09-09 15:08:12 +00004222 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004223}
4224
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004225/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4226/// garbage collection attribute.
4227///
John McCall0953e762009-09-24 19:53:00 +00004228Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4229 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004230 if (getLangOptions().ObjC1 &&
4231 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00004232 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004233 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00004234 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004235 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00004236 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00004237 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004238 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004239 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00004240 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004241 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00004242 // Non-pointers have none gc'able attribute regardless of the attribute
4243 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00004244 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004245 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004246 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00004247 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004248}
4249
Chris Lattner6ac46a42008-04-07 06:51:04 +00004250//===----------------------------------------------------------------------===//
4251// Type Compatibility Testing
4252//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00004253
Mike Stump1eb44332009-09-09 15:08:12 +00004254/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004255/// compatible.
4256static bool areCompatVectorTypes(const VectorType *LHS,
4257 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00004258 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00004259 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00004260 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00004261}
4262
Douglas Gregor255210e2010-08-06 10:14:59 +00004263bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4264 QualType SecondVec) {
4265 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4266 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4267
4268 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4269 return true;
4270
4271 // AltiVec vectors types are identical to equivalent GCC vector types
4272 const VectorType *First = FirstVec->getAs<VectorType>();
4273 const VectorType *Second = SecondVec->getAs<VectorType>();
4274 if ((((First->getAltiVecSpecific() == VectorType::AltiVec) &&
4275 (Second->getAltiVecSpecific() == VectorType::NotAltiVec)) ||
4276 ((First->getAltiVecSpecific() == VectorType::NotAltiVec) &&
4277 (Second->getAltiVecSpecific() == VectorType::AltiVec))) &&
4278 hasSameType(First->getElementType(), Second->getElementType()) &&
4279 (First->getNumElements() == Second->getNumElements()))
4280 return true;
4281
4282 return false;
4283}
4284
Steve Naroff4084c302009-07-23 01:01:38 +00004285//===----------------------------------------------------------------------===//
4286// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4287//===----------------------------------------------------------------------===//
4288
4289/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4290/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004291bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4292 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00004293 if (lProto == rProto)
4294 return true;
4295 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4296 E = rProto->protocol_end(); PI != E; ++PI)
4297 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4298 return true;
4299 return false;
4300}
4301
Steve Naroff4084c302009-07-23 01:01:38 +00004302/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4303/// return true if lhs's protocols conform to rhs's protocol; false
4304/// otherwise.
4305bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4306 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4307 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4308 return false;
4309}
4310
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004311/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4312/// Class<p1, ...>.
4313bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4314 QualType rhs) {
4315 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4316 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4317 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4318
4319 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4320 E = lhsQID->qual_end(); I != E; ++I) {
4321 bool match = false;
4322 ObjCProtocolDecl *lhsProto = *I;
4323 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4324 E = rhsOPT->qual_end(); J != E; ++J) {
4325 ObjCProtocolDecl *rhsProto = *J;
4326 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4327 match = true;
4328 break;
4329 }
4330 }
4331 if (!match)
4332 return false;
4333 }
4334 return true;
4335}
4336
Steve Naroff4084c302009-07-23 01:01:38 +00004337/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4338/// ObjCQualifiedIDType.
4339bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4340 bool compare) {
4341 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00004342 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004343 lhs->isObjCIdType() || lhs->isObjCClassType())
4344 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004345 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004346 rhs->isObjCIdType() || rhs->isObjCClassType())
4347 return true;
4348
4349 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00004350 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004351
Steve Naroff4084c302009-07-23 01:01:38 +00004352 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004353
Steve Naroff4084c302009-07-23 01:01:38 +00004354 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004355 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00004356 // make sure we check the class hierarchy.
4357 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4358 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4359 E = lhsQID->qual_end(); I != E; ++I) {
4360 // when comparing an id<P> on lhs with a static type on rhs,
4361 // see if static class implements all of id's protocols, directly or
4362 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004363 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00004364 return false;
4365 }
4366 }
4367 // If there are no qualifiers and no interface, we have an 'id'.
4368 return true;
4369 }
Mike Stump1eb44332009-09-09 15:08:12 +00004370 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004371 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4372 E = lhsQID->qual_end(); I != E; ++I) {
4373 ObjCProtocolDecl *lhsProto = *I;
4374 bool match = false;
4375
4376 // when comparing an id<P> on lhs with a static type on rhs,
4377 // see if static class implements all of id's protocols, directly or
4378 // through its super class and categories.
4379 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4380 E = rhsOPT->qual_end(); J != E; ++J) {
4381 ObjCProtocolDecl *rhsProto = *J;
4382 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4383 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4384 match = true;
4385 break;
4386 }
4387 }
Mike Stump1eb44332009-09-09 15:08:12 +00004388 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004389 // make sure we check the class hierarchy.
4390 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4391 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4392 E = lhsQID->qual_end(); I != E; ++I) {
4393 // when comparing an id<P> on lhs with a static type on rhs,
4394 // see if static class implements all of id's protocols, directly or
4395 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004396 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004397 match = true;
4398 break;
4399 }
4400 }
4401 }
4402 if (!match)
4403 return false;
4404 }
Mike Stump1eb44332009-09-09 15:08:12 +00004405
Steve Naroff4084c302009-07-23 01:01:38 +00004406 return true;
4407 }
Mike Stump1eb44332009-09-09 15:08:12 +00004408
Steve Naroff4084c302009-07-23 01:01:38 +00004409 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4410 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4411
Mike Stump1eb44332009-09-09 15:08:12 +00004412 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004413 lhs->getAsObjCInterfacePointerType()) {
4414 if (lhsOPT->qual_empty()) {
4415 bool match = false;
4416 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4417 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4418 E = rhsQID->qual_end(); I != E; ++I) {
Fariborz Jahaniand1909bb2010-08-09 18:21:43 +00004419 // when comparing an id<P> on rhs with a static type on lhs,
4420 // static class must implement all of id's protocols directly or
4421 // indirectly through its super class.
Fariborz Jahanian192b1462010-08-12 20:46:12 +00004422 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004423 match = true;
4424 break;
4425 }
4426 }
4427 if (!match)
4428 return false;
4429 }
4430 return true;
4431 }
Mike Stump1eb44332009-09-09 15:08:12 +00004432 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004433 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4434 E = lhsOPT->qual_end(); I != E; ++I) {
4435 ObjCProtocolDecl *lhsProto = *I;
4436 bool match = false;
4437
4438 // when comparing an id<P> on lhs with a static type on rhs,
4439 // see if static class implements all of id's protocols, directly or
4440 // through its super class and categories.
4441 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4442 E = rhsQID->qual_end(); J != E; ++J) {
4443 ObjCProtocolDecl *rhsProto = *J;
4444 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4445 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4446 match = true;
4447 break;
4448 }
4449 }
4450 if (!match)
4451 return false;
4452 }
4453 return true;
4454 }
4455 return false;
4456}
4457
Eli Friedman3d815e72008-08-22 00:56:42 +00004458/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004459/// compatible for assignment from RHS to LHS. This handles validation of any
4460/// protocol qualifiers on the LHS or RHS.
4461///
Steve Naroff14108da2009-07-10 23:34:53 +00004462bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4463 const ObjCObjectPointerType *RHSOPT) {
John McCallc12c5bb2010-05-15 11:32:37 +00004464 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4465 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4466
Steve Naroffde2e22d2009-07-15 18:40:39 +00004467 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCallc12c5bb2010-05-15 11:32:37 +00004468 if (LHS->isObjCUnqualifiedIdOrClass() ||
4469 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff14108da2009-07-10 23:34:53 +00004470 return true;
4471
John McCallc12c5bb2010-05-15 11:32:37 +00004472 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump1eb44332009-09-09 15:08:12 +00004473 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4474 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004475 false);
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004476
4477 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4478 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4479 QualType(RHSOPT,0));
4480
John McCallc12c5bb2010-05-15 11:32:37 +00004481 // If we have 2 user-defined types, fall into that path.
4482 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff4084c302009-07-23 01:01:38 +00004483 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004484
Steve Naroff4084c302009-07-23 01:01:38 +00004485 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004486}
4487
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004488/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4489/// for providing type-safty for objective-c pointers used to pass/return
4490/// arguments in block literals. When passed as arguments, passing 'A*' where
4491/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4492/// not OK. For the return type, the opposite is not OK.
4493bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4494 const ObjCObjectPointerType *LHSOPT,
4495 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004496 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004497 return true;
4498
4499 if (LHSOPT->isObjCBuiltinType()) {
4500 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4501 }
4502
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004503 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004504 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4505 QualType(RHSOPT,0),
4506 false);
4507
4508 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4509 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4510 if (LHS && RHS) { // We have 2 user-defined types.
4511 if (LHS != RHS) {
4512 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4513 return false;
4514 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4515 return true;
4516 }
4517 else
4518 return true;
4519 }
4520 return false;
4521}
4522
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004523/// getIntersectionOfProtocols - This routine finds the intersection of set
4524/// of protocols inherited from two distinct objective-c pointer objects.
4525/// It is used to build composite qualifier list of the composite type of
4526/// the conditional expression involving two objective-c pointer objects.
4527static
4528void getIntersectionOfProtocols(ASTContext &Context,
4529 const ObjCObjectPointerType *LHSOPT,
4530 const ObjCObjectPointerType *RHSOPT,
4531 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4532
John McCallc12c5bb2010-05-15 11:32:37 +00004533 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4534 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4535 assert(LHS->getInterface() && "LHS must have an interface base");
4536 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004537
4538 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4539 unsigned LHSNumProtocols = LHS->getNumProtocols();
4540 if (LHSNumProtocols > 0)
4541 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4542 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004543 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004544 Context.CollectInheritedProtocols(LHS->getInterface(),
4545 LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004546 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4547 LHSInheritedProtocols.end());
4548 }
4549
4550 unsigned RHSNumProtocols = RHS->getNumProtocols();
4551 if (RHSNumProtocols > 0) {
Dan Gohmancb421fa2010-04-19 16:39:44 +00004552 ObjCProtocolDecl **RHSProtocols =
4553 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004554 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4555 if (InheritedProtocolSet.count(RHSProtocols[i]))
4556 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4557 }
4558 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004559 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004560 Context.CollectInheritedProtocols(RHS->getInterface(),
4561 RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004562 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4563 RHSInheritedProtocols.begin(),
4564 E = RHSInheritedProtocols.end(); I != E; ++I)
4565 if (InheritedProtocolSet.count((*I)))
4566 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004567 }
4568}
4569
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004570/// areCommonBaseCompatible - Returns common base class of the two classes if
4571/// one found. Note that this is O'2 algorithm. But it will be called as the
4572/// last type comparison in a ?-exp of ObjC pointer types before a
4573/// warning is issued. So, its invokation is extremely rare.
4574QualType ASTContext::areCommonBaseCompatible(
John McCallc12c5bb2010-05-15 11:32:37 +00004575 const ObjCObjectPointerType *Lptr,
4576 const ObjCObjectPointerType *Rptr) {
4577 const ObjCObjectType *LHS = Lptr->getObjectType();
4578 const ObjCObjectType *RHS = Rptr->getObjectType();
4579 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4580 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4581 if (!LDecl || !RDecl)
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004582 return QualType();
4583
John McCallc12c5bb2010-05-15 11:32:37 +00004584 while ((LDecl = LDecl->getSuperClass())) {
4585 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004586 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCallc12c5bb2010-05-15 11:32:37 +00004587 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4588 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4589
4590 QualType Result = QualType(LHS, 0);
4591 if (!Protocols.empty())
4592 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4593 Result = getObjCObjectPointerType(Result);
4594 return Result;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004595 }
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004596 }
4597
4598 return QualType();
4599}
4600
John McCallc12c5bb2010-05-15 11:32:37 +00004601bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4602 const ObjCObjectType *RHS) {
4603 assert(LHS->getInterface() && "LHS is not an interface type");
4604 assert(RHS->getInterface() && "RHS is not an interface type");
4605
Chris Lattner6ac46a42008-04-07 06:51:04 +00004606 // Verify that the base decls are compatible: the RHS must be a subclass of
4607 // the LHS.
John McCallc12c5bb2010-05-15 11:32:37 +00004608 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner6ac46a42008-04-07 06:51:04 +00004609 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004610
Chris Lattner6ac46a42008-04-07 06:51:04 +00004611 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4612 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004613 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004614 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004615
Chris Lattner6ac46a42008-04-07 06:51:04 +00004616 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4617 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004618 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004619 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00004620
John McCallc12c5bb2010-05-15 11:32:37 +00004621 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4622 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004623 LHSPI != LHSPE; LHSPI++) {
4624 bool RHSImplementsProtocol = false;
4625
4626 // If the RHS doesn't implement the protocol on the left, the types
4627 // are incompatible.
John McCallc12c5bb2010-05-15 11:32:37 +00004628 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4629 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00004630 RHSPI != RHSPE; RHSPI++) {
4631 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004632 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00004633 break;
4634 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004635 }
4636 // FIXME: For better diagnostics, consider passing back the protocol name.
4637 if (!RHSImplementsProtocol)
4638 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004639 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004640 // The RHS implements all protocols listed on the LHS.
4641 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004642}
4643
Steve Naroff389bf462009-02-12 17:52:19 +00004644bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4645 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00004646 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4647 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004648
Steve Naroff14108da2009-07-10 23:34:53 +00004649 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00004650 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004651
4652 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4653 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00004654}
4655
Douglas Gregor569c3162010-08-07 11:51:51 +00004656bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
4657 return canAssignObjCInterfaces(
4658 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
4659 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
4660}
4661
Mike Stump1eb44332009-09-09 15:08:12 +00004662/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00004663/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00004664/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00004665/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor447234d2010-07-29 15:18:02 +00004666bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
4667 bool CompareUnqualified) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004668 if (getLangOptions().CPlusPlus)
4669 return hasSameType(LHS, RHS);
4670
Douglas Gregor447234d2010-07-29 15:18:02 +00004671 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman3d815e72008-08-22 00:56:42 +00004672}
4673
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004674bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4675 return !mergeTypes(LHS, RHS, true).isNull();
4676}
4677
Peter Collingbourne48466752010-10-24 18:30:18 +00004678/// mergeTransparentUnionType - if T is a transparent union type and a member
4679/// of T is compatible with SubType, return the merged type, else return
4680/// QualType()
4681QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
4682 bool OfBlockPointer,
4683 bool Unqualified) {
4684 if (const RecordType *UT = T->getAsUnionType()) {
4685 RecordDecl *UD = UT->getDecl();
4686 if (UD->hasAttr<TransparentUnionAttr>()) {
4687 for (RecordDecl::field_iterator it = UD->field_begin(),
4688 itend = UD->field_end(); it != itend; ++it) {
4689 QualType ET = it->getType();
4690 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
4691 if (!MT.isNull())
4692 return MT;
4693 }
4694 }
4695 }
4696
4697 return QualType();
4698}
4699
4700/// mergeFunctionArgumentTypes - merge two types which appear as function
4701/// argument types
4702QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
4703 bool OfBlockPointer,
4704 bool Unqualified) {
4705 // GNU extension: two types are compatible if they appear as a function
4706 // argument, one of the types is a transparent union type and the other
4707 // type is compatible with a union member
4708 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
4709 Unqualified);
4710 if (!lmerge.isNull())
4711 return lmerge;
4712
4713 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
4714 Unqualified);
4715 if (!rmerge.isNull())
4716 return rmerge;
4717
4718 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
4719}
4720
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004721QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor447234d2010-07-29 15:18:02 +00004722 bool OfBlockPointer,
4723 bool Unqualified) {
John McCall183700f2009-09-21 23:43:11 +00004724 const FunctionType *lbase = lhs->getAs<FunctionType>();
4725 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00004726 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4727 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00004728 bool allLTypes = true;
4729 bool allRTypes = true;
4730
4731 // Check return type
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004732 QualType retType;
4733 if (OfBlockPointer)
Douglas Gregor447234d2010-07-29 15:18:02 +00004734 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true,
4735 Unqualified);
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004736 else
Douglas Gregor447234d2010-07-29 15:18:02 +00004737 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(),
4738 false, Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004739 if (retType.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004740
4741 if (Unqualified)
4742 retType = retType.getUnqualifiedType();
4743
4744 CanQualType LRetType = getCanonicalType(lbase->getResultType());
4745 CanQualType RRetType = getCanonicalType(rbase->getResultType());
4746 if (Unqualified) {
4747 LRetType = LRetType.getUnqualifiedType();
4748 RRetType = RRetType.getUnqualifiedType();
4749 }
4750
4751 if (getCanonicalType(retType) != LRetType)
Chris Lattner61710852008-10-05 17:34:18 +00004752 allLTypes = false;
Douglas Gregor447234d2010-07-29 15:18:02 +00004753 if (getCanonicalType(retType) != RRetType)
Chris Lattner61710852008-10-05 17:34:18 +00004754 allRTypes = false;
Daniel Dunbar6a15c852010-04-28 16:20:58 +00004755 // FIXME: double check this
4756 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4757 // rbase->getRegParmAttr() != 0 &&
4758 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindola264ba482010-03-30 20:24:48 +00004759 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4760 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbar6a15c852010-04-28 16:20:58 +00004761 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4762 lbaseInfo.getRegParm();
4763 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4764 if (NoReturn != lbaseInfo.getNoReturn() ||
4765 RegParm != lbaseInfo.getRegParm())
4766 allLTypes = false;
4767 if (NoReturn != rbaseInfo.getNoReturn() ||
4768 RegParm != rbaseInfo.getRegParm())
4769 allRTypes = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00004770 CallingConv lcc = lbaseInfo.getCC();
4771 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004772 // Compatible functions must have compatible calling conventions
John McCall04a67a62010-02-05 21:31:56 +00004773 if (!isSameCallConv(lcc, rcc))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004774 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004775
Eli Friedman3d815e72008-08-22 00:56:42 +00004776 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00004777 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4778 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004779 unsigned lproto_nargs = lproto->getNumArgs();
4780 unsigned rproto_nargs = rproto->getNumArgs();
4781
4782 // Compatible functions must have the same number of arguments
4783 if (lproto_nargs != rproto_nargs)
4784 return QualType();
4785
4786 // Variadic and non-variadic functions aren't compatible
4787 if (lproto->isVariadic() != rproto->isVariadic())
4788 return QualType();
4789
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00004790 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4791 return QualType();
4792
Eli Friedman3d815e72008-08-22 00:56:42 +00004793 // Check argument compatibility
4794 llvm::SmallVector<QualType, 10> types;
4795 for (unsigned i = 0; i < lproto_nargs; i++) {
4796 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4797 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbourne48466752010-10-24 18:30:18 +00004798 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
4799 OfBlockPointer,
4800 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004801 if (argtype.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004802
4803 if (Unqualified)
4804 argtype = argtype.getUnqualifiedType();
4805
Eli Friedman3d815e72008-08-22 00:56:42 +00004806 types.push_back(argtype);
Douglas Gregor447234d2010-07-29 15:18:02 +00004807 if (Unqualified) {
4808 largtype = largtype.getUnqualifiedType();
4809 rargtype = rargtype.getUnqualifiedType();
4810 }
4811
Chris Lattner61710852008-10-05 17:34:18 +00004812 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4813 allLTypes = false;
4814 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4815 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00004816 }
4817 if (allLTypes) return lhs;
4818 if (allRTypes) return rhs;
4819 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00004820 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindola264ba482010-03-30 20:24:48 +00004821 false, false, 0, 0,
Rafael Espindola425ef722010-03-30 22:15:11 +00004822 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman3d815e72008-08-22 00:56:42 +00004823 }
4824
4825 if (lproto) allRTypes = false;
4826 if (rproto) allLTypes = false;
4827
Douglas Gregor72564e72009-02-26 23:50:07 +00004828 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00004829 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00004830 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004831 if (proto->isVariadic()) return QualType();
4832 // Check that the types are compatible with the types that
4833 // would result from default argument promotions (C99 6.7.5.3p15).
4834 // The only types actually affected are promotable integer
4835 // types and floats, which would be passed as a different
4836 // type depending on whether the prototype is visible.
4837 unsigned proto_nargs = proto->getNumArgs();
4838 for (unsigned i = 0; i < proto_nargs; ++i) {
4839 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00004840
4841 // Look at the promotion type of enum types, since that is the type used
4842 // to pass enum values.
4843 if (const EnumType *Enum = argTy->getAs<EnumType>())
4844 argTy = Enum->getDecl()->getPromotionType();
4845
Eli Friedman3d815e72008-08-22 00:56:42 +00004846 if (argTy->isPromotableIntegerType() ||
4847 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4848 return QualType();
4849 }
4850
4851 if (allLTypes) return lhs;
4852 if (allRTypes) return rhs;
4853 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00004854 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindola264ba482010-03-30 20:24:48 +00004855 proto->getTypeQuals(),
4856 false, false, 0, 0,
Rafael Espindola425ef722010-03-30 22:15:11 +00004857 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman3d815e72008-08-22 00:56:42 +00004858 }
4859
4860 if (allLTypes) return lhs;
4861 if (allRTypes) return rhs;
Rafael Espindola425ef722010-03-30 22:15:11 +00004862 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindola264ba482010-03-30 20:24:48 +00004863 return getFunctionNoProtoType(retType, Info);
Eli Friedman3d815e72008-08-22 00:56:42 +00004864}
4865
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004866QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor447234d2010-07-29 15:18:02 +00004867 bool OfBlockPointer,
4868 bool Unqualified) {
Bill Wendling43d69752007-12-03 07:33:35 +00004869 // C++ [expr]: If an expression initially has the type "reference to T", the
4870 // type is adjusted to "T" prior to any further analysis, the expression
4871 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004872 // expression is an lvalue unless the reference is an rvalue reference and
4873 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004874 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4875 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor447234d2010-07-29 15:18:02 +00004876
4877 if (Unqualified) {
4878 LHS = LHS.getUnqualifiedType();
4879 RHS = RHS.getUnqualifiedType();
4880 }
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004881
Eli Friedman3d815e72008-08-22 00:56:42 +00004882 QualType LHSCan = getCanonicalType(LHS),
4883 RHSCan = getCanonicalType(RHS);
4884
4885 // If two types are identical, they are compatible.
4886 if (LHSCan == RHSCan)
4887 return LHS;
4888
John McCall0953e762009-09-24 19:53:00 +00004889 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00004890 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4891 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00004892 if (LQuals != RQuals) {
4893 // If any of these qualifiers are different, we have a type
4894 // mismatch.
4895 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4896 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4897 return QualType();
4898
4899 // Exactly one GC qualifier difference is allowed: __strong is
4900 // okay if the other type has no GC qualifier but is an Objective
4901 // C object pointer (i.e. implicitly strong by default). We fix
4902 // this by pretending that the unqualified type was actually
4903 // qualified __strong.
4904 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4905 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4906 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4907
4908 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4909 return QualType();
4910
4911 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4912 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4913 }
4914 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4915 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4916 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004917 return QualType();
John McCall0953e762009-09-24 19:53:00 +00004918 }
4919
4920 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00004921
Eli Friedman852d63b2009-06-01 01:22:52 +00004922 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4923 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00004924
Chris Lattner1adb8832008-01-14 05:45:46 +00004925 // We want to consider the two function types to be the same for these
4926 // comparisons, just force one to the other.
4927 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4928 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00004929
4930 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00004931 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4932 LHSClass = Type::ConstantArray;
4933 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4934 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00004935
John McCallc12c5bb2010-05-15 11:32:37 +00004936 // ObjCInterfaces are just specialized ObjCObjects.
4937 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
4938 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
4939
Nate Begeman213541a2008-04-18 23:10:10 +00004940 // Canonicalize ExtVector -> Vector.
4941 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4942 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00004943
Chris Lattnera36a61f2008-04-07 05:43:21 +00004944 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004945 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00004946 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00004947 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00004948 // Compatibility is based on the underlying type, not the promotion
4949 // type.
John McCall183700f2009-09-21 23:43:11 +00004950 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004951 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4952 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004953 }
John McCall183700f2009-09-21 23:43:11 +00004954 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004955 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4956 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004957 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004958
Eli Friedman3d815e72008-08-22 00:56:42 +00004959 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004960 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004961
Steve Naroff4a746782008-01-09 22:43:08 +00004962 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004963 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00004964#define TYPE(Class, Base)
4965#define ABSTRACT_TYPE(Class, Base)
John McCallad5e7382010-03-01 23:49:17 +00004966#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +00004967#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4968#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4969#include "clang/AST/TypeNodes.def"
4970 assert(false && "Non-canonical and dependent types shouldn't get here");
4971 return QualType();
4972
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004973 case Type::LValueReference:
4974 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00004975 case Type::MemberPointer:
4976 assert(false && "C++ should never be in mergeTypes");
4977 return QualType();
4978
John McCallc12c5bb2010-05-15 11:32:37 +00004979 case Type::ObjCInterface:
Douglas Gregor72564e72009-02-26 23:50:07 +00004980 case Type::IncompleteArray:
4981 case Type::VariableArray:
4982 case Type::FunctionProto:
4983 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00004984 assert(false && "Types are eliminated above");
4985 return QualType();
4986
Chris Lattner1adb8832008-01-14 05:45:46 +00004987 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00004988 {
4989 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004990 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4991 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004992 if (Unqualified) {
4993 LHSPointee = LHSPointee.getUnqualifiedType();
4994 RHSPointee = RHSPointee.getUnqualifiedType();
4995 }
4996 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
4997 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004998 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00004999 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005000 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00005001 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005002 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005003 return getPointerType(ResultType);
5004 }
Steve Naroffc0febd52008-12-10 17:49:55 +00005005 case Type::BlockPointer:
5006 {
5007 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00005008 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5009 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005010 if (Unqualified) {
5011 LHSPointee = LHSPointee.getUnqualifiedType();
5012 RHSPointee = RHSPointee.getUnqualifiedType();
5013 }
5014 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5015 Unqualified);
Steve Naroffc0febd52008-12-10 17:49:55 +00005016 if (ResultType.isNull()) return QualType();
5017 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5018 return LHS;
5019 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5020 return RHS;
5021 return getBlockPointerType(ResultType);
5022 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005023 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00005024 {
5025 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5026 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5027 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5028 return QualType();
5029
5030 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5031 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005032 if (Unqualified) {
5033 LHSElem = LHSElem.getUnqualifiedType();
5034 RHSElem = RHSElem.getUnqualifiedType();
5035 }
5036
5037 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005038 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00005039 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5040 return LHS;
5041 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5042 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00005043 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5044 ArrayType::ArraySizeModifier(), 0);
5045 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5046 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005047 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5048 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00005049 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5050 return LHS;
5051 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5052 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005053 if (LVAT) {
5054 // FIXME: This isn't correct! But tricky to implement because
5055 // the array's size has to be the size of LHS, but the type
5056 // has to be different.
5057 return LHS;
5058 }
5059 if (RVAT) {
5060 // FIXME: This isn't correct! But tricky to implement because
5061 // the array's size has to be the size of RHS, but the type
5062 // has to be different.
5063 return RHS;
5064 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00005065 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5066 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005067 return getIncompleteArrayType(ResultType,
5068 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005069 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005070 case Type::FunctionNoProto:
Douglas Gregor447234d2010-07-29 15:18:02 +00005071 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregor72564e72009-02-26 23:50:07 +00005072 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00005073 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00005074 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00005075 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005076 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00005077 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00005078 case Type::Complex:
5079 // Distinct complex types are incompatible.
5080 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005081 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005082 // FIXME: The merged type should be an ExtVector!
John McCall1c471f32010-03-12 23:14:13 +00005083 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5084 RHSCan->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00005085 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00005086 return QualType();
John McCallc12c5bb2010-05-15 11:32:37 +00005087 case Type::ObjCObject: {
5088 // Check if the types are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005089 // FIXME: This should be type compatibility, e.g. whether
5090 // "LHS x; RHS x;" at global scope is legal.
John McCallc12c5bb2010-05-15 11:32:37 +00005091 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5092 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5093 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff5fd659d2009-02-21 16:18:07 +00005094 return LHS;
5095
Eli Friedman3d815e72008-08-22 00:56:42 +00005096 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00005097 }
Steve Naroff14108da2009-07-10 23:34:53 +00005098 case Type::ObjCObjectPointer: {
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005099 if (OfBlockPointer) {
5100 if (canAssignObjCInterfacesInBlockPointer(
5101 LHS->getAs<ObjCObjectPointerType>(),
5102 RHS->getAs<ObjCObjectPointerType>()))
5103 return LHS;
5104 return QualType();
5105 }
John McCall183700f2009-09-21 23:43:11 +00005106 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5107 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00005108 return LHS;
5109
Steve Naroffbc76dd02008-12-10 22:14:21 +00005110 return QualType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005111 }
Steve Naroffec0550f2007-10-15 20:41:53 +00005112 }
Douglas Gregor72564e72009-02-26 23:50:07 +00005113
5114 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00005115}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00005116
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005117/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5118/// 'RHS' attributes and returns the merged version; including for function
5119/// return types.
5120QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5121 QualType LHSCan = getCanonicalType(LHS),
5122 RHSCan = getCanonicalType(RHS);
5123 // If two types are identical, they are compatible.
5124 if (LHSCan == RHSCan)
5125 return LHS;
5126 if (RHSCan->isFunctionType()) {
5127 if (!LHSCan->isFunctionType())
5128 return QualType();
5129 QualType OldReturnType =
5130 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5131 QualType NewReturnType =
5132 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5133 QualType ResReturnType =
5134 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5135 if (ResReturnType.isNull())
5136 return QualType();
5137 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5138 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5139 // In either case, use OldReturnType to build the new function type.
5140 const FunctionType *F = LHS->getAs<FunctionType>();
5141 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
5142 FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
5143 QualType ResultType
5144 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
5145 FPT->getNumArgs(), FPT->isVariadic(),
5146 FPT->getTypeQuals(),
5147 FPT->hasExceptionSpec(),
5148 FPT->hasAnyExceptionSpec(),
5149 FPT->getNumExceptions(),
5150 FPT->exception_begin(),
5151 Info);
5152 return ResultType;
5153 }
5154 }
5155 return QualType();
5156 }
5157
5158 // If the qualifiers are different, the types can still be merged.
5159 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5160 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5161 if (LQuals != RQuals) {
5162 // If any of these qualifiers are different, we have a type mismatch.
5163 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5164 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5165 return QualType();
5166
5167 // Exactly one GC qualifier difference is allowed: __strong is
5168 // okay if the other type has no GC qualifier but is an Objective
5169 // C object pointer (i.e. implicitly strong by default). We fix
5170 // this by pretending that the unqualified type was actually
5171 // qualified __strong.
5172 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5173 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5174 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5175
5176 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5177 return QualType();
5178
5179 if (GC_L == Qualifiers::Strong)
5180 return LHS;
5181 if (GC_R == Qualifiers::Strong)
5182 return RHS;
5183 return QualType();
5184 }
5185
5186 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5187 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5188 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5189 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5190 if (ResQT == LHSBaseQT)
5191 return LHS;
5192 if (ResQT == RHSBaseQT)
5193 return RHS;
5194 }
5195 return QualType();
5196}
5197
Chris Lattner5426bf62008-04-07 07:01:58 +00005198//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00005199// Integer Predicates
5200//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00005201
Eli Friedmanad74a752008-06-28 06:23:08 +00005202unsigned ASTContext::getIntWidth(QualType T) {
John McCall842aef82009-12-09 09:09:27 +00005203 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00005204 T = ET->getDecl()->getIntegerType();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005205 if (T->isBooleanType())
5206 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00005207 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00005208 return (unsigned)getTypeSize(T);
5209}
5210
5211QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregorf6094622010-07-23 15:58:24 +00005212 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00005213
5214 // Turn <4 x signed int> -> <4 x unsigned int>
5215 if (const VectorType *VTy = T->getAs<VectorType>())
5216 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Chris Lattner788b0fd2010-06-23 06:00:24 +00005217 VTy->getNumElements(), VTy->getAltiVecSpecific());
Chris Lattner6a2b9262009-10-17 20:33:28 +00005218
5219 // For enums, we return the unsigned version of the base type.
5220 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00005221 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00005222
5223 const BuiltinType *BTy = T->getAs<BuiltinType>();
5224 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00005225 switch (BTy->getKind()) {
5226 case BuiltinType::Char_S:
5227 case BuiltinType::SChar:
5228 return UnsignedCharTy;
5229 case BuiltinType::Short:
5230 return UnsignedShortTy;
5231 case BuiltinType::Int:
5232 return UnsignedIntTy;
5233 case BuiltinType::Long:
5234 return UnsignedLongTy;
5235 case BuiltinType::LongLong:
5236 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00005237 case BuiltinType::Int128:
5238 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00005239 default:
5240 assert(0 && "Unexpected signed integer type");
5241 return QualType();
5242 }
5243}
5244
Douglas Gregor2cf26342009-04-09 22:27:44 +00005245ExternalASTSource::~ExternalASTSource() { }
5246
5247void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00005248
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005249ASTMutationListener::~ASTMutationListener() { }
5250
Chris Lattner86df27b2009-06-14 00:45:47 +00005251
5252//===----------------------------------------------------------------------===//
5253// Builtin Type Computation
5254//===----------------------------------------------------------------------===//
5255
5256/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattner33daae62010-10-01 22:42:38 +00005257/// pointer over the consumed characters. This returns the resultant type. If
5258/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5259/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5260/// a vector of "i*".
Chris Lattner14e0e742010-10-01 22:53:11 +00005261///
5262/// RequiresICE is filled in on return to indicate whether the value is required
5263/// to be an Integer Constant Expression.
Mike Stump1eb44332009-09-09 15:08:12 +00005264static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00005265 ASTContext::GetBuiltinTypeError &Error,
Chris Lattner14e0e742010-10-01 22:53:11 +00005266 bool &RequiresICE,
Chris Lattner33daae62010-10-01 22:42:38 +00005267 bool AllowTypeModifiers) {
Chris Lattner86df27b2009-06-14 00:45:47 +00005268 // Modifiers.
5269 int HowLong = 0;
5270 bool Signed = false, Unsigned = false;
Chris Lattner14e0e742010-10-01 22:53:11 +00005271 RequiresICE = false;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005272
Chris Lattner33daae62010-10-01 22:42:38 +00005273 // Read the prefixed modifiers first.
Chris Lattner86df27b2009-06-14 00:45:47 +00005274 bool Done = false;
5275 while (!Done) {
5276 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00005277 default: Done = true; --Str; break;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005278 case 'I':
Chris Lattner14e0e742010-10-01 22:53:11 +00005279 RequiresICE = true;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005280 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005281 case 'S':
5282 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5283 assert(!Signed && "Can't use 'S' modifier multiple times!");
5284 Signed = true;
5285 break;
5286 case 'U':
5287 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5288 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5289 Unsigned = true;
5290 break;
5291 case 'L':
5292 assert(HowLong <= 2 && "Can't have LLLL modifier");
5293 ++HowLong;
5294 break;
5295 }
5296 }
5297
5298 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00005299
Chris Lattner86df27b2009-06-14 00:45:47 +00005300 // Read the base type.
5301 switch (*Str++) {
5302 default: assert(0 && "Unknown builtin type letter!");
5303 case 'v':
5304 assert(HowLong == 0 && !Signed && !Unsigned &&
5305 "Bad modifiers used with 'v'!");
5306 Type = Context.VoidTy;
5307 break;
5308 case 'f':
5309 assert(HowLong == 0 && !Signed && !Unsigned &&
5310 "Bad modifiers used with 'f'!");
5311 Type = Context.FloatTy;
5312 break;
5313 case 'd':
5314 assert(HowLong < 2 && !Signed && !Unsigned &&
5315 "Bad modifiers used with 'd'!");
5316 if (HowLong)
5317 Type = Context.LongDoubleTy;
5318 else
5319 Type = Context.DoubleTy;
5320 break;
5321 case 's':
5322 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5323 if (Unsigned)
5324 Type = Context.UnsignedShortTy;
5325 else
5326 Type = Context.ShortTy;
5327 break;
5328 case 'i':
5329 if (HowLong == 3)
5330 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5331 else if (HowLong == 2)
5332 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5333 else if (HowLong == 1)
5334 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5335 else
5336 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5337 break;
5338 case 'c':
5339 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5340 if (Signed)
5341 Type = Context.SignedCharTy;
5342 else if (Unsigned)
5343 Type = Context.UnsignedCharTy;
5344 else
5345 Type = Context.CharTy;
5346 break;
5347 case 'b': // boolean
5348 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5349 Type = Context.BoolTy;
5350 break;
5351 case 'z': // size_t.
5352 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5353 Type = Context.getSizeType();
5354 break;
5355 case 'F':
5356 Type = Context.getCFConstantStringType();
5357 break;
5358 case 'a':
5359 Type = Context.getBuiltinVaListType();
5360 assert(!Type.isNull() && "builtin va list type not initialized!");
5361 break;
5362 case 'A':
5363 // This is a "reference" to a va_list; however, what exactly
5364 // this means depends on how va_list is defined. There are two
5365 // different kinds of va_list: ones passed by value, and ones
5366 // passed by reference. An example of a by-value va_list is
5367 // x86, where va_list is a char*. An example of by-ref va_list
5368 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5369 // we want this argument to be a char*&; for x86-64, we want
5370 // it to be a __va_list_tag*.
5371 Type = Context.getBuiltinVaListType();
5372 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattner14e0e742010-10-01 22:53:11 +00005373 if (Type->isArrayType())
Chris Lattner86df27b2009-06-14 00:45:47 +00005374 Type = Context.getArrayDecayedType(Type);
Chris Lattner14e0e742010-10-01 22:53:11 +00005375 else
Chris Lattner86df27b2009-06-14 00:45:47 +00005376 Type = Context.getLValueReferenceType(Type);
Chris Lattner86df27b2009-06-14 00:45:47 +00005377 break;
5378 case 'V': {
5379 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00005380 unsigned NumElements = strtoul(Str, &End, 10);
5381 assert(End != Str && "Missing vector size");
Chris Lattner86df27b2009-06-14 00:45:47 +00005382 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00005383
Chris Lattner14e0e742010-10-01 22:53:11 +00005384 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5385 RequiresICE, false);
5386 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattner33daae62010-10-01 22:42:38 +00005387
5388 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner788b0fd2010-06-23 06:00:24 +00005389 Type = Context.getVectorType(ElementType, NumElements,
5390 VectorType::NotAltiVec);
Chris Lattner86df27b2009-06-14 00:45:47 +00005391 break;
5392 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00005393 case 'X': {
Chris Lattner14e0e742010-10-01 22:53:11 +00005394 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5395 false);
5396 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregord3a23b22009-09-28 21:45:01 +00005397 Type = Context.getComplexType(ElementType);
5398 break;
5399 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005400 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00005401 Type = Context.getFILEType();
5402 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005403 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00005404 return QualType();
5405 }
Mike Stumpfd612db2009-07-28 23:47:15 +00005406 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005407 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00005408 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00005409 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00005410 else
5411 Type = Context.getjmp_bufType();
5412
Mike Stumpfd612db2009-07-28 23:47:15 +00005413 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005414 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00005415 return QualType();
5416 }
5417 break;
Mike Stump782fa302009-07-28 02:25:19 +00005418 }
Mike Stump1eb44332009-09-09 15:08:12 +00005419
Chris Lattner33daae62010-10-01 22:42:38 +00005420 // If there are modifiers and if we're allowed to parse them, go for it.
5421 Done = !AllowTypeModifiers;
Chris Lattner86df27b2009-06-14 00:45:47 +00005422 while (!Done) {
John McCall187ab372010-03-12 04:21:28 +00005423 switch (char c = *Str++) {
Chris Lattner33daae62010-10-01 22:42:38 +00005424 default: Done = true; --Str; break;
5425 case '*':
5426 case '&': {
5427 // Both pointers and references can have their pointee types
5428 // qualified with an address space.
5429 char *End;
5430 unsigned AddrSpace = strtoul(Str, &End, 10);
5431 if (End != Str && AddrSpace != 0) {
5432 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5433 Str = End;
5434 }
5435 if (c == '*')
5436 Type = Context.getPointerType(Type);
5437 else
5438 Type = Context.getLValueReferenceType(Type);
5439 break;
5440 }
5441 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5442 case 'C':
5443 Type = Type.withConst();
5444 break;
5445 case 'D':
5446 Type = Context.getVolatileType(Type);
5447 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005448 }
5449 }
Chris Lattner393bd8e2010-10-01 07:13:18 +00005450
Chris Lattner14e0e742010-10-01 22:53:11 +00005451 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner393bd8e2010-10-01 07:13:18 +00005452 "Integer constant 'I' type must be an integer");
Mike Stump1eb44332009-09-09 15:08:12 +00005453
Chris Lattner86df27b2009-06-14 00:45:47 +00005454 return Type;
5455}
5456
5457/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattner33daae62010-10-01 22:42:38 +00005458QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattner14e0e742010-10-01 22:53:11 +00005459 GetBuiltinTypeError &Error,
5460 unsigned *IntegerConstantArgs) {
Chris Lattner33daae62010-10-01 22:42:38 +00005461 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump1eb44332009-09-09 15:08:12 +00005462
Chris Lattner86df27b2009-06-14 00:45:47 +00005463 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00005464
Chris Lattner14e0e742010-10-01 22:53:11 +00005465 bool RequiresICE = false;
Chris Lattner86df27b2009-06-14 00:45:47 +00005466 Error = GE_None;
Chris Lattner14e0e742010-10-01 22:53:11 +00005467 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5468 RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005469 if (Error != GE_None)
5470 return QualType();
Chris Lattner14e0e742010-10-01 22:53:11 +00005471
5472 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5473
Chris Lattner86df27b2009-06-14 00:45:47 +00005474 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattner14e0e742010-10-01 22:53:11 +00005475 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005476 if (Error != GE_None)
5477 return QualType();
5478
Chris Lattner14e0e742010-10-01 22:53:11 +00005479 // If this argument is required to be an IntegerConstantExpression and the
5480 // caller cares, fill in the bitmask we return.
5481 if (RequiresICE && IntegerConstantArgs)
5482 *IntegerConstantArgs |= 1 << ArgTypes.size();
5483
Chris Lattner86df27b2009-06-14 00:45:47 +00005484 // Do array -> pointer decay. The builtin should use the decayed type.
5485 if (Ty->isArrayType())
5486 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00005487
Chris Lattner86df27b2009-06-14 00:45:47 +00005488 ArgTypes.push_back(Ty);
5489 }
5490
5491 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5492 "'.' should only occur at end of builtin type list!");
5493
5494 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
5495 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
5496 return getFunctionNoProtoType(ResType);
Douglas Gregorce056bc2010-02-21 22:15:06 +00005497
5498 // FIXME: Should we create noreturn types?
Chris Lattner86df27b2009-06-14 00:45:47 +00005499 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00005500 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindola264ba482010-03-30 20:24:48 +00005501 FunctionType::ExtInfo());
Chris Lattner86df27b2009-06-14 00:45:47 +00005502}
Eli Friedmana95d7572009-08-19 07:44:53 +00005503
5504QualType
5505ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
5506 // Perform the usual unary conversions. We do this early so that
5507 // integral promotions to "int" can allow us to exit early, in the
5508 // lhs == rhs check. Also, for conversion purposes, we ignore any
5509 // qualifiers. For example, "const float" and "float" are
5510 // equivalent.
5511 if (lhs->isPromotableIntegerType())
5512 lhs = getPromotedIntegerType(lhs);
5513 else
5514 lhs = lhs.getUnqualifiedType();
5515 if (rhs->isPromotableIntegerType())
5516 rhs = getPromotedIntegerType(rhs);
5517 else
5518 rhs = rhs.getUnqualifiedType();
5519
5520 // If both types are identical, no conversion is needed.
5521 if (lhs == rhs)
5522 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00005523
Eli Friedmana95d7572009-08-19 07:44:53 +00005524 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
5525 // The caller can deal with this (e.g. pointer + int).
5526 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5527 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00005528
5529 // At this point, we have two different arithmetic types.
5530
Eli Friedmana95d7572009-08-19 07:44:53 +00005531 // Handle complex types first (C99 6.3.1.8p1).
5532 if (lhs->isComplexType() || rhs->isComplexType()) {
5533 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00005534 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00005535 // convert the rhs to the lhs complex type.
5536 return lhs;
5537 }
Mike Stump1eb44332009-09-09 15:08:12 +00005538 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00005539 // convert the lhs to the rhs complex type.
5540 return rhs;
5541 }
5542 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00005543 // When both operands are complex, the shorter operand is converted to the
5544 // type of the longer, and that is the type of the result. This corresponds
5545 // to what is done when combining two real floating-point operands.
5546 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00005547 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00005548 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00005549 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00005550 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00005551 // "double _Complex" is promoted to "long double _Complex".
5552 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00005553
5554 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00005555 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00005556 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00005557 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00005558 }
Eli Friedmana95d7572009-08-19 07:44:53 +00005559 // At this point, lhs and rhs have the same rank/size. Now, make sure the
5560 // domains match. This is a requirement for our implementation, C99
5561 // does not require this promotion.
5562 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5563 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5564 return rhs;
5565 } else { // handle "_Complex double, double".
5566 return lhs;
5567 }
5568 }
5569 return lhs; // The domain/size match exactly.
5570 }
5571 // Now handle "real" floating types (i.e. float, double, long double).
5572 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5573 // if we have an integer operand, the result is the real floating type.
5574 if (rhs->isIntegerType()) {
5575 // convert rhs to the lhs floating point type.
5576 return lhs;
5577 }
5578 if (rhs->isComplexIntegerType()) {
5579 // convert rhs to the complex floating point type.
5580 return getComplexType(lhs);
5581 }
5582 if (lhs->isIntegerType()) {
5583 // convert lhs to the rhs floating point type.
5584 return rhs;
5585 }
Mike Stump1eb44332009-09-09 15:08:12 +00005586 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00005587 // convert lhs to the complex floating point type.
5588 return getComplexType(rhs);
5589 }
5590 // We have two real floating types, float/complex combos were handled above.
5591 // Convert the smaller operand to the bigger result.
5592 int result = getFloatingTypeOrder(lhs, rhs);
5593 if (result > 0) // convert the rhs
5594 return lhs;
5595 assert(result < 0 && "illegal float comparison");
5596 return rhs; // convert the lhs
5597 }
5598 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5599 // Handle GCC complex int extension.
5600 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5601 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5602
5603 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00005604 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00005605 rhsComplexInt->getElementType()) >= 0)
5606 return lhs; // convert the rhs
5607 return rhs;
5608 } else if (lhsComplexInt && rhs->isIntegerType()) {
5609 // convert the rhs to the lhs complex type.
5610 return lhs;
5611 } else if (rhsComplexInt && lhs->isIntegerType()) {
5612 // convert the lhs to the rhs complex type.
5613 return rhs;
5614 }
5615 }
5616 // Finally, we have two differing integer types.
5617 // The rules for this case are in C99 6.3.1.8
5618 int compare = getIntegerTypeOrder(lhs, rhs);
Douglas Gregorf6094622010-07-23 15:58:24 +00005619 bool lhsSigned = lhs->hasSignedIntegerRepresentation(),
5620 rhsSigned = rhs->hasSignedIntegerRepresentation();
Eli Friedmana95d7572009-08-19 07:44:53 +00005621 QualType destType;
5622 if (lhsSigned == rhsSigned) {
5623 // Same signedness; use the higher-ranked type
5624 destType = compare >= 0 ? lhs : rhs;
5625 } else if (compare != (lhsSigned ? 1 : -1)) {
5626 // The unsigned type has greater than or equal rank to the
5627 // signed type, so use the unsigned type
5628 destType = lhsSigned ? rhs : lhs;
5629 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5630 // The two types are different widths; if we are here, that
5631 // means the signed type is larger than the unsigned type, so
5632 // use the signed type.
5633 destType = lhsSigned ? lhs : rhs;
5634 } else {
5635 // The signed type is higher-ranked than the unsigned type,
5636 // but isn't actually any bigger (like unsigned int and long
5637 // on most 32-bit systems). Use the unsigned type corresponding
5638 // to the signed type.
5639 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5640 }
5641 return destType;
5642}
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005643
5644GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5645 GVALinkage External = GVA_StrongExternal;
5646
5647 Linkage L = FD->getLinkage();
5648 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5649 FD->getType()->getLinkage() == UniqueExternalLinkage)
5650 L = UniqueExternalLinkage;
5651
5652 switch (L) {
5653 case NoLinkage:
5654 case InternalLinkage:
5655 case UniqueExternalLinkage:
5656 return GVA_Internal;
5657
5658 case ExternalLinkage:
5659 switch (FD->getTemplateSpecializationKind()) {
5660 case TSK_Undeclared:
5661 case TSK_ExplicitSpecialization:
5662 External = GVA_StrongExternal;
5663 break;
5664
5665 case TSK_ExplicitInstantiationDefinition:
5666 return GVA_ExplicitTemplateInstantiation;
5667
5668 case TSK_ExplicitInstantiationDeclaration:
5669 case TSK_ImplicitInstantiation:
5670 External = GVA_TemplateInstantiation;
5671 break;
5672 }
5673 }
5674
5675 if (!FD->isInlined())
5676 return External;
5677
5678 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5679 // GNU or C99 inline semantics. Determine whether this symbol should be
5680 // externally visible.
5681 if (FD->isInlineDefinitionExternallyVisible())
5682 return External;
5683
5684 // C99 inline semantics, where the symbol is not externally visible.
5685 return GVA_C99Inline;
5686 }
5687
5688 // C++0x [temp.explicit]p9:
5689 // [ Note: The intent is that an inline function that is the subject of
5690 // an explicit instantiation declaration will still be implicitly
5691 // instantiated when used so that the body can be considered for
5692 // inlining, but that no out-of-line copy of the inline function would be
5693 // generated in the translation unit. -- end note ]
5694 if (FD->getTemplateSpecializationKind()
5695 == TSK_ExplicitInstantiationDeclaration)
5696 return GVA_C99Inline;
5697
5698 return GVA_CXXInline;
5699}
5700
5701GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
5702 // If this is a static data member, compute the kind of template
5703 // specialization. Otherwise, this variable is not part of a
5704 // template.
5705 TemplateSpecializationKind TSK = TSK_Undeclared;
5706 if (VD->isStaticDataMember())
5707 TSK = VD->getTemplateSpecializationKind();
5708
5709 Linkage L = VD->getLinkage();
5710 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5711 VD->getType()->getLinkage() == UniqueExternalLinkage)
5712 L = UniqueExternalLinkage;
5713
5714 switch (L) {
5715 case NoLinkage:
5716 case InternalLinkage:
5717 case UniqueExternalLinkage:
5718 return GVA_Internal;
5719
5720 case ExternalLinkage:
5721 switch (TSK) {
5722 case TSK_Undeclared:
5723 case TSK_ExplicitSpecialization:
5724 return GVA_StrongExternal;
5725
5726 case TSK_ExplicitInstantiationDeclaration:
5727 llvm_unreachable("Variable should not be instantiated");
5728 // Fall through to treat this like any other instantiation.
5729
5730 case TSK_ExplicitInstantiationDefinition:
5731 return GVA_ExplicitTemplateInstantiation;
5732
5733 case TSK_ImplicitInstantiation:
5734 return GVA_TemplateInstantiation;
5735 }
5736 }
5737
5738 return GVA_StrongExternal;
5739}
5740
Argyrios Kyrtzidis4ac7c0b2010-07-29 20:08:05 +00005741bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005742 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
5743 if (!VD->isFileVarDecl())
5744 return false;
5745 } else if (!isa<FunctionDecl>(D))
5746 return false;
5747
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00005748 // Weak references don't produce any output by themselves.
5749 if (D->hasAttr<WeakRefAttr>())
5750 return false;
5751
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005752 // Aliases and used decls are required.
5753 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
5754 return true;
5755
5756 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5757 // Forward declarations aren't required.
5758 if (!FD->isThisDeclarationADefinition())
5759 return false;
5760
5761 // Constructors and destructors are required.
5762 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
5763 return true;
5764
5765 // The key function for a class is required.
5766 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5767 const CXXRecordDecl *RD = MD->getParent();
5768 if (MD->isOutOfLine() && RD->isDynamicClass()) {
5769 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
5770 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
5771 return true;
5772 }
5773 }
5774
5775 GVALinkage Linkage = GetGVALinkageForFunction(FD);
5776
5777 // static, static inline, always_inline, and extern inline functions can
5778 // always be deferred. Normal inline functions can be deferred in C99/C++.
5779 // Implicit template instantiations can also be deferred in C++.
5780 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
5781 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
5782 return false;
5783 return true;
5784 }
5785
5786 const VarDecl *VD = cast<VarDecl>(D);
5787 assert(VD->isFileVarDecl() && "Expected file scoped var");
5788
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00005789 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
5790 return false;
5791
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005792 // Structs that have non-trivial constructors or destructors are required.
5793
5794 // FIXME: Handle references.
5795 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
5796 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005797 if (RD->hasDefinition() &&
5798 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005799 return true;
5800 }
5801 }
5802
5803 GVALinkage L = GetGVALinkageForVariable(VD);
5804 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
5805 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
5806 return false;
5807 }
5808
5809 return true;
5810}
Charles Davis071cc7d2010-08-16 03:33:14 +00005811
5812CXXABI::~CXXABI() {}