blob: 10f875b1b7683ce92559dc0eb0f3cf9891af5b91 [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
1525 if (NumElts) {
1526 // Dependently-sized array types that do not have a specified
1527 // number of elements will have their sizes deduced from an
1528 // initializer.
Douglas Gregorcb78d882009-11-19 18:03:26 +00001529 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1530 EltTypeQuals, NumElts);
1531
1532 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1533 }
1534
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001535 DependentSizedArrayType *New;
1536 if (Canon) {
1537 // We already have a canonical version of this array type; use it as
1538 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001539 New = new (*this, TypeAlignment)
1540 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1541 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001542 } else {
1543 QualType CanonEltTy = getCanonicalType(EltTy);
1544 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001545 New = new (*this, TypeAlignment)
1546 DependentSizedArrayType(*this, EltTy, QualType(),
1547 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001548
Douglas Gregor789b1f62010-02-04 18:10:26 +00001549 if (NumElts) {
1550 DependentSizedArrayType *CanonCheck
1551 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1552 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1553 (void)CanonCheck;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001554 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001555 }
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001556 } else {
1557 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1558 ASM, EltTypeQuals,
1559 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001560 New = new (*this, TypeAlignment)
1561 DependentSizedArrayType(*this, EltTy, Canon,
1562 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001563 }
1564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Douglas Gregor898574e2008-12-05 23:32:09 +00001566 Types.push_back(New);
1567 return QualType(New, 0);
1568}
1569
Eli Friedmanc5773c42008-02-15 18:16:39 +00001570QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1571 ArrayType::ArraySizeModifier ASM,
1572 unsigned EltTypeQuals) {
1573 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001574 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001575
1576 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001577 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001578 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1579 return QualType(ATP, 0);
1580
1581 // If the element type isn't canonical, this won't be a canonical type
1582 // either, so fill in the canonical type field.
1583 QualType Canonical;
1584
John McCall467b27b2009-10-22 20:10:53 +00001585 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001586 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001587 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001588
1589 // Get the new insert position for the node we care about.
1590 IncompleteArrayType *NewIP =
1591 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001592 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001593 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001594
John McCall6b304a02009-09-24 23:30:46 +00001595 IncompleteArrayType *New = new (*this, TypeAlignment)
1596 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001597
1598 IncompleteArrayTypes.InsertNode(New, InsertPos);
1599 Types.push_back(New);
1600 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001601}
1602
Steve Naroff73322922007-07-18 18:00:27 +00001603/// getVectorType - Return the unique reference to a vector type of
1604/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001605QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Chris Lattner33daae62010-10-01 22:42:38 +00001606 VectorType::AltiVecSpecific AltiVecSpec) {
1607 BuiltinType *BaseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Chris Lattner33daae62010-10-01 22:42:38 +00001609 BaseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
1610 assert(BaseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 // Check if we've already instantiated a vector of this type.
1613 llvm::FoldingSetNodeID ID;
Chris Lattner788b0fd2010-06-23 06:00:24 +00001614 VectorType::Profile(ID, vecType, NumElts, Type::Vector, AltiVecSpec);
1615
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 void *InsertPos = 0;
1617 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1618 return QualType(VTP, 0);
1619
1620 // If the element type isn't canonical, this won't be a canonical type either,
1621 // so fill in the canonical type field.
1622 QualType Canonical;
Douglas Gregor255210e2010-08-06 10:14:59 +00001623 if (!vecType.isCanonical()) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00001624 Canonical = getVectorType(getCanonicalType(vecType), NumElts,
1625 VectorType::NotAltiVec);
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 // Get the new insert position for the node we care about.
1628 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001629 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 }
John McCall6b304a02009-09-24 23:30:46 +00001631 VectorType *New = new (*this, TypeAlignment)
Chris Lattner788b0fd2010-06-23 06:00:24 +00001632 VectorType(vecType, NumElts, Canonical, AltiVecSpec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 VectorTypes.InsertNode(New, InsertPos);
1634 Types.push_back(New);
1635 return QualType(New, 0);
1636}
1637
Nate Begeman213541a2008-04-18 23:10:10 +00001638/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001639/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001640QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001641 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Chris Lattnerf52ab252008-04-06 22:59:24 +00001643 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001644 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Steve Naroff73322922007-07-18 18:00:27 +00001646 // Check if we've already instantiated a vector of this type.
1647 llvm::FoldingSetNodeID ID;
Chris Lattner788b0fd2010-06-23 06:00:24 +00001648 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
1649 VectorType::NotAltiVec);
Steve Naroff73322922007-07-18 18:00:27 +00001650 void *InsertPos = 0;
1651 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1652 return QualType(VTP, 0);
1653
1654 // If the element type isn't canonical, this won't be a canonical type either,
1655 // so fill in the canonical type field.
1656 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001657 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001658 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Steve Naroff73322922007-07-18 18:00:27 +00001660 // Get the new insert position for the node we care about.
1661 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001662 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001663 }
John McCall6b304a02009-09-24 23:30:46 +00001664 ExtVectorType *New = new (*this, TypeAlignment)
1665 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001666 VectorTypes.InsertNode(New, InsertPos);
1667 Types.push_back(New);
1668 return QualType(New, 0);
1669}
1670
Mike Stump1eb44332009-09-09 15:08:12 +00001671QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001672 Expr *SizeExpr,
1673 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001674 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001675 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001676 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001678 void *InsertPos = 0;
1679 DependentSizedExtVectorType *Canon
1680 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1681 DependentSizedExtVectorType *New;
1682 if (Canon) {
1683 // We already have a canonical version of this array type; use it as
1684 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001685 New = new (*this, TypeAlignment)
1686 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1687 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001688 } else {
1689 QualType CanonVecTy = getCanonicalType(vecType);
1690 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001691 New = new (*this, TypeAlignment)
1692 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1693 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001694
1695 DependentSizedExtVectorType *CanonCheck
1696 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1697 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1698 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001699 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1700 } else {
1701 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1702 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001703 New = new (*this, TypeAlignment)
1704 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001705 }
1706 }
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001708 Types.push_back(New);
1709 return QualType(New, 0);
1710}
1711
Douglas Gregor72564e72009-02-26 23:50:07 +00001712/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001713///
Rafael Espindola264ba482010-03-30 20:24:48 +00001714QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1715 const FunctionType::ExtInfo &Info) {
1716 const CallingConv CallConv = Info.getCC();
Reid Spencer5f016e22007-07-11 17:01:13 +00001717 // Unique functions, to guarantee there is only one function of a particular
1718 // structure.
1719 llvm::FoldingSetNodeID ID;
Rafael Espindola264ba482010-03-30 20:24:48 +00001720 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Reid Spencer5f016e22007-07-11 17:01:13 +00001722 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001723 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001724 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Reid Spencer5f016e22007-07-11 17:01:13 +00001727 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001728 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001729 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindola264ba482010-03-30 20:24:48 +00001730 Canonical =
1731 getFunctionNoProtoType(getCanonicalType(ResultTy),
1732 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Reid Spencer5f016e22007-07-11 17:01:13 +00001734 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001735 FunctionNoProtoType *NewIP =
1736 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001737 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001738 }
Mike Stump1eb44332009-09-09 15:08:12 +00001739
John McCall6b304a02009-09-24 23:30:46 +00001740 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindola264ba482010-03-30 20:24:48 +00001741 FunctionNoProtoType(ResultTy, Canonical, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001743 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 return QualType(New, 0);
1745}
1746
1747/// getFunctionType - Return a normal function type with a typed argument
1748/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001749QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001750 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001751 unsigned TypeQuals, bool hasExceptionSpec,
1752 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001753 const QualType *ExArray,
1754 const FunctionType::ExtInfo &Info) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00001755
Rafael Espindola264ba482010-03-30 20:24:48 +00001756 const CallingConv CallConv= Info.getCC();
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 // Unique functions, to guarantee there is only one function of a particular
1758 // structure.
1759 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001760 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001761 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindola264ba482010-03-30 20:24:48 +00001762 NumExs, ExArray, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001763
1764 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001765 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001766 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001768
1769 // Determine whether the type being created is already canonical or not.
John McCall54e14c42009-10-22 22:37:11 +00001770 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001772 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001773 isCanonical = false;
1774
1775 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001776 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001778 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 llvm::SmallVector<QualType, 16> CanonicalArgs;
1780 CanonicalArgs.reserve(NumArgs);
1781 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001782 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001783
Chris Lattnerf52ab252008-04-06 22:59:24 +00001784 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001785 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001786 isVariadic, TypeQuals, false,
Rafael Espindola264ba482010-03-30 20:24:48 +00001787 false, 0, 0,
1788 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl465226e2009-05-27 22:11:52 +00001789
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001791 FunctionProtoType *NewIP =
1792 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001793 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001795
Douglas Gregor72564e72009-02-26 23:50:07 +00001796 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001797 // for two variable size arrays (for parameter and exception types) at the
1798 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001799 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001800 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1801 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001802 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001803 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001804 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindola264ba482010-03-30 20:24:48 +00001805 ExArray, NumExs, Canonical, Info);
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001807 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 return QualType(FTP, 0);
1809}
1810
John McCall3cb0ebd2010-03-10 03:28:59 +00001811#ifndef NDEBUG
1812static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1813 if (!isa<CXXRecordDecl>(D)) return false;
1814 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1815 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1816 return true;
1817 if (RD->getDescribedClassTemplate() &&
1818 !isa<ClassTemplateSpecializationDecl>(RD))
1819 return true;
1820 return false;
1821}
1822#endif
1823
1824/// getInjectedClassNameType - Return the unique reference to the
1825/// injected class name type for the specified templated declaration.
1826QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1827 QualType TST) {
1828 assert(NeedsInjectedClassNameType(Decl));
1829 if (Decl->TypeForDecl) {
1830 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00001831 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCall3cb0ebd2010-03-10 03:28:59 +00001832 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1833 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1834 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1835 } else {
John McCall31f17ec2010-04-27 00:57:59 +00001836 Decl->TypeForDecl =
1837 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCall3cb0ebd2010-03-10 03:28:59 +00001838 Types.push_back(Decl->TypeForDecl);
1839 }
1840 return QualType(Decl->TypeForDecl, 0);
1841}
1842
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001843/// getTypeDeclType - Return the unique reference to the type for the
1844/// specified type declaration.
John McCallbecb8d52010-03-10 06:48:02 +00001845QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001846 assert(Decl && "Passed null for Decl param");
John McCallbecb8d52010-03-10 06:48:02 +00001847 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump1eb44332009-09-09 15:08:12 +00001848
John McCall19c85762010-02-16 03:57:14 +00001849 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001850 return getTypedefType(Typedef);
John McCallbecb8d52010-03-10 06:48:02 +00001851
John McCallbecb8d52010-03-10 06:48:02 +00001852 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1853 "Template type parameter types are always available.");
1854
John McCall19c85762010-02-16 03:57:14 +00001855 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001856 assert(!Record->getPreviousDeclaration() &&
1857 "struct/union has previous declaration");
1858 assert(!NeedsInjectedClassNameType(Record));
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001859 return getRecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00001860 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001861 assert(!Enum->getPreviousDeclaration() &&
1862 "enum has previous declaration");
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001863 return getEnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00001864 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00001865 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1866 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stump9fdbab32009-07-31 02:02:20 +00001867 } else
John McCallbecb8d52010-03-10 06:48:02 +00001868 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001869
John McCallbecb8d52010-03-10 06:48:02 +00001870 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001871 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001872}
1873
Reid Spencer5f016e22007-07-11 17:01:13 +00001874/// getTypedefType - Return the unique reference to the type for the
1875/// specified typename decl.
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00001876QualType
1877ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00001880 if (Canonical.isNull())
1881 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001882 Decl->TypeForDecl = new(*this, TypeAlignment)
1883 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001884 Types.push_back(Decl->TypeForDecl);
1885 return QualType(Decl->TypeForDecl, 0);
1886}
1887
Argyrios Kyrtzidis400f5122010-07-04 21:44:47 +00001888QualType ASTContext::getRecordType(const RecordDecl *Decl) {
1889 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1890
1891 if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
1892 if (PrevDecl->TypeForDecl)
1893 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1894
1895 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Decl);
1896 Types.push_back(Decl->TypeForDecl);
1897 return QualType(Decl->TypeForDecl, 0);
1898}
1899
1900QualType ASTContext::getEnumType(const EnumDecl *Decl) {
1901 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1902
1903 if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
1904 if (PrevDecl->TypeForDecl)
1905 return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
1906
1907 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Decl);
1908 Types.push_back(Decl->TypeForDecl);
1909 return QualType(Decl->TypeForDecl, 0);
1910}
1911
John McCall49a832b2009-10-18 09:09:24 +00001912/// \brief Retrieve a substitution-result type.
1913QualType
1914ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1915 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001916 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001917 && "replacement types must always be canonical");
1918
1919 llvm::FoldingSetNodeID ID;
1920 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1921 void *InsertPos = 0;
1922 SubstTemplateTypeParmType *SubstParm
1923 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1924
1925 if (!SubstParm) {
1926 SubstParm = new (*this, TypeAlignment)
1927 SubstTemplateTypeParmType(Parm, Replacement);
1928 Types.push_back(SubstParm);
1929 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1930 }
1931
1932 return QualType(SubstParm, 0);
1933}
1934
Douglas Gregorfab9d672009-02-05 23:33:38 +00001935/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001936/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001937/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001938QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001939 bool ParameterPack,
Douglas Gregorefed5c82010-06-16 15:23:05 +00001940 IdentifierInfo *Name) {
Douglas Gregorfab9d672009-02-05 23:33:38 +00001941 llvm::FoldingSetNodeID ID;
Douglas Gregorefed5c82010-06-16 15:23:05 +00001942 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001943 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001944 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001945 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1946
1947 if (TypeParm)
1948 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001949
Douglas Gregorefed5c82010-06-16 15:23:05 +00001950 if (Name) {
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001951 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorefed5c82010-06-16 15:23:05 +00001952 TypeParm = new (*this, TypeAlignment)
1953 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001954
1955 TemplateTypeParmType *TypeCheck
1956 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1957 assert(!TypeCheck && "Template type parameter canonical type broken");
1958 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001959 } else
John McCall6b304a02009-09-24 23:30:46 +00001960 TypeParm = new (*this, TypeAlignment)
1961 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001962
1963 Types.push_back(TypeParm);
1964 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1965
1966 return QualType(TypeParm, 0);
1967}
1968
John McCall3cb0ebd2010-03-10 03:28:59 +00001969TypeSourceInfo *
1970ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1971 SourceLocation NameLoc,
1972 const TemplateArgumentListInfo &Args,
1973 QualType CanonType) {
1974 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1975
1976 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1977 TemplateSpecializationTypeLoc TL
1978 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1979 TL.setTemplateNameLoc(NameLoc);
1980 TL.setLAngleLoc(Args.getLAngleLoc());
1981 TL.setRAngleLoc(Args.getRAngleLoc());
1982 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1983 TL.setArgLocInfo(i, Args[i].getLocInfo());
1984 return DI;
1985}
1986
Mike Stump1eb44332009-09-09 15:08:12 +00001987QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001988ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00001989 const TemplateArgumentListInfo &Args,
John McCall71d74bc2010-06-13 09:25:03 +00001990 QualType Canon) {
John McCalld5532b62009-11-23 01:53:49 +00001991 unsigned NumArgs = Args.size();
1992
John McCall833ca992009-10-29 08:12:44 +00001993 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1994 ArgVec.reserve(NumArgs);
1995 for (unsigned i = 0; i != NumArgs; ++i)
1996 ArgVec.push_back(Args[i].getArgument());
1997
John McCall31f17ec2010-04-27 00:57:59 +00001998 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00001999 Canon);
John McCall833ca992009-10-29 08:12:44 +00002000}
2001
2002QualType
2003ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002004 const TemplateArgument *Args,
2005 unsigned NumArgs,
John McCall71d74bc2010-06-13 09:25:03 +00002006 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00002007 if (!Canon.isNull())
2008 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002009 else
2010 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregorfc705b82009-02-26 22:19:44 +00002011
Douglas Gregor1275ae02009-07-28 23:00:59 +00002012 // Allocate the (non-canonical) template specialization type, but don't
2013 // try to unique it: these types typically have location information that
2014 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00002015 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00002016 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00002017 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00002018 TemplateSpecializationType *Spec
John McCallef990012010-06-11 11:07:21 +00002019 = new (Mem) TemplateSpecializationType(Template,
John McCall31f17ec2010-04-27 00:57:59 +00002020 Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00002021 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Douglas Gregor55f6b142009-02-09 18:46:07 +00002023 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00002024 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002025}
2026
Mike Stump1eb44332009-09-09 15:08:12 +00002027QualType
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00002028ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
2029 const TemplateArgument *Args,
2030 unsigned NumArgs) {
2031 // Build the canonical template specialization type.
2032 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
2033 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
2034 CanonArgs.reserve(NumArgs);
2035 for (unsigned I = 0; I != NumArgs; ++I)
2036 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
2037
2038 // Determine whether this canonical template specialization type already
2039 // exists.
2040 llvm::FoldingSetNodeID ID;
2041 TemplateSpecializationType::Profile(ID, CanonTemplate,
2042 CanonArgs.data(), NumArgs, *this);
2043
2044 void *InsertPos = 0;
2045 TemplateSpecializationType *Spec
2046 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2047
2048 if (!Spec) {
2049 // Allocate a new canonical template specialization type.
2050 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
2051 sizeof(TemplateArgument) * NumArgs),
2052 TypeAlignment);
2053 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
2054 CanonArgs.data(), NumArgs,
2055 QualType());
2056 Types.push_back(Spec);
2057 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
2058 }
2059
2060 assert(Spec->isDependentType() &&
2061 "Non-dependent template-id type must have a canonical type");
2062 return QualType(Spec, 0);
2063}
2064
2065QualType
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002066ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2067 NestedNameSpecifier *NNS,
2068 QualType NamedType) {
Douglas Gregore4e5b052009-03-19 00:18:19 +00002069 llvm::FoldingSetNodeID ID;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002070 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002071
2072 void *InsertPos = 0;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002073 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002074 if (T)
2075 return QualType(T, 0);
2076
Douglas Gregor789b1f62010-02-04 18:10:26 +00002077 QualType Canon = NamedType;
2078 if (!Canon.isCanonical()) {
2079 Canon = getCanonicalType(NamedType);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002080 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2081 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregor789b1f62010-02-04 18:10:26 +00002082 (void)CheckT;
2083 }
2084
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002085 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002086 Types.push_back(T);
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002087 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002088 return QualType(T, 0);
2089}
2090
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002091QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2092 NestedNameSpecifier *NNS,
2093 const IdentifierInfo *Name,
2094 QualType Canon) {
Douglas Gregord57959a2009-03-27 23:10:48 +00002095 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2096
2097 if (Canon.isNull()) {
2098 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002099 ElaboratedTypeKeyword CanonKeyword = Keyword;
2100 if (Keyword == ETK_None)
2101 CanonKeyword = ETK_Typename;
2102
2103 if (CanonNNS != NNS || CanonKeyword != Keyword)
2104 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002105 }
2106
2107 llvm::FoldingSetNodeID ID;
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002108 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregord57959a2009-03-27 23:10:48 +00002109
2110 void *InsertPos = 0;
Douglas Gregor4714c122010-03-31 17:34:00 +00002111 DependentNameType *T
2112 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregord57959a2009-03-27 23:10:48 +00002113 if (T)
2114 return QualType(T, 0);
2115
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002116 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregord57959a2009-03-27 23:10:48 +00002117 Types.push_back(T);
Douglas Gregor4714c122010-03-31 17:34:00 +00002118 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002119 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00002120}
2121
Mike Stump1eb44332009-09-09 15:08:12 +00002122QualType
John McCall33500952010-06-11 00:33:02 +00002123ASTContext::getDependentTemplateSpecializationType(
2124 ElaboratedTypeKeyword Keyword,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00002125 NestedNameSpecifier *NNS,
John McCall33500952010-06-11 00:33:02 +00002126 const IdentifierInfo *Name,
2127 const TemplateArgumentListInfo &Args) {
2128 // TODO: avoid this copy
2129 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2130 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2131 ArgCopy.push_back(Args[I].getArgument());
2132 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2133 ArgCopy.size(),
2134 ArgCopy.data());
2135}
2136
2137QualType
2138ASTContext::getDependentTemplateSpecializationType(
2139 ElaboratedTypeKeyword Keyword,
2140 NestedNameSpecifier *NNS,
2141 const IdentifierInfo *Name,
2142 unsigned NumArgs,
2143 const TemplateArgument *Args) {
Douglas Gregor17343172009-04-01 00:28:59 +00002144 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2145
Douglas Gregor789b1f62010-02-04 18:10:26 +00002146 llvm::FoldingSetNodeID ID;
John McCall33500952010-06-11 00:33:02 +00002147 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2148 Name, NumArgs, Args);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002149
2150 void *InsertPos = 0;
John McCall33500952010-06-11 00:33:02 +00002151 DependentTemplateSpecializationType *T
2152 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002153 if (T)
2154 return QualType(T, 0);
2155
John McCall33500952010-06-11 00:33:02 +00002156 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor789b1f62010-02-04 18:10:26 +00002157
John McCall33500952010-06-11 00:33:02 +00002158 ElaboratedTypeKeyword CanonKeyword = Keyword;
2159 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2160
2161 bool AnyNonCanonArgs = false;
2162 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2163 for (unsigned I = 0; I != NumArgs; ++I) {
2164 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2165 if (!CanonArgs[I].structurallyEquals(Args[I]))
2166 AnyNonCanonArgs = true;
Douglas Gregor17343172009-04-01 00:28:59 +00002167 }
2168
John McCall33500952010-06-11 00:33:02 +00002169 QualType Canon;
2170 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2171 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2172 Name, NumArgs,
2173 CanonArgs.data());
2174
2175 // Find the insert position again.
2176 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2177 }
2178
2179 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2180 sizeof(TemplateArgument) * NumArgs),
2181 TypeAlignment);
John McCallef990012010-06-11 11:07:21 +00002182 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCall33500952010-06-11 00:33:02 +00002183 Name, NumArgs, Args, Canon);
Douglas Gregor17343172009-04-01 00:28:59 +00002184 Types.push_back(T);
John McCall33500952010-06-11 00:33:02 +00002185 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002186 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002187}
2188
Chris Lattner88cb27a2008-04-07 04:56:42 +00002189/// CmpProtocolNames - Comparison predicate for sorting protocols
2190/// alphabetically.
2191static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2192 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002193 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00002194}
2195
John McCallc12c5bb2010-05-15 11:32:37 +00002196static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCall54e14c42009-10-22 22:37:11 +00002197 unsigned NumProtocols) {
2198 if (NumProtocols == 0) return true;
2199
2200 for (unsigned i = 1; i != NumProtocols; ++i)
2201 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2202 return false;
2203 return true;
2204}
2205
2206static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00002207 unsigned &NumProtocols) {
2208 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00002209
Chris Lattner88cb27a2008-04-07 04:56:42 +00002210 // Sort protocols, keyed by name.
2211 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2212
2213 // Remove duplicates.
2214 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2215 NumProtocols = ProtocolsEnd-Protocols;
2216}
2217
John McCallc12c5bb2010-05-15 11:32:37 +00002218QualType ASTContext::getObjCObjectType(QualType BaseType,
2219 ObjCProtocolDecl * const *Protocols,
2220 unsigned NumProtocols) {
2221 // If the base type is an interface and there aren't any protocols
2222 // to add, then the interface type will do just fine.
2223 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2224 return BaseType;
2225
2226 // Look in the folding set for an existing type.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002227 llvm::FoldingSetNodeID ID;
John McCallc12c5bb2010-05-15 11:32:37 +00002228 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002229 void *InsertPos = 0;
John McCallc12c5bb2010-05-15 11:32:37 +00002230 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2231 return QualType(QT, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002232
John McCallc12c5bb2010-05-15 11:32:37 +00002233 // Build the canonical type, which has the canonical base type and
2234 // a sorted-and-uniqued list of protocols.
John McCall54e14c42009-10-22 22:37:11 +00002235 QualType Canonical;
John McCallc12c5bb2010-05-15 11:32:37 +00002236 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2237 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2238 if (!ProtocolsSorted) {
Benjamin Kramer02379412010-04-27 17:12:11 +00002239 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2240 Protocols + NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002241 unsigned UniqueCount = NumProtocols;
2242
John McCall54e14c42009-10-22 22:37:11 +00002243 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCallc12c5bb2010-05-15 11:32:37 +00002244 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2245 &Sorted[0], UniqueCount);
John McCall54e14c42009-10-22 22:37:11 +00002246 } else {
John McCallc12c5bb2010-05-15 11:32:37 +00002247 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2248 Protocols, NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002249 }
2250
2251 // Regenerate InsertPos.
John McCallc12c5bb2010-05-15 11:32:37 +00002252 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2253 }
2254
2255 unsigned Size = sizeof(ObjCObjectTypeImpl);
2256 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2257 void *Mem = Allocate(Size, TypeAlignment);
2258 ObjCObjectTypeImpl *T =
2259 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2260
2261 Types.push_back(T);
2262 ObjCObjectTypes.InsertNode(T, InsertPos);
2263 return QualType(T, 0);
2264}
2265
2266/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2267/// the given object type.
2268QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2269 llvm::FoldingSetNodeID ID;
2270 ObjCObjectPointerType::Profile(ID, ObjectT);
2271
2272 void *InsertPos = 0;
2273 if (ObjCObjectPointerType *QT =
2274 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2275 return QualType(QT, 0);
2276
2277 // Find the canonical object type.
2278 QualType Canonical;
2279 if (!ObjectT.isCanonical()) {
2280 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2281
2282 // Regenerate InsertPos.
John McCall54e14c42009-10-22 22:37:11 +00002283 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2284 }
2285
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002286 // No match.
John McCallc12c5bb2010-05-15 11:32:37 +00002287 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2288 ObjCObjectPointerType *QType =
2289 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump1eb44332009-09-09 15:08:12 +00002290
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002291 Types.push_back(QType);
2292 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCallc12c5bb2010-05-15 11:32:37 +00002293 return QualType(QType, 0);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002294}
Chris Lattner88cb27a2008-04-07 04:56:42 +00002295
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002296/// getObjCInterfaceType - Return the unique reference to the type for the
2297/// specified ObjC interface decl. The list of protocols is optional.
2298QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2299 if (Decl->TypeForDecl)
2300 return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002301
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002302 // FIXME: redeclarations?
2303 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2304 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2305 Decl->TypeForDecl = T;
2306 Types.push_back(T);
2307 return QualType(T, 0);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002308}
2309
Douglas Gregor72564e72009-02-26 23:50:07 +00002310/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2311/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002312/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002313/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002314/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002315QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002316 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002317 if (tofExpr->isTypeDependent()) {
2318 llvm::FoldingSetNodeID ID;
2319 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Douglas Gregorb1975722009-07-30 23:18:24 +00002321 void *InsertPos = 0;
2322 DependentTypeOfExprType *Canon
2323 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2324 if (Canon) {
2325 // We already have a "canonical" version of an identical, dependent
2326 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002327 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002328 QualType((TypeOfExprType*)Canon, 0));
2329 }
2330 else {
2331 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002332 Canon
2333 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002334 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2335 toe = Canon;
2336 }
2337 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002338 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002339 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002340 }
Steve Naroff9752f252007-08-01 18:02:17 +00002341 Types.push_back(toe);
2342 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002343}
2344
Steve Naroff9752f252007-08-01 18:02:17 +00002345/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2346/// TypeOfType AST's. The only motivation to unique these nodes would be
2347/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002348/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002349/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002350QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002351 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002352 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002353 Types.push_back(tot);
2354 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002355}
2356
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002357/// getDecltypeForExpr - Given an expr, will return the decltype for that
2358/// expression, according to the rules in C++0x [dcl.type.simple]p4
2359static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002360 if (e->isTypeDependent())
2361 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002362
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002363 // If e is an id expression or a class member access, decltype(e) is defined
2364 // as the type of the entity named by e.
2365 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2366 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2367 return VD->getType();
2368 }
2369 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2370 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2371 return FD->getType();
2372 }
2373 // If e is a function call or an invocation of an overloaded operator,
2374 // (parentheses around e are ignored), decltype(e) is defined as the
2375 // return type of that function.
2376 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2377 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002379 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002380
2381 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002382 // defined as T&, otherwise decltype(e) is defined as T.
2383 if (e->isLvalue(Context) == Expr::LV_Valid)
2384 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002386 return T;
2387}
2388
Anders Carlsson395b4752009-06-24 19:06:50 +00002389/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2390/// DecltypeType AST's. The only motivation to unique these nodes would be
2391/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002392/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002393/// on canonical type's (which are always unique).
2394QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002395 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002396 if (e->isTypeDependent()) {
2397 llvm::FoldingSetNodeID ID;
2398 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002400 void *InsertPos = 0;
2401 DependentDecltypeType *Canon
2402 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2403 if (Canon) {
2404 // We already have a "canonical" version of an equivalent, dependent
2405 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002406 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002407 QualType((DecltypeType*)Canon, 0));
2408 }
2409 else {
2410 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002411 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002412 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2413 dt = Canon;
2414 }
2415 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002416 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002417 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002418 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002419 Types.push_back(dt);
2420 return QualType(dt, 0);
2421}
2422
Reid Spencer5f016e22007-07-11 17:01:13 +00002423/// getTagDeclType - Return the unique reference to the type for the
2424/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002425QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002426 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002427 // FIXME: What is the design on getTagDeclType when it requires casting
2428 // away const? mutable?
2429 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002430}
2431
Mike Stump1eb44332009-09-09 15:08:12 +00002432/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2433/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2434/// needs to agree with the definition in <stddef.h>.
Anders Carlssona3ccda52009-12-12 00:26:23 +00002435CanQualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002436 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002437}
2438
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002439/// getSignedWCharType - Return the type of "signed wchar_t".
2440/// Used when in C++, as a GCC extension.
2441QualType ASTContext::getSignedWCharType() const {
2442 // FIXME: derive from "Target" ?
2443 return WCharTy;
2444}
2445
2446/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2447/// Used when in C++, as a GCC extension.
2448QualType ASTContext::getUnsignedWCharType() const {
2449 // FIXME: derive from "Target" ?
2450 return UnsignedIntTy;
2451}
2452
Chris Lattner8b9023b2007-07-13 03:05:23 +00002453/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2454/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2455QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002456 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002457}
2458
Chris Lattnere6327742008-04-02 05:18:44 +00002459//===----------------------------------------------------------------------===//
2460// Type Operators
2461//===----------------------------------------------------------------------===//
2462
John McCall54e14c42009-10-22 22:37:11 +00002463CanQualType ASTContext::getCanonicalParamType(QualType T) {
2464 // Push qualifiers into arrays, and then discard any remaining
2465 // qualifiers.
2466 T = getCanonicalType(T);
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002467 T = getVariableArrayDecayedType(T);
John McCall54e14c42009-10-22 22:37:11 +00002468 const Type *Ty = T.getTypePtr();
John McCall54e14c42009-10-22 22:37:11 +00002469 QualType Result;
2470 if (isa<ArrayType>(Ty)) {
2471 Result = getArrayDecayedType(QualType(Ty,0));
2472 } else if (isa<FunctionType>(Ty)) {
2473 Result = getPointerType(QualType(Ty, 0));
2474 } else {
2475 Result = QualType(Ty, 0);
2476 }
2477
2478 return CanQualType::CreateUnsafe(Result);
2479}
2480
Chris Lattner77c96472008-04-06 22:41:35 +00002481/// getCanonicalType - Return the canonical (structural) type corresponding to
2482/// the specified potentially non-canonical type. The non-canonical version
2483/// of a type may have many "decorated" versions of types. Decorators can
2484/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2485/// to be free of any of these, allowing two canonical types to be compared
2486/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002487CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002488 QualifierCollector Quals;
2489 const Type *Ptr = Quals.strip(T);
2490 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002491
John McCall0953e762009-09-24 19:53:00 +00002492 // The canonical internal type will be the canonical type *except*
2493 // that we push type qualifiers down through array types.
2494
2495 // If there are no new qualifiers to push down, stop here.
2496 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002497 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002498
John McCall0953e762009-09-24 19:53:00 +00002499 // If the type qualifiers are on an array type, get the canonical
2500 // type of the array with the qualifiers applied to the element
2501 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002502 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2503 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002504 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002505
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002506 // Get the canonical version of the element with the extra qualifiers on it.
2507 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002508 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002509 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002510
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002511 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002512 return CanQualType::CreateUnsafe(
2513 getConstantArrayType(NewEltTy, CAT->getSize(),
2514 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002515 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002516 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002517 return CanQualType::CreateUnsafe(
2518 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002519 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002520
Douglas Gregor898574e2008-12-05 23:32:09 +00002521 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002522 return CanQualType::CreateUnsafe(
2523 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002524 DSAT->getSizeExpr() ?
2525 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002526 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002527 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor87a924e2009-10-30 22:56:57 +00002528 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor898574e2008-12-05 23:32:09 +00002529
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002530 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002531 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002532 VAT->getSizeExpr() ?
2533 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002534 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002535 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002536 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002537}
2538
Chandler Carruth28e318c2009-12-29 07:16:59 +00002539QualType ASTContext::getUnqualifiedArrayType(QualType T,
2540 Qualifiers &Quals) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002541 Quals = T.getQualifiers();
Douglas Gregor9dadd942010-05-17 18:45:21 +00002542 const ArrayType *AT = getAsArrayType(T);
2543 if (!AT) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002544 return T.getUnqualifiedType();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002545 }
2546
Chandler Carruth28e318c2009-12-29 07:16:59 +00002547 QualType Elt = AT->getElementType();
Zhongxing Xuc1ae0a82010-01-05 08:15:06 +00002548 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002549 if (Elt == UnqualElt)
2550 return T;
2551
Douglas Gregor9dadd942010-05-17 18:45:21 +00002552 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth28e318c2009-12-29 07:16:59 +00002553 return getConstantArrayType(UnqualElt, CAT->getSize(),
2554 CAT->getSizeModifier(), 0);
2555 }
2556
Douglas Gregor9dadd942010-05-17 18:45:21 +00002557 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth28e318c2009-12-29 07:16:59 +00002558 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2559 }
2560
Douglas Gregor9dadd942010-05-17 18:45:21 +00002561 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2562 return getVariableArrayType(UnqualElt,
2563 VAT->getSizeExpr() ?
2564 VAT->getSizeExpr()->Retain() : 0,
2565 VAT->getSizeModifier(),
2566 VAT->getIndexTypeCVRQualifiers(),
2567 VAT->getBracketsRange());
2568 }
2569
2570 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002571 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2572 DSAT->getSizeModifier(), 0,
2573 SourceRange());
2574}
2575
Douglas Gregor5a57efd2010-06-09 03:53:18 +00002576/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2577/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2578/// they point to and return true. If T1 and T2 aren't pointer types
2579/// or pointer-to-member types, or if they are not similar at this
2580/// level, returns false and leaves T1 and T2 unchanged. Top-level
2581/// qualifiers on T1 and T2 are ignored. This function will typically
2582/// be called in a loop that successively "unwraps" pointer and
2583/// pointer-to-member types to compare them at each level.
2584bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2585 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2586 *T2PtrType = T2->getAs<PointerType>();
2587 if (T1PtrType && T2PtrType) {
2588 T1 = T1PtrType->getPointeeType();
2589 T2 = T2PtrType->getPointeeType();
2590 return true;
2591 }
2592
2593 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2594 *T2MPType = T2->getAs<MemberPointerType>();
2595 if (T1MPType && T2MPType &&
2596 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2597 QualType(T2MPType->getClass(), 0))) {
2598 T1 = T1MPType->getPointeeType();
2599 T2 = T2MPType->getPointeeType();
2600 return true;
2601 }
2602
2603 if (getLangOptions().ObjC1) {
2604 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2605 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2606 if (T1OPType && T2OPType) {
2607 T1 = T1OPType->getPointeeType();
2608 T2 = T2OPType->getPointeeType();
2609 return true;
2610 }
2611 }
2612
2613 // FIXME: Block pointers, too?
2614
2615 return false;
2616}
2617
Abramo Bagnara25777432010-08-11 22:01:17 +00002618DeclarationNameInfo ASTContext::getNameForTemplate(TemplateName Name,
2619 SourceLocation NameLoc) {
John McCall80ad16f2009-11-24 18:42:40 +00002620 if (TemplateDecl *TD = Name.getAsTemplateDecl())
Abramo Bagnara25777432010-08-11 22:01:17 +00002621 // DNInfo work in progress: CHECKME: what about DNLoc?
2622 return DeclarationNameInfo(TD->getDeclName(), NameLoc);
2623
John McCall80ad16f2009-11-24 18:42:40 +00002624 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002625 DeclarationName DName;
John McCall80ad16f2009-11-24 18:42:40 +00002626 if (DTN->isIdentifier()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002627 DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
2628 return DeclarationNameInfo(DName, NameLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002629 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002630 DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
2631 // DNInfo work in progress: FIXME: source locations?
2632 DeclarationNameLoc DNLoc;
2633 DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
2634 DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
2635 return DeclarationNameInfo(DName, NameLoc, DNLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002636 }
2637 }
2638
John McCall0bd6feb2009-12-02 08:04:21 +00002639 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2640 assert(Storage);
Abramo Bagnara25777432010-08-11 22:01:17 +00002641 // DNInfo work in progress: CHECKME: what about DNLoc?
2642 return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
John McCall80ad16f2009-11-24 18:42:40 +00002643}
2644
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002645TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor3e1274f2010-06-16 21:09:37 +00002646 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2647 if (TemplateTemplateParmDecl *TTP
2648 = dyn_cast<TemplateTemplateParmDecl>(Template))
2649 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2650
2651 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002652 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor3e1274f2010-06-16 21:09:37 +00002653 }
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002654
John McCall0bd6feb2009-12-02 08:04:21 +00002655 assert(!Name.getAsOverloadedTemplate());
Mike Stump1eb44332009-09-09 15:08:12 +00002656
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002657 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2658 assert(DTN && "Non-dependent template names must refer to template decls.");
2659 return DTN->CanonicalTemplateName;
2660}
2661
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002662bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2663 X = getCanonicalTemplateName(X);
2664 Y = getCanonicalTemplateName(Y);
2665 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2666}
2667
Mike Stump1eb44332009-09-09 15:08:12 +00002668TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002669ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2670 switch (Arg.getKind()) {
2671 case TemplateArgument::Null:
2672 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Douglas Gregor1275ae02009-07-28 23:00:59 +00002674 case TemplateArgument::Expression:
Douglas Gregor1275ae02009-07-28 23:00:59 +00002675 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002676
Douglas Gregor1275ae02009-07-28 23:00:59 +00002677 case TemplateArgument::Declaration:
John McCall833ca992009-10-29 08:12:44 +00002678 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Douglas Gregor788cd062009-11-11 01:00:40 +00002680 case TemplateArgument::Template:
2681 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2682
Douglas Gregor1275ae02009-07-28 23:00:59 +00002683 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002684 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002685 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Douglas Gregor1275ae02009-07-28 23:00:59 +00002687 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00002688 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Douglas Gregor1275ae02009-07-28 23:00:59 +00002690 case TemplateArgument::Pack: {
2691 // FIXME: Allocate in ASTContext
2692 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2693 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002694 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002695 AEnd = Arg.pack_end();
2696 A != AEnd; (void)++A, ++Idx)
2697 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Douglas Gregor1275ae02009-07-28 23:00:59 +00002699 TemplateArgument Result;
2700 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2701 return Result;
2702 }
2703 }
2704
2705 // Silence GCC warning
2706 assert(false && "Unhandled template argument kind");
2707 return TemplateArgument();
2708}
2709
Douglas Gregord57959a2009-03-27 23:10:48 +00002710NestedNameSpecifier *
2711ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002712 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002713 return 0;
2714
2715 switch (NNS->getKind()) {
2716 case NestedNameSpecifier::Identifier:
2717 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002718 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002719 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2720 NNS->getAsIdentifier());
2721
2722 case NestedNameSpecifier::Namespace:
2723 // A namespace is canonical; build a nested-name-specifier with
2724 // this namespace and no prefix.
2725 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2726
2727 case NestedNameSpecifier::TypeSpec:
2728 case NestedNameSpecifier::TypeSpecWithTemplate: {
2729 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002730 return NestedNameSpecifier::Create(*this, 0,
2731 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002732 T.getTypePtr());
2733 }
2734
2735 case NestedNameSpecifier::Global:
2736 // The global specifier is canonical and unique.
2737 return NNS;
2738 }
2739
2740 // Required to silence a GCC warning
2741 return 0;
2742}
2743
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002744
2745const ArrayType *ASTContext::getAsArrayType(QualType T) {
2746 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00002747 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002748 // Handle the common positive case fast.
2749 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2750 return AT;
2751 }
Mike Stump1eb44332009-09-09 15:08:12 +00002752
John McCall0953e762009-09-24 19:53:00 +00002753 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002754 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002755 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002756 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002757
John McCall0953e762009-09-24 19:53:00 +00002758 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002759 // implements C99 6.7.3p8: "If the specification of an array type includes
2760 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002762 // If we get here, we either have type qualifiers on the type, or we have
2763 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002764 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002765
John McCall0953e762009-09-24 19:53:00 +00002766 QualifierCollector Qs;
2767 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002768
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002769 // If we have a simple case, just return now.
2770 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002771 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002772 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002774 // Otherwise, we have an array and we have qualifiers on it. Push the
2775 // qualifiers into the array element type and return a new array type.
2776 // Get the canonical version of the element with the extra qualifiers on it.
2777 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002778 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002779
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002780 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2781 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2782 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002783 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002784 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2785 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2786 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002787 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002788
Mike Stump1eb44332009-09-09 15:08:12 +00002789 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002790 = dyn_cast<DependentSizedArrayType>(ATy))
2791 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002792 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002793 DSAT->getSizeExpr() ?
2794 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002795 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002796 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002797 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002799 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002800 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002801 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002802 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002803 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002804 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002805 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002806}
2807
Chris Lattnere6327742008-04-02 05:18:44 +00002808/// getArrayDecayedType - Return the properly qualified result of decaying the
2809/// specified array type to a pointer. This operation is non-trivial when
2810/// handling typedefs etc. The canonical type of "T" must be an array type,
2811/// this returns a pointer to a properly qualified element of the array.
2812///
2813/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2814QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002815 // Get the element type with 'getAsArrayType' so that we don't lose any
2816 // typedefs in the element type of the array. This also handles propagation
2817 // of type qualifiers from the array type into the element type if present
2818 // (C99 6.7.3p8).
2819 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2820 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002821
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002822 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002823
2824 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002825 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002826}
2827
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002828QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002829 QualifierCollector Qs;
Benjamin Kramer02379412010-04-27 17:12:11 +00002830 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2831 QT = AT->getElementType();
2832 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002833}
2834
Anders Carlssonfbbce492009-09-25 01:23:32 +00002835QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2836 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002837
Anders Carlssonfbbce492009-09-25 01:23:32 +00002838 if (const ArrayType *AT = getAsArrayType(ElemTy))
2839 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Anders Carlsson6183a992008-12-21 03:44:36 +00002841 return ElemTy;
2842}
2843
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002844/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002845uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002846ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2847 uint64_t ElementCount = 1;
2848 do {
2849 ElementCount *= CA->getSize().getZExtValue();
2850 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2851 } while (CA);
2852 return ElementCount;
2853}
2854
Reid Spencer5f016e22007-07-11 17:01:13 +00002855/// getFloatingRank - Return a relative rank for floating point types.
2856/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002857static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002858 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002859 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002860
John McCall183700f2009-09-21 23:43:11 +00002861 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2862 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002863 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002864 case BuiltinType::Float: return FloatRank;
2865 case BuiltinType::Double: return DoubleRank;
2866 case BuiltinType::LongDouble: return LongDoubleRank;
2867 }
2868}
2869
Mike Stump1eb44332009-09-09 15:08:12 +00002870/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2871/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002872/// 'typeDomain' is a real floating point or complex type.
2873/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002874QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2875 QualType Domain) const {
2876 FloatingRank EltRank = getFloatingRank(Size);
2877 if (Domain->isComplexType()) {
2878 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002879 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002880 case FloatRank: return FloatComplexTy;
2881 case DoubleRank: return DoubleComplexTy;
2882 case LongDoubleRank: return LongDoubleComplexTy;
2883 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002884 }
Chris Lattner1361b112008-04-06 23:58:54 +00002885
2886 assert(Domain->isRealFloatingType() && "Unknown domain!");
2887 switch (EltRank) {
2888 default: assert(0 && "getFloatingRank(): illegal value for rank");
2889 case FloatRank: return FloatTy;
2890 case DoubleRank: return DoubleTy;
2891 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002892 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002893}
2894
Chris Lattner7cfeb082008-04-06 23:55:33 +00002895/// getFloatingTypeOrder - Compare the rank of the two specified floating
2896/// point types, ignoring the domain of the type (i.e. 'double' ==
2897/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002898/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002899int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2900 FloatingRank LHSR = getFloatingRank(LHS);
2901 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002902
Chris Lattnera75cea32008-04-06 23:38:49 +00002903 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002904 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002905 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002906 return 1;
2907 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002908}
2909
Chris Lattnerf52ab252008-04-06 22:59:24 +00002910/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2911/// routine will assert if passed a built-in type that isn't an integer or enum,
2912/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002913unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002914 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002915 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00002916 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00002917
Eli Friedmana3426752009-07-05 23:44:27 +00002918 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2919 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2920
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002921 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2922 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2923
2924 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2925 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2926
Chris Lattnerf52ab252008-04-06 22:59:24 +00002927 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002928 default: assert(0 && "getIntegerRank(): not a built-in integer");
2929 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002930 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002931 case BuiltinType::Char_S:
2932 case BuiltinType::Char_U:
2933 case BuiltinType::SChar:
2934 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002935 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002936 case BuiltinType::Short:
2937 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002938 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002939 case BuiltinType::Int:
2940 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002941 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002942 case BuiltinType::Long:
2943 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002944 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002945 case BuiltinType::LongLong:
2946 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002947 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002948 case BuiltinType::Int128:
2949 case BuiltinType::UInt128:
2950 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002951 }
2952}
2953
Eli Friedman04e83572009-08-20 04:21:42 +00002954/// \brief Whether this is a promotable bitfield reference according
2955/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2956///
2957/// \returns the type this bit-field will promote to, or NULL if no
2958/// promotion occurs.
2959QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregorceafbde2010-05-24 20:13:53 +00002960 if (E->isTypeDependent() || E->isValueDependent())
2961 return QualType();
2962
Eli Friedman04e83572009-08-20 04:21:42 +00002963 FieldDecl *Field = E->getBitField();
2964 if (!Field)
2965 return QualType();
2966
2967 QualType FT = Field->getType();
2968
2969 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2970 uint64_t BitWidth = BitWidthAP.getZExtValue();
2971 uint64_t IntSize = getTypeSize(IntTy);
2972 // GCC extension compatibility: if the bit-field size is less than or equal
2973 // to the size of int, it gets promoted no matter what its type is.
2974 // For instance, unsigned long bf : 4 gets promoted to signed int.
2975 if (BitWidth < IntSize)
2976 return IntTy;
2977
2978 if (BitWidth == IntSize)
2979 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2980
2981 // Types bigger than int are not subject to promotions, and therefore act
2982 // like the base type.
2983 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2984 // is ridiculous.
2985 return QualType();
2986}
2987
Eli Friedmana95d7572009-08-19 07:44:53 +00002988/// getPromotedIntegerType - Returns the type that Promotable will
2989/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2990/// integer type.
2991QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2992 assert(!Promotable.isNull());
2993 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00002994 if (const EnumType *ET = Promotable->getAs<EnumType>())
2995 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00002996 if (Promotable->isSignedIntegerType())
2997 return IntTy;
2998 uint64_t PromotableSize = getTypeSize(Promotable);
2999 uint64_t IntSize = getTypeSize(IntTy);
3000 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3001 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3002}
3003
Mike Stump1eb44332009-09-09 15:08:12 +00003004/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00003005/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00003006/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003007int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00003008 Type *LHSC = getCanonicalType(LHS).getTypePtr();
3009 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00003010 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003011
Chris Lattnerf52ab252008-04-06 22:59:24 +00003012 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3013 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Chris Lattner7cfeb082008-04-06 23:55:33 +00003015 unsigned LHSRank = getIntegerRank(LHSC);
3016 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00003017
Chris Lattner7cfeb082008-04-06 23:55:33 +00003018 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
3019 if (LHSRank == RHSRank) return 0;
3020 return LHSRank > RHSRank ? 1 : -1;
3021 }
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Chris Lattner7cfeb082008-04-06 23:55:33 +00003023 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
3024 if (LHSUnsigned) {
3025 // If the unsigned [LHS] type is larger, return it.
3026 if (LHSRank >= RHSRank)
3027 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00003028
Chris Lattner7cfeb082008-04-06 23:55:33 +00003029 // If the signed type can represent all values of the unsigned type, it
3030 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003031 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003032 return -1;
3033 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00003034
Chris Lattner7cfeb082008-04-06 23:55:33 +00003035 // If the unsigned [RHS] type is larger, return it.
3036 if (RHSRank >= LHSRank)
3037 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00003038
Chris Lattner7cfeb082008-04-06 23:55:33 +00003039 // If the signed type can represent all values of the unsigned type, it
3040 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00003041 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00003042 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00003043}
Anders Carlsson71993dd2007-08-17 05:31:46 +00003044
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003045static RecordDecl *
3046CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
3047 SourceLocation L, IdentifierInfo *Id) {
3048 if (Ctx.getLangOptions().CPlusPlus)
3049 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
3050 else
3051 return RecordDecl::Create(Ctx, TK, DC, L, Id);
3052}
3053
Mike Stump1eb44332009-09-09 15:08:12 +00003054// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00003055QualType ASTContext::getCFConstantStringType() {
3056 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003057 CFConstantStringTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003058 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003059 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00003060 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003061
Anders Carlssonf06273f2007-11-19 00:25:30 +00003062 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00003063
Anders Carlsson71993dd2007-08-17 05:31:46 +00003064 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00003065 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00003066 // int flags;
3067 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00003068 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00003069 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00003070 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00003071 FieldTypes[3] = LongTy;
3072
Anders Carlsson71993dd2007-08-17 05:31:46 +00003073 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00003074 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003075 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00003076 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003077 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003078 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003079 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003080 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003081 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003082 }
3083
Douglas Gregor838db382010-02-11 01:19:42 +00003084 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00003085 }
Mike Stump1eb44332009-09-09 15:08:12 +00003086
Anders Carlsson71993dd2007-08-17 05:31:46 +00003087 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00003088}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003089
Douglas Gregor319ac892009-04-23 22:29:11 +00003090void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003091 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003092 assert(Rec && "Invalid CFConstantStringType");
3093 CFConstantStringTypeDecl = Rec->getDecl();
3094}
3095
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003096// getNSConstantStringType - Return the type used for constant NSStrings.
3097QualType ASTContext::getNSConstantStringType() {
3098 if (!NSConstantStringTypeDecl) {
3099 NSConstantStringTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003100 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003101 &Idents.get("__builtin_NSString"));
3102 NSConstantStringTypeDecl->startDefinition();
3103
3104 QualType FieldTypes[3];
3105
3106 // const int *isa;
3107 FieldTypes[0] = getPointerType(IntTy.withConst());
3108 // const char *str;
3109 FieldTypes[1] = getPointerType(CharTy.withConst());
3110 // unsigned int length;
3111 FieldTypes[2] = UnsignedIntTy;
3112
3113 // Create fields
3114 for (unsigned i = 0; i < 3; ++i) {
3115 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3116 SourceLocation(), 0,
3117 FieldTypes[i], /*TInfo=*/0,
3118 /*BitWidth=*/0,
3119 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003120 Field->setAccess(AS_public);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00003121 NSConstantStringTypeDecl->addDecl(Field);
3122 }
3123
3124 NSConstantStringTypeDecl->completeDefinition();
3125 }
3126
3127 return getTagDeclType(NSConstantStringTypeDecl);
3128}
3129
3130void ASTContext::setNSConstantStringType(QualType T) {
3131 const RecordType *Rec = T->getAs<RecordType>();
3132 assert(Rec && "Invalid NSConstantStringType");
3133 NSConstantStringTypeDecl = Rec->getDecl();
3134}
3135
Mike Stump1eb44332009-09-09 15:08:12 +00003136QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003137 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00003138 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003139 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003140 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00003141 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00003142
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003143 QualType FieldTypes[] = {
3144 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00003145 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003146 getPointerType(UnsignedLongTy),
3147 getConstantArrayType(UnsignedLongTy,
3148 llvm::APInt(32, 5), ArrayType::Normal, 0)
3149 };
Mike Stump1eb44332009-09-09 15:08:12 +00003150
Douglas Gregor44b43212008-12-11 16:49:14 +00003151 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00003152 FieldDecl *Field = FieldDecl::Create(*this,
3153 ObjCFastEnumerationStateTypeDecl,
3154 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00003155 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00003156 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00003157 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003158 Field->setAccess(AS_public);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003159 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00003160 }
Mike Stump1eb44332009-09-09 15:08:12 +00003161
Douglas Gregor838db382010-02-11 01:19:42 +00003162 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003163 }
Mike Stump1eb44332009-09-09 15:08:12 +00003164
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00003165 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3166}
3167
Mike Stumpadaaad32009-10-20 02:12:22 +00003168QualType ASTContext::getBlockDescriptorType() {
3169 if (BlockDescriptorType)
3170 return getTagDeclType(BlockDescriptorType);
3171
3172 RecordDecl *T;
3173 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003174 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003175 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00003176 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003177
3178 QualType FieldTypes[] = {
3179 UnsignedLongTy,
3180 UnsignedLongTy,
3181 };
3182
3183 const char *FieldNames[] = {
3184 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00003185 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00003186 };
3187
3188 for (size_t i = 0; i < 2; ++i) {
3189 FieldDecl *Field = FieldDecl::Create(*this,
3190 T,
3191 SourceLocation(),
3192 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003193 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00003194 /*BitWidth=*/0,
3195 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003196 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003197 T->addDecl(Field);
3198 }
3199
Douglas Gregor838db382010-02-11 01:19:42 +00003200 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003201
3202 BlockDescriptorType = T;
3203
3204 return getTagDeclType(BlockDescriptorType);
3205}
3206
3207void ASTContext::setBlockDescriptorType(QualType T) {
3208 const RecordType *Rec = T->getAs<RecordType>();
3209 assert(Rec && "Invalid BlockDescriptorType");
3210 BlockDescriptorType = Rec->getDecl();
3211}
3212
Mike Stump083c25e2009-10-22 00:49:09 +00003213QualType ASTContext::getBlockDescriptorExtendedType() {
3214 if (BlockDescriptorExtendedType)
3215 return getTagDeclType(BlockDescriptorExtendedType);
3216
3217 RecordDecl *T;
3218 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003219 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003220 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00003221 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003222
3223 QualType FieldTypes[] = {
3224 UnsignedLongTy,
3225 UnsignedLongTy,
3226 getPointerType(VoidPtrTy),
3227 getPointerType(VoidPtrTy)
3228 };
3229
3230 const char *FieldNames[] = {
3231 "reserved",
3232 "Size",
3233 "CopyFuncPtr",
3234 "DestroyFuncPtr"
3235 };
3236
3237 for (size_t i = 0; i < 4; ++i) {
3238 FieldDecl *Field = FieldDecl::Create(*this,
3239 T,
3240 SourceLocation(),
3241 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003242 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00003243 /*BitWidth=*/0,
3244 /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003245 Field->setAccess(AS_public);
Mike Stump083c25e2009-10-22 00:49:09 +00003246 T->addDecl(Field);
3247 }
3248
Douglas Gregor838db382010-02-11 01:19:42 +00003249 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003250
3251 BlockDescriptorExtendedType = T;
3252
3253 return getTagDeclType(BlockDescriptorExtendedType);
3254}
3255
3256void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3257 const RecordType *Rec = T->getAs<RecordType>();
3258 assert(Rec && "Invalid BlockDescriptorType");
3259 BlockDescriptorExtendedType = Rec->getDecl();
3260}
3261
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003262bool ASTContext::BlockRequiresCopying(QualType Ty) {
3263 if (Ty->isBlockPointerType())
3264 return true;
3265 if (isObjCNSObjectType(Ty))
3266 return true;
3267 if (Ty->isObjCObjectPointerType())
3268 return true;
3269 return false;
3270}
3271
Daniel Dunbar4087f272010-08-17 22:39:59 +00003272QualType ASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003273 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003274 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003275 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003276 // unsigned int __flags;
3277 // unsigned int __size;
Eli Friedmana7e68452010-08-22 01:00:03 +00003278 // void *__copy_helper; // as needed
3279 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003280 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003281 // } *
3282
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003283 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3284
3285 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003286 llvm::SmallString<36> Name;
3287 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3288 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003289 RecordDecl *T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003290 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003291 &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003292 T->startDefinition();
3293 QualType Int32Ty = IntTy;
3294 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3295 QualType FieldTypes[] = {
3296 getPointerType(VoidPtrTy),
3297 getPointerType(getTagDeclType(T)),
3298 Int32Ty,
3299 Int32Ty,
3300 getPointerType(VoidPtrTy),
3301 getPointerType(VoidPtrTy),
3302 Ty
3303 };
3304
Daniel Dunbar4087f272010-08-17 22:39:59 +00003305 llvm::StringRef FieldNames[] = {
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003306 "__isa",
3307 "__forwarding",
3308 "__flags",
3309 "__size",
3310 "__copy_helper",
3311 "__destroy_helper",
3312 DeclName,
3313 };
3314
3315 for (size_t i = 0; i < 7; ++i) {
3316 if (!HasCopyAndDispose && i >=4 && i <= 5)
3317 continue;
3318 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3319 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003320 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003321 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003322 Field->setAccess(AS_public);
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003323 T->addDecl(Field);
3324 }
3325
Douglas Gregor838db382010-02-11 01:19:42 +00003326 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003327
3328 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003329}
3330
3331
3332QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00003333 bool BlockHasCopyDispose,
John McCallea1471e2010-05-20 01:18:31 +00003334 llvm::SmallVectorImpl<const Expr *> &Layout) {
3335
Mike Stumpadaaad32009-10-20 02:12:22 +00003336 // FIXME: Move up
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003337 llvm::SmallString<36> Name;
3338 llvm::raw_svector_ostream(Name) << "__block_literal_"
3339 << ++UniqueBlockParmTypeID;
Mike Stumpadaaad32009-10-20 02:12:22 +00003340 RecordDecl *T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003341 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003342 &Idents.get(Name.str()));
John McCall5cfa0112010-02-05 01:33:36 +00003343 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003344 QualType FieldTypes[] = {
3345 getPointerType(VoidPtrTy),
3346 IntTy,
3347 IntTy,
3348 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00003349 (BlockHasCopyDispose ?
3350 getPointerType(getBlockDescriptorExtendedType()) :
3351 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00003352 };
3353
3354 const char *FieldNames[] = {
3355 "__isa",
3356 "__flags",
3357 "__reserved",
3358 "__FuncPtr",
3359 "__descriptor"
3360 };
3361
3362 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00003363 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003364 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003365 FieldTypes[i], /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003366 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003367 Field->setAccess(AS_public);
Mike Stumpea26cb52009-10-21 03:49:08 +00003368 T->addDecl(Field);
3369 }
3370
John McCallea1471e2010-05-20 01:18:31 +00003371 for (unsigned i = 0; i < Layout.size(); ++i) {
3372 const Expr *E = Layout[i];
Mike Stumpea26cb52009-10-21 03:49:08 +00003373
John McCallea1471e2010-05-20 01:18:31 +00003374 QualType FieldType = E->getType();
3375 IdentifierInfo *FieldName = 0;
3376 if (isa<CXXThisExpr>(E)) {
3377 FieldName = &Idents.get("this");
3378 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3379 const ValueDecl *D = BDRE->getDecl();
3380 FieldName = D->getIdentifier();
3381 if (BDRE->isByRef())
Daniel Dunbar4087f272010-08-17 22:39:59 +00003382 FieldType = BuildByRefType(D->getName(), FieldType);
John McCallea1471e2010-05-20 01:18:31 +00003383 } else {
3384 // Padding.
3385 assert(isa<ConstantArrayType>(FieldType) &&
3386 isa<DeclRefExpr>(E) &&
3387 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3388 "doesn't match characteristics of padding decl");
3389 }
Mike Stumpea26cb52009-10-21 03:49:08 +00003390
3391 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallea1471e2010-05-20 01:18:31 +00003392 FieldName, FieldType, /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003393 /*BitWidth=*/0, /*Mutable=*/false);
John McCall2888b652010-04-30 21:35:41 +00003394 Field->setAccess(AS_public);
Mike Stumpadaaad32009-10-20 02:12:22 +00003395 T->addDecl(Field);
3396 }
3397
Douglas Gregor838db382010-02-11 01:19:42 +00003398 T->completeDefinition();
Mike Stumpea26cb52009-10-21 03:49:08 +00003399
3400 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00003401}
3402
Douglas Gregor319ac892009-04-23 22:29:11 +00003403void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003404 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003405 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3406 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3407}
3408
Anders Carlssone8c49532007-10-29 06:33:42 +00003409// This returns true if a type has been typedefed to BOOL:
3410// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003411static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003412 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003413 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3414 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003415
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003416 return false;
3417}
3418
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003419/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003420/// purpose.
Ken Dyckaa8741a2010-01-11 19:19:56 +00003421CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck199c3d62010-01-11 17:06:35 +00003422 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003423
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003424 // Make all integer and enum types at least as large as an int
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003425 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003426 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003427 // Treat arrays as pointers, since that's how they're passed in.
3428 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003429 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003430 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003431}
3432
3433static inline
3434std::string charUnitsToString(const CharUnits &CU) {
3435 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003436}
3437
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003438/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall5e530af2009-11-17 19:33:30 +00003439/// declaration.
3440void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3441 std::string& S) {
3442 const BlockDecl *Decl = Expr->getBlockDecl();
3443 QualType BlockTy =
3444 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3445 // Encode result type.
John McCallc71a4912010-06-04 19:02:56 +00003446 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall5e530af2009-11-17 19:33:30 +00003447 // Compute size of all parameters.
3448 // Start with computing size of a pointer in number of bytes.
3449 // FIXME: There might(should) be a better way of doing this computation!
3450 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003451 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3452 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian6f46c262010-04-08 18:06:22 +00003453 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall5e530af2009-11-17 19:33:30 +00003454 E = Decl->param_end(); PI != E; ++PI) {
3455 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003456 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003457 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003458 ParmOffset += sz;
3459 }
3460 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003461 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003462 // Block pointer and offset.
3463 S += "@?0";
3464 ParmOffset = PtrSize;
3465
3466 // Argument types.
3467 ParmOffset = PtrSize;
3468 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3469 Decl->param_end(); PI != E; ++PI) {
3470 ParmVarDecl *PVDecl = *PI;
3471 QualType PType = PVDecl->getOriginalType();
3472 if (const ArrayType *AT =
3473 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3474 // Use array's original type only if it has known number of
3475 // elements.
3476 if (!isa<ConstantArrayType>(AT))
3477 PType = PVDecl->getType();
3478 } else if (PType->isFunctionType())
3479 PType = PVDecl->getType();
3480 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003481 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003482 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003483 }
3484}
3485
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003486/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003487/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003488void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003489 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003490 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003491 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003492 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003493 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003494 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003495 // Compute size of all parameters.
3496 // Start with computing size of a pointer in number of bytes.
3497 // FIXME: There might(should) be a better way of doing this computation!
3498 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003499 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003500 // The first two arguments (self and _cmd) are pointers; account for
3501 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003502 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003503 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003504 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003505 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003506 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003507 assert (sz.isPositive() &&
3508 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003509 ParmOffset += sz;
3510 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003511 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003512 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003513 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003514
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003515 // Argument types.
3516 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003517 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahanian7732cc92010-04-08 21:29:11 +00003518 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattner89951a82009-02-20 18:43:26 +00003519 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003520 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003521 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003522 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3523 // Use array's original type only if it has known number of
3524 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003525 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003526 PType = PVDecl->getType();
3527 } else if (PType->isFunctionType())
3528 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003529 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003530 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003531 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003532 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003533 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003534 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003535 }
3536}
3537
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003538/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003539/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003540/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3541/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003542/// Property attributes are stored as a comma-delimited C string. The simple
3543/// attributes readonly and bycopy are encoded as single characters. The
3544/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3545/// encoded as single characters, followed by an identifier. Property types
3546/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003547/// these attributes are defined by the following enumeration:
3548/// @code
3549/// enum PropertyAttributes {
3550/// kPropertyReadOnly = 'R', // property is read-only.
3551/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3552/// kPropertyByref = '&', // property is a reference to the value last assigned
3553/// kPropertyDynamic = 'D', // property is dynamic
3554/// kPropertyGetter = 'G', // followed by getter selector name
3555/// kPropertySetter = 'S', // followed by setter selector name
3556/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3557/// kPropertyType = 't' // followed by old-style type encoding.
3558/// kPropertyWeak = 'W' // 'weak' property
3559/// kPropertyStrong = 'P' // property GC'able
3560/// kPropertyNonAtomic = 'N' // property non-atomic
3561/// };
3562/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003563void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003564 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003565 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003566 // Collect information from the property implementation decl(s).
3567 bool Dynamic = false;
3568 ObjCPropertyImplDecl *SynthesizePID = 0;
3569
3570 // FIXME: Duplicated code due to poor abstraction.
3571 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003572 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003573 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3574 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003575 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003576 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003577 ObjCPropertyImplDecl *PID = *i;
3578 if (PID->getPropertyDecl() == PD) {
3579 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3580 Dynamic = true;
3581 } else {
3582 SynthesizePID = PID;
3583 }
3584 }
3585 }
3586 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003587 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003588 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003589 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003590 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003591 ObjCPropertyImplDecl *PID = *i;
3592 if (PID->getPropertyDecl() == PD) {
3593 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3594 Dynamic = true;
3595 } else {
3596 SynthesizePID = PID;
3597 }
3598 }
Mike Stump1eb44332009-09-09 15:08:12 +00003599 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003600 }
3601 }
3602
3603 // FIXME: This is not very efficient.
3604 S = "T";
3605
3606 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003607 // GCC has some special rules regarding encoding of properties which
3608 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003609 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003610 true /* outermost type */,
3611 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003612
3613 if (PD->isReadOnly()) {
3614 S += ",R";
3615 } else {
3616 switch (PD->getSetterKind()) {
3617 case ObjCPropertyDecl::Assign: break;
3618 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003619 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003620 }
3621 }
3622
3623 // It really isn't clear at all what this means, since properties
3624 // are "dynamic by default".
3625 if (Dynamic)
3626 S += ",D";
3627
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003628 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3629 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003630
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003631 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3632 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003633 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003634 }
3635
3636 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3637 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003638 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003639 }
3640
3641 if (SynthesizePID) {
3642 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3643 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003644 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003645 }
3646
3647 // FIXME: OBJCGC: weak & strong
3648}
3649
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003650/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003651/// Another legacy compatibility encoding: 32-bit longs are encoded as
3652/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003653/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3654///
3655void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003656 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003657 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003658 if (BT->getKind() == BuiltinType::ULong &&
3659 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003660 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003661 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003662 if (BT->getKind() == BuiltinType::Long &&
3663 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003664 PointeeTy = IntTy;
3665 }
3666 }
3667}
3668
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003669void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003670 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003671 // We follow the behavior of gcc, expanding structures which are
3672 // directly pointed to, and expanding embedded structures. Note that
3673 // these rules are sufficient to prevent recursive encoding of the
3674 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003675 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003676 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003677}
3678
David Chisnall64fd7e82010-06-04 01:10:52 +00003679static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3680 switch (T->getAs<BuiltinType>()->getKind()) {
3681 default: assert(0 && "Unhandled builtin type kind");
3682 case BuiltinType::Void: return 'v';
3683 case BuiltinType::Bool: return 'B';
3684 case BuiltinType::Char_U:
3685 case BuiltinType::UChar: return 'C';
3686 case BuiltinType::UShort: return 'S';
3687 case BuiltinType::UInt: return 'I';
3688 case BuiltinType::ULong:
3689 return
3690 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3691 case BuiltinType::UInt128: return 'T';
3692 case BuiltinType::ULongLong: return 'Q';
3693 case BuiltinType::Char_S:
3694 case BuiltinType::SChar: return 'c';
3695 case BuiltinType::Short: return 's';
John McCall24da7092010-06-11 10:11:05 +00003696 case BuiltinType::WChar:
David Chisnall64fd7e82010-06-04 01:10:52 +00003697 case BuiltinType::Int: return 'i';
3698 case BuiltinType::Long:
3699 return
3700 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3701 case BuiltinType::LongLong: return 'q';
3702 case BuiltinType::Int128: return 't';
3703 case BuiltinType::Float: return 'f';
3704 case BuiltinType::Double: return 'd';
Daniel Dunbar3a0be842010-10-11 21:13:48 +00003705 case BuiltinType::LongDouble: return 'D';
David Chisnall64fd7e82010-06-04 01:10:52 +00003706 }
3707}
3708
Mike Stump1eb44332009-09-09 15:08:12 +00003709static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnall64fd7e82010-06-04 01:10:52 +00003710 QualType T, const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003711 const Expr *E = FD->getBitWidth();
3712 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3713 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003714 S += 'b';
David Chisnall64fd7e82010-06-04 01:10:52 +00003715 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3716 // The GNU runtime requires more information; bitfields are encoded as b,
3717 // then the offset (in bits) of the first element, then the type of the
3718 // bitfield, then the size in bits. For example, in this structure:
3719 //
3720 // struct
3721 // {
3722 // int integer;
3723 // int flags:2;
3724 // };
3725 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3726 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3727 // information is not especially sensible, but we're stuck with it for
3728 // compatibility with GCC, although providing it breaks anything that
3729 // actually uses runtime introspection and wants to work on both runtimes...
3730 if (!Ctx->getLangOptions().NeXTRuntime) {
3731 const RecordDecl *RD = FD->getParent();
3732 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3733 // FIXME: This same linear search is also used in ExprConstant - it might
3734 // be better if the FieldDecl stored its offset. We'd be increasing the
3735 // size of the object slightly, but saving some time every time it is used.
3736 unsigned i = 0;
3737 for (RecordDecl::field_iterator Field = RD->field_begin(),
3738 FieldEnd = RD->field_end();
3739 Field != FieldEnd; (void)++Field, ++i) {
3740 if (*Field == FD)
3741 break;
3742 }
3743 S += llvm::utostr(RL.getFieldOffset(i));
3744 S += ObjCEncodingForPrimitiveKind(Context, T);
3745 }
3746 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003747 S += llvm::utostr(N);
3748}
3749
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003750// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003751void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3752 bool ExpandPointedToStructures,
3753 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003754 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003755 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003756 bool EncodingProperty) {
David Chisnall64fd7e82010-06-04 01:10:52 +00003757 if (T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003758 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00003759 return EncodeBitField(this, S, T, FD);
3760 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003761 return;
3762 }
Mike Stump1eb44332009-09-09 15:08:12 +00003763
John McCall183700f2009-09-21 23:43:11 +00003764 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003765 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003766 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003767 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003768 return;
3769 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00003770
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003771 // encoding for pointer or r3eference types.
3772 QualType PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003773 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003774 if (PT->isObjCSelType()) {
3775 S += ':';
3776 return;
3777 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003778 PointeeTy = PT->getPointeeType();
3779 }
3780 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3781 PointeeTy = RT->getPointeeType();
3782 if (!PointeeTy.isNull()) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003783 bool isReadOnly = false;
3784 // For historical/compatibility reasons, the read-only qualifier of the
3785 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3786 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003787 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003788 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003789 if (OutermostType && T.isConstQualified()) {
3790 isReadOnly = true;
3791 S += 'r';
3792 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003793 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003794 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003795 while (P->getAs<PointerType>())
3796 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003797 if (P.isConstQualified()) {
3798 isReadOnly = true;
3799 S += 'r';
3800 }
3801 }
3802 if (isReadOnly) {
3803 // Another legacy compatibility encoding. Some ObjC qualifier and type
3804 // combinations need to be rearranged.
3805 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer02379412010-04-27 17:12:11 +00003806 if (llvm::StringRef(S).endswith("nr"))
3807 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003808 }
Mike Stump1eb44332009-09-09 15:08:12 +00003809
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003810 if (PointeeTy->isCharType()) {
3811 // char pointer types should be encoded as '*' unless it is a
3812 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003813 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003814 S += '*';
3815 return;
3816 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003817 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003818 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3819 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3820 S += '#';
3821 return;
3822 }
3823 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3824 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3825 S += '@';
3826 return;
3827 }
3828 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003829 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003830 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003831 getLegacyIntegralTypeEncoding(PointeeTy);
3832
Mike Stump1eb44332009-09-09 15:08:12 +00003833 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003834 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003835 return;
3836 }
Fariborz Jahanianaa1d7612010-04-13 23:45:47 +00003837
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003838 if (const ArrayType *AT =
3839 // Ignore type qualifiers etc.
3840 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003841 if (isa<IncompleteArrayType>(AT)) {
3842 // Incomplete arrays are encoded as a pointer to the array element.
3843 S += '^';
3844
Mike Stump1eb44332009-09-09 15:08:12 +00003845 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003846 false, ExpandStructures, FD);
3847 } else {
3848 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003849
Anders Carlsson559a8332009-02-22 01:38:57 +00003850 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3851 S += llvm::utostr(CAT->getSize().getZExtValue());
3852 else {
3853 //Variable length arrays are encoded as a regular array with 0 elements.
3854 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3855 S += '0';
3856 }
Mike Stump1eb44332009-09-09 15:08:12 +00003857
3858 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003859 false, ExpandStructures, FD);
3860 S += ']';
3861 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003862 return;
3863 }
Mike Stump1eb44332009-09-09 15:08:12 +00003864
John McCall183700f2009-09-21 23:43:11 +00003865 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003866 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003867 return;
3868 }
Mike Stump1eb44332009-09-09 15:08:12 +00003869
Ted Kremenek6217b802009-07-29 21:53:49 +00003870 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003871 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003872 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003873 // Anonymous structures print as '?'
3874 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3875 S += II->getName();
Fariborz Jahanian6fb94392010-05-07 00:28:49 +00003876 if (ClassTemplateSpecializationDecl *Spec
3877 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3878 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3879 std::string TemplateArgsStr
3880 = TemplateSpecializationType::PrintTemplateArgumentList(
3881 TemplateArgs.getFlatArgumentList(),
3882 TemplateArgs.flat_size(),
3883 (*this).PrintingPolicy);
3884
3885 S += TemplateArgsStr;
3886 }
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003887 } else {
3888 S += '?';
3889 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003890 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003891 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003892 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3893 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003894 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003895 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003896 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003897 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003898 S += '"';
3899 }
Mike Stump1eb44332009-09-09 15:08:12 +00003900
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003901 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003902 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003903 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003904 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003905 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003906 QualType qt = Field->getType();
3907 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003908 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003909 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003910 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003911 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003912 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003913 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003914 return;
3915 }
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00003916
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003917 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003918 if (FD && FD->isBitField())
David Chisnall64fd7e82010-06-04 01:10:52 +00003919 EncodeBitField(this, S, T, FD);
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003920 else
3921 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003922 return;
3923 }
Mike Stump1eb44332009-09-09 15:08:12 +00003924
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003925 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003926 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003927 return;
3928 }
Mike Stump1eb44332009-09-09 15:08:12 +00003929
John McCallc12c5bb2010-05-15 11:32:37 +00003930 // Ignore protocol qualifiers when mangling at this level.
3931 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3932 T = OT->getBaseType();
3933
John McCall0953e762009-09-24 19:53:00 +00003934 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003935 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003936 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003937 S += '{';
3938 const IdentifierInfo *II = OI->getIdentifier();
3939 S += II->getName();
3940 S += '=';
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003941 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
3942 DeepCollectObjCIvars(OI, true, Ivars);
3943 for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
3944 FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
3945 if (Field->isBitField())
3946 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003947 else
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003948 getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003949 }
3950 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003951 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003952 }
Mike Stump1eb44332009-09-09 15:08:12 +00003953
John McCall183700f2009-09-21 23:43:11 +00003954 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003955 if (OPT->isObjCIdType()) {
3956 S += '@';
3957 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003958 }
Mike Stump1eb44332009-09-09 15:08:12 +00003959
Steve Naroff27d20a22009-10-28 22:03:49 +00003960 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3961 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3962 // Since this is a binary compatibility issue, need to consult with runtime
3963 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00003964 S += '#';
3965 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003966 }
Mike Stump1eb44332009-09-09 15:08:12 +00003967
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003968 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003969 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003970 ExpandPointedToStructures,
3971 ExpandStructures, FD);
3972 if (FD || EncodingProperty) {
3973 // Note that we do extended encoding of protocol qualifer list
3974 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003975 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003976 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3977 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003978 S += '<';
3979 S += (*I)->getNameAsString();
3980 S += '>';
3981 }
3982 S += '"';
3983 }
3984 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003985 }
Mike Stump1eb44332009-09-09 15:08:12 +00003986
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003987 QualType PointeeTy = OPT->getPointeeType();
3988 if (!EncodingProperty &&
3989 isa<TypedefType>(PointeeTy.getTypePtr())) {
3990 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003991 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003992 // {...};
3993 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003994 getObjCEncodingForTypeImpl(PointeeTy, S,
3995 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003996 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003997 return;
3998 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003999
4000 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00004001 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004002 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00004003 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00004004 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
4005 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004006 S += '<';
4007 S += (*I)->getNameAsString();
4008 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00004009 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004010 S += '"';
4011 }
4012 return;
4013 }
Mike Stump1eb44332009-09-09 15:08:12 +00004014
John McCall532ec7b2010-05-17 23:56:34 +00004015 // gcc just blithely ignores member pointers.
4016 // TODO: maybe there should be a mangling for these
4017 if (T->getAs<MemberPointerType>())
4018 return;
Fariborz Jahaniane6012c72010-10-07 21:25:25 +00004019
4020 if (T->isVectorType()) {
4021 // This matches gcc's encoding, even though technically it is
4022 // insufficient.
4023 // FIXME. We should do a better job than gcc.
4024 return;
4025 }
4026
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004027 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00004028}
4029
Mike Stump1eb44332009-09-09 15:08:12 +00004030void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00004031 std::string& S) const {
4032 if (QT & Decl::OBJC_TQ_In)
4033 S += 'n';
4034 if (QT & Decl::OBJC_TQ_Inout)
4035 S += 'N';
4036 if (QT & Decl::OBJC_TQ_Out)
4037 S += 'o';
4038 if (QT & Decl::OBJC_TQ_Bycopy)
4039 S += 'O';
4040 if (QT & Decl::OBJC_TQ_Byref)
4041 S += 'R';
4042 if (QT & Decl::OBJC_TQ_Oneway)
4043 S += 'V';
4044}
4045
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004046void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004047 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004048
Anders Carlssonb2cf3572007-10-11 01:00:40 +00004049 BuiltinVaListType = T;
4050}
4051
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004052void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004053 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00004054}
4055
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004056void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00004057 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00004058}
4059
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004060void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004061 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00004062}
4063
Chris Lattnerce7b38c2009-07-13 00:10:46 +00004064void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004065 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00004066}
4067
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004068void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00004069 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00004070 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00004071
Ted Kremeneka526c5c2008-01-07 19:49:32 +00004072 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00004073}
4074
John McCall0bd6feb2009-12-02 08:04:21 +00004075/// \brief Retrieve the template name that corresponds to a non-empty
4076/// lookup.
John McCalleec51cf2010-01-20 00:46:10 +00004077TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
4078 UnresolvedSetIterator End) {
John McCall0bd6feb2009-12-02 08:04:21 +00004079 unsigned size = End - Begin;
4080 assert(size > 1 && "set is not overloaded!");
4081
4082 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
4083 size * sizeof(FunctionTemplateDecl*));
4084 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
4085
4086 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00004087 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00004088 NamedDecl *D = *I;
4089 assert(isa<FunctionTemplateDecl>(D) ||
4090 (isa<UsingShadowDecl>(D) &&
4091 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
4092 *Storage++ = D;
4093 }
4094
4095 return TemplateName(OT);
4096}
4097
Douglas Gregor7532dc62009-03-30 22:58:21 +00004098/// \brief Retrieve the template name that represents a qualified
4099/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00004100TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004101 bool TemplateKeyword,
4102 TemplateDecl *Template) {
Douglas Gregor789b1f62010-02-04 18:10:26 +00004103 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00004104 llvm::FoldingSetNodeID ID;
4105 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
4106
4107 void *InsertPos = 0;
4108 QualifiedTemplateName *QTN =
4109 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4110 if (!QTN) {
4111 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4112 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4113 }
4114
4115 return TemplateName(QTN);
4116}
4117
4118/// \brief Retrieve the template name that represents a dependent
4119/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00004120TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004121 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004122 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00004123 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00004124
4125 llvm::FoldingSetNodeID ID;
4126 DependentTemplateName::Profile(ID, NNS, Name);
4127
4128 void *InsertPos = 0;
4129 DependentTemplateName *QTN =
4130 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4131
4132 if (QTN)
4133 return TemplateName(QTN);
4134
4135 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4136 if (CanonNNS == NNS) {
4137 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4138 } else {
4139 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4140 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004141 DependentTemplateName *CheckQTN =
4142 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4143 assert(!CheckQTN && "Dependent type name canonicalization broken");
4144 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00004145 }
4146
4147 DependentTemplateNames.InsertNode(QTN, InsertPos);
4148 return TemplateName(QTN);
4149}
4150
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004151/// \brief Retrieve the template name that represents a dependent
4152/// template name such as \c MetaFun::template operator+.
4153TemplateName
4154ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4155 OverloadedOperatorKind Operator) {
4156 assert((!NNS || NNS->isDependent()) &&
4157 "Nested name specifier must be dependent");
4158
4159 llvm::FoldingSetNodeID ID;
4160 DependentTemplateName::Profile(ID, NNS, Operator);
4161
4162 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00004163 DependentTemplateName *QTN
4164 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004165
4166 if (QTN)
4167 return TemplateName(QTN);
4168
4169 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4170 if (CanonNNS == NNS) {
4171 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4172 } else {
4173 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4174 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00004175
4176 DependentTemplateName *CheckQTN
4177 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4178 assert(!CheckQTN && "Dependent template name canonicalization broken");
4179 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00004180 }
4181
4182 DependentTemplateNames.InsertNode(QTN, InsertPos);
4183 return TemplateName(QTN);
4184}
4185
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004186/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00004187/// TargetInfo, produce the corresponding type. The unsigned @p Type
4188/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00004189CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004190 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00004191 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004192 case TargetInfo::SignedShort: return ShortTy;
4193 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4194 case TargetInfo::SignedInt: return IntTy;
4195 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4196 case TargetInfo::SignedLong: return LongTy;
4197 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4198 case TargetInfo::SignedLongLong: return LongLongTy;
4199 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4200 }
4201
4202 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00004203 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00004204}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00004205
4206//===----------------------------------------------------------------------===//
4207// Type Predicates.
4208//===----------------------------------------------------------------------===//
4209
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004210/// isObjCNSObjectType - Return true if this is an NSObject object using
4211/// NSObject attribute on a c-style pointer type.
4212/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00004213/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004214///
4215bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4216 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4217 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00004218 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004219 return true;
4220 }
Mike Stump1eb44332009-09-09 15:08:12 +00004221 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00004222}
4223
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004224/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4225/// garbage collection attribute.
4226///
John McCall0953e762009-09-24 19:53:00 +00004227Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4228 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004229 if (getLangOptions().ObjC1 &&
4230 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00004231 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004232 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00004233 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004234 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00004235 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00004236 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004237 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004238 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00004239 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004240 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00004241 // Non-pointers have none gc'able attribute regardless of the attribute
4242 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00004243 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004244 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004245 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00004246 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004247}
4248
Chris Lattner6ac46a42008-04-07 06:51:04 +00004249//===----------------------------------------------------------------------===//
4250// Type Compatibility Testing
4251//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00004252
Mike Stump1eb44332009-09-09 15:08:12 +00004253/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004254/// compatible.
4255static bool areCompatVectorTypes(const VectorType *LHS,
4256 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00004257 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00004258 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00004259 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00004260}
4261
Douglas Gregor255210e2010-08-06 10:14:59 +00004262bool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4263 QualType SecondVec) {
4264 assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4265 assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4266
4267 if (hasSameUnqualifiedType(FirstVec, SecondVec))
4268 return true;
4269
4270 // AltiVec vectors types are identical to equivalent GCC vector types
4271 const VectorType *First = FirstVec->getAs<VectorType>();
4272 const VectorType *Second = SecondVec->getAs<VectorType>();
4273 if ((((First->getAltiVecSpecific() == VectorType::AltiVec) &&
4274 (Second->getAltiVecSpecific() == VectorType::NotAltiVec)) ||
4275 ((First->getAltiVecSpecific() == VectorType::NotAltiVec) &&
4276 (Second->getAltiVecSpecific() == VectorType::AltiVec))) &&
4277 hasSameType(First->getElementType(), Second->getElementType()) &&
4278 (First->getNumElements() == Second->getNumElements()))
4279 return true;
4280
4281 return false;
4282}
4283
Steve Naroff4084c302009-07-23 01:01:38 +00004284//===----------------------------------------------------------------------===//
4285// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4286//===----------------------------------------------------------------------===//
4287
4288/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4289/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004290bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4291 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00004292 if (lProto == rProto)
4293 return true;
4294 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4295 E = rProto->protocol_end(); PI != E; ++PI)
4296 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4297 return true;
4298 return false;
4299}
4300
Steve Naroff4084c302009-07-23 01:01:38 +00004301/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4302/// return true if lhs's protocols conform to rhs's protocol; false
4303/// otherwise.
4304bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4305 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4306 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4307 return false;
4308}
4309
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004310/// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and
4311/// Class<p1, ...>.
4312bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4313 QualType rhs) {
4314 const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4315 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4316 assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4317
4318 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4319 E = lhsQID->qual_end(); I != E; ++I) {
4320 bool match = false;
4321 ObjCProtocolDecl *lhsProto = *I;
4322 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4323 E = rhsOPT->qual_end(); J != E; ++J) {
4324 ObjCProtocolDecl *rhsProto = *J;
4325 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4326 match = true;
4327 break;
4328 }
4329 }
4330 if (!match)
4331 return false;
4332 }
4333 return true;
4334}
4335
Steve Naroff4084c302009-07-23 01:01:38 +00004336/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4337/// ObjCQualifiedIDType.
4338bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4339 bool compare) {
4340 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00004341 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004342 lhs->isObjCIdType() || lhs->isObjCClassType())
4343 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004344 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004345 rhs->isObjCIdType() || rhs->isObjCClassType())
4346 return true;
4347
4348 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00004349 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004350
Steve Naroff4084c302009-07-23 01:01:38 +00004351 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004352
Steve Naroff4084c302009-07-23 01:01:38 +00004353 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004354 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00004355 // make sure we check the class hierarchy.
4356 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4357 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4358 E = lhsQID->qual_end(); I != E; ++I) {
4359 // when comparing an id<P> on lhs with a static type on rhs,
4360 // see if static class implements all of id's protocols, directly or
4361 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004362 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00004363 return false;
4364 }
4365 }
4366 // If there are no qualifiers and no interface, we have an 'id'.
4367 return true;
4368 }
Mike Stump1eb44332009-09-09 15:08:12 +00004369 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004370 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4371 E = lhsQID->qual_end(); I != E; ++I) {
4372 ObjCProtocolDecl *lhsProto = *I;
4373 bool match = false;
4374
4375 // when comparing an id<P> on lhs with a static type on rhs,
4376 // see if static class implements all of id's protocols, directly or
4377 // through its super class and categories.
4378 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4379 E = rhsOPT->qual_end(); J != E; ++J) {
4380 ObjCProtocolDecl *rhsProto = *J;
4381 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4382 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4383 match = true;
4384 break;
4385 }
4386 }
Mike Stump1eb44332009-09-09 15:08:12 +00004387 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004388 // make sure we check the class hierarchy.
4389 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4390 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4391 E = lhsQID->qual_end(); I != E; ++I) {
4392 // when comparing an id<P> on lhs with a static type on rhs,
4393 // see if static class implements all of id's protocols, directly or
4394 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004395 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004396 match = true;
4397 break;
4398 }
4399 }
4400 }
4401 if (!match)
4402 return false;
4403 }
Mike Stump1eb44332009-09-09 15:08:12 +00004404
Steve Naroff4084c302009-07-23 01:01:38 +00004405 return true;
4406 }
Mike Stump1eb44332009-09-09 15:08:12 +00004407
Steve Naroff4084c302009-07-23 01:01:38 +00004408 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4409 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4410
Mike Stump1eb44332009-09-09 15:08:12 +00004411 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004412 lhs->getAsObjCInterfacePointerType()) {
4413 if (lhsOPT->qual_empty()) {
4414 bool match = false;
4415 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4416 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4417 E = rhsQID->qual_end(); I != E; ++I) {
Fariborz Jahaniand1909bb2010-08-09 18:21:43 +00004418 // when comparing an id<P> on rhs with a static type on lhs,
4419 // static class must implement all of id's protocols directly or
4420 // indirectly through its super class.
Fariborz Jahanian192b1462010-08-12 20:46:12 +00004421 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004422 match = true;
4423 break;
4424 }
4425 }
4426 if (!match)
4427 return false;
4428 }
4429 return true;
4430 }
Mike Stump1eb44332009-09-09 15:08:12 +00004431 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004432 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4433 E = lhsOPT->qual_end(); I != E; ++I) {
4434 ObjCProtocolDecl *lhsProto = *I;
4435 bool match = false;
4436
4437 // when comparing an id<P> on lhs with a static type on rhs,
4438 // see if static class implements all of id's protocols, directly or
4439 // through its super class and categories.
4440 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4441 E = rhsQID->qual_end(); J != E; ++J) {
4442 ObjCProtocolDecl *rhsProto = *J;
4443 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4444 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4445 match = true;
4446 break;
4447 }
4448 }
4449 if (!match)
4450 return false;
4451 }
4452 return true;
4453 }
4454 return false;
4455}
4456
Eli Friedman3d815e72008-08-22 00:56:42 +00004457/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004458/// compatible for assignment from RHS to LHS. This handles validation of any
4459/// protocol qualifiers on the LHS or RHS.
4460///
Steve Naroff14108da2009-07-10 23:34:53 +00004461bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4462 const ObjCObjectPointerType *RHSOPT) {
John McCallc12c5bb2010-05-15 11:32:37 +00004463 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4464 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4465
Steve Naroffde2e22d2009-07-15 18:40:39 +00004466 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCallc12c5bb2010-05-15 11:32:37 +00004467 if (LHS->isObjCUnqualifiedIdOrClass() ||
4468 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff14108da2009-07-10 23:34:53 +00004469 return true;
4470
John McCallc12c5bb2010-05-15 11:32:37 +00004471 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump1eb44332009-09-09 15:08:12 +00004472 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4473 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004474 false);
Fariborz Jahaniana8f8dac2010-07-19 22:02:22 +00004475
4476 if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
4477 return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
4478 QualType(RHSOPT,0));
4479
John McCallc12c5bb2010-05-15 11:32:37 +00004480 // If we have 2 user-defined types, fall into that path.
4481 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff4084c302009-07-23 01:01:38 +00004482 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004483
Steve Naroff4084c302009-07-23 01:01:38 +00004484 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004485}
4486
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004487/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4488/// for providing type-safty for objective-c pointers used to pass/return
4489/// arguments in block literals. When passed as arguments, passing 'A*' where
4490/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4491/// not OK. For the return type, the opposite is not OK.
4492bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4493 const ObjCObjectPointerType *LHSOPT,
4494 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004495 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004496 return true;
4497
4498 if (LHSOPT->isObjCBuiltinType()) {
4499 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4500 }
4501
Fariborz Jahaniana9834482010-04-06 17:23:39 +00004502 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004503 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4504 QualType(RHSOPT,0),
4505 false);
4506
4507 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4508 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4509 if (LHS && RHS) { // We have 2 user-defined types.
4510 if (LHS != RHS) {
4511 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4512 return false;
4513 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4514 return true;
4515 }
4516 else
4517 return true;
4518 }
4519 return false;
4520}
4521
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004522/// getIntersectionOfProtocols - This routine finds the intersection of set
4523/// of protocols inherited from two distinct objective-c pointer objects.
4524/// It is used to build composite qualifier list of the composite type of
4525/// the conditional expression involving two objective-c pointer objects.
4526static
4527void getIntersectionOfProtocols(ASTContext &Context,
4528 const ObjCObjectPointerType *LHSOPT,
4529 const ObjCObjectPointerType *RHSOPT,
4530 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4531
John McCallc12c5bb2010-05-15 11:32:37 +00004532 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4533 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4534 assert(LHS->getInterface() && "LHS must have an interface base");
4535 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004536
4537 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4538 unsigned LHSNumProtocols = LHS->getNumProtocols();
4539 if (LHSNumProtocols > 0)
4540 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4541 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004542 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004543 Context.CollectInheritedProtocols(LHS->getInterface(),
4544 LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004545 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4546 LHSInheritedProtocols.end());
4547 }
4548
4549 unsigned RHSNumProtocols = RHS->getNumProtocols();
4550 if (RHSNumProtocols > 0) {
Dan Gohmancb421fa2010-04-19 16:39:44 +00004551 ObjCProtocolDecl **RHSProtocols =
4552 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004553 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4554 if (InheritedProtocolSet.count(RHSProtocols[i]))
4555 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4556 }
4557 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004558 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCallc12c5bb2010-05-15 11:32:37 +00004559 Context.CollectInheritedProtocols(RHS->getInterface(),
4560 RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004561 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4562 RHSInheritedProtocols.begin(),
4563 E = RHSInheritedProtocols.end(); I != E; ++I)
4564 if (InheritedProtocolSet.count((*I)))
4565 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004566 }
4567}
4568
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004569/// areCommonBaseCompatible - Returns common base class of the two classes if
4570/// one found. Note that this is O'2 algorithm. But it will be called as the
4571/// last type comparison in a ?-exp of ObjC pointer types before a
4572/// warning is issued. So, its invokation is extremely rare.
4573QualType ASTContext::areCommonBaseCompatible(
John McCallc12c5bb2010-05-15 11:32:37 +00004574 const ObjCObjectPointerType *Lptr,
4575 const ObjCObjectPointerType *Rptr) {
4576 const ObjCObjectType *LHS = Lptr->getObjectType();
4577 const ObjCObjectType *RHS = Rptr->getObjectType();
4578 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4579 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4580 if (!LDecl || !RDecl)
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004581 return QualType();
4582
John McCallc12c5bb2010-05-15 11:32:37 +00004583 while ((LDecl = LDecl->getSuperClass())) {
4584 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004585 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCallc12c5bb2010-05-15 11:32:37 +00004586 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4587 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4588
4589 QualType Result = QualType(LHS, 0);
4590 if (!Protocols.empty())
4591 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4592 Result = getObjCObjectPointerType(Result);
4593 return Result;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004594 }
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004595 }
4596
4597 return QualType();
4598}
4599
John McCallc12c5bb2010-05-15 11:32:37 +00004600bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4601 const ObjCObjectType *RHS) {
4602 assert(LHS->getInterface() && "LHS is not an interface type");
4603 assert(RHS->getInterface() && "RHS is not an interface type");
4604
Chris Lattner6ac46a42008-04-07 06:51:04 +00004605 // Verify that the base decls are compatible: the RHS must be a subclass of
4606 // the LHS.
John McCallc12c5bb2010-05-15 11:32:37 +00004607 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner6ac46a42008-04-07 06:51:04 +00004608 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004609
Chris Lattner6ac46a42008-04-07 06:51:04 +00004610 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4611 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004612 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004613 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004614
Chris Lattner6ac46a42008-04-07 06:51:04 +00004615 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4616 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004617 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004618 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00004619
John McCallc12c5bb2010-05-15 11:32:37 +00004620 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4621 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004622 LHSPI != LHSPE; LHSPI++) {
4623 bool RHSImplementsProtocol = false;
4624
4625 // If the RHS doesn't implement the protocol on the left, the types
4626 // are incompatible.
John McCallc12c5bb2010-05-15 11:32:37 +00004627 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4628 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00004629 RHSPI != RHSPE; RHSPI++) {
4630 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004631 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00004632 break;
4633 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004634 }
4635 // FIXME: For better diagnostics, consider passing back the protocol name.
4636 if (!RHSImplementsProtocol)
4637 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004638 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004639 // The RHS implements all protocols listed on the LHS.
4640 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004641}
4642
Steve Naroff389bf462009-02-12 17:52:19 +00004643bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4644 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00004645 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4646 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004647
Steve Naroff14108da2009-07-10 23:34:53 +00004648 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00004649 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004650
4651 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4652 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00004653}
4654
Douglas Gregor569c3162010-08-07 11:51:51 +00004655bool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
4656 return canAssignObjCInterfaces(
4657 getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
4658 getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
4659}
4660
Mike Stump1eb44332009-09-09 15:08:12 +00004661/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00004662/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00004663/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00004664/// same. See 6.7.[2,3,5] for additional rules.
Douglas Gregor447234d2010-07-29 15:18:02 +00004665bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
4666 bool CompareUnqualified) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004667 if (getLangOptions().CPlusPlus)
4668 return hasSameType(LHS, RHS);
4669
Douglas Gregor447234d2010-07-29 15:18:02 +00004670 return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
Eli Friedman3d815e72008-08-22 00:56:42 +00004671}
4672
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004673bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4674 return !mergeTypes(LHS, RHS, true).isNull();
4675}
4676
Peter Collingbourne48466752010-10-24 18:30:18 +00004677/// mergeTransparentUnionType - if T is a transparent union type and a member
4678/// of T is compatible with SubType, return the merged type, else return
4679/// QualType()
4680QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
4681 bool OfBlockPointer,
4682 bool Unqualified) {
4683 if (const RecordType *UT = T->getAsUnionType()) {
4684 RecordDecl *UD = UT->getDecl();
4685 if (UD->hasAttr<TransparentUnionAttr>()) {
4686 for (RecordDecl::field_iterator it = UD->field_begin(),
4687 itend = UD->field_end(); it != itend; ++it) {
4688 QualType ET = it->getType();
4689 QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
4690 if (!MT.isNull())
4691 return MT;
4692 }
4693 }
4694 }
4695
4696 return QualType();
4697}
4698
4699/// mergeFunctionArgumentTypes - merge two types which appear as function
4700/// argument types
4701QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
4702 bool OfBlockPointer,
4703 bool Unqualified) {
4704 // GNU extension: two types are compatible if they appear as a function
4705 // argument, one of the types is a transparent union type and the other
4706 // type is compatible with a union member
4707 QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
4708 Unqualified);
4709 if (!lmerge.isNull())
4710 return lmerge;
4711
4712 QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
4713 Unqualified);
4714 if (!rmerge.isNull())
4715 return rmerge;
4716
4717 return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
4718}
4719
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004720QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
Douglas Gregor447234d2010-07-29 15:18:02 +00004721 bool OfBlockPointer,
4722 bool Unqualified) {
John McCall183700f2009-09-21 23:43:11 +00004723 const FunctionType *lbase = lhs->getAs<FunctionType>();
4724 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00004725 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4726 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00004727 bool allLTypes = true;
4728 bool allRTypes = true;
4729
4730 // Check return type
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004731 QualType retType;
4732 if (OfBlockPointer)
Douglas Gregor447234d2010-07-29 15:18:02 +00004733 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true,
4734 Unqualified);
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004735 else
Douglas Gregor447234d2010-07-29 15:18:02 +00004736 retType = mergeTypes(lbase->getResultType(), rbase->getResultType(),
4737 false, Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004738 if (retType.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004739
4740 if (Unqualified)
4741 retType = retType.getUnqualifiedType();
4742
4743 CanQualType LRetType = getCanonicalType(lbase->getResultType());
4744 CanQualType RRetType = getCanonicalType(rbase->getResultType());
4745 if (Unqualified) {
4746 LRetType = LRetType.getUnqualifiedType();
4747 RRetType = RRetType.getUnqualifiedType();
4748 }
4749
4750 if (getCanonicalType(retType) != LRetType)
Chris Lattner61710852008-10-05 17:34:18 +00004751 allLTypes = false;
Douglas Gregor447234d2010-07-29 15:18:02 +00004752 if (getCanonicalType(retType) != RRetType)
Chris Lattner61710852008-10-05 17:34:18 +00004753 allRTypes = false;
Daniel Dunbar6a15c852010-04-28 16:20:58 +00004754 // FIXME: double check this
4755 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4756 // rbase->getRegParmAttr() != 0 &&
4757 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindola264ba482010-03-30 20:24:48 +00004758 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4759 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbar6a15c852010-04-28 16:20:58 +00004760 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4761 lbaseInfo.getRegParm();
4762 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4763 if (NoReturn != lbaseInfo.getNoReturn() ||
4764 RegParm != lbaseInfo.getRegParm())
4765 allLTypes = false;
4766 if (NoReturn != rbaseInfo.getNoReturn() ||
4767 RegParm != rbaseInfo.getRegParm())
4768 allRTypes = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00004769 CallingConv lcc = lbaseInfo.getCC();
4770 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004771 // Compatible functions must have compatible calling conventions
John McCall04a67a62010-02-05 21:31:56 +00004772 if (!isSameCallConv(lcc, rcc))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004773 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004774
Eli Friedman3d815e72008-08-22 00:56:42 +00004775 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00004776 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4777 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004778 unsigned lproto_nargs = lproto->getNumArgs();
4779 unsigned rproto_nargs = rproto->getNumArgs();
4780
4781 // Compatible functions must have the same number of arguments
4782 if (lproto_nargs != rproto_nargs)
4783 return QualType();
4784
4785 // Variadic and non-variadic functions aren't compatible
4786 if (lproto->isVariadic() != rproto->isVariadic())
4787 return QualType();
4788
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00004789 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4790 return QualType();
4791
Eli Friedman3d815e72008-08-22 00:56:42 +00004792 // Check argument compatibility
4793 llvm::SmallVector<QualType, 10> types;
4794 for (unsigned i = 0; i < lproto_nargs; i++) {
4795 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4796 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Peter Collingbourne48466752010-10-24 18:30:18 +00004797 QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
4798 OfBlockPointer,
4799 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004800 if (argtype.isNull()) return QualType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004801
4802 if (Unqualified)
4803 argtype = argtype.getUnqualifiedType();
4804
Eli Friedman3d815e72008-08-22 00:56:42 +00004805 types.push_back(argtype);
Douglas Gregor447234d2010-07-29 15:18:02 +00004806 if (Unqualified) {
4807 largtype = largtype.getUnqualifiedType();
4808 rargtype = rargtype.getUnqualifiedType();
4809 }
4810
Chris Lattner61710852008-10-05 17:34:18 +00004811 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4812 allLTypes = false;
4813 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4814 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00004815 }
4816 if (allLTypes) return lhs;
4817 if (allRTypes) return rhs;
4818 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00004819 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindola264ba482010-03-30 20:24:48 +00004820 false, false, 0, 0,
Rafael Espindola425ef722010-03-30 22:15:11 +00004821 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman3d815e72008-08-22 00:56:42 +00004822 }
4823
4824 if (lproto) allRTypes = false;
4825 if (rproto) allLTypes = false;
4826
Douglas Gregor72564e72009-02-26 23:50:07 +00004827 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00004828 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00004829 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004830 if (proto->isVariadic()) return QualType();
4831 // Check that the types are compatible with the types that
4832 // would result from default argument promotions (C99 6.7.5.3p15).
4833 // The only types actually affected are promotable integer
4834 // types and floats, which would be passed as a different
4835 // type depending on whether the prototype is visible.
4836 unsigned proto_nargs = proto->getNumArgs();
4837 for (unsigned i = 0; i < proto_nargs; ++i) {
4838 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00004839
4840 // Look at the promotion type of enum types, since that is the type used
4841 // to pass enum values.
4842 if (const EnumType *Enum = argTy->getAs<EnumType>())
4843 argTy = Enum->getDecl()->getPromotionType();
4844
Eli Friedman3d815e72008-08-22 00:56:42 +00004845 if (argTy->isPromotableIntegerType() ||
4846 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4847 return QualType();
4848 }
4849
4850 if (allLTypes) return lhs;
4851 if (allRTypes) return rhs;
4852 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00004853 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindola264ba482010-03-30 20:24:48 +00004854 proto->getTypeQuals(),
4855 false, false, 0, 0,
Rafael Espindola425ef722010-03-30 22:15:11 +00004856 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman3d815e72008-08-22 00:56:42 +00004857 }
4858
4859 if (allLTypes) return lhs;
4860 if (allRTypes) return rhs;
Rafael Espindola425ef722010-03-30 22:15:11 +00004861 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindola264ba482010-03-30 20:24:48 +00004862 return getFunctionNoProtoType(retType, Info);
Eli Friedman3d815e72008-08-22 00:56:42 +00004863}
4864
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004865QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
Douglas Gregor447234d2010-07-29 15:18:02 +00004866 bool OfBlockPointer,
4867 bool Unqualified) {
Bill Wendling43d69752007-12-03 07:33:35 +00004868 // C++ [expr]: If an expression initially has the type "reference to T", the
4869 // type is adjusted to "T" prior to any further analysis, the expression
4870 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004871 // expression is an lvalue unless the reference is an rvalue reference and
4872 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004873 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4874 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
Douglas Gregor447234d2010-07-29 15:18:02 +00004875
4876 if (Unqualified) {
4877 LHS = LHS.getUnqualifiedType();
4878 RHS = RHS.getUnqualifiedType();
4879 }
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004880
Eli Friedman3d815e72008-08-22 00:56:42 +00004881 QualType LHSCan = getCanonicalType(LHS),
4882 RHSCan = getCanonicalType(RHS);
4883
4884 // If two types are identical, they are compatible.
4885 if (LHSCan == RHSCan)
4886 return LHS;
4887
John McCall0953e762009-09-24 19:53:00 +00004888 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00004889 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4890 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00004891 if (LQuals != RQuals) {
4892 // If any of these qualifiers are different, we have a type
4893 // mismatch.
4894 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4895 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4896 return QualType();
4897
4898 // Exactly one GC qualifier difference is allowed: __strong is
4899 // okay if the other type has no GC qualifier but is an Objective
4900 // C object pointer (i.e. implicitly strong by default). We fix
4901 // this by pretending that the unqualified type was actually
4902 // qualified __strong.
4903 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4904 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4905 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4906
4907 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4908 return QualType();
4909
4910 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4911 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4912 }
4913 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4914 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4915 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004916 return QualType();
John McCall0953e762009-09-24 19:53:00 +00004917 }
4918
4919 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00004920
Eli Friedman852d63b2009-06-01 01:22:52 +00004921 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4922 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00004923
Chris Lattner1adb8832008-01-14 05:45:46 +00004924 // We want to consider the two function types to be the same for these
4925 // comparisons, just force one to the other.
4926 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4927 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00004928
4929 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00004930 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4931 LHSClass = Type::ConstantArray;
4932 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4933 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00004934
John McCallc12c5bb2010-05-15 11:32:37 +00004935 // ObjCInterfaces are just specialized ObjCObjects.
4936 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
4937 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
4938
Nate Begeman213541a2008-04-18 23:10:10 +00004939 // Canonicalize ExtVector -> Vector.
4940 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4941 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00004942
Chris Lattnera36a61f2008-04-07 05:43:21 +00004943 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004944 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00004945 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00004946 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00004947 // Compatibility is based on the underlying type, not the promotion
4948 // type.
John McCall183700f2009-09-21 23:43:11 +00004949 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004950 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4951 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004952 }
John McCall183700f2009-09-21 23:43:11 +00004953 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004954 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4955 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004956 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004957
Eli Friedman3d815e72008-08-22 00:56:42 +00004958 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004959 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004960
Steve Naroff4a746782008-01-09 22:43:08 +00004961 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004962 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00004963#define TYPE(Class, Base)
4964#define ABSTRACT_TYPE(Class, Base)
John McCallad5e7382010-03-01 23:49:17 +00004965#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +00004966#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4967#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4968#include "clang/AST/TypeNodes.def"
4969 assert(false && "Non-canonical and dependent types shouldn't get here");
4970 return QualType();
4971
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004972 case Type::LValueReference:
4973 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00004974 case Type::MemberPointer:
4975 assert(false && "C++ should never be in mergeTypes");
4976 return QualType();
4977
John McCallc12c5bb2010-05-15 11:32:37 +00004978 case Type::ObjCInterface:
Douglas Gregor72564e72009-02-26 23:50:07 +00004979 case Type::IncompleteArray:
4980 case Type::VariableArray:
4981 case Type::FunctionProto:
4982 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00004983 assert(false && "Types are eliminated above");
4984 return QualType();
4985
Chris Lattner1adb8832008-01-14 05:45:46 +00004986 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00004987 {
4988 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004989 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4990 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00004991 if (Unqualified) {
4992 LHSPointee = LHSPointee.getUnqualifiedType();
4993 RHSPointee = RHSPointee.getUnqualifiedType();
4994 }
4995 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
4996 Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00004997 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00004998 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004999 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00005000 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00005001 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005002 return getPointerType(ResultType);
5003 }
Steve Naroffc0febd52008-12-10 17:49:55 +00005004 case Type::BlockPointer:
5005 {
5006 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00005007 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
5008 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005009 if (Unqualified) {
5010 LHSPointee = LHSPointee.getUnqualifiedType();
5011 RHSPointee = RHSPointee.getUnqualifiedType();
5012 }
5013 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5014 Unqualified);
Steve Naroffc0febd52008-12-10 17:49:55 +00005015 if (ResultType.isNull()) return QualType();
5016 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5017 return LHS;
5018 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5019 return RHS;
5020 return getBlockPointerType(ResultType);
5021 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005022 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00005023 {
5024 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
5025 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
5026 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
5027 return QualType();
5028
5029 QualType LHSElem = getAsArrayType(LHS)->getElementType();
5030 QualType RHSElem = getAsArrayType(RHS)->getElementType();
Douglas Gregor447234d2010-07-29 15:18:02 +00005031 if (Unqualified) {
5032 LHSElem = LHSElem.getUnqualifiedType();
5033 RHSElem = RHSElem.getUnqualifiedType();
5034 }
5035
5036 QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
Eli Friedman3d815e72008-08-22 00:56:42 +00005037 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00005038 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5039 return LHS;
5040 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5041 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00005042 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
5043 ArrayType::ArraySizeModifier(), 0);
5044 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
5045 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005046 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
5047 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00005048 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
5049 return LHS;
5050 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
5051 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00005052 if (LVAT) {
5053 // FIXME: This isn't correct! But tricky to implement because
5054 // the array's size has to be the size of LHS, but the type
5055 // has to be different.
5056 return LHS;
5057 }
5058 if (RVAT) {
5059 // FIXME: This isn't correct! But tricky to implement because
5060 // the array's size has to be the size of RHS, but the type
5061 // has to be different.
5062 return RHS;
5063 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00005064 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
5065 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005066 return getIncompleteArrayType(ResultType,
5067 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00005068 }
Chris Lattner1adb8832008-01-14 05:45:46 +00005069 case Type::FunctionNoProto:
Douglas Gregor447234d2010-07-29 15:18:02 +00005070 return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
Douglas Gregor72564e72009-02-26 23:50:07 +00005071 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00005072 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00005073 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00005074 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005075 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00005076 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00005077 case Type::Complex:
5078 // Distinct complex types are incompatible.
5079 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00005080 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005081 // FIXME: The merged type should be an ExtVector!
John McCall1c471f32010-03-12 23:14:13 +00005082 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
5083 RHSCan->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00005084 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00005085 return QualType();
John McCallc12c5bb2010-05-15 11:32:37 +00005086 case Type::ObjCObject: {
5087 // Check if the types are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00005088 // FIXME: This should be type compatibility, e.g. whether
5089 // "LHS x; RHS x;" at global scope is legal.
John McCallc12c5bb2010-05-15 11:32:37 +00005090 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5091 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5092 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff5fd659d2009-02-21 16:18:07 +00005093 return LHS;
5094
Eli Friedman3d815e72008-08-22 00:56:42 +00005095 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00005096 }
Steve Naroff14108da2009-07-10 23:34:53 +00005097 case Type::ObjCObjectPointer: {
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005098 if (OfBlockPointer) {
5099 if (canAssignObjCInterfacesInBlockPointer(
5100 LHS->getAs<ObjCObjectPointerType>(),
5101 RHS->getAs<ObjCObjectPointerType>()))
5102 return LHS;
5103 return QualType();
5104 }
John McCall183700f2009-09-21 23:43:11 +00005105 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5106 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00005107 return LHS;
5108
Steve Naroffbc76dd02008-12-10 22:14:21 +00005109 return QualType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00005110 }
Steve Naroffec0550f2007-10-15 20:41:53 +00005111 }
Douglas Gregor72564e72009-02-26 23:50:07 +00005112
5113 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00005114}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00005115
Fariborz Jahanian2390a722010-05-19 21:37:30 +00005116/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
5117/// 'RHS' attributes and returns the merged version; including for function
5118/// return types.
5119QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
5120 QualType LHSCan = getCanonicalType(LHS),
5121 RHSCan = getCanonicalType(RHS);
5122 // If two types are identical, they are compatible.
5123 if (LHSCan == RHSCan)
5124 return LHS;
5125 if (RHSCan->isFunctionType()) {
5126 if (!LHSCan->isFunctionType())
5127 return QualType();
5128 QualType OldReturnType =
5129 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
5130 QualType NewReturnType =
5131 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
5132 QualType ResReturnType =
5133 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
5134 if (ResReturnType.isNull())
5135 return QualType();
5136 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
5137 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
5138 // In either case, use OldReturnType to build the new function type.
5139 const FunctionType *F = LHS->getAs<FunctionType>();
5140 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
5141 FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
5142 QualType ResultType
5143 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
5144 FPT->getNumArgs(), FPT->isVariadic(),
5145 FPT->getTypeQuals(),
5146 FPT->hasExceptionSpec(),
5147 FPT->hasAnyExceptionSpec(),
5148 FPT->getNumExceptions(),
5149 FPT->exception_begin(),
5150 Info);
5151 return ResultType;
5152 }
5153 }
5154 return QualType();
5155 }
5156
5157 // If the qualifiers are different, the types can still be merged.
5158 Qualifiers LQuals = LHSCan.getLocalQualifiers();
5159 Qualifiers RQuals = RHSCan.getLocalQualifiers();
5160 if (LQuals != RQuals) {
5161 // If any of these qualifiers are different, we have a type mismatch.
5162 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5163 LQuals.getAddressSpace() != RQuals.getAddressSpace())
5164 return QualType();
5165
5166 // Exactly one GC qualifier difference is allowed: __strong is
5167 // okay if the other type has no GC qualifier but is an Objective
5168 // C object pointer (i.e. implicitly strong by default). We fix
5169 // this by pretending that the unqualified type was actually
5170 // qualified __strong.
5171 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
5172 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
5173 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
5174
5175 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
5176 return QualType();
5177
5178 if (GC_L == Qualifiers::Strong)
5179 return LHS;
5180 if (GC_R == Qualifiers::Strong)
5181 return RHS;
5182 return QualType();
5183 }
5184
5185 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
5186 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5187 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
5188 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
5189 if (ResQT == LHSBaseQT)
5190 return LHS;
5191 if (ResQT == RHSBaseQT)
5192 return RHS;
5193 }
5194 return QualType();
5195}
5196
Chris Lattner5426bf62008-04-07 07:01:58 +00005197//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00005198// Integer Predicates
5199//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00005200
Eli Friedmanad74a752008-06-28 06:23:08 +00005201unsigned ASTContext::getIntWidth(QualType T) {
John McCall842aef82009-12-09 09:09:27 +00005202 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00005203 T = ET->getDecl()->getIntegerType();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005204 if (T->isBooleanType())
5205 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00005206 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00005207 return (unsigned)getTypeSize(T);
5208}
5209
5210QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
Douglas Gregorf6094622010-07-23 15:58:24 +00005211 assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00005212
5213 // Turn <4 x signed int> -> <4 x unsigned int>
5214 if (const VectorType *VTy = T->getAs<VectorType>())
5215 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Chris Lattner788b0fd2010-06-23 06:00:24 +00005216 VTy->getNumElements(), VTy->getAltiVecSpecific());
Chris Lattner6a2b9262009-10-17 20:33:28 +00005217
5218 // For enums, we return the unsigned version of the base type.
5219 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00005220 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00005221
5222 const BuiltinType *BTy = T->getAs<BuiltinType>();
5223 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00005224 switch (BTy->getKind()) {
5225 case BuiltinType::Char_S:
5226 case BuiltinType::SChar:
5227 return UnsignedCharTy;
5228 case BuiltinType::Short:
5229 return UnsignedShortTy;
5230 case BuiltinType::Int:
5231 return UnsignedIntTy;
5232 case BuiltinType::Long:
5233 return UnsignedLongTy;
5234 case BuiltinType::LongLong:
5235 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00005236 case BuiltinType::Int128:
5237 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00005238 default:
5239 assert(0 && "Unexpected signed integer type");
5240 return QualType();
5241 }
5242}
5243
Douglas Gregor2cf26342009-04-09 22:27:44 +00005244ExternalASTSource::~ExternalASTSource() { }
5245
5246void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00005247
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005248ASTMutationListener::~ASTMutationListener() { }
5249
Chris Lattner86df27b2009-06-14 00:45:47 +00005250
5251//===----------------------------------------------------------------------===//
5252// Builtin Type Computation
5253//===----------------------------------------------------------------------===//
5254
5255/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
Chris Lattner33daae62010-10-01 22:42:38 +00005256/// pointer over the consumed characters. This returns the resultant type. If
5257/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
5258/// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of
5259/// a vector of "i*".
Chris Lattner14e0e742010-10-01 22:53:11 +00005260///
5261/// RequiresICE is filled in on return to indicate whether the value is required
5262/// to be an Integer Constant Expression.
Mike Stump1eb44332009-09-09 15:08:12 +00005263static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00005264 ASTContext::GetBuiltinTypeError &Error,
Chris Lattner14e0e742010-10-01 22:53:11 +00005265 bool &RequiresICE,
Chris Lattner33daae62010-10-01 22:42:38 +00005266 bool AllowTypeModifiers) {
Chris Lattner86df27b2009-06-14 00:45:47 +00005267 // Modifiers.
5268 int HowLong = 0;
5269 bool Signed = false, Unsigned = false;
Chris Lattner14e0e742010-10-01 22:53:11 +00005270 RequiresICE = false;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005271
Chris Lattner33daae62010-10-01 22:42:38 +00005272 // Read the prefixed modifiers first.
Chris Lattner86df27b2009-06-14 00:45:47 +00005273 bool Done = false;
5274 while (!Done) {
5275 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00005276 default: Done = true; --Str; break;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005277 case 'I':
Chris Lattner14e0e742010-10-01 22:53:11 +00005278 RequiresICE = true;
Chris Lattner393bd8e2010-10-01 07:13:18 +00005279 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005280 case 'S':
5281 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
5282 assert(!Signed && "Can't use 'S' modifier multiple times!");
5283 Signed = true;
5284 break;
5285 case 'U':
5286 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5287 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5288 Unsigned = true;
5289 break;
5290 case 'L':
5291 assert(HowLong <= 2 && "Can't have LLLL modifier");
5292 ++HowLong;
5293 break;
5294 }
5295 }
5296
5297 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00005298
Chris Lattner86df27b2009-06-14 00:45:47 +00005299 // Read the base type.
5300 switch (*Str++) {
5301 default: assert(0 && "Unknown builtin type letter!");
5302 case 'v':
5303 assert(HowLong == 0 && !Signed && !Unsigned &&
5304 "Bad modifiers used with 'v'!");
5305 Type = Context.VoidTy;
5306 break;
5307 case 'f':
5308 assert(HowLong == 0 && !Signed && !Unsigned &&
5309 "Bad modifiers used with 'f'!");
5310 Type = Context.FloatTy;
5311 break;
5312 case 'd':
5313 assert(HowLong < 2 && !Signed && !Unsigned &&
5314 "Bad modifiers used with 'd'!");
5315 if (HowLong)
5316 Type = Context.LongDoubleTy;
5317 else
5318 Type = Context.DoubleTy;
5319 break;
5320 case 's':
5321 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5322 if (Unsigned)
5323 Type = Context.UnsignedShortTy;
5324 else
5325 Type = Context.ShortTy;
5326 break;
5327 case 'i':
5328 if (HowLong == 3)
5329 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5330 else if (HowLong == 2)
5331 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5332 else if (HowLong == 1)
5333 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5334 else
5335 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5336 break;
5337 case 'c':
5338 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5339 if (Signed)
5340 Type = Context.SignedCharTy;
5341 else if (Unsigned)
5342 Type = Context.UnsignedCharTy;
5343 else
5344 Type = Context.CharTy;
5345 break;
5346 case 'b': // boolean
5347 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5348 Type = Context.BoolTy;
5349 break;
5350 case 'z': // size_t.
5351 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5352 Type = Context.getSizeType();
5353 break;
5354 case 'F':
5355 Type = Context.getCFConstantStringType();
5356 break;
5357 case 'a':
5358 Type = Context.getBuiltinVaListType();
5359 assert(!Type.isNull() && "builtin va list type not initialized!");
5360 break;
5361 case 'A':
5362 // This is a "reference" to a va_list; however, what exactly
5363 // this means depends on how va_list is defined. There are two
5364 // different kinds of va_list: ones passed by value, and ones
5365 // passed by reference. An example of a by-value va_list is
5366 // x86, where va_list is a char*. An example of by-ref va_list
5367 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5368 // we want this argument to be a char*&; for x86-64, we want
5369 // it to be a __va_list_tag*.
5370 Type = Context.getBuiltinVaListType();
5371 assert(!Type.isNull() && "builtin va list type not initialized!");
Chris Lattner14e0e742010-10-01 22:53:11 +00005372 if (Type->isArrayType())
Chris Lattner86df27b2009-06-14 00:45:47 +00005373 Type = Context.getArrayDecayedType(Type);
Chris Lattner14e0e742010-10-01 22:53:11 +00005374 else
Chris Lattner86df27b2009-06-14 00:45:47 +00005375 Type = Context.getLValueReferenceType(Type);
Chris Lattner86df27b2009-06-14 00:45:47 +00005376 break;
5377 case 'V': {
5378 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00005379 unsigned NumElements = strtoul(Str, &End, 10);
5380 assert(End != Str && "Missing vector size");
Chris Lattner86df27b2009-06-14 00:45:47 +00005381 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00005382
Chris Lattner14e0e742010-10-01 22:53:11 +00005383 QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
5384 RequiresICE, false);
5385 assert(!RequiresICE && "Can't require vector ICE");
Chris Lattner33daae62010-10-01 22:42:38 +00005386
5387 // TODO: No way to make AltiVec vectors in builtins yet.
Chris Lattner788b0fd2010-06-23 06:00:24 +00005388 Type = Context.getVectorType(ElementType, NumElements,
5389 VectorType::NotAltiVec);
Chris Lattner86df27b2009-06-14 00:45:47 +00005390 break;
5391 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00005392 case 'X': {
Chris Lattner14e0e742010-10-01 22:53:11 +00005393 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
5394 false);
5395 assert(!RequiresICE && "Can't require complex ICE");
Douglas Gregord3a23b22009-09-28 21:45:01 +00005396 Type = Context.getComplexType(ElementType);
5397 break;
5398 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005399 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00005400 Type = Context.getFILEType();
5401 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005402 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00005403 return QualType();
5404 }
Mike Stumpfd612db2009-07-28 23:47:15 +00005405 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00005406 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00005407 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00005408 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00005409 else
5410 Type = Context.getjmp_bufType();
5411
Mike Stumpfd612db2009-07-28 23:47:15 +00005412 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00005413 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00005414 return QualType();
5415 }
5416 break;
Mike Stump782fa302009-07-28 02:25:19 +00005417 }
Mike Stump1eb44332009-09-09 15:08:12 +00005418
Chris Lattner33daae62010-10-01 22:42:38 +00005419 // If there are modifiers and if we're allowed to parse them, go for it.
5420 Done = !AllowTypeModifiers;
Chris Lattner86df27b2009-06-14 00:45:47 +00005421 while (!Done) {
John McCall187ab372010-03-12 04:21:28 +00005422 switch (char c = *Str++) {
Chris Lattner33daae62010-10-01 22:42:38 +00005423 default: Done = true; --Str; break;
5424 case '*':
5425 case '&': {
5426 // Both pointers and references can have their pointee types
5427 // qualified with an address space.
5428 char *End;
5429 unsigned AddrSpace = strtoul(Str, &End, 10);
5430 if (End != Str && AddrSpace != 0) {
5431 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5432 Str = End;
5433 }
5434 if (c == '*')
5435 Type = Context.getPointerType(Type);
5436 else
5437 Type = Context.getLValueReferenceType(Type);
5438 break;
5439 }
5440 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5441 case 'C':
5442 Type = Type.withConst();
5443 break;
5444 case 'D':
5445 Type = Context.getVolatileType(Type);
5446 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00005447 }
5448 }
Chris Lattner393bd8e2010-10-01 07:13:18 +00005449
Chris Lattner14e0e742010-10-01 22:53:11 +00005450 assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
Chris Lattner393bd8e2010-10-01 07:13:18 +00005451 "Integer constant 'I' type must be an integer");
Mike Stump1eb44332009-09-09 15:08:12 +00005452
Chris Lattner86df27b2009-06-14 00:45:47 +00005453 return Type;
5454}
5455
5456/// GetBuiltinType - Return the type for the specified builtin.
Chris Lattner33daae62010-10-01 22:42:38 +00005457QualType ASTContext::GetBuiltinType(unsigned Id,
Chris Lattner14e0e742010-10-01 22:53:11 +00005458 GetBuiltinTypeError &Error,
5459 unsigned *IntegerConstantArgs) {
Chris Lattner33daae62010-10-01 22:42:38 +00005460 const char *TypeStr = BuiltinInfo.GetTypeString(Id);
Mike Stump1eb44332009-09-09 15:08:12 +00005461
Chris Lattner86df27b2009-06-14 00:45:47 +00005462 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00005463
Chris Lattner14e0e742010-10-01 22:53:11 +00005464 bool RequiresICE = false;
Chris Lattner86df27b2009-06-14 00:45:47 +00005465 Error = GE_None;
Chris Lattner14e0e742010-10-01 22:53:11 +00005466 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
5467 RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005468 if (Error != GE_None)
5469 return QualType();
Chris Lattner14e0e742010-10-01 22:53:11 +00005470
5471 assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
5472
Chris Lattner86df27b2009-06-14 00:45:47 +00005473 while (TypeStr[0] && TypeStr[0] != '.') {
Chris Lattner14e0e742010-10-01 22:53:11 +00005474 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
Chris Lattner86df27b2009-06-14 00:45:47 +00005475 if (Error != GE_None)
5476 return QualType();
5477
Chris Lattner14e0e742010-10-01 22:53:11 +00005478 // If this argument is required to be an IntegerConstantExpression and the
5479 // caller cares, fill in the bitmask we return.
5480 if (RequiresICE && IntegerConstantArgs)
5481 *IntegerConstantArgs |= 1 << ArgTypes.size();
5482
Chris Lattner86df27b2009-06-14 00:45:47 +00005483 // Do array -> pointer decay. The builtin should use the decayed type.
5484 if (Ty->isArrayType())
5485 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00005486
Chris Lattner86df27b2009-06-14 00:45:47 +00005487 ArgTypes.push_back(Ty);
5488 }
5489
5490 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5491 "'.' should only occur at end of builtin type list!");
5492
5493 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
5494 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
5495 return getFunctionNoProtoType(ResType);
Douglas Gregorce056bc2010-02-21 22:15:06 +00005496
5497 // FIXME: Should we create noreturn types?
Chris Lattner86df27b2009-06-14 00:45:47 +00005498 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00005499 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindola264ba482010-03-30 20:24:48 +00005500 FunctionType::ExtInfo());
Chris Lattner86df27b2009-06-14 00:45:47 +00005501}
Eli Friedmana95d7572009-08-19 07:44:53 +00005502
5503QualType
5504ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
5505 // Perform the usual unary conversions. We do this early so that
5506 // integral promotions to "int" can allow us to exit early, in the
5507 // lhs == rhs check. Also, for conversion purposes, we ignore any
5508 // qualifiers. For example, "const float" and "float" are
5509 // equivalent.
5510 if (lhs->isPromotableIntegerType())
5511 lhs = getPromotedIntegerType(lhs);
5512 else
5513 lhs = lhs.getUnqualifiedType();
5514 if (rhs->isPromotableIntegerType())
5515 rhs = getPromotedIntegerType(rhs);
5516 else
5517 rhs = rhs.getUnqualifiedType();
5518
5519 // If both types are identical, no conversion is needed.
5520 if (lhs == rhs)
5521 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00005522
Eli Friedmana95d7572009-08-19 07:44:53 +00005523 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
5524 // The caller can deal with this (e.g. pointer + int).
5525 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5526 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00005527
5528 // At this point, we have two different arithmetic types.
5529
Eli Friedmana95d7572009-08-19 07:44:53 +00005530 // Handle complex types first (C99 6.3.1.8p1).
5531 if (lhs->isComplexType() || rhs->isComplexType()) {
5532 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00005533 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00005534 // convert the rhs to the lhs complex type.
5535 return lhs;
5536 }
Mike Stump1eb44332009-09-09 15:08:12 +00005537 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00005538 // convert the lhs to the rhs complex type.
5539 return rhs;
5540 }
5541 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00005542 // When both operands are complex, the shorter operand is converted to the
5543 // type of the longer, and that is the type of the result. This corresponds
5544 // to what is done when combining two real floating-point operands.
5545 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00005546 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00005547 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00005548 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00005549 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00005550 // "double _Complex" is promoted to "long double _Complex".
5551 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00005552
5553 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00005554 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00005555 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00005556 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00005557 }
Eli Friedmana95d7572009-08-19 07:44:53 +00005558 // At this point, lhs and rhs have the same rank/size. Now, make sure the
5559 // domains match. This is a requirement for our implementation, C99
5560 // does not require this promotion.
5561 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5562 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5563 return rhs;
5564 } else { // handle "_Complex double, double".
5565 return lhs;
5566 }
5567 }
5568 return lhs; // The domain/size match exactly.
5569 }
5570 // Now handle "real" floating types (i.e. float, double, long double).
5571 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5572 // if we have an integer operand, the result is the real floating type.
5573 if (rhs->isIntegerType()) {
5574 // convert rhs to the lhs floating point type.
5575 return lhs;
5576 }
5577 if (rhs->isComplexIntegerType()) {
5578 // convert rhs to the complex floating point type.
5579 return getComplexType(lhs);
5580 }
5581 if (lhs->isIntegerType()) {
5582 // convert lhs to the rhs floating point type.
5583 return rhs;
5584 }
Mike Stump1eb44332009-09-09 15:08:12 +00005585 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00005586 // convert lhs to the complex floating point type.
5587 return getComplexType(rhs);
5588 }
5589 // We have two real floating types, float/complex combos were handled above.
5590 // Convert the smaller operand to the bigger result.
5591 int result = getFloatingTypeOrder(lhs, rhs);
5592 if (result > 0) // convert the rhs
5593 return lhs;
5594 assert(result < 0 && "illegal float comparison");
5595 return rhs; // convert the lhs
5596 }
5597 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5598 // Handle GCC complex int extension.
5599 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5600 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5601
5602 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00005603 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00005604 rhsComplexInt->getElementType()) >= 0)
5605 return lhs; // convert the rhs
5606 return rhs;
5607 } else if (lhsComplexInt && rhs->isIntegerType()) {
5608 // convert the rhs to the lhs complex type.
5609 return lhs;
5610 } else if (rhsComplexInt && lhs->isIntegerType()) {
5611 // convert the lhs to the rhs complex type.
5612 return rhs;
5613 }
5614 }
5615 // Finally, we have two differing integer types.
5616 // The rules for this case are in C99 6.3.1.8
5617 int compare = getIntegerTypeOrder(lhs, rhs);
Douglas Gregorf6094622010-07-23 15:58:24 +00005618 bool lhsSigned = lhs->hasSignedIntegerRepresentation(),
5619 rhsSigned = rhs->hasSignedIntegerRepresentation();
Eli Friedmana95d7572009-08-19 07:44:53 +00005620 QualType destType;
5621 if (lhsSigned == rhsSigned) {
5622 // Same signedness; use the higher-ranked type
5623 destType = compare >= 0 ? lhs : rhs;
5624 } else if (compare != (lhsSigned ? 1 : -1)) {
5625 // The unsigned type has greater than or equal rank to the
5626 // signed type, so use the unsigned type
5627 destType = lhsSigned ? rhs : lhs;
5628 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5629 // The two types are different widths; if we are here, that
5630 // means the signed type is larger than the unsigned type, so
5631 // use the signed type.
5632 destType = lhsSigned ? lhs : rhs;
5633 } else {
5634 // The signed type is higher-ranked than the unsigned type,
5635 // but isn't actually any bigger (like unsigned int and long
5636 // on most 32-bit systems). Use the unsigned type corresponding
5637 // to the signed type.
5638 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5639 }
5640 return destType;
5641}
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005642
5643GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
5644 GVALinkage External = GVA_StrongExternal;
5645
5646 Linkage L = FD->getLinkage();
5647 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5648 FD->getType()->getLinkage() == UniqueExternalLinkage)
5649 L = UniqueExternalLinkage;
5650
5651 switch (L) {
5652 case NoLinkage:
5653 case InternalLinkage:
5654 case UniqueExternalLinkage:
5655 return GVA_Internal;
5656
5657 case ExternalLinkage:
5658 switch (FD->getTemplateSpecializationKind()) {
5659 case TSK_Undeclared:
5660 case TSK_ExplicitSpecialization:
5661 External = GVA_StrongExternal;
5662 break;
5663
5664 case TSK_ExplicitInstantiationDefinition:
5665 return GVA_ExplicitTemplateInstantiation;
5666
5667 case TSK_ExplicitInstantiationDeclaration:
5668 case TSK_ImplicitInstantiation:
5669 External = GVA_TemplateInstantiation;
5670 break;
5671 }
5672 }
5673
5674 if (!FD->isInlined())
5675 return External;
5676
5677 if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
5678 // GNU or C99 inline semantics. Determine whether this symbol should be
5679 // externally visible.
5680 if (FD->isInlineDefinitionExternallyVisible())
5681 return External;
5682
5683 // C99 inline semantics, where the symbol is not externally visible.
5684 return GVA_C99Inline;
5685 }
5686
5687 // C++0x [temp.explicit]p9:
5688 // [ Note: The intent is that an inline function that is the subject of
5689 // an explicit instantiation declaration will still be implicitly
5690 // instantiated when used so that the body can be considered for
5691 // inlining, but that no out-of-line copy of the inline function would be
5692 // generated in the translation unit. -- end note ]
5693 if (FD->getTemplateSpecializationKind()
5694 == TSK_ExplicitInstantiationDeclaration)
5695 return GVA_C99Inline;
5696
5697 return GVA_CXXInline;
5698}
5699
5700GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
5701 // If this is a static data member, compute the kind of template
5702 // specialization. Otherwise, this variable is not part of a
5703 // template.
5704 TemplateSpecializationKind TSK = TSK_Undeclared;
5705 if (VD->isStaticDataMember())
5706 TSK = VD->getTemplateSpecializationKind();
5707
5708 Linkage L = VD->getLinkage();
5709 if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
5710 VD->getType()->getLinkage() == UniqueExternalLinkage)
5711 L = UniqueExternalLinkage;
5712
5713 switch (L) {
5714 case NoLinkage:
5715 case InternalLinkage:
5716 case UniqueExternalLinkage:
5717 return GVA_Internal;
5718
5719 case ExternalLinkage:
5720 switch (TSK) {
5721 case TSK_Undeclared:
5722 case TSK_ExplicitSpecialization:
5723 return GVA_StrongExternal;
5724
5725 case TSK_ExplicitInstantiationDeclaration:
5726 llvm_unreachable("Variable should not be instantiated");
5727 // Fall through to treat this like any other instantiation.
5728
5729 case TSK_ExplicitInstantiationDefinition:
5730 return GVA_ExplicitTemplateInstantiation;
5731
5732 case TSK_ImplicitInstantiation:
5733 return GVA_TemplateInstantiation;
5734 }
5735 }
5736
5737 return GVA_StrongExternal;
5738}
5739
Argyrios Kyrtzidis4ac7c0b2010-07-29 20:08:05 +00005740bool ASTContext::DeclMustBeEmitted(const Decl *D) {
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005741 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
5742 if (!VD->isFileVarDecl())
5743 return false;
5744 } else if (!isa<FunctionDecl>(D))
5745 return false;
5746
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00005747 // Weak references don't produce any output by themselves.
5748 if (D->hasAttr<WeakRefAttr>())
5749 return false;
5750
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005751 // Aliases and used decls are required.
5752 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
5753 return true;
5754
5755 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5756 // Forward declarations aren't required.
5757 if (!FD->isThisDeclarationADefinition())
5758 return false;
5759
5760 // Constructors and destructors are required.
5761 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
5762 return true;
5763
5764 // The key function for a class is required.
5765 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
5766 const CXXRecordDecl *RD = MD->getParent();
5767 if (MD->isOutOfLine() && RD->isDynamicClass()) {
5768 const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
5769 if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
5770 return true;
5771 }
5772 }
5773
5774 GVALinkage Linkage = GetGVALinkageForFunction(FD);
5775
5776 // static, static inline, always_inline, and extern inline functions can
5777 // always be deferred. Normal inline functions can be deferred in C99/C++.
5778 // Implicit template instantiations can also be deferred in C++.
5779 if (Linkage == GVA_Internal || Linkage == GVA_C99Inline ||
5780 Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
5781 return false;
5782 return true;
5783 }
5784
5785 const VarDecl *VD = cast<VarDecl>(D);
5786 assert(VD->isFileVarDecl() && "Expected file scoped var");
5787
Argyrios Kyrtzidisab411c82010-07-29 20:07:52 +00005788 if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
5789 return false;
5790
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005791 // Structs that have non-trivial constructors or destructors are required.
5792
5793 // FIXME: Handle references.
5794 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
5795 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005796 if (RD->hasDefinition() &&
5797 (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()))
Argyrios Kyrtzidis90e99a82010-07-29 18:15:58 +00005798 return true;
5799 }
5800 }
5801
5802 GVALinkage L = GetGVALinkageForVariable(VD);
5803 if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
5804 if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
5805 return false;
5806 }
5807
5808 return true;
5809}
Charles Davis071cc7d2010-08-16 03:33:14 +00005810
5811CXXABI::~CXXABI() {}