blob: ca83964b3ecd1457f213f0a008185d13a7674e89 [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerddc135e2006-11-10 06:34:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyck8c89d592009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff67391b82007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
John McCall87fe5d52010-05-20 01:18:31 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000025#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000026#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000027#include "llvm/ADT/SmallString.h"
Anders Carlssond8499822007-10-29 05:01:08 +000028#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000029#include "llvm/Support/MathExtras.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000030#include "llvm/Support/raw_ostream.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000031
Chris Lattnerddc135e2006-11-10 06:34:16 +000032using namespace clang;
33
Steve Naroff0af91202007-04-27 21:51:21 +000034enum FloatingRank {
35 FloatRank, DoubleRank, LongDoubleRank
36};
37
Douglas Gregor7dbfb462010-06-16 21:09:37 +000038void
39ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
40 TemplateTemplateParmDecl *Parm) {
41 ID.AddInteger(Parm->getDepth());
42 ID.AddInteger(Parm->getPosition());
43 // FIXME: Parameter pack
44
45 TemplateParameterList *Params = Parm->getTemplateParameters();
46 ID.AddInteger(Params->size());
47 for (TemplateParameterList::const_iterator P = Params->begin(),
48 PEnd = Params->end();
49 P != PEnd; ++P) {
50 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
51 ID.AddInteger(0);
52 ID.AddBoolean(TTP->isParameterPack());
53 continue;
54 }
55
56 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
57 ID.AddInteger(1);
58 // FIXME: Parameter pack
59 ID.AddPointer(NTTP->getType().getAsOpaquePtr());
60 continue;
61 }
62
63 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
64 ID.AddInteger(2);
65 Profile(ID, TTP);
66 }
67}
68
69TemplateTemplateParmDecl *
70ASTContext::getCanonicalTemplateTemplateParmDecl(
71 TemplateTemplateParmDecl *TTP) {
72 // Check if we already have a canonical template template parameter.
73 llvm::FoldingSetNodeID ID;
74 CanonicalTemplateTemplateParm::Profile(ID, TTP);
75 void *InsertPos = 0;
76 CanonicalTemplateTemplateParm *Canonical
77 = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
78 if (Canonical)
79 return Canonical->getParam();
80
81 // Build a canonical template parameter list.
82 TemplateParameterList *Params = TTP->getTemplateParameters();
83 llvm::SmallVector<NamedDecl *, 4> CanonParams;
84 CanonParams.reserve(Params->size());
85 for (TemplateParameterList::const_iterator P = Params->begin(),
86 PEnd = Params->end();
87 P != PEnd; ++P) {
88 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
89 CanonParams.push_back(
90 TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
91 SourceLocation(), TTP->getDepth(),
92 TTP->getIndex(), 0, false,
93 TTP->isParameterPack()));
94 else if (NonTypeTemplateParmDecl *NTTP
95 = dyn_cast<NonTypeTemplateParmDecl>(*P))
96 CanonParams.push_back(
97 NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
98 SourceLocation(), NTTP->getDepth(),
99 NTTP->getPosition(), 0,
100 getCanonicalType(NTTP->getType()),
101 0));
102 else
103 CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
104 cast<TemplateTemplateParmDecl>(*P)));
105 }
106
107 TemplateTemplateParmDecl *CanonTTP
108 = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
109 SourceLocation(), TTP->getDepth(),
110 TTP->getPosition(), 0,
111 TemplateParameterList::Create(*this, SourceLocation(),
112 SourceLocation(),
113 CanonParams.data(),
114 CanonParams.size(),
115 SourceLocation()));
116
117 // Get the new insert position for the node we care about.
118 Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
119 assert(Canonical == 0 && "Shouldn't be in the map!");
120 (void)Canonical;
121
122 // Create the canonical template template parameter entry.
123 Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
124 CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
125 return CanonTTP;
126}
127
Chris Lattner465fa322008-10-05 17:34:18 +0000128ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar1b444192009-11-13 05:51:54 +0000129 const TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +0000130 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +0000131 Builtin::Context &builtins,
Mike Stump11289f42009-09-09 15:08:12 +0000132 bool FreeMem, unsigned size_reserve) :
John McCall773cc982010-06-11 11:07:21 +0000133 TemplateSpecializationTypes(this_()),
134 DependentTemplateSpecializationTypes(this_()),
Mike Stump11289f42009-09-09 15:08:12 +0000135 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Fariborz Jahaniane804c282010-04-23 17:41:07 +0000136 NSConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +0000137 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +0000138 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
John McCall8cb7bdf2010-06-04 23:28:52 +0000139 NullTypeSourceInfo(QualType()),
Douglas Gregor9507d462010-03-19 22:13:20 +0000140 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000141 Idents(idents), Selectors(sels),
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000142 BuiltinInfo(builtins),
143 DeclarationNames(*this),
144 ExternalSource(0), PrintingPolicy(LOpts),
Ted Kremenek520f47b2010-06-18 00:31:04 +0000145 LastSDM(0, 0),
146 UniqueBlockByRefTypeID(0), UniqueBlockParmTypeID(0) {
David Chisnall9f57c292009-08-17 16:35:33 +0000147 ObjCIdRedefinitionType = QualType();
148 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian04b258c2009-11-25 23:07:42 +0000149 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000150 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +0000151 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000152 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +0000153}
154
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000155ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000156 // Release the DenseMaps associated with DeclContext objects.
157 // FIXME: Is this the ideal solution?
158 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +0000159
Douglas Gregor1a809332010-05-23 18:26:36 +0000160 if (!FreeMemory) {
161 // Call all of the deallocation functions.
162 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
163 Deallocations[I].first(Deallocations[I].second);
164 }
165
Douglas Gregor832940b2010-03-02 23:58:15 +0000166 // Release all of the memory associated with overridden C++ methods.
167 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
168 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
169 OM != OMEnd; ++OM)
170 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000171
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000172 if (FreeMemory) {
173 // Deallocate all the types.
174 while (!Types.empty()) {
175 Types.back()->Destroy(*this);
176 Types.pop_back();
177 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000178
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000179 for (llvm::FoldingSet<ExtQuals>::iterator
180 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
181 // Increment in loop to prevent using deallocated memory.
John McCall8ccfcb52009-09-24 19:53:00 +0000182 Deallocate(&*I++);
Nuno Lopese013c7f2008-12-17 22:30:25 +0000183 }
Nuno Lopese013c7f2008-12-17 22:30:25 +0000184
Ted Kremenekc3015a92010-03-08 20:56:29 +0000185 for (llvm::DenseMap<const ObjCContainerDecl*,
186 const ASTRecordLayout*>::iterator
187 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
188 // Increment in loop to prevent using deallocated memory.
189 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
190 R->Destroy(*this);
191 }
Nuno Lopese013c7f2008-12-17 22:30:25 +0000192 }
193
Ted Kremenek076baeb2010-06-08 23:00:58 +0000194 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
195 // even when using the BumpPtrAllocator because they can contain
196 // DenseMaps.
197 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
198 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
199 // Increment in loop to prevent using deallocated memory.
200 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
201 R->Destroy(*this);
202 }
203
Douglas Gregorf21eb492009-03-26 23:50:42 +0000204 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000205 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
206 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000207 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000208 NNS != NNSEnd; ) {
209 // Increment in loop to prevent using deallocated memory.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000210 (*NNS++).Destroy(*this);
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000211 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000212
213 if (GlobalNestedNameSpecifier)
214 GlobalNestedNameSpecifier->Destroy(*this);
215
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000216 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000217}
218
Douglas Gregor1a809332010-05-23 18:26:36 +0000219void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
220 Deallocations.push_back(std::make_pair(Callback, Data));
221}
222
Mike Stump11289f42009-09-09 15:08:12 +0000223void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
225 ExternalSource.reset(Source.take());
226}
227
Chris Lattner4eb445d2007-01-26 01:27:23 +0000228void ASTContext::PrintStats() const {
229 fprintf(stderr, "*** AST Context Stats:\n");
230 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000231
Douglas Gregora30d0462009-05-26 14:40:08 +0000232 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000233#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000234#define ABSTRACT_TYPE(Name, Parent)
235#include "clang/AST/TypeNodes.def"
236 0 // Extra
237 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000238
Chris Lattner4eb445d2007-01-26 01:27:23 +0000239 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
240 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000241 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000242 }
243
Douglas Gregora30d0462009-05-26 14:40:08 +0000244 unsigned Idx = 0;
245 unsigned TotalBytes = 0;
246#define TYPE(Name, Parent) \
247 if (counts[Idx]) \
248 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
249 TotalBytes += counts[Idx] * sizeof(Name##Type); \
250 ++Idx;
251#define ABSTRACT_TYPE(Name, Parent)
252#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000253
Douglas Gregora30d0462009-05-26 14:40:08 +0000254 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255
256 if (ExternalSource.get()) {
257 fprintf(stderr, "\n");
258 ExternalSource->PrintStats();
259 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000260}
261
262
John McCall48f2d582009-10-23 23:03:21 +0000263void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000264 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000265 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000266 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000267}
268
Chris Lattner970e54e2006-11-12 00:37:36 +0000269void ASTContext::InitBuiltinTypes() {
270 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000271
Chris Lattner970e54e2006-11-12 00:37:36 +0000272 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000273 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattner970e54e2006-11-12 00:37:36 +0000275 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000276 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000277 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000278 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000279 InitBuiltinType(CharTy, BuiltinType::Char_S);
280 else
281 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000282 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000283 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
284 InitBuiltinType(ShortTy, BuiltinType::Short);
285 InitBuiltinType(IntTy, BuiltinType::Int);
286 InitBuiltinType(LongTy, BuiltinType::Long);
287 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner970e54e2006-11-12 00:37:36 +0000289 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000290 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
291 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
292 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
293 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
294 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000295
Chris Lattner970e54e2006-11-12 00:37:36 +0000296 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000297 InitBuiltinType(FloatTy, BuiltinType::Float);
298 InitBuiltinType(DoubleTy, BuiltinType::Double);
299 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000300
Chris Lattnerf122cef2009-04-30 02:43:43 +0000301 // GNU extension, 128-bit integers.
302 InitBuiltinType(Int128Ty, BuiltinType::Int128);
303 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
304
Chris Lattner007cb022009-02-26 23:43:47 +0000305 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
306 InitBuiltinType(WCharTy, BuiltinType::WChar);
307 else // C99
308 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000309
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000310 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
311 InitBuiltinType(Char16Ty, BuiltinType::Char16);
312 else // C99
313 Char16Ty = getFromTargetType(Target.getChar16Type());
314
315 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
316 InitBuiltinType(Char32Ty, BuiltinType::Char32);
317 else // C99
318 Char32Ty = getFromTargetType(Target.getChar32Type());
319
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000320 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000321 InitBuiltinType(OverloadTy, BuiltinType::Overload);
322
323 // Placeholder type for type-dependent expressions whose type is
324 // completely unknown. No code should ever check a type against
325 // DependentTy and users should never see it; however, it is here to
326 // help diagnose failures to properly check for type-dependent
327 // expressions.
328 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000329
Mike Stump11289f42009-09-09 15:08:12 +0000330 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000331 // not yet been deduced.
332 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000333
Chris Lattner970e54e2006-11-12 00:37:36 +0000334 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000335 FloatComplexTy = getComplexType(FloatTy);
336 DoubleComplexTy = getComplexType(DoubleTy);
337 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000338
Steve Naroff66e9f332007-10-15 14:41:52 +0000339 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000340
Steve Naroff1329fa02009-07-15 18:40:39 +0000341 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
342 ObjCIdTypedefType = QualType();
343 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000344 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000345
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000346 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000347 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
348 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000349 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000350
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000351 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000352
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000353 // void * type
354 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000355
356 // nullptr type (C++0x 2.14.7)
357 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000358}
359
Douglas Gregor86d142a2009-10-08 07:24:58 +0000360MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000361ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000362 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000363 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000364 = InstantiatedFromStaticDataMember.find(Var);
365 if (Pos == InstantiatedFromStaticDataMember.end())
366 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000368 return Pos->second;
369}
370
Mike Stump11289f42009-09-09 15:08:12 +0000371void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000372ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
373 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000374 assert(Inst->isStaticDataMember() && "Not a static data member");
375 assert(Tmpl->isStaticDataMember() && "Not a static data member");
376 assert(!InstantiatedFromStaticDataMember[Inst] &&
377 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000378 InstantiatedFromStaticDataMember[Inst]
379 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000380}
381
John McCalle61f2ba2009-11-18 02:36:19 +0000382NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000383ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000384 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000385 = InstantiatedFromUsingDecl.find(UUD);
386 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000387 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000389 return Pos->second;
390}
391
392void
John McCallb96ec562009-12-04 22:46:56 +0000393ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
394 assert((isa<UsingDecl>(Pattern) ||
395 isa<UnresolvedUsingValueDecl>(Pattern) ||
396 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
397 "pattern decl is not a using decl");
398 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
399 InstantiatedFromUsingDecl[Inst] = Pattern;
400}
401
402UsingShadowDecl *
403ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
404 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
405 = InstantiatedFromUsingShadowDecl.find(Inst);
406 if (Pos == InstantiatedFromUsingShadowDecl.end())
407 return 0;
408
409 return Pos->second;
410}
411
412void
413ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
414 UsingShadowDecl *Pattern) {
415 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
416 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000417}
418
Anders Carlsson5da84842009-09-01 04:26:58 +0000419FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
420 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
421 = InstantiatedFromUnnamedFieldDecl.find(Field);
422 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
423 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000424
Anders Carlsson5da84842009-09-01 04:26:58 +0000425 return Pos->second;
426}
427
428void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
429 FieldDecl *Tmpl) {
430 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
431 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
432 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
433 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000434
Anders Carlsson5da84842009-09-01 04:26:58 +0000435 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
436}
437
Douglas Gregor832940b2010-03-02 23:58:15 +0000438ASTContext::overridden_cxx_method_iterator
439ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
440 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
441 = OverriddenMethods.find(Method);
442 if (Pos == OverriddenMethods.end())
443 return 0;
444
445 return Pos->second.begin();
446}
447
448ASTContext::overridden_cxx_method_iterator
449ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
450 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
451 = OverriddenMethods.find(Method);
452 if (Pos == OverriddenMethods.end())
453 return 0;
454
455 return Pos->second.end();
456}
457
458void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
459 const CXXMethodDecl *Overridden) {
460 OverriddenMethods[Method].push_back(Overridden);
461}
462
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000463namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000464 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000465 : std::binary_function<SourceRange, SourceRange, bool> {
466 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000468 public:
469 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000471 bool operator()(SourceRange X, SourceRange Y) {
472 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
473 }
474 };
475}
476
Chris Lattner53cfe802007-07-18 17:52:12 +0000477//===----------------------------------------------------------------------===//
478// Type Sizing and Analysis
479//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000480
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000481/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
482/// scalar floating point type.
483const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000484 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000485 assert(BT && "Not a floating point type!");
486 switch (BT->getKind()) {
487 default: assert(0 && "Not a floating point type!");
488 case BuiltinType::Float: return Target.getFloatFormat();
489 case BuiltinType::Double: return Target.getDoubleFormat();
490 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
491 }
492}
493
Ken Dyck160146e2010-01-27 17:10:57 +0000494/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000495/// specified decl. Note that bitfields do not have a valid alignment, so
496/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000497/// If @p RefAsPointee, references are treated like their underlying type
498/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000499CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000500 unsigned Align = Target.getCharWidth();
501
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000502 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000503 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedman19a546c2009-02-22 02:56:25 +0000504
Chris Lattner68061312009-01-24 21:53:27 +0000505 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
506 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000507 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000508 if (RefAsPointee)
509 T = RT->getPointeeType();
510 else
511 T = getPointerType(RT->getPointeeType());
512 }
513 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindolae971b9a2010-06-04 23:15:27 +0000514 unsigned MinWidth = Target.getLargeArrayMinWidth();
515 unsigned ArrayAlign = Target.getLargeArrayAlign();
516 if (isa<VariableArrayType>(T) && MinWidth != 0)
517 Align = std::max(Align, ArrayAlign);
518 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
519 unsigned Size = getTypeSize(CT);
520 if (MinWidth != 0 && MinWidth <= Size)
521 Align = std::max(Align, ArrayAlign);
522 }
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000523 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000524 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
525 T = cast<ArrayType>(T)->getElementType();
526
527 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
528 }
Charles Davis3fc51072010-02-23 04:52:00 +0000529 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
530 // In the case of a field in a packed struct, we want the minimum
531 // of the alignment of the field and the alignment of the struct.
532 Align = std::min(Align,
533 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
534 }
Chris Lattner68061312009-01-24 21:53:27 +0000535 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000536
Ken Dyck160146e2010-01-27 17:10:57 +0000537 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000538}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000539
John McCall87fe5d52010-05-20 01:18:31 +0000540std::pair<CharUnits, CharUnits>
541ASTContext::getTypeInfoInChars(const Type *T) {
542 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
543 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
544 CharUnits::fromQuantity(Info.second / getCharWidth()));
545}
546
547std::pair<CharUnits, CharUnits>
548ASTContext::getTypeInfoInChars(QualType T) {
549 return getTypeInfoInChars(T.getTypePtr());
550}
551
Chris Lattner983a8bb2007-07-13 22:13:22 +0000552/// getTypeSize - Return the size of the specified type, in bits. This method
553/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000554///
555/// FIXME: Pointers into different addr spaces could have different sizes and
556/// alignment requirements: getPointerInfo should take an AddrSpace, this
557/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000558std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000559ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000560 uint64_t Width=0;
561 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000562 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000563#define TYPE(Class, Base)
564#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000565#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000566#define DEPENDENT_TYPE(Class, Base) case Type::Class:
567#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000568 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000569 break;
570
Chris Lattner355332d2007-07-13 22:27:08 +0000571 case Type::FunctionNoProto:
572 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000573 // GCC extension: alignof(function) = 32 bits
574 Width = 0;
575 Align = 32;
576 break;
577
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000578 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000579 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000580 Width = 0;
581 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
582 break;
583
Steve Naroff5c131802007-08-30 01:06:46 +0000584 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000585 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000586
Chris Lattner37e05872008-03-05 18:54:05 +0000587 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000588 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000589 Align = EltInfo.second;
590 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000591 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000592 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000593 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000594 const VectorType *VT = cast<VectorType>(T);
595 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
596 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000597 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000598 // If the alignment is not a power of 2, round up to the next power of 2.
599 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000600 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000601 Align = llvm::NextPowerOf2(Align);
602 Width = llvm::RoundUpToAlignment(Width, Align);
603 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000604 break;
605 }
Chris Lattner647fb222007-07-18 18:26:58 +0000606
Chris Lattner7570e9c2008-03-08 08:52:55 +0000607 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000608 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000609 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000610 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000611 // GCC extension: alignof(void) = 8 bits.
612 Width = 0;
613 Align = 8;
614 break;
615
Chris Lattner6a4f7452007-12-19 19:23:28 +0000616 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000617 Width = Target.getBoolWidth();
618 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000619 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000620 case BuiltinType::Char_S:
621 case BuiltinType::Char_U:
622 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000623 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000624 Width = Target.getCharWidth();
625 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000626 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000627 case BuiltinType::WChar:
628 Width = Target.getWCharWidth();
629 Align = Target.getWCharAlign();
630 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000631 case BuiltinType::Char16:
632 Width = Target.getChar16Width();
633 Align = Target.getChar16Align();
634 break;
635 case BuiltinType::Char32:
636 Width = Target.getChar32Width();
637 Align = Target.getChar32Align();
638 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000639 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000640 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000641 Width = Target.getShortWidth();
642 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000643 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000644 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000645 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000646 Width = Target.getIntWidth();
647 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000648 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000649 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000650 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000651 Width = Target.getLongWidth();
652 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000653 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000654 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000655 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000656 Width = Target.getLongLongWidth();
657 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000658 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000659 case BuiltinType::Int128:
660 case BuiltinType::UInt128:
661 Width = 128;
662 Align = 128; // int128_t is 128-bit aligned on all targets.
663 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000664 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000665 Width = Target.getFloatWidth();
666 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000667 break;
668 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000669 Width = Target.getDoubleWidth();
670 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000671 break;
672 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000673 Width = Target.getLongDoubleWidth();
674 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000675 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000676 case BuiltinType::NullPtr:
677 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
678 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000679 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000680 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000681 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000682 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000683 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000684 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000685 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000686 case Type::BlockPointer: {
687 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
688 Width = Target.getPointerWidth(AS);
689 Align = Target.getPointerAlign(AS);
690 break;
691 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000692 case Type::LValueReference:
693 case Type::RValueReference: {
694 // alignof and sizeof should never enter this code path here, so we go
695 // the pointer route.
696 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
697 Width = Target.getPointerWidth(AS);
698 Align = Target.getPointerAlign(AS);
699 break;
700 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000701 case Type::Pointer: {
702 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000703 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000704 Align = Target.getPointerAlign(AS);
705 break;
706 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000707 case Type::MemberPointer: {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000708 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000709 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000710 getTypeInfo(getPointerDiffType());
711 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000712 if (Pointee->isFunctionType())
713 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000714 Align = PtrDiffInfo.second;
715 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000716 }
Chris Lattner647fb222007-07-18 18:26:58 +0000717 case Type::Complex: {
718 // Complex types have the same alignment as their elements, but twice the
719 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000720 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000721 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000722 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000723 Align = EltInfo.second;
724 break;
725 }
John McCall8b07ec22010-05-15 11:32:37 +0000726 case Type::ObjCObject:
727 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Pateldbb72632008-06-04 21:54:36 +0000728 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000729 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000730 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
731 Width = Layout.getSize();
732 Align = Layout.getAlignment();
733 break;
734 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000735 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000736 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000737 const TagType *TT = cast<TagType>(T);
738
739 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000740 Width = 1;
741 Align = 1;
742 break;
743 }
Mike Stump11289f42009-09-09 15:08:12 +0000744
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000745 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000746 return getTypeInfo(ET->getDecl()->getIntegerType());
747
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000748 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000749 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
750 Width = Layout.getSize();
751 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000752 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000753 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000754
Chris Lattner63d2b362009-10-22 05:17:15 +0000755 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000756 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
757 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000758
Douglas Gregoref462e62009-04-30 17:32:17 +0000759 case Type::Typedef: {
760 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000761 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000762 Align = std::max(Aligned->getMaxAlignment(),
763 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregoref462e62009-04-30 17:32:17 +0000764 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
765 } else
766 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000767 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000768 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000769
770 case Type::TypeOfExpr:
771 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
772 .getTypePtr());
773
774 case Type::TypeOf:
775 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
776
Anders Carlsson81df7b82009-06-24 19:06:50 +0000777 case Type::Decltype:
778 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
779 .getTypePtr());
780
Abramo Bagnara6150c882010-05-11 21:36:43 +0000781 case Type::Elaborated:
782 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000783
Douglas Gregoref462e62009-04-30 17:32:17 +0000784 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000785 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000786 "Cannot request the size of a dependent type");
787 // FIXME: this is likely to be wrong once we support template
788 // aliases, since a template alias could refer to a typedef that
789 // has an __aligned__ attribute on it.
790 return getTypeInfo(getCanonicalType(T));
791 }
Mike Stump11289f42009-09-09 15:08:12 +0000792
Chris Lattner53cfe802007-07-18 17:52:12 +0000793 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000794 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000795}
796
Ken Dyck8c89d592009-12-22 14:23:30 +0000797/// getTypeSizeInChars - Return the size of the specified type, in characters.
798/// This method does not work on incomplete types.
799CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000800 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000801}
802CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000803 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000804}
805
Ken Dycka6046ab2010-01-26 17:25:18 +0000806/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000807/// characters. This method does not work on incomplete types.
808CharUnits ASTContext::getTypeAlignInChars(QualType T) {
809 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
810}
811CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
812 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
813}
814
Chris Lattnera3402cd2009-01-27 18:08:34 +0000815/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
816/// type for the current target in bits. This can be different than the ABI
817/// alignment in cases where it is beneficial for performance to overalign
818/// a data type.
819unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
820 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000821
822 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000823 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000824 T = CT->getElementType().getTypePtr();
825 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
826 T->isSpecificBuiltinType(BuiltinType::LongLong))
827 return std::max(ABIAlign, (unsigned)getTypeSize(T));
828
Chris Lattnera3402cd2009-01-27 18:08:34 +0000829 return ABIAlign;
830}
831
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000832static void CollectLocalObjCIvars(ASTContext *Ctx,
833 const ObjCInterfaceDecl *OI,
834 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000835 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
836 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000837 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000838 if (!IVDecl->isInvalidDecl())
839 Fields.push_back(cast<FieldDecl>(IVDecl));
840 }
841}
842
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000843void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
844 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
845 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
846 CollectObjCIvars(SuperClass, Fields);
847 CollectLocalObjCIvars(this, OI, Fields);
848}
849
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000850/// ShallowCollectObjCIvars -
851/// Collect all ivars, including those synthesized, in the current class.
852///
853void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000854 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000855 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
856 E = OI->ivar_end(); I != E; ++I) {
857 Ivars.push_back(*I);
858 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000859
860 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000861}
862
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000863/// CollectNonClassIvars -
864/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000865/// This includes synthesized ivars (via @synthesize) and those in
866// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000867///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000868void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000869 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000870 // Find ivars declared in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000871 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
872 CDecl = CDecl->getNextClassExtension()) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000873 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
874 E = CDecl->ivar_end(); I != E; ++I) {
875 Ivars.push_back(*I);
876 }
877 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000878
Ted Kremenek86838aa2010-03-11 19:44:54 +0000879 // Also add any ivar defined in this class's implementation. This
880 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000881 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
882 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
883 E = ImplDecl->ivar_end(); I != E; ++I)
884 Ivars.push_back(*I);
885 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000886}
887
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000888/// CollectInheritedProtocols - Collect all protocols in current class and
889/// those inherited by it.
890void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000891 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000892 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
893 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
894 PE = OI->protocol_end(); P != PE; ++P) {
895 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000896 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000897 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000898 PE = Proto->protocol_end(); P != PE; ++P) {
899 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000900 CollectInheritedProtocols(*P, Protocols);
901 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000902 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000903
904 // Categories of this Interface.
905 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
906 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
907 CollectInheritedProtocols(CDeclChain, Protocols);
908 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
909 while (SD) {
910 CollectInheritedProtocols(SD, Protocols);
911 SD = SD->getSuperClass();
912 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000913 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000914 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
915 PE = OC->protocol_end(); P != PE; ++P) {
916 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000917 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000918 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
919 PE = Proto->protocol_end(); P != PE; ++P)
920 CollectInheritedProtocols(*P, Protocols);
921 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000922 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000923 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
924 PE = OP->protocol_end(); P != PE; ++P) {
925 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000926 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000927 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
928 PE = Proto->protocol_end(); P != PE; ++P)
929 CollectInheritedProtocols(*P, Protocols);
930 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000931 }
932}
933
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000934unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
935 unsigned count = 0;
936 // Count ivars declared in class extension.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000937 for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
938 CDecl = CDecl->getNextClassExtension())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000939 count += CDecl->ivar_size();
940
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000941 // Count ivar defined in this class's implementation. This
942 // includes synthesized ivars.
943 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000944 count += ImplDecl->ivar_size();
945
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000946 return count;
947}
948
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000949/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
950ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
951 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
952 I = ObjCImpls.find(D);
953 if (I != ObjCImpls.end())
954 return cast<ObjCImplementationDecl>(I->second);
955 return 0;
956}
957/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
958ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
959 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
960 I = ObjCImpls.find(D);
961 if (I != ObjCImpls.end())
962 return cast<ObjCCategoryImplDecl>(I->second);
963 return 0;
964}
965
966/// \brief Set the implementation of ObjCInterfaceDecl.
967void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
968 ObjCImplementationDecl *ImplD) {
969 assert(IFaceD && ImplD && "Passed null params");
970 ObjCImpls[IFaceD] = ImplD;
971}
972/// \brief Set the implementation of ObjCCategoryDecl.
973void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
974 ObjCCategoryImplDecl *ImplD) {
975 assert(CatD && ImplD && "Passed null params");
976 ObjCImpls[CatD] = ImplD;
977}
978
John McCallbcd03502009-12-07 02:54:59 +0000979/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000980///
John McCallbcd03502009-12-07 02:54:59 +0000981/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000982/// the TypeLoc wrappers.
983///
984/// \param T the type that will be the basis for type source info. This type
985/// should refer to how the declarator was written in source code, not to
986/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000987TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000988 unsigned DataSize) {
989 if (!DataSize)
990 DataSize = TypeLoc::getFullDataSizeForType(T);
991 else
992 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000993 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000994
John McCallbcd03502009-12-07 02:54:59 +0000995 TypeSourceInfo *TInfo =
996 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
997 new (TInfo) TypeSourceInfo(T);
998 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000999}
1000
John McCallbcd03502009-12-07 02:54:59 +00001001TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +00001002 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +00001003 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +00001004 DI->getTypeLoc().initialize(L);
1005 return DI;
1006}
1007
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001008const ASTRecordLayout &
1009ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1010 return getObjCLayout(D, 0);
1011}
1012
1013const ASTRecordLayout &
1014ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1015 return getObjCLayout(D->getClassInterface(), D);
1016}
1017
Chris Lattner983a8bb2007-07-13 22:13:22 +00001018//===----------------------------------------------------------------------===//
1019// Type creation/memoization methods
1020//===----------------------------------------------------------------------===//
1021
John McCall8ccfcb52009-09-24 19:53:00 +00001022QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1023 unsigned Fast = Quals.getFastQualifiers();
1024 Quals.removeFastQualifiers();
1025
1026 // Check if we've already instantiated this type.
1027 llvm::FoldingSetNodeID ID;
1028 ExtQuals::Profile(ID, TypeNode, Quals);
1029 void *InsertPos = 0;
1030 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1031 assert(EQ->getQualifiers() == Quals);
1032 QualType T = QualType(EQ, Fast);
1033 return T;
1034 }
1035
John McCall90d1c2d2009-09-24 23:30:46 +00001036 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001037 ExtQualNodes.InsertNode(New, InsertPos);
1038 QualType T = QualType(New, Fast);
1039 return T;
1040}
1041
1042QualType ASTContext::getVolatileType(QualType T) {
1043 QualType CanT = getCanonicalType(T);
1044 if (CanT.isVolatileQualified()) return T;
1045
1046 QualifierCollector Quals;
1047 const Type *TypeNode = Quals.strip(T);
1048 Quals.addVolatile();
1049
1050 return getExtQualType(TypeNode, Quals);
1051}
1052
Fariborz Jahanianece85822009-02-17 18:27:45 +00001053QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001054 QualType CanT = getCanonicalType(T);
1055 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001056 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001057
John McCall8ccfcb52009-09-24 19:53:00 +00001058 // If we are composing extended qualifiers together, merge together
1059 // into one ExtQuals node.
1060 QualifierCollector Quals;
1061 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001062
John McCall8ccfcb52009-09-24 19:53:00 +00001063 // If this type already has an address space specified, it cannot get
1064 // another one.
1065 assert(!Quals.hasAddressSpace() &&
1066 "Type cannot be in multiple addr spaces!");
1067 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001068
John McCall8ccfcb52009-09-24 19:53:00 +00001069 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001070}
1071
Chris Lattnerd60183d2009-02-18 22:53:11 +00001072QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001073 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001074 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001075 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001076 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001077
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001078 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001079 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001080 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001081 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1082 return getPointerType(ResultType);
1083 }
1084 }
Mike Stump11289f42009-09-09 15:08:12 +00001085
John McCall8ccfcb52009-09-24 19:53:00 +00001086 // If we are composing extended qualifiers together, merge together
1087 // into one ExtQuals node.
1088 QualifierCollector Quals;
1089 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001090
John McCall8ccfcb52009-09-24 19:53:00 +00001091 // If this type already has an ObjCGC specified, it cannot get
1092 // another one.
1093 assert(!Quals.hasObjCGCAttr() &&
1094 "Type cannot have multiple ObjCGCs!");
1095 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001096
John McCall8ccfcb52009-09-24 19:53:00 +00001097 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001098}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001099
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001100static QualType getExtFunctionType(ASTContext& Context, QualType T,
1101 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001102 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001103 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1104 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001105 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001106 if (ResultType == Pointee)
1107 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001108
1109 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001110 } else if (const BlockPointerType *BlockPointer
1111 = T->getAs<BlockPointerType>()) {
1112 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001113 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001114 if (ResultType == Pointee)
1115 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001116
1117 ResultType = Context.getBlockPointerType(ResultType);
1118 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001119 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001120 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001121
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001122 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001123 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001124 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001125 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001126 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001127 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001128 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1129 FPT->getNumArgs(), FPT->isVariadic(),
1130 FPT->getTypeQuals(),
1131 FPT->hasExceptionSpec(),
1132 FPT->hasAnyExceptionSpec(),
1133 FPT->getNumExceptions(),
1134 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001135 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001136 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001137 } else
1138 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001139
1140 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1141}
1142
1143QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001144 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001145 return getExtFunctionType(*this, T,
1146 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001147}
1148
1149QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001150 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001151 return getExtFunctionType(*this, T,
1152 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001153}
1154
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001155QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1156 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1157 return getExtFunctionType(*this, T,
1158 Info.withRegParm(RegParm));
1159}
1160
Chris Lattnerc6395932007-06-22 20:56:16 +00001161/// getComplexType - Return the uniqued reference to the type for a complex
1162/// number with the specified element type.
1163QualType ASTContext::getComplexType(QualType T) {
1164 // Unique pointers, to guarantee there is only one pointer of a particular
1165 // structure.
1166 llvm::FoldingSetNodeID ID;
1167 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001168
Chris Lattnerc6395932007-06-22 20:56:16 +00001169 void *InsertPos = 0;
1170 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1171 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001172
Chris Lattnerc6395932007-06-22 20:56:16 +00001173 // If the pointee type isn't canonical, this won't be a canonical type either,
1174 // so fill in the canonical type field.
1175 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001176 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001177 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001178
Chris Lattnerc6395932007-06-22 20:56:16 +00001179 // Get the new insert position for the node we care about.
1180 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001181 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001182 }
John McCall90d1c2d2009-09-24 23:30:46 +00001183 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001184 Types.push_back(New);
1185 ComplexTypes.InsertNode(New, InsertPos);
1186 return QualType(New, 0);
1187}
1188
Chris Lattner970e54e2006-11-12 00:37:36 +00001189/// getPointerType - Return the uniqued reference to the type for a pointer to
1190/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001191QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001192 // Unique pointers, to guarantee there is only one pointer of a particular
1193 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001194 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001195 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001196
Chris Lattner67521df2007-01-27 01:29:36 +00001197 void *InsertPos = 0;
1198 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001199 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001200
Chris Lattner7ccecb92006-11-12 08:50:50 +00001201 // If the pointee type isn't canonical, this won't be a canonical type either,
1202 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001203 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001204 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001205 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001206
Chris Lattner67521df2007-01-27 01:29:36 +00001207 // Get the new insert position for the node we care about.
1208 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001209 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001210 }
John McCall90d1c2d2009-09-24 23:30:46 +00001211 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001212 Types.push_back(New);
1213 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001214 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001215}
1216
Mike Stump11289f42009-09-09 15:08:12 +00001217/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001218/// a pointer to the specified block.
1219QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001220 assert(T->isFunctionType() && "block of function types only");
1221 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001222 // structure.
1223 llvm::FoldingSetNodeID ID;
1224 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001225
Steve Naroffec33ed92008-08-27 16:04:49 +00001226 void *InsertPos = 0;
1227 if (BlockPointerType *PT =
1228 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1229 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001230
1231 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001232 // type either so fill in the canonical type field.
1233 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001234 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001235 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001236
Steve Naroffec33ed92008-08-27 16:04:49 +00001237 // Get the new insert position for the node we care about.
1238 BlockPointerType *NewIP =
1239 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001240 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001241 }
John McCall90d1c2d2009-09-24 23:30:46 +00001242 BlockPointerType *New
1243 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001244 Types.push_back(New);
1245 BlockPointerTypes.InsertNode(New, InsertPos);
1246 return QualType(New, 0);
1247}
1248
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001249/// getLValueReferenceType - Return the uniqued reference to the type for an
1250/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001251QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001252 // Unique pointers, to guarantee there is only one pointer of a particular
1253 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001254 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001255 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001256
1257 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001258 if (LValueReferenceType *RT =
1259 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001260 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001261
John McCallfc93cf92009-10-22 22:37:11 +00001262 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1263
Bill Wendling3708c182007-05-27 10:15:43 +00001264 // If the referencee type isn't canonical, this won't be a canonical type
1265 // either, so fill in the canonical type field.
1266 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001267 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1268 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1269 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001270
Bill Wendling3708c182007-05-27 10:15:43 +00001271 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001272 LValueReferenceType *NewIP =
1273 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001274 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001275 }
1276
John McCall90d1c2d2009-09-24 23:30:46 +00001277 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001278 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1279 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001280 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001281 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001282
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001283 return QualType(New, 0);
1284}
1285
1286/// getRValueReferenceType - Return the uniqued reference to the type for an
1287/// rvalue reference to the specified type.
1288QualType ASTContext::getRValueReferenceType(QualType T) {
1289 // Unique pointers, to guarantee there is only one pointer of a particular
1290 // structure.
1291 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001292 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001293
1294 void *InsertPos = 0;
1295 if (RValueReferenceType *RT =
1296 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1297 return QualType(RT, 0);
1298
John McCallfc93cf92009-10-22 22:37:11 +00001299 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1300
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001301 // If the referencee type isn't canonical, this won't be a canonical type
1302 // either, so fill in the canonical type field.
1303 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001304 if (InnerRef || !T.isCanonical()) {
1305 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1306 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001307
1308 // Get the new insert position for the node we care about.
1309 RValueReferenceType *NewIP =
1310 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1311 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1312 }
1313
John McCall90d1c2d2009-09-24 23:30:46 +00001314 RValueReferenceType *New
1315 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001316 Types.push_back(New);
1317 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001318 return QualType(New, 0);
1319}
1320
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001321/// getMemberPointerType - Return the uniqued reference to the type for a
1322/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001323QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001324 // Unique pointers, to guarantee there is only one pointer of a particular
1325 // structure.
1326 llvm::FoldingSetNodeID ID;
1327 MemberPointerType::Profile(ID, T, Cls);
1328
1329 void *InsertPos = 0;
1330 if (MemberPointerType *PT =
1331 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1332 return QualType(PT, 0);
1333
1334 // If the pointee or class type isn't canonical, this won't be a canonical
1335 // type either, so fill in the canonical type field.
1336 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001337 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001338 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1339
1340 // Get the new insert position for the node we care about.
1341 MemberPointerType *NewIP =
1342 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1343 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1344 }
John McCall90d1c2d2009-09-24 23:30:46 +00001345 MemberPointerType *New
1346 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001347 Types.push_back(New);
1348 MemberPointerTypes.InsertNode(New, InsertPos);
1349 return QualType(New, 0);
1350}
1351
Mike Stump11289f42009-09-09 15:08:12 +00001352/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001353/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001354QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001355 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001356 ArrayType::ArraySizeModifier ASM,
1357 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001358 assert((EltTy->isDependentType() ||
1359 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001360 "Constant array of VLAs is illegal!");
1361
Chris Lattnere2df3f92009-05-13 04:12:56 +00001362 // Convert the array size into a canonical width matching the pointer size for
1363 // the target.
1364 llvm::APInt ArySize(ArySizeIn);
1365 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001366
Chris Lattner23b7eb62007-06-15 23:05:46 +00001367 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001368 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattner36f8e652007-01-27 08:31:04 +00001370 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001371 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001372 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001373 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001374
Chris Lattner7ccecb92006-11-12 08:50:50 +00001375 // If the element type isn't canonical, this won't be a canonical type either,
1376 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001377 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001378 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001379 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001380 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001381 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001382 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001383 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001384 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001385 }
Mike Stump11289f42009-09-09 15:08:12 +00001386
John McCall90d1c2d2009-09-24 23:30:46 +00001387 ConstantArrayType *New = new(*this,TypeAlignment)
1388 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001389 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001390 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001391 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001392}
1393
Steve Naroffcadebd02007-08-30 18:14:25 +00001394/// getVariableArrayType - Returns a non-unique reference to the type for a
1395/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001396QualType ASTContext::getVariableArrayType(QualType EltTy,
1397 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001398 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001399 unsigned EltTypeQuals,
1400 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001401 // Since we don't unique expressions, it isn't possible to unique VLA's
1402 // that have an expression provided for their size.
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001403 QualType CanonType;
1404
1405 if (!EltTy.isCanonical()) {
1406 if (NumElts)
1407 NumElts->Retain();
1408 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1409 EltTypeQuals, Brackets);
1410 }
1411
John McCall90d1c2d2009-09-24 23:30:46 +00001412 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001413 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001414
1415 VariableArrayTypes.push_back(New);
1416 Types.push_back(New);
1417 return QualType(New, 0);
1418}
1419
Douglas Gregor4619e432008-12-05 23:32:09 +00001420/// getDependentSizedArrayType - Returns a non-unique reference to
1421/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001422/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001423QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1424 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001425 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001426 unsigned EltTypeQuals,
1427 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001428 assert((!NumElts || NumElts->isTypeDependent() ||
1429 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001430 "Size must be type- or value-dependent!");
1431
Douglas Gregorf3f95522009-07-31 00:23:35 +00001432 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001433 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001434 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001435
1436 if (NumElts) {
1437 // Dependently-sized array types that do not have a specified
1438 // number of elements will have their sizes deduced from an
1439 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001440 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1441 EltTypeQuals, NumElts);
1442
1443 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1444 }
1445
Douglas Gregorf3f95522009-07-31 00:23:35 +00001446 DependentSizedArrayType *New;
1447 if (Canon) {
1448 // We already have a canonical version of this array type; use it as
1449 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001450 New = new (*this, TypeAlignment)
1451 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1452 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001453 } else {
1454 QualType CanonEltTy = getCanonicalType(EltTy);
1455 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001456 New = new (*this, TypeAlignment)
1457 DependentSizedArrayType(*this, EltTy, QualType(),
1458 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001459
Douglas Gregorc42075a2010-02-04 18:10:26 +00001460 if (NumElts) {
1461 DependentSizedArrayType *CanonCheck
1462 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1463 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1464 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001465 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001466 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001467 } else {
1468 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1469 ASM, EltTypeQuals,
1470 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001471 New = new (*this, TypeAlignment)
1472 DependentSizedArrayType(*this, EltTy, Canon,
1473 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001474 }
1475 }
Mike Stump11289f42009-09-09 15:08:12 +00001476
Douglas Gregor4619e432008-12-05 23:32:09 +00001477 Types.push_back(New);
1478 return QualType(New, 0);
1479}
1480
Eli Friedmanbd258282008-02-15 18:16:39 +00001481QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1482 ArrayType::ArraySizeModifier ASM,
1483 unsigned EltTypeQuals) {
1484 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001485 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001486
1487 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001488 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001489 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1490 return QualType(ATP, 0);
1491
1492 // If the element type isn't canonical, this won't be a canonical type
1493 // either, so fill in the canonical type field.
1494 QualType Canonical;
1495
John McCallb692a092009-10-22 20:10:53 +00001496 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001497 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001498 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001499
1500 // Get the new insert position for the node we care about.
1501 IncompleteArrayType *NewIP =
1502 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001503 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001504 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001505
John McCall90d1c2d2009-09-24 23:30:46 +00001506 IncompleteArrayType *New = new (*this, TypeAlignment)
1507 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001508
1509 IncompleteArrayTypes.InsertNode(New, InsertPos);
1510 Types.push_back(New);
1511 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001512}
1513
Steve Naroff91fcddb2007-07-18 18:00:27 +00001514/// getVectorType - Return the unique reference to a vector type of
1515/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001516QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
Chris Lattner37141f42010-06-23 06:00:24 +00001517 VectorType::AltiVecSpecific AltiVecSpec) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001518 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001519
Chris Lattner76a00cf2008-04-06 22:59:24 +00001520 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001521 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001522
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001523 // Check if we've already instantiated a vector of this type.
1524 llvm::FoldingSetNodeID ID;
Chris Lattner37141f42010-06-23 06:00:24 +00001525 VectorType::Profile(ID, vecType, NumElts, Type::Vector, AltiVecSpec);
1526
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001527 void *InsertPos = 0;
1528 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1529 return QualType(VTP, 0);
1530
1531 // If the element type isn't canonical, this won't be a canonical type either,
1532 // so fill in the canonical type field.
1533 QualType Canonical;
Chris Lattner37141f42010-06-23 06:00:24 +00001534 if (!vecType.isCanonical() || (AltiVecSpec == VectorType::AltiVec)) {
1535 // pass VectorType::NotAltiVec for AltiVecSpec to make AltiVec canonical
1536 // vector type (except 'vector bool ...' and 'vector Pixel') the same as
1537 // the equivalent GCC vector types
1538 Canonical = getVectorType(getCanonicalType(vecType), NumElts,
1539 VectorType::NotAltiVec);
Mike Stump11289f42009-09-09 15:08:12 +00001540
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001541 // Get the new insert position for the node we care about.
1542 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001543 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001544 }
John McCall90d1c2d2009-09-24 23:30:46 +00001545 VectorType *New = new (*this, TypeAlignment)
Chris Lattner37141f42010-06-23 06:00:24 +00001546 VectorType(vecType, NumElts, Canonical, AltiVecSpec);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001547 VectorTypes.InsertNode(New, InsertPos);
1548 Types.push_back(New);
1549 return QualType(New, 0);
1550}
1551
Nate Begemance4d7fc2008-04-18 23:10:10 +00001552/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001553/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001554QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001555 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001556
Chris Lattner76a00cf2008-04-06 22:59:24 +00001557 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001558 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001559
Steve Naroff91fcddb2007-07-18 18:00:27 +00001560 // Check if we've already instantiated a vector of this type.
1561 llvm::FoldingSetNodeID ID;
Chris Lattner37141f42010-06-23 06:00:24 +00001562 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
1563 VectorType::NotAltiVec);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001564 void *InsertPos = 0;
1565 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1566 return QualType(VTP, 0);
1567
1568 // If the element type isn't canonical, this won't be a canonical type either,
1569 // so fill in the canonical type field.
1570 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001571 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001572 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001573
Steve Naroff91fcddb2007-07-18 18:00:27 +00001574 // Get the new insert position for the node we care about.
1575 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001576 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001577 }
John McCall90d1c2d2009-09-24 23:30:46 +00001578 ExtVectorType *New = new (*this, TypeAlignment)
1579 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001580 VectorTypes.InsertNode(New, InsertPos);
1581 Types.push_back(New);
1582 return QualType(New, 0);
1583}
1584
Mike Stump11289f42009-09-09 15:08:12 +00001585QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001586 Expr *SizeExpr,
1587 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001588 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001589 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001590 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001591
Douglas Gregor352169a2009-07-31 03:54:25 +00001592 void *InsertPos = 0;
1593 DependentSizedExtVectorType *Canon
1594 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1595 DependentSizedExtVectorType *New;
1596 if (Canon) {
1597 // We already have a canonical version of this array type; use it as
1598 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001599 New = new (*this, TypeAlignment)
1600 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1601 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001602 } else {
1603 QualType CanonVecTy = getCanonicalType(vecType);
1604 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001605 New = new (*this, TypeAlignment)
1606 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1607 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001608
1609 DependentSizedExtVectorType *CanonCheck
1610 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1611 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1612 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001613 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1614 } else {
1615 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1616 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001617 New = new (*this, TypeAlignment)
1618 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001619 }
1620 }
Mike Stump11289f42009-09-09 15:08:12 +00001621
Douglas Gregor758a8692009-06-17 21:51:59 +00001622 Types.push_back(New);
1623 return QualType(New, 0);
1624}
1625
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001626/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001627///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001628QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1629 const FunctionType::ExtInfo &Info) {
1630 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001631 // Unique functions, to guarantee there is only one function of a particular
1632 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001633 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001634 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001635
Chris Lattner47955de2007-01-27 08:37:20 +00001636 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001637 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001638 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001639 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001640
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001641 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001642 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001643 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001644 Canonical =
1645 getFunctionNoProtoType(getCanonicalType(ResultTy),
1646 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001647
Chris Lattner47955de2007-01-27 08:37:20 +00001648 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001649 FunctionNoProtoType *NewIP =
1650 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001651 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001652 }
Mike Stump11289f42009-09-09 15:08:12 +00001653
John McCall90d1c2d2009-09-24 23:30:46 +00001654 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001655 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001656 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001657 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001658 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001659}
1660
1661/// getFunctionType - Return a normal function type with a typed argument
1662/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001663QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001664 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001665 unsigned TypeQuals, bool hasExceptionSpec,
1666 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001667 const QualType *ExArray,
1668 const FunctionType::ExtInfo &Info) {
1669 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001670 // Unique functions, to guarantee there is only one function of a particular
1671 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001672 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001673 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001674 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001675 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001676
1677 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001678 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001679 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001680 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001681
1682 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001683 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001684 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001685 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001686 isCanonical = false;
1687
1688 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001689 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001690 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001691 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001692 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001693 CanonicalArgs.reserve(NumArgs);
1694 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001695 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001696
Chris Lattner76a00cf2008-04-06 22:59:24 +00001697 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001698 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001699 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001700 false, 0, 0,
1701 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001702
Chris Lattnerfd4de792007-01-27 01:15:32 +00001703 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001704 FunctionProtoType *NewIP =
1705 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001706 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001707 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001708
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001709 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001710 // for two variable size arrays (for parameter and exception types) at the
1711 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001712 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001713 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1714 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001715 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001716 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001717 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001718 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001719 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001720 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001721 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001722}
Chris Lattneref51c202006-11-10 07:17:23 +00001723
John McCalle78aac42010-03-10 03:28:59 +00001724#ifndef NDEBUG
1725static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1726 if (!isa<CXXRecordDecl>(D)) return false;
1727 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1728 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1729 return true;
1730 if (RD->getDescribedClassTemplate() &&
1731 !isa<ClassTemplateSpecializationDecl>(RD))
1732 return true;
1733 return false;
1734}
1735#endif
1736
1737/// getInjectedClassNameType - Return the unique reference to the
1738/// injected class name type for the specified templated declaration.
1739QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1740 QualType TST) {
1741 assert(NeedsInjectedClassNameType(Decl));
1742 if (Decl->TypeForDecl) {
1743 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00001744 } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
John McCalle78aac42010-03-10 03:28:59 +00001745 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1746 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1747 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1748 } else {
John McCall2408e322010-04-27 00:57:59 +00001749 Decl->TypeForDecl =
1750 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001751 Types.push_back(Decl->TypeForDecl);
1752 }
1753 return QualType(Decl->TypeForDecl, 0);
1754}
1755
Douglas Gregor83a586e2008-04-13 21:07:44 +00001756/// getTypeDeclType - Return the unique reference to the type for the
1757/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001758QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001759 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001760 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001761
John McCall81e38502010-02-16 03:57:14 +00001762 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001763 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001764
John McCall96f0b5f2010-03-10 06:48:02 +00001765 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1766 "Template type parameter types are always available.");
1767
John McCall81e38502010-02-16 03:57:14 +00001768 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001769 assert(!Record->getPreviousDeclaration() &&
1770 "struct/union has previous declaration");
1771 assert(!NeedsInjectedClassNameType(Record));
1772 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001773 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001774 assert(!Enum->getPreviousDeclaration() &&
1775 "enum has previous declaration");
1776 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001777 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001778 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1779 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001780 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001781 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001782
John McCall96f0b5f2010-03-10 06:48:02 +00001783 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001784 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001785}
1786
Chris Lattner32d920b2007-01-26 02:01:53 +00001787/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001788/// specified typename decl.
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001789QualType
1790ASTContext::getTypedefType(const TypedefDecl *Decl, QualType Canonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001791 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001792
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001793 if (Canonical.isNull())
1794 Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001795 Decl->TypeForDecl = new(*this, TypeAlignment)
1796 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001797 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001798 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001799}
1800
John McCallcebee162009-10-18 09:09:24 +00001801/// \brief Retrieve a substitution-result type.
1802QualType
1803ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1804 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001805 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001806 && "replacement types must always be canonical");
1807
1808 llvm::FoldingSetNodeID ID;
1809 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1810 void *InsertPos = 0;
1811 SubstTemplateTypeParmType *SubstParm
1812 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1813
1814 if (!SubstParm) {
1815 SubstParm = new (*this, TypeAlignment)
1816 SubstTemplateTypeParmType(Parm, Replacement);
1817 Types.push_back(SubstParm);
1818 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1819 }
1820
1821 return QualType(SubstParm, 0);
1822}
1823
Douglas Gregoreff93e02009-02-05 23:33:38 +00001824/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001825/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001826/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001827QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001828 bool ParameterPack,
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001829 IdentifierInfo *Name) {
Douglas Gregoreff93e02009-02-05 23:33:38 +00001830 llvm::FoldingSetNodeID ID;
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001831 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001832 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001833 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001834 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1835
1836 if (TypeParm)
1837 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001838
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001839 if (Name) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00001840 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001841 TypeParm = new (*this, TypeAlignment)
1842 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001843
1844 TemplateTypeParmType *TypeCheck
1845 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1846 assert(!TypeCheck && "Template type parameter canonical type broken");
1847 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001848 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001849 TypeParm = new (*this, TypeAlignment)
1850 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001851
1852 Types.push_back(TypeParm);
1853 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1854
1855 return QualType(TypeParm, 0);
1856}
1857
John McCalle78aac42010-03-10 03:28:59 +00001858TypeSourceInfo *
1859ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1860 SourceLocation NameLoc,
1861 const TemplateArgumentListInfo &Args,
1862 QualType CanonType) {
1863 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1864
1865 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1866 TemplateSpecializationTypeLoc TL
1867 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1868 TL.setTemplateNameLoc(NameLoc);
1869 TL.setLAngleLoc(Args.getLAngleLoc());
1870 TL.setRAngleLoc(Args.getRAngleLoc());
1871 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1872 TL.setArgLocInfo(i, Args[i].getLocInfo());
1873 return DI;
1874}
1875
Mike Stump11289f42009-09-09 15:08:12 +00001876QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001877ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001878 const TemplateArgumentListInfo &Args,
John McCall30576cd2010-06-13 09:25:03 +00001879 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00001880 unsigned NumArgs = Args.size();
1881
John McCall0ad16662009-10-29 08:12:44 +00001882 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1883 ArgVec.reserve(NumArgs);
1884 for (unsigned i = 0; i != NumArgs; ++i)
1885 ArgVec.push_back(Args[i].getArgument());
1886
John McCall2408e322010-04-27 00:57:59 +00001887 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00001888 Canon);
John McCall0ad16662009-10-29 08:12:44 +00001889}
1890
1891QualType
1892ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001893 const TemplateArgument *Args,
1894 unsigned NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00001895 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001896 if (!Canon.isNull())
1897 Canon = getCanonicalType(Canon);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001898 else
1899 Canon = getCanonicalTemplateSpecializationType(Template, Args, NumArgs);
Douglas Gregord56a91e2009-02-26 22:19:44 +00001900
Douglas Gregora8e02e72009-07-28 23:00:59 +00001901 // Allocate the (non-canonical) template specialization type, but don't
1902 // try to unique it: these types typically have location information that
1903 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001904 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001905 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001906 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001907 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00001908 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00001909 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001910 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001911
Douglas Gregor8bf42052009-02-09 18:46:07 +00001912 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001913 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001914}
1915
Mike Stump11289f42009-09-09 15:08:12 +00001916QualType
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00001917ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
1918 const TemplateArgument *Args,
1919 unsigned NumArgs) {
1920 // Build the canonical template specialization type.
1921 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1922 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1923 CanonArgs.reserve(NumArgs);
1924 for (unsigned I = 0; I != NumArgs; ++I)
1925 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1926
1927 // Determine whether this canonical template specialization type already
1928 // exists.
1929 llvm::FoldingSetNodeID ID;
1930 TemplateSpecializationType::Profile(ID, CanonTemplate,
1931 CanonArgs.data(), NumArgs, *this);
1932
1933 void *InsertPos = 0;
1934 TemplateSpecializationType *Spec
1935 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1936
1937 if (!Spec) {
1938 // Allocate a new canonical template specialization type.
1939 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
1940 sizeof(TemplateArgument) * NumArgs),
1941 TypeAlignment);
1942 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
1943 CanonArgs.data(), NumArgs,
1944 QualType());
1945 Types.push_back(Spec);
1946 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1947 }
1948
1949 assert(Spec->isDependentType() &&
1950 "Non-dependent template-id type must have a canonical type");
1951 return QualType(Spec, 0);
1952}
1953
1954QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00001955ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
1956 NestedNameSpecifier *NNS,
1957 QualType NamedType) {
Douglas Gregor52537682009-03-19 00:18:19 +00001958 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001959 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001960
1961 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001962 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00001963 if (T)
1964 return QualType(T, 0);
1965
Douglas Gregorc42075a2010-02-04 18:10:26 +00001966 QualType Canon = NamedType;
1967 if (!Canon.isCanonical()) {
1968 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00001969 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1970 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00001971 (void)CheckT;
1972 }
1973
Abramo Bagnara6150c882010-05-11 21:36:43 +00001974 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001975 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00001976 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00001977 return QualType(T, 0);
1978}
1979
Douglas Gregor02085352010-03-31 20:19:30 +00001980QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1981 NestedNameSpecifier *NNS,
1982 const IdentifierInfo *Name,
1983 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001984 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1985
1986 if (Canon.isNull()) {
1987 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001988 ElaboratedTypeKeyword CanonKeyword = Keyword;
1989 if (Keyword == ETK_None)
1990 CanonKeyword = ETK_Typename;
1991
1992 if (CanonNNS != NNS || CanonKeyword != Keyword)
1993 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001994 }
1995
1996 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001997 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001998
1999 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002000 DependentNameType *T
2001 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00002002 if (T)
2003 return QualType(T, 0);
2004
Douglas Gregor02085352010-03-31 20:19:30 +00002005 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002006 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002007 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002008 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002009}
2010
Mike Stump11289f42009-09-09 15:08:12 +00002011QualType
John McCallc392f372010-06-11 00:33:02 +00002012ASTContext::getDependentTemplateSpecializationType(
2013 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002014 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002015 const IdentifierInfo *Name,
2016 const TemplateArgumentListInfo &Args) {
2017 // TODO: avoid this copy
2018 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2019 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2020 ArgCopy.push_back(Args[I].getArgument());
2021 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2022 ArgCopy.size(),
2023 ArgCopy.data());
2024}
2025
2026QualType
2027ASTContext::getDependentTemplateSpecializationType(
2028 ElaboratedTypeKeyword Keyword,
2029 NestedNameSpecifier *NNS,
2030 const IdentifierInfo *Name,
2031 unsigned NumArgs,
2032 const TemplateArgument *Args) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002033 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2034
Douglas Gregorc42075a2010-02-04 18:10:26 +00002035 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002036 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2037 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002038
2039 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002040 DependentTemplateSpecializationType *T
2041 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002042 if (T)
2043 return QualType(T, 0);
2044
John McCallc392f372010-06-11 00:33:02 +00002045 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002046
John McCallc392f372010-06-11 00:33:02 +00002047 ElaboratedTypeKeyword CanonKeyword = Keyword;
2048 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2049
2050 bool AnyNonCanonArgs = false;
2051 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2052 for (unsigned I = 0; I != NumArgs; ++I) {
2053 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2054 if (!CanonArgs[I].structurallyEquals(Args[I]))
2055 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002056 }
2057
John McCallc392f372010-06-11 00:33:02 +00002058 QualType Canon;
2059 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2060 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2061 Name, NumArgs,
2062 CanonArgs.data());
2063
2064 // Find the insert position again.
2065 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2066 }
2067
2068 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2069 sizeof(TemplateArgument) * NumArgs),
2070 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002071 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002072 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002073 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002074 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002075 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002076}
2077
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002078/// CmpProtocolNames - Comparison predicate for sorting protocols
2079/// alphabetically.
2080static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2081 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002082 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002083}
2084
John McCall8b07ec22010-05-15 11:32:37 +00002085static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002086 unsigned NumProtocols) {
2087 if (NumProtocols == 0) return true;
2088
2089 for (unsigned i = 1; i != NumProtocols; ++i)
2090 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2091 return false;
2092 return true;
2093}
2094
2095static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002096 unsigned &NumProtocols) {
2097 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002098
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002099 // Sort protocols, keyed by name.
2100 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2101
2102 // Remove duplicates.
2103 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2104 NumProtocols = ProtocolsEnd-Protocols;
2105}
2106
John McCall8b07ec22010-05-15 11:32:37 +00002107QualType ASTContext::getObjCObjectType(QualType BaseType,
2108 ObjCProtocolDecl * const *Protocols,
2109 unsigned NumProtocols) {
2110 // If the base type is an interface and there aren't any protocols
2111 // to add, then the interface type will do just fine.
2112 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2113 return BaseType;
2114
2115 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002116 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002117 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002118 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002119 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2120 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002121
John McCall8b07ec22010-05-15 11:32:37 +00002122 // Build the canonical type, which has the canonical base type and
2123 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002124 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002125 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2126 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2127 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002128 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2129 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002130 unsigned UniqueCount = NumProtocols;
2131
John McCallfc93cf92009-10-22 22:37:11 +00002132 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002133 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2134 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002135 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002136 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2137 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002138 }
2139
2140 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002141 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2142 }
2143
2144 unsigned Size = sizeof(ObjCObjectTypeImpl);
2145 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2146 void *Mem = Allocate(Size, TypeAlignment);
2147 ObjCObjectTypeImpl *T =
2148 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2149
2150 Types.push_back(T);
2151 ObjCObjectTypes.InsertNode(T, InsertPos);
2152 return QualType(T, 0);
2153}
2154
2155/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2156/// the given object type.
2157QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2158 llvm::FoldingSetNodeID ID;
2159 ObjCObjectPointerType::Profile(ID, ObjectT);
2160
2161 void *InsertPos = 0;
2162 if (ObjCObjectPointerType *QT =
2163 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2164 return QualType(QT, 0);
2165
2166 // Find the canonical object type.
2167 QualType Canonical;
2168 if (!ObjectT.isCanonical()) {
2169 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2170
2171 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002172 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2173 }
2174
Douglas Gregorf85bee62010-02-08 22:59:26 +00002175 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002176 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2177 ObjCObjectPointerType *QType =
2178 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002179
Steve Narofffb4330f2009-06-17 22:40:22 +00002180 Types.push_back(QType);
2181 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002182 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002183}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002184
Steve Naroffc277ad12009-07-18 15:33:26 +00002185/// getObjCInterfaceType - Return the unique reference to the type for the
2186/// specified ObjC interface decl. The list of protocols is optional.
John McCall8b07ec22010-05-15 11:32:37 +00002187QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2188 if (Decl->TypeForDecl)
2189 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002190
John McCall8b07ec22010-05-15 11:32:37 +00002191 // FIXME: redeclarations?
2192 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2193 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2194 Decl->TypeForDecl = T;
2195 Types.push_back(T);
2196 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002197}
2198
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002199/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2200/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002201/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002202/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002203/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002204QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002205 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002206 if (tofExpr->isTypeDependent()) {
2207 llvm::FoldingSetNodeID ID;
2208 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002209
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002210 void *InsertPos = 0;
2211 DependentTypeOfExprType *Canon
2212 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2213 if (Canon) {
2214 // We already have a "canonical" version of an identical, dependent
2215 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002216 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002217 QualType((TypeOfExprType*)Canon, 0));
2218 }
2219 else {
2220 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002221 Canon
2222 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002223 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2224 toe = Canon;
2225 }
2226 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002227 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002228 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002229 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002230 Types.push_back(toe);
2231 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002232}
2233
Steve Naroffa773cd52007-08-01 18:02:17 +00002234/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2235/// TypeOfType AST's. The only motivation to unique these nodes would be
2236/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002237/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002238/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002239QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002240 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002241 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002242 Types.push_back(tot);
2243 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002244}
2245
Anders Carlssonad6bd352009-06-24 21:24:56 +00002246/// getDecltypeForExpr - Given an expr, will return the decltype for that
2247/// expression, according to the rules in C++0x [dcl.type.simple]p4
2248static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002249 if (e->isTypeDependent())
2250 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002251
Anders Carlssonad6bd352009-06-24 21:24:56 +00002252 // If e is an id expression or a class member access, decltype(e) is defined
2253 // as the type of the entity named by e.
2254 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2255 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2256 return VD->getType();
2257 }
2258 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2259 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2260 return FD->getType();
2261 }
2262 // If e is a function call or an invocation of an overloaded operator,
2263 // (parentheses around e are ignored), decltype(e) is defined as the
2264 // return type of that function.
2265 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2266 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002267
Anders Carlssonad6bd352009-06-24 21:24:56 +00002268 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002269
2270 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002271 // defined as T&, otherwise decltype(e) is defined as T.
2272 if (e->isLvalue(Context) == Expr::LV_Valid)
2273 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002274
Anders Carlssonad6bd352009-06-24 21:24:56 +00002275 return T;
2276}
2277
Anders Carlsson81df7b82009-06-24 19:06:50 +00002278/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2279/// DecltypeType AST's. The only motivation to unique these nodes would be
2280/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002281/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002282/// on canonical type's (which are always unique).
2283QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002284 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002285 if (e->isTypeDependent()) {
2286 llvm::FoldingSetNodeID ID;
2287 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002288
Douglas Gregora21f6c32009-07-30 23:36:40 +00002289 void *InsertPos = 0;
2290 DependentDecltypeType *Canon
2291 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2292 if (Canon) {
2293 // We already have a "canonical" version of an equivalent, dependent
2294 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002295 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002296 QualType((DecltypeType*)Canon, 0));
2297 }
2298 else {
2299 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002300 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002301 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2302 dt = Canon;
2303 }
2304 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002305 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002306 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002307 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002308 Types.push_back(dt);
2309 return QualType(dt, 0);
2310}
2311
Chris Lattnerfb072462007-01-23 05:45:31 +00002312/// getTagDeclType - Return the unique reference to the type for the
2313/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002314QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002315 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002316 // FIXME: What is the design on getTagDeclType when it requires casting
2317 // away const? mutable?
2318 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002319}
2320
Mike Stump11289f42009-09-09 15:08:12 +00002321/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2322/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2323/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002324CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002325 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002326}
Chris Lattnerfb072462007-01-23 05:45:31 +00002327
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002328/// getSignedWCharType - Return the type of "signed wchar_t".
2329/// Used when in C++, as a GCC extension.
2330QualType ASTContext::getSignedWCharType() const {
2331 // FIXME: derive from "Target" ?
2332 return WCharTy;
2333}
2334
2335/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2336/// Used when in C++, as a GCC extension.
2337QualType ASTContext::getUnsignedWCharType() const {
2338 // FIXME: derive from "Target" ?
2339 return UnsignedIntTy;
2340}
2341
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002342/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2343/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2344QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002345 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002346}
2347
Chris Lattnera21ad802008-04-02 05:18:44 +00002348//===----------------------------------------------------------------------===//
2349// Type Operators
2350//===----------------------------------------------------------------------===//
2351
John McCallfc93cf92009-10-22 22:37:11 +00002352CanQualType ASTContext::getCanonicalParamType(QualType T) {
2353 // Push qualifiers into arrays, and then discard any remaining
2354 // qualifiers.
2355 T = getCanonicalType(T);
2356 const Type *Ty = T.getTypePtr();
2357
2358 QualType Result;
2359 if (isa<ArrayType>(Ty)) {
2360 Result = getArrayDecayedType(QualType(Ty,0));
2361 } else if (isa<FunctionType>(Ty)) {
2362 Result = getPointerType(QualType(Ty, 0));
2363 } else {
2364 Result = QualType(Ty, 0);
2365 }
2366
2367 return CanQualType::CreateUnsafe(Result);
2368}
2369
Chris Lattnered0d0792008-04-06 22:41:35 +00002370/// getCanonicalType - Return the canonical (structural) type corresponding to
2371/// the specified potentially non-canonical type. The non-canonical version
2372/// of a type may have many "decorated" versions of types. Decorators can
2373/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2374/// to be free of any of these, allowing two canonical types to be compared
2375/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002376CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002377 QualifierCollector Quals;
2378 const Type *Ptr = Quals.strip(T);
2379 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002380
John McCall8ccfcb52009-09-24 19:53:00 +00002381 // The canonical internal type will be the canonical type *except*
2382 // that we push type qualifiers down through array types.
2383
2384 // If there are no new qualifiers to push down, stop here.
2385 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002386 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002387
John McCall8ccfcb52009-09-24 19:53:00 +00002388 // If the type qualifiers are on an array type, get the canonical
2389 // type of the array with the qualifiers applied to the element
2390 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002391 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2392 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002393 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002394
Chris Lattner7adf0762008-08-04 07:31:14 +00002395 // Get the canonical version of the element with the extra qualifiers on it.
2396 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002397 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002398 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002399
Chris Lattner7adf0762008-08-04 07:31:14 +00002400 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002401 return CanQualType::CreateUnsafe(
2402 getConstantArrayType(NewEltTy, CAT->getSize(),
2403 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002404 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002405 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002406 return CanQualType::CreateUnsafe(
2407 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002408 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002409
Douglas Gregor4619e432008-12-05 23:32:09 +00002410 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002411 return CanQualType::CreateUnsafe(
2412 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002413 DSAT->getSizeExpr() ?
2414 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002415 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002416 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002417 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002418
Chris Lattner7adf0762008-08-04 07:31:14 +00002419 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002420 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002421 VAT->getSizeExpr() ?
2422 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002423 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002424 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002425 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002426}
2427
Chandler Carruth607f38e2009-12-29 07:16:59 +00002428QualType ASTContext::getUnqualifiedArrayType(QualType T,
2429 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002430 Quals = T.getQualifiers();
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002431 const ArrayType *AT = getAsArrayType(T);
2432 if (!AT) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002433 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002434 }
2435
Chandler Carruth607f38e2009-12-29 07:16:59 +00002436 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002437 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002438 if (Elt == UnqualElt)
2439 return T;
2440
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002441 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002442 return getConstantArrayType(UnqualElt, CAT->getSize(),
2443 CAT->getSizeModifier(), 0);
2444 }
2445
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002446 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002447 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2448 }
2449
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002450 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2451 return getVariableArrayType(UnqualElt,
2452 VAT->getSizeExpr() ?
2453 VAT->getSizeExpr()->Retain() : 0,
2454 VAT->getSizeModifier(),
2455 VAT->getIndexTypeCVRQualifiers(),
2456 VAT->getBracketsRange());
2457 }
2458
2459 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002460 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2461 DSAT->getSizeModifier(), 0,
2462 SourceRange());
2463}
2464
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002465/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2466/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2467/// they point to and return true. If T1 and T2 aren't pointer types
2468/// or pointer-to-member types, or if they are not similar at this
2469/// level, returns false and leaves T1 and T2 unchanged. Top-level
2470/// qualifiers on T1 and T2 are ignored. This function will typically
2471/// be called in a loop that successively "unwraps" pointer and
2472/// pointer-to-member types to compare them at each level.
2473bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2474 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2475 *T2PtrType = T2->getAs<PointerType>();
2476 if (T1PtrType && T2PtrType) {
2477 T1 = T1PtrType->getPointeeType();
2478 T2 = T2PtrType->getPointeeType();
2479 return true;
2480 }
2481
2482 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2483 *T2MPType = T2->getAs<MemberPointerType>();
2484 if (T1MPType && T2MPType &&
2485 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2486 QualType(T2MPType->getClass(), 0))) {
2487 T1 = T1MPType->getPointeeType();
2488 T2 = T2MPType->getPointeeType();
2489 return true;
2490 }
2491
2492 if (getLangOptions().ObjC1) {
2493 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2494 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2495 if (T1OPType && T2OPType) {
2496 T1 = T1OPType->getPointeeType();
2497 T2 = T2OPType->getPointeeType();
2498 return true;
2499 }
2500 }
2501
2502 // FIXME: Block pointers, too?
2503
2504 return false;
2505}
2506
John McCall847e2a12009-11-24 18:42:40 +00002507DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2508 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2509 return TD->getDeclName();
2510
2511 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2512 if (DTN->isIdentifier()) {
2513 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2514 } else {
2515 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2516 }
2517 }
2518
John McCalld28ae272009-12-02 08:04:21 +00002519 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2520 assert(Storage);
2521 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002522}
2523
Douglas Gregor6bc50582009-05-07 06:41:52 +00002524TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002525 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2526 if (TemplateTemplateParmDecl *TTP
2527 = dyn_cast<TemplateTemplateParmDecl>(Template))
2528 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2529
2530 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002531 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002532 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002533
John McCalld28ae272009-12-02 08:04:21 +00002534 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002535
Douglas Gregor6bc50582009-05-07 06:41:52 +00002536 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2537 assert(DTN && "Non-dependent template names must refer to template decls.");
2538 return DTN->CanonicalTemplateName;
2539}
2540
Douglas Gregoradee3e32009-11-11 23:06:43 +00002541bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2542 X = getCanonicalTemplateName(X);
2543 Y = getCanonicalTemplateName(Y);
2544 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2545}
2546
Mike Stump11289f42009-09-09 15:08:12 +00002547TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002548ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2549 switch (Arg.getKind()) {
2550 case TemplateArgument::Null:
2551 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002552
Douglas Gregora8e02e72009-07-28 23:00:59 +00002553 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002554 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002555
Douglas Gregora8e02e72009-07-28 23:00:59 +00002556 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002557 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002558
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002559 case TemplateArgument::Template:
2560 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2561
Douglas Gregora8e02e72009-07-28 23:00:59 +00002562 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002563 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002564 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002565
Douglas Gregora8e02e72009-07-28 23:00:59 +00002566 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002567 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002568
Douglas Gregora8e02e72009-07-28 23:00:59 +00002569 case TemplateArgument::Pack: {
2570 // FIXME: Allocate in ASTContext
2571 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2572 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002573 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002574 AEnd = Arg.pack_end();
2575 A != AEnd; (void)++A, ++Idx)
2576 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002577
Douglas Gregora8e02e72009-07-28 23:00:59 +00002578 TemplateArgument Result;
2579 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2580 return Result;
2581 }
2582 }
2583
2584 // Silence GCC warning
2585 assert(false && "Unhandled template argument kind");
2586 return TemplateArgument();
2587}
2588
Douglas Gregor333489b2009-03-27 23:10:48 +00002589NestedNameSpecifier *
2590ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002591 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002592 return 0;
2593
2594 switch (NNS->getKind()) {
2595 case NestedNameSpecifier::Identifier:
2596 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002597 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002598 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2599 NNS->getAsIdentifier());
2600
2601 case NestedNameSpecifier::Namespace:
2602 // A namespace is canonical; build a nested-name-specifier with
2603 // this namespace and no prefix.
2604 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2605
2606 case NestedNameSpecifier::TypeSpec:
2607 case NestedNameSpecifier::TypeSpecWithTemplate: {
2608 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002609 return NestedNameSpecifier::Create(*this, 0,
2610 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002611 T.getTypePtr());
2612 }
2613
2614 case NestedNameSpecifier::Global:
2615 // The global specifier is canonical and unique.
2616 return NNS;
2617 }
2618
2619 // Required to silence a GCC warning
2620 return 0;
2621}
2622
Chris Lattner7adf0762008-08-04 07:31:14 +00002623
2624const ArrayType *ASTContext::getAsArrayType(QualType T) {
2625 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002626 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002627 // Handle the common positive case fast.
2628 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2629 return AT;
2630 }
Mike Stump11289f42009-09-09 15:08:12 +00002631
John McCall8ccfcb52009-09-24 19:53:00 +00002632 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002633 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002634 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002635 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002636
John McCall8ccfcb52009-09-24 19:53:00 +00002637 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002638 // implements C99 6.7.3p8: "If the specification of an array type includes
2639 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002640
Chris Lattner7adf0762008-08-04 07:31:14 +00002641 // If we get here, we either have type qualifiers on the type, or we have
2642 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002643 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002644
John McCall8ccfcb52009-09-24 19:53:00 +00002645 QualifierCollector Qs;
2646 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002647
Chris Lattner7adf0762008-08-04 07:31:14 +00002648 // If we have a simple case, just return now.
2649 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002650 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002651 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002652
Chris Lattner7adf0762008-08-04 07:31:14 +00002653 // Otherwise, we have an array and we have qualifiers on it. Push the
2654 // qualifiers into the array element type and return a new array type.
2655 // Get the canonical version of the element with the extra qualifiers on it.
2656 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002657 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002658
Chris Lattner7adf0762008-08-04 07:31:14 +00002659 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2660 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2661 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002662 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002663 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2664 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2665 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002666 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002667
Mike Stump11289f42009-09-09 15:08:12 +00002668 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002669 = dyn_cast<DependentSizedArrayType>(ATy))
2670 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002671 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002672 DSAT->getSizeExpr() ?
2673 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002674 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002675 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002676 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002677
Chris Lattner7adf0762008-08-04 07:31:14 +00002678 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002679 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002680 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002681 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002682 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002683 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002684 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002685}
2686
2687
Chris Lattnera21ad802008-04-02 05:18:44 +00002688/// getArrayDecayedType - Return the properly qualified result of decaying the
2689/// specified array type to a pointer. This operation is non-trivial when
2690/// handling typedefs etc. The canonical type of "T" must be an array type,
2691/// this returns a pointer to a properly qualified element of the array.
2692///
2693/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2694QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002695 // Get the element type with 'getAsArrayType' so that we don't lose any
2696 // typedefs in the element type of the array. This also handles propagation
2697 // of type qualifiers from the array type into the element type if present
2698 // (C99 6.7.3p8).
2699 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2700 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002701
Chris Lattner7adf0762008-08-04 07:31:14 +00002702 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002703
2704 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002705 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002706}
2707
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002708QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002709 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002710 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2711 QT = AT->getElementType();
2712 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002713}
2714
Anders Carlsson4bf82142009-09-25 01:23:32 +00002715QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2716 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002717
Anders Carlsson4bf82142009-09-25 01:23:32 +00002718 if (const ArrayType *AT = getAsArrayType(ElemTy))
2719 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002720
Anders Carlssone0808df2008-12-21 03:44:36 +00002721 return ElemTy;
2722}
2723
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002724/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002725uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002726ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2727 uint64_t ElementCount = 1;
2728 do {
2729 ElementCount *= CA->getSize().getZExtValue();
2730 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2731 } while (CA);
2732 return ElementCount;
2733}
2734
Steve Naroff0af91202007-04-27 21:51:21 +00002735/// getFloatingRank - Return a relative rank for floating point types.
2736/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002737static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002738 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002739 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002740
John McCall9dd450b2009-09-21 23:43:11 +00002741 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2742 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002743 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002744 case BuiltinType::Float: return FloatRank;
2745 case BuiltinType::Double: return DoubleRank;
2746 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002747 }
2748}
2749
Mike Stump11289f42009-09-09 15:08:12 +00002750/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2751/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002752/// 'typeDomain' is a real floating point or complex type.
2753/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002754QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2755 QualType Domain) const {
2756 FloatingRank EltRank = getFloatingRank(Size);
2757 if (Domain->isComplexType()) {
2758 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002759 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002760 case FloatRank: return FloatComplexTy;
2761 case DoubleRank: return DoubleComplexTy;
2762 case LongDoubleRank: return LongDoubleComplexTy;
2763 }
Steve Naroff0af91202007-04-27 21:51:21 +00002764 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002765
2766 assert(Domain->isRealFloatingType() && "Unknown domain!");
2767 switch (EltRank) {
2768 default: assert(0 && "getFloatingRank(): illegal value for rank");
2769 case FloatRank: return FloatTy;
2770 case DoubleRank: return DoubleTy;
2771 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002772 }
Steve Naroffe4718892007-04-27 18:30:00 +00002773}
2774
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002775/// getFloatingTypeOrder - Compare the rank of the two specified floating
2776/// point types, ignoring the domain of the type (i.e. 'double' ==
2777/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002778/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002779int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2780 FloatingRank LHSR = getFloatingRank(LHS);
2781 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002782
Chris Lattnerb90739d2008-04-06 23:38:49 +00002783 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002784 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002785 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002786 return 1;
2787 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002788}
2789
Chris Lattner76a00cf2008-04-06 22:59:24 +00002790/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2791/// routine will assert if passed a built-in type that isn't an integer or enum,
2792/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002793unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002794 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002795 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002796 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002797
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002798 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2799 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2800
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002801 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2802 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2803
2804 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2805 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2806
Chris Lattner76a00cf2008-04-06 22:59:24 +00002807 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002808 default: assert(0 && "getIntegerRank(): not a built-in integer");
2809 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002810 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002811 case BuiltinType::Char_S:
2812 case BuiltinType::Char_U:
2813 case BuiltinType::SChar:
2814 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002815 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002816 case BuiltinType::Short:
2817 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002818 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002819 case BuiltinType::Int:
2820 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002821 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002822 case BuiltinType::Long:
2823 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002824 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002825 case BuiltinType::LongLong:
2826 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002827 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002828 case BuiltinType::Int128:
2829 case BuiltinType::UInt128:
2830 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002831 }
2832}
2833
Eli Friedman629ffb92009-08-20 04:21:42 +00002834/// \brief Whether this is a promotable bitfield reference according
2835/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2836///
2837/// \returns the type this bit-field will promote to, or NULL if no
2838/// promotion occurs.
2839QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00002840 if (E->isTypeDependent() || E->isValueDependent())
2841 return QualType();
2842
Eli Friedman629ffb92009-08-20 04:21:42 +00002843 FieldDecl *Field = E->getBitField();
2844 if (!Field)
2845 return QualType();
2846
2847 QualType FT = Field->getType();
2848
2849 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2850 uint64_t BitWidth = BitWidthAP.getZExtValue();
2851 uint64_t IntSize = getTypeSize(IntTy);
2852 // GCC extension compatibility: if the bit-field size is less than or equal
2853 // to the size of int, it gets promoted no matter what its type is.
2854 // For instance, unsigned long bf : 4 gets promoted to signed int.
2855 if (BitWidth < IntSize)
2856 return IntTy;
2857
2858 if (BitWidth == IntSize)
2859 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2860
2861 // Types bigger than int are not subject to promotions, and therefore act
2862 // like the base type.
2863 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2864 // is ridiculous.
2865 return QualType();
2866}
2867
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002868/// getPromotedIntegerType - Returns the type that Promotable will
2869/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2870/// integer type.
2871QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2872 assert(!Promotable.isNull());
2873 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002874 if (const EnumType *ET = Promotable->getAs<EnumType>())
2875 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002876 if (Promotable->isSignedIntegerType())
2877 return IntTy;
2878 uint64_t PromotableSize = getTypeSize(Promotable);
2879 uint64_t IntSize = getTypeSize(IntTy);
2880 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2881 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2882}
2883
Mike Stump11289f42009-09-09 15:08:12 +00002884/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002885/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002886/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002887int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002888 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2889 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002890 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002891
Chris Lattner76a00cf2008-04-06 22:59:24 +00002892 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2893 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002894
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002895 unsigned LHSRank = getIntegerRank(LHSC);
2896 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002897
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002898 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2899 if (LHSRank == RHSRank) return 0;
2900 return LHSRank > RHSRank ? 1 : -1;
2901 }
Mike Stump11289f42009-09-09 15:08:12 +00002902
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002903 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2904 if (LHSUnsigned) {
2905 // If the unsigned [LHS] type is larger, return it.
2906 if (LHSRank >= RHSRank)
2907 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002908
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002909 // If the signed type can represent all values of the unsigned type, it
2910 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002911 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002912 return -1;
2913 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002914
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002915 // If the unsigned [RHS] type is larger, return it.
2916 if (RHSRank >= LHSRank)
2917 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002918
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002919 // If the signed type can represent all values of the unsigned type, it
2920 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002921 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002922 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002923}
Anders Carlsson98f07902007-08-17 05:31:46 +00002924
Anders Carlsson6d417272009-11-14 21:45:58 +00002925static RecordDecl *
2926CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2927 SourceLocation L, IdentifierInfo *Id) {
2928 if (Ctx.getLangOptions().CPlusPlus)
2929 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2930 else
2931 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2932}
2933
Mike Stump11289f42009-09-09 15:08:12 +00002934// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002935QualType ASTContext::getCFConstantStringType() {
2936 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002937 CFConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002938 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002939 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002940 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002941
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002942 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002943
Anders Carlsson98f07902007-08-17 05:31:46 +00002944 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002945 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002946 // int flags;
2947 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002948 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002949 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002950 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002951 FieldTypes[3] = LongTy;
2952
Anders Carlsson98f07902007-08-17 05:31:46 +00002953 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002954 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002955 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002956 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002957 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002958 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002959 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002960 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002961 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002962 }
2963
Douglas Gregord5058122010-02-11 01:19:42 +00002964 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002965 }
Mike Stump11289f42009-09-09 15:08:12 +00002966
Anders Carlsson98f07902007-08-17 05:31:46 +00002967 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002968}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002969
Douglas Gregor512b0772009-04-23 22:29:11 +00002970void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002971 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002972 assert(Rec && "Invalid CFConstantStringType");
2973 CFConstantStringTypeDecl = Rec->getDecl();
2974}
2975
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002976// getNSConstantStringType - Return the type used for constant NSStrings.
2977QualType ASTContext::getNSConstantStringType() {
2978 if (!NSConstantStringTypeDecl) {
2979 NSConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002980 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002981 &Idents.get("__builtin_NSString"));
2982 NSConstantStringTypeDecl->startDefinition();
2983
2984 QualType FieldTypes[3];
2985
2986 // const int *isa;
2987 FieldTypes[0] = getPointerType(IntTy.withConst());
2988 // const char *str;
2989 FieldTypes[1] = getPointerType(CharTy.withConst());
2990 // unsigned int length;
2991 FieldTypes[2] = UnsignedIntTy;
2992
2993 // Create fields
2994 for (unsigned i = 0; i < 3; ++i) {
2995 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2996 SourceLocation(), 0,
2997 FieldTypes[i], /*TInfo=*/0,
2998 /*BitWidth=*/0,
2999 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003000 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00003001 NSConstantStringTypeDecl->addDecl(Field);
3002 }
3003
3004 NSConstantStringTypeDecl->completeDefinition();
3005 }
3006
3007 return getTagDeclType(NSConstantStringTypeDecl);
3008}
3009
3010void ASTContext::setNSConstantStringType(QualType T) {
3011 const RecordType *Rec = T->getAs<RecordType>();
3012 assert(Rec && "Invalid NSConstantStringType");
3013 NSConstantStringTypeDecl = Rec->getDecl();
3014}
3015
Mike Stump11289f42009-09-09 15:08:12 +00003016QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003017 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003018 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003019 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003020 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003021 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003022
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003023 QualType FieldTypes[] = {
3024 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003025 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003026 getPointerType(UnsignedLongTy),
3027 getConstantArrayType(UnsignedLongTy,
3028 llvm::APInt(32, 5), ArrayType::Normal, 0)
3029 };
Mike Stump11289f42009-09-09 15:08:12 +00003030
Douglas Gregor91f84212008-12-11 16:49:14 +00003031 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003032 FieldDecl *Field = FieldDecl::Create(*this,
3033 ObjCFastEnumerationStateTypeDecl,
3034 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003035 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003036 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003037 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003038 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003039 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003040 }
Fariborz Jahanian9ea58392010-05-27 16:05:06 +00003041 if (getLangOptions().CPlusPlus)
Fariborz Jahanianc77f0f32010-05-27 16:35:00 +00003042 if (CXXRecordDecl *CXXRD =
3043 dyn_cast<CXXRecordDecl>(ObjCFastEnumerationStateTypeDecl))
Fariborz Jahanian9ea58392010-05-27 16:05:06 +00003044 CXXRD->setEmpty(false);
Mike Stump11289f42009-09-09 15:08:12 +00003045
Douglas Gregord5058122010-02-11 01:19:42 +00003046 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003047 }
Mike Stump11289f42009-09-09 15:08:12 +00003048
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003049 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3050}
3051
Mike Stumpd0153282009-10-20 02:12:22 +00003052QualType ASTContext::getBlockDescriptorType() {
3053 if (BlockDescriptorType)
3054 return getTagDeclType(BlockDescriptorType);
3055
3056 RecordDecl *T;
3057 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003058 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003059 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003060 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003061
3062 QualType FieldTypes[] = {
3063 UnsignedLongTy,
3064 UnsignedLongTy,
3065 };
3066
3067 const char *FieldNames[] = {
3068 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003069 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003070 };
3071
3072 for (size_t i = 0; i < 2; ++i) {
3073 FieldDecl *Field = FieldDecl::Create(*this,
3074 T,
3075 SourceLocation(),
3076 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003077 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003078 /*BitWidth=*/0,
3079 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003080 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003081 T->addDecl(Field);
3082 }
3083
Douglas Gregord5058122010-02-11 01:19:42 +00003084 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003085
3086 BlockDescriptorType = T;
3087
3088 return getTagDeclType(BlockDescriptorType);
3089}
3090
3091void ASTContext::setBlockDescriptorType(QualType T) {
3092 const RecordType *Rec = T->getAs<RecordType>();
3093 assert(Rec && "Invalid BlockDescriptorType");
3094 BlockDescriptorType = Rec->getDecl();
3095}
3096
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003097QualType ASTContext::getBlockDescriptorExtendedType() {
3098 if (BlockDescriptorExtendedType)
3099 return getTagDeclType(BlockDescriptorExtendedType);
3100
3101 RecordDecl *T;
3102 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003103 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003104 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003105 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003106
3107 QualType FieldTypes[] = {
3108 UnsignedLongTy,
3109 UnsignedLongTy,
3110 getPointerType(VoidPtrTy),
3111 getPointerType(VoidPtrTy)
3112 };
3113
3114 const char *FieldNames[] = {
3115 "reserved",
3116 "Size",
3117 "CopyFuncPtr",
3118 "DestroyFuncPtr"
3119 };
3120
3121 for (size_t i = 0; i < 4; ++i) {
3122 FieldDecl *Field = FieldDecl::Create(*this,
3123 T,
3124 SourceLocation(),
3125 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003126 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003127 /*BitWidth=*/0,
3128 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003129 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003130 T->addDecl(Field);
3131 }
3132
Douglas Gregord5058122010-02-11 01:19:42 +00003133 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003134
3135 BlockDescriptorExtendedType = T;
3136
3137 return getTagDeclType(BlockDescriptorExtendedType);
3138}
3139
3140void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3141 const RecordType *Rec = T->getAs<RecordType>();
3142 assert(Rec && "Invalid BlockDescriptorType");
3143 BlockDescriptorExtendedType = Rec->getDecl();
3144}
3145
Mike Stump94967902009-10-21 18:16:27 +00003146bool ASTContext::BlockRequiresCopying(QualType Ty) {
3147 if (Ty->isBlockPointerType())
3148 return true;
3149 if (isObjCNSObjectType(Ty))
3150 return true;
3151 if (Ty->isObjCObjectPointerType())
3152 return true;
3153 return false;
3154}
3155
3156QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3157 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003158 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003159 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003160 // unsigned int __flags;
3161 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003162 // void *__copy_helper; // as needed
3163 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003164 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003165 // } *
3166
Mike Stump94967902009-10-21 18:16:27 +00003167 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3168
3169 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003170 llvm::SmallString<36> Name;
3171 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3172 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003173 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003174 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003175 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003176 T->startDefinition();
3177 QualType Int32Ty = IntTy;
3178 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3179 QualType FieldTypes[] = {
3180 getPointerType(VoidPtrTy),
3181 getPointerType(getTagDeclType(T)),
3182 Int32Ty,
3183 Int32Ty,
3184 getPointerType(VoidPtrTy),
3185 getPointerType(VoidPtrTy),
3186 Ty
3187 };
3188
3189 const char *FieldNames[] = {
3190 "__isa",
3191 "__forwarding",
3192 "__flags",
3193 "__size",
3194 "__copy_helper",
3195 "__destroy_helper",
3196 DeclName,
3197 };
3198
3199 for (size_t i = 0; i < 7; ++i) {
3200 if (!HasCopyAndDispose && i >=4 && i <= 5)
3201 continue;
3202 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3203 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003204 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003205 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003206 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003207 T->addDecl(Field);
3208 }
3209
Douglas Gregord5058122010-02-11 01:19:42 +00003210 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003211
3212 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003213}
3214
3215
3216QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003217 bool BlockHasCopyDispose,
John McCall87fe5d52010-05-20 01:18:31 +00003218 llvm::SmallVectorImpl<const Expr *> &Layout) {
3219
Mike Stumpd0153282009-10-20 02:12:22 +00003220 // FIXME: Move up
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003221 llvm::SmallString<36> Name;
3222 llvm::raw_svector_ostream(Name) << "__block_literal_"
3223 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003224 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003225 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003226 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003227 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003228 QualType FieldTypes[] = {
3229 getPointerType(VoidPtrTy),
3230 IntTy,
3231 IntTy,
3232 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003233 (BlockHasCopyDispose ?
3234 getPointerType(getBlockDescriptorExtendedType()) :
3235 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003236 };
3237
3238 const char *FieldNames[] = {
3239 "__isa",
3240 "__flags",
3241 "__reserved",
3242 "__FuncPtr",
3243 "__descriptor"
3244 };
3245
3246 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003247 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003248 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003249 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003250 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003251 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003252 T->addDecl(Field);
3253 }
3254
John McCall87fe5d52010-05-20 01:18:31 +00003255 for (unsigned i = 0; i < Layout.size(); ++i) {
3256 const Expr *E = Layout[i];
Mike Stump7fe9cc12009-10-21 03:49:08 +00003257
John McCall87fe5d52010-05-20 01:18:31 +00003258 QualType FieldType = E->getType();
3259 IdentifierInfo *FieldName = 0;
3260 if (isa<CXXThisExpr>(E)) {
3261 FieldName = &Idents.get("this");
3262 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3263 const ValueDecl *D = BDRE->getDecl();
3264 FieldName = D->getIdentifier();
3265 if (BDRE->isByRef())
3266 FieldType = BuildByRefType(D->getNameAsCString(), FieldType);
3267 } else {
3268 // Padding.
3269 assert(isa<ConstantArrayType>(FieldType) &&
3270 isa<DeclRefExpr>(E) &&
3271 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3272 "doesn't match characteristics of padding decl");
3273 }
Mike Stump7fe9cc12009-10-21 03:49:08 +00003274
3275 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCall87fe5d52010-05-20 01:18:31 +00003276 FieldName, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003277 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003278 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003279 T->addDecl(Field);
3280 }
3281
Douglas Gregord5058122010-02-11 01:19:42 +00003282 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003283
3284 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003285}
3286
Douglas Gregor512b0772009-04-23 22:29:11 +00003287void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003288 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003289 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3290 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3291}
3292
Anders Carlsson18acd442007-10-29 06:33:42 +00003293// This returns true if a type has been typedefed to BOOL:
3294// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003295static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003296 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003297 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3298 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003299
Anders Carlssond8499822007-10-29 05:01:08 +00003300 return false;
3301}
3302
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003303/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003304/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003305CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003306 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003307
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003308 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003309 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003310 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003311 // Treat arrays as pointers, since that's how they're passed in.
3312 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003313 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003314 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003315}
3316
3317static inline
3318std::string charUnitsToString(const CharUnits &CU) {
3319 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003320}
3321
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003322/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003323/// declaration.
3324void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3325 std::string& S) {
3326 const BlockDecl *Decl = Expr->getBlockDecl();
3327 QualType BlockTy =
3328 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3329 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003330 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003331 // Compute size of all parameters.
3332 // Start with computing size of a pointer in number of bytes.
3333 // FIXME: There might(should) be a better way of doing this computation!
3334 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003335 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3336 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003337 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003338 E = Decl->param_end(); PI != E; ++PI) {
3339 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003340 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003341 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003342 ParmOffset += sz;
3343 }
3344 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003345 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003346 // Block pointer and offset.
3347 S += "@?0";
3348 ParmOffset = PtrSize;
3349
3350 // Argument types.
3351 ParmOffset = PtrSize;
3352 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3353 Decl->param_end(); PI != E; ++PI) {
3354 ParmVarDecl *PVDecl = *PI;
3355 QualType PType = PVDecl->getOriginalType();
3356 if (const ArrayType *AT =
3357 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3358 // Use array's original type only if it has known number of
3359 // elements.
3360 if (!isa<ConstantArrayType>(AT))
3361 PType = PVDecl->getType();
3362 } else if (PType->isFunctionType())
3363 PType = PVDecl->getType();
3364 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003365 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003366 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003367 }
3368}
3369
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003370/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003371/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003372void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003373 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003374 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003375 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003376 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003377 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003378 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003379 // Compute size of all parameters.
3380 // Start with computing size of a pointer in number of bytes.
3381 // FIXME: There might(should) be a better way of doing this computation!
3382 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003383 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003384 // The first two arguments (self and _cmd) are pointers; account for
3385 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003386 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003387 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003388 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003389 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003390 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003391 assert (sz.isPositive() &&
3392 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003393 ParmOffset += sz;
3394 }
Ken Dyck40775002010-01-11 17:06:35 +00003395 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003396 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003397 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003398
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003399 // Argument types.
3400 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003401 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003402 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003403 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003404 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003405 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003406 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3407 // Use array's original type only if it has known number of
3408 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003409 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003410 PType = PVDecl->getType();
3411 } else if (PType->isFunctionType())
3412 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003413 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003414 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003415 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003416 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003417 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003418 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003419 }
3420}
3421
Daniel Dunbar4932b362008-08-28 04:38:10 +00003422/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003423/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003424/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3425/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003426/// Property attributes are stored as a comma-delimited C string. The simple
3427/// attributes readonly and bycopy are encoded as single characters. The
3428/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3429/// encoded as single characters, followed by an identifier. Property types
3430/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003431/// these attributes are defined by the following enumeration:
3432/// @code
3433/// enum PropertyAttributes {
3434/// kPropertyReadOnly = 'R', // property is read-only.
3435/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3436/// kPropertyByref = '&', // property is a reference to the value last assigned
3437/// kPropertyDynamic = 'D', // property is dynamic
3438/// kPropertyGetter = 'G', // followed by getter selector name
3439/// kPropertySetter = 'S', // followed by setter selector name
3440/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3441/// kPropertyType = 't' // followed by old-style type encoding.
3442/// kPropertyWeak = 'W' // 'weak' property
3443/// kPropertyStrong = 'P' // property GC'able
3444/// kPropertyNonAtomic = 'N' // property non-atomic
3445/// };
3446/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003447void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003448 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003449 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003450 // Collect information from the property implementation decl(s).
3451 bool Dynamic = false;
3452 ObjCPropertyImplDecl *SynthesizePID = 0;
3453
3454 // FIXME: Duplicated code due to poor abstraction.
3455 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003456 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003457 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3458 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003459 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003460 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003461 ObjCPropertyImplDecl *PID = *i;
3462 if (PID->getPropertyDecl() == PD) {
3463 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3464 Dynamic = true;
3465 } else {
3466 SynthesizePID = PID;
3467 }
3468 }
3469 }
3470 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003471 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003472 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003473 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003474 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003475 ObjCPropertyImplDecl *PID = *i;
3476 if (PID->getPropertyDecl() == PD) {
3477 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3478 Dynamic = true;
3479 } else {
3480 SynthesizePID = PID;
3481 }
3482 }
Mike Stump11289f42009-09-09 15:08:12 +00003483 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003484 }
3485 }
3486
3487 // FIXME: This is not very efficient.
3488 S = "T";
3489
3490 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003491 // GCC has some special rules regarding encoding of properties which
3492 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003493 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003494 true /* outermost type */,
3495 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003496
3497 if (PD->isReadOnly()) {
3498 S += ",R";
3499 } else {
3500 switch (PD->getSetterKind()) {
3501 case ObjCPropertyDecl::Assign: break;
3502 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003503 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003504 }
3505 }
3506
3507 // It really isn't clear at all what this means, since properties
3508 // are "dynamic by default".
3509 if (Dynamic)
3510 S += ",D";
3511
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003512 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3513 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003514
Daniel Dunbar4932b362008-08-28 04:38:10 +00003515 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3516 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003517 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003518 }
3519
3520 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3521 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003522 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003523 }
3524
3525 if (SynthesizePID) {
3526 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3527 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003528 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003529 }
3530
3531 // FIXME: OBJCGC: weak & strong
3532}
3533
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003534/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003535/// Another legacy compatibility encoding: 32-bit longs are encoded as
3536/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003537/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3538///
3539void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003540 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003541 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003542 if (BT->getKind() == BuiltinType::ULong &&
3543 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003544 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003545 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003546 if (BT->getKind() == BuiltinType::Long &&
3547 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003548 PointeeTy = IntTy;
3549 }
3550 }
3551}
3552
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003553void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003554 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003555 // We follow the behavior of gcc, expanding structures which are
3556 // directly pointed to, and expanding embedded structures. Note that
3557 // these rules are sufficient to prevent recursive encoding of the
3558 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003559 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003560 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003561}
3562
David Chisnallb190a2c2010-06-04 01:10:52 +00003563static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3564 switch (T->getAs<BuiltinType>()->getKind()) {
3565 default: assert(0 && "Unhandled builtin type kind");
3566 case BuiltinType::Void: return 'v';
3567 case BuiltinType::Bool: return 'B';
3568 case BuiltinType::Char_U:
3569 case BuiltinType::UChar: return 'C';
3570 case BuiltinType::UShort: return 'S';
3571 case BuiltinType::UInt: return 'I';
3572 case BuiltinType::ULong:
3573 return
3574 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3575 case BuiltinType::UInt128: return 'T';
3576 case BuiltinType::ULongLong: return 'Q';
3577 case BuiltinType::Char_S:
3578 case BuiltinType::SChar: return 'c';
3579 case BuiltinType::Short: return 's';
John McCalldad856d2010-06-11 10:11:05 +00003580 case BuiltinType::WChar:
David Chisnallb190a2c2010-06-04 01:10:52 +00003581 case BuiltinType::Int: return 'i';
3582 case BuiltinType::Long:
3583 return
3584 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3585 case BuiltinType::LongLong: return 'q';
3586 case BuiltinType::Int128: return 't';
3587 case BuiltinType::Float: return 'f';
3588 case BuiltinType::Double: return 'd';
3589 case BuiltinType::LongDouble: return 'd';
3590 }
3591}
3592
Mike Stump11289f42009-09-09 15:08:12 +00003593static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00003594 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003595 const Expr *E = FD->getBitWidth();
3596 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3597 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003598 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00003599 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3600 // The GNU runtime requires more information; bitfields are encoded as b,
3601 // then the offset (in bits) of the first element, then the type of the
3602 // bitfield, then the size in bits. For example, in this structure:
3603 //
3604 // struct
3605 // {
3606 // int integer;
3607 // int flags:2;
3608 // };
3609 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3610 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3611 // information is not especially sensible, but we're stuck with it for
3612 // compatibility with GCC, although providing it breaks anything that
3613 // actually uses runtime introspection and wants to work on both runtimes...
3614 if (!Ctx->getLangOptions().NeXTRuntime) {
3615 const RecordDecl *RD = FD->getParent();
3616 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3617 // FIXME: This same linear search is also used in ExprConstant - it might
3618 // be better if the FieldDecl stored its offset. We'd be increasing the
3619 // size of the object slightly, but saving some time every time it is used.
3620 unsigned i = 0;
3621 for (RecordDecl::field_iterator Field = RD->field_begin(),
3622 FieldEnd = RD->field_end();
3623 Field != FieldEnd; (void)++Field, ++i) {
3624 if (*Field == FD)
3625 break;
3626 }
3627 S += llvm::utostr(RL.getFieldOffset(i));
3628 S += ObjCEncodingForPrimitiveKind(Context, T);
3629 }
3630 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003631 S += llvm::utostr(N);
3632}
3633
Daniel Dunbar07d07852009-10-18 21:17:35 +00003634// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003635void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3636 bool ExpandPointedToStructures,
3637 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003638 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003639 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003640 bool EncodingProperty) {
David Chisnallb190a2c2010-06-04 01:10:52 +00003641 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003642 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003643 return EncodeBitField(this, S, T, FD);
3644 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003645 return;
3646 }
Mike Stump11289f42009-09-09 15:08:12 +00003647
John McCall9dd450b2009-09-21 23:43:11 +00003648 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003649 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003650 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003651 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003652 return;
3653 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003654
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003655 // encoding for pointer or r3eference types.
3656 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003657 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003658 if (PT->isObjCSelType()) {
3659 S += ':';
3660 return;
3661 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003662 PointeeTy = PT->getPointeeType();
3663 }
3664 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3665 PointeeTy = RT->getPointeeType();
3666 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003667 bool isReadOnly = false;
3668 // For historical/compatibility reasons, the read-only qualifier of the
3669 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3670 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003671 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003672 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003673 if (OutermostType && T.isConstQualified()) {
3674 isReadOnly = true;
3675 S += 'r';
3676 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003677 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003678 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003679 while (P->getAs<PointerType>())
3680 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003681 if (P.isConstQualified()) {
3682 isReadOnly = true;
3683 S += 'r';
3684 }
3685 }
3686 if (isReadOnly) {
3687 // Another legacy compatibility encoding. Some ObjC qualifier and type
3688 // combinations need to be rearranged.
3689 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003690 if (llvm::StringRef(S).endswith("nr"))
3691 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003692 }
Mike Stump11289f42009-09-09 15:08:12 +00003693
Anders Carlssond8499822007-10-29 05:01:08 +00003694 if (PointeeTy->isCharType()) {
3695 // char pointer types should be encoded as '*' unless it is a
3696 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003697 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003698 S += '*';
3699 return;
3700 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003701 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003702 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3703 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3704 S += '#';
3705 return;
3706 }
3707 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3708 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3709 S += '@';
3710 return;
3711 }
3712 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003713 }
Anders Carlssond8499822007-10-29 05:01:08 +00003714 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003715 getLegacyIntegralTypeEncoding(PointeeTy);
3716
Mike Stump11289f42009-09-09 15:08:12 +00003717 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003718 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003719 return;
3720 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003721
Chris Lattnere7cabb92009-07-13 00:10:46 +00003722 if (const ArrayType *AT =
3723 // Ignore type qualifiers etc.
3724 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003725 if (isa<IncompleteArrayType>(AT)) {
3726 // Incomplete arrays are encoded as a pointer to the array element.
3727 S += '^';
3728
Mike Stump11289f42009-09-09 15:08:12 +00003729 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003730 false, ExpandStructures, FD);
3731 } else {
3732 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003733
Anders Carlssond05f44b2009-02-22 01:38:57 +00003734 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3735 S += llvm::utostr(CAT->getSize().getZExtValue());
3736 else {
3737 //Variable length arrays are encoded as a regular array with 0 elements.
3738 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3739 S += '0';
3740 }
Mike Stump11289f42009-09-09 15:08:12 +00003741
3742 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003743 false, ExpandStructures, FD);
3744 S += ']';
3745 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003746 return;
3747 }
Mike Stump11289f42009-09-09 15:08:12 +00003748
John McCall9dd450b2009-09-21 23:43:11 +00003749 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003750 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003751 return;
3752 }
Mike Stump11289f42009-09-09 15:08:12 +00003753
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003754 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003755 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003756 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003757 // Anonymous structures print as '?'
3758 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3759 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003760 if (ClassTemplateSpecializationDecl *Spec
3761 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3762 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3763 std::string TemplateArgsStr
3764 = TemplateSpecializationType::PrintTemplateArgumentList(
3765 TemplateArgs.getFlatArgumentList(),
3766 TemplateArgs.flat_size(),
3767 (*this).PrintingPolicy);
3768
3769 S += TemplateArgsStr;
3770 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003771 } else {
3772 S += '?';
3773 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003774 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003775 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003776 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3777 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003778 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003779 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003780 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003781 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003782 S += '"';
3783 }
Mike Stump11289f42009-09-09 15:08:12 +00003784
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003785 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003786 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003787 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003788 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003789 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003790 QualType qt = Field->getType();
3791 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003792 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003793 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003794 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003795 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003796 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003797 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003798 return;
3799 }
Mike Stump11289f42009-09-09 15:08:12 +00003800
Chris Lattnere7cabb92009-07-13 00:10:46 +00003801 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003802 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003803 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003804 else
3805 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003806 return;
3807 }
Mike Stump11289f42009-09-09 15:08:12 +00003808
Chris Lattnere7cabb92009-07-13 00:10:46 +00003809 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003810 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003811 return;
3812 }
Mike Stump11289f42009-09-09 15:08:12 +00003813
John McCall8b07ec22010-05-15 11:32:37 +00003814 // Ignore protocol qualifiers when mangling at this level.
3815 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3816 T = OT->getBaseType();
3817
John McCall8ccfcb52009-09-24 19:53:00 +00003818 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003819 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003820 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003821 S += '{';
3822 const IdentifierInfo *II = OI->getIdentifier();
3823 S += II->getName();
3824 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003825 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003826 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003827 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003828 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003829 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003830 RecFields[i]);
3831 else
Mike Stump11289f42009-09-09 15:08:12 +00003832 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003833 FD);
3834 }
3835 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003836 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003837 }
Mike Stump11289f42009-09-09 15:08:12 +00003838
John McCall9dd450b2009-09-21 23:43:11 +00003839 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003840 if (OPT->isObjCIdType()) {
3841 S += '@';
3842 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003843 }
Mike Stump11289f42009-09-09 15:08:12 +00003844
Steve Narofff0c86112009-10-28 22:03:49 +00003845 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3846 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3847 // Since this is a binary compatibility issue, need to consult with runtime
3848 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003849 S += '#';
3850 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003851 }
Mike Stump11289f42009-09-09 15:08:12 +00003852
Chris Lattnere7cabb92009-07-13 00:10:46 +00003853 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003854 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003855 ExpandPointedToStructures,
3856 ExpandStructures, FD);
3857 if (FD || EncodingProperty) {
3858 // Note that we do extended encoding of protocol qualifer list
3859 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003860 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003861 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3862 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003863 S += '<';
3864 S += (*I)->getNameAsString();
3865 S += '>';
3866 }
3867 S += '"';
3868 }
3869 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003870 }
Mike Stump11289f42009-09-09 15:08:12 +00003871
Chris Lattnere7cabb92009-07-13 00:10:46 +00003872 QualType PointeeTy = OPT->getPointeeType();
3873 if (!EncodingProperty &&
3874 isa<TypedefType>(PointeeTy.getTypePtr())) {
3875 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003876 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003877 // {...};
3878 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003879 getObjCEncodingForTypeImpl(PointeeTy, S,
3880 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003881 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003882 return;
3883 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003884
3885 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003886 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003887 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003888 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003889 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3890 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003891 S += '<';
3892 S += (*I)->getNameAsString();
3893 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003894 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003895 S += '"';
3896 }
3897 return;
3898 }
Mike Stump11289f42009-09-09 15:08:12 +00003899
John McCalla9e6e8d2010-05-17 23:56:34 +00003900 // gcc just blithely ignores member pointers.
3901 // TODO: maybe there should be a mangling for these
3902 if (T->getAs<MemberPointerType>())
3903 return;
3904
Chris Lattnere7cabb92009-07-13 00:10:46 +00003905 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003906}
3907
Mike Stump11289f42009-09-09 15:08:12 +00003908void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003909 std::string& S) const {
3910 if (QT & Decl::OBJC_TQ_In)
3911 S += 'n';
3912 if (QT & Decl::OBJC_TQ_Inout)
3913 S += 'N';
3914 if (QT & Decl::OBJC_TQ_Out)
3915 S += 'o';
3916 if (QT & Decl::OBJC_TQ_Bycopy)
3917 S += 'O';
3918 if (QT & Decl::OBJC_TQ_Byref)
3919 S += 'R';
3920 if (QT & Decl::OBJC_TQ_Oneway)
3921 S += 'V';
3922}
3923
Chris Lattnere7cabb92009-07-13 00:10:46 +00003924void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003925 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003926
Anders Carlsson87c149b2007-10-11 01:00:40 +00003927 BuiltinVaListType = T;
3928}
3929
Chris Lattnere7cabb92009-07-13 00:10:46 +00003930void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003931 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003932}
3933
Chris Lattnere7cabb92009-07-13 00:10:46 +00003934void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003935 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003936}
3937
Chris Lattnere7cabb92009-07-13 00:10:46 +00003938void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003939 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003940}
3941
Chris Lattnere7cabb92009-07-13 00:10:46 +00003942void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003943 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003944}
3945
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003946void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003947 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003948 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003949
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003950 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003951}
3952
John McCalld28ae272009-12-02 08:04:21 +00003953/// \brief Retrieve the template name that corresponds to a non-empty
3954/// lookup.
John McCallad371252010-01-20 00:46:10 +00003955TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3956 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003957 unsigned size = End - Begin;
3958 assert(size > 1 && "set is not overloaded!");
3959
3960 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3961 size * sizeof(FunctionTemplateDecl*));
3962 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3963
3964 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003965 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003966 NamedDecl *D = *I;
3967 assert(isa<FunctionTemplateDecl>(D) ||
3968 (isa<UsingShadowDecl>(D) &&
3969 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3970 *Storage++ = D;
3971 }
3972
3973 return TemplateName(OT);
3974}
3975
Douglas Gregordc572a32009-03-30 22:58:21 +00003976/// \brief Retrieve the template name that represents a qualified
3977/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003978TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003979 bool TemplateKeyword,
3980 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003981 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003982 llvm::FoldingSetNodeID ID;
3983 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3984
3985 void *InsertPos = 0;
3986 QualifiedTemplateName *QTN =
3987 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3988 if (!QTN) {
3989 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3990 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3991 }
3992
3993 return TemplateName(QTN);
3994}
3995
3996/// \brief Retrieve the template name that represents a dependent
3997/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003998TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003999 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00004000 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00004001 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00004002
4003 llvm::FoldingSetNodeID ID;
4004 DependentTemplateName::Profile(ID, NNS, Name);
4005
4006 void *InsertPos = 0;
4007 DependentTemplateName *QTN =
4008 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4009
4010 if (QTN)
4011 return TemplateName(QTN);
4012
4013 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4014 if (CanonNNS == NNS) {
4015 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4016 } else {
4017 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4018 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004019 DependentTemplateName *CheckQTN =
4020 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4021 assert(!CheckQTN && "Dependent type name canonicalization broken");
4022 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004023 }
4024
4025 DependentTemplateNames.InsertNode(QTN, InsertPos);
4026 return TemplateName(QTN);
4027}
4028
Douglas Gregor71395fa2009-11-04 00:56:37 +00004029/// \brief Retrieve the template name that represents a dependent
4030/// template name such as \c MetaFun::template operator+.
4031TemplateName
4032ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4033 OverloadedOperatorKind Operator) {
4034 assert((!NNS || NNS->isDependent()) &&
4035 "Nested name specifier must be dependent");
4036
4037 llvm::FoldingSetNodeID ID;
4038 DependentTemplateName::Profile(ID, NNS, Operator);
4039
4040 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004041 DependentTemplateName *QTN
4042 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004043
4044 if (QTN)
4045 return TemplateName(QTN);
4046
4047 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4048 if (CanonNNS == NNS) {
4049 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4050 } else {
4051 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4052 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004053
4054 DependentTemplateName *CheckQTN
4055 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4056 assert(!CheckQTN && "Dependent template name canonicalization broken");
4057 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004058 }
4059
4060 DependentTemplateNames.InsertNode(QTN, InsertPos);
4061 return TemplateName(QTN);
4062}
4063
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004064/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004065/// TargetInfo, produce the corresponding type. The unsigned @p Type
4066/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004067CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004068 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004069 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004070 case TargetInfo::SignedShort: return ShortTy;
4071 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4072 case TargetInfo::SignedInt: return IntTy;
4073 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4074 case TargetInfo::SignedLong: return LongTy;
4075 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4076 case TargetInfo::SignedLongLong: return LongLongTy;
4077 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4078 }
4079
4080 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004081 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004082}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004083
4084//===----------------------------------------------------------------------===//
4085// Type Predicates.
4086//===----------------------------------------------------------------------===//
4087
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004088/// isObjCNSObjectType - Return true if this is an NSObject object using
4089/// NSObject attribute on a c-style pointer type.
4090/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004091/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004092///
4093bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4094 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4095 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004096 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004097 return true;
4098 }
Mike Stump11289f42009-09-09 15:08:12 +00004099 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004100}
4101
Fariborz Jahanian96207692009-02-18 21:49:28 +00004102/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4103/// garbage collection attribute.
4104///
John McCall8ccfcb52009-09-24 19:53:00 +00004105Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4106 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004107 if (getLangOptions().ObjC1 &&
4108 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00004109 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00004110 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00004111 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004112 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00004113 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00004114 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004115 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004116 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004117 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004118 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00004119 // Non-pointers have none gc'able attribute regardless of the attribute
4120 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00004121 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004122 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004123 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004124 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004125}
4126
Chris Lattner49af6a42008-04-07 06:51:04 +00004127//===----------------------------------------------------------------------===//
4128// Type Compatibility Testing
4129//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004130
Mike Stump11289f42009-09-09 15:08:12 +00004131/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004132/// compatible.
4133static bool areCompatVectorTypes(const VectorType *LHS,
4134 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004135 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004136 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004137 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004138}
4139
Steve Naroff8e6aee52009-07-23 01:01:38 +00004140//===----------------------------------------------------------------------===//
4141// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4142//===----------------------------------------------------------------------===//
4143
4144/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4145/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004146bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4147 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004148 if (lProto == rProto)
4149 return true;
4150 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4151 E = rProto->protocol_end(); PI != E; ++PI)
4152 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4153 return true;
4154 return false;
4155}
4156
Steve Naroff8e6aee52009-07-23 01:01:38 +00004157/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4158/// return true if lhs's protocols conform to rhs's protocol; false
4159/// otherwise.
4160bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4161 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4162 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4163 return false;
4164}
4165
4166/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4167/// ObjCQualifiedIDType.
4168bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4169 bool compare) {
4170 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004171 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004172 lhs->isObjCIdType() || lhs->isObjCClassType())
4173 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004174 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004175 rhs->isObjCIdType() || rhs->isObjCClassType())
4176 return true;
4177
4178 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004179 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004180
Steve Naroff8e6aee52009-07-23 01:01:38 +00004181 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004182
Steve Naroff8e6aee52009-07-23 01:01:38 +00004183 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004184 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004185 // make sure we check the class hierarchy.
4186 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4187 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4188 E = lhsQID->qual_end(); I != E; ++I) {
4189 // when comparing an id<P> on lhs with a static type on rhs,
4190 // see if static class implements all of id's protocols, directly or
4191 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004192 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004193 return false;
4194 }
4195 }
4196 // If there are no qualifiers and no interface, we have an 'id'.
4197 return true;
4198 }
Mike Stump11289f42009-09-09 15:08:12 +00004199 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004200 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4201 E = lhsQID->qual_end(); I != E; ++I) {
4202 ObjCProtocolDecl *lhsProto = *I;
4203 bool match = false;
4204
4205 // when comparing an id<P> on lhs with a static type on rhs,
4206 // see if static class implements all of id's protocols, directly or
4207 // through its super class and categories.
4208 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4209 E = rhsOPT->qual_end(); J != E; ++J) {
4210 ObjCProtocolDecl *rhsProto = *J;
4211 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4212 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4213 match = true;
4214 break;
4215 }
4216 }
Mike Stump11289f42009-09-09 15:08:12 +00004217 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004218 // make sure we check the class hierarchy.
4219 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4220 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4221 E = lhsQID->qual_end(); I != E; ++I) {
4222 // when comparing an id<P> on lhs with a static type on rhs,
4223 // see if static class implements all of id's protocols, directly or
4224 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004225 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004226 match = true;
4227 break;
4228 }
4229 }
4230 }
4231 if (!match)
4232 return false;
4233 }
Mike Stump11289f42009-09-09 15:08:12 +00004234
Steve Naroff8e6aee52009-07-23 01:01:38 +00004235 return true;
4236 }
Mike Stump11289f42009-09-09 15:08:12 +00004237
Steve Naroff8e6aee52009-07-23 01:01:38 +00004238 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4239 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4240
Mike Stump11289f42009-09-09 15:08:12 +00004241 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004242 lhs->getAsObjCInterfacePointerType()) {
4243 if (lhsOPT->qual_empty()) {
4244 bool match = false;
4245 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4246 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4247 E = rhsQID->qual_end(); I != E; ++I) {
4248 // when comparing an id<P> on lhs with a static type on rhs,
4249 // see if static class implements all of id's protocols, directly or
4250 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004251 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004252 match = true;
4253 break;
4254 }
4255 }
4256 if (!match)
4257 return false;
4258 }
4259 return true;
4260 }
Mike Stump11289f42009-09-09 15:08:12 +00004261 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004262 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4263 E = lhsOPT->qual_end(); I != E; ++I) {
4264 ObjCProtocolDecl *lhsProto = *I;
4265 bool match = false;
4266
4267 // when comparing an id<P> on lhs with a static type on rhs,
4268 // see if static class implements all of id's protocols, directly or
4269 // through its super class and categories.
4270 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4271 E = rhsQID->qual_end(); J != E; ++J) {
4272 ObjCProtocolDecl *rhsProto = *J;
4273 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4274 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4275 match = true;
4276 break;
4277 }
4278 }
4279 if (!match)
4280 return false;
4281 }
4282 return true;
4283 }
4284 return false;
4285}
4286
Eli Friedman47f77112008-08-22 00:56:42 +00004287/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004288/// compatible for assignment from RHS to LHS. This handles validation of any
4289/// protocol qualifiers on the LHS or RHS.
4290///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004291bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4292 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004293 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4294 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4295
Steve Naroff1329fa02009-07-15 18:40:39 +00004296 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004297 if (LHS->isObjCUnqualifiedIdOrClass() ||
4298 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004299 return true;
4300
John McCall8b07ec22010-05-15 11:32:37 +00004301 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004302 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4303 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004304 false);
4305
John McCall8b07ec22010-05-15 11:32:37 +00004306 // If we have 2 user-defined types, fall into that path.
4307 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004308 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004309
Steve Naroff8e6aee52009-07-23 01:01:38 +00004310 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004311}
4312
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004313/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4314/// for providing type-safty for objective-c pointers used to pass/return
4315/// arguments in block literals. When passed as arguments, passing 'A*' where
4316/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4317/// not OK. For the return type, the opposite is not OK.
4318bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4319 const ObjCObjectPointerType *LHSOPT,
4320 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004321 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004322 return true;
4323
4324 if (LHSOPT->isObjCBuiltinType()) {
4325 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4326 }
4327
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004328 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004329 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4330 QualType(RHSOPT,0),
4331 false);
4332
4333 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4334 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4335 if (LHS && RHS) { // We have 2 user-defined types.
4336 if (LHS != RHS) {
4337 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4338 return false;
4339 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4340 return true;
4341 }
4342 else
4343 return true;
4344 }
4345 return false;
4346}
4347
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004348/// getIntersectionOfProtocols - This routine finds the intersection of set
4349/// of protocols inherited from two distinct objective-c pointer objects.
4350/// It is used to build composite qualifier list of the composite type of
4351/// the conditional expression involving two objective-c pointer objects.
4352static
4353void getIntersectionOfProtocols(ASTContext &Context,
4354 const ObjCObjectPointerType *LHSOPT,
4355 const ObjCObjectPointerType *RHSOPT,
4356 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4357
John McCall8b07ec22010-05-15 11:32:37 +00004358 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4359 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4360 assert(LHS->getInterface() && "LHS must have an interface base");
4361 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004362
4363 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4364 unsigned LHSNumProtocols = LHS->getNumProtocols();
4365 if (LHSNumProtocols > 0)
4366 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4367 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004368 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004369 Context.CollectInheritedProtocols(LHS->getInterface(),
4370 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004371 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4372 LHSInheritedProtocols.end());
4373 }
4374
4375 unsigned RHSNumProtocols = RHS->getNumProtocols();
4376 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004377 ObjCProtocolDecl **RHSProtocols =
4378 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004379 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4380 if (InheritedProtocolSet.count(RHSProtocols[i]))
4381 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4382 }
4383 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004384 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004385 Context.CollectInheritedProtocols(RHS->getInterface(),
4386 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004387 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4388 RHSInheritedProtocols.begin(),
4389 E = RHSInheritedProtocols.end(); I != E; ++I)
4390 if (InheritedProtocolSet.count((*I)))
4391 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004392 }
4393}
4394
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004395/// areCommonBaseCompatible - Returns common base class of the two classes if
4396/// one found. Note that this is O'2 algorithm. But it will be called as the
4397/// last type comparison in a ?-exp of ObjC pointer types before a
4398/// warning is issued. So, its invokation is extremely rare.
4399QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004400 const ObjCObjectPointerType *Lptr,
4401 const ObjCObjectPointerType *Rptr) {
4402 const ObjCObjectType *LHS = Lptr->getObjectType();
4403 const ObjCObjectType *RHS = Rptr->getObjectType();
4404 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4405 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4406 if (!LDecl || !RDecl)
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004407 return QualType();
4408
John McCall8b07ec22010-05-15 11:32:37 +00004409 while ((LDecl = LDecl->getSuperClass())) {
4410 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004411 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004412 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4413 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4414
4415 QualType Result = QualType(LHS, 0);
4416 if (!Protocols.empty())
4417 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4418 Result = getObjCObjectPointerType(Result);
4419 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004420 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004421 }
4422
4423 return QualType();
4424}
4425
John McCall8b07ec22010-05-15 11:32:37 +00004426bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4427 const ObjCObjectType *RHS) {
4428 assert(LHS->getInterface() && "LHS is not an interface type");
4429 assert(RHS->getInterface() && "RHS is not an interface type");
4430
Chris Lattner49af6a42008-04-07 06:51:04 +00004431 // Verify that the base decls are compatible: the RHS must be a subclass of
4432 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00004433 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00004434 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004435
Chris Lattner49af6a42008-04-07 06:51:04 +00004436 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4437 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004438 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004439 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004440
Chris Lattner49af6a42008-04-07 06:51:04 +00004441 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4442 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004443 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004444 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004445
John McCall8b07ec22010-05-15 11:32:37 +00004446 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4447 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004448 LHSPI != LHSPE; LHSPI++) {
4449 bool RHSImplementsProtocol = false;
4450
4451 // If the RHS doesn't implement the protocol on the left, the types
4452 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00004453 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4454 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004455 RHSPI != RHSPE; RHSPI++) {
4456 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004457 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004458 break;
4459 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004460 }
4461 // FIXME: For better diagnostics, consider passing back the protocol name.
4462 if (!RHSImplementsProtocol)
4463 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004464 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004465 // The RHS implements all protocols listed on the LHS.
4466 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004467}
4468
Steve Naroffb7605152009-02-12 17:52:19 +00004469bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4470 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004471 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4472 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004473
Steve Naroff7cae42b2009-07-10 23:34:53 +00004474 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004475 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004476
4477 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4478 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004479}
4480
Mike Stump11289f42009-09-09 15:08:12 +00004481/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004482/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004483/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004484/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004485bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004486 if (getLangOptions().CPlusPlus)
4487 return hasSameType(LHS, RHS);
4488
Eli Friedman47f77112008-08-22 00:56:42 +00004489 return !mergeTypes(LHS, RHS).isNull();
4490}
4491
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004492bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4493 return !mergeTypes(LHS, RHS, true).isNull();
4494}
4495
4496QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4497 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004498 const FunctionType *lbase = lhs->getAs<FunctionType>();
4499 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004500 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4501 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004502 bool allLTypes = true;
4503 bool allRTypes = true;
4504
4505 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004506 QualType retType;
4507 if (OfBlockPointer)
4508 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4509 else
4510 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004511 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004512 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004513 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004514 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004515 allRTypes = false;
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004516 // FIXME: double check this
4517 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4518 // rbase->getRegParmAttr() != 0 &&
4519 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004520 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4521 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004522 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4523 lbaseInfo.getRegParm();
4524 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4525 if (NoReturn != lbaseInfo.getNoReturn() ||
4526 RegParm != lbaseInfo.getRegParm())
4527 allLTypes = false;
4528 if (NoReturn != rbaseInfo.getNoReturn() ||
4529 RegParm != rbaseInfo.getRegParm())
4530 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004531 CallingConv lcc = lbaseInfo.getCC();
4532 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004533 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004534 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004535 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004536
Eli Friedman47f77112008-08-22 00:56:42 +00004537 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004538 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4539 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004540 unsigned lproto_nargs = lproto->getNumArgs();
4541 unsigned rproto_nargs = rproto->getNumArgs();
4542
4543 // Compatible functions must have the same number of arguments
4544 if (lproto_nargs != rproto_nargs)
4545 return QualType();
4546
4547 // Variadic and non-variadic functions aren't compatible
4548 if (lproto->isVariadic() != rproto->isVariadic())
4549 return QualType();
4550
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004551 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4552 return QualType();
4553
Eli Friedman47f77112008-08-22 00:56:42 +00004554 // Check argument compatibility
4555 llvm::SmallVector<QualType, 10> types;
4556 for (unsigned i = 0; i < lproto_nargs; i++) {
4557 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4558 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004559 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004560 if (argtype.isNull()) return QualType();
4561 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004562 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4563 allLTypes = false;
4564 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4565 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004566 }
4567 if (allLTypes) return lhs;
4568 if (allRTypes) return rhs;
4569 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004570 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004571 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004572 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004573 }
4574
4575 if (lproto) allRTypes = false;
4576 if (rproto) allLTypes = false;
4577
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004578 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004579 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004580 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004581 if (proto->isVariadic()) return QualType();
4582 // Check that the types are compatible with the types that
4583 // would result from default argument promotions (C99 6.7.5.3p15).
4584 // The only types actually affected are promotable integer
4585 // types and floats, which would be passed as a different
4586 // type depending on whether the prototype is visible.
4587 unsigned proto_nargs = proto->getNumArgs();
4588 for (unsigned i = 0; i < proto_nargs; ++i) {
4589 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004590
4591 // Look at the promotion type of enum types, since that is the type used
4592 // to pass enum values.
4593 if (const EnumType *Enum = argTy->getAs<EnumType>())
4594 argTy = Enum->getDecl()->getPromotionType();
4595
Eli Friedman47f77112008-08-22 00:56:42 +00004596 if (argTy->isPromotableIntegerType() ||
4597 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4598 return QualType();
4599 }
4600
4601 if (allLTypes) return lhs;
4602 if (allRTypes) return rhs;
4603 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004604 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004605 proto->getTypeQuals(),
4606 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004607 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004608 }
4609
4610 if (allLTypes) return lhs;
4611 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004612 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004613 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004614}
4615
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004616QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4617 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004618 // C++ [expr]: If an expression initially has the type "reference to T", the
4619 // type is adjusted to "T" prior to any further analysis, the expression
4620 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004621 // expression is an lvalue unless the reference is an rvalue reference and
4622 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004623 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4624 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4625
Eli Friedman47f77112008-08-22 00:56:42 +00004626 QualType LHSCan = getCanonicalType(LHS),
4627 RHSCan = getCanonicalType(RHS);
4628
4629 // If two types are identical, they are compatible.
4630 if (LHSCan == RHSCan)
4631 return LHS;
4632
John McCall8ccfcb52009-09-24 19:53:00 +00004633 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004634 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4635 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004636 if (LQuals != RQuals) {
4637 // If any of these qualifiers are different, we have a type
4638 // mismatch.
4639 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4640 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4641 return QualType();
4642
4643 // Exactly one GC qualifier difference is allowed: __strong is
4644 // okay if the other type has no GC qualifier but is an Objective
4645 // C object pointer (i.e. implicitly strong by default). We fix
4646 // this by pretending that the unqualified type was actually
4647 // qualified __strong.
4648 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4649 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4650 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4651
4652 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4653 return QualType();
4654
4655 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4656 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4657 }
4658 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4659 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4660 }
Eli Friedman47f77112008-08-22 00:56:42 +00004661 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004662 }
4663
4664 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004665
Eli Friedmandcca6332009-06-01 01:22:52 +00004666 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4667 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004668
Chris Lattnerfd652912008-01-14 05:45:46 +00004669 // We want to consider the two function types to be the same for these
4670 // comparisons, just force one to the other.
4671 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4672 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004673
4674 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004675 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4676 LHSClass = Type::ConstantArray;
4677 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4678 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004679
John McCall8b07ec22010-05-15 11:32:37 +00004680 // ObjCInterfaces are just specialized ObjCObjects.
4681 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
4682 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
4683
Nate Begemance4d7fc2008-04-18 23:10:10 +00004684 // Canonicalize ExtVector -> Vector.
4685 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4686 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004687
Chris Lattner95554662008-04-07 05:43:21 +00004688 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004689 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004690 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004691 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004692 // Compatibility is based on the underlying type, not the promotion
4693 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004694 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004695 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4696 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004697 }
John McCall9dd450b2009-09-21 23:43:11 +00004698 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004699 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4700 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004701 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004702
Eli Friedman47f77112008-08-22 00:56:42 +00004703 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004704 }
Eli Friedman47f77112008-08-22 00:56:42 +00004705
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004706 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004707 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004708#define TYPE(Class, Base)
4709#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004710#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004711#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4712#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4713#include "clang/AST/TypeNodes.def"
4714 assert(false && "Non-canonical and dependent types shouldn't get here");
4715 return QualType();
4716
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004717 case Type::LValueReference:
4718 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004719 case Type::MemberPointer:
4720 assert(false && "C++ should never be in mergeTypes");
4721 return QualType();
4722
John McCall8b07ec22010-05-15 11:32:37 +00004723 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004724 case Type::IncompleteArray:
4725 case Type::VariableArray:
4726 case Type::FunctionProto:
4727 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004728 assert(false && "Types are eliminated above");
4729 return QualType();
4730
Chris Lattnerfd652912008-01-14 05:45:46 +00004731 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004732 {
4733 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004734 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4735 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004736 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4737 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004738 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004739 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004740 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004741 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004742 return getPointerType(ResultType);
4743 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004744 case Type::BlockPointer:
4745 {
4746 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004747 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4748 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004749 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004750 if (ResultType.isNull()) return QualType();
4751 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4752 return LHS;
4753 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4754 return RHS;
4755 return getBlockPointerType(ResultType);
4756 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004757 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004758 {
4759 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4760 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4761 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4762 return QualType();
4763
4764 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4765 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4766 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4767 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004768 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4769 return LHS;
4770 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4771 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004772 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4773 ArrayType::ArraySizeModifier(), 0);
4774 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4775 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004776 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4777 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004778 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4779 return LHS;
4780 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4781 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004782 if (LVAT) {
4783 // FIXME: This isn't correct! But tricky to implement because
4784 // the array's size has to be the size of LHS, but the type
4785 // has to be different.
4786 return LHS;
4787 }
4788 if (RVAT) {
4789 // FIXME: This isn't correct! But tricky to implement because
4790 // the array's size has to be the size of RHS, but the type
4791 // has to be different.
4792 return RHS;
4793 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004794 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4795 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004796 return getIncompleteArrayType(ResultType,
4797 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004798 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004799 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004800 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004801 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004802 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004803 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004804 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004805 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004806 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004807 case Type::Complex:
4808 // Distinct complex types are incompatible.
4809 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004810 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004811 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004812 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4813 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004814 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004815 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00004816 case Type::ObjCObject: {
4817 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004818 // FIXME: This should be type compatibility, e.g. whether
4819 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00004820 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
4821 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
4822 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00004823 return LHS;
4824
Eli Friedman47f77112008-08-22 00:56:42 +00004825 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004826 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004827 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004828 if (OfBlockPointer) {
4829 if (canAssignObjCInterfacesInBlockPointer(
4830 LHS->getAs<ObjCObjectPointerType>(),
4831 RHS->getAs<ObjCObjectPointerType>()))
4832 return LHS;
4833 return QualType();
4834 }
John McCall9dd450b2009-09-21 23:43:11 +00004835 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4836 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004837 return LHS;
4838
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004839 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004840 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004841 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004842
4843 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004844}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004845
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00004846/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
4847/// 'RHS' attributes and returns the merged version; including for function
4848/// return types.
4849QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
4850 QualType LHSCan = getCanonicalType(LHS),
4851 RHSCan = getCanonicalType(RHS);
4852 // If two types are identical, they are compatible.
4853 if (LHSCan == RHSCan)
4854 return LHS;
4855 if (RHSCan->isFunctionType()) {
4856 if (!LHSCan->isFunctionType())
4857 return QualType();
4858 QualType OldReturnType =
4859 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
4860 QualType NewReturnType =
4861 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
4862 QualType ResReturnType =
4863 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
4864 if (ResReturnType.isNull())
4865 return QualType();
4866 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
4867 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
4868 // In either case, use OldReturnType to build the new function type.
4869 const FunctionType *F = LHS->getAs<FunctionType>();
4870 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
4871 FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
4872 QualType ResultType
4873 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
4874 FPT->getNumArgs(), FPT->isVariadic(),
4875 FPT->getTypeQuals(),
4876 FPT->hasExceptionSpec(),
4877 FPT->hasAnyExceptionSpec(),
4878 FPT->getNumExceptions(),
4879 FPT->exception_begin(),
4880 Info);
4881 return ResultType;
4882 }
4883 }
4884 return QualType();
4885 }
4886
4887 // If the qualifiers are different, the types can still be merged.
4888 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4889 Qualifiers RQuals = RHSCan.getLocalQualifiers();
4890 if (LQuals != RQuals) {
4891 // If any of these qualifiers are different, we have a type mismatch.
4892 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4893 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4894 return QualType();
4895
4896 // Exactly one GC qualifier difference is allowed: __strong is
4897 // okay if the other type has no GC qualifier but is an Objective
4898 // C object pointer (i.e. implicitly strong by default). We fix
4899 // this by pretending that the unqualified type was actually
4900 // qualified __strong.
4901 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4902 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4903 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4904
4905 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4906 return QualType();
4907
4908 if (GC_L == Qualifiers::Strong)
4909 return LHS;
4910 if (GC_R == Qualifiers::Strong)
4911 return RHS;
4912 return QualType();
4913 }
4914
4915 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
4916 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
4917 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
4918 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
4919 if (ResQT == LHSBaseQT)
4920 return LHS;
4921 if (ResQT == RHSBaseQT)
4922 return RHS;
4923 }
4924 return QualType();
4925}
4926
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004927//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004928// Integer Predicates
4929//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004930
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004931unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004932 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004933 return 1;
John McCall56774992009-12-09 09:09:27 +00004934 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004935 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004936 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004937 return (unsigned)getTypeSize(T);
4938}
4939
4940QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4941 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004942
4943 // Turn <4 x signed int> -> <4 x unsigned int>
4944 if (const VectorType *VTy = T->getAs<VectorType>())
4945 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
Chris Lattner37141f42010-06-23 06:00:24 +00004946 VTy->getNumElements(), VTy->getAltiVecSpecific());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004947
4948 // For enums, we return the unsigned version of the base type.
4949 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004950 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004951
4952 const BuiltinType *BTy = T->getAs<BuiltinType>();
4953 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004954 switch (BTy->getKind()) {
4955 case BuiltinType::Char_S:
4956 case BuiltinType::SChar:
4957 return UnsignedCharTy;
4958 case BuiltinType::Short:
4959 return UnsignedShortTy;
4960 case BuiltinType::Int:
4961 return UnsignedIntTy;
4962 case BuiltinType::Long:
4963 return UnsignedLongTy;
4964 case BuiltinType::LongLong:
4965 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004966 case BuiltinType::Int128:
4967 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004968 default:
4969 assert(0 && "Unexpected signed integer type");
4970 return QualType();
4971 }
4972}
4973
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004974ExternalASTSource::~ExternalASTSource() { }
4975
4976void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004977
4978
4979//===----------------------------------------------------------------------===//
4980// Builtin Type Computation
4981//===----------------------------------------------------------------------===//
4982
4983/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4984/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004985static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004986 ASTContext::GetBuiltinTypeError &Error,
4987 bool AllowTypeModifiers = true) {
4988 // Modifiers.
4989 int HowLong = 0;
4990 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004991
Chris Lattnerecd79c62009-06-14 00:45:47 +00004992 // Read the modifiers first.
4993 bool Done = false;
4994 while (!Done) {
4995 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004996 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004997 case 'S':
4998 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4999 assert(!Signed && "Can't use 'S' modifier multiple times!");
5000 Signed = true;
5001 break;
5002 case 'U':
5003 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
5004 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
5005 Unsigned = true;
5006 break;
5007 case 'L':
5008 assert(HowLong <= 2 && "Can't have LLLL modifier");
5009 ++HowLong;
5010 break;
5011 }
5012 }
5013
5014 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005015
Chris Lattnerecd79c62009-06-14 00:45:47 +00005016 // Read the base type.
5017 switch (*Str++) {
5018 default: assert(0 && "Unknown builtin type letter!");
5019 case 'v':
5020 assert(HowLong == 0 && !Signed && !Unsigned &&
5021 "Bad modifiers used with 'v'!");
5022 Type = Context.VoidTy;
5023 break;
5024 case 'f':
5025 assert(HowLong == 0 && !Signed && !Unsigned &&
5026 "Bad modifiers used with 'f'!");
5027 Type = Context.FloatTy;
5028 break;
5029 case 'd':
5030 assert(HowLong < 2 && !Signed && !Unsigned &&
5031 "Bad modifiers used with 'd'!");
5032 if (HowLong)
5033 Type = Context.LongDoubleTy;
5034 else
5035 Type = Context.DoubleTy;
5036 break;
5037 case 's':
5038 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5039 if (Unsigned)
5040 Type = Context.UnsignedShortTy;
5041 else
5042 Type = Context.ShortTy;
5043 break;
5044 case 'i':
5045 if (HowLong == 3)
5046 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5047 else if (HowLong == 2)
5048 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5049 else if (HowLong == 1)
5050 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5051 else
5052 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5053 break;
5054 case 'c':
5055 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5056 if (Signed)
5057 Type = Context.SignedCharTy;
5058 else if (Unsigned)
5059 Type = Context.UnsignedCharTy;
5060 else
5061 Type = Context.CharTy;
5062 break;
5063 case 'b': // boolean
5064 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5065 Type = Context.BoolTy;
5066 break;
5067 case 'z': // size_t.
5068 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5069 Type = Context.getSizeType();
5070 break;
5071 case 'F':
5072 Type = Context.getCFConstantStringType();
5073 break;
5074 case 'a':
5075 Type = Context.getBuiltinVaListType();
5076 assert(!Type.isNull() && "builtin va list type not initialized!");
5077 break;
5078 case 'A':
5079 // This is a "reference" to a va_list; however, what exactly
5080 // this means depends on how va_list is defined. There are two
5081 // different kinds of va_list: ones passed by value, and ones
5082 // passed by reference. An example of a by-value va_list is
5083 // x86, where va_list is a char*. An example of by-ref va_list
5084 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5085 // we want this argument to be a char*&; for x86-64, we want
5086 // it to be a __va_list_tag*.
5087 Type = Context.getBuiltinVaListType();
5088 assert(!Type.isNull() && "builtin va list type not initialized!");
5089 if (Type->isArrayType()) {
5090 Type = Context.getArrayDecayedType(Type);
5091 } else {
5092 Type = Context.getLValueReferenceType(Type);
5093 }
5094 break;
5095 case 'V': {
5096 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005097 unsigned NumElements = strtoul(Str, &End, 10);
5098 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00005099
Chris Lattnerecd79c62009-06-14 00:45:47 +00005100 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005101
Chris Lattnerecd79c62009-06-14 00:45:47 +00005102 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00005103 // FIXME: Don't know what to do about AltiVec.
Chris Lattner37141f42010-06-23 06:00:24 +00005104 Type = Context.getVectorType(ElementType, NumElements,
5105 VectorType::NotAltiVec);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005106 break;
5107 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005108 case 'X': {
5109 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
5110 Type = Context.getComplexType(ElementType);
5111 break;
5112 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005113 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005114 Type = Context.getFILEType();
5115 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005116 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005117 return QualType();
5118 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005119 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005120 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005121 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005122 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005123 else
5124 Type = Context.getjmp_bufType();
5125
Mike Stump2adb4da2009-07-28 23:47:15 +00005126 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005127 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005128 return QualType();
5129 }
5130 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005131 }
Mike Stump11289f42009-09-09 15:08:12 +00005132
Chris Lattnerecd79c62009-06-14 00:45:47 +00005133 if (!AllowTypeModifiers)
5134 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00005135
Chris Lattnerecd79c62009-06-14 00:45:47 +00005136 Done = false;
5137 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005138 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005139 default: Done = true; --Str; break;
5140 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00005141 case '&':
John McCallb8b94662010-03-12 04:21:28 +00005142 {
5143 // Both pointers and references can have their pointee types
5144 // qualified with an address space.
5145 char *End;
5146 unsigned AddrSpace = strtoul(Str, &End, 10);
5147 if (End != Str && AddrSpace != 0) {
5148 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5149 Str = End;
5150 }
5151 }
5152 if (c == '*')
5153 Type = Context.getPointerType(Type);
5154 else
5155 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005156 break;
5157 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5158 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00005159 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00005160 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00005161 case 'D':
5162 Type = Context.getVolatileType(Type);
5163 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005164 }
5165 }
Mike Stump11289f42009-09-09 15:08:12 +00005166
Chris Lattnerecd79c62009-06-14 00:45:47 +00005167 return Type;
5168}
5169
5170/// GetBuiltinType - Return the type for the specified builtin.
5171QualType ASTContext::GetBuiltinType(unsigned id,
5172 GetBuiltinTypeError &Error) {
5173 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00005174
Chris Lattnerecd79c62009-06-14 00:45:47 +00005175 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005176
Chris Lattnerecd79c62009-06-14 00:45:47 +00005177 Error = GE_None;
5178 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
5179 if (Error != GE_None)
5180 return QualType();
5181 while (TypeStr[0] && TypeStr[0] != '.') {
5182 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
5183 if (Error != GE_None)
5184 return QualType();
5185
5186 // Do array -> pointer decay. The builtin should use the decayed type.
5187 if (Ty->isArrayType())
5188 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005189
Chris Lattnerecd79c62009-06-14 00:45:47 +00005190 ArgTypes.push_back(Ty);
5191 }
5192
5193 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5194 "'.' should only occur at end of builtin type list!");
5195
5196 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
5197 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
5198 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005199
5200 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00005201 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00005202 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005203 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00005204}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005205
5206QualType
5207ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
5208 // Perform the usual unary conversions. We do this early so that
5209 // integral promotions to "int" can allow us to exit early, in the
5210 // lhs == rhs check. Also, for conversion purposes, we ignore any
5211 // qualifiers. For example, "const float" and "float" are
5212 // equivalent.
5213 if (lhs->isPromotableIntegerType())
5214 lhs = getPromotedIntegerType(lhs);
5215 else
5216 lhs = lhs.getUnqualifiedType();
5217 if (rhs->isPromotableIntegerType())
5218 rhs = getPromotedIntegerType(rhs);
5219 else
5220 rhs = rhs.getUnqualifiedType();
5221
5222 // If both types are identical, no conversion is needed.
5223 if (lhs == rhs)
5224 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005225
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005226 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
5227 // The caller can deal with this (e.g. pointer + int).
5228 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5229 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005230
5231 // At this point, we have two different arithmetic types.
5232
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005233 // Handle complex types first (C99 6.3.1.8p1).
5234 if (lhs->isComplexType() || rhs->isComplexType()) {
5235 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00005236 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005237 // convert the rhs to the lhs complex type.
5238 return lhs;
5239 }
Mike Stump11289f42009-09-09 15:08:12 +00005240 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005241 // convert the lhs to the rhs complex type.
5242 return rhs;
5243 }
5244 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00005245 // When both operands are complex, the shorter operand is converted to the
5246 // type of the longer, and that is the type of the result. This corresponds
5247 // to what is done when combining two real floating-point operands.
5248 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005249 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00005250 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005251 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00005252 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005253 // "double _Complex" is promoted to "long double _Complex".
5254 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005255
5256 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005257 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005258 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005259 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00005260 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005261 // At this point, lhs and rhs have the same rank/size. Now, make sure the
5262 // domains match. This is a requirement for our implementation, C99
5263 // does not require this promotion.
5264 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5265 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5266 return rhs;
5267 } else { // handle "_Complex double, double".
5268 return lhs;
5269 }
5270 }
5271 return lhs; // The domain/size match exactly.
5272 }
5273 // Now handle "real" floating types (i.e. float, double, long double).
5274 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5275 // if we have an integer operand, the result is the real floating type.
5276 if (rhs->isIntegerType()) {
5277 // convert rhs to the lhs floating point type.
5278 return lhs;
5279 }
5280 if (rhs->isComplexIntegerType()) {
5281 // convert rhs to the complex floating point type.
5282 return getComplexType(lhs);
5283 }
5284 if (lhs->isIntegerType()) {
5285 // convert lhs to the rhs floating point type.
5286 return rhs;
5287 }
Mike Stump11289f42009-09-09 15:08:12 +00005288 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005289 // convert lhs to the complex floating point type.
5290 return getComplexType(rhs);
5291 }
5292 // We have two real floating types, float/complex combos were handled above.
5293 // Convert the smaller operand to the bigger result.
5294 int result = getFloatingTypeOrder(lhs, rhs);
5295 if (result > 0) // convert the rhs
5296 return lhs;
5297 assert(result < 0 && "illegal float comparison");
5298 return rhs; // convert the lhs
5299 }
5300 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5301 // Handle GCC complex int extension.
5302 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5303 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5304
5305 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005306 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005307 rhsComplexInt->getElementType()) >= 0)
5308 return lhs; // convert the rhs
5309 return rhs;
5310 } else if (lhsComplexInt && rhs->isIntegerType()) {
5311 // convert the rhs to the lhs complex type.
5312 return lhs;
5313 } else if (rhsComplexInt && lhs->isIntegerType()) {
5314 // convert the lhs to the rhs complex type.
5315 return rhs;
5316 }
5317 }
5318 // Finally, we have two differing integer types.
5319 // The rules for this case are in C99 6.3.1.8
5320 int compare = getIntegerTypeOrder(lhs, rhs);
5321 bool lhsSigned = lhs->isSignedIntegerType(),
5322 rhsSigned = rhs->isSignedIntegerType();
5323 QualType destType;
5324 if (lhsSigned == rhsSigned) {
5325 // Same signedness; use the higher-ranked type
5326 destType = compare >= 0 ? lhs : rhs;
5327 } else if (compare != (lhsSigned ? 1 : -1)) {
5328 // The unsigned type has greater than or equal rank to the
5329 // signed type, so use the unsigned type
5330 destType = lhsSigned ? rhs : lhs;
5331 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5332 // The two types are different widths; if we are here, that
5333 // means the signed type is larger than the unsigned type, so
5334 // use the signed type.
5335 destType = lhsSigned ? lhs : rhs;
5336 } else {
5337 // The signed type is higher-ranked than the unsigned type,
5338 // but isn't actually any bigger (like unsigned int and long
5339 // on most 32-bit systems). Use the unsigned type corresponding
5340 // to the signed type.
5341 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5342 }
5343 return destType;
5344}