blob: 2db5c5e914a419d333e3962a822e3e5d608c5922 [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),
John McCallc62bb642010-03-24 05:22:00 +0000145 LastSDM(0, 0) {
David Chisnall9f57c292009-08-17 16:35:33 +0000146 ObjCIdRedefinitionType = QualType();
147 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian04b258c2009-11-25 23:07:42 +0000148 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000149 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +0000150 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000151 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +0000152}
153
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000154ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000155 // Release the DenseMaps associated with DeclContext objects.
156 // FIXME: Is this the ideal solution?
157 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +0000158
Douglas Gregor1a809332010-05-23 18:26:36 +0000159 if (!FreeMemory) {
160 // Call all of the deallocation functions.
161 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
162 Deallocations[I].first(Deallocations[I].second);
163 }
164
Douglas Gregor832940b2010-03-02 23:58:15 +0000165 // Release all of the memory associated with overridden C++ methods.
166 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
167 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
168 OM != OMEnd; ++OM)
169 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +0000170
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000171 if (FreeMemory) {
172 // Deallocate all the types.
173 while (!Types.empty()) {
174 Types.back()->Destroy(*this);
175 Types.pop_back();
176 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000177
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000178 for (llvm::FoldingSet<ExtQuals>::iterator
179 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
180 // Increment in loop to prevent using deallocated memory.
John McCall8ccfcb52009-09-24 19:53:00 +0000181 Deallocate(&*I++);
Nuno Lopese013c7f2008-12-17 22:30:25 +0000182 }
Nuno Lopese013c7f2008-12-17 22:30:25 +0000183
Ted Kremenekc3015a92010-03-08 20:56:29 +0000184 for (llvm::DenseMap<const ObjCContainerDecl*,
185 const ASTRecordLayout*>::iterator
186 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
187 // Increment in loop to prevent using deallocated memory.
188 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
189 R->Destroy(*this);
190 }
Nuno Lopese013c7f2008-12-17 22:30:25 +0000191 }
192
Ted Kremenek076baeb2010-06-08 23:00:58 +0000193 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
194 // even when using the BumpPtrAllocator because they can contain
195 // DenseMaps.
196 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
197 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
198 // Increment in loop to prevent using deallocated memory.
199 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
200 R->Destroy(*this);
201 }
202
Douglas Gregorf21eb492009-03-26 23:50:42 +0000203 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000204 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
205 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000206 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000207 NNS != NNSEnd; ) {
208 // Increment in loop to prevent using deallocated memory.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000209 (*NNS++).Destroy(*this);
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000210 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000211
212 if (GlobalNestedNameSpecifier)
213 GlobalNestedNameSpecifier->Destroy(*this);
214
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000215 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000216}
217
Douglas Gregor1a809332010-05-23 18:26:36 +0000218void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
219 Deallocations.push_back(std::make_pair(Callback, Data));
220}
221
Mike Stump11289f42009-09-09 15:08:12 +0000222void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
224 ExternalSource.reset(Source.take());
225}
226
Chris Lattner4eb445d2007-01-26 01:27:23 +0000227void ASTContext::PrintStats() const {
228 fprintf(stderr, "*** AST Context Stats:\n");
229 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000230
Douglas Gregora30d0462009-05-26 14:40:08 +0000231 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000232#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000233#define ABSTRACT_TYPE(Name, Parent)
234#include "clang/AST/TypeNodes.def"
235 0 // Extra
236 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000237
Chris Lattner4eb445d2007-01-26 01:27:23 +0000238 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
239 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000240 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000241 }
242
Douglas Gregora30d0462009-05-26 14:40:08 +0000243 unsigned Idx = 0;
244 unsigned TotalBytes = 0;
245#define TYPE(Name, Parent) \
246 if (counts[Idx]) \
247 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
248 TotalBytes += counts[Idx] * sizeof(Name##Type); \
249 ++Idx;
250#define ABSTRACT_TYPE(Name, Parent)
251#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000252
Douglas Gregora30d0462009-05-26 14:40:08 +0000253 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000254
255 if (ExternalSource.get()) {
256 fprintf(stderr, "\n");
257 ExternalSource->PrintStats();
258 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000259}
260
261
John McCall48f2d582009-10-23 23:03:21 +0000262void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000263 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000264 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000265 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000266}
267
Chris Lattner970e54e2006-11-12 00:37:36 +0000268void ASTContext::InitBuiltinTypes() {
269 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000270
Chris Lattner970e54e2006-11-12 00:37:36 +0000271 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000272 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattner970e54e2006-11-12 00:37:36 +0000274 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000275 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000276 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000277 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000278 InitBuiltinType(CharTy, BuiltinType::Char_S);
279 else
280 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000281 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000282 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
283 InitBuiltinType(ShortTy, BuiltinType::Short);
284 InitBuiltinType(IntTy, BuiltinType::Int);
285 InitBuiltinType(LongTy, BuiltinType::Long);
286 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000287
Chris Lattner970e54e2006-11-12 00:37:36 +0000288 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000289 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
290 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
291 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
292 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
293 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000294
Chris Lattner970e54e2006-11-12 00:37:36 +0000295 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000296 InitBuiltinType(FloatTy, BuiltinType::Float);
297 InitBuiltinType(DoubleTy, BuiltinType::Double);
298 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000299
Chris Lattnerf122cef2009-04-30 02:43:43 +0000300 // GNU extension, 128-bit integers.
301 InitBuiltinType(Int128Ty, BuiltinType::Int128);
302 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
303
Chris Lattner007cb022009-02-26 23:43:47 +0000304 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
305 InitBuiltinType(WCharTy, BuiltinType::WChar);
306 else // C99
307 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000308
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000309 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
310 InitBuiltinType(Char16Ty, BuiltinType::Char16);
311 else // C99
312 Char16Ty = getFromTargetType(Target.getChar16Type());
313
314 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
315 InitBuiltinType(Char32Ty, BuiltinType::Char32);
316 else // C99
317 Char32Ty = getFromTargetType(Target.getChar32Type());
318
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000319 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000320 InitBuiltinType(OverloadTy, BuiltinType::Overload);
321
322 // Placeholder type for type-dependent expressions whose type is
323 // completely unknown. No code should ever check a type against
324 // DependentTy and users should never see it; however, it is here to
325 // help diagnose failures to properly check for type-dependent
326 // expressions.
327 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000328
Mike Stump11289f42009-09-09 15:08:12 +0000329 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000330 // not yet been deduced.
331 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000332
Chris Lattner970e54e2006-11-12 00:37:36 +0000333 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000334 FloatComplexTy = getComplexType(FloatTy);
335 DoubleComplexTy = getComplexType(DoubleTy);
336 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000337
Steve Naroff66e9f332007-10-15 14:41:52 +0000338 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000339
Steve Naroff1329fa02009-07-15 18:40:39 +0000340 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
341 ObjCIdTypedefType = QualType();
342 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000343 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000344
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000345 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000346 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
347 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000348 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000349
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000350 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000351
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000352 // void * type
353 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000354
355 // nullptr type (C++0x 2.14.7)
356 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000357}
358
Douglas Gregor86d142a2009-10-08 07:24:58 +0000359MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000360ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000361 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000362 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000363 = InstantiatedFromStaticDataMember.find(Var);
364 if (Pos == InstantiatedFromStaticDataMember.end())
365 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000366
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000367 return Pos->second;
368}
369
Mike Stump11289f42009-09-09 15:08:12 +0000370void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000371ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
372 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000373 assert(Inst->isStaticDataMember() && "Not a static data member");
374 assert(Tmpl->isStaticDataMember() && "Not a static data member");
375 assert(!InstantiatedFromStaticDataMember[Inst] &&
376 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000377 InstantiatedFromStaticDataMember[Inst]
378 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000379}
380
John McCalle61f2ba2009-11-18 02:36:19 +0000381NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000382ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000383 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000384 = InstantiatedFromUsingDecl.find(UUD);
385 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000386 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000387
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000388 return Pos->second;
389}
390
391void
John McCallb96ec562009-12-04 22:46:56 +0000392ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
393 assert((isa<UsingDecl>(Pattern) ||
394 isa<UnresolvedUsingValueDecl>(Pattern) ||
395 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
396 "pattern decl is not a using decl");
397 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
398 InstantiatedFromUsingDecl[Inst] = Pattern;
399}
400
401UsingShadowDecl *
402ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
403 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
404 = InstantiatedFromUsingShadowDecl.find(Inst);
405 if (Pos == InstantiatedFromUsingShadowDecl.end())
406 return 0;
407
408 return Pos->second;
409}
410
411void
412ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
413 UsingShadowDecl *Pattern) {
414 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
415 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000416}
417
Anders Carlsson5da84842009-09-01 04:26:58 +0000418FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
419 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
420 = InstantiatedFromUnnamedFieldDecl.find(Field);
421 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
422 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000423
Anders Carlsson5da84842009-09-01 04:26:58 +0000424 return Pos->second;
425}
426
427void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
428 FieldDecl *Tmpl) {
429 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
430 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
431 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
432 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000433
Anders Carlsson5da84842009-09-01 04:26:58 +0000434 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
435}
436
Douglas Gregor832940b2010-03-02 23:58:15 +0000437ASTContext::overridden_cxx_method_iterator
438ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
439 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
440 = OverriddenMethods.find(Method);
441 if (Pos == OverriddenMethods.end())
442 return 0;
443
444 return Pos->second.begin();
445}
446
447ASTContext::overridden_cxx_method_iterator
448ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
449 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
450 = OverriddenMethods.find(Method);
451 if (Pos == OverriddenMethods.end())
452 return 0;
453
454 return Pos->second.end();
455}
456
457void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
458 const CXXMethodDecl *Overridden) {
459 OverriddenMethods[Method].push_back(Overridden);
460}
461
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000462namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000463 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000464 : std::binary_function<SourceRange, SourceRange, bool> {
465 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000466
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000467 public:
468 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000469
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000470 bool operator()(SourceRange X, SourceRange Y) {
471 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
472 }
473 };
474}
475
Chris Lattner53cfe802007-07-18 17:52:12 +0000476//===----------------------------------------------------------------------===//
477// Type Sizing and Analysis
478//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000479
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000480/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
481/// scalar floating point type.
482const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000483 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000484 assert(BT && "Not a floating point type!");
485 switch (BT->getKind()) {
486 default: assert(0 && "Not a floating point type!");
487 case BuiltinType::Float: return Target.getFloatFormat();
488 case BuiltinType::Double: return Target.getDoubleFormat();
489 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
490 }
491}
492
Ken Dyck160146e2010-01-27 17:10:57 +0000493/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000494/// specified decl. Note that bitfields do not have a valid alignment, so
495/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000496/// If @p RefAsPointee, references are treated like their underlying type
497/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000498CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000499 unsigned Align = Target.getCharWidth();
500
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000501 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000502 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedman19a546c2009-02-22 02:56:25 +0000503
Chris Lattner68061312009-01-24 21:53:27 +0000504 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
505 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000506 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000507 if (RefAsPointee)
508 T = RT->getPointeeType();
509 else
510 T = getPointerType(RT->getPointeeType());
511 }
512 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindolae971b9a2010-06-04 23:15:27 +0000513 unsigned MinWidth = Target.getLargeArrayMinWidth();
514 unsigned ArrayAlign = Target.getLargeArrayAlign();
515 if (isa<VariableArrayType>(T) && MinWidth != 0)
516 Align = std::max(Align, ArrayAlign);
517 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
518 unsigned Size = getTypeSize(CT);
519 if (MinWidth != 0 && MinWidth <= Size)
520 Align = std::max(Align, ArrayAlign);
521 }
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000522 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000523 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
524 T = cast<ArrayType>(T)->getElementType();
525
526 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
527 }
Charles Davis3fc51072010-02-23 04:52:00 +0000528 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
529 // In the case of a field in a packed struct, we want the minimum
530 // of the alignment of the field and the alignment of the struct.
531 Align = std::min(Align,
532 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
533 }
Chris Lattner68061312009-01-24 21:53:27 +0000534 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000535
Ken Dyck160146e2010-01-27 17:10:57 +0000536 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000537}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000538
John McCall87fe5d52010-05-20 01:18:31 +0000539std::pair<CharUnits, CharUnits>
540ASTContext::getTypeInfoInChars(const Type *T) {
541 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
542 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
543 CharUnits::fromQuantity(Info.second / getCharWidth()));
544}
545
546std::pair<CharUnits, CharUnits>
547ASTContext::getTypeInfoInChars(QualType T) {
548 return getTypeInfoInChars(T.getTypePtr());
549}
550
Chris Lattner983a8bb2007-07-13 22:13:22 +0000551/// getTypeSize - Return the size of the specified type, in bits. This method
552/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000553///
554/// FIXME: Pointers into different addr spaces could have different sizes and
555/// alignment requirements: getPointerInfo should take an AddrSpace, this
556/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000557std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000558ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000559 uint64_t Width=0;
560 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000561 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000562#define TYPE(Class, Base)
563#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000564#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000565#define DEPENDENT_TYPE(Class, Base) case Type::Class:
566#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000567 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000568 break;
569
Chris Lattner355332d2007-07-13 22:27:08 +0000570 case Type::FunctionNoProto:
571 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000572 // GCC extension: alignof(function) = 32 bits
573 Width = 0;
574 Align = 32;
575 break;
576
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000577 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000578 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000579 Width = 0;
580 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
581 break;
582
Steve Naroff5c131802007-08-30 01:06:46 +0000583 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000584 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000585
Chris Lattner37e05872008-03-05 18:54:05 +0000586 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000587 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000588 Align = EltInfo.second;
589 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000590 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000591 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000592 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000593 const VectorType *VT = cast<VectorType>(T);
594 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
595 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000596 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000597 // If the alignment is not a power of 2, round up to the next power of 2.
598 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000599 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000600 Align = llvm::NextPowerOf2(Align);
601 Width = llvm::RoundUpToAlignment(Width, Align);
602 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000603 break;
604 }
Chris Lattner647fb222007-07-18 18:26:58 +0000605
Chris Lattner7570e9c2008-03-08 08:52:55 +0000606 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000607 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000608 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000609 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000610 // GCC extension: alignof(void) = 8 bits.
611 Width = 0;
612 Align = 8;
613 break;
614
Chris Lattner6a4f7452007-12-19 19:23:28 +0000615 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000616 Width = Target.getBoolWidth();
617 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000618 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000619 case BuiltinType::Char_S:
620 case BuiltinType::Char_U:
621 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000622 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000623 Width = Target.getCharWidth();
624 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000625 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000626 case BuiltinType::WChar:
627 Width = Target.getWCharWidth();
628 Align = Target.getWCharAlign();
629 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000630 case BuiltinType::Char16:
631 Width = Target.getChar16Width();
632 Align = Target.getChar16Align();
633 break;
634 case BuiltinType::Char32:
635 Width = Target.getChar32Width();
636 Align = Target.getChar32Align();
637 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000638 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000639 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000640 Width = Target.getShortWidth();
641 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000642 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000643 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000644 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000645 Width = Target.getIntWidth();
646 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000647 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000648 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000649 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000650 Width = Target.getLongWidth();
651 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000652 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000653 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000654 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000655 Width = Target.getLongLongWidth();
656 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000657 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000658 case BuiltinType::Int128:
659 case BuiltinType::UInt128:
660 Width = 128;
661 Align = 128; // int128_t is 128-bit aligned on all targets.
662 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000663 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000664 Width = Target.getFloatWidth();
665 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000666 break;
667 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000668 Width = Target.getDoubleWidth();
669 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000670 break;
671 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000672 Width = Target.getLongDoubleWidth();
673 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000674 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000675 case BuiltinType::NullPtr:
676 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
677 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000678 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000679 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000680 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000681 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000682 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000683 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000684 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000685 case Type::BlockPointer: {
686 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
687 Width = Target.getPointerWidth(AS);
688 Align = Target.getPointerAlign(AS);
689 break;
690 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000691 case Type::LValueReference:
692 case Type::RValueReference: {
693 // alignof and sizeof should never enter this code path here, so we go
694 // the pointer route.
695 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
696 Width = Target.getPointerWidth(AS);
697 Align = Target.getPointerAlign(AS);
698 break;
699 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000700 case Type::Pointer: {
701 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000702 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000703 Align = Target.getPointerAlign(AS);
704 break;
705 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000706 case Type::MemberPointer: {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000707 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000708 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000709 getTypeInfo(getPointerDiffType());
710 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000711 if (Pointee->isFunctionType())
712 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000713 Align = PtrDiffInfo.second;
714 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000715 }
Chris Lattner647fb222007-07-18 18:26:58 +0000716 case Type::Complex: {
717 // Complex types have the same alignment as their elements, but twice the
718 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000719 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000720 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000721 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000722 Align = EltInfo.second;
723 break;
724 }
John McCall8b07ec22010-05-15 11:32:37 +0000725 case Type::ObjCObject:
726 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Pateldbb72632008-06-04 21:54:36 +0000727 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000728 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000729 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
730 Width = Layout.getSize();
731 Align = Layout.getAlignment();
732 break;
733 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000734 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000735 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000736 const TagType *TT = cast<TagType>(T);
737
738 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000739 Width = 1;
740 Align = 1;
741 break;
742 }
Mike Stump11289f42009-09-09 15:08:12 +0000743
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000744 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000745 return getTypeInfo(ET->getDecl()->getIntegerType());
746
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000747 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000748 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
749 Width = Layout.getSize();
750 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000751 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000752 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000753
Chris Lattner63d2b362009-10-22 05:17:15 +0000754 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000755 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
756 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000757
Douglas Gregoref462e62009-04-30 17:32:17 +0000758 case Type::Typedef: {
759 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000760 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000761 Align = std::max(Aligned->getMaxAlignment(),
762 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregoref462e62009-04-30 17:32:17 +0000763 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
764 } else
765 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000766 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000767 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000768
769 case Type::TypeOfExpr:
770 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
771 .getTypePtr());
772
773 case Type::TypeOf:
774 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
775
Anders Carlsson81df7b82009-06-24 19:06:50 +0000776 case Type::Decltype:
777 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
778 .getTypePtr());
779
Abramo Bagnara6150c882010-05-11 21:36:43 +0000780 case Type::Elaborated:
781 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000782
Douglas Gregoref462e62009-04-30 17:32:17 +0000783 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000784 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000785 "Cannot request the size of a dependent type");
786 // FIXME: this is likely to be wrong once we support template
787 // aliases, since a template alias could refer to a typedef that
788 // has an __aligned__ attribute on it.
789 return getTypeInfo(getCanonicalType(T));
790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791
Chris Lattner53cfe802007-07-18 17:52:12 +0000792 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000793 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000794}
795
Ken Dyck8c89d592009-12-22 14:23:30 +0000796/// getTypeSizeInChars - Return the size of the specified type, in characters.
797/// This method does not work on incomplete types.
798CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000799 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000800}
801CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000802 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000803}
804
Ken Dycka6046ab2010-01-26 17:25:18 +0000805/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000806/// characters. This method does not work on incomplete types.
807CharUnits ASTContext::getTypeAlignInChars(QualType T) {
808 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
809}
810CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
811 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
812}
813
Chris Lattnera3402cd2009-01-27 18:08:34 +0000814/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
815/// type for the current target in bits. This can be different than the ABI
816/// alignment in cases where it is beneficial for performance to overalign
817/// a data type.
818unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
819 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000820
821 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000822 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000823 T = CT->getElementType().getTypePtr();
824 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
825 T->isSpecificBuiltinType(BuiltinType::LongLong))
826 return std::max(ABIAlign, (unsigned)getTypeSize(T));
827
Chris Lattnera3402cd2009-01-27 18:08:34 +0000828 return ABIAlign;
829}
830
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000831static void CollectLocalObjCIvars(ASTContext *Ctx,
832 const ObjCInterfaceDecl *OI,
833 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000834 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
835 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000836 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000837 if (!IVDecl->isInvalidDecl())
838 Fields.push_back(cast<FieldDecl>(IVDecl));
839 }
840}
841
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000842void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
843 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
844 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
845 CollectObjCIvars(SuperClass, Fields);
846 CollectLocalObjCIvars(this, OI, Fields);
847}
848
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000849/// ShallowCollectObjCIvars -
850/// Collect all ivars, including those synthesized, in the current class.
851///
852void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000853 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000854 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
855 E = OI->ivar_end(); I != E; ++I) {
856 Ivars.push_back(*I);
857 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000858
859 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000860}
861
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000862/// CollectNonClassIvars -
863/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000864/// This includes synthesized ivars (via @synthesize) and those in
865// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000866///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000867void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000868 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000869 // Find ivars declared in class extension.
870 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
871 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
872 E = CDecl->ivar_end(); I != E; ++I) {
873 Ivars.push_back(*I);
874 }
875 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000876
Ted Kremenek86838aa2010-03-11 19:44:54 +0000877 // Also add any ivar defined in this class's implementation. This
878 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000879 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
880 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
881 E = ImplDecl->ivar_end(); I != E; ++I)
882 Ivars.push_back(*I);
883 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000884}
885
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000886/// CollectInheritedProtocols - Collect all protocols in current class and
887/// those inherited by it.
888void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000889 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000890 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
891 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
892 PE = OI->protocol_end(); P != PE; ++P) {
893 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000894 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000895 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000896 PE = Proto->protocol_end(); P != PE; ++P) {
897 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000898 CollectInheritedProtocols(*P, Protocols);
899 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000900 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000901
902 // Categories of this Interface.
903 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
904 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
905 CollectInheritedProtocols(CDeclChain, Protocols);
906 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
907 while (SD) {
908 CollectInheritedProtocols(SD, Protocols);
909 SD = SD->getSuperClass();
910 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000911 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000912 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
913 PE = OC->protocol_end(); P != PE; ++P) {
914 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000915 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000916 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
917 PE = Proto->protocol_end(); P != PE; ++P)
918 CollectInheritedProtocols(*P, Protocols);
919 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000920 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000921 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
922 PE = OP->protocol_end(); P != PE; ++P) {
923 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000924 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000925 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
926 PE = Proto->protocol_end(); P != PE; ++P)
927 CollectInheritedProtocols(*P, Protocols);
928 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000929 }
930}
931
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000932unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
933 unsigned count = 0;
934 // Count ivars declared in class extension.
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000935 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension())
936 count += CDecl->ivar_size();
937
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000938 // Count ivar defined in this class's implementation. This
939 // includes synthesized ivars.
940 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000941 count += ImplDecl->ivar_size();
942
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000943 return count;
944}
945
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000946/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
947ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
948 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
949 I = ObjCImpls.find(D);
950 if (I != ObjCImpls.end())
951 return cast<ObjCImplementationDecl>(I->second);
952 return 0;
953}
954/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
955ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
956 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
957 I = ObjCImpls.find(D);
958 if (I != ObjCImpls.end())
959 return cast<ObjCCategoryImplDecl>(I->second);
960 return 0;
961}
962
963/// \brief Set the implementation of ObjCInterfaceDecl.
964void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
965 ObjCImplementationDecl *ImplD) {
966 assert(IFaceD && ImplD && "Passed null params");
967 ObjCImpls[IFaceD] = ImplD;
968}
969/// \brief Set the implementation of ObjCCategoryDecl.
970void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
971 ObjCCategoryImplDecl *ImplD) {
972 assert(CatD && ImplD && "Passed null params");
973 ObjCImpls[CatD] = ImplD;
974}
975
John McCallbcd03502009-12-07 02:54:59 +0000976/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000977///
John McCallbcd03502009-12-07 02:54:59 +0000978/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000979/// the TypeLoc wrappers.
980///
981/// \param T the type that will be the basis for type source info. This type
982/// should refer to how the declarator was written in source code, not to
983/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000984TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000985 unsigned DataSize) {
986 if (!DataSize)
987 DataSize = TypeLoc::getFullDataSizeForType(T);
988 else
989 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000990 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000991
John McCallbcd03502009-12-07 02:54:59 +0000992 TypeSourceInfo *TInfo =
993 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
994 new (TInfo) TypeSourceInfo(T);
995 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000996}
997
John McCallbcd03502009-12-07 02:54:59 +0000998TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000999 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +00001000 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +00001001 DI->getTypeLoc().initialize(L);
1002 return DI;
1003}
1004
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001005const ASTRecordLayout &
1006ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1007 return getObjCLayout(D, 0);
1008}
1009
1010const ASTRecordLayout &
1011ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1012 return getObjCLayout(D->getClassInterface(), D);
1013}
1014
Chris Lattner983a8bb2007-07-13 22:13:22 +00001015//===----------------------------------------------------------------------===//
1016// Type creation/memoization methods
1017//===----------------------------------------------------------------------===//
1018
John McCall8ccfcb52009-09-24 19:53:00 +00001019QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1020 unsigned Fast = Quals.getFastQualifiers();
1021 Quals.removeFastQualifiers();
1022
1023 // Check if we've already instantiated this type.
1024 llvm::FoldingSetNodeID ID;
1025 ExtQuals::Profile(ID, TypeNode, Quals);
1026 void *InsertPos = 0;
1027 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1028 assert(EQ->getQualifiers() == Quals);
1029 QualType T = QualType(EQ, Fast);
1030 return T;
1031 }
1032
John McCall90d1c2d2009-09-24 23:30:46 +00001033 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001034 ExtQualNodes.InsertNode(New, InsertPos);
1035 QualType T = QualType(New, Fast);
1036 return T;
1037}
1038
1039QualType ASTContext::getVolatileType(QualType T) {
1040 QualType CanT = getCanonicalType(T);
1041 if (CanT.isVolatileQualified()) return T;
1042
1043 QualifierCollector Quals;
1044 const Type *TypeNode = Quals.strip(T);
1045 Quals.addVolatile();
1046
1047 return getExtQualType(TypeNode, Quals);
1048}
1049
Fariborz Jahanianece85822009-02-17 18:27:45 +00001050QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001051 QualType CanT = getCanonicalType(T);
1052 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001053 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001054
John McCall8ccfcb52009-09-24 19:53:00 +00001055 // If we are composing extended qualifiers together, merge together
1056 // into one ExtQuals node.
1057 QualifierCollector Quals;
1058 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001059
John McCall8ccfcb52009-09-24 19:53:00 +00001060 // If this type already has an address space specified, it cannot get
1061 // another one.
1062 assert(!Quals.hasAddressSpace() &&
1063 "Type cannot be in multiple addr spaces!");
1064 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001065
John McCall8ccfcb52009-09-24 19:53:00 +00001066 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001067}
1068
Chris Lattnerd60183d2009-02-18 22:53:11 +00001069QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001070 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001071 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001072 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001073 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001074
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001075 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001076 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001077 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001078 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1079 return getPointerType(ResultType);
1080 }
1081 }
Mike Stump11289f42009-09-09 15:08:12 +00001082
John McCall8ccfcb52009-09-24 19:53:00 +00001083 // If we are composing extended qualifiers together, merge together
1084 // into one ExtQuals node.
1085 QualifierCollector Quals;
1086 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001087
John McCall8ccfcb52009-09-24 19:53:00 +00001088 // If this type already has an ObjCGC specified, it cannot get
1089 // another one.
1090 assert(!Quals.hasObjCGCAttr() &&
1091 "Type cannot have multiple ObjCGCs!");
1092 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001093
John McCall8ccfcb52009-09-24 19:53:00 +00001094 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001095}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001096
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001097static QualType getExtFunctionType(ASTContext& Context, QualType T,
1098 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001099 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001100 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1101 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001102 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001103 if (ResultType == Pointee)
1104 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001105
1106 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001107 } else if (const BlockPointerType *BlockPointer
1108 = T->getAs<BlockPointerType>()) {
1109 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001110 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001111 if (ResultType == Pointee)
1112 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001113
1114 ResultType = Context.getBlockPointerType(ResultType);
1115 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001116 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001117 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001118
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001119 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001120 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001121 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001122 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001123 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001124 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001125 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1126 FPT->getNumArgs(), FPT->isVariadic(),
1127 FPT->getTypeQuals(),
1128 FPT->hasExceptionSpec(),
1129 FPT->hasAnyExceptionSpec(),
1130 FPT->getNumExceptions(),
1131 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001132 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001133 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001134 } else
1135 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001136
1137 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1138}
1139
1140QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001141 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001142 return getExtFunctionType(*this, T,
1143 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001144}
1145
1146QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001147 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001148 return getExtFunctionType(*this, T,
1149 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001150}
1151
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001152QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1153 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1154 return getExtFunctionType(*this, T,
1155 Info.withRegParm(RegParm));
1156}
1157
Chris Lattnerc6395932007-06-22 20:56:16 +00001158/// getComplexType - Return the uniqued reference to the type for a complex
1159/// number with the specified element type.
1160QualType ASTContext::getComplexType(QualType T) {
1161 // Unique pointers, to guarantee there is only one pointer of a particular
1162 // structure.
1163 llvm::FoldingSetNodeID ID;
1164 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001165
Chris Lattnerc6395932007-06-22 20:56:16 +00001166 void *InsertPos = 0;
1167 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1168 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001169
Chris Lattnerc6395932007-06-22 20:56:16 +00001170 // If the pointee type isn't canonical, this won't be a canonical type either,
1171 // so fill in the canonical type field.
1172 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001173 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001174 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001175
Chris Lattnerc6395932007-06-22 20:56:16 +00001176 // Get the new insert position for the node we care about.
1177 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001178 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001179 }
John McCall90d1c2d2009-09-24 23:30:46 +00001180 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001181 Types.push_back(New);
1182 ComplexTypes.InsertNode(New, InsertPos);
1183 return QualType(New, 0);
1184}
1185
Chris Lattner970e54e2006-11-12 00:37:36 +00001186/// getPointerType - Return the uniqued reference to the type for a pointer to
1187/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001188QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001189 // Unique pointers, to guarantee there is only one pointer of a particular
1190 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001191 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001192 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001193
Chris Lattner67521df2007-01-27 01:29:36 +00001194 void *InsertPos = 0;
1195 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001196 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001197
Chris Lattner7ccecb92006-11-12 08:50:50 +00001198 // If the pointee type isn't canonical, this won't be a canonical type either,
1199 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001200 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001201 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001202 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001203
Chris Lattner67521df2007-01-27 01:29:36 +00001204 // Get the new insert position for the node we care about.
1205 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001206 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001207 }
John McCall90d1c2d2009-09-24 23:30:46 +00001208 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001209 Types.push_back(New);
1210 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001211 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001212}
1213
Mike Stump11289f42009-09-09 15:08:12 +00001214/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001215/// a pointer to the specified block.
1216QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001217 assert(T->isFunctionType() && "block of function types only");
1218 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001219 // structure.
1220 llvm::FoldingSetNodeID ID;
1221 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001222
Steve Naroffec33ed92008-08-27 16:04:49 +00001223 void *InsertPos = 0;
1224 if (BlockPointerType *PT =
1225 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1226 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001227
1228 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001229 // type either so fill in the canonical type field.
1230 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001231 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001232 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001233
Steve Naroffec33ed92008-08-27 16:04:49 +00001234 // Get the new insert position for the node we care about.
1235 BlockPointerType *NewIP =
1236 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001237 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001238 }
John McCall90d1c2d2009-09-24 23:30:46 +00001239 BlockPointerType *New
1240 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001241 Types.push_back(New);
1242 BlockPointerTypes.InsertNode(New, InsertPos);
1243 return QualType(New, 0);
1244}
1245
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001246/// getLValueReferenceType - Return the uniqued reference to the type for an
1247/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001248QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001249 // Unique pointers, to guarantee there is only one pointer of a particular
1250 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001251 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001252 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001253
1254 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001255 if (LValueReferenceType *RT =
1256 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001257 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001258
John McCallfc93cf92009-10-22 22:37:11 +00001259 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1260
Bill Wendling3708c182007-05-27 10:15:43 +00001261 // If the referencee type isn't canonical, this won't be a canonical type
1262 // either, so fill in the canonical type field.
1263 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001264 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1265 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1266 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001267
Bill Wendling3708c182007-05-27 10:15:43 +00001268 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001269 LValueReferenceType *NewIP =
1270 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001271 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001272 }
1273
John McCall90d1c2d2009-09-24 23:30:46 +00001274 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001275 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1276 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001277 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001278 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001279
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001280 return QualType(New, 0);
1281}
1282
1283/// getRValueReferenceType - Return the uniqued reference to the type for an
1284/// rvalue reference to the specified type.
1285QualType ASTContext::getRValueReferenceType(QualType T) {
1286 // Unique pointers, to guarantee there is only one pointer of a particular
1287 // structure.
1288 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001289 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001290
1291 void *InsertPos = 0;
1292 if (RValueReferenceType *RT =
1293 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1294 return QualType(RT, 0);
1295
John McCallfc93cf92009-10-22 22:37:11 +00001296 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1297
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001298 // If the referencee type isn't canonical, this won't be a canonical type
1299 // either, so fill in the canonical type field.
1300 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001301 if (InnerRef || !T.isCanonical()) {
1302 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1303 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001304
1305 // Get the new insert position for the node we care about.
1306 RValueReferenceType *NewIP =
1307 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1308 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1309 }
1310
John McCall90d1c2d2009-09-24 23:30:46 +00001311 RValueReferenceType *New
1312 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001313 Types.push_back(New);
1314 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001315 return QualType(New, 0);
1316}
1317
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001318/// getMemberPointerType - Return the uniqued reference to the type for a
1319/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001320QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001321 // Unique pointers, to guarantee there is only one pointer of a particular
1322 // structure.
1323 llvm::FoldingSetNodeID ID;
1324 MemberPointerType::Profile(ID, T, Cls);
1325
1326 void *InsertPos = 0;
1327 if (MemberPointerType *PT =
1328 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1329 return QualType(PT, 0);
1330
1331 // If the pointee or class type isn't canonical, this won't be a canonical
1332 // type either, so fill in the canonical type field.
1333 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001334 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001335 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1336
1337 // Get the new insert position for the node we care about.
1338 MemberPointerType *NewIP =
1339 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1340 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1341 }
John McCall90d1c2d2009-09-24 23:30:46 +00001342 MemberPointerType *New
1343 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001344 Types.push_back(New);
1345 MemberPointerTypes.InsertNode(New, InsertPos);
1346 return QualType(New, 0);
1347}
1348
Mike Stump11289f42009-09-09 15:08:12 +00001349/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001350/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001351QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001352 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001353 ArrayType::ArraySizeModifier ASM,
1354 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001355 assert((EltTy->isDependentType() ||
1356 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001357 "Constant array of VLAs is illegal!");
1358
Chris Lattnere2df3f92009-05-13 04:12:56 +00001359 // Convert the array size into a canonical width matching the pointer size for
1360 // the target.
1361 llvm::APInt ArySize(ArySizeIn);
1362 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001363
Chris Lattner23b7eb62007-06-15 23:05:46 +00001364 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001365 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001366
Chris Lattner36f8e652007-01-27 08:31:04 +00001367 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001368 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001369 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001370 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattner7ccecb92006-11-12 08:50:50 +00001372 // If the element type isn't canonical, this won't be a canonical type either,
1373 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001374 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001375 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001376 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001377 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001378 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001379 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001380 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001381 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001382 }
Mike Stump11289f42009-09-09 15:08:12 +00001383
John McCall90d1c2d2009-09-24 23:30:46 +00001384 ConstantArrayType *New = new(*this,TypeAlignment)
1385 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001386 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001387 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001388 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001389}
1390
Steve Naroffcadebd02007-08-30 18:14:25 +00001391/// getVariableArrayType - Returns a non-unique reference to the type for a
1392/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001393QualType ASTContext::getVariableArrayType(QualType EltTy,
1394 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001395 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001396 unsigned EltTypeQuals,
1397 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001398 // Since we don't unique expressions, it isn't possible to unique VLA's
1399 // that have an expression provided for their size.
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001400 QualType CanonType;
1401
1402 if (!EltTy.isCanonical()) {
1403 if (NumElts)
1404 NumElts->Retain();
1405 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1406 EltTypeQuals, Brackets);
1407 }
1408
John McCall90d1c2d2009-09-24 23:30:46 +00001409 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001410 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001411
1412 VariableArrayTypes.push_back(New);
1413 Types.push_back(New);
1414 return QualType(New, 0);
1415}
1416
Douglas Gregor4619e432008-12-05 23:32:09 +00001417/// getDependentSizedArrayType - Returns a non-unique reference to
1418/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001419/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001420QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1421 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001422 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001423 unsigned EltTypeQuals,
1424 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001425 assert((!NumElts || NumElts->isTypeDependent() ||
1426 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001427 "Size must be type- or value-dependent!");
1428
Douglas Gregorf3f95522009-07-31 00:23:35 +00001429 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001430 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001431 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001432
1433 if (NumElts) {
1434 // Dependently-sized array types that do not have a specified
1435 // number of elements will have their sizes deduced from an
1436 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001437 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1438 EltTypeQuals, NumElts);
1439
1440 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1441 }
1442
Douglas Gregorf3f95522009-07-31 00:23:35 +00001443 DependentSizedArrayType *New;
1444 if (Canon) {
1445 // We already have a canonical version of this array type; use it as
1446 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001447 New = new (*this, TypeAlignment)
1448 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1449 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001450 } else {
1451 QualType CanonEltTy = getCanonicalType(EltTy);
1452 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001453 New = new (*this, TypeAlignment)
1454 DependentSizedArrayType(*this, EltTy, QualType(),
1455 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001456
Douglas Gregorc42075a2010-02-04 18:10:26 +00001457 if (NumElts) {
1458 DependentSizedArrayType *CanonCheck
1459 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1460 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1461 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001462 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001463 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001464 } else {
1465 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1466 ASM, EltTypeQuals,
1467 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001468 New = new (*this, TypeAlignment)
1469 DependentSizedArrayType(*this, EltTy, Canon,
1470 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001471 }
1472 }
Mike Stump11289f42009-09-09 15:08:12 +00001473
Douglas Gregor4619e432008-12-05 23:32:09 +00001474 Types.push_back(New);
1475 return QualType(New, 0);
1476}
1477
Eli Friedmanbd258282008-02-15 18:16:39 +00001478QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1479 ArrayType::ArraySizeModifier ASM,
1480 unsigned EltTypeQuals) {
1481 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001482 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001483
1484 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001485 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001486 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1487 return QualType(ATP, 0);
1488
1489 // If the element type isn't canonical, this won't be a canonical type
1490 // either, so fill in the canonical type field.
1491 QualType Canonical;
1492
John McCallb692a092009-10-22 20:10:53 +00001493 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001494 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001495 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001496
1497 // Get the new insert position for the node we care about.
1498 IncompleteArrayType *NewIP =
1499 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001500 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001501 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001502
John McCall90d1c2d2009-09-24 23:30:46 +00001503 IncompleteArrayType *New = new (*this, TypeAlignment)
1504 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001505
1506 IncompleteArrayTypes.InsertNode(New, InsertPos);
1507 Types.push_back(New);
1508 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001509}
1510
Steve Naroff91fcddb2007-07-18 18:00:27 +00001511/// getVectorType - Return the unique reference to a vector type of
1512/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001513QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1514 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001515 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001516
Chris Lattner76a00cf2008-04-06 22:59:24 +00001517 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001518 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001519
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001520 // Check if we've already instantiated a vector of this type.
1521 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001522 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1523 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001524 void *InsertPos = 0;
1525 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1526 return QualType(VTP, 0);
1527
1528 // If the element type isn't canonical, this won't be a canonical type either,
1529 // so fill in the canonical type field.
1530 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001531 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1532 Canonical = getVectorType(getCanonicalType(vecType),
1533 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001534
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001535 // Get the new insert position for the node we care about.
1536 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001537 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001538 }
John McCall90d1c2d2009-09-24 23:30:46 +00001539 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001540 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001541 VectorTypes.InsertNode(New, InsertPos);
1542 Types.push_back(New);
1543 return QualType(New, 0);
1544}
1545
Nate Begemance4d7fc2008-04-18 23:10:10 +00001546/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001547/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001548QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001549 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001550
Chris Lattner76a00cf2008-04-06 22:59:24 +00001551 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001552 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001553
Steve Naroff91fcddb2007-07-18 18:00:27 +00001554 // Check if we've already instantiated a vector of this type.
1555 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001556 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001557 void *InsertPos = 0;
1558 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1559 return QualType(VTP, 0);
1560
1561 // If the element type isn't canonical, this won't be a canonical type either,
1562 // so fill in the canonical type field.
1563 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001564 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001565 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001566
Steve Naroff91fcddb2007-07-18 18:00:27 +00001567 // Get the new insert position for the node we care about.
1568 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001569 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001570 }
John McCall90d1c2d2009-09-24 23:30:46 +00001571 ExtVectorType *New = new (*this, TypeAlignment)
1572 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001573 VectorTypes.InsertNode(New, InsertPos);
1574 Types.push_back(New);
1575 return QualType(New, 0);
1576}
1577
Mike Stump11289f42009-09-09 15:08:12 +00001578QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001579 Expr *SizeExpr,
1580 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001581 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001582 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001583 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001584
Douglas Gregor352169a2009-07-31 03:54:25 +00001585 void *InsertPos = 0;
1586 DependentSizedExtVectorType *Canon
1587 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1588 DependentSizedExtVectorType *New;
1589 if (Canon) {
1590 // We already have a canonical version of this array type; use it as
1591 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001592 New = new (*this, TypeAlignment)
1593 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1594 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001595 } else {
1596 QualType CanonVecTy = getCanonicalType(vecType);
1597 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001598 New = new (*this, TypeAlignment)
1599 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1600 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001601
1602 DependentSizedExtVectorType *CanonCheck
1603 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1604 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1605 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001606 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1607 } else {
1608 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1609 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001610 New = new (*this, TypeAlignment)
1611 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001612 }
1613 }
Mike Stump11289f42009-09-09 15:08:12 +00001614
Douglas Gregor758a8692009-06-17 21:51:59 +00001615 Types.push_back(New);
1616 return QualType(New, 0);
1617}
1618
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001619/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001620///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001621QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1622 const FunctionType::ExtInfo &Info) {
1623 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001624 // Unique functions, to guarantee there is only one function of a particular
1625 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001626 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001627 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001628
Chris Lattner47955de2007-01-27 08:37:20 +00001629 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001630 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001631 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001632 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001633
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001634 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001635 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001636 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001637 Canonical =
1638 getFunctionNoProtoType(getCanonicalType(ResultTy),
1639 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001640
Chris Lattner47955de2007-01-27 08:37:20 +00001641 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001642 FunctionNoProtoType *NewIP =
1643 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001644 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001645 }
Mike Stump11289f42009-09-09 15:08:12 +00001646
John McCall90d1c2d2009-09-24 23:30:46 +00001647 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001648 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001649 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001650 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001651 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001652}
1653
1654/// getFunctionType - Return a normal function type with a typed argument
1655/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001656QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001657 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001658 unsigned TypeQuals, bool hasExceptionSpec,
1659 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001660 const QualType *ExArray,
1661 const FunctionType::ExtInfo &Info) {
1662 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001663 // Unique functions, to guarantee there is only one function of a particular
1664 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001665 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001666 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001667 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001668 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001669
1670 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001671 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001672 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001673 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001674
1675 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001676 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001677 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001678 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001679 isCanonical = false;
1680
1681 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001682 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001683 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001684 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001685 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001686 CanonicalArgs.reserve(NumArgs);
1687 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001688 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001689
Chris Lattner76a00cf2008-04-06 22:59:24 +00001690 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001691 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001692 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001693 false, 0, 0,
1694 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001695
Chris Lattnerfd4de792007-01-27 01:15:32 +00001696 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001697 FunctionProtoType *NewIP =
1698 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001699 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001700 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001701
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001702 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001703 // for two variable size arrays (for parameter and exception types) at the
1704 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001705 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001706 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1707 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001708 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001709 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001710 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001711 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001712 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001713 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001714 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001715}
Chris Lattneref51c202006-11-10 07:17:23 +00001716
John McCalle78aac42010-03-10 03:28:59 +00001717#ifndef NDEBUG
1718static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1719 if (!isa<CXXRecordDecl>(D)) return false;
1720 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1721 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1722 return true;
1723 if (RD->getDescribedClassTemplate() &&
1724 !isa<ClassTemplateSpecializationDecl>(RD))
1725 return true;
1726 return false;
1727}
1728#endif
1729
1730/// getInjectedClassNameType - Return the unique reference to the
1731/// injected class name type for the specified templated declaration.
1732QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1733 QualType TST) {
1734 assert(NeedsInjectedClassNameType(Decl));
1735 if (Decl->TypeForDecl) {
1736 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1737 } else if (CXXRecordDecl *PrevDecl
1738 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1739 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1740 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1741 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1742 } else {
John McCall2408e322010-04-27 00:57:59 +00001743 Decl->TypeForDecl =
1744 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001745 Types.push_back(Decl->TypeForDecl);
1746 }
1747 return QualType(Decl->TypeForDecl, 0);
1748}
1749
Douglas Gregor83a586e2008-04-13 21:07:44 +00001750/// getTypeDeclType - Return the unique reference to the type for the
1751/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001752QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001753 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001754 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001755
John McCall81e38502010-02-16 03:57:14 +00001756 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001757 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001758
John McCall96f0b5f2010-03-10 06:48:02 +00001759 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1760 "Template type parameter types are always available.");
1761
John McCall81e38502010-02-16 03:57:14 +00001762 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001763 assert(!Record->getPreviousDeclaration() &&
1764 "struct/union has previous declaration");
1765 assert(!NeedsInjectedClassNameType(Record));
1766 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001767 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001768 assert(!Enum->getPreviousDeclaration() &&
1769 "enum has previous declaration");
1770 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001771 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001772 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1773 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001774 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001775 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001776
John McCall96f0b5f2010-03-10 06:48:02 +00001777 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001778 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001779}
1780
Chris Lattner32d920b2007-01-26 02:01:53 +00001781/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001782/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001783QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001784 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001785
Chris Lattner76a00cf2008-04-06 22:59:24 +00001786 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001787 Decl->TypeForDecl = new(*this, TypeAlignment)
1788 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001789 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001790 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001791}
1792
John McCallcebee162009-10-18 09:09:24 +00001793/// \brief Retrieve a substitution-result type.
1794QualType
1795ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1796 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001797 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001798 && "replacement types must always be canonical");
1799
1800 llvm::FoldingSetNodeID ID;
1801 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1802 void *InsertPos = 0;
1803 SubstTemplateTypeParmType *SubstParm
1804 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1805
1806 if (!SubstParm) {
1807 SubstParm = new (*this, TypeAlignment)
1808 SubstTemplateTypeParmType(Parm, Replacement);
1809 Types.push_back(SubstParm);
1810 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1811 }
1812
1813 return QualType(SubstParm, 0);
1814}
1815
Douglas Gregoreff93e02009-02-05 23:33:38 +00001816/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001817/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001818/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001819QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001820 bool ParameterPack,
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001821 IdentifierInfo *Name) {
Douglas Gregoreff93e02009-02-05 23:33:38 +00001822 llvm::FoldingSetNodeID ID;
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001823 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001824 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001825 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001826 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1827
1828 if (TypeParm)
1829 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001830
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001831 if (Name) {
Anders Carlsson90036dc2009-06-16 00:30:48 +00001832 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001833 TypeParm = new (*this, TypeAlignment)
1834 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001835
1836 TemplateTypeParmType *TypeCheck
1837 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1838 assert(!TypeCheck && "Template type parameter canonical type broken");
1839 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001840 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001841 TypeParm = new (*this, TypeAlignment)
1842 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001843
1844 Types.push_back(TypeParm);
1845 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1846
1847 return QualType(TypeParm, 0);
1848}
1849
John McCalle78aac42010-03-10 03:28:59 +00001850TypeSourceInfo *
1851ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1852 SourceLocation NameLoc,
1853 const TemplateArgumentListInfo &Args,
1854 QualType CanonType) {
1855 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1856
1857 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1858 TemplateSpecializationTypeLoc TL
1859 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1860 TL.setTemplateNameLoc(NameLoc);
1861 TL.setLAngleLoc(Args.getLAngleLoc());
1862 TL.setRAngleLoc(Args.getRAngleLoc());
1863 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1864 TL.setArgLocInfo(i, Args[i].getLocInfo());
1865 return DI;
1866}
1867
Mike Stump11289f42009-09-09 15:08:12 +00001868QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001869ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001870 const TemplateArgumentListInfo &Args,
John McCall30576cd2010-06-13 09:25:03 +00001871 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00001872 unsigned NumArgs = Args.size();
1873
John McCall0ad16662009-10-29 08:12:44 +00001874 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1875 ArgVec.reserve(NumArgs);
1876 for (unsigned i = 0; i != NumArgs; ++i)
1877 ArgVec.push_back(Args[i].getArgument());
1878
John McCall2408e322010-04-27 00:57:59 +00001879 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00001880 Canon);
John McCall0ad16662009-10-29 08:12:44 +00001881}
1882
1883QualType
1884ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001885 const TemplateArgument *Args,
1886 unsigned NumArgs,
John McCall30576cd2010-06-13 09:25:03 +00001887 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001888 if (!Canon.isNull())
1889 Canon = getCanonicalType(Canon);
1890 else {
1891 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001892 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1893 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1894 CanonArgs.reserve(NumArgs);
1895 for (unsigned I = 0; I != NumArgs; ++I)
1896 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1897
1898 // Determine whether this canonical template specialization type already
1899 // exists.
1900 llvm::FoldingSetNodeID ID;
John McCall30576cd2010-06-13 09:25:03 +00001901 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor00044172009-07-29 16:09:57 +00001902 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001903
1904 void *InsertPos = 0;
1905 TemplateSpecializationType *Spec
1906 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001907
Douglas Gregora8e02e72009-07-28 23:00:59 +00001908 if (!Spec) {
1909 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001910 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001911 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001912 TypeAlignment);
John McCall30576cd2010-06-13 09:25:03 +00001913 Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001914 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001915 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001916 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001917 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001918 }
Mike Stump11289f42009-09-09 15:08:12 +00001919
Douglas Gregor15301382009-07-30 17:40:51 +00001920 if (Canon.isNull())
1921 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001922 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001923 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001924 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001925
Douglas Gregora8e02e72009-07-28 23:00:59 +00001926 // Allocate the (non-canonical) template specialization type, but don't
1927 // try to unique it: these types typically have location information that
1928 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001929 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001930 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001931 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001932 TemplateSpecializationType *Spec
John McCall773cc982010-06-11 11:07:21 +00001933 = new (Mem) TemplateSpecializationType(Template,
John McCall2408e322010-04-27 00:57:59 +00001934 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001935 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001936
Douglas Gregor8bf42052009-02-09 18:46:07 +00001937 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001938 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001939}
1940
Mike Stump11289f42009-09-09 15:08:12 +00001941QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00001942ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
1943 NestedNameSpecifier *NNS,
1944 QualType NamedType) {
Douglas Gregor52537682009-03-19 00:18:19 +00001945 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001946 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001947
1948 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001949 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00001950 if (T)
1951 return QualType(T, 0);
1952
Douglas Gregorc42075a2010-02-04 18:10:26 +00001953 QualType Canon = NamedType;
1954 if (!Canon.isCanonical()) {
1955 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00001956 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1957 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00001958 (void)CheckT;
1959 }
1960
Abramo Bagnara6150c882010-05-11 21:36:43 +00001961 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001962 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00001963 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00001964 return QualType(T, 0);
1965}
1966
Douglas Gregor02085352010-03-31 20:19:30 +00001967QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1968 NestedNameSpecifier *NNS,
1969 const IdentifierInfo *Name,
1970 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001971 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1972
1973 if (Canon.isNull()) {
1974 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001975 ElaboratedTypeKeyword CanonKeyword = Keyword;
1976 if (Keyword == ETK_None)
1977 CanonKeyword = ETK_Typename;
1978
1979 if (CanonNNS != NNS || CanonKeyword != Keyword)
1980 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001981 }
1982
1983 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001984 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001985
1986 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001987 DependentNameType *T
1988 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001989 if (T)
1990 return QualType(T, 0);
1991
Douglas Gregor02085352010-03-31 20:19:30 +00001992 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00001993 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001994 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001995 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001996}
1997
Mike Stump11289f42009-09-09 15:08:12 +00001998QualType
John McCallc392f372010-06-11 00:33:02 +00001999ASTContext::getDependentTemplateSpecializationType(
2000 ElaboratedTypeKeyword Keyword,
Douglas Gregor02085352010-03-31 20:19:30 +00002001 NestedNameSpecifier *NNS,
John McCallc392f372010-06-11 00:33:02 +00002002 const IdentifierInfo *Name,
2003 const TemplateArgumentListInfo &Args) {
2004 // TODO: avoid this copy
2005 llvm::SmallVector<TemplateArgument, 16> ArgCopy;
2006 for (unsigned I = 0, E = Args.size(); I != E; ++I)
2007 ArgCopy.push_back(Args[I].getArgument());
2008 return getDependentTemplateSpecializationType(Keyword, NNS, Name,
2009 ArgCopy.size(),
2010 ArgCopy.data());
2011}
2012
2013QualType
2014ASTContext::getDependentTemplateSpecializationType(
2015 ElaboratedTypeKeyword Keyword,
2016 NestedNameSpecifier *NNS,
2017 const IdentifierInfo *Name,
2018 unsigned NumArgs,
2019 const TemplateArgument *Args) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002020 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2021
Douglas Gregorc42075a2010-02-04 18:10:26 +00002022 llvm::FoldingSetNodeID ID;
John McCallc392f372010-06-11 00:33:02 +00002023 DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
2024 Name, NumArgs, Args);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002025
2026 void *InsertPos = 0;
John McCallc392f372010-06-11 00:33:02 +00002027 DependentTemplateSpecializationType *T
2028 = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002029 if (T)
2030 return QualType(T, 0);
2031
John McCallc392f372010-06-11 00:33:02 +00002032 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002033
John McCallc392f372010-06-11 00:33:02 +00002034 ElaboratedTypeKeyword CanonKeyword = Keyword;
2035 if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
2036
2037 bool AnyNonCanonArgs = false;
2038 llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
2039 for (unsigned I = 0; I != NumArgs; ++I) {
2040 CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
2041 if (!CanonArgs[I].structurallyEquals(Args[I]))
2042 AnyNonCanonArgs = true;
Douglas Gregordce2b622009-04-01 00:28:59 +00002043 }
2044
John McCallc392f372010-06-11 00:33:02 +00002045 QualType Canon;
2046 if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
2047 Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
2048 Name, NumArgs,
2049 CanonArgs.data());
2050
2051 // Find the insert position again.
2052 DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2053 }
2054
2055 void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
2056 sizeof(TemplateArgument) * NumArgs),
2057 TypeAlignment);
John McCall773cc982010-06-11 11:07:21 +00002058 T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
John McCallc392f372010-06-11 00:33:02 +00002059 Name, NumArgs, Args, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002060 Types.push_back(T);
John McCallc392f372010-06-11 00:33:02 +00002061 DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002062 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002063}
2064
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002065/// CmpProtocolNames - Comparison predicate for sorting protocols
2066/// alphabetically.
2067static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2068 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002069 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002070}
2071
John McCall8b07ec22010-05-15 11:32:37 +00002072static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00002073 unsigned NumProtocols) {
2074 if (NumProtocols == 0) return true;
2075
2076 for (unsigned i = 1; i != NumProtocols; ++i)
2077 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2078 return false;
2079 return true;
2080}
2081
2082static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002083 unsigned &NumProtocols) {
2084 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002085
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002086 // Sort protocols, keyed by name.
2087 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2088
2089 // Remove duplicates.
2090 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2091 NumProtocols = ProtocolsEnd-Protocols;
2092}
2093
John McCall8b07ec22010-05-15 11:32:37 +00002094QualType ASTContext::getObjCObjectType(QualType BaseType,
2095 ObjCProtocolDecl * const *Protocols,
2096 unsigned NumProtocols) {
2097 // If the base type is an interface and there aren't any protocols
2098 // to add, then the interface type will do just fine.
2099 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2100 return BaseType;
2101
2102 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00002103 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00002104 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00002105 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00002106 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2107 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002108
John McCall8b07ec22010-05-15 11:32:37 +00002109 // Build the canonical type, which has the canonical base type and
2110 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002111 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002112 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2113 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2114 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002115 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2116 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002117 unsigned UniqueCount = NumProtocols;
2118
John McCallfc93cf92009-10-22 22:37:11 +00002119 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002120 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2121 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002122 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002123 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2124 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002125 }
2126
2127 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002128 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2129 }
2130
2131 unsigned Size = sizeof(ObjCObjectTypeImpl);
2132 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2133 void *Mem = Allocate(Size, TypeAlignment);
2134 ObjCObjectTypeImpl *T =
2135 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2136
2137 Types.push_back(T);
2138 ObjCObjectTypes.InsertNode(T, InsertPos);
2139 return QualType(T, 0);
2140}
2141
2142/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2143/// the given object type.
2144QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2145 llvm::FoldingSetNodeID ID;
2146 ObjCObjectPointerType::Profile(ID, ObjectT);
2147
2148 void *InsertPos = 0;
2149 if (ObjCObjectPointerType *QT =
2150 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2151 return QualType(QT, 0);
2152
2153 // Find the canonical object type.
2154 QualType Canonical;
2155 if (!ObjectT.isCanonical()) {
2156 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2157
2158 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002159 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2160 }
2161
Douglas Gregorf85bee62010-02-08 22:59:26 +00002162 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002163 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2164 ObjCObjectPointerType *QType =
2165 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002166
Steve Narofffb4330f2009-06-17 22:40:22 +00002167 Types.push_back(QType);
2168 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002169 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002170}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002171
Steve Naroffc277ad12009-07-18 15:33:26 +00002172/// getObjCInterfaceType - Return the unique reference to the type for the
2173/// specified ObjC interface decl. The list of protocols is optional.
John McCall8b07ec22010-05-15 11:32:37 +00002174QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2175 if (Decl->TypeForDecl)
2176 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002177
John McCall8b07ec22010-05-15 11:32:37 +00002178 // FIXME: redeclarations?
2179 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2180 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2181 Decl->TypeForDecl = T;
2182 Types.push_back(T);
2183 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002184}
2185
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002186/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2187/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002188/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002189/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002190/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002191QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002192 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002193 if (tofExpr->isTypeDependent()) {
2194 llvm::FoldingSetNodeID ID;
2195 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002196
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002197 void *InsertPos = 0;
2198 DependentTypeOfExprType *Canon
2199 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2200 if (Canon) {
2201 // We already have a "canonical" version of an identical, dependent
2202 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002203 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002204 QualType((TypeOfExprType*)Canon, 0));
2205 }
2206 else {
2207 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002208 Canon
2209 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002210 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2211 toe = Canon;
2212 }
2213 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002214 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002215 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002216 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002217 Types.push_back(toe);
2218 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002219}
2220
Steve Naroffa773cd52007-08-01 18:02:17 +00002221/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2222/// TypeOfType AST's. The only motivation to unique these nodes would be
2223/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002224/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002225/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002226QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002227 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002228 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002229 Types.push_back(tot);
2230 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002231}
2232
Anders Carlssonad6bd352009-06-24 21:24:56 +00002233/// getDecltypeForExpr - Given an expr, will return the decltype for that
2234/// expression, according to the rules in C++0x [dcl.type.simple]p4
2235static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002236 if (e->isTypeDependent())
2237 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002238
Anders Carlssonad6bd352009-06-24 21:24:56 +00002239 // If e is an id expression or a class member access, decltype(e) is defined
2240 // as the type of the entity named by e.
2241 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2242 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2243 return VD->getType();
2244 }
2245 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2246 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2247 return FD->getType();
2248 }
2249 // If e is a function call or an invocation of an overloaded operator,
2250 // (parentheses around e are ignored), decltype(e) is defined as the
2251 // return type of that function.
2252 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2253 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002254
Anders Carlssonad6bd352009-06-24 21:24:56 +00002255 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002256
2257 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002258 // defined as T&, otherwise decltype(e) is defined as T.
2259 if (e->isLvalue(Context) == Expr::LV_Valid)
2260 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002261
Anders Carlssonad6bd352009-06-24 21:24:56 +00002262 return T;
2263}
2264
Anders Carlsson81df7b82009-06-24 19:06:50 +00002265/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2266/// DecltypeType AST's. The only motivation to unique these nodes would be
2267/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002268/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002269/// on canonical type's (which are always unique).
2270QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002271 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002272 if (e->isTypeDependent()) {
2273 llvm::FoldingSetNodeID ID;
2274 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002275
Douglas Gregora21f6c32009-07-30 23:36:40 +00002276 void *InsertPos = 0;
2277 DependentDecltypeType *Canon
2278 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2279 if (Canon) {
2280 // We already have a "canonical" version of an equivalent, dependent
2281 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002282 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002283 QualType((DecltypeType*)Canon, 0));
2284 }
2285 else {
2286 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002287 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002288 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2289 dt = Canon;
2290 }
2291 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002292 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002293 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002294 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002295 Types.push_back(dt);
2296 return QualType(dt, 0);
2297}
2298
Chris Lattnerfb072462007-01-23 05:45:31 +00002299/// getTagDeclType - Return the unique reference to the type for the
2300/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002301QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002302 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002303 // FIXME: What is the design on getTagDeclType when it requires casting
2304 // away const? mutable?
2305 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002306}
2307
Mike Stump11289f42009-09-09 15:08:12 +00002308/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2309/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2310/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002311CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002312 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002313}
Chris Lattnerfb072462007-01-23 05:45:31 +00002314
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002315/// getSignedWCharType - Return the type of "signed wchar_t".
2316/// Used when in C++, as a GCC extension.
2317QualType ASTContext::getSignedWCharType() const {
2318 // FIXME: derive from "Target" ?
2319 return WCharTy;
2320}
2321
2322/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2323/// Used when in C++, as a GCC extension.
2324QualType ASTContext::getUnsignedWCharType() const {
2325 // FIXME: derive from "Target" ?
2326 return UnsignedIntTy;
2327}
2328
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002329/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2330/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2331QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002332 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002333}
2334
Chris Lattnera21ad802008-04-02 05:18:44 +00002335//===----------------------------------------------------------------------===//
2336// Type Operators
2337//===----------------------------------------------------------------------===//
2338
John McCallfc93cf92009-10-22 22:37:11 +00002339CanQualType ASTContext::getCanonicalParamType(QualType T) {
2340 // Push qualifiers into arrays, and then discard any remaining
2341 // qualifiers.
2342 T = getCanonicalType(T);
2343 const Type *Ty = T.getTypePtr();
2344
2345 QualType Result;
2346 if (isa<ArrayType>(Ty)) {
2347 Result = getArrayDecayedType(QualType(Ty,0));
2348 } else if (isa<FunctionType>(Ty)) {
2349 Result = getPointerType(QualType(Ty, 0));
2350 } else {
2351 Result = QualType(Ty, 0);
2352 }
2353
2354 return CanQualType::CreateUnsafe(Result);
2355}
2356
Chris Lattnered0d0792008-04-06 22:41:35 +00002357/// getCanonicalType - Return the canonical (structural) type corresponding to
2358/// the specified potentially non-canonical type. The non-canonical version
2359/// of a type may have many "decorated" versions of types. Decorators can
2360/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2361/// to be free of any of these, allowing two canonical types to be compared
2362/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002363CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002364 QualifierCollector Quals;
2365 const Type *Ptr = Quals.strip(T);
2366 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002367
John McCall8ccfcb52009-09-24 19:53:00 +00002368 // The canonical internal type will be the canonical type *except*
2369 // that we push type qualifiers down through array types.
2370
2371 // If there are no new qualifiers to push down, stop here.
2372 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002373 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002374
John McCall8ccfcb52009-09-24 19:53:00 +00002375 // If the type qualifiers are on an array type, get the canonical
2376 // type of the array with the qualifiers applied to the element
2377 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002378 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2379 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002380 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002381
Chris Lattner7adf0762008-08-04 07:31:14 +00002382 // Get the canonical version of the element with the extra qualifiers on it.
2383 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002384 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002385 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002386
Chris Lattner7adf0762008-08-04 07:31:14 +00002387 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002388 return CanQualType::CreateUnsafe(
2389 getConstantArrayType(NewEltTy, CAT->getSize(),
2390 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002391 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002392 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002393 return CanQualType::CreateUnsafe(
2394 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002395 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002396
Douglas Gregor4619e432008-12-05 23:32:09 +00002397 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002398 return CanQualType::CreateUnsafe(
2399 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002400 DSAT->getSizeExpr() ?
2401 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002402 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002403 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002404 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002405
Chris Lattner7adf0762008-08-04 07:31:14 +00002406 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002407 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002408 VAT->getSizeExpr() ?
2409 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002410 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002411 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002412 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002413}
2414
Chandler Carruth607f38e2009-12-29 07:16:59 +00002415QualType ASTContext::getUnqualifiedArrayType(QualType T,
2416 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002417 Quals = T.getQualifiers();
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002418 const ArrayType *AT = getAsArrayType(T);
2419 if (!AT) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002420 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002421 }
2422
Chandler Carruth607f38e2009-12-29 07:16:59 +00002423 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002424 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002425 if (Elt == UnqualElt)
2426 return T;
2427
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002428 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002429 return getConstantArrayType(UnqualElt, CAT->getSize(),
2430 CAT->getSizeModifier(), 0);
2431 }
2432
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002433 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002434 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2435 }
2436
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002437 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2438 return getVariableArrayType(UnqualElt,
2439 VAT->getSizeExpr() ?
2440 VAT->getSizeExpr()->Retain() : 0,
2441 VAT->getSizeModifier(),
2442 VAT->getIndexTypeCVRQualifiers(),
2443 VAT->getBracketsRange());
2444 }
2445
2446 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002447 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2448 DSAT->getSizeModifier(), 0,
2449 SourceRange());
2450}
2451
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002452/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2453/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2454/// they point to and return true. If T1 and T2 aren't pointer types
2455/// or pointer-to-member types, or if they are not similar at this
2456/// level, returns false and leaves T1 and T2 unchanged. Top-level
2457/// qualifiers on T1 and T2 are ignored. This function will typically
2458/// be called in a loop that successively "unwraps" pointer and
2459/// pointer-to-member types to compare them at each level.
2460bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2461 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2462 *T2PtrType = T2->getAs<PointerType>();
2463 if (T1PtrType && T2PtrType) {
2464 T1 = T1PtrType->getPointeeType();
2465 T2 = T2PtrType->getPointeeType();
2466 return true;
2467 }
2468
2469 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2470 *T2MPType = T2->getAs<MemberPointerType>();
2471 if (T1MPType && T2MPType &&
2472 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2473 QualType(T2MPType->getClass(), 0))) {
2474 T1 = T1MPType->getPointeeType();
2475 T2 = T2MPType->getPointeeType();
2476 return true;
2477 }
2478
2479 if (getLangOptions().ObjC1) {
2480 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2481 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2482 if (T1OPType && T2OPType) {
2483 T1 = T1OPType->getPointeeType();
2484 T2 = T2OPType->getPointeeType();
2485 return true;
2486 }
2487 }
2488
2489 // FIXME: Block pointers, too?
2490
2491 return false;
2492}
2493
John McCall847e2a12009-11-24 18:42:40 +00002494DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2495 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2496 return TD->getDeclName();
2497
2498 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2499 if (DTN->isIdentifier()) {
2500 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2501 } else {
2502 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2503 }
2504 }
2505
John McCalld28ae272009-12-02 08:04:21 +00002506 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2507 assert(Storage);
2508 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002509}
2510
Douglas Gregor6bc50582009-05-07 06:41:52 +00002511TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002512 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2513 if (TemplateTemplateParmDecl *TTP
2514 = dyn_cast<TemplateTemplateParmDecl>(Template))
2515 Template = getCanonicalTemplateTemplateParmDecl(TTP);
2516
2517 // The canonical template name is the canonical template declaration.
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002518 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor7dbfb462010-06-16 21:09:37 +00002519 }
Douglas Gregor6bc50582009-05-07 06:41:52 +00002520
John McCalld28ae272009-12-02 08:04:21 +00002521 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002522
Douglas Gregor6bc50582009-05-07 06:41:52 +00002523 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2524 assert(DTN && "Non-dependent template names must refer to template decls.");
2525 return DTN->CanonicalTemplateName;
2526}
2527
Douglas Gregoradee3e32009-11-11 23:06:43 +00002528bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2529 X = getCanonicalTemplateName(X);
2530 Y = getCanonicalTemplateName(Y);
2531 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2532}
2533
Mike Stump11289f42009-09-09 15:08:12 +00002534TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002535ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2536 switch (Arg.getKind()) {
2537 case TemplateArgument::Null:
2538 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002539
Douglas Gregora8e02e72009-07-28 23:00:59 +00002540 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002541 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002542
Douglas Gregora8e02e72009-07-28 23:00:59 +00002543 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002544 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002545
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002546 case TemplateArgument::Template:
2547 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2548
Douglas Gregora8e02e72009-07-28 23:00:59 +00002549 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002550 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002551 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002552
Douglas Gregora8e02e72009-07-28 23:00:59 +00002553 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002554 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002555
Douglas Gregora8e02e72009-07-28 23:00:59 +00002556 case TemplateArgument::Pack: {
2557 // FIXME: Allocate in ASTContext
2558 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2559 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002560 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002561 AEnd = Arg.pack_end();
2562 A != AEnd; (void)++A, ++Idx)
2563 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002564
Douglas Gregora8e02e72009-07-28 23:00:59 +00002565 TemplateArgument Result;
2566 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2567 return Result;
2568 }
2569 }
2570
2571 // Silence GCC warning
2572 assert(false && "Unhandled template argument kind");
2573 return TemplateArgument();
2574}
2575
Douglas Gregor333489b2009-03-27 23:10:48 +00002576NestedNameSpecifier *
2577ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002578 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002579 return 0;
2580
2581 switch (NNS->getKind()) {
2582 case NestedNameSpecifier::Identifier:
2583 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002584 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002585 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2586 NNS->getAsIdentifier());
2587
2588 case NestedNameSpecifier::Namespace:
2589 // A namespace is canonical; build a nested-name-specifier with
2590 // this namespace and no prefix.
2591 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2592
2593 case NestedNameSpecifier::TypeSpec:
2594 case NestedNameSpecifier::TypeSpecWithTemplate: {
2595 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002596 return NestedNameSpecifier::Create(*this, 0,
2597 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002598 T.getTypePtr());
2599 }
2600
2601 case NestedNameSpecifier::Global:
2602 // The global specifier is canonical and unique.
2603 return NNS;
2604 }
2605
2606 // Required to silence a GCC warning
2607 return 0;
2608}
2609
Chris Lattner7adf0762008-08-04 07:31:14 +00002610
2611const ArrayType *ASTContext::getAsArrayType(QualType T) {
2612 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002613 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002614 // Handle the common positive case fast.
2615 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2616 return AT;
2617 }
Mike Stump11289f42009-09-09 15:08:12 +00002618
John McCall8ccfcb52009-09-24 19:53:00 +00002619 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002620 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002621 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002622 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002623
John McCall8ccfcb52009-09-24 19:53:00 +00002624 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002625 // implements C99 6.7.3p8: "If the specification of an array type includes
2626 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002627
Chris Lattner7adf0762008-08-04 07:31:14 +00002628 // If we get here, we either have type qualifiers on the type, or we have
2629 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002630 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002631
John McCall8ccfcb52009-09-24 19:53:00 +00002632 QualifierCollector Qs;
2633 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002634
Chris Lattner7adf0762008-08-04 07:31:14 +00002635 // If we have a simple case, just return now.
2636 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002637 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002638 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002639
Chris Lattner7adf0762008-08-04 07:31:14 +00002640 // Otherwise, we have an array and we have qualifiers on it. Push the
2641 // qualifiers into the array element type and return a new array type.
2642 // Get the canonical version of the element with the extra qualifiers on it.
2643 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002644 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002645
Chris Lattner7adf0762008-08-04 07:31:14 +00002646 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2647 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2648 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002649 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002650 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2651 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2652 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002653 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002654
Mike Stump11289f42009-09-09 15:08:12 +00002655 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002656 = dyn_cast<DependentSizedArrayType>(ATy))
2657 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002658 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002659 DSAT->getSizeExpr() ?
2660 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002661 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002662 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002663 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002664
Chris Lattner7adf0762008-08-04 07:31:14 +00002665 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002666 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002667 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002668 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002669 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002670 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002671 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002672}
2673
2674
Chris Lattnera21ad802008-04-02 05:18:44 +00002675/// getArrayDecayedType - Return the properly qualified result of decaying the
2676/// specified array type to a pointer. This operation is non-trivial when
2677/// handling typedefs etc. The canonical type of "T" must be an array type,
2678/// this returns a pointer to a properly qualified element of the array.
2679///
2680/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2681QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002682 // Get the element type with 'getAsArrayType' so that we don't lose any
2683 // typedefs in the element type of the array. This also handles propagation
2684 // of type qualifiers from the array type into the element type if present
2685 // (C99 6.7.3p8).
2686 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2687 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002688
Chris Lattner7adf0762008-08-04 07:31:14 +00002689 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002690
2691 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002692 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002693}
2694
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002695QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002696 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002697 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2698 QT = AT->getElementType();
2699 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002700}
2701
Anders Carlsson4bf82142009-09-25 01:23:32 +00002702QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2703 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002704
Anders Carlsson4bf82142009-09-25 01:23:32 +00002705 if (const ArrayType *AT = getAsArrayType(ElemTy))
2706 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002707
Anders Carlssone0808df2008-12-21 03:44:36 +00002708 return ElemTy;
2709}
2710
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002711/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002712uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002713ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2714 uint64_t ElementCount = 1;
2715 do {
2716 ElementCount *= CA->getSize().getZExtValue();
2717 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2718 } while (CA);
2719 return ElementCount;
2720}
2721
Steve Naroff0af91202007-04-27 21:51:21 +00002722/// getFloatingRank - Return a relative rank for floating point types.
2723/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002724static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002725 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002726 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002727
John McCall9dd450b2009-09-21 23:43:11 +00002728 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2729 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002730 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002731 case BuiltinType::Float: return FloatRank;
2732 case BuiltinType::Double: return DoubleRank;
2733 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002734 }
2735}
2736
Mike Stump11289f42009-09-09 15:08:12 +00002737/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2738/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002739/// 'typeDomain' is a real floating point or complex type.
2740/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002741QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2742 QualType Domain) const {
2743 FloatingRank EltRank = getFloatingRank(Size);
2744 if (Domain->isComplexType()) {
2745 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002746 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002747 case FloatRank: return FloatComplexTy;
2748 case DoubleRank: return DoubleComplexTy;
2749 case LongDoubleRank: return LongDoubleComplexTy;
2750 }
Steve Naroff0af91202007-04-27 21:51:21 +00002751 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002752
2753 assert(Domain->isRealFloatingType() && "Unknown domain!");
2754 switch (EltRank) {
2755 default: assert(0 && "getFloatingRank(): illegal value for rank");
2756 case FloatRank: return FloatTy;
2757 case DoubleRank: return DoubleTy;
2758 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002759 }
Steve Naroffe4718892007-04-27 18:30:00 +00002760}
2761
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002762/// getFloatingTypeOrder - Compare the rank of the two specified floating
2763/// point types, ignoring the domain of the type (i.e. 'double' ==
2764/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002765/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002766int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2767 FloatingRank LHSR = getFloatingRank(LHS);
2768 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002769
Chris Lattnerb90739d2008-04-06 23:38:49 +00002770 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002771 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002772 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002773 return 1;
2774 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002775}
2776
Chris Lattner76a00cf2008-04-06 22:59:24 +00002777/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2778/// routine will assert if passed a built-in type that isn't an integer or enum,
2779/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002780unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002781 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002782 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002783 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002784
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002785 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2786 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2787
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002788 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2789 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2790
2791 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2792 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2793
Chris Lattner76a00cf2008-04-06 22:59:24 +00002794 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002795 default: assert(0 && "getIntegerRank(): not a built-in integer");
2796 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002797 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002798 case BuiltinType::Char_S:
2799 case BuiltinType::Char_U:
2800 case BuiltinType::SChar:
2801 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002802 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002803 case BuiltinType::Short:
2804 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002805 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002806 case BuiltinType::Int:
2807 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002808 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002809 case BuiltinType::Long:
2810 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002811 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002812 case BuiltinType::LongLong:
2813 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002814 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002815 case BuiltinType::Int128:
2816 case BuiltinType::UInt128:
2817 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002818 }
2819}
2820
Eli Friedman629ffb92009-08-20 04:21:42 +00002821/// \brief Whether this is a promotable bitfield reference according
2822/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2823///
2824/// \returns the type this bit-field will promote to, or NULL if no
2825/// promotion occurs.
2826QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00002827 if (E->isTypeDependent() || E->isValueDependent())
2828 return QualType();
2829
Eli Friedman629ffb92009-08-20 04:21:42 +00002830 FieldDecl *Field = E->getBitField();
2831 if (!Field)
2832 return QualType();
2833
2834 QualType FT = Field->getType();
2835
2836 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2837 uint64_t BitWidth = BitWidthAP.getZExtValue();
2838 uint64_t IntSize = getTypeSize(IntTy);
2839 // GCC extension compatibility: if the bit-field size is less than or equal
2840 // to the size of int, it gets promoted no matter what its type is.
2841 // For instance, unsigned long bf : 4 gets promoted to signed int.
2842 if (BitWidth < IntSize)
2843 return IntTy;
2844
2845 if (BitWidth == IntSize)
2846 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2847
2848 // Types bigger than int are not subject to promotions, and therefore act
2849 // like the base type.
2850 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2851 // is ridiculous.
2852 return QualType();
2853}
2854
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002855/// getPromotedIntegerType - Returns the type that Promotable will
2856/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2857/// integer type.
2858QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2859 assert(!Promotable.isNull());
2860 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002861 if (const EnumType *ET = Promotable->getAs<EnumType>())
2862 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002863 if (Promotable->isSignedIntegerType())
2864 return IntTy;
2865 uint64_t PromotableSize = getTypeSize(Promotable);
2866 uint64_t IntSize = getTypeSize(IntTy);
2867 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2868 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2869}
2870
Mike Stump11289f42009-09-09 15:08:12 +00002871/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002872/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002873/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002874int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002875 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2876 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002877 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002878
Chris Lattner76a00cf2008-04-06 22:59:24 +00002879 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2880 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002881
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002882 unsigned LHSRank = getIntegerRank(LHSC);
2883 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002884
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002885 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2886 if (LHSRank == RHSRank) return 0;
2887 return LHSRank > RHSRank ? 1 : -1;
2888 }
Mike Stump11289f42009-09-09 15:08:12 +00002889
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002890 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2891 if (LHSUnsigned) {
2892 // If the unsigned [LHS] type is larger, return it.
2893 if (LHSRank >= RHSRank)
2894 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002895
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002896 // If the signed type can represent all values of the unsigned type, it
2897 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002898 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002899 return -1;
2900 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002901
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002902 // If the unsigned [RHS] type is larger, return it.
2903 if (RHSRank >= LHSRank)
2904 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002905
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002906 // If the signed type can represent all values of the unsigned type, it
2907 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002908 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002909 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002910}
Anders Carlsson98f07902007-08-17 05:31:46 +00002911
Anders Carlsson6d417272009-11-14 21:45:58 +00002912static RecordDecl *
2913CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2914 SourceLocation L, IdentifierInfo *Id) {
2915 if (Ctx.getLangOptions().CPlusPlus)
2916 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2917 else
2918 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2919}
2920
Mike Stump11289f42009-09-09 15:08:12 +00002921// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002922QualType ASTContext::getCFConstantStringType() {
2923 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002924 CFConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002925 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002926 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002927 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002928
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002929 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002930
Anders Carlsson98f07902007-08-17 05:31:46 +00002931 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002932 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002933 // int flags;
2934 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002935 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002936 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002937 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002938 FieldTypes[3] = LongTy;
2939
Anders Carlsson98f07902007-08-17 05:31:46 +00002940 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002941 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002942 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002943 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002944 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002945 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002946 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002947 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002948 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002949 }
2950
Douglas Gregord5058122010-02-11 01:19:42 +00002951 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002952 }
Mike Stump11289f42009-09-09 15:08:12 +00002953
Anders Carlsson98f07902007-08-17 05:31:46 +00002954 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002955}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002956
Douglas Gregor512b0772009-04-23 22:29:11 +00002957void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002958 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002959 assert(Rec && "Invalid CFConstantStringType");
2960 CFConstantStringTypeDecl = Rec->getDecl();
2961}
2962
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002963// getNSConstantStringType - Return the type used for constant NSStrings.
2964QualType ASTContext::getNSConstantStringType() {
2965 if (!NSConstantStringTypeDecl) {
2966 NSConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002967 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002968 &Idents.get("__builtin_NSString"));
2969 NSConstantStringTypeDecl->startDefinition();
2970
2971 QualType FieldTypes[3];
2972
2973 // const int *isa;
2974 FieldTypes[0] = getPointerType(IntTy.withConst());
2975 // const char *str;
2976 FieldTypes[1] = getPointerType(CharTy.withConst());
2977 // unsigned int length;
2978 FieldTypes[2] = UnsignedIntTy;
2979
2980 // Create fields
2981 for (unsigned i = 0; i < 3; ++i) {
2982 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2983 SourceLocation(), 0,
2984 FieldTypes[i], /*TInfo=*/0,
2985 /*BitWidth=*/0,
2986 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002987 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002988 NSConstantStringTypeDecl->addDecl(Field);
2989 }
2990
2991 NSConstantStringTypeDecl->completeDefinition();
2992 }
2993
2994 return getTagDeclType(NSConstantStringTypeDecl);
2995}
2996
2997void ASTContext::setNSConstantStringType(QualType T) {
2998 const RecordType *Rec = T->getAs<RecordType>();
2999 assert(Rec && "Invalid NSConstantStringType");
3000 NSConstantStringTypeDecl = Rec->getDecl();
3001}
3002
Mike Stump11289f42009-09-09 15:08:12 +00003003QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003004 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00003005 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00003006 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003007 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00003008 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00003009
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003010 QualType FieldTypes[] = {
3011 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00003012 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003013 getPointerType(UnsignedLongTy),
3014 getConstantArrayType(UnsignedLongTy,
3015 llvm::APInt(32, 5), ArrayType::Normal, 0)
3016 };
Mike Stump11289f42009-09-09 15:08:12 +00003017
Douglas Gregor91f84212008-12-11 16:49:14 +00003018 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00003019 FieldDecl *Field = FieldDecl::Create(*this,
3020 ObjCFastEnumerationStateTypeDecl,
3021 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00003022 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00003023 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00003024 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003025 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003026 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00003027 }
Fariborz Jahanian9ea58392010-05-27 16:05:06 +00003028 if (getLangOptions().CPlusPlus)
Fariborz Jahanianc77f0f32010-05-27 16:35:00 +00003029 if (CXXRecordDecl *CXXRD =
3030 dyn_cast<CXXRecordDecl>(ObjCFastEnumerationStateTypeDecl))
Fariborz Jahanian9ea58392010-05-27 16:05:06 +00003031 CXXRD->setEmpty(false);
Mike Stump11289f42009-09-09 15:08:12 +00003032
Douglas Gregord5058122010-02-11 01:19:42 +00003033 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003034 }
Mike Stump11289f42009-09-09 15:08:12 +00003035
Anders Carlssond89ba7d2008-08-30 19:34:46 +00003036 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3037}
3038
Mike Stumpd0153282009-10-20 02:12:22 +00003039QualType ASTContext::getBlockDescriptorType() {
3040 if (BlockDescriptorType)
3041 return getTagDeclType(BlockDescriptorType);
3042
3043 RecordDecl *T;
3044 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003045 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003046 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00003047 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003048
3049 QualType FieldTypes[] = {
3050 UnsignedLongTy,
3051 UnsignedLongTy,
3052 };
3053
3054 const char *FieldNames[] = {
3055 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003056 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00003057 };
3058
3059 for (size_t i = 0; i < 2; ++i) {
3060 FieldDecl *Field = FieldDecl::Create(*this,
3061 T,
3062 SourceLocation(),
3063 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003064 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003065 /*BitWidth=*/0,
3066 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003067 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003068 T->addDecl(Field);
3069 }
3070
Douglas Gregord5058122010-02-11 01:19:42 +00003071 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003072
3073 BlockDescriptorType = T;
3074
3075 return getTagDeclType(BlockDescriptorType);
3076}
3077
3078void ASTContext::setBlockDescriptorType(QualType T) {
3079 const RecordType *Rec = T->getAs<RecordType>();
3080 assert(Rec && "Invalid BlockDescriptorType");
3081 BlockDescriptorType = Rec->getDecl();
3082}
3083
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003084QualType ASTContext::getBlockDescriptorExtendedType() {
3085 if (BlockDescriptorExtendedType)
3086 return getTagDeclType(BlockDescriptorExtendedType);
3087
3088 RecordDecl *T;
3089 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00003090 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003091 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003092 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003093
3094 QualType FieldTypes[] = {
3095 UnsignedLongTy,
3096 UnsignedLongTy,
3097 getPointerType(VoidPtrTy),
3098 getPointerType(VoidPtrTy)
3099 };
3100
3101 const char *FieldNames[] = {
3102 "reserved",
3103 "Size",
3104 "CopyFuncPtr",
3105 "DestroyFuncPtr"
3106 };
3107
3108 for (size_t i = 0; i < 4; ++i) {
3109 FieldDecl *Field = FieldDecl::Create(*this,
3110 T,
3111 SourceLocation(),
3112 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003113 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003114 /*BitWidth=*/0,
3115 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003116 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003117 T->addDecl(Field);
3118 }
3119
Douglas Gregord5058122010-02-11 01:19:42 +00003120 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003121
3122 BlockDescriptorExtendedType = T;
3123
3124 return getTagDeclType(BlockDescriptorExtendedType);
3125}
3126
3127void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3128 const RecordType *Rec = T->getAs<RecordType>();
3129 assert(Rec && "Invalid BlockDescriptorType");
3130 BlockDescriptorExtendedType = Rec->getDecl();
3131}
3132
Mike Stump94967902009-10-21 18:16:27 +00003133bool ASTContext::BlockRequiresCopying(QualType Ty) {
3134 if (Ty->isBlockPointerType())
3135 return true;
3136 if (isObjCNSObjectType(Ty))
3137 return true;
3138 if (Ty->isObjCObjectPointerType())
3139 return true;
3140 return false;
3141}
3142
3143QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3144 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003145 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003146 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003147 // unsigned int __flags;
3148 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003149 // void *__copy_helper; // as needed
3150 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003151 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003152 // } *
3153
Mike Stump94967902009-10-21 18:16:27 +00003154 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3155
3156 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003157 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003158 llvm::SmallString<36> Name;
3159 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3160 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003161 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003162 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003163 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003164 T->startDefinition();
3165 QualType Int32Ty = IntTy;
3166 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3167 QualType FieldTypes[] = {
3168 getPointerType(VoidPtrTy),
3169 getPointerType(getTagDeclType(T)),
3170 Int32Ty,
3171 Int32Ty,
3172 getPointerType(VoidPtrTy),
3173 getPointerType(VoidPtrTy),
3174 Ty
3175 };
3176
3177 const char *FieldNames[] = {
3178 "__isa",
3179 "__forwarding",
3180 "__flags",
3181 "__size",
3182 "__copy_helper",
3183 "__destroy_helper",
3184 DeclName,
3185 };
3186
3187 for (size_t i = 0; i < 7; ++i) {
3188 if (!HasCopyAndDispose && i >=4 && i <= 5)
3189 continue;
3190 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3191 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003192 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003193 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003194 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003195 T->addDecl(Field);
3196 }
3197
Douglas Gregord5058122010-02-11 01:19:42 +00003198 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003199
3200 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003201}
3202
3203
3204QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003205 bool BlockHasCopyDispose,
John McCall87fe5d52010-05-20 01:18:31 +00003206 llvm::SmallVectorImpl<const Expr *> &Layout) {
3207
Mike Stumpd0153282009-10-20 02:12:22 +00003208 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003209 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003210 llvm::SmallString<36> Name;
3211 llvm::raw_svector_ostream(Name) << "__block_literal_"
3212 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003213 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003214 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003215 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003216 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003217 QualType FieldTypes[] = {
3218 getPointerType(VoidPtrTy),
3219 IntTy,
3220 IntTy,
3221 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003222 (BlockHasCopyDispose ?
3223 getPointerType(getBlockDescriptorExtendedType()) :
3224 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003225 };
3226
3227 const char *FieldNames[] = {
3228 "__isa",
3229 "__flags",
3230 "__reserved",
3231 "__FuncPtr",
3232 "__descriptor"
3233 };
3234
3235 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003236 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003237 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003238 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003239 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003240 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003241 T->addDecl(Field);
3242 }
3243
John McCall87fe5d52010-05-20 01:18:31 +00003244 for (unsigned i = 0; i < Layout.size(); ++i) {
3245 const Expr *E = Layout[i];
Mike Stump7fe9cc12009-10-21 03:49:08 +00003246
John McCall87fe5d52010-05-20 01:18:31 +00003247 QualType FieldType = E->getType();
3248 IdentifierInfo *FieldName = 0;
3249 if (isa<CXXThisExpr>(E)) {
3250 FieldName = &Idents.get("this");
3251 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3252 const ValueDecl *D = BDRE->getDecl();
3253 FieldName = D->getIdentifier();
3254 if (BDRE->isByRef())
3255 FieldType = BuildByRefType(D->getNameAsCString(), FieldType);
3256 } else {
3257 // Padding.
3258 assert(isa<ConstantArrayType>(FieldType) &&
3259 isa<DeclRefExpr>(E) &&
3260 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3261 "doesn't match characteristics of padding decl");
3262 }
Mike Stump7fe9cc12009-10-21 03:49:08 +00003263
3264 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCall87fe5d52010-05-20 01:18:31 +00003265 FieldName, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003266 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003267 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003268 T->addDecl(Field);
3269 }
3270
Douglas Gregord5058122010-02-11 01:19:42 +00003271 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003272
3273 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003274}
3275
Douglas Gregor512b0772009-04-23 22:29:11 +00003276void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003277 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003278 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3279 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3280}
3281
Anders Carlsson18acd442007-10-29 06:33:42 +00003282// This returns true if a type has been typedefed to BOOL:
3283// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003284static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003285 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003286 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3287 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003288
Anders Carlssond8499822007-10-29 05:01:08 +00003289 return false;
3290}
3291
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003292/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003293/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003294CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003295 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003296
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003297 // Make all integer and enum types at least as large as an int
Douglas Gregorb90df602010-06-16 00:17:44 +00003298 if (sz.isPositive() && type->isIntegralOrEnumerationType())
Ken Dyck40775002010-01-11 17:06:35 +00003299 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003300 // Treat arrays as pointers, since that's how they're passed in.
3301 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003302 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003303 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003304}
3305
3306static inline
3307std::string charUnitsToString(const CharUnits &CU) {
3308 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003309}
3310
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003311/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003312/// declaration.
3313void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3314 std::string& S) {
3315 const BlockDecl *Decl = Expr->getBlockDecl();
3316 QualType BlockTy =
3317 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3318 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003319 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003320 // Compute size of all parameters.
3321 // Start with computing size of a pointer in number of bytes.
3322 // FIXME: There might(should) be a better way of doing this computation!
3323 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003324 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3325 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003326 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003327 E = Decl->param_end(); PI != E; ++PI) {
3328 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003329 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003330 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003331 ParmOffset += sz;
3332 }
3333 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003334 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003335 // Block pointer and offset.
3336 S += "@?0";
3337 ParmOffset = PtrSize;
3338
3339 // Argument types.
3340 ParmOffset = PtrSize;
3341 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3342 Decl->param_end(); PI != E; ++PI) {
3343 ParmVarDecl *PVDecl = *PI;
3344 QualType PType = PVDecl->getOriginalType();
3345 if (const ArrayType *AT =
3346 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3347 // Use array's original type only if it has known number of
3348 // elements.
3349 if (!isa<ConstantArrayType>(AT))
3350 PType = PVDecl->getType();
3351 } else if (PType->isFunctionType())
3352 PType = PVDecl->getType();
3353 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003354 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003355 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003356 }
3357}
3358
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003359/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003360/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003361void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003362 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003363 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003364 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003365 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003366 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003367 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003368 // Compute size of all parameters.
3369 // Start with computing size of a pointer in number of bytes.
3370 // FIXME: There might(should) be a better way of doing this computation!
3371 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003372 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003373 // The first two arguments (self and _cmd) are pointers; account for
3374 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003375 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003376 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003377 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003378 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003379 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003380 assert (sz.isPositive() &&
3381 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003382 ParmOffset += sz;
3383 }
Ken Dyck40775002010-01-11 17:06:35 +00003384 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003385 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003386 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003387
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003388 // Argument types.
3389 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003390 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003391 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003392 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003393 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003394 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003395 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3396 // Use array's original type only if it has known number of
3397 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003398 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003399 PType = PVDecl->getType();
3400 } else if (PType->isFunctionType())
3401 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003402 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003403 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003404 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003405 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003406 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003407 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003408 }
3409}
3410
Daniel Dunbar4932b362008-08-28 04:38:10 +00003411/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003412/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003413/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3414/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003415/// Property attributes are stored as a comma-delimited C string. The simple
3416/// attributes readonly and bycopy are encoded as single characters. The
3417/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3418/// encoded as single characters, followed by an identifier. Property types
3419/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003420/// these attributes are defined by the following enumeration:
3421/// @code
3422/// enum PropertyAttributes {
3423/// kPropertyReadOnly = 'R', // property is read-only.
3424/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3425/// kPropertyByref = '&', // property is a reference to the value last assigned
3426/// kPropertyDynamic = 'D', // property is dynamic
3427/// kPropertyGetter = 'G', // followed by getter selector name
3428/// kPropertySetter = 'S', // followed by setter selector name
3429/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3430/// kPropertyType = 't' // followed by old-style type encoding.
3431/// kPropertyWeak = 'W' // 'weak' property
3432/// kPropertyStrong = 'P' // property GC'able
3433/// kPropertyNonAtomic = 'N' // property non-atomic
3434/// };
3435/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003436void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003437 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003438 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003439 // Collect information from the property implementation decl(s).
3440 bool Dynamic = false;
3441 ObjCPropertyImplDecl *SynthesizePID = 0;
3442
3443 // FIXME: Duplicated code due to poor abstraction.
3444 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003445 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003446 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3447 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003448 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003449 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003450 ObjCPropertyImplDecl *PID = *i;
3451 if (PID->getPropertyDecl() == PD) {
3452 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3453 Dynamic = true;
3454 } else {
3455 SynthesizePID = PID;
3456 }
3457 }
3458 }
3459 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003460 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003461 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003462 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003463 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003464 ObjCPropertyImplDecl *PID = *i;
3465 if (PID->getPropertyDecl() == PD) {
3466 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3467 Dynamic = true;
3468 } else {
3469 SynthesizePID = PID;
3470 }
3471 }
Mike Stump11289f42009-09-09 15:08:12 +00003472 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003473 }
3474 }
3475
3476 // FIXME: This is not very efficient.
3477 S = "T";
3478
3479 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003480 // GCC has some special rules regarding encoding of properties which
3481 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003482 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003483 true /* outermost type */,
3484 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003485
3486 if (PD->isReadOnly()) {
3487 S += ",R";
3488 } else {
3489 switch (PD->getSetterKind()) {
3490 case ObjCPropertyDecl::Assign: break;
3491 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003492 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003493 }
3494 }
3495
3496 // It really isn't clear at all what this means, since properties
3497 // are "dynamic by default".
3498 if (Dynamic)
3499 S += ",D";
3500
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003501 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3502 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003503
Daniel Dunbar4932b362008-08-28 04:38:10 +00003504 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3505 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003506 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003507 }
3508
3509 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3510 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003511 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003512 }
3513
3514 if (SynthesizePID) {
3515 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3516 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003517 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003518 }
3519
3520 // FIXME: OBJCGC: weak & strong
3521}
3522
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003523/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003524/// Another legacy compatibility encoding: 32-bit longs are encoded as
3525/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003526/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3527///
3528void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003529 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003530 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003531 if (BT->getKind() == BuiltinType::ULong &&
3532 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003533 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003534 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003535 if (BT->getKind() == BuiltinType::Long &&
3536 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003537 PointeeTy = IntTy;
3538 }
3539 }
3540}
3541
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003542void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003543 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003544 // We follow the behavior of gcc, expanding structures which are
3545 // directly pointed to, and expanding embedded structures. Note that
3546 // these rules are sufficient to prevent recursive encoding of the
3547 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003548 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003549 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003550}
3551
David Chisnallb190a2c2010-06-04 01:10:52 +00003552static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3553 switch (T->getAs<BuiltinType>()->getKind()) {
3554 default: assert(0 && "Unhandled builtin type kind");
3555 case BuiltinType::Void: return 'v';
3556 case BuiltinType::Bool: return 'B';
3557 case BuiltinType::Char_U:
3558 case BuiltinType::UChar: return 'C';
3559 case BuiltinType::UShort: return 'S';
3560 case BuiltinType::UInt: return 'I';
3561 case BuiltinType::ULong:
3562 return
3563 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3564 case BuiltinType::UInt128: return 'T';
3565 case BuiltinType::ULongLong: return 'Q';
3566 case BuiltinType::Char_S:
3567 case BuiltinType::SChar: return 'c';
3568 case BuiltinType::Short: return 's';
John McCalldad856d2010-06-11 10:11:05 +00003569 case BuiltinType::WChar:
David Chisnallb190a2c2010-06-04 01:10:52 +00003570 case BuiltinType::Int: return 'i';
3571 case BuiltinType::Long:
3572 return
3573 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3574 case BuiltinType::LongLong: return 'q';
3575 case BuiltinType::Int128: return 't';
3576 case BuiltinType::Float: return 'f';
3577 case BuiltinType::Double: return 'd';
3578 case BuiltinType::LongDouble: return 'd';
3579 }
3580}
3581
Mike Stump11289f42009-09-09 15:08:12 +00003582static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00003583 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003584 const Expr *E = FD->getBitWidth();
3585 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3586 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003587 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00003588 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3589 // The GNU runtime requires more information; bitfields are encoded as b,
3590 // then the offset (in bits) of the first element, then the type of the
3591 // bitfield, then the size in bits. For example, in this structure:
3592 //
3593 // struct
3594 // {
3595 // int integer;
3596 // int flags:2;
3597 // };
3598 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3599 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3600 // information is not especially sensible, but we're stuck with it for
3601 // compatibility with GCC, although providing it breaks anything that
3602 // actually uses runtime introspection and wants to work on both runtimes...
3603 if (!Ctx->getLangOptions().NeXTRuntime) {
3604 const RecordDecl *RD = FD->getParent();
3605 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3606 // FIXME: This same linear search is also used in ExprConstant - it might
3607 // be better if the FieldDecl stored its offset. We'd be increasing the
3608 // size of the object slightly, but saving some time every time it is used.
3609 unsigned i = 0;
3610 for (RecordDecl::field_iterator Field = RD->field_begin(),
3611 FieldEnd = RD->field_end();
3612 Field != FieldEnd; (void)++Field, ++i) {
3613 if (*Field == FD)
3614 break;
3615 }
3616 S += llvm::utostr(RL.getFieldOffset(i));
3617 S += ObjCEncodingForPrimitiveKind(Context, T);
3618 }
3619 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003620 S += llvm::utostr(N);
3621}
3622
Daniel Dunbar07d07852009-10-18 21:17:35 +00003623// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003624void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3625 bool ExpandPointedToStructures,
3626 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003627 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003628 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003629 bool EncodingProperty) {
David Chisnallb190a2c2010-06-04 01:10:52 +00003630 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003631 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003632 return EncodeBitField(this, S, T, FD);
3633 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003634 return;
3635 }
Mike Stump11289f42009-09-09 15:08:12 +00003636
John McCall9dd450b2009-09-21 23:43:11 +00003637 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003638 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003639 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003640 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003641 return;
3642 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003643
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003644 // encoding for pointer or r3eference types.
3645 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003646 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003647 if (PT->isObjCSelType()) {
3648 S += ':';
3649 return;
3650 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003651 PointeeTy = PT->getPointeeType();
3652 }
3653 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3654 PointeeTy = RT->getPointeeType();
3655 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003656 bool isReadOnly = false;
3657 // For historical/compatibility reasons, the read-only qualifier of the
3658 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3659 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003660 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003661 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003662 if (OutermostType && T.isConstQualified()) {
3663 isReadOnly = true;
3664 S += 'r';
3665 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003666 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003667 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003668 while (P->getAs<PointerType>())
3669 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003670 if (P.isConstQualified()) {
3671 isReadOnly = true;
3672 S += 'r';
3673 }
3674 }
3675 if (isReadOnly) {
3676 // Another legacy compatibility encoding. Some ObjC qualifier and type
3677 // combinations need to be rearranged.
3678 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003679 if (llvm::StringRef(S).endswith("nr"))
3680 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003681 }
Mike Stump11289f42009-09-09 15:08:12 +00003682
Anders Carlssond8499822007-10-29 05:01:08 +00003683 if (PointeeTy->isCharType()) {
3684 // char pointer types should be encoded as '*' unless it is a
3685 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003686 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003687 S += '*';
3688 return;
3689 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003690 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003691 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3692 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3693 S += '#';
3694 return;
3695 }
3696 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3697 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3698 S += '@';
3699 return;
3700 }
3701 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003702 }
Anders Carlssond8499822007-10-29 05:01:08 +00003703 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003704 getLegacyIntegralTypeEncoding(PointeeTy);
3705
Mike Stump11289f42009-09-09 15:08:12 +00003706 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003707 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003708 return;
3709 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003710
Chris Lattnere7cabb92009-07-13 00:10:46 +00003711 if (const ArrayType *AT =
3712 // Ignore type qualifiers etc.
3713 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003714 if (isa<IncompleteArrayType>(AT)) {
3715 // Incomplete arrays are encoded as a pointer to the array element.
3716 S += '^';
3717
Mike Stump11289f42009-09-09 15:08:12 +00003718 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003719 false, ExpandStructures, FD);
3720 } else {
3721 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003722
Anders Carlssond05f44b2009-02-22 01:38:57 +00003723 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3724 S += llvm::utostr(CAT->getSize().getZExtValue());
3725 else {
3726 //Variable length arrays are encoded as a regular array with 0 elements.
3727 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3728 S += '0';
3729 }
Mike Stump11289f42009-09-09 15:08:12 +00003730
3731 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003732 false, ExpandStructures, FD);
3733 S += ']';
3734 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003735 return;
3736 }
Mike Stump11289f42009-09-09 15:08:12 +00003737
John McCall9dd450b2009-09-21 23:43:11 +00003738 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003739 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003740 return;
3741 }
Mike Stump11289f42009-09-09 15:08:12 +00003742
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003743 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003744 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003745 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003746 // Anonymous structures print as '?'
3747 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3748 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003749 if (ClassTemplateSpecializationDecl *Spec
3750 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3751 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3752 std::string TemplateArgsStr
3753 = TemplateSpecializationType::PrintTemplateArgumentList(
3754 TemplateArgs.getFlatArgumentList(),
3755 TemplateArgs.flat_size(),
3756 (*this).PrintingPolicy);
3757
3758 S += TemplateArgsStr;
3759 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003760 } else {
3761 S += '?';
3762 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003763 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003764 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003765 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3766 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003767 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003768 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003769 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003770 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003771 S += '"';
3772 }
Mike Stump11289f42009-09-09 15:08:12 +00003773
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003774 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003775 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003776 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003777 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003778 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003779 QualType qt = Field->getType();
3780 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003781 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003782 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003783 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003784 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003785 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003786 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003787 return;
3788 }
Mike Stump11289f42009-09-09 15:08:12 +00003789
Chris Lattnere7cabb92009-07-13 00:10:46 +00003790 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003791 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003792 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003793 else
3794 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003795 return;
3796 }
Mike Stump11289f42009-09-09 15:08:12 +00003797
Chris Lattnere7cabb92009-07-13 00:10:46 +00003798 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003799 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003800 return;
3801 }
Mike Stump11289f42009-09-09 15:08:12 +00003802
John McCall8b07ec22010-05-15 11:32:37 +00003803 // Ignore protocol qualifiers when mangling at this level.
3804 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3805 T = OT->getBaseType();
3806
John McCall8ccfcb52009-09-24 19:53:00 +00003807 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003808 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003809 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003810 S += '{';
3811 const IdentifierInfo *II = OI->getIdentifier();
3812 S += II->getName();
3813 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003814 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003815 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003816 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003817 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003818 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003819 RecFields[i]);
3820 else
Mike Stump11289f42009-09-09 15:08:12 +00003821 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003822 FD);
3823 }
3824 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003825 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003826 }
Mike Stump11289f42009-09-09 15:08:12 +00003827
John McCall9dd450b2009-09-21 23:43:11 +00003828 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003829 if (OPT->isObjCIdType()) {
3830 S += '@';
3831 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003832 }
Mike Stump11289f42009-09-09 15:08:12 +00003833
Steve Narofff0c86112009-10-28 22:03:49 +00003834 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3835 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3836 // Since this is a binary compatibility issue, need to consult with runtime
3837 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003838 S += '#';
3839 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003840 }
Mike Stump11289f42009-09-09 15:08:12 +00003841
Chris Lattnere7cabb92009-07-13 00:10:46 +00003842 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003843 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003844 ExpandPointedToStructures,
3845 ExpandStructures, FD);
3846 if (FD || EncodingProperty) {
3847 // Note that we do extended encoding of protocol qualifer list
3848 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003849 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003850 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3851 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003852 S += '<';
3853 S += (*I)->getNameAsString();
3854 S += '>';
3855 }
3856 S += '"';
3857 }
3858 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003859 }
Mike Stump11289f42009-09-09 15:08:12 +00003860
Chris Lattnere7cabb92009-07-13 00:10:46 +00003861 QualType PointeeTy = OPT->getPointeeType();
3862 if (!EncodingProperty &&
3863 isa<TypedefType>(PointeeTy.getTypePtr())) {
3864 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003865 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003866 // {...};
3867 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003868 getObjCEncodingForTypeImpl(PointeeTy, S,
3869 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003870 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003871 return;
3872 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003873
3874 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003875 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003876 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003877 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003878 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3879 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003880 S += '<';
3881 S += (*I)->getNameAsString();
3882 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003883 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003884 S += '"';
3885 }
3886 return;
3887 }
Mike Stump11289f42009-09-09 15:08:12 +00003888
John McCalla9e6e8d2010-05-17 23:56:34 +00003889 // gcc just blithely ignores member pointers.
3890 // TODO: maybe there should be a mangling for these
3891 if (T->getAs<MemberPointerType>())
3892 return;
3893
Chris Lattnere7cabb92009-07-13 00:10:46 +00003894 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003895}
3896
Mike Stump11289f42009-09-09 15:08:12 +00003897void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003898 std::string& S) const {
3899 if (QT & Decl::OBJC_TQ_In)
3900 S += 'n';
3901 if (QT & Decl::OBJC_TQ_Inout)
3902 S += 'N';
3903 if (QT & Decl::OBJC_TQ_Out)
3904 S += 'o';
3905 if (QT & Decl::OBJC_TQ_Bycopy)
3906 S += 'O';
3907 if (QT & Decl::OBJC_TQ_Byref)
3908 S += 'R';
3909 if (QT & Decl::OBJC_TQ_Oneway)
3910 S += 'V';
3911}
3912
Chris Lattnere7cabb92009-07-13 00:10:46 +00003913void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003914 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003915
Anders Carlsson87c149b2007-10-11 01:00:40 +00003916 BuiltinVaListType = T;
3917}
3918
Chris Lattnere7cabb92009-07-13 00:10:46 +00003919void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003920 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003921}
3922
Chris Lattnere7cabb92009-07-13 00:10:46 +00003923void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003924 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003925}
3926
Chris Lattnere7cabb92009-07-13 00:10:46 +00003927void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003928 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003929}
3930
Chris Lattnere7cabb92009-07-13 00:10:46 +00003931void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003932 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003933}
3934
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003935void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003936 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003937 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003938
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003939 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003940}
3941
John McCalld28ae272009-12-02 08:04:21 +00003942/// \brief Retrieve the template name that corresponds to a non-empty
3943/// lookup.
John McCallad371252010-01-20 00:46:10 +00003944TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3945 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003946 unsigned size = End - Begin;
3947 assert(size > 1 && "set is not overloaded!");
3948
3949 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3950 size * sizeof(FunctionTemplateDecl*));
3951 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3952
3953 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003954 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003955 NamedDecl *D = *I;
3956 assert(isa<FunctionTemplateDecl>(D) ||
3957 (isa<UsingShadowDecl>(D) &&
3958 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3959 *Storage++ = D;
3960 }
3961
3962 return TemplateName(OT);
3963}
3964
Douglas Gregordc572a32009-03-30 22:58:21 +00003965/// \brief Retrieve the template name that represents a qualified
3966/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003967TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003968 bool TemplateKeyword,
3969 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003970 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003971 llvm::FoldingSetNodeID ID;
3972 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3973
3974 void *InsertPos = 0;
3975 QualifiedTemplateName *QTN =
3976 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3977 if (!QTN) {
3978 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3979 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3980 }
3981
3982 return TemplateName(QTN);
3983}
3984
3985/// \brief Retrieve the template name that represents a dependent
3986/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003987TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003988 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003989 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003990 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003991
3992 llvm::FoldingSetNodeID ID;
3993 DependentTemplateName::Profile(ID, NNS, Name);
3994
3995 void *InsertPos = 0;
3996 DependentTemplateName *QTN =
3997 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3998
3999 if (QTN)
4000 return TemplateName(QTN);
4001
4002 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4003 if (CanonNNS == NNS) {
4004 QTN = new (*this,4) DependentTemplateName(NNS, Name);
4005 } else {
4006 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
4007 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004008 DependentTemplateName *CheckQTN =
4009 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4010 assert(!CheckQTN && "Dependent type name canonicalization broken");
4011 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00004012 }
4013
4014 DependentTemplateNames.InsertNode(QTN, InsertPos);
4015 return TemplateName(QTN);
4016}
4017
Douglas Gregor71395fa2009-11-04 00:56:37 +00004018/// \brief Retrieve the template name that represents a dependent
4019/// template name such as \c MetaFun::template operator+.
4020TemplateName
4021ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
4022 OverloadedOperatorKind Operator) {
4023 assert((!NNS || NNS->isDependent()) &&
4024 "Nested name specifier must be dependent");
4025
4026 llvm::FoldingSetNodeID ID;
4027 DependentTemplateName::Profile(ID, NNS, Operator);
4028
4029 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00004030 DependentTemplateName *QTN
4031 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00004032
4033 if (QTN)
4034 return TemplateName(QTN);
4035
4036 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4037 if (CanonNNS == NNS) {
4038 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4039 } else {
4040 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4041 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00004042
4043 DependentTemplateName *CheckQTN
4044 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4045 assert(!CheckQTN && "Dependent template name canonicalization broken");
4046 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00004047 }
4048
4049 DependentTemplateNames.InsertNode(QTN, InsertPos);
4050 return TemplateName(QTN);
4051}
4052
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004053/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00004054/// TargetInfo, produce the corresponding type. The unsigned @p Type
4055/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00004056CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004057 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00004058 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004059 case TargetInfo::SignedShort: return ShortTy;
4060 case TargetInfo::UnsignedShort: return UnsignedShortTy;
4061 case TargetInfo::SignedInt: return IntTy;
4062 case TargetInfo::UnsignedInt: return UnsignedIntTy;
4063 case TargetInfo::SignedLong: return LongTy;
4064 case TargetInfo::UnsignedLong: return UnsignedLongTy;
4065 case TargetInfo::SignedLongLong: return LongLongTy;
4066 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4067 }
4068
4069 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00004070 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00004071}
Ted Kremenek77c51b22008-07-24 23:58:27 +00004072
4073//===----------------------------------------------------------------------===//
4074// Type Predicates.
4075//===----------------------------------------------------------------------===//
4076
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004077/// isObjCNSObjectType - Return true if this is an NSObject object using
4078/// NSObject attribute on a c-style pointer type.
4079/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00004080/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004081///
4082bool ASTContext::isObjCNSObjectType(QualType Ty) const {
4083 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4084 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00004085 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004086 return true;
4087 }
Mike Stump11289f42009-09-09 15:08:12 +00004088 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00004089}
4090
Fariborz Jahanian96207692009-02-18 21:49:28 +00004091/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
4092/// garbage collection attribute.
4093///
John McCall8ccfcb52009-09-24 19:53:00 +00004094Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
4095 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004096 if (getLangOptions().ObjC1 &&
4097 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00004098 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00004099 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00004100 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004101 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00004102 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00004103 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004104 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004105 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004106 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00004107 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00004108 // Non-pointers have none gc'able attribute regardless of the attribute
4109 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00004110 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00004111 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004112 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00004113 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00004114}
4115
Chris Lattner49af6a42008-04-07 06:51:04 +00004116//===----------------------------------------------------------------------===//
4117// Type Compatibility Testing
4118//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004119
Mike Stump11289f42009-09-09 15:08:12 +00004120/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004121/// compatible.
4122static bool areCompatVectorTypes(const VectorType *LHS,
4123 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004124 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004125 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004126 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004127}
4128
Steve Naroff8e6aee52009-07-23 01:01:38 +00004129//===----------------------------------------------------------------------===//
4130// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4131//===----------------------------------------------------------------------===//
4132
4133/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4134/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004135bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4136 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004137 if (lProto == rProto)
4138 return true;
4139 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4140 E = rProto->protocol_end(); PI != E; ++PI)
4141 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4142 return true;
4143 return false;
4144}
4145
Steve Naroff8e6aee52009-07-23 01:01:38 +00004146/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4147/// return true if lhs's protocols conform to rhs's protocol; false
4148/// otherwise.
4149bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4150 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4151 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4152 return false;
4153}
4154
4155/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4156/// ObjCQualifiedIDType.
4157bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4158 bool compare) {
4159 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004160 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004161 lhs->isObjCIdType() || lhs->isObjCClassType())
4162 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004163 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004164 rhs->isObjCIdType() || rhs->isObjCClassType())
4165 return true;
4166
4167 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004168 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004169
Steve Naroff8e6aee52009-07-23 01:01:38 +00004170 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004171
Steve Naroff8e6aee52009-07-23 01:01:38 +00004172 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004173 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004174 // make sure we check the class hierarchy.
4175 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4176 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4177 E = lhsQID->qual_end(); I != E; ++I) {
4178 // when comparing an id<P> on lhs with a static type on rhs,
4179 // see if static class implements all of id's protocols, directly or
4180 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004181 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004182 return false;
4183 }
4184 }
4185 // If there are no qualifiers and no interface, we have an 'id'.
4186 return true;
4187 }
Mike Stump11289f42009-09-09 15:08:12 +00004188 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004189 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4190 E = lhsQID->qual_end(); I != E; ++I) {
4191 ObjCProtocolDecl *lhsProto = *I;
4192 bool match = false;
4193
4194 // when comparing an id<P> on lhs with a static type on rhs,
4195 // see if static class implements all of id's protocols, directly or
4196 // through its super class and categories.
4197 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4198 E = rhsOPT->qual_end(); J != E; ++J) {
4199 ObjCProtocolDecl *rhsProto = *J;
4200 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4201 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4202 match = true;
4203 break;
4204 }
4205 }
Mike Stump11289f42009-09-09 15:08:12 +00004206 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004207 // make sure we check the class hierarchy.
4208 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4209 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4210 E = lhsQID->qual_end(); I != E; ++I) {
4211 // when comparing an id<P> on lhs with a static type on rhs,
4212 // see if static class implements all of id's protocols, directly or
4213 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004214 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004215 match = true;
4216 break;
4217 }
4218 }
4219 }
4220 if (!match)
4221 return false;
4222 }
Mike Stump11289f42009-09-09 15:08:12 +00004223
Steve Naroff8e6aee52009-07-23 01:01:38 +00004224 return true;
4225 }
Mike Stump11289f42009-09-09 15:08:12 +00004226
Steve Naroff8e6aee52009-07-23 01:01:38 +00004227 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4228 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4229
Mike Stump11289f42009-09-09 15:08:12 +00004230 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004231 lhs->getAsObjCInterfacePointerType()) {
4232 if (lhsOPT->qual_empty()) {
4233 bool match = false;
4234 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4235 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4236 E = rhsQID->qual_end(); I != E; ++I) {
4237 // when comparing an id<P> on lhs with a static type on rhs,
4238 // see if static class implements all of id's protocols, directly or
4239 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004240 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004241 match = true;
4242 break;
4243 }
4244 }
4245 if (!match)
4246 return false;
4247 }
4248 return true;
4249 }
Mike Stump11289f42009-09-09 15:08:12 +00004250 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004251 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4252 E = lhsOPT->qual_end(); I != E; ++I) {
4253 ObjCProtocolDecl *lhsProto = *I;
4254 bool match = false;
4255
4256 // when comparing an id<P> on lhs with a static type on rhs,
4257 // see if static class implements all of id's protocols, directly or
4258 // through its super class and categories.
4259 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4260 E = rhsQID->qual_end(); J != E; ++J) {
4261 ObjCProtocolDecl *rhsProto = *J;
4262 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4263 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4264 match = true;
4265 break;
4266 }
4267 }
4268 if (!match)
4269 return false;
4270 }
4271 return true;
4272 }
4273 return false;
4274}
4275
Eli Friedman47f77112008-08-22 00:56:42 +00004276/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004277/// compatible for assignment from RHS to LHS. This handles validation of any
4278/// protocol qualifiers on the LHS or RHS.
4279///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004280bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4281 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004282 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4283 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4284
Steve Naroff1329fa02009-07-15 18:40:39 +00004285 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004286 if (LHS->isObjCUnqualifiedIdOrClass() ||
4287 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004288 return true;
4289
John McCall8b07ec22010-05-15 11:32:37 +00004290 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004291 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4292 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004293 false);
4294
John McCall8b07ec22010-05-15 11:32:37 +00004295 // If we have 2 user-defined types, fall into that path.
4296 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004297 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004298
Steve Naroff8e6aee52009-07-23 01:01:38 +00004299 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004300}
4301
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004302/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4303/// for providing type-safty for objective-c pointers used to pass/return
4304/// arguments in block literals. When passed as arguments, passing 'A*' where
4305/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4306/// not OK. For the return type, the opposite is not OK.
4307bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4308 const ObjCObjectPointerType *LHSOPT,
4309 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004310 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004311 return true;
4312
4313 if (LHSOPT->isObjCBuiltinType()) {
4314 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4315 }
4316
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004317 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004318 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4319 QualType(RHSOPT,0),
4320 false);
4321
4322 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4323 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4324 if (LHS && RHS) { // We have 2 user-defined types.
4325 if (LHS != RHS) {
4326 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4327 return false;
4328 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4329 return true;
4330 }
4331 else
4332 return true;
4333 }
4334 return false;
4335}
4336
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004337/// getIntersectionOfProtocols - This routine finds the intersection of set
4338/// of protocols inherited from two distinct objective-c pointer objects.
4339/// It is used to build composite qualifier list of the composite type of
4340/// the conditional expression involving two objective-c pointer objects.
4341static
4342void getIntersectionOfProtocols(ASTContext &Context,
4343 const ObjCObjectPointerType *LHSOPT,
4344 const ObjCObjectPointerType *RHSOPT,
4345 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4346
John McCall8b07ec22010-05-15 11:32:37 +00004347 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4348 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4349 assert(LHS->getInterface() && "LHS must have an interface base");
4350 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004351
4352 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4353 unsigned LHSNumProtocols = LHS->getNumProtocols();
4354 if (LHSNumProtocols > 0)
4355 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4356 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004357 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004358 Context.CollectInheritedProtocols(LHS->getInterface(),
4359 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004360 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4361 LHSInheritedProtocols.end());
4362 }
4363
4364 unsigned RHSNumProtocols = RHS->getNumProtocols();
4365 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004366 ObjCProtocolDecl **RHSProtocols =
4367 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004368 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4369 if (InheritedProtocolSet.count(RHSProtocols[i]))
4370 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4371 }
4372 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004373 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004374 Context.CollectInheritedProtocols(RHS->getInterface(),
4375 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004376 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4377 RHSInheritedProtocols.begin(),
4378 E = RHSInheritedProtocols.end(); I != E; ++I)
4379 if (InheritedProtocolSet.count((*I)))
4380 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004381 }
4382}
4383
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004384/// areCommonBaseCompatible - Returns common base class of the two classes if
4385/// one found. Note that this is O'2 algorithm. But it will be called as the
4386/// last type comparison in a ?-exp of ObjC pointer types before a
4387/// warning is issued. So, its invokation is extremely rare.
4388QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004389 const ObjCObjectPointerType *Lptr,
4390 const ObjCObjectPointerType *Rptr) {
4391 const ObjCObjectType *LHS = Lptr->getObjectType();
4392 const ObjCObjectType *RHS = Rptr->getObjectType();
4393 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4394 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4395 if (!LDecl || !RDecl)
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004396 return QualType();
4397
John McCall8b07ec22010-05-15 11:32:37 +00004398 while ((LDecl = LDecl->getSuperClass())) {
4399 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004400 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004401 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4402 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4403
4404 QualType Result = QualType(LHS, 0);
4405 if (!Protocols.empty())
4406 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4407 Result = getObjCObjectPointerType(Result);
4408 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004409 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004410 }
4411
4412 return QualType();
4413}
4414
John McCall8b07ec22010-05-15 11:32:37 +00004415bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4416 const ObjCObjectType *RHS) {
4417 assert(LHS->getInterface() && "LHS is not an interface type");
4418 assert(RHS->getInterface() && "RHS is not an interface type");
4419
Chris Lattner49af6a42008-04-07 06:51:04 +00004420 // Verify that the base decls are compatible: the RHS must be a subclass of
4421 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00004422 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00004423 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004424
Chris Lattner49af6a42008-04-07 06:51:04 +00004425 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4426 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004427 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004428 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004429
Chris Lattner49af6a42008-04-07 06:51:04 +00004430 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4431 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004432 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004433 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004434
John McCall8b07ec22010-05-15 11:32:37 +00004435 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4436 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004437 LHSPI != LHSPE; LHSPI++) {
4438 bool RHSImplementsProtocol = false;
4439
4440 // If the RHS doesn't implement the protocol on the left, the types
4441 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00004442 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4443 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004444 RHSPI != RHSPE; RHSPI++) {
4445 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004446 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004447 break;
4448 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004449 }
4450 // FIXME: For better diagnostics, consider passing back the protocol name.
4451 if (!RHSImplementsProtocol)
4452 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004453 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004454 // The RHS implements all protocols listed on the LHS.
4455 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004456}
4457
Steve Naroffb7605152009-02-12 17:52:19 +00004458bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4459 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004460 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4461 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004462
Steve Naroff7cae42b2009-07-10 23:34:53 +00004463 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004464 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004465
4466 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4467 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004468}
4469
Mike Stump11289f42009-09-09 15:08:12 +00004470/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004471/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004472/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004473/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004474bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004475 if (getLangOptions().CPlusPlus)
4476 return hasSameType(LHS, RHS);
4477
Eli Friedman47f77112008-08-22 00:56:42 +00004478 return !mergeTypes(LHS, RHS).isNull();
4479}
4480
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004481bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4482 return !mergeTypes(LHS, RHS, true).isNull();
4483}
4484
4485QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4486 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004487 const FunctionType *lbase = lhs->getAs<FunctionType>();
4488 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004489 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4490 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004491 bool allLTypes = true;
4492 bool allRTypes = true;
4493
4494 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004495 QualType retType;
4496 if (OfBlockPointer)
4497 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4498 else
4499 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004500 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004501 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004502 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004503 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004504 allRTypes = false;
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004505 // FIXME: double check this
4506 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4507 // rbase->getRegParmAttr() != 0 &&
4508 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004509 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4510 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004511 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4512 lbaseInfo.getRegParm();
4513 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4514 if (NoReturn != lbaseInfo.getNoReturn() ||
4515 RegParm != lbaseInfo.getRegParm())
4516 allLTypes = false;
4517 if (NoReturn != rbaseInfo.getNoReturn() ||
4518 RegParm != rbaseInfo.getRegParm())
4519 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004520 CallingConv lcc = lbaseInfo.getCC();
4521 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004522 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004523 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004524 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004525
Eli Friedman47f77112008-08-22 00:56:42 +00004526 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004527 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4528 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004529 unsigned lproto_nargs = lproto->getNumArgs();
4530 unsigned rproto_nargs = rproto->getNumArgs();
4531
4532 // Compatible functions must have the same number of arguments
4533 if (lproto_nargs != rproto_nargs)
4534 return QualType();
4535
4536 // Variadic and non-variadic functions aren't compatible
4537 if (lproto->isVariadic() != rproto->isVariadic())
4538 return QualType();
4539
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004540 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4541 return QualType();
4542
Eli Friedman47f77112008-08-22 00:56:42 +00004543 // Check argument compatibility
4544 llvm::SmallVector<QualType, 10> types;
4545 for (unsigned i = 0; i < lproto_nargs; i++) {
4546 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4547 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004548 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004549 if (argtype.isNull()) return QualType();
4550 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004551 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4552 allLTypes = false;
4553 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4554 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004555 }
4556 if (allLTypes) return lhs;
4557 if (allRTypes) return rhs;
4558 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004559 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004560 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004561 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004562 }
4563
4564 if (lproto) allRTypes = false;
4565 if (rproto) allLTypes = false;
4566
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004567 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004568 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004569 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004570 if (proto->isVariadic()) return QualType();
4571 // Check that the types are compatible with the types that
4572 // would result from default argument promotions (C99 6.7.5.3p15).
4573 // The only types actually affected are promotable integer
4574 // types and floats, which would be passed as a different
4575 // type depending on whether the prototype is visible.
4576 unsigned proto_nargs = proto->getNumArgs();
4577 for (unsigned i = 0; i < proto_nargs; ++i) {
4578 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004579
4580 // Look at the promotion type of enum types, since that is the type used
4581 // to pass enum values.
4582 if (const EnumType *Enum = argTy->getAs<EnumType>())
4583 argTy = Enum->getDecl()->getPromotionType();
4584
Eli Friedman47f77112008-08-22 00:56:42 +00004585 if (argTy->isPromotableIntegerType() ||
4586 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4587 return QualType();
4588 }
4589
4590 if (allLTypes) return lhs;
4591 if (allRTypes) return rhs;
4592 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004593 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004594 proto->getTypeQuals(),
4595 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004596 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004597 }
4598
4599 if (allLTypes) return lhs;
4600 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004601 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004602 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004603}
4604
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004605QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4606 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004607 // C++ [expr]: If an expression initially has the type "reference to T", the
4608 // type is adjusted to "T" prior to any further analysis, the expression
4609 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004610 // expression is an lvalue unless the reference is an rvalue reference and
4611 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004612 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4613 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4614
Eli Friedman47f77112008-08-22 00:56:42 +00004615 QualType LHSCan = getCanonicalType(LHS),
4616 RHSCan = getCanonicalType(RHS);
4617
4618 // If two types are identical, they are compatible.
4619 if (LHSCan == RHSCan)
4620 return LHS;
4621
John McCall8ccfcb52009-09-24 19:53:00 +00004622 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004623 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4624 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004625 if (LQuals != RQuals) {
4626 // If any of these qualifiers are different, we have a type
4627 // mismatch.
4628 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4629 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4630 return QualType();
4631
4632 // Exactly one GC qualifier difference is allowed: __strong is
4633 // okay if the other type has no GC qualifier but is an Objective
4634 // C object pointer (i.e. implicitly strong by default). We fix
4635 // this by pretending that the unqualified type was actually
4636 // qualified __strong.
4637 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4638 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4639 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4640
4641 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4642 return QualType();
4643
4644 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4645 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4646 }
4647 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4648 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4649 }
Eli Friedman47f77112008-08-22 00:56:42 +00004650 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004651 }
4652
4653 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004654
Eli Friedmandcca6332009-06-01 01:22:52 +00004655 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4656 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004657
Chris Lattnerfd652912008-01-14 05:45:46 +00004658 // We want to consider the two function types to be the same for these
4659 // comparisons, just force one to the other.
4660 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4661 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004662
4663 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004664 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4665 LHSClass = Type::ConstantArray;
4666 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4667 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004668
John McCall8b07ec22010-05-15 11:32:37 +00004669 // ObjCInterfaces are just specialized ObjCObjects.
4670 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
4671 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
4672
Nate Begemance4d7fc2008-04-18 23:10:10 +00004673 // Canonicalize ExtVector -> Vector.
4674 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4675 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004676
Chris Lattner95554662008-04-07 05:43:21 +00004677 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004678 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004679 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004680 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004681 // Compatibility is based on the underlying type, not the promotion
4682 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004683 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004684 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4685 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004686 }
John McCall9dd450b2009-09-21 23:43:11 +00004687 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004688 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4689 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004690 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004691
Eli Friedman47f77112008-08-22 00:56:42 +00004692 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004693 }
Eli Friedman47f77112008-08-22 00:56:42 +00004694
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004695 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004696 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004697#define TYPE(Class, Base)
4698#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004699#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004700#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4701#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4702#include "clang/AST/TypeNodes.def"
4703 assert(false && "Non-canonical and dependent types shouldn't get here");
4704 return QualType();
4705
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004706 case Type::LValueReference:
4707 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004708 case Type::MemberPointer:
4709 assert(false && "C++ should never be in mergeTypes");
4710 return QualType();
4711
John McCall8b07ec22010-05-15 11:32:37 +00004712 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004713 case Type::IncompleteArray:
4714 case Type::VariableArray:
4715 case Type::FunctionProto:
4716 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004717 assert(false && "Types are eliminated above");
4718 return QualType();
4719
Chris Lattnerfd652912008-01-14 05:45:46 +00004720 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004721 {
4722 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004723 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4724 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004725 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4726 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004727 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004728 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004729 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004730 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004731 return getPointerType(ResultType);
4732 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004733 case Type::BlockPointer:
4734 {
4735 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004736 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4737 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004738 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004739 if (ResultType.isNull()) return QualType();
4740 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4741 return LHS;
4742 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4743 return RHS;
4744 return getBlockPointerType(ResultType);
4745 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004746 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004747 {
4748 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4749 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4750 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4751 return QualType();
4752
4753 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4754 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4755 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4756 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004757 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4758 return LHS;
4759 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4760 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004761 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4762 ArrayType::ArraySizeModifier(), 0);
4763 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4764 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004765 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4766 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004767 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4768 return LHS;
4769 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4770 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004771 if (LVAT) {
4772 // FIXME: This isn't correct! But tricky to implement because
4773 // the array's size has to be the size of LHS, but the type
4774 // has to be different.
4775 return LHS;
4776 }
4777 if (RVAT) {
4778 // FIXME: This isn't correct! But tricky to implement because
4779 // the array's size has to be the size of RHS, but the type
4780 // has to be different.
4781 return RHS;
4782 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004783 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4784 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004785 return getIncompleteArrayType(ResultType,
4786 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004787 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004788 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004789 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004790 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004791 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004792 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004793 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004794 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004795 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004796 case Type::Complex:
4797 // Distinct complex types are incompatible.
4798 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004799 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004800 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004801 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4802 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004803 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004804 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00004805 case Type::ObjCObject: {
4806 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004807 // FIXME: This should be type compatibility, e.g. whether
4808 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00004809 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
4810 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
4811 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00004812 return LHS;
4813
Eli Friedman47f77112008-08-22 00:56:42 +00004814 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004815 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004816 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004817 if (OfBlockPointer) {
4818 if (canAssignObjCInterfacesInBlockPointer(
4819 LHS->getAs<ObjCObjectPointerType>(),
4820 RHS->getAs<ObjCObjectPointerType>()))
4821 return LHS;
4822 return QualType();
4823 }
John McCall9dd450b2009-09-21 23:43:11 +00004824 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4825 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004826 return LHS;
4827
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004828 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004829 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004830 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004831
4832 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004833}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004834
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00004835/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
4836/// 'RHS' attributes and returns the merged version; including for function
4837/// return types.
4838QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
4839 QualType LHSCan = getCanonicalType(LHS),
4840 RHSCan = getCanonicalType(RHS);
4841 // If two types are identical, they are compatible.
4842 if (LHSCan == RHSCan)
4843 return LHS;
4844 if (RHSCan->isFunctionType()) {
4845 if (!LHSCan->isFunctionType())
4846 return QualType();
4847 QualType OldReturnType =
4848 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
4849 QualType NewReturnType =
4850 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
4851 QualType ResReturnType =
4852 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
4853 if (ResReturnType.isNull())
4854 return QualType();
4855 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
4856 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
4857 // In either case, use OldReturnType to build the new function type.
4858 const FunctionType *F = LHS->getAs<FunctionType>();
4859 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
4860 FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
4861 QualType ResultType
4862 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
4863 FPT->getNumArgs(), FPT->isVariadic(),
4864 FPT->getTypeQuals(),
4865 FPT->hasExceptionSpec(),
4866 FPT->hasAnyExceptionSpec(),
4867 FPT->getNumExceptions(),
4868 FPT->exception_begin(),
4869 Info);
4870 return ResultType;
4871 }
4872 }
4873 return QualType();
4874 }
4875
4876 // If the qualifiers are different, the types can still be merged.
4877 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4878 Qualifiers RQuals = RHSCan.getLocalQualifiers();
4879 if (LQuals != RQuals) {
4880 // If any of these qualifiers are different, we have a type mismatch.
4881 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4882 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4883 return QualType();
4884
4885 // Exactly one GC qualifier difference is allowed: __strong is
4886 // okay if the other type has no GC qualifier but is an Objective
4887 // C object pointer (i.e. implicitly strong by default). We fix
4888 // this by pretending that the unqualified type was actually
4889 // qualified __strong.
4890 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4891 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4892 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4893
4894 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4895 return QualType();
4896
4897 if (GC_L == Qualifiers::Strong)
4898 return LHS;
4899 if (GC_R == Qualifiers::Strong)
4900 return RHS;
4901 return QualType();
4902 }
4903
4904 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
4905 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
4906 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
4907 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
4908 if (ResQT == LHSBaseQT)
4909 return LHS;
4910 if (ResQT == RHSBaseQT)
4911 return RHS;
4912 }
4913 return QualType();
4914}
4915
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004916//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004917// Integer Predicates
4918//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004919
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004920unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004921 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004922 return 1;
John McCall56774992009-12-09 09:09:27 +00004923 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004924 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004925 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004926 return (unsigned)getTypeSize(T);
4927}
4928
4929QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4930 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004931
4932 // Turn <4 x signed int> -> <4 x unsigned int>
4933 if (const VectorType *VTy = T->getAs<VectorType>())
4934 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004935 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004936
4937 // For enums, we return the unsigned version of the base type.
4938 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004939 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004940
4941 const BuiltinType *BTy = T->getAs<BuiltinType>();
4942 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004943 switch (BTy->getKind()) {
4944 case BuiltinType::Char_S:
4945 case BuiltinType::SChar:
4946 return UnsignedCharTy;
4947 case BuiltinType::Short:
4948 return UnsignedShortTy;
4949 case BuiltinType::Int:
4950 return UnsignedIntTy;
4951 case BuiltinType::Long:
4952 return UnsignedLongTy;
4953 case BuiltinType::LongLong:
4954 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004955 case BuiltinType::Int128:
4956 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004957 default:
4958 assert(0 && "Unexpected signed integer type");
4959 return QualType();
4960 }
4961}
4962
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004963ExternalASTSource::~ExternalASTSource() { }
4964
4965void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004966
4967
4968//===----------------------------------------------------------------------===//
4969// Builtin Type Computation
4970//===----------------------------------------------------------------------===//
4971
4972/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4973/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004974static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004975 ASTContext::GetBuiltinTypeError &Error,
4976 bool AllowTypeModifiers = true) {
4977 // Modifiers.
4978 int HowLong = 0;
4979 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004980
Chris Lattnerecd79c62009-06-14 00:45:47 +00004981 // Read the modifiers first.
4982 bool Done = false;
4983 while (!Done) {
4984 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004985 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004986 case 'S':
4987 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4988 assert(!Signed && "Can't use 'S' modifier multiple times!");
4989 Signed = true;
4990 break;
4991 case 'U':
4992 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4993 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4994 Unsigned = true;
4995 break;
4996 case 'L':
4997 assert(HowLong <= 2 && "Can't have LLLL modifier");
4998 ++HowLong;
4999 break;
5000 }
5001 }
5002
5003 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00005004
Chris Lattnerecd79c62009-06-14 00:45:47 +00005005 // Read the base type.
5006 switch (*Str++) {
5007 default: assert(0 && "Unknown builtin type letter!");
5008 case 'v':
5009 assert(HowLong == 0 && !Signed && !Unsigned &&
5010 "Bad modifiers used with 'v'!");
5011 Type = Context.VoidTy;
5012 break;
5013 case 'f':
5014 assert(HowLong == 0 && !Signed && !Unsigned &&
5015 "Bad modifiers used with 'f'!");
5016 Type = Context.FloatTy;
5017 break;
5018 case 'd':
5019 assert(HowLong < 2 && !Signed && !Unsigned &&
5020 "Bad modifiers used with 'd'!");
5021 if (HowLong)
5022 Type = Context.LongDoubleTy;
5023 else
5024 Type = Context.DoubleTy;
5025 break;
5026 case 's':
5027 assert(HowLong == 0 && "Bad modifiers used with 's'!");
5028 if (Unsigned)
5029 Type = Context.UnsignedShortTy;
5030 else
5031 Type = Context.ShortTy;
5032 break;
5033 case 'i':
5034 if (HowLong == 3)
5035 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
5036 else if (HowLong == 2)
5037 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
5038 else if (HowLong == 1)
5039 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
5040 else
5041 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
5042 break;
5043 case 'c':
5044 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
5045 if (Signed)
5046 Type = Context.SignedCharTy;
5047 else if (Unsigned)
5048 Type = Context.UnsignedCharTy;
5049 else
5050 Type = Context.CharTy;
5051 break;
5052 case 'b': // boolean
5053 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
5054 Type = Context.BoolTy;
5055 break;
5056 case 'z': // size_t.
5057 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
5058 Type = Context.getSizeType();
5059 break;
5060 case 'F':
5061 Type = Context.getCFConstantStringType();
5062 break;
5063 case 'a':
5064 Type = Context.getBuiltinVaListType();
5065 assert(!Type.isNull() && "builtin va list type not initialized!");
5066 break;
5067 case 'A':
5068 // This is a "reference" to a va_list; however, what exactly
5069 // this means depends on how va_list is defined. There are two
5070 // different kinds of va_list: ones passed by value, and ones
5071 // passed by reference. An example of a by-value va_list is
5072 // x86, where va_list is a char*. An example of by-ref va_list
5073 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
5074 // we want this argument to be a char*&; for x86-64, we want
5075 // it to be a __va_list_tag*.
5076 Type = Context.getBuiltinVaListType();
5077 assert(!Type.isNull() && "builtin va list type not initialized!");
5078 if (Type->isArrayType()) {
5079 Type = Context.getArrayDecayedType(Type);
5080 } else {
5081 Type = Context.getLValueReferenceType(Type);
5082 }
5083 break;
5084 case 'V': {
5085 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005086 unsigned NumElements = strtoul(Str, &End, 10);
5087 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00005088
Chris Lattnerecd79c62009-06-14 00:45:47 +00005089 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00005090
Chris Lattnerecd79c62009-06-14 00:45:47 +00005091 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00005092 // FIXME: Don't know what to do about AltiVec.
5093 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005094 break;
5095 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00005096 case 'X': {
5097 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
5098 Type = Context.getComplexType(ElementType);
5099 break;
5100 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00005101 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00005102 Type = Context.getFILEType();
5103 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005104 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005105 return QualType();
5106 }
Mike Stump2adb4da2009-07-28 23:47:15 +00005107 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00005108 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00005109 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00005110 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00005111 else
5112 Type = Context.getjmp_bufType();
5113
Mike Stump2adb4da2009-07-28 23:47:15 +00005114 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005115 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005116 return QualType();
5117 }
5118 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005119 }
Mike Stump11289f42009-09-09 15:08:12 +00005120
Chris Lattnerecd79c62009-06-14 00:45:47 +00005121 if (!AllowTypeModifiers)
5122 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00005123
Chris Lattnerecd79c62009-06-14 00:45:47 +00005124 Done = false;
5125 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005126 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005127 default: Done = true; --Str; break;
5128 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00005129 case '&':
John McCallb8b94662010-03-12 04:21:28 +00005130 {
5131 // Both pointers and references can have their pointee types
5132 // qualified with an address space.
5133 char *End;
5134 unsigned AddrSpace = strtoul(Str, &End, 10);
5135 if (End != Str && AddrSpace != 0) {
5136 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5137 Str = End;
5138 }
5139 }
5140 if (c == '*')
5141 Type = Context.getPointerType(Type);
5142 else
5143 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005144 break;
5145 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5146 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00005147 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00005148 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00005149 case 'D':
5150 Type = Context.getVolatileType(Type);
5151 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005152 }
5153 }
Mike Stump11289f42009-09-09 15:08:12 +00005154
Chris Lattnerecd79c62009-06-14 00:45:47 +00005155 return Type;
5156}
5157
5158/// GetBuiltinType - Return the type for the specified builtin.
5159QualType ASTContext::GetBuiltinType(unsigned id,
5160 GetBuiltinTypeError &Error) {
5161 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00005162
Chris Lattnerecd79c62009-06-14 00:45:47 +00005163 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005164
Chris Lattnerecd79c62009-06-14 00:45:47 +00005165 Error = GE_None;
5166 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
5167 if (Error != GE_None)
5168 return QualType();
5169 while (TypeStr[0] && TypeStr[0] != '.') {
5170 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
5171 if (Error != GE_None)
5172 return QualType();
5173
5174 // Do array -> pointer decay. The builtin should use the decayed type.
5175 if (Ty->isArrayType())
5176 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005177
Chris Lattnerecd79c62009-06-14 00:45:47 +00005178 ArgTypes.push_back(Ty);
5179 }
5180
5181 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5182 "'.' should only occur at end of builtin type list!");
5183
5184 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
5185 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
5186 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005187
5188 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00005189 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00005190 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005191 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00005192}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005193
5194QualType
5195ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
5196 // Perform the usual unary conversions. We do this early so that
5197 // integral promotions to "int" can allow us to exit early, in the
5198 // lhs == rhs check. Also, for conversion purposes, we ignore any
5199 // qualifiers. For example, "const float" and "float" are
5200 // equivalent.
5201 if (lhs->isPromotableIntegerType())
5202 lhs = getPromotedIntegerType(lhs);
5203 else
5204 lhs = lhs.getUnqualifiedType();
5205 if (rhs->isPromotableIntegerType())
5206 rhs = getPromotedIntegerType(rhs);
5207 else
5208 rhs = rhs.getUnqualifiedType();
5209
5210 // If both types are identical, no conversion is needed.
5211 if (lhs == rhs)
5212 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005213
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005214 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
5215 // The caller can deal with this (e.g. pointer + int).
5216 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5217 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005218
5219 // At this point, we have two different arithmetic types.
5220
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005221 // Handle complex types first (C99 6.3.1.8p1).
5222 if (lhs->isComplexType() || rhs->isComplexType()) {
5223 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00005224 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005225 // convert the rhs to the lhs complex type.
5226 return lhs;
5227 }
Mike Stump11289f42009-09-09 15:08:12 +00005228 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005229 // convert the lhs to the rhs complex type.
5230 return rhs;
5231 }
5232 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00005233 // When both operands are complex, the shorter operand is converted to the
5234 // type of the longer, and that is the type of the result. This corresponds
5235 // to what is done when combining two real floating-point operands.
5236 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005237 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00005238 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005239 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00005240 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005241 // "double _Complex" is promoted to "long double _Complex".
5242 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005243
5244 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005245 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005246 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005247 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00005248 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005249 // At this point, lhs and rhs have the same rank/size. Now, make sure the
5250 // domains match. This is a requirement for our implementation, C99
5251 // does not require this promotion.
5252 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5253 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5254 return rhs;
5255 } else { // handle "_Complex double, double".
5256 return lhs;
5257 }
5258 }
5259 return lhs; // The domain/size match exactly.
5260 }
5261 // Now handle "real" floating types (i.e. float, double, long double).
5262 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5263 // if we have an integer operand, the result is the real floating type.
5264 if (rhs->isIntegerType()) {
5265 // convert rhs to the lhs floating point type.
5266 return lhs;
5267 }
5268 if (rhs->isComplexIntegerType()) {
5269 // convert rhs to the complex floating point type.
5270 return getComplexType(lhs);
5271 }
5272 if (lhs->isIntegerType()) {
5273 // convert lhs to the rhs floating point type.
5274 return rhs;
5275 }
Mike Stump11289f42009-09-09 15:08:12 +00005276 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005277 // convert lhs to the complex floating point type.
5278 return getComplexType(rhs);
5279 }
5280 // We have two real floating types, float/complex combos were handled above.
5281 // Convert the smaller operand to the bigger result.
5282 int result = getFloatingTypeOrder(lhs, rhs);
5283 if (result > 0) // convert the rhs
5284 return lhs;
5285 assert(result < 0 && "illegal float comparison");
5286 return rhs; // convert the lhs
5287 }
5288 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5289 // Handle GCC complex int extension.
5290 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5291 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5292
5293 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005294 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005295 rhsComplexInt->getElementType()) >= 0)
5296 return lhs; // convert the rhs
5297 return rhs;
5298 } else if (lhsComplexInt && rhs->isIntegerType()) {
5299 // convert the rhs to the lhs complex type.
5300 return lhs;
5301 } else if (rhsComplexInt && lhs->isIntegerType()) {
5302 // convert the lhs to the rhs complex type.
5303 return rhs;
5304 }
5305 }
5306 // Finally, we have two differing integer types.
5307 // The rules for this case are in C99 6.3.1.8
5308 int compare = getIntegerTypeOrder(lhs, rhs);
5309 bool lhsSigned = lhs->isSignedIntegerType(),
5310 rhsSigned = rhs->isSignedIntegerType();
5311 QualType destType;
5312 if (lhsSigned == rhsSigned) {
5313 // Same signedness; use the higher-ranked type
5314 destType = compare >= 0 ? lhs : rhs;
5315 } else if (compare != (lhsSigned ? 1 : -1)) {
5316 // The unsigned type has greater than or equal rank to the
5317 // signed type, so use the unsigned type
5318 destType = lhsSigned ? rhs : lhs;
5319 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5320 // The two types are different widths; if we are here, that
5321 // means the signed type is larger than the unsigned type, so
5322 // use the signed type.
5323 destType = lhsSigned ? lhs : rhs;
5324 } else {
5325 // The signed type is higher-ranked than the unsigned type,
5326 // but isn't actually any bigger (like unsigned int and long
5327 // on most 32-bit systems). Use the unsigned type corresponding
5328 // to the signed type.
5329 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5330 }
5331 return destType;
5332}