blob: fcc3e3d0c239f84d02d006af3b26c17a31142556 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyckbdc601b2009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/ExternalASTSource.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000024#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000026#include "llvm/ADT/SmallString.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000027#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000028#include "llvm/Support/MathExtras.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000029#include "llvm/Support/raw_ostream.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000030#include "RecordLayoutBuilder.h"
31
Reid Spencer5f016e22007-07-11 17:01:13 +000032using namespace clang;
33
34enum FloatingRank {
35 FloatRank, DoubleRank, LongDoubleRank
36};
37
Chris Lattner61710852008-10-05 17:34:18 +000038ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar444be732009-11-13 05:51:54 +000039 const TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000040 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +000041 Builtin::Context &builtins,
Mike Stump1eb44332009-09-09 15:08:12 +000042 bool FreeMem, unsigned size_reserve) :
43 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +000044 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump083c25e2009-10-22 00:49:09 +000045 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
Douglas Gregorc6fbbed2010-03-19 22:13:20 +000046 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +000047 Idents(idents), Selectors(sels),
Mike Stump1eb44332009-09-09 15:08:12 +000048 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall0f436562009-08-17 16:35:33 +000049 ObjCIdRedefinitionType = QualType();
50 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +000051 ObjCSelRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000052 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000053 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000054 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000055}
56
Reid Spencer5f016e22007-07-11 17:01:13 +000057ASTContext::~ASTContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +000058 // Release the DenseMaps associated with DeclContext objects.
59 // FIXME: Is this the ideal solution?
60 ReleaseDeclContextMaps();
Douglas Gregor7d10b7e2010-03-02 23:58:15 +000061
62 // Release all of the memory associated with overridden C++ methods.
63 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
64 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
65 OM != OMEnd; ++OM)
66 OM->second.Destroy();
Ted Kremenek3478eb62010-02-11 07:12:28 +000067
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000068 if (FreeMemory) {
69 // Deallocate all the types.
70 while (!Types.empty()) {
71 Types.back()->Destroy(*this);
72 Types.pop_back();
73 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000074
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000075 for (llvm::FoldingSet<ExtQuals>::iterator
76 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
77 // Increment in loop to prevent using deallocated memory.
John McCall0953e762009-09-24 19:53:00 +000078 Deallocate(&*I++);
Nuno Lopesb74668e2008-12-17 22:30:25 +000079 }
Nuno Lopesb74668e2008-12-17 22:30:25 +000080
Ted Kremenek503524a2010-03-08 20:56:29 +000081 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
82 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
83 // Increment in loop to prevent using deallocated memory.
84 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
85 R->Destroy(*this);
86 }
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000087
Ted Kremenek503524a2010-03-08 20:56:29 +000088 for (llvm::DenseMap<const ObjCContainerDecl*,
89 const ASTRecordLayout*>::iterator
90 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
91 // Increment in loop to prevent using deallocated memory.
92 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
93 R->Destroy(*this);
94 }
Nuno Lopesb74668e2008-12-17 22:30:25 +000095 }
96
Douglas Gregorab452ba2009-03-26 23:50:42 +000097 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000098 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
99 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000100 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenekbbfd68d2009-12-23 21:13:52 +0000101 NNS != NNSEnd; ) {
102 // Increment in loop to prevent using deallocated memory.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +0000103 (*NNS++).Destroy(*this);
Ted Kremenekbbfd68d2009-12-23 21:13:52 +0000104 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000105
106 if (GlobalNestedNameSpecifier)
107 GlobalNestedNameSpecifier->Destroy(*this);
108
Eli Friedmanb26153c2008-05-27 03:08:09 +0000109 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000110}
111
Mike Stump1eb44332009-09-09 15:08:12 +0000112void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
114 ExternalSource.reset(Source.take());
115}
116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117void ASTContext::PrintStats() const {
118 fprintf(stderr, "*** AST Context Stats:\n");
119 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000120
Douglas Gregordbe833d2009-05-26 14:40:08 +0000121 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000122#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000123#define ABSTRACT_TYPE(Name, Parent)
124#include "clang/AST/TypeNodes.def"
125 0 // Extra
126 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
129 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000130 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 }
132
Douglas Gregordbe833d2009-05-26 14:40:08 +0000133 unsigned Idx = 0;
134 unsigned TotalBytes = 0;
135#define TYPE(Name, Parent) \
136 if (counts[Idx]) \
137 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
138 TotalBytes += counts[Idx] * sizeof(Name##Type); \
139 ++Idx;
140#define ABSTRACT_TYPE(Name, Parent)
141#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Douglas Gregordbe833d2009-05-26 14:40:08 +0000143 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144
145 if (ExternalSource.get()) {
146 fprintf(stderr, "\n");
147 ExternalSource->PrintStats();
148 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000149}
150
151
John McCalle27ec8a2009-10-23 23:03:21 +0000152void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000153 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000154 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000155 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156}
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158void ASTContext::InitBuiltinTypes() {
159 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 // C99 6.2.5p19.
162 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // C99 6.2.5p2.
165 InitBuiltinType(BoolTy, BuiltinType::Bool);
166 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000167 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 InitBuiltinType(CharTy, BuiltinType::Char_S);
169 else
170 InitBuiltinType(CharTy, BuiltinType::Char_U);
171 // C99 6.2.5p4.
172 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
173 InitBuiltinType(ShortTy, BuiltinType::Short);
174 InitBuiltinType(IntTy, BuiltinType::Int);
175 InitBuiltinType(LongTy, BuiltinType::Long);
176 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 // C99 6.2.5p6.
179 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
180 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
181 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
182 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
183 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 // C99 6.2.5p10.
186 InitBuiltinType(FloatTy, BuiltinType::Float);
187 InitBuiltinType(DoubleTy, BuiltinType::Double);
188 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000189
Chris Lattner2df9ced2009-04-30 02:43:43 +0000190 // GNU extension, 128-bit integers.
191 InitBuiltinType(Int128Ty, BuiltinType::Int128);
192 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
193
Chris Lattner3a250322009-02-26 23:43:47 +0000194 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
195 InitBuiltinType(WCharTy, BuiltinType::WChar);
196 else // C99
197 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000198
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000199 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
200 InitBuiltinType(Char16Ty, BuiltinType::Char16);
201 else // C99
202 Char16Ty = getFromTargetType(Target.getChar16Type());
203
204 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
205 InitBuiltinType(Char32Ty, BuiltinType::Char32);
206 else // C99
207 Char32Ty = getFromTargetType(Target.getChar32Type());
208
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000209 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000210 InitBuiltinType(OverloadTy, BuiltinType::Overload);
211
212 // Placeholder type for type-dependent expressions whose type is
213 // completely unknown. No code should ever check a type against
214 // DependentTy and users should never see it; however, it is here to
215 // help diagnose failures to properly check for type-dependent
216 // expressions.
217 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000218
Mike Stump1eb44332009-09-09 15:08:12 +0000219 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000220 // not yet been deduced.
221 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 // C99 6.2.5p11.
224 FloatComplexTy = getComplexType(FloatTy);
225 DoubleComplexTy = getComplexType(DoubleTy);
226 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000227
Steve Naroff7e219e42007-10-15 14:41:52 +0000228 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Steve Naroffde2e22d2009-07-15 18:40:39 +0000230 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
231 ObjCIdTypedefType = QualType();
232 ObjCClassTypedefType = QualType();
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000233 ObjCSelTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000235 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroffde2e22d2009-07-15 18:40:39 +0000236 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
237 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000238 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff14108da2009-07-10 23:34:53 +0000239
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000244
245 // nullptr type (C++0x 2.14.7)
246 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000247}
248
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000249MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000250ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000251 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000252 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000253 = InstantiatedFromStaticDataMember.find(Var);
254 if (Pos == InstantiatedFromStaticDataMember.end())
255 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Douglas Gregor7caa6822009-07-24 20:34:43 +0000257 return Pos->second;
258}
259
Mike Stump1eb44332009-09-09 15:08:12 +0000260void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000261ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
262 TemplateSpecializationKind TSK) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000263 assert(Inst->isStaticDataMember() && "Not a static data member");
264 assert(Tmpl->isStaticDataMember() && "Not a static data member");
265 assert(!InstantiatedFromStaticDataMember[Inst] &&
266 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000267 InstantiatedFromStaticDataMember[Inst]
268 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000269}
270
John McCall7ba107a2009-11-18 02:36:19 +0000271NamedDecl *
John McCalled976492009-12-04 22:46:56 +0000272ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCall7ba107a2009-11-18 02:36:19 +0000273 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCalled976492009-12-04 22:46:56 +0000274 = InstantiatedFromUsingDecl.find(UUD);
275 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson0d8df782009-08-29 19:37:28 +0000276 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Anders Carlsson0d8df782009-08-29 19:37:28 +0000278 return Pos->second;
279}
280
281void
John McCalled976492009-12-04 22:46:56 +0000282ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
283 assert((isa<UsingDecl>(Pattern) ||
284 isa<UnresolvedUsingValueDecl>(Pattern) ||
285 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
286 "pattern decl is not a using decl");
287 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
288 InstantiatedFromUsingDecl[Inst] = Pattern;
289}
290
291UsingShadowDecl *
292ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
293 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
294 = InstantiatedFromUsingShadowDecl.find(Inst);
295 if (Pos == InstantiatedFromUsingShadowDecl.end())
296 return 0;
297
298 return Pos->second;
299}
300
301void
302ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
303 UsingShadowDecl *Pattern) {
304 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
305 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +0000306}
307
Anders Carlssond8b285f2009-09-01 04:26:58 +0000308FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
309 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
310 = InstantiatedFromUnnamedFieldDecl.find(Field);
311 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
312 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Anders Carlssond8b285f2009-09-01 04:26:58 +0000314 return Pos->second;
315}
316
317void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
318 FieldDecl *Tmpl) {
319 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
320 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
321 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
322 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Anders Carlssond8b285f2009-09-01 04:26:58 +0000324 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
325}
326
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000327CXXMethodVector::iterator CXXMethodVector::begin() const {
328 if ((Storage & 0x01) == 0)
329 return reinterpret_cast<iterator>(&Storage);
330
331 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
332 return &Vec->front();
333}
334
335CXXMethodVector::iterator CXXMethodVector::end() const {
336 if ((Storage & 0x01) == 0) {
337 if (Storage == 0)
338 return reinterpret_cast<iterator>(&Storage);
339
340 return reinterpret_cast<iterator>(&Storage) + 1;
341 }
342
343 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
344 return &Vec->front() + Vec->size();
345}
346
347void CXXMethodVector::push_back(const CXXMethodDecl *Method) {
348 if (Storage == 0) {
349 // 0 -> 1 element.
350 Storage = reinterpret_cast<uintptr_t>(Method);
351 return;
352 }
353
354 vector_type *Vec;
355 if ((Storage & 0x01) == 0) {
356 // 1 -> 2 elements. Allocate a new vector and push the element into that
357 // vector.
358 Vec = new vector_type;
359 Vec->push_back(reinterpret_cast<const CXXMethodDecl *>(Storage));
360 Storage = reinterpret_cast<uintptr_t>(Vec) | 0x01;
361 } else
362 Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
363
364 // Add the new method to the vector.
365 Vec->push_back(Method);
366}
367
368void CXXMethodVector::Destroy() {
369 if (Storage & 0x01)
370 delete reinterpret_cast<vector_type *>(Storage & ~0x01);
371
372 Storage = 0;
373}
374
375
376ASTContext::overridden_cxx_method_iterator
377ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
378 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
379 = OverriddenMethods.find(Method);
380 if (Pos == OverriddenMethods.end())
381 return 0;
382
383 return Pos->second.begin();
384}
385
386ASTContext::overridden_cxx_method_iterator
387ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
388 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
389 = OverriddenMethods.find(Method);
390 if (Pos == OverriddenMethods.end())
391 return 0;
392
393 return Pos->second.end();
394}
395
396void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
397 const CXXMethodDecl *Overridden) {
398 OverriddenMethods[Method].push_back(Overridden);
399}
400
Douglas Gregor2e222532009-07-02 17:08:52 +0000401namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000402 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000403 : std::binary_function<SourceRange, SourceRange, bool> {
404 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Douglas Gregor2e222532009-07-02 17:08:52 +0000406 public:
407 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Douglas Gregor2e222532009-07-02 17:08:52 +0000409 bool operator()(SourceRange X, SourceRange Y) {
410 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
411 }
412 };
413}
414
Chris Lattner464175b2007-07-18 17:52:12 +0000415//===----------------------------------------------------------------------===//
416// Type Sizing and Analysis
417//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000418
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000419/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
420/// scalar floating point type.
421const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000422 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000423 assert(BT && "Not a floating point type!");
424 switch (BT->getKind()) {
425 default: assert(0 && "Not a floating point type!");
426 case BuiltinType::Float: return Target.getFloatFormat();
427 case BuiltinType::Double: return Target.getDoubleFormat();
428 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
429 }
430}
431
Ken Dyck8b752f12010-01-27 17:10:57 +0000432/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000433/// specified decl. Note that bitfields do not have a valid alignment, so
434/// this method will assert on them.
Sebastian Redl5d484e82009-11-23 17:18:46 +0000435/// If @p RefAsPointee, references are treated like their underlying type
436/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck8b752f12010-01-27 17:10:57 +0000437CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000438 unsigned Align = Target.getCharWidth();
439
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000440 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Sean Huntbbd37c62009-11-21 08:43:09 +0000441 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedmandcdafb62009-02-22 02:56:25 +0000442
Chris Lattneraf707ab2009-01-24 21:53:27 +0000443 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
444 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000445 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl5d484e82009-11-23 17:18:46 +0000446 if (RefAsPointee)
447 T = RT->getPointeeType();
448 else
449 T = getPointerType(RT->getPointeeType());
450 }
451 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000452 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000453 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
454 T = cast<ArrayType>(T)->getElementType();
455
456 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
457 }
Charles Davis05f62472010-02-23 04:52:00 +0000458 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
459 // In the case of a field in a packed struct, we want the minimum
460 // of the alignment of the field and the alignment of the struct.
461 Align = std::min(Align,
462 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
463 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000464 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000465
Ken Dyck8b752f12010-01-27 17:10:57 +0000466 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000467}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000468
Chris Lattnera7674d82007-07-13 22:13:22 +0000469/// getTypeSize - Return the size of the specified type, in bits. This method
470/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000471///
472/// FIXME: Pointers into different addr spaces could have different sizes and
473/// alignment requirements: getPointerInfo should take an AddrSpace, this
474/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000475std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000476ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000477 uint64_t Width=0;
478 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000479 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000480#define TYPE(Class, Base)
481#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000482#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000483#define DEPENDENT_TYPE(Class, Base) case Type::Class:
484#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000485 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000486 break;
487
Chris Lattner692233e2007-07-13 22:27:08 +0000488 case Type::FunctionNoProto:
489 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000490 // GCC extension: alignof(function) = 32 bits
491 Width = 0;
492 Align = 32;
493 break;
494
Douglas Gregor72564e72009-02-26 23:50:07 +0000495 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000496 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000497 Width = 0;
498 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
499 break;
500
Steve Narofffb22d962007-08-30 01:06:46 +0000501 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000502 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner98be4942008-03-05 18:54:05 +0000504 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000505 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000506 Align = EltInfo.second;
507 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000508 }
Nate Begeman213541a2008-04-18 23:10:10 +0000509 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000510 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000511 const VectorType *VT = cast<VectorType>(T);
512 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
513 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000514 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000515 // If the alignment is not a power of 2, round up to the next power of 2.
516 // This happens for non-power-of-2 length vectors.
Chris Lattner9fcfe922009-10-22 05:17:15 +0000517 if (VT->getNumElements() & (VT->getNumElements()-1)) {
518 Align = llvm::NextPowerOf2(Align);
519 Width = llvm::RoundUpToAlignment(Width, Align);
520 }
Chris Lattner030d8842007-07-19 22:06:24 +0000521 break;
522 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000523
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000524 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000525 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000526 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000527 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000528 // GCC extension: alignof(void) = 8 bits.
529 Width = 0;
530 Align = 8;
531 break;
532
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000533 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000534 Width = Target.getBoolWidth();
535 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000536 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000537 case BuiltinType::Char_S:
538 case BuiltinType::Char_U:
539 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000540 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000541 Width = Target.getCharWidth();
542 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000543 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000544 case BuiltinType::WChar:
545 Width = Target.getWCharWidth();
546 Align = Target.getWCharAlign();
547 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000548 case BuiltinType::Char16:
549 Width = Target.getChar16Width();
550 Align = Target.getChar16Align();
551 break;
552 case BuiltinType::Char32:
553 Width = Target.getChar32Width();
554 Align = Target.getChar32Align();
555 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000556 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000557 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000558 Width = Target.getShortWidth();
559 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000560 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000561 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000562 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000563 Width = Target.getIntWidth();
564 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000565 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000566 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000567 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000568 Width = Target.getLongWidth();
569 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000570 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000571 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000572 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000573 Width = Target.getLongLongWidth();
574 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000575 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000576 case BuiltinType::Int128:
577 case BuiltinType::UInt128:
578 Width = 128;
579 Align = 128; // int128_t is 128-bit aligned on all targets.
580 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000581 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000582 Width = Target.getFloatWidth();
583 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000584 break;
585 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000586 Width = Target.getDoubleWidth();
587 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000588 break;
589 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000590 Width = Target.getLongDoubleWidth();
591 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000592 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000593 case BuiltinType::NullPtr:
594 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
595 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000596 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000597 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000598 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000599 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000600 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000601 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000602 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000603 case Type::BlockPointer: {
604 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
605 Width = Target.getPointerWidth(AS);
606 Align = Target.getPointerAlign(AS);
607 break;
608 }
Sebastian Redl5d484e82009-11-23 17:18:46 +0000609 case Type::LValueReference:
610 case Type::RValueReference: {
611 // alignof and sizeof should never enter this code path here, so we go
612 // the pointer route.
613 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
614 Width = Target.getPointerWidth(AS);
615 Align = Target.getPointerAlign(AS);
616 break;
617 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000618 case Type::Pointer: {
619 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000620 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000621 Align = Target.getPointerAlign(AS);
622 break;
623 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000624 case Type::MemberPointer: {
Sebastian Redlf30208a2009-01-24 21:16:55 +0000625 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000626 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000627 getTypeInfo(getPointerDiffType());
628 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000629 if (Pointee->isFunctionType())
630 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000631 Align = PtrDiffInfo.second;
632 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000633 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000634 case Type::Complex: {
635 // Complex types have the same alignment as their elements, but twice the
636 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000637 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000638 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000639 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000640 Align = EltInfo.second;
641 break;
642 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000643 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000644 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000645 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
646 Width = Layout.getSize();
647 Align = Layout.getAlignment();
648 break;
649 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000650 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000651 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000652 const TagType *TT = cast<TagType>(T);
653
654 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000655 Width = 1;
656 Align = 1;
657 break;
658 }
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Daniel Dunbar1d751182008-11-08 05:48:37 +0000660 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000661 return getTypeInfo(ET->getDecl()->getIntegerType());
662
Daniel Dunbar1d751182008-11-08 05:48:37 +0000663 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000664 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
665 Width = Layout.getSize();
666 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000667 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000668 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000669
Chris Lattner9fcfe922009-10-22 05:17:15 +0000670 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000671 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
672 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000673
Chris Lattner9fcfe922009-10-22 05:17:15 +0000674 case Type::Elaborated:
675 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
676 .getTypePtr());
John McCall7da24312009-09-05 00:15:47 +0000677
Douglas Gregor18857642009-04-30 17:32:17 +0000678 case Type::Typedef: {
679 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000680 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000681 Align = std::max(Aligned->getMaxAlignment(),
682 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregor18857642009-04-30 17:32:17 +0000683 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
684 } else
685 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000686 break;
Chris Lattner71763312008-04-06 22:05:18 +0000687 }
Douglas Gregor18857642009-04-30 17:32:17 +0000688
689 case Type::TypeOfExpr:
690 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
691 .getTypePtr());
692
693 case Type::TypeOf:
694 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
695
Anders Carlsson395b4752009-06-24 19:06:50 +0000696 case Type::Decltype:
697 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
698 .getTypePtr());
699
Douglas Gregor18857642009-04-30 17:32:17 +0000700 case Type::QualifiedName:
701 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000702
John McCall3cb0ebd2010-03-10 03:28:59 +0000703 case Type::InjectedClassName:
704 return getTypeInfo(cast<InjectedClassNameType>(T)
705 ->getUnderlyingType().getTypePtr());
706
Douglas Gregor18857642009-04-30 17:32:17 +0000707 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000708 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000709 "Cannot request the size of a dependent type");
710 // FIXME: this is likely to be wrong once we support template
711 // aliases, since a template alias could refer to a typedef that
712 // has an __aligned__ attribute on it.
713 return getTypeInfo(getCanonicalType(T));
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattner464175b2007-07-18 17:52:12 +0000716 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000717 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000718}
719
Ken Dyckbdc601b2009-12-22 14:23:30 +0000720/// getTypeSizeInChars - Return the size of the specified type, in characters.
721/// This method does not work on incomplete types.
722CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000723 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000724}
725CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000726 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000727}
728
Ken Dyck16e20cc2010-01-26 17:25:18 +0000729/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck86fa4312010-01-26 17:22:55 +0000730/// characters. This method does not work on incomplete types.
731CharUnits ASTContext::getTypeAlignInChars(QualType T) {
732 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
733}
734CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
735 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
736}
737
Chris Lattner34ebde42009-01-27 18:08:34 +0000738/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
739/// type for the current target in bits. This can be different than the ABI
740/// alignment in cases where it is beneficial for performance to overalign
741/// a data type.
742unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
743 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000744
745 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000746 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000747 T = CT->getElementType().getTypePtr();
748 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
749 T->isSpecificBuiltinType(BuiltinType::LongLong))
750 return std::max(ABIAlign, (unsigned)getTypeSize(T));
751
Chris Lattner34ebde42009-01-27 18:08:34 +0000752 return ABIAlign;
753}
754
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000755static void CollectLocalObjCIvars(ASTContext *Ctx,
756 const ObjCInterfaceDecl *OI,
757 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000758 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
759 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000760 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000761 if (!IVDecl->isInvalidDecl())
762 Fields.push_back(cast<FieldDecl>(IVDecl));
763 }
764}
765
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000766void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
767 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
768 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
769 CollectObjCIvars(SuperClass, Fields);
770 CollectLocalObjCIvars(this, OI, Fields);
771}
772
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000773/// ShallowCollectObjCIvars -
774/// Collect all ivars, including those synthesized, in the current class.
775///
776void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000777 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000778 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
779 E = OI->ivar_end(); I != E; ++I) {
780 Ivars.push_back(*I);
781 }
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000782
783 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000784}
785
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000786/// CollectNonClassIvars -
787/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek7d2aa112010-03-11 19:44:54 +0000788/// This includes synthesized ivars (via @synthesize) and those in
789// class's @implementation.
Fariborz Jahanian98200742009-05-12 18:14:29 +0000790///
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000791void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian98200742009-05-12 18:14:29 +0000792 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000793 // Find ivars declared in class extension.
794 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
795 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
796 E = CDecl->ivar_end(); I != E; ++I) {
797 Ivars.push_back(*I);
798 }
799 }
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000800
Ted Kremenek7d2aa112010-03-11 19:44:54 +0000801 // Also add any ivar defined in this class's implementation. This
802 // includes synthesized ivars.
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000803 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
804 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
805 E = ImplDecl->ivar_end(); I != E; ++I)
806 Ivars.push_back(*I);
807 }
Fariborz Jahanian98200742009-05-12 18:14:29 +0000808}
809
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000810/// CollectInheritedProtocols - Collect all protocols in current class and
811/// those inherited by it.
812void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000813 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000814 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
815 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
816 PE = OI->protocol_end(); P != PE; ++P) {
817 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000818 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000819 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000820 PE = Proto->protocol_end(); P != PE; ++P) {
821 Protocols.insert(*P);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000822 CollectInheritedProtocols(*P, Protocols);
823 }
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000824 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000825
826 // Categories of this Interface.
827 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
828 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
829 CollectInheritedProtocols(CDeclChain, Protocols);
830 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
831 while (SD) {
832 CollectInheritedProtocols(SD, Protocols);
833 SD = SD->getSuperClass();
834 }
835 return;
836 }
837 if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
838 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
839 PE = OC->protocol_end(); P != PE; ++P) {
840 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000841 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000842 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
843 PE = Proto->protocol_end(); P != PE; ++P)
844 CollectInheritedProtocols(*P, Protocols);
845 }
846 return;
847 }
848 if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
849 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
850 PE = OP->protocol_end(); P != PE; ++P) {
851 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000852 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000853 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
854 PE = Proto->protocol_end(); P != PE; ++P)
855 CollectInheritedProtocols(*P, Protocols);
856 }
857 return;
858 }
859}
860
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000861unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
862 unsigned count = 0;
863 // Count ivars declared in class extension.
864 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
865 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
866 E = CDecl->ivar_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000867 ++count;
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000868 }
869 }
870
871 // Count ivar defined in this class's implementation. This
872 // includes synthesized ivars.
873 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
874 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
875 E = ImplDecl->ivar_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000876 ++count;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000877 return count;
878}
879
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000880/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
881ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
882 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
883 I = ObjCImpls.find(D);
884 if (I != ObjCImpls.end())
885 return cast<ObjCImplementationDecl>(I->second);
886 return 0;
887}
888/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
889ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
890 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
891 I = ObjCImpls.find(D);
892 if (I != ObjCImpls.end())
893 return cast<ObjCCategoryImplDecl>(I->second);
894 return 0;
895}
896
897/// \brief Set the implementation of ObjCInterfaceDecl.
898void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
899 ObjCImplementationDecl *ImplD) {
900 assert(IFaceD && ImplD && "Passed null params");
901 ObjCImpls[IFaceD] = ImplD;
902}
903/// \brief Set the implementation of ObjCCategoryDecl.
904void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
905 ObjCCategoryImplDecl *ImplD) {
906 assert(CatD && ImplD && "Passed null params");
907 ObjCImpls[CatD] = ImplD;
908}
909
John McCalla93c9342009-12-07 02:54:59 +0000910/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000911///
John McCalla93c9342009-12-07 02:54:59 +0000912/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000913/// the TypeLoc wrappers.
914///
915/// \param T the type that will be the basis for type source info. This type
916/// should refer to how the declarator was written in source code, not to
917/// what type semantic analysis resolved the declarator to.
John McCalla93c9342009-12-07 02:54:59 +0000918TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall109de5e2009-10-21 00:23:54 +0000919 unsigned DataSize) {
920 if (!DataSize)
921 DataSize = TypeLoc::getFullDataSizeForType(T);
922 else
923 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCalla93c9342009-12-07 02:54:59 +0000924 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall109de5e2009-10-21 00:23:54 +0000925
John McCalla93c9342009-12-07 02:54:59 +0000926 TypeSourceInfo *TInfo =
927 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
928 new (TInfo) TypeSourceInfo(T);
929 return TInfo;
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000930}
931
John McCalla93c9342009-12-07 02:54:59 +0000932TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCalla4eb74d2009-10-23 21:14:09 +0000933 SourceLocation L) {
John McCalla93c9342009-12-07 02:54:59 +0000934 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCalla4eb74d2009-10-23 21:14:09 +0000935 DI->getTypeLoc().initialize(L);
936 return DI;
937}
938
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000939/// getInterfaceLayoutImpl - Get or compute information about the
940/// layout of the given interface.
941///
942/// \param Impl - If given, also include the layout of the interface's
943/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000944const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000945ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
946 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000947 assert(!D->isForwardDecl() && "Invalid interface decl!");
948
Devang Patel44a3dde2008-06-04 21:54:36 +0000949 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000950 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000951 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
952 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
953 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000954
Daniel Dunbar453addb2009-05-03 11:16:44 +0000955 // Add in synthesized ivar count if laying out an implementation.
956 if (Impl) {
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000957 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000958 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000959 // entry. Note we can't cache this because we simply free all
960 // entries later; however we shouldn't look up implementations
961 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000962 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000963 return getObjCLayout(D, 0);
964 }
965
Mike Stump1eb44332009-09-09 15:08:12 +0000966 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000967 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
968 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Devang Patel44a3dde2008-06-04 21:54:36 +0000970 return *NewEntry;
971}
972
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000973const ASTRecordLayout &
974ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
975 return getObjCLayout(D, 0);
976}
977
978const ASTRecordLayout &
979ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
980 return getObjCLayout(D->getClassInterface(), D);
981}
982
Devang Patel88a981b2007-11-01 19:11:01 +0000983/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000984/// specified record (struct/union/class), which indicates its size and field
985/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000986const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000987 D = D->getDefinition();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000988 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000989
Chris Lattner464175b2007-07-18 17:52:12 +0000990 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +0000991 // Note that we can't save a reference to the entry because this function
992 // is recursive.
993 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000994 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000995
Mike Stump1eb44332009-09-09 15:08:12 +0000996 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000997 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +0000998 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattner5d2a6302007-07-18 18:26:58 +00001000 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001001}
1002
Anders Carlssonf53df232009-12-07 04:35:11 +00001003const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor952b0172010-02-11 01:04:33 +00001004 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlssonf53df232009-12-07 04:35:11 +00001005 assert(RD && "Cannot get key function for forward declarations!");
1006
1007 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1008 if (!Entry)
1009 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
1010 else
1011 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
1012 "Key function changed!");
1013
1014 return Entry;
1015}
1016
Chris Lattnera7674d82007-07-13 22:13:22 +00001017//===----------------------------------------------------------------------===//
1018// Type creation/memoization methods
1019//===----------------------------------------------------------------------===//
1020
John McCall0953e762009-09-24 19:53:00 +00001021QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1022 unsigned Fast = Quals.getFastQualifiers();
1023 Quals.removeFastQualifiers();
1024
1025 // Check if we've already instantiated this type.
1026 llvm::FoldingSetNodeID ID;
1027 ExtQuals::Profile(ID, TypeNode, Quals);
1028 void *InsertPos = 0;
1029 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1030 assert(EQ->getQualifiers() == Quals);
1031 QualType T = QualType(EQ, Fast);
1032 return T;
1033 }
1034
John McCall6b304a02009-09-24 23:30:46 +00001035 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001036 ExtQualNodes.InsertNode(New, InsertPos);
1037 QualType T = QualType(New, Fast);
1038 return T;
1039}
1040
1041QualType ASTContext::getVolatileType(QualType T) {
1042 QualType CanT = getCanonicalType(T);
1043 if (CanT.isVolatileQualified()) return T;
1044
1045 QualifierCollector Quals;
1046 const Type *TypeNode = Quals.strip(T);
1047 Quals.addVolatile();
1048
1049 return getExtQualType(TypeNode, Quals);
1050}
1051
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001052QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001053 QualType CanT = getCanonicalType(T);
1054 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001055 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001056
John McCall0953e762009-09-24 19:53:00 +00001057 // If we are composing extended qualifiers together, merge together
1058 // into one ExtQuals node.
1059 QualifierCollector Quals;
1060 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
John McCall0953e762009-09-24 19:53:00 +00001062 // If this type already has an address space specified, it cannot get
1063 // another one.
1064 assert(!Quals.hasAddressSpace() &&
1065 "Type cannot be in multiple addr spaces!");
1066 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
John McCall0953e762009-09-24 19:53:00 +00001068 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001069}
1070
Chris Lattnerb7d25532009-02-18 22:53:11 +00001071QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001072 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001073 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001074 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001075 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001077 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001078 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001079 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001080 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1081 return getPointerType(ResultType);
1082 }
1083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
John McCall0953e762009-09-24 19:53:00 +00001085 // If we are composing extended qualifiers together, merge together
1086 // into one ExtQuals node.
1087 QualifierCollector Quals;
1088 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001089
John McCall0953e762009-09-24 19:53:00 +00001090 // If this type already has an ObjCGC specified, it cannot get
1091 // another one.
1092 assert(!Quals.hasObjCGCAttr() &&
1093 "Type cannot have multiple ObjCGCs!");
1094 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001095
John McCall0953e762009-09-24 19:53:00 +00001096 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001097}
Chris Lattnera7674d82007-07-13 22:13:22 +00001098
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001099static QualType getNoReturnCallConvType(ASTContext& Context, QualType T,
1100 bool AddNoReturn,
1101 CallingConv CallConv) {
John McCall0953e762009-09-24 19:53:00 +00001102 QualType ResultType;
Douglas Gregor43c79c22009-12-09 00:47:37 +00001103 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1104 QualType Pointee = Pointer->getPointeeType();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001105 ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn,
1106 CallConv);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001107 if (ResultType == Pointee)
1108 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001109
1110 ResultType = Context.getPointerType(ResultType);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001111 } else if (const BlockPointerType *BlockPointer
1112 = T->getAs<BlockPointerType>()) {
1113 QualType Pointee = BlockPointer->getPointeeType();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001114 ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn,
1115 CallConv);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001116 if (ResultType == Pointee)
1117 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001118
1119 ResultType = Context.getBlockPointerType(ResultType);
1120 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
1121 if (F->getNoReturnAttr() == AddNoReturn && F->getCallConv() == CallConv)
Douglas Gregor43c79c22009-12-09 00:47:37 +00001122 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001123
Douglas Gregor43c79c22009-12-09 00:47:37 +00001124 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001125 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
1126 AddNoReturn, CallConv);
John McCall0953e762009-09-24 19:53:00 +00001127 } else {
Douglas Gregor43c79c22009-12-09 00:47:37 +00001128 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall0953e762009-09-24 19:53:00 +00001129 ResultType
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001130 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1131 FPT->getNumArgs(), FPT->isVariadic(),
1132 FPT->getTypeQuals(),
1133 FPT->hasExceptionSpec(),
1134 FPT->hasAnyExceptionSpec(),
1135 FPT->getNumExceptions(),
1136 FPT->exception_begin(),
1137 AddNoReturn, CallConv);
John McCall0953e762009-09-24 19:53:00 +00001138 }
Douglas Gregor43c79c22009-12-09 00:47:37 +00001139 } else
1140 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001141
1142 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1143}
1144
1145QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
1146 return getNoReturnCallConvType(*this, T, AddNoReturn, T.getCallConv());
1147}
1148
1149QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
1150 return getNoReturnCallConvType(*this, T, T.getNoReturnAttr(), CallConv);
Mike Stump24556362009-07-25 21:26:53 +00001151}
1152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153/// getComplexType - Return the uniqued reference to the type for a complex
1154/// number with the specified element type.
1155QualType ASTContext::getComplexType(QualType T) {
1156 // Unique pointers, to guarantee there is only one pointer of a particular
1157 // structure.
1158 llvm::FoldingSetNodeID ID;
1159 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 void *InsertPos = 0;
1162 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1163 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 // If the pointee type isn't canonical, this won't be a canonical type either,
1166 // so fill in the canonical type field.
1167 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001168 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001169 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 // Get the new insert position for the node we care about.
1172 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001173 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 }
John McCall6b304a02009-09-24 23:30:46 +00001175 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 Types.push_back(New);
1177 ComplexTypes.InsertNode(New, InsertPos);
1178 return QualType(New, 0);
1179}
1180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181/// getPointerType - Return the uniqued reference to the type for a pointer to
1182/// the specified type.
1183QualType ASTContext::getPointerType(QualType T) {
1184 // Unique pointers, to guarantee there is only one pointer of a particular
1185 // structure.
1186 llvm::FoldingSetNodeID ID;
1187 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 void *InsertPos = 0;
1190 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1191 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 // If the pointee type isn't canonical, this won't be a canonical type either,
1194 // so fill in the canonical type field.
1195 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001196 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001197 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 // Get the new insert position for the node we care about.
1200 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001201 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 }
John McCall6b304a02009-09-24 23:30:46 +00001203 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 Types.push_back(New);
1205 PointerTypes.InsertNode(New, InsertPos);
1206 return QualType(New, 0);
1207}
1208
Mike Stump1eb44332009-09-09 15:08:12 +00001209/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001210/// a pointer to the specified block.
1211QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001212 assert(T->isFunctionType() && "block of function types only");
1213 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001214 // structure.
1215 llvm::FoldingSetNodeID ID;
1216 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Steve Naroff5618bd42008-08-27 16:04:49 +00001218 void *InsertPos = 0;
1219 if (BlockPointerType *PT =
1220 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1221 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001222
1223 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001224 // type either so fill in the canonical type field.
1225 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001226 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001227 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Steve Naroff5618bd42008-08-27 16:04:49 +00001229 // Get the new insert position for the node we care about.
1230 BlockPointerType *NewIP =
1231 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001232 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001233 }
John McCall6b304a02009-09-24 23:30:46 +00001234 BlockPointerType *New
1235 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001236 Types.push_back(New);
1237 BlockPointerTypes.InsertNode(New, InsertPos);
1238 return QualType(New, 0);
1239}
1240
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001241/// getLValueReferenceType - Return the uniqued reference to the type for an
1242/// lvalue reference to the specified type.
John McCall54e14c42009-10-22 22:37:11 +00001243QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 // Unique pointers, to guarantee there is only one pointer of a particular
1245 // structure.
1246 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001247 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001248
1249 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001250 if (LValueReferenceType *RT =
1251 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001253
John McCall54e14c42009-10-22 22:37:11 +00001254 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 // If the referencee type isn't canonical, this won't be a canonical type
1257 // either, so fill in the canonical type field.
1258 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001259 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1260 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1261 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001262
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001264 LValueReferenceType *NewIP =
1265 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001266 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 }
1268
John McCall6b304a02009-09-24 23:30:46 +00001269 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001270 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1271 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001273 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001274
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001275 return QualType(New, 0);
1276}
1277
1278/// getRValueReferenceType - Return the uniqued reference to the type for an
1279/// rvalue reference to the specified type.
1280QualType ASTContext::getRValueReferenceType(QualType T) {
1281 // Unique pointers, to guarantee there is only one pointer of a particular
1282 // structure.
1283 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001284 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001285
1286 void *InsertPos = 0;
1287 if (RValueReferenceType *RT =
1288 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1289 return QualType(RT, 0);
1290
John McCall54e14c42009-10-22 22:37:11 +00001291 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1292
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001293 // If the referencee type isn't canonical, this won't be a canonical type
1294 // either, so fill in the canonical type field.
1295 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001296 if (InnerRef || !T.isCanonical()) {
1297 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1298 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001299
1300 // Get the new insert position for the node we care about.
1301 RValueReferenceType *NewIP =
1302 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1303 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1304 }
1305
John McCall6b304a02009-09-24 23:30:46 +00001306 RValueReferenceType *New
1307 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001308 Types.push_back(New);
1309 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 return QualType(New, 0);
1311}
1312
Sebastian Redlf30208a2009-01-24 21:16:55 +00001313/// getMemberPointerType - Return the uniqued reference to the type for a
1314/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001315QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001316 // Unique pointers, to guarantee there is only one pointer of a particular
1317 // structure.
1318 llvm::FoldingSetNodeID ID;
1319 MemberPointerType::Profile(ID, T, Cls);
1320
1321 void *InsertPos = 0;
1322 if (MemberPointerType *PT =
1323 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1324 return QualType(PT, 0);
1325
1326 // If the pointee or class type isn't canonical, this won't be a canonical
1327 // type either, so fill in the canonical type field.
1328 QualType Canonical;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001329 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001330 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1331
1332 // Get the new insert position for the node we care about.
1333 MemberPointerType *NewIP =
1334 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1335 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1336 }
John McCall6b304a02009-09-24 23:30:46 +00001337 MemberPointerType *New
1338 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001339 Types.push_back(New);
1340 MemberPointerTypes.InsertNode(New, InsertPos);
1341 return QualType(New, 0);
1342}
1343
Mike Stump1eb44332009-09-09 15:08:12 +00001344/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001345/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001346QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001347 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001348 ArrayType::ArraySizeModifier ASM,
1349 unsigned EltTypeQuals) {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001350 assert((EltTy->isDependentType() ||
1351 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedman587cbdf2009-05-29 20:17:55 +00001352 "Constant array of VLAs is illegal!");
1353
Chris Lattner38aeec72009-05-13 04:12:56 +00001354 // Convert the array size into a canonical width matching the pointer size for
1355 // the target.
1356 llvm::APInt ArySize(ArySizeIn);
1357 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001360 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001363 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001364 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001365 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 // If the element type isn't canonical, this won't be a canonical type either,
1368 // so fill in the canonical type field.
1369 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001370 if (!EltTy.isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001371 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001372 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001374 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001375 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001376 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 }
Mike Stump1eb44332009-09-09 15:08:12 +00001378
John McCall6b304a02009-09-24 23:30:46 +00001379 ConstantArrayType *New = new(*this,TypeAlignment)
1380 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001381 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001382 Types.push_back(New);
1383 return QualType(New, 0);
1384}
1385
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001386/// getVariableArrayType - Returns a non-unique reference to the type for a
1387/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001388QualType ASTContext::getVariableArrayType(QualType EltTy,
1389 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001390 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001391 unsigned EltTypeQuals,
1392 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001393 // Since we don't unique expressions, it isn't possible to unique VLA's
1394 // that have an expression provided for their size.
1395
John McCall6b304a02009-09-24 23:30:46 +00001396 VariableArrayType *New = new(*this, TypeAlignment)
1397 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001398
1399 VariableArrayTypes.push_back(New);
1400 Types.push_back(New);
1401 return QualType(New, 0);
1402}
1403
Douglas Gregor898574e2008-12-05 23:32:09 +00001404/// getDependentSizedArrayType - Returns a non-unique reference to
1405/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001406/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001407QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1408 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001409 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001410 unsigned EltTypeQuals,
1411 SourceRange Brackets) {
Douglas Gregorcb78d882009-11-19 18:03:26 +00001412 assert((!NumElts || NumElts->isTypeDependent() ||
1413 NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001414 "Size must be type- or value-dependent!");
1415
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001416 void *InsertPos = 0;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001417 DependentSizedArrayType *Canon = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00001418 llvm::FoldingSetNodeID ID;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001419
1420 if (NumElts) {
1421 // Dependently-sized array types that do not have a specified
1422 // number of elements will have their sizes deduced from an
1423 // initializer.
Douglas Gregorcb78d882009-11-19 18:03:26 +00001424 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1425 EltTypeQuals, NumElts);
1426
1427 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1428 }
1429
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001430 DependentSizedArrayType *New;
1431 if (Canon) {
1432 // We already have a canonical version of this array type; use it as
1433 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001434 New = new (*this, TypeAlignment)
1435 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1436 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001437 } else {
1438 QualType CanonEltTy = getCanonicalType(EltTy);
1439 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001440 New = new (*this, TypeAlignment)
1441 DependentSizedArrayType(*this, EltTy, QualType(),
1442 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001443
Douglas Gregor789b1f62010-02-04 18:10:26 +00001444 if (NumElts) {
1445 DependentSizedArrayType *CanonCheck
1446 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1447 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1448 (void)CanonCheck;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001449 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001450 }
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001451 } else {
1452 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1453 ASM, EltTypeQuals,
1454 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001455 New = new (*this, TypeAlignment)
1456 DependentSizedArrayType(*this, EltTy, Canon,
1457 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001458 }
1459 }
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregor898574e2008-12-05 23:32:09 +00001461 Types.push_back(New);
1462 return QualType(New, 0);
1463}
1464
Eli Friedmanc5773c42008-02-15 18:16:39 +00001465QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1466 ArrayType::ArraySizeModifier ASM,
1467 unsigned EltTypeQuals) {
1468 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001469 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001470
1471 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001472 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001473 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1474 return QualType(ATP, 0);
1475
1476 // If the element type isn't canonical, this won't be a canonical type
1477 // either, so fill in the canonical type field.
1478 QualType Canonical;
1479
John McCall467b27b2009-10-22 20:10:53 +00001480 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001481 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001482 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001483
1484 // Get the new insert position for the node we care about.
1485 IncompleteArrayType *NewIP =
1486 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001487 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001488 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001489
John McCall6b304a02009-09-24 23:30:46 +00001490 IncompleteArrayType *New = new (*this, TypeAlignment)
1491 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001492
1493 IncompleteArrayTypes.InsertNode(New, InsertPos);
1494 Types.push_back(New);
1495 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001496}
1497
Steve Naroff73322922007-07-18 18:00:27 +00001498/// getVectorType - Return the unique reference to a vector type of
1499/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001500QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1501 bool IsAltiVec, bool IsPixel) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001502 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Chris Lattnerf52ab252008-04-06 22:59:24 +00001504 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001505 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Reid Spencer5f016e22007-07-11 17:01:13 +00001507 // Check if we've already instantiated a vector of this type.
1508 llvm::FoldingSetNodeID ID;
John Thompson82287d12010-02-05 00:12:22 +00001509 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1510 IsAltiVec, IsPixel);
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 void *InsertPos = 0;
1512 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1513 return QualType(VTP, 0);
1514
1515 // If the element type isn't canonical, this won't be a canonical type either,
1516 // so fill in the canonical type field.
1517 QualType Canonical;
John Thompson82287d12010-02-05 00:12:22 +00001518 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1519 Canonical = getVectorType(getCanonicalType(vecType),
1520 NumElts, false, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Reid Spencer5f016e22007-07-11 17:01:13 +00001522 // Get the new insert position for the node we care about.
1523 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001524 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001525 }
John McCall6b304a02009-09-24 23:30:46 +00001526 VectorType *New = new (*this, TypeAlignment)
John Thompson82287d12010-02-05 00:12:22 +00001527 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 VectorTypes.InsertNode(New, InsertPos);
1529 Types.push_back(New);
1530 return QualType(New, 0);
1531}
1532
Nate Begeman213541a2008-04-18 23:10:10 +00001533/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001534/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001535QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001536 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Chris Lattnerf52ab252008-04-06 22:59:24 +00001538 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001539 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Steve Naroff73322922007-07-18 18:00:27 +00001541 // Check if we've already instantiated a vector of this type.
1542 llvm::FoldingSetNodeID ID;
John Thompson82287d12010-02-05 00:12:22 +00001543 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff73322922007-07-18 18:00:27 +00001544 void *InsertPos = 0;
1545 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1546 return QualType(VTP, 0);
1547
1548 // If the element type isn't canonical, this won't be a canonical type either,
1549 // so fill in the canonical type field.
1550 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001551 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001552 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Steve Naroff73322922007-07-18 18:00:27 +00001554 // Get the new insert position for the node we care about.
1555 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001556 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001557 }
John McCall6b304a02009-09-24 23:30:46 +00001558 ExtVectorType *New = new (*this, TypeAlignment)
1559 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001560 VectorTypes.InsertNode(New, InsertPos);
1561 Types.push_back(New);
1562 return QualType(New, 0);
1563}
1564
Mike Stump1eb44332009-09-09 15:08:12 +00001565QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001566 Expr *SizeExpr,
1567 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001568 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001569 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001570 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001572 void *InsertPos = 0;
1573 DependentSizedExtVectorType *Canon
1574 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1575 DependentSizedExtVectorType *New;
1576 if (Canon) {
1577 // We already have a canonical version of this array type; use it as
1578 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001579 New = new (*this, TypeAlignment)
1580 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1581 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001582 } else {
1583 QualType CanonVecTy = getCanonicalType(vecType);
1584 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001585 New = new (*this, TypeAlignment)
1586 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1587 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001588
1589 DependentSizedExtVectorType *CanonCheck
1590 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1591 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1592 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001593 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1594 } else {
1595 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1596 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001597 New = new (*this, TypeAlignment)
1598 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001599 }
1600 }
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001602 Types.push_back(New);
1603 return QualType(New, 0);
1604}
1605
Douglas Gregor72564e72009-02-26 23:50:07 +00001606/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001607///
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001608QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn,
1609 CallingConv CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001610 // Unique functions, to guarantee there is only one function of a particular
1611 // structure.
1612 llvm::FoldingSetNodeID ID;
John McCallf82b4e82010-02-04 05:44:44 +00001613 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn, CallConv);
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Reid Spencer5f016e22007-07-11 17:01:13 +00001615 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001616 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001617 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001621 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001622 getCanonicalCallConv(CallConv) != CallConv) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001623 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn,
John McCall04a67a62010-02-05 21:31:56 +00001624 getCanonicalCallConv(CallConv));
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001627 FunctionNoProtoType *NewIP =
1628 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001629 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 }
Mike Stump1eb44332009-09-09 15:08:12 +00001631
John McCall6b304a02009-09-24 23:30:46 +00001632 FunctionNoProtoType *New = new (*this, TypeAlignment)
John McCallf82b4e82010-02-04 05:44:44 +00001633 FunctionNoProtoType(ResultTy, Canonical, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001635 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 return QualType(New, 0);
1637}
1638
1639/// getFunctionType - Return a normal function type with a typed argument
1640/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001641QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001642 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001643 unsigned TypeQuals, bool hasExceptionSpec,
1644 bool hasAnyExceptionSpec, unsigned NumExs,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001645 const QualType *ExArray, bool NoReturn,
1646 CallingConv CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 // Unique functions, to guarantee there is only one function of a particular
1648 // structure.
1649 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001650 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001651 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
John McCallf82b4e82010-02-04 05:44:44 +00001652 NumExs, ExArray, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001653
1654 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001655 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001656 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001658
1659 // Determine whether the type being created is already canonical or not.
John McCall54e14c42009-10-22 22:37:11 +00001660 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001662 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 isCanonical = false;
1664
1665 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001666 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001668 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 llvm::SmallVector<QualType, 16> CanonicalArgs;
1670 CanonicalArgs.reserve(NumArgs);
1671 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001672 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001673
Chris Lattnerf52ab252008-04-06 22:59:24 +00001674 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001675 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001676 isVariadic, TypeQuals, false,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001677 false, 0, 0, NoReturn,
John McCall04a67a62010-02-05 21:31:56 +00001678 getCanonicalCallConv(CallConv));
Sebastian Redl465226e2009-05-27 22:11:52 +00001679
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001681 FunctionProtoType *NewIP =
1682 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001683 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001685
Douglas Gregor72564e72009-02-26 23:50:07 +00001686 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001687 // for two variable size arrays (for parameter and exception types) at the
1688 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001689 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001690 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1691 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001692 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001693 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001694 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001695 ExArray, NumExs, Canonical, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001697 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 return QualType(FTP, 0);
1699}
1700
John McCall3cb0ebd2010-03-10 03:28:59 +00001701#ifndef NDEBUG
1702static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1703 if (!isa<CXXRecordDecl>(D)) return false;
1704 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1705 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1706 return true;
1707 if (RD->getDescribedClassTemplate() &&
1708 !isa<ClassTemplateSpecializationDecl>(RD))
1709 return true;
1710 return false;
1711}
1712#endif
1713
1714/// getInjectedClassNameType - Return the unique reference to the
1715/// injected class name type for the specified templated declaration.
1716QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1717 QualType TST) {
1718 assert(NeedsInjectedClassNameType(Decl));
1719 if (Decl->TypeForDecl) {
1720 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1721 } else if (CXXRecordDecl *PrevDecl
1722 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1723 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1724 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1725 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1726 } else {
1727 Decl->TypeForDecl = new (*this, TypeAlignment)
1728 InjectedClassNameType(Decl, TST, TST->getCanonicalTypeInternal());
1729 Types.push_back(Decl->TypeForDecl);
1730 }
1731 return QualType(Decl->TypeForDecl, 0);
1732}
1733
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001734/// getTypeDeclType - Return the unique reference to the type for the
1735/// specified type declaration.
John McCallbecb8d52010-03-10 06:48:02 +00001736QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001737 assert(Decl && "Passed null for Decl param");
John McCallbecb8d52010-03-10 06:48:02 +00001738 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump1eb44332009-09-09 15:08:12 +00001739
John McCall19c85762010-02-16 03:57:14 +00001740 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001741 return getTypedefType(Typedef);
John McCallbecb8d52010-03-10 06:48:02 +00001742
1743 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stump9fdbab32009-07-31 02:02:20 +00001744 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001745 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001746
John McCallbecb8d52010-03-10 06:48:02 +00001747 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1748 "Template type parameter types are always available.");
1749
John McCall19c85762010-02-16 03:57:14 +00001750 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001751 assert(!Record->getPreviousDeclaration() &&
1752 "struct/union has previous declaration");
1753 assert(!NeedsInjectedClassNameType(Record));
1754 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00001755 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001756 assert(!Enum->getPreviousDeclaration() &&
1757 "enum has previous declaration");
1758 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00001759 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00001760 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1761 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stump9fdbab32009-07-31 02:02:20 +00001762 } else
John McCallbecb8d52010-03-10 06:48:02 +00001763 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001764
John McCallbecb8d52010-03-10 06:48:02 +00001765 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001766 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001767}
1768
Reid Spencer5f016e22007-07-11 17:01:13 +00001769/// getTypedefType - Return the unique reference to the type for the
1770/// specified typename decl.
John McCall19c85762010-02-16 03:57:14 +00001771QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Chris Lattnerf52ab252008-04-06 22:59:24 +00001774 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001775 Decl->TypeForDecl = new(*this, TypeAlignment)
1776 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 Types.push_back(Decl->TypeForDecl);
1778 return QualType(Decl->TypeForDecl, 0);
1779}
1780
John McCall49a832b2009-10-18 09:09:24 +00001781/// \brief Retrieve a substitution-result type.
1782QualType
1783ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1784 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001785 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001786 && "replacement types must always be canonical");
1787
1788 llvm::FoldingSetNodeID ID;
1789 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1790 void *InsertPos = 0;
1791 SubstTemplateTypeParmType *SubstParm
1792 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1793
1794 if (!SubstParm) {
1795 SubstParm = new (*this, TypeAlignment)
1796 SubstTemplateTypeParmType(Parm, Replacement);
1797 Types.push_back(SubstParm);
1798 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1799 }
1800
1801 return QualType(SubstParm, 0);
1802}
1803
Douglas Gregorfab9d672009-02-05 23:33:38 +00001804/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001805/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001806/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001807QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001808 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001809 IdentifierInfo *Name) {
1810 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001811 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001812 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001813 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001814 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1815
1816 if (TypeParm)
1817 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001819 if (Name) {
1820 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001821 TypeParm = new (*this, TypeAlignment)
1822 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001823
1824 TemplateTypeParmType *TypeCheck
1825 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1826 assert(!TypeCheck && "Template type parameter canonical type broken");
1827 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001828 } else
John McCall6b304a02009-09-24 23:30:46 +00001829 TypeParm = new (*this, TypeAlignment)
1830 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001831
1832 Types.push_back(TypeParm);
1833 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1834
1835 return QualType(TypeParm, 0);
1836}
1837
John McCall3cb0ebd2010-03-10 03:28:59 +00001838TypeSourceInfo *
1839ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1840 SourceLocation NameLoc,
1841 const TemplateArgumentListInfo &Args,
1842 QualType CanonType) {
1843 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1844
1845 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1846 TemplateSpecializationTypeLoc TL
1847 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1848 TL.setTemplateNameLoc(NameLoc);
1849 TL.setLAngleLoc(Args.getLAngleLoc());
1850 TL.setRAngleLoc(Args.getRAngleLoc());
1851 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1852 TL.setArgLocInfo(i, Args[i].getLocInfo());
1853 return DI;
1854}
1855
Mike Stump1eb44332009-09-09 15:08:12 +00001856QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001857ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00001858 const TemplateArgumentListInfo &Args,
John McCall833ca992009-10-29 08:12:44 +00001859 QualType Canon) {
John McCalld5532b62009-11-23 01:53:49 +00001860 unsigned NumArgs = Args.size();
1861
John McCall833ca992009-10-29 08:12:44 +00001862 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1863 ArgVec.reserve(NumArgs);
1864 for (unsigned i = 0; i != NumArgs; ++i)
1865 ArgVec.push_back(Args[i].getArgument());
1866
1867 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon);
1868}
1869
1870QualType
1871ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001872 const TemplateArgument *Args,
1873 unsigned NumArgs,
1874 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001875 if (!Canon.isNull())
1876 Canon = getCanonicalType(Canon);
1877 else {
1878 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001879 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1880 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1881 CanonArgs.reserve(NumArgs);
1882 for (unsigned I = 0; I != NumArgs; ++I)
1883 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1884
1885 // Determine whether this canonical template specialization type already
1886 // exists.
1887 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001888 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001889 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001890
1891 void *InsertPos = 0;
1892 TemplateSpecializationType *Spec
1893 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Douglas Gregor1275ae02009-07-28 23:00:59 +00001895 if (!Spec) {
1896 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001897 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001898 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001899 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001900 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001901 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001902 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001903 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001904 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001905 }
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Douglas Gregorb88e8882009-07-30 17:40:51 +00001907 if (Canon.isNull())
1908 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001909 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001910 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001911 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001912
Douglas Gregor1275ae02009-07-28 23:00:59 +00001913 // Allocate the (non-canonical) template specialization type, but don't
1914 // try to unique it: these types typically have location information that
1915 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001916 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001917 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001918 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001919 TemplateSpecializationType *Spec
1920 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001921 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Douglas Gregor55f6b142009-02-09 18:46:07 +00001923 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001924 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001925}
1926
Mike Stump1eb44332009-09-09 15:08:12 +00001927QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001928ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001929 QualType NamedType) {
1930 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001931 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001932
1933 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001934 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001935 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1936 if (T)
1937 return QualType(T, 0);
1938
Douglas Gregor789b1f62010-02-04 18:10:26 +00001939 QualType Canon = NamedType;
1940 if (!Canon.isCanonical()) {
1941 Canon = getCanonicalType(NamedType);
1942 QualifiedNameType *CheckT
1943 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1944 assert(!CheckT && "Qualified name canonical type broken");
1945 (void)CheckT;
1946 }
1947
1948 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001949 Types.push_back(T);
1950 QualifiedNameTypes.InsertNode(T, InsertPos);
1951 return QualType(T, 0);
1952}
1953
Mike Stump1eb44332009-09-09 15:08:12 +00001954QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001955 const IdentifierInfo *Name,
1956 QualType Canon) {
1957 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1958
1959 if (Canon.isNull()) {
1960 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1961 if (CanonNNS != NNS)
1962 Canon = getTypenameType(CanonNNS, Name);
1963 }
1964
1965 llvm::FoldingSetNodeID ID;
1966 TypenameType::Profile(ID, NNS, Name);
1967
1968 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001969 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001970 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1971 if (T)
1972 return QualType(T, 0);
1973
1974 T = new (*this) TypenameType(NNS, Name, Canon);
1975 Types.push_back(T);
1976 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001977 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001978}
1979
Mike Stump1eb44332009-09-09 15:08:12 +00001980QualType
1981ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001982 const TemplateSpecializationType *TemplateId,
1983 QualType Canon) {
1984 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1985
Douglas Gregor789b1f62010-02-04 18:10:26 +00001986 llvm::FoldingSetNodeID ID;
1987 TypenameType::Profile(ID, NNS, TemplateId);
1988
1989 void *InsertPos = 0;
1990 TypenameType *T
1991 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1992 if (T)
1993 return QualType(T, 0);
1994
Douglas Gregor17343172009-04-01 00:28:59 +00001995 if (Canon.isNull()) {
1996 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1997 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1998 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1999 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00002000 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00002001 assert(CanonTemplateId &&
2002 "Canonical type must also be a template specialization type");
2003 Canon = getTypenameType(CanonNNS, CanonTemplateId);
2004 }
Douglas Gregor789b1f62010-02-04 18:10:26 +00002005
2006 TypenameType *CheckT
2007 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
2008 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregor17343172009-04-01 00:28:59 +00002009 }
2010
Douglas Gregor17343172009-04-01 00:28:59 +00002011 T = new (*this) TypenameType(NNS, TemplateId, Canon);
2012 Types.push_back(T);
2013 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002014 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002015}
2016
John McCall7da24312009-09-05 00:15:47 +00002017QualType
2018ASTContext::getElaboratedType(QualType UnderlyingType,
2019 ElaboratedType::TagKind Tag) {
2020 llvm::FoldingSetNodeID ID;
2021 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00002022
John McCall7da24312009-09-05 00:15:47 +00002023 void *InsertPos = 0;
2024 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2025 if (T)
2026 return QualType(T, 0);
2027
Douglas Gregor789b1f62010-02-04 18:10:26 +00002028 QualType Canon = UnderlyingType;
2029 if (!Canon.isCanonical()) {
2030 Canon = getCanonicalType(Canon);
2031 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2032 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2033 }
John McCall7da24312009-09-05 00:15:47 +00002034
2035 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2036 Types.push_back(T);
2037 ElaboratedTypes.InsertNode(T, InsertPos);
2038 return QualType(T, 0);
2039}
2040
Chris Lattner88cb27a2008-04-07 04:56:42 +00002041/// CmpProtocolNames - Comparison predicate for sorting protocols
2042/// alphabetically.
2043static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2044 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002045 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00002046}
2047
John McCall54e14c42009-10-22 22:37:11 +00002048static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2049 unsigned NumProtocols) {
2050 if (NumProtocols == 0) return true;
2051
2052 for (unsigned i = 1; i != NumProtocols; ++i)
2053 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2054 return false;
2055 return true;
2056}
2057
2058static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00002059 unsigned &NumProtocols) {
2060 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Chris Lattner88cb27a2008-04-07 04:56:42 +00002062 // Sort protocols, keyed by name.
2063 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2064
2065 // Remove duplicates.
2066 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2067 NumProtocols = ProtocolsEnd-Protocols;
2068}
2069
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002070/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2071/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00002072QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00002073 ObjCProtocolDecl **Protocols,
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002074 unsigned NumProtocols,
2075 unsigned Quals) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002076 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00002077 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002078 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002079
2080 void *InsertPos = 0;
2081 if (ObjCObjectPointerType *QT =
2082 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002083 return getQualifiedType(QualType(QT, 0), Qs);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002084
John McCall54e14c42009-10-22 22:37:11 +00002085 // Sort the protocol list alphabetically to canonicalize it.
2086 QualType Canonical;
2087 if (!InterfaceT.isCanonical() ||
2088 !areSortedAndUniqued(Protocols, NumProtocols)) {
2089 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
2090 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2091 unsigned UniqueCount = NumProtocols;
2092
2093 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2094 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2095
2096 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2097 &Sorted[0], UniqueCount);
2098 } else {
2099 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2100 Protocols, NumProtocols);
2101 }
2102
2103 // Regenerate InsertPos.
2104 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2105 }
2106
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002107 // No match.
2108 unsigned Size = sizeof(ObjCObjectPointerType)
2109 + NumProtocols * sizeof(ObjCProtocolDecl *);
2110 void *Mem = Allocate(Size, TypeAlignment);
2111 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2112 InterfaceT,
2113 Protocols,
2114 NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002116 Types.push_back(QType);
2117 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002118 return getQualifiedType(QualType(QType, 0), Qs);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002119}
Chris Lattner88cb27a2008-04-07 04:56:42 +00002120
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002121/// getObjCInterfaceType - Return the unique reference to the type for the
2122/// specified ObjC interface decl. The list of protocols is optional.
2123QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002124 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002125 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002126 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002128 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002129 if (ObjCInterfaceType *QT =
2130 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002131 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002132
John McCall54e14c42009-10-22 22:37:11 +00002133 // Sort the protocol list alphabetically to canonicalize it.
2134 QualType Canonical;
2135 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2136 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2137 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2138
2139 unsigned UniqueCount = NumProtocols;
2140 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2141
2142 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2143
2144 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2145 }
2146
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002147 unsigned Size = sizeof(ObjCInterfaceType)
2148 + NumProtocols * sizeof(ObjCProtocolDecl *);
2149 void *Mem = Allocate(Size, TypeAlignment);
2150 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2151 const_cast<ObjCInterfaceDecl*>(Decl),
2152 Protocols,
2153 NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002154
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002155 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002156 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002157 return QualType(QType, 0);
2158}
2159
Douglas Gregor72564e72009-02-26 23:50:07 +00002160/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2161/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002162/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002163/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002164/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002165QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002166 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002167 if (tofExpr->isTypeDependent()) {
2168 llvm::FoldingSetNodeID ID;
2169 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Douglas Gregorb1975722009-07-30 23:18:24 +00002171 void *InsertPos = 0;
2172 DependentTypeOfExprType *Canon
2173 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2174 if (Canon) {
2175 // We already have a "canonical" version of an identical, dependent
2176 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002177 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002178 QualType((TypeOfExprType*)Canon, 0));
2179 }
2180 else {
2181 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002182 Canon
2183 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002184 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2185 toe = Canon;
2186 }
2187 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002188 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002189 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002190 }
Steve Naroff9752f252007-08-01 18:02:17 +00002191 Types.push_back(toe);
2192 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002193}
2194
Steve Naroff9752f252007-08-01 18:02:17 +00002195/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2196/// TypeOfType AST's. The only motivation to unique these nodes would be
2197/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002198/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002199/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002200QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002201 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002202 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002203 Types.push_back(tot);
2204 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002205}
2206
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002207/// getDecltypeForExpr - Given an expr, will return the decltype for that
2208/// expression, according to the rules in C++0x [dcl.type.simple]p4
2209static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002210 if (e->isTypeDependent())
2211 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002213 // If e is an id expression or a class member access, decltype(e) is defined
2214 // as the type of the entity named by e.
2215 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2216 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2217 return VD->getType();
2218 }
2219 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2220 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2221 return FD->getType();
2222 }
2223 // If e is a function call or an invocation of an overloaded operator,
2224 // (parentheses around e are ignored), decltype(e) is defined as the
2225 // return type of that function.
2226 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2227 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002229 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002230
2231 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002232 // defined as T&, otherwise decltype(e) is defined as T.
2233 if (e->isLvalue(Context) == Expr::LV_Valid)
2234 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002236 return T;
2237}
2238
Anders Carlsson395b4752009-06-24 19:06:50 +00002239/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2240/// DecltypeType AST's. The only motivation to unique these nodes would be
2241/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002242/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002243/// on canonical type's (which are always unique).
2244QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002245 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002246 if (e->isTypeDependent()) {
2247 llvm::FoldingSetNodeID ID;
2248 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002249
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002250 void *InsertPos = 0;
2251 DependentDecltypeType *Canon
2252 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2253 if (Canon) {
2254 // We already have a "canonical" version of an equivalent, dependent
2255 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002256 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002257 QualType((DecltypeType*)Canon, 0));
2258 }
2259 else {
2260 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002261 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002262 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2263 dt = Canon;
2264 }
2265 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002266 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002267 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002268 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002269 Types.push_back(dt);
2270 return QualType(dt, 0);
2271}
2272
Reid Spencer5f016e22007-07-11 17:01:13 +00002273/// getTagDeclType - Return the unique reference to the type for the
2274/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002275QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002276 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002277 // FIXME: What is the design on getTagDeclType when it requires casting
2278 // away const? mutable?
2279 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002280}
2281
Mike Stump1eb44332009-09-09 15:08:12 +00002282/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2283/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2284/// needs to agree with the definition in <stddef.h>.
Anders Carlssona3ccda52009-12-12 00:26:23 +00002285CanQualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002286 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002287}
2288
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002289/// getSignedWCharType - Return the type of "signed wchar_t".
2290/// Used when in C++, as a GCC extension.
2291QualType ASTContext::getSignedWCharType() const {
2292 // FIXME: derive from "Target" ?
2293 return WCharTy;
2294}
2295
2296/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2297/// Used when in C++, as a GCC extension.
2298QualType ASTContext::getUnsignedWCharType() const {
2299 // FIXME: derive from "Target" ?
2300 return UnsignedIntTy;
2301}
2302
Chris Lattner8b9023b2007-07-13 03:05:23 +00002303/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2304/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2305QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002306 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002307}
2308
Chris Lattnere6327742008-04-02 05:18:44 +00002309//===----------------------------------------------------------------------===//
2310// Type Operators
2311//===----------------------------------------------------------------------===//
2312
John McCall54e14c42009-10-22 22:37:11 +00002313CanQualType ASTContext::getCanonicalParamType(QualType T) {
2314 // Push qualifiers into arrays, and then discard any remaining
2315 // qualifiers.
2316 T = getCanonicalType(T);
2317 const Type *Ty = T.getTypePtr();
2318
2319 QualType Result;
2320 if (isa<ArrayType>(Ty)) {
2321 Result = getArrayDecayedType(QualType(Ty,0));
2322 } else if (isa<FunctionType>(Ty)) {
2323 Result = getPointerType(QualType(Ty, 0));
2324 } else {
2325 Result = QualType(Ty, 0);
2326 }
2327
2328 return CanQualType::CreateUnsafe(Result);
2329}
2330
Chris Lattner77c96472008-04-06 22:41:35 +00002331/// getCanonicalType - Return the canonical (structural) type corresponding to
2332/// the specified potentially non-canonical type. The non-canonical version
2333/// of a type may have many "decorated" versions of types. Decorators can
2334/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2335/// to be free of any of these, allowing two canonical types to be compared
2336/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002337CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002338 QualifierCollector Quals;
2339 const Type *Ptr = Quals.strip(T);
2340 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002341
John McCall0953e762009-09-24 19:53:00 +00002342 // The canonical internal type will be the canonical type *except*
2343 // that we push type qualifiers down through array types.
2344
2345 // If there are no new qualifiers to push down, stop here.
2346 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002347 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002348
John McCall0953e762009-09-24 19:53:00 +00002349 // If the type qualifiers are on an array type, get the canonical
2350 // type of the array with the qualifiers applied to the element
2351 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002352 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2353 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002354 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002356 // Get the canonical version of the element with the extra qualifiers on it.
2357 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002358 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002359 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002361 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002362 return CanQualType::CreateUnsafe(
2363 getConstantArrayType(NewEltTy, CAT->getSize(),
2364 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002365 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002366 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002367 return CanQualType::CreateUnsafe(
2368 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002369 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002370
Douglas Gregor898574e2008-12-05 23:32:09 +00002371 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002372 return CanQualType::CreateUnsafe(
2373 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002374 DSAT->getSizeExpr() ?
2375 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002376 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002377 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor87a924e2009-10-30 22:56:57 +00002378 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor898574e2008-12-05 23:32:09 +00002379
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002380 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002381 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002382 VAT->getSizeExpr() ?
2383 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002384 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002385 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002386 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002387}
2388
Chandler Carruth28e318c2009-12-29 07:16:59 +00002389QualType ASTContext::getUnqualifiedArrayType(QualType T,
2390 Qualifiers &Quals) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002391 Quals = T.getQualifiers();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002392 if (!isa<ArrayType>(T)) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002393 return T.getUnqualifiedType();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002394 }
2395
Chandler Carruth28e318c2009-12-29 07:16:59 +00002396 const ArrayType *AT = cast<ArrayType>(T);
2397 QualType Elt = AT->getElementType();
Zhongxing Xuc1ae0a82010-01-05 08:15:06 +00002398 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002399 if (Elt == UnqualElt)
2400 return T;
2401
2402 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2403 return getConstantArrayType(UnqualElt, CAT->getSize(),
2404 CAT->getSizeModifier(), 0);
2405 }
2406
2407 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2408 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2409 }
2410
2411 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2412 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2413 DSAT->getSizeModifier(), 0,
2414 SourceRange());
2415}
2416
John McCall80ad16f2009-11-24 18:42:40 +00002417DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2418 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2419 return TD->getDeclName();
2420
2421 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2422 if (DTN->isIdentifier()) {
2423 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2424 } else {
2425 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2426 }
2427 }
2428
John McCall0bd6feb2009-12-02 08:04:21 +00002429 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2430 assert(Storage);
2431 return (*Storage->begin())->getDeclName();
John McCall80ad16f2009-11-24 18:42:40 +00002432}
2433
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002434TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2435 // If this template name refers to a template, the canonical
2436 // template name merely stores the template itself.
2437 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002438 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002439
John McCall0bd6feb2009-12-02 08:04:21 +00002440 assert(!Name.getAsOverloadedTemplate());
Mike Stump1eb44332009-09-09 15:08:12 +00002441
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002442 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2443 assert(DTN && "Non-dependent template names must refer to template decls.");
2444 return DTN->CanonicalTemplateName;
2445}
2446
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002447bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2448 X = getCanonicalTemplateName(X);
2449 Y = getCanonicalTemplateName(Y);
2450 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2451}
2452
Mike Stump1eb44332009-09-09 15:08:12 +00002453TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002454ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2455 switch (Arg.getKind()) {
2456 case TemplateArgument::Null:
2457 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002458
Douglas Gregor1275ae02009-07-28 23:00:59 +00002459 case TemplateArgument::Expression:
Douglas Gregor1275ae02009-07-28 23:00:59 +00002460 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Douglas Gregor1275ae02009-07-28 23:00:59 +00002462 case TemplateArgument::Declaration:
John McCall833ca992009-10-29 08:12:44 +00002463 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002464
Douglas Gregor788cd062009-11-11 01:00:40 +00002465 case TemplateArgument::Template:
2466 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2467
Douglas Gregor1275ae02009-07-28 23:00:59 +00002468 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002469 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002470 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002471
Douglas Gregor1275ae02009-07-28 23:00:59 +00002472 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00002473 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Douglas Gregor1275ae02009-07-28 23:00:59 +00002475 case TemplateArgument::Pack: {
2476 // FIXME: Allocate in ASTContext
2477 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2478 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002479 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002480 AEnd = Arg.pack_end();
2481 A != AEnd; (void)++A, ++Idx)
2482 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Douglas Gregor1275ae02009-07-28 23:00:59 +00002484 TemplateArgument Result;
2485 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2486 return Result;
2487 }
2488 }
2489
2490 // Silence GCC warning
2491 assert(false && "Unhandled template argument kind");
2492 return TemplateArgument();
2493}
2494
Douglas Gregord57959a2009-03-27 23:10:48 +00002495NestedNameSpecifier *
2496ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002497 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002498 return 0;
2499
2500 switch (NNS->getKind()) {
2501 case NestedNameSpecifier::Identifier:
2502 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002503 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002504 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2505 NNS->getAsIdentifier());
2506
2507 case NestedNameSpecifier::Namespace:
2508 // A namespace is canonical; build a nested-name-specifier with
2509 // this namespace and no prefix.
2510 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2511
2512 case NestedNameSpecifier::TypeSpec:
2513 case NestedNameSpecifier::TypeSpecWithTemplate: {
2514 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002515 return NestedNameSpecifier::Create(*this, 0,
2516 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002517 T.getTypePtr());
2518 }
2519
2520 case NestedNameSpecifier::Global:
2521 // The global specifier is canonical and unique.
2522 return NNS;
2523 }
2524
2525 // Required to silence a GCC warning
2526 return 0;
2527}
2528
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002529
2530const ArrayType *ASTContext::getAsArrayType(QualType T) {
2531 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00002532 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002533 // Handle the common positive case fast.
2534 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2535 return AT;
2536 }
Mike Stump1eb44332009-09-09 15:08:12 +00002537
John McCall0953e762009-09-24 19:53:00 +00002538 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002539 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002540 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002541 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002542
John McCall0953e762009-09-24 19:53:00 +00002543 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002544 // implements C99 6.7.3p8: "If the specification of an array type includes
2545 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002547 // If we get here, we either have type qualifiers on the type, or we have
2548 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002549 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002550
John McCall0953e762009-09-24 19:53:00 +00002551 QualifierCollector Qs;
2552 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002554 // If we have a simple case, just return now.
2555 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002556 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002557 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002559 // Otherwise, we have an array and we have qualifiers on it. Push the
2560 // qualifiers into the array element type and return a new array type.
2561 // Get the canonical version of the element with the extra qualifiers on it.
2562 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002563 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002564
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002565 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2566 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2567 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002568 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002569 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2570 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2571 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002572 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002573
Mike Stump1eb44332009-09-09 15:08:12 +00002574 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002575 = dyn_cast<DependentSizedArrayType>(ATy))
2576 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002577 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002578 DSAT->getSizeExpr() ?
2579 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002580 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002581 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002582 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002584 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002585 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002586 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002587 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002588 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002589 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002590 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002591}
2592
2593
Chris Lattnere6327742008-04-02 05:18:44 +00002594/// getArrayDecayedType - Return the properly qualified result of decaying the
2595/// specified array type to a pointer. This operation is non-trivial when
2596/// handling typedefs etc. The canonical type of "T" must be an array type,
2597/// this returns a pointer to a properly qualified element of the array.
2598///
2599/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2600QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002601 // Get the element type with 'getAsArrayType' so that we don't lose any
2602 // typedefs in the element type of the array. This also handles propagation
2603 // of type qualifiers from the array type into the element type if present
2604 // (C99 6.7.3p8).
2605 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2606 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002607
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002608 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002609
2610 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002611 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002612}
2613
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002614QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002615 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002616 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002617 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002618 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2619 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002620 } else {
John McCall0953e762009-09-24 19:53:00 +00002621 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002622 }
2623 }
2624}
2625
Anders Carlssonfbbce492009-09-25 01:23:32 +00002626QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2627 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Anders Carlssonfbbce492009-09-25 01:23:32 +00002629 if (const ArrayType *AT = getAsArrayType(ElemTy))
2630 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002631
Anders Carlsson6183a992008-12-21 03:44:36 +00002632 return ElemTy;
2633}
2634
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002635/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002636uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002637ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2638 uint64_t ElementCount = 1;
2639 do {
2640 ElementCount *= CA->getSize().getZExtValue();
2641 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2642 } while (CA);
2643 return ElementCount;
2644}
2645
Reid Spencer5f016e22007-07-11 17:01:13 +00002646/// getFloatingRank - Return a relative rank for floating point types.
2647/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002648static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002649 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002651
John McCall183700f2009-09-21 23:43:11 +00002652 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2653 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002654 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002655 case BuiltinType::Float: return FloatRank;
2656 case BuiltinType::Double: return DoubleRank;
2657 case BuiltinType::LongDouble: return LongDoubleRank;
2658 }
2659}
2660
Mike Stump1eb44332009-09-09 15:08:12 +00002661/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2662/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002663/// 'typeDomain' is a real floating point or complex type.
2664/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002665QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2666 QualType Domain) const {
2667 FloatingRank EltRank = getFloatingRank(Size);
2668 if (Domain->isComplexType()) {
2669 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002670 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002671 case FloatRank: return FloatComplexTy;
2672 case DoubleRank: return DoubleComplexTy;
2673 case LongDoubleRank: return LongDoubleComplexTy;
2674 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002675 }
Chris Lattner1361b112008-04-06 23:58:54 +00002676
2677 assert(Domain->isRealFloatingType() && "Unknown domain!");
2678 switch (EltRank) {
2679 default: assert(0 && "getFloatingRank(): illegal value for rank");
2680 case FloatRank: return FloatTy;
2681 case DoubleRank: return DoubleTy;
2682 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002683 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002684}
2685
Chris Lattner7cfeb082008-04-06 23:55:33 +00002686/// getFloatingTypeOrder - Compare the rank of the two specified floating
2687/// point types, ignoring the domain of the type (i.e. 'double' ==
2688/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002689/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002690int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2691 FloatingRank LHSR = getFloatingRank(LHS);
2692 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Chris Lattnera75cea32008-04-06 23:38:49 +00002694 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002695 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002696 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002697 return 1;
2698 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002699}
2700
Chris Lattnerf52ab252008-04-06 22:59:24 +00002701/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2702/// routine will assert if passed a built-in type that isn't an integer or enum,
2703/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002704unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002705 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002706 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00002707 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00002708
Eli Friedmana3426752009-07-05 23:44:27 +00002709 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2710 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2711
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002712 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2713 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2714
2715 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2716 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2717
Chris Lattnerf52ab252008-04-06 22:59:24 +00002718 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002719 default: assert(0 && "getIntegerRank(): not a built-in integer");
2720 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002721 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002722 case BuiltinType::Char_S:
2723 case BuiltinType::Char_U:
2724 case BuiltinType::SChar:
2725 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002726 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002727 case BuiltinType::Short:
2728 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002729 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002730 case BuiltinType::Int:
2731 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002732 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002733 case BuiltinType::Long:
2734 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002735 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002736 case BuiltinType::LongLong:
2737 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002738 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002739 case BuiltinType::Int128:
2740 case BuiltinType::UInt128:
2741 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002742 }
2743}
2744
Eli Friedman04e83572009-08-20 04:21:42 +00002745/// \brief Whether this is a promotable bitfield reference according
2746/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2747///
2748/// \returns the type this bit-field will promote to, or NULL if no
2749/// promotion occurs.
2750QualType ASTContext::isPromotableBitField(Expr *E) {
2751 FieldDecl *Field = E->getBitField();
2752 if (!Field)
2753 return QualType();
2754
2755 QualType FT = Field->getType();
2756
2757 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2758 uint64_t BitWidth = BitWidthAP.getZExtValue();
2759 uint64_t IntSize = getTypeSize(IntTy);
2760 // GCC extension compatibility: if the bit-field size is less than or equal
2761 // to the size of int, it gets promoted no matter what its type is.
2762 // For instance, unsigned long bf : 4 gets promoted to signed int.
2763 if (BitWidth < IntSize)
2764 return IntTy;
2765
2766 if (BitWidth == IntSize)
2767 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2768
2769 // Types bigger than int are not subject to promotions, and therefore act
2770 // like the base type.
2771 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2772 // is ridiculous.
2773 return QualType();
2774}
2775
Eli Friedmana95d7572009-08-19 07:44:53 +00002776/// getPromotedIntegerType - Returns the type that Promotable will
2777/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2778/// integer type.
2779QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2780 assert(!Promotable.isNull());
2781 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00002782 if (const EnumType *ET = Promotable->getAs<EnumType>())
2783 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00002784 if (Promotable->isSignedIntegerType())
2785 return IntTy;
2786 uint64_t PromotableSize = getTypeSize(Promotable);
2787 uint64_t IntSize = getTypeSize(IntTy);
2788 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2789 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2790}
2791
Mike Stump1eb44332009-09-09 15:08:12 +00002792/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002793/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002794/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002795int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002796 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2797 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002798 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002799
Chris Lattnerf52ab252008-04-06 22:59:24 +00002800 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2801 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Chris Lattner7cfeb082008-04-06 23:55:33 +00002803 unsigned LHSRank = getIntegerRank(LHSC);
2804 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002805
Chris Lattner7cfeb082008-04-06 23:55:33 +00002806 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2807 if (LHSRank == RHSRank) return 0;
2808 return LHSRank > RHSRank ? 1 : -1;
2809 }
Mike Stump1eb44332009-09-09 15:08:12 +00002810
Chris Lattner7cfeb082008-04-06 23:55:33 +00002811 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2812 if (LHSUnsigned) {
2813 // If the unsigned [LHS] type is larger, return it.
2814 if (LHSRank >= RHSRank)
2815 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002816
Chris Lattner7cfeb082008-04-06 23:55:33 +00002817 // If the signed type can represent all values of the unsigned type, it
2818 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002819 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002820 return -1;
2821 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002822
Chris Lattner7cfeb082008-04-06 23:55:33 +00002823 // If the unsigned [RHS] type is larger, return it.
2824 if (RHSRank >= LHSRank)
2825 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002826
Chris Lattner7cfeb082008-04-06 23:55:33 +00002827 // If the signed type can represent all values of the unsigned type, it
2828 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002829 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002830 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002831}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002832
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002833static RecordDecl *
2834CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2835 SourceLocation L, IdentifierInfo *Id) {
2836 if (Ctx.getLangOptions().CPlusPlus)
2837 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2838 else
2839 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2840}
2841
Mike Stump1eb44332009-09-09 15:08:12 +00002842// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002843QualType ASTContext::getCFConstantStringType() {
2844 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002845 CFConstantStringTypeDecl =
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002846 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2847 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00002848 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002849
Anders Carlssonf06273f2007-11-19 00:25:30 +00002850 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002851
Anders Carlsson71993dd2007-08-17 05:31:46 +00002852 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002853 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002854 // int flags;
2855 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002856 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002857 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002858 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002859 FieldTypes[3] = LongTy;
2860
Anders Carlsson71993dd2007-08-17 05:31:46 +00002861 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002862 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002863 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002864 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00002865 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002866 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002867 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002868 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002869 }
2870
Douglas Gregor838db382010-02-11 01:19:42 +00002871 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00002872 }
Mike Stump1eb44332009-09-09 15:08:12 +00002873
Anders Carlsson71993dd2007-08-17 05:31:46 +00002874 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002875}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002876
Douglas Gregor319ac892009-04-23 22:29:11 +00002877void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002878 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002879 assert(Rec && "Invalid CFConstantStringType");
2880 CFConstantStringTypeDecl = Rec->getDecl();
2881}
2882
Mike Stump1eb44332009-09-09 15:08:12 +00002883QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002884 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002885 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002886 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2887 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00002888 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002889
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002890 QualType FieldTypes[] = {
2891 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002892 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002893 getPointerType(UnsignedLongTy),
2894 getConstantArrayType(UnsignedLongTy,
2895 llvm::APInt(32, 5), ArrayType::Normal, 0)
2896 };
Mike Stump1eb44332009-09-09 15:08:12 +00002897
Douglas Gregor44b43212008-12-11 16:49:14 +00002898 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002899 FieldDecl *Field = FieldDecl::Create(*this,
2900 ObjCFastEnumerationStateTypeDecl,
2901 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00002902 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002903 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002904 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002905 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002906 }
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Douglas Gregor838db382010-02-11 01:19:42 +00002908 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002909 }
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002911 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2912}
2913
Mike Stumpadaaad32009-10-20 02:12:22 +00002914QualType ASTContext::getBlockDescriptorType() {
2915 if (BlockDescriptorType)
2916 return getTagDeclType(BlockDescriptorType);
2917
2918 RecordDecl *T;
2919 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002920 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2921 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00002922 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00002923
2924 QualType FieldTypes[] = {
2925 UnsignedLongTy,
2926 UnsignedLongTy,
2927 };
2928
2929 const char *FieldNames[] = {
2930 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00002931 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00002932 };
2933
2934 for (size_t i = 0; i < 2; ++i) {
2935 FieldDecl *Field = FieldDecl::Create(*this,
2936 T,
2937 SourceLocation(),
2938 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00002939 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00002940 /*BitWidth=*/0,
2941 /*Mutable=*/false);
2942 T->addDecl(Field);
2943 }
2944
Douglas Gregor838db382010-02-11 01:19:42 +00002945 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00002946
2947 BlockDescriptorType = T;
2948
2949 return getTagDeclType(BlockDescriptorType);
2950}
2951
2952void ASTContext::setBlockDescriptorType(QualType T) {
2953 const RecordType *Rec = T->getAs<RecordType>();
2954 assert(Rec && "Invalid BlockDescriptorType");
2955 BlockDescriptorType = Rec->getDecl();
2956}
2957
Mike Stump083c25e2009-10-22 00:49:09 +00002958QualType ASTContext::getBlockDescriptorExtendedType() {
2959 if (BlockDescriptorExtendedType)
2960 return getTagDeclType(BlockDescriptorExtendedType);
2961
2962 RecordDecl *T;
2963 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002964 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2965 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00002966 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00002967
2968 QualType FieldTypes[] = {
2969 UnsignedLongTy,
2970 UnsignedLongTy,
2971 getPointerType(VoidPtrTy),
2972 getPointerType(VoidPtrTy)
2973 };
2974
2975 const char *FieldNames[] = {
2976 "reserved",
2977 "Size",
2978 "CopyFuncPtr",
2979 "DestroyFuncPtr"
2980 };
2981
2982 for (size_t i = 0; i < 4; ++i) {
2983 FieldDecl *Field = FieldDecl::Create(*this,
2984 T,
2985 SourceLocation(),
2986 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00002987 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00002988 /*BitWidth=*/0,
2989 /*Mutable=*/false);
2990 T->addDecl(Field);
2991 }
2992
Douglas Gregor838db382010-02-11 01:19:42 +00002993 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00002994
2995 BlockDescriptorExtendedType = T;
2996
2997 return getTagDeclType(BlockDescriptorExtendedType);
2998}
2999
3000void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3001 const RecordType *Rec = T->getAs<RecordType>();
3002 assert(Rec && "Invalid BlockDescriptorType");
3003 BlockDescriptorExtendedType = Rec->getDecl();
3004}
3005
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003006bool ASTContext::BlockRequiresCopying(QualType Ty) {
3007 if (Ty->isBlockPointerType())
3008 return true;
3009 if (isObjCNSObjectType(Ty))
3010 return true;
3011 if (Ty->isObjCObjectPointerType())
3012 return true;
3013 return false;
3014}
3015
3016QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3017 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003018 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003019 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003020 // unsigned int __flags;
3021 // unsigned int __size;
Mike Stump38e16272009-10-21 22:01:24 +00003022 // void *__copy_helper; // as needed
3023 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003024 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003025 // } *
3026
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003027 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3028
3029 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00003030 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003031 llvm::SmallString<36> Name;
3032 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3033 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003034 RecordDecl *T;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003035 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3036 &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003037 T->startDefinition();
3038 QualType Int32Ty = IntTy;
3039 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3040 QualType FieldTypes[] = {
3041 getPointerType(VoidPtrTy),
3042 getPointerType(getTagDeclType(T)),
3043 Int32Ty,
3044 Int32Ty,
3045 getPointerType(VoidPtrTy),
3046 getPointerType(VoidPtrTy),
3047 Ty
3048 };
3049
3050 const char *FieldNames[] = {
3051 "__isa",
3052 "__forwarding",
3053 "__flags",
3054 "__size",
3055 "__copy_helper",
3056 "__destroy_helper",
3057 DeclName,
3058 };
3059
3060 for (size_t i = 0; i < 7; ++i) {
3061 if (!HasCopyAndDispose && i >=4 && i <= 5)
3062 continue;
3063 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3064 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003065 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003066 /*BitWidth=*/0, /*Mutable=*/false);
3067 T->addDecl(Field);
3068 }
3069
Douglas Gregor838db382010-02-11 01:19:42 +00003070 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003071
3072 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003073}
3074
3075
3076QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00003077 bool BlockHasCopyDispose,
Mike Stumpea26cb52009-10-21 03:49:08 +00003078 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpadaaad32009-10-20 02:12:22 +00003079 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00003080 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003081 llvm::SmallString<36> Name;
3082 llvm::raw_svector_ostream(Name) << "__block_literal_"
3083 << ++UniqueBlockParmTypeID;
Mike Stumpadaaad32009-10-20 02:12:22 +00003084 RecordDecl *T;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003085 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3086 &Idents.get(Name.str()));
John McCall5cfa0112010-02-05 01:33:36 +00003087 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003088 QualType FieldTypes[] = {
3089 getPointerType(VoidPtrTy),
3090 IntTy,
3091 IntTy,
3092 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00003093 (BlockHasCopyDispose ?
3094 getPointerType(getBlockDescriptorExtendedType()) :
3095 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00003096 };
3097
3098 const char *FieldNames[] = {
3099 "__isa",
3100 "__flags",
3101 "__reserved",
3102 "__FuncPtr",
3103 "__descriptor"
3104 };
3105
3106 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00003107 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003108 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003109 FieldTypes[i], /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003110 /*BitWidth=*/0, /*Mutable=*/false);
3111 T->addDecl(Field);
3112 }
3113
3114 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3115 const Expr *E = BlockDeclRefDecls[i];
3116 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3117 clang::IdentifierInfo *Name = 0;
3118 if (BDRE) {
3119 const ValueDecl *D = BDRE->getDecl();
3120 Name = &Idents.get(D->getName());
3121 }
3122 QualType FieldType = E->getType();
3123
3124 if (BDRE && BDRE->isByRef())
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003125 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3126 FieldType);
Mike Stumpea26cb52009-10-21 03:49:08 +00003127
3128 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCalla93c9342009-12-07 02:54:59 +00003129 Name, FieldType, /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003130 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpadaaad32009-10-20 02:12:22 +00003131 T->addDecl(Field);
3132 }
3133
Douglas Gregor838db382010-02-11 01:19:42 +00003134 T->completeDefinition();
Mike Stumpea26cb52009-10-21 03:49:08 +00003135
3136 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00003137}
3138
Douglas Gregor319ac892009-04-23 22:29:11 +00003139void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003140 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003141 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3142 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3143}
3144
Anders Carlssone8c49532007-10-29 06:33:42 +00003145// This returns true if a type has been typedefed to BOOL:
3146// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003147static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003148 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003149 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3150 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003151
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003152 return false;
3153}
3154
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003155/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003156/// purpose.
Ken Dyckaa8741a2010-01-11 19:19:56 +00003157CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck199c3d62010-01-11 17:06:35 +00003158 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003159
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003160 // Make all integer and enum types at least as large as an int
Ken Dyck199c3d62010-01-11 17:06:35 +00003161 if (sz.isPositive() && type->isIntegralType())
3162 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003163 // Treat arrays as pointers, since that's how they're passed in.
3164 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003165 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003166 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003167}
3168
3169static inline
3170std::string charUnitsToString(const CharUnits &CU) {
3171 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003172}
3173
David Chisnall5e530af2009-11-17 19:33:30 +00003174/// getObjCEncodingForBlockDecl - Return the encoded type for this method
3175/// declaration.
3176void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3177 std::string& S) {
3178 const BlockDecl *Decl = Expr->getBlockDecl();
3179 QualType BlockTy =
3180 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3181 // Encode result type.
3182 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3183 // Compute size of all parameters.
3184 // Start with computing size of a pointer in number of bytes.
3185 // FIXME: There might(should) be a better way of doing this computation!
3186 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003187 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3188 CharUnits ParmOffset = PtrSize;
David Chisnall5e530af2009-11-17 19:33:30 +00003189 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3190 E = Decl->param_end(); PI != E; ++PI) {
3191 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003192 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003193 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003194 ParmOffset += sz;
3195 }
3196 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003197 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003198 // Block pointer and offset.
3199 S += "@?0";
3200 ParmOffset = PtrSize;
3201
3202 // Argument types.
3203 ParmOffset = PtrSize;
3204 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3205 Decl->param_end(); PI != E; ++PI) {
3206 ParmVarDecl *PVDecl = *PI;
3207 QualType PType = PVDecl->getOriginalType();
3208 if (const ArrayType *AT =
3209 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3210 // Use array's original type only if it has known number of
3211 // elements.
3212 if (!isa<ConstantArrayType>(AT))
3213 PType = PVDecl->getType();
3214 } else if (PType->isFunctionType())
3215 PType = PVDecl->getType();
3216 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003217 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003218 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003219 }
3220}
3221
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003222/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003223/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003224void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003225 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003226 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003227 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003228 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003229 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003230 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003231 // Compute size of all parameters.
3232 // Start with computing size of a pointer in number of bytes.
3233 // FIXME: There might(should) be a better way of doing this computation!
3234 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003235 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003236 // The first two arguments (self and _cmd) are pointers; account for
3237 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003238 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003239 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3240 E = Decl->param_end(); PI != E; ++PI) {
3241 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003242 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003243 assert (sz.isPositive() &&
3244 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003245 ParmOffset += sz;
3246 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003247 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003248 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003249 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003250
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003251 // Argument types.
3252 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003253 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3254 E = Decl->param_end(); PI != E; ++PI) {
3255 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003256 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003257 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003258 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3259 // Use array's original type only if it has known number of
3260 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003261 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003262 PType = PVDecl->getType();
3263 } else if (PType->isFunctionType())
3264 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003265 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003266 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003267 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003268 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003269 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003270 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003271 }
3272}
3273
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003274/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003275/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003276/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3277/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003278/// Property attributes are stored as a comma-delimited C string. The simple
3279/// attributes readonly and bycopy are encoded as single characters. The
3280/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3281/// encoded as single characters, followed by an identifier. Property types
3282/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003283/// these attributes are defined by the following enumeration:
3284/// @code
3285/// enum PropertyAttributes {
3286/// kPropertyReadOnly = 'R', // property is read-only.
3287/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3288/// kPropertyByref = '&', // property is a reference to the value last assigned
3289/// kPropertyDynamic = 'D', // property is dynamic
3290/// kPropertyGetter = 'G', // followed by getter selector name
3291/// kPropertySetter = 'S', // followed by setter selector name
3292/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3293/// kPropertyType = 't' // followed by old-style type encoding.
3294/// kPropertyWeak = 'W' // 'weak' property
3295/// kPropertyStrong = 'P' // property GC'able
3296/// kPropertyNonAtomic = 'N' // property non-atomic
3297/// };
3298/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003299void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003300 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003301 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003302 // Collect information from the property implementation decl(s).
3303 bool Dynamic = false;
3304 ObjCPropertyImplDecl *SynthesizePID = 0;
3305
3306 // FIXME: Duplicated code due to poor abstraction.
3307 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003308 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003309 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3310 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003311 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003312 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003313 ObjCPropertyImplDecl *PID = *i;
3314 if (PID->getPropertyDecl() == PD) {
3315 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3316 Dynamic = true;
3317 } else {
3318 SynthesizePID = PID;
3319 }
3320 }
3321 }
3322 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003323 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003324 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003325 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003326 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003327 ObjCPropertyImplDecl *PID = *i;
3328 if (PID->getPropertyDecl() == PD) {
3329 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3330 Dynamic = true;
3331 } else {
3332 SynthesizePID = PID;
3333 }
3334 }
Mike Stump1eb44332009-09-09 15:08:12 +00003335 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003336 }
3337 }
3338
3339 // FIXME: This is not very efficient.
3340 S = "T";
3341
3342 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003343 // GCC has some special rules regarding encoding of properties which
3344 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003345 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003346 true /* outermost type */,
3347 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003348
3349 if (PD->isReadOnly()) {
3350 S += ",R";
3351 } else {
3352 switch (PD->getSetterKind()) {
3353 case ObjCPropertyDecl::Assign: break;
3354 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003355 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003356 }
3357 }
3358
3359 // It really isn't clear at all what this means, since properties
3360 // are "dynamic by default".
3361 if (Dynamic)
3362 S += ",D";
3363
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003364 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3365 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003366
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003367 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3368 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003369 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003370 }
3371
3372 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3373 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003374 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003375 }
3376
3377 if (SynthesizePID) {
3378 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3379 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003380 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003381 }
3382
3383 // FIXME: OBJCGC: weak & strong
3384}
3385
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003386/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003387/// Another legacy compatibility encoding: 32-bit longs are encoded as
3388/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003389/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3390///
3391void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003392 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003393 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003394 if (BT->getKind() == BuiltinType::ULong &&
3395 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003396 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003397 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003398 if (BT->getKind() == BuiltinType::Long &&
3399 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003400 PointeeTy = IntTy;
3401 }
3402 }
3403}
3404
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003405void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003406 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003407 // We follow the behavior of gcc, expanding structures which are
3408 // directly pointed to, and expanding embedded structures. Note that
3409 // these rules are sufficient to prevent recursive encoding of the
3410 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003411 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003412 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003413}
3414
Mike Stump1eb44332009-09-09 15:08:12 +00003415static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003416 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003417 const Expr *E = FD->getBitWidth();
3418 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3419 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00003420 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003421 S += 'b';
3422 S += llvm::utostr(N);
3423}
3424
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003425// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003426void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3427 bool ExpandPointedToStructures,
3428 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003429 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003430 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003431 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00003432 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003433 if (FD && FD->isBitField())
3434 return EncodeBitField(this, S, FD);
3435 char encoding;
3436 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003437 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003438 case BuiltinType::Void: encoding = 'v'; break;
3439 case BuiltinType::Bool: encoding = 'B'; break;
3440 case BuiltinType::Char_U:
3441 case BuiltinType::UChar: encoding = 'C'; break;
3442 case BuiltinType::UShort: encoding = 'S'; break;
3443 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003444 case BuiltinType::ULong:
3445 encoding =
3446 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00003447 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003448 case BuiltinType::UInt128: encoding = 'T'; break;
3449 case BuiltinType::ULongLong: encoding = 'Q'; break;
3450 case BuiltinType::Char_S:
3451 case BuiltinType::SChar: encoding = 'c'; break;
3452 case BuiltinType::Short: encoding = 's'; break;
3453 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003454 case BuiltinType::Long:
3455 encoding =
3456 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003457 break;
3458 case BuiltinType::LongLong: encoding = 'q'; break;
3459 case BuiltinType::Int128: encoding = 't'; break;
3460 case BuiltinType::Float: encoding = 'f'; break;
3461 case BuiltinType::Double: encoding = 'd'; break;
3462 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003463 }
Mike Stump1eb44332009-09-09 15:08:12 +00003464
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003465 S += encoding;
3466 return;
3467 }
Mike Stump1eb44332009-09-09 15:08:12 +00003468
John McCall183700f2009-09-21 23:43:11 +00003469 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003470 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003471 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003472 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003473 return;
3474 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00003475
Ted Kremenek6217b802009-07-29 21:53:49 +00003476 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003477 if (PT->isObjCSelType()) {
3478 S += ':';
3479 return;
3480 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003481 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003482
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003483 bool isReadOnly = false;
3484 // For historical/compatibility reasons, the read-only qualifier of the
3485 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3486 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003487 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003488 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003489 if (OutermostType && T.isConstQualified()) {
3490 isReadOnly = true;
3491 S += 'r';
3492 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003493 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003494 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003495 while (P->getAs<PointerType>())
3496 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003497 if (P.isConstQualified()) {
3498 isReadOnly = true;
3499 S += 'r';
3500 }
3501 }
3502 if (isReadOnly) {
3503 // Another legacy compatibility encoding. Some ObjC qualifier and type
3504 // combinations need to be rearranged.
3505 // Rewrite "in const" from "nr" to "rn"
3506 const char * s = S.c_str();
3507 int len = S.length();
3508 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3509 std::string replace = "rn";
3510 S.replace(S.end()-2, S.end(), replace);
3511 }
3512 }
Mike Stump1eb44332009-09-09 15:08:12 +00003513
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003514 if (PointeeTy->isCharType()) {
3515 // char pointer types should be encoded as '*' unless it is a
3516 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003517 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003518 S += '*';
3519 return;
3520 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003521 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003522 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3523 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3524 S += '#';
3525 return;
3526 }
3527 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3528 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3529 S += '@';
3530 return;
3531 }
3532 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003533 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003534 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003535 getLegacyIntegralTypeEncoding(PointeeTy);
3536
Mike Stump1eb44332009-09-09 15:08:12 +00003537 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003538 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003539 return;
3540 }
Mike Stump1eb44332009-09-09 15:08:12 +00003541
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003542 if (const ArrayType *AT =
3543 // Ignore type qualifiers etc.
3544 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003545 if (isa<IncompleteArrayType>(AT)) {
3546 // Incomplete arrays are encoded as a pointer to the array element.
3547 S += '^';
3548
Mike Stump1eb44332009-09-09 15:08:12 +00003549 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003550 false, ExpandStructures, FD);
3551 } else {
3552 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003553
Anders Carlsson559a8332009-02-22 01:38:57 +00003554 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3555 S += llvm::utostr(CAT->getSize().getZExtValue());
3556 else {
3557 //Variable length arrays are encoded as a regular array with 0 elements.
3558 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3559 S += '0';
3560 }
Mike Stump1eb44332009-09-09 15:08:12 +00003561
3562 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003563 false, ExpandStructures, FD);
3564 S += ']';
3565 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003566 return;
3567 }
Mike Stump1eb44332009-09-09 15:08:12 +00003568
John McCall183700f2009-09-21 23:43:11 +00003569 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003570 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003571 return;
3572 }
Mike Stump1eb44332009-09-09 15:08:12 +00003573
Ted Kremenek6217b802009-07-29 21:53:49 +00003574 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003575 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003576 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003577 // Anonymous structures print as '?'
3578 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3579 S += II->getName();
3580 } else {
3581 S += '?';
3582 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003583 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003584 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003585 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3586 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003587 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003588 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003589 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003590 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003591 S += '"';
3592 }
Mike Stump1eb44332009-09-09 15:08:12 +00003593
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003594 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003595 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003596 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003597 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003598 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003599 QualType qt = Field->getType();
3600 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003601 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003602 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003603 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003604 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003605 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003606 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003607 return;
3608 }
Mike Stump1eb44332009-09-09 15:08:12 +00003609
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003610 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003611 if (FD && FD->isBitField())
3612 EncodeBitField(this, S, FD);
3613 else
3614 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003615 return;
3616 }
Mike Stump1eb44332009-09-09 15:08:12 +00003617
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003618 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003619 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003620 return;
3621 }
Mike Stump1eb44332009-09-09 15:08:12 +00003622
John McCall0953e762009-09-24 19:53:00 +00003623 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003624 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003625 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003626 S += '{';
3627 const IdentifierInfo *II = OI->getIdentifier();
3628 S += II->getName();
3629 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003630 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003631 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003632 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003633 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003634 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003635 RecFields[i]);
3636 else
Mike Stump1eb44332009-09-09 15:08:12 +00003637 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003638 FD);
3639 }
3640 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003641 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003642 }
Mike Stump1eb44332009-09-09 15:08:12 +00003643
John McCall183700f2009-09-21 23:43:11 +00003644 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003645 if (OPT->isObjCIdType()) {
3646 S += '@';
3647 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003648 }
Mike Stump1eb44332009-09-09 15:08:12 +00003649
Steve Naroff27d20a22009-10-28 22:03:49 +00003650 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3651 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3652 // Since this is a binary compatibility issue, need to consult with runtime
3653 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00003654 S += '#';
3655 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003656 }
Mike Stump1eb44332009-09-09 15:08:12 +00003657
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003658 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003659 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003660 ExpandPointedToStructures,
3661 ExpandStructures, FD);
3662 if (FD || EncodingProperty) {
3663 // Note that we do extended encoding of protocol qualifer list
3664 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003665 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003666 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3667 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003668 S += '<';
3669 S += (*I)->getNameAsString();
3670 S += '>';
3671 }
3672 S += '"';
3673 }
3674 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003675 }
Mike Stump1eb44332009-09-09 15:08:12 +00003676
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003677 QualType PointeeTy = OPT->getPointeeType();
3678 if (!EncodingProperty &&
3679 isa<TypedefType>(PointeeTy.getTypePtr())) {
3680 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003681 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003682 // {...};
3683 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003684 getObjCEncodingForTypeImpl(PointeeTy, S,
3685 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003686 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003687 return;
3688 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003689
3690 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00003691 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003692 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00003693 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003694 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3695 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003696 S += '<';
3697 S += (*I)->getNameAsString();
3698 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003699 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003700 S += '"';
3701 }
3702 return;
3703 }
Mike Stump1eb44332009-09-09 15:08:12 +00003704
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003705 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003706}
3707
Mike Stump1eb44332009-09-09 15:08:12 +00003708void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003709 std::string& S) const {
3710 if (QT & Decl::OBJC_TQ_In)
3711 S += 'n';
3712 if (QT & Decl::OBJC_TQ_Inout)
3713 S += 'N';
3714 if (QT & Decl::OBJC_TQ_Out)
3715 S += 'o';
3716 if (QT & Decl::OBJC_TQ_Bycopy)
3717 S += 'O';
3718 if (QT & Decl::OBJC_TQ_Byref)
3719 S += 'R';
3720 if (QT & Decl::OBJC_TQ_Oneway)
3721 S += 'V';
3722}
3723
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003724void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003725 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003726
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003727 BuiltinVaListType = T;
3728}
3729
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003730void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003731 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003732}
3733
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003734void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00003735 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003736}
3737
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003738void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003739 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003740}
3741
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003742void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003743 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003744}
3745
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003746void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003747 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003748 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003749
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003750 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003751}
3752
John McCall0bd6feb2009-12-02 08:04:21 +00003753/// \brief Retrieve the template name that corresponds to a non-empty
3754/// lookup.
John McCalleec51cf2010-01-20 00:46:10 +00003755TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3756 UnresolvedSetIterator End) {
John McCall0bd6feb2009-12-02 08:04:21 +00003757 unsigned size = End - Begin;
3758 assert(size > 1 && "set is not overloaded!");
3759
3760 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3761 size * sizeof(FunctionTemplateDecl*));
3762 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3763
3764 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00003765 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00003766 NamedDecl *D = *I;
3767 assert(isa<FunctionTemplateDecl>(D) ||
3768 (isa<UsingShadowDecl>(D) &&
3769 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3770 *Storage++ = D;
3771 }
3772
3773 return TemplateName(OT);
3774}
3775
Douglas Gregor7532dc62009-03-30 22:58:21 +00003776/// \brief Retrieve the template name that represents a qualified
3777/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003778TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003779 bool TemplateKeyword,
3780 TemplateDecl *Template) {
Douglas Gregor789b1f62010-02-04 18:10:26 +00003781 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00003782 llvm::FoldingSetNodeID ID;
3783 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3784
3785 void *InsertPos = 0;
3786 QualifiedTemplateName *QTN =
3787 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3788 if (!QTN) {
3789 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3790 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3791 }
3792
3793 return TemplateName(QTN);
3794}
3795
3796/// \brief Retrieve the template name that represents a dependent
3797/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003798TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003799 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003800 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003801 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003802
3803 llvm::FoldingSetNodeID ID;
3804 DependentTemplateName::Profile(ID, NNS, Name);
3805
3806 void *InsertPos = 0;
3807 DependentTemplateName *QTN =
3808 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3809
3810 if (QTN)
3811 return TemplateName(QTN);
3812
3813 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3814 if (CanonNNS == NNS) {
3815 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3816 } else {
3817 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3818 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00003819 DependentTemplateName *CheckQTN =
3820 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3821 assert(!CheckQTN && "Dependent type name canonicalization broken");
3822 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00003823 }
3824
3825 DependentTemplateNames.InsertNode(QTN, InsertPos);
3826 return TemplateName(QTN);
3827}
3828
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003829/// \brief Retrieve the template name that represents a dependent
3830/// template name such as \c MetaFun::template operator+.
3831TemplateName
3832ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3833 OverloadedOperatorKind Operator) {
3834 assert((!NNS || NNS->isDependent()) &&
3835 "Nested name specifier must be dependent");
3836
3837 llvm::FoldingSetNodeID ID;
3838 DependentTemplateName::Profile(ID, NNS, Operator);
3839
3840 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00003841 DependentTemplateName *QTN
3842 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003843
3844 if (QTN)
3845 return TemplateName(QTN);
3846
3847 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3848 if (CanonNNS == NNS) {
3849 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3850 } else {
3851 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3852 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00003853
3854 DependentTemplateName *CheckQTN
3855 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3856 assert(!CheckQTN && "Dependent template name canonicalization broken");
3857 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003858 }
3859
3860 DependentTemplateNames.InsertNode(QTN, InsertPos);
3861 return TemplateName(QTN);
3862}
3863
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003864/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003865/// TargetInfo, produce the corresponding type. The unsigned @p Type
3866/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00003867CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003868 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00003869 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003870 case TargetInfo::SignedShort: return ShortTy;
3871 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3872 case TargetInfo::SignedInt: return IntTy;
3873 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3874 case TargetInfo::SignedLong: return LongTy;
3875 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3876 case TargetInfo::SignedLongLong: return LongLongTy;
3877 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3878 }
3879
3880 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00003881 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003882}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003883
3884//===----------------------------------------------------------------------===//
3885// Type Predicates.
3886//===----------------------------------------------------------------------===//
3887
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003888/// isObjCNSObjectType - Return true if this is an NSObject object using
3889/// NSObject attribute on a c-style pointer type.
3890/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003891/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003892///
3893bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3894 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3895 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003896 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003897 return true;
3898 }
Mike Stump1eb44332009-09-09 15:08:12 +00003899 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003900}
3901
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003902/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3903/// garbage collection attribute.
3904///
John McCall0953e762009-09-24 19:53:00 +00003905Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3906 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003907 if (getLangOptions().ObjC1 &&
3908 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003909 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003910 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003911 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003912 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003913 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003914 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003915 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003916 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003917 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003918 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003919 // Non-pointers have none gc'able attribute regardless of the attribute
3920 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003921 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003922 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003923 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003924 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003925}
3926
Chris Lattner6ac46a42008-04-07 06:51:04 +00003927//===----------------------------------------------------------------------===//
3928// Type Compatibility Testing
3929//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003930
Mike Stump1eb44332009-09-09 15:08:12 +00003931/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003932/// compatible.
3933static bool areCompatVectorTypes(const VectorType *LHS,
3934 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00003935 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00003936 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003937 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003938}
3939
Steve Naroff4084c302009-07-23 01:01:38 +00003940//===----------------------------------------------------------------------===//
3941// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3942//===----------------------------------------------------------------------===//
3943
3944/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3945/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003946bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3947 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003948 if (lProto == rProto)
3949 return true;
3950 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3951 E = rProto->protocol_end(); PI != E; ++PI)
3952 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3953 return true;
3954 return false;
3955}
3956
Steve Naroff4084c302009-07-23 01:01:38 +00003957/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3958/// return true if lhs's protocols conform to rhs's protocol; false
3959/// otherwise.
3960bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3961 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3962 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3963 return false;
3964}
3965
3966/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3967/// ObjCQualifiedIDType.
3968bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3969 bool compare) {
3970 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003971 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003972 lhs->isObjCIdType() || lhs->isObjCClassType())
3973 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003974 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003975 rhs->isObjCIdType() || rhs->isObjCClassType())
3976 return true;
3977
3978 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003979 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003980
Steve Naroff4084c302009-07-23 01:01:38 +00003981 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003982
Steve Naroff4084c302009-07-23 01:01:38 +00003983 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003984 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003985 // make sure we check the class hierarchy.
3986 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3987 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3988 E = lhsQID->qual_end(); I != E; ++I) {
3989 // when comparing an id<P> on lhs with a static type on rhs,
3990 // see if static class implements all of id's protocols, directly or
3991 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003992 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003993 return false;
3994 }
3995 }
3996 // If there are no qualifiers and no interface, we have an 'id'.
3997 return true;
3998 }
Mike Stump1eb44332009-09-09 15:08:12 +00003999 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004000 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4001 E = lhsQID->qual_end(); I != E; ++I) {
4002 ObjCProtocolDecl *lhsProto = *I;
4003 bool match = false;
4004
4005 // when comparing an id<P> on lhs with a static type on rhs,
4006 // see if static class implements all of id's protocols, directly or
4007 // through its super class and categories.
4008 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4009 E = rhsOPT->qual_end(); J != E; ++J) {
4010 ObjCProtocolDecl *rhsProto = *J;
4011 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4012 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4013 match = true;
4014 break;
4015 }
4016 }
Mike Stump1eb44332009-09-09 15:08:12 +00004017 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004018 // make sure we check the class hierarchy.
4019 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4020 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4021 E = lhsQID->qual_end(); I != E; ++I) {
4022 // when comparing an id<P> on lhs with a static type on rhs,
4023 // see if static class implements all of id's protocols, directly or
4024 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004025 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004026 match = true;
4027 break;
4028 }
4029 }
4030 }
4031 if (!match)
4032 return false;
4033 }
Mike Stump1eb44332009-09-09 15:08:12 +00004034
Steve Naroff4084c302009-07-23 01:01:38 +00004035 return true;
4036 }
Mike Stump1eb44332009-09-09 15:08:12 +00004037
Steve Naroff4084c302009-07-23 01:01:38 +00004038 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4039 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4040
Mike Stump1eb44332009-09-09 15:08:12 +00004041 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004042 lhs->getAsObjCInterfacePointerType()) {
4043 if (lhsOPT->qual_empty()) {
4044 bool match = false;
4045 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4046 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4047 E = rhsQID->qual_end(); I != E; ++I) {
4048 // when comparing an id<P> on lhs with a static type on rhs,
4049 // see if static class implements all of id's protocols, directly or
4050 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004051 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004052 match = true;
4053 break;
4054 }
4055 }
4056 if (!match)
4057 return false;
4058 }
4059 return true;
4060 }
Mike Stump1eb44332009-09-09 15:08:12 +00004061 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004062 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4063 E = lhsOPT->qual_end(); I != E; ++I) {
4064 ObjCProtocolDecl *lhsProto = *I;
4065 bool match = false;
4066
4067 // when comparing an id<P> on lhs with a static type on rhs,
4068 // see if static class implements all of id's protocols, directly or
4069 // through its super class and categories.
4070 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4071 E = rhsQID->qual_end(); J != E; ++J) {
4072 ObjCProtocolDecl *rhsProto = *J;
4073 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4074 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4075 match = true;
4076 break;
4077 }
4078 }
4079 if (!match)
4080 return false;
4081 }
4082 return true;
4083 }
4084 return false;
4085}
4086
Eli Friedman3d815e72008-08-22 00:56:42 +00004087/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004088/// compatible for assignment from RHS to LHS. This handles validation of any
4089/// protocol qualifiers on the LHS or RHS.
4090///
Steve Naroff14108da2009-07-10 23:34:53 +00004091bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4092 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004093 // If either type represents the built-in 'id' or 'Class' types, return true.
4094 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00004095 return true;
4096
Steve Naroff4084c302009-07-23 01:01:38 +00004097 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00004098 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4099 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004100 false);
4101
Steve Naroff14108da2009-07-10 23:34:53 +00004102 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4103 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00004104 if (LHS && RHS) // We have 2 user-defined types.
4105 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004106
Steve Naroff4084c302009-07-23 01:01:38 +00004107 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004108}
4109
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004110/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4111/// for providing type-safty for objective-c pointers used to pass/return
4112/// arguments in block literals. When passed as arguments, passing 'A*' where
4113/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4114/// not OK. For the return type, the opposite is not OK.
4115bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4116 const ObjCObjectPointerType *LHSOPT,
4117 const ObjCObjectPointerType *RHSOPT) {
4118 if (RHSOPT->isObjCBuiltinType())
4119 return true;
4120
4121 if (LHSOPT->isObjCBuiltinType()) {
4122 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4123 }
4124
4125 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
4126 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4127 QualType(RHSOPT,0),
4128 false);
4129
4130 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4131 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4132 if (LHS && RHS) { // We have 2 user-defined types.
4133 if (LHS != RHS) {
4134 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4135 return false;
4136 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4137 return true;
4138 }
4139 else
4140 return true;
4141 }
4142 return false;
4143}
4144
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004145/// getIntersectionOfProtocols - This routine finds the intersection of set
4146/// of protocols inherited from two distinct objective-c pointer objects.
4147/// It is used to build composite qualifier list of the composite type of
4148/// the conditional expression involving two objective-c pointer objects.
4149static
4150void getIntersectionOfProtocols(ASTContext &Context,
4151 const ObjCObjectPointerType *LHSOPT,
4152 const ObjCObjectPointerType *RHSOPT,
4153 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4154
4155 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4156 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4157
4158 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4159 unsigned LHSNumProtocols = LHS->getNumProtocols();
4160 if (LHSNumProtocols > 0)
4161 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4162 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004163 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4164 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004165 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4166 LHSInheritedProtocols.end());
4167 }
4168
4169 unsigned RHSNumProtocols = RHS->getNumProtocols();
4170 if (RHSNumProtocols > 0) {
4171 ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin();
4172 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4173 if (InheritedProtocolSet.count(RHSProtocols[i]))
4174 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4175 }
4176 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004177 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004178 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004179 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4180 RHSInheritedProtocols.begin(),
4181 E = RHSInheritedProtocols.end(); I != E; ++I)
4182 if (InheritedProtocolSet.count((*I)))
4183 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004184 }
4185}
4186
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004187/// areCommonBaseCompatible - Returns common base class of the two classes if
4188/// one found. Note that this is O'2 algorithm. But it will be called as the
4189/// last type comparison in a ?-exp of ObjC pointer types before a
4190/// warning is issued. So, its invokation is extremely rare.
4191QualType ASTContext::areCommonBaseCompatible(
4192 const ObjCObjectPointerType *LHSOPT,
4193 const ObjCObjectPointerType *RHSOPT) {
4194 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4195 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4196 if (!LHS || !RHS)
4197 return QualType();
4198
4199 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4200 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4201 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004202 if (canAssignObjCInterfaces(LHS, RHS)) {
4203 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4204 getIntersectionOfProtocols(*this,
4205 LHSOPT, RHSOPT, IntersectionOfProtocols);
4206 if (IntersectionOfProtocols.empty())
4207 LHSTy = getObjCObjectPointerType(LHSTy);
4208 else
4209 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4210 IntersectionOfProtocols.size());
4211 return LHSTy;
4212 }
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004213 }
4214
4215 return QualType();
4216}
4217
Eli Friedman3d815e72008-08-22 00:56:42 +00004218bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4219 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00004220 // Verify that the base decls are compatible: the RHS must be a subclass of
4221 // the LHS.
4222 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4223 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004224
Chris Lattner6ac46a42008-04-07 06:51:04 +00004225 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4226 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004227 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004228 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004229
Chris Lattner6ac46a42008-04-07 06:51:04 +00004230 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4231 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004232 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004233 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00004234
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004235 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4236 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004237 LHSPI != LHSPE; LHSPI++) {
4238 bool RHSImplementsProtocol = false;
4239
4240 // If the RHS doesn't implement the protocol on the left, the types
4241 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004242 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00004243 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00004244 RHSPI != RHSPE; RHSPI++) {
4245 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004246 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00004247 break;
4248 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004249 }
4250 // FIXME: For better diagnostics, consider passing back the protocol name.
4251 if (!RHSImplementsProtocol)
4252 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004253 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004254 // The RHS implements all protocols listed on the LHS.
4255 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004256}
4257
Steve Naroff389bf462009-02-12 17:52:19 +00004258bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4259 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00004260 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4261 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004262
Steve Naroff14108da2009-07-10 23:34:53 +00004263 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00004264 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004265
4266 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4267 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00004268}
4269
Mike Stump1eb44332009-09-09 15:08:12 +00004270/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00004271/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00004272/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00004273/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00004274bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004275 if (getLangOptions().CPlusPlus)
4276 return hasSameType(LHS, RHS);
4277
Eli Friedman3d815e72008-08-22 00:56:42 +00004278 return !mergeTypes(LHS, RHS).isNull();
4279}
4280
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004281bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4282 return !mergeTypes(LHS, RHS, true).isNull();
4283}
4284
4285QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4286 bool OfBlockPointer) {
John McCall183700f2009-09-21 23:43:11 +00004287 const FunctionType *lbase = lhs->getAs<FunctionType>();
4288 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00004289 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4290 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00004291 bool allLTypes = true;
4292 bool allRTypes = true;
4293
4294 // Check return type
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004295 QualType retType;
4296 if (OfBlockPointer)
4297 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4298 else
4299 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman3d815e72008-08-22 00:56:42 +00004300 if (retType.isNull()) return QualType();
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004301 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner61710852008-10-05 17:34:18 +00004302 allLTypes = false;
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004303 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner61710852008-10-05 17:34:18 +00004304 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00004305 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00004306 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
4307 if (NoReturn != lbase->getNoReturnAttr())
4308 allLTypes = false;
4309 if (NoReturn != rbase->getNoReturnAttr())
4310 allRTypes = false;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004311 CallingConv lcc = lbase->getCallConv();
4312 CallingConv rcc = rbase->getCallConv();
4313 // Compatible functions must have compatible calling conventions
John McCall04a67a62010-02-05 21:31:56 +00004314 if (!isSameCallConv(lcc, rcc))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004315 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004316
Eli Friedman3d815e72008-08-22 00:56:42 +00004317 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00004318 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4319 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004320 unsigned lproto_nargs = lproto->getNumArgs();
4321 unsigned rproto_nargs = rproto->getNumArgs();
4322
4323 // Compatible functions must have the same number of arguments
4324 if (lproto_nargs != rproto_nargs)
4325 return QualType();
4326
4327 // Variadic and non-variadic functions aren't compatible
4328 if (lproto->isVariadic() != rproto->isVariadic())
4329 return QualType();
4330
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00004331 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4332 return QualType();
4333
Eli Friedman3d815e72008-08-22 00:56:42 +00004334 // Check argument compatibility
4335 llvm::SmallVector<QualType, 10> types;
4336 for (unsigned i = 0; i < lproto_nargs; i++) {
4337 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4338 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004339 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman3d815e72008-08-22 00:56:42 +00004340 if (argtype.isNull()) return QualType();
4341 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00004342 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4343 allLTypes = false;
4344 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4345 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00004346 }
4347 if (allLTypes) return lhs;
4348 if (allRTypes) return rhs;
4349 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00004350 lproto->isVariadic(), lproto->getTypeQuals(),
Douglas Gregor1bf40242010-02-12 17:17:28 +00004351 false, false, 0, 0, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004352 }
4353
4354 if (lproto) allRTypes = false;
4355 if (rproto) allLTypes = false;
4356
Douglas Gregor72564e72009-02-26 23:50:07 +00004357 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00004358 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00004359 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004360 if (proto->isVariadic()) return QualType();
4361 // Check that the types are compatible with the types that
4362 // would result from default argument promotions (C99 6.7.5.3p15).
4363 // The only types actually affected are promotable integer
4364 // types and floats, which would be passed as a different
4365 // type depending on whether the prototype is visible.
4366 unsigned proto_nargs = proto->getNumArgs();
4367 for (unsigned i = 0; i < proto_nargs; ++i) {
4368 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00004369
4370 // Look at the promotion type of enum types, since that is the type used
4371 // to pass enum values.
4372 if (const EnumType *Enum = argTy->getAs<EnumType>())
4373 argTy = Enum->getDecl()->getPromotionType();
4374
Eli Friedman3d815e72008-08-22 00:56:42 +00004375 if (argTy->isPromotableIntegerType() ||
4376 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4377 return QualType();
4378 }
4379
4380 if (allLTypes) return lhs;
4381 if (allRTypes) return rhs;
4382 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00004383 proto->getNumArgs(), proto->isVariadic(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00004384 proto->getTypeQuals(),
4385 false, false, 0, 0, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004386 }
4387
4388 if (allLTypes) return lhs;
4389 if (allRTypes) return rhs;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004390 return getFunctionNoProtoType(retType, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004391}
4392
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004393QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4394 bool OfBlockPointer) {
Bill Wendling43d69752007-12-03 07:33:35 +00004395 // C++ [expr]: If an expression initially has the type "reference to T", the
4396 // type is adjusted to "T" prior to any further analysis, the expression
4397 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004398 // expression is an lvalue unless the reference is an rvalue reference and
4399 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004400 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4401 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4402
Eli Friedman3d815e72008-08-22 00:56:42 +00004403 QualType LHSCan = getCanonicalType(LHS),
4404 RHSCan = getCanonicalType(RHS);
4405
4406 // If two types are identical, they are compatible.
4407 if (LHSCan == RHSCan)
4408 return LHS;
4409
John McCall0953e762009-09-24 19:53:00 +00004410 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00004411 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4412 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00004413 if (LQuals != RQuals) {
4414 // If any of these qualifiers are different, we have a type
4415 // mismatch.
4416 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4417 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4418 return QualType();
4419
4420 // Exactly one GC qualifier difference is allowed: __strong is
4421 // okay if the other type has no GC qualifier but is an Objective
4422 // C object pointer (i.e. implicitly strong by default). We fix
4423 // this by pretending that the unqualified type was actually
4424 // qualified __strong.
4425 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4426 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4427 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4428
4429 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4430 return QualType();
4431
4432 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4433 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4434 }
4435 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4436 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4437 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004438 return QualType();
John McCall0953e762009-09-24 19:53:00 +00004439 }
4440
4441 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00004442
Eli Friedman852d63b2009-06-01 01:22:52 +00004443 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4444 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00004445
Chris Lattner1adb8832008-01-14 05:45:46 +00004446 // We want to consider the two function types to be the same for these
4447 // comparisons, just force one to the other.
4448 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4449 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00004450
4451 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00004452 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4453 LHSClass = Type::ConstantArray;
4454 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4455 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00004456
Nate Begeman213541a2008-04-18 23:10:10 +00004457 // Canonicalize ExtVector -> Vector.
4458 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4459 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00004460
Chris Lattnera36a61f2008-04-07 05:43:21 +00004461 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004462 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00004463 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00004464 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00004465 // Compatibility is based on the underlying type, not the promotion
4466 // type.
John McCall183700f2009-09-21 23:43:11 +00004467 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004468 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4469 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004470 }
John McCall183700f2009-09-21 23:43:11 +00004471 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004472 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4473 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004474 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004475
Eli Friedman3d815e72008-08-22 00:56:42 +00004476 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004477 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004478
Steve Naroff4a746782008-01-09 22:43:08 +00004479 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004480 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00004481#define TYPE(Class, Base)
4482#define ABSTRACT_TYPE(Class, Base)
John McCallad5e7382010-03-01 23:49:17 +00004483#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +00004484#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4485#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4486#include "clang/AST/TypeNodes.def"
4487 assert(false && "Non-canonical and dependent types shouldn't get here");
4488 return QualType();
4489
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004490 case Type::LValueReference:
4491 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00004492 case Type::MemberPointer:
4493 assert(false && "C++ should never be in mergeTypes");
4494 return QualType();
4495
4496 case Type::IncompleteArray:
4497 case Type::VariableArray:
4498 case Type::FunctionProto:
4499 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00004500 assert(false && "Types are eliminated above");
4501 return QualType();
4502
Chris Lattner1adb8832008-01-14 05:45:46 +00004503 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00004504 {
4505 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004506 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4507 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00004508 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4509 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00004510 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004511 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00004512 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004513 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004514 return getPointerType(ResultType);
4515 }
Steve Naroffc0febd52008-12-10 17:49:55 +00004516 case Type::BlockPointer:
4517 {
4518 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004519 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4520 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004521 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroffc0febd52008-12-10 17:49:55 +00004522 if (ResultType.isNull()) return QualType();
4523 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4524 return LHS;
4525 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4526 return RHS;
4527 return getBlockPointerType(ResultType);
4528 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004529 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00004530 {
4531 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4532 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4533 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4534 return QualType();
4535
4536 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4537 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4538 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4539 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00004540 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4541 return LHS;
4542 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4543 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00004544 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4545 ArrayType::ArraySizeModifier(), 0);
4546 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4547 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004548 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4549 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00004550 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4551 return LHS;
4552 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4553 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004554 if (LVAT) {
4555 // FIXME: This isn't correct! But tricky to implement because
4556 // the array's size has to be the size of LHS, but the type
4557 // has to be different.
4558 return LHS;
4559 }
4560 if (RVAT) {
4561 // FIXME: This isn't correct! But tricky to implement because
4562 // the array's size has to be the size of RHS, but the type
4563 // has to be different.
4564 return RHS;
4565 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00004566 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4567 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004568 return getIncompleteArrayType(ResultType,
4569 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004570 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004571 case Type::FunctionNoProto:
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004572 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregor72564e72009-02-26 23:50:07 +00004573 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00004574 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00004575 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00004576 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004577 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00004578 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00004579 case Type::Complex:
4580 // Distinct complex types are incompatible.
4581 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004582 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004583 // FIXME: The merged type should be an ExtVector!
John McCall1c471f32010-03-12 23:14:13 +00004584 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4585 RHSCan->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00004586 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00004587 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004588 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00004589 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004590 // FIXME: This should be type compatibility, e.g. whether
4591 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00004592 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4593 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00004594 if (LHSIface && RHSIface &&
4595 canAssignObjCInterfaces(LHSIface, RHSIface))
4596 return LHS;
4597
Eli Friedman3d815e72008-08-22 00:56:42 +00004598 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004599 }
Steve Naroff14108da2009-07-10 23:34:53 +00004600 case Type::ObjCObjectPointer: {
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004601 if (OfBlockPointer) {
4602 if (canAssignObjCInterfacesInBlockPointer(
4603 LHS->getAs<ObjCObjectPointerType>(),
4604 RHS->getAs<ObjCObjectPointerType>()))
4605 return LHS;
4606 return QualType();
4607 }
John McCall183700f2009-09-21 23:43:11 +00004608 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4609 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00004610 return LHS;
4611
Steve Naroffbc76dd02008-12-10 22:14:21 +00004612 return QualType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004613 }
Steve Naroffec0550f2007-10-15 20:41:53 +00004614 }
Douglas Gregor72564e72009-02-26 23:50:07 +00004615
4616 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004617}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00004618
Chris Lattner5426bf62008-04-07 07:01:58 +00004619//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00004620// Integer Predicates
4621//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00004622
Eli Friedmanad74a752008-06-28 06:23:08 +00004623unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl632d7722009-11-05 21:10:57 +00004624 if (T->isBooleanType())
Eli Friedmanad74a752008-06-28 06:23:08 +00004625 return 1;
John McCall842aef82009-12-09 09:09:27 +00004626 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00004627 T = ET->getDecl()->getIntegerType();
Eli Friedmanf98aba32009-02-13 02:31:07 +00004628 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00004629 return (unsigned)getTypeSize(T);
4630}
4631
4632QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4633 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00004634
4635 // Turn <4 x signed int> -> <4 x unsigned int>
4636 if (const VectorType *VTy = T->getAs<VectorType>())
4637 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson82287d12010-02-05 00:12:22 +00004638 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattner6a2b9262009-10-17 20:33:28 +00004639
4640 // For enums, we return the unsigned version of the base type.
4641 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00004642 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00004643
4644 const BuiltinType *BTy = T->getAs<BuiltinType>();
4645 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00004646 switch (BTy->getKind()) {
4647 case BuiltinType::Char_S:
4648 case BuiltinType::SChar:
4649 return UnsignedCharTy;
4650 case BuiltinType::Short:
4651 return UnsignedShortTy;
4652 case BuiltinType::Int:
4653 return UnsignedIntTy;
4654 case BuiltinType::Long:
4655 return UnsignedLongTy;
4656 case BuiltinType::LongLong:
4657 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004658 case BuiltinType::Int128:
4659 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004660 default:
4661 assert(0 && "Unexpected signed integer type");
4662 return QualType();
4663 }
4664}
4665
Douglas Gregor2cf26342009-04-09 22:27:44 +00004666ExternalASTSource::~ExternalASTSource() { }
4667
4668void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004669
4670
4671//===----------------------------------------------------------------------===//
4672// Builtin Type Computation
4673//===----------------------------------------------------------------------===//
4674
4675/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4676/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004677static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004678 ASTContext::GetBuiltinTypeError &Error,
4679 bool AllowTypeModifiers = true) {
4680 // Modifiers.
4681 int HowLong = 0;
4682 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004683
Chris Lattner86df27b2009-06-14 00:45:47 +00004684 // Read the modifiers first.
4685 bool Done = false;
4686 while (!Done) {
4687 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004688 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004689 case 'S':
4690 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4691 assert(!Signed && "Can't use 'S' modifier multiple times!");
4692 Signed = true;
4693 break;
4694 case 'U':
4695 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4696 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4697 Unsigned = true;
4698 break;
4699 case 'L':
4700 assert(HowLong <= 2 && "Can't have LLLL modifier");
4701 ++HowLong;
4702 break;
4703 }
4704 }
4705
4706 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004707
Chris Lattner86df27b2009-06-14 00:45:47 +00004708 // Read the base type.
4709 switch (*Str++) {
4710 default: assert(0 && "Unknown builtin type letter!");
4711 case 'v':
4712 assert(HowLong == 0 && !Signed && !Unsigned &&
4713 "Bad modifiers used with 'v'!");
4714 Type = Context.VoidTy;
4715 break;
4716 case 'f':
4717 assert(HowLong == 0 && !Signed && !Unsigned &&
4718 "Bad modifiers used with 'f'!");
4719 Type = Context.FloatTy;
4720 break;
4721 case 'd':
4722 assert(HowLong < 2 && !Signed && !Unsigned &&
4723 "Bad modifiers used with 'd'!");
4724 if (HowLong)
4725 Type = Context.LongDoubleTy;
4726 else
4727 Type = Context.DoubleTy;
4728 break;
4729 case 's':
4730 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4731 if (Unsigned)
4732 Type = Context.UnsignedShortTy;
4733 else
4734 Type = Context.ShortTy;
4735 break;
4736 case 'i':
4737 if (HowLong == 3)
4738 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4739 else if (HowLong == 2)
4740 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4741 else if (HowLong == 1)
4742 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4743 else
4744 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4745 break;
4746 case 'c':
4747 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4748 if (Signed)
4749 Type = Context.SignedCharTy;
4750 else if (Unsigned)
4751 Type = Context.UnsignedCharTy;
4752 else
4753 Type = Context.CharTy;
4754 break;
4755 case 'b': // boolean
4756 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4757 Type = Context.BoolTy;
4758 break;
4759 case 'z': // size_t.
4760 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4761 Type = Context.getSizeType();
4762 break;
4763 case 'F':
4764 Type = Context.getCFConstantStringType();
4765 break;
4766 case 'a':
4767 Type = Context.getBuiltinVaListType();
4768 assert(!Type.isNull() && "builtin va list type not initialized!");
4769 break;
4770 case 'A':
4771 // This is a "reference" to a va_list; however, what exactly
4772 // this means depends on how va_list is defined. There are two
4773 // different kinds of va_list: ones passed by value, and ones
4774 // passed by reference. An example of a by-value va_list is
4775 // x86, where va_list is a char*. An example of by-ref va_list
4776 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4777 // we want this argument to be a char*&; for x86-64, we want
4778 // it to be a __va_list_tag*.
4779 Type = Context.getBuiltinVaListType();
4780 assert(!Type.isNull() && "builtin va list type not initialized!");
4781 if (Type->isArrayType()) {
4782 Type = Context.getArrayDecayedType(Type);
4783 } else {
4784 Type = Context.getLValueReferenceType(Type);
4785 }
4786 break;
4787 case 'V': {
4788 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004789 unsigned NumElements = strtoul(Str, &End, 10);
4790 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004791
Chris Lattner86df27b2009-06-14 00:45:47 +00004792 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004793
Chris Lattner86df27b2009-06-14 00:45:47 +00004794 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson82287d12010-02-05 00:12:22 +00004795 // FIXME: Don't know what to do about AltiVec.
4796 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattner86df27b2009-06-14 00:45:47 +00004797 break;
4798 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004799 case 'X': {
4800 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4801 Type = Context.getComplexType(ElementType);
4802 break;
4803 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004804 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004805 Type = Context.getFILEType();
4806 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004807 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004808 return QualType();
4809 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004810 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004811 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004812 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004813 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004814 else
4815 Type = Context.getjmp_bufType();
4816
Mike Stumpfd612db2009-07-28 23:47:15 +00004817 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004818 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004819 return QualType();
4820 }
4821 break;
Mike Stump782fa302009-07-28 02:25:19 +00004822 }
Mike Stump1eb44332009-09-09 15:08:12 +00004823
Chris Lattner86df27b2009-06-14 00:45:47 +00004824 if (!AllowTypeModifiers)
4825 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004826
Chris Lattner86df27b2009-06-14 00:45:47 +00004827 Done = false;
4828 while (!Done) {
John McCall187ab372010-03-12 04:21:28 +00004829 switch (char c = *Str++) {
Chris Lattner86df27b2009-06-14 00:45:47 +00004830 default: Done = true; --Str; break;
4831 case '*':
Chris Lattner86df27b2009-06-14 00:45:47 +00004832 case '&':
John McCall187ab372010-03-12 04:21:28 +00004833 {
4834 // Both pointers and references can have their pointee types
4835 // qualified with an address space.
4836 char *End;
4837 unsigned AddrSpace = strtoul(Str, &End, 10);
4838 if (End != Str && AddrSpace != 0) {
4839 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4840 Str = End;
4841 }
4842 }
4843 if (c == '*')
4844 Type = Context.getPointerType(Type);
4845 else
4846 Type = Context.getLValueReferenceType(Type);
Chris Lattner86df27b2009-06-14 00:45:47 +00004847 break;
4848 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4849 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004850 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004851 break;
Fariborz Jahanian013af392010-01-26 22:48:42 +00004852 case 'D':
4853 Type = Context.getVolatileType(Type);
4854 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004855 }
4856 }
Mike Stump1eb44332009-09-09 15:08:12 +00004857
Chris Lattner86df27b2009-06-14 00:45:47 +00004858 return Type;
4859}
4860
4861/// GetBuiltinType - Return the type for the specified builtin.
4862QualType ASTContext::GetBuiltinType(unsigned id,
4863 GetBuiltinTypeError &Error) {
4864 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004865
Chris Lattner86df27b2009-06-14 00:45:47 +00004866 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004867
Chris Lattner86df27b2009-06-14 00:45:47 +00004868 Error = GE_None;
4869 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4870 if (Error != GE_None)
4871 return QualType();
4872 while (TypeStr[0] && TypeStr[0] != '.') {
4873 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4874 if (Error != GE_None)
4875 return QualType();
4876
4877 // Do array -> pointer decay. The builtin should use the decayed type.
4878 if (Ty->isArrayType())
4879 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004880
Chris Lattner86df27b2009-06-14 00:45:47 +00004881 ArgTypes.push_back(Ty);
4882 }
4883
4884 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4885 "'.' should only occur at end of builtin type list!");
4886
4887 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4888 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4889 return getFunctionNoProtoType(ResType);
Douglas Gregorce056bc2010-02-21 22:15:06 +00004890
4891 // FIXME: Should we create noreturn types?
Chris Lattner86df27b2009-06-14 00:45:47 +00004892 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00004893 TypeStr[0] == '.', 0, false, false, 0, 0,
4894 false, CC_Default);
Chris Lattner86df27b2009-06-14 00:45:47 +00004895}
Eli Friedmana95d7572009-08-19 07:44:53 +00004896
4897QualType
4898ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4899 // Perform the usual unary conversions. We do this early so that
4900 // integral promotions to "int" can allow us to exit early, in the
4901 // lhs == rhs check. Also, for conversion purposes, we ignore any
4902 // qualifiers. For example, "const float" and "float" are
4903 // equivalent.
4904 if (lhs->isPromotableIntegerType())
4905 lhs = getPromotedIntegerType(lhs);
4906 else
4907 lhs = lhs.getUnqualifiedType();
4908 if (rhs->isPromotableIntegerType())
4909 rhs = getPromotedIntegerType(rhs);
4910 else
4911 rhs = rhs.getUnqualifiedType();
4912
4913 // If both types are identical, no conversion is needed.
4914 if (lhs == rhs)
4915 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004916
Eli Friedmana95d7572009-08-19 07:44:53 +00004917 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4918 // The caller can deal with this (e.g. pointer + int).
4919 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4920 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004921
4922 // At this point, we have two different arithmetic types.
4923
Eli Friedmana95d7572009-08-19 07:44:53 +00004924 // Handle complex types first (C99 6.3.1.8p1).
4925 if (lhs->isComplexType() || rhs->isComplexType()) {
4926 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004927 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004928 // convert the rhs to the lhs complex type.
4929 return lhs;
4930 }
Mike Stump1eb44332009-09-09 15:08:12 +00004931 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004932 // convert the lhs to the rhs complex type.
4933 return rhs;
4934 }
4935 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004936 // When both operands are complex, the shorter operand is converted to the
4937 // type of the longer, and that is the type of the result. This corresponds
4938 // to what is done when combining two real floating-point operands.
4939 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004940 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004941 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004942 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004943 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004944 // "double _Complex" is promoted to "long double _Complex".
4945 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004946
4947 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004948 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004949 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004950 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004951 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004952 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4953 // domains match. This is a requirement for our implementation, C99
4954 // does not require this promotion.
4955 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4956 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4957 return rhs;
4958 } else { // handle "_Complex double, double".
4959 return lhs;
4960 }
4961 }
4962 return lhs; // The domain/size match exactly.
4963 }
4964 // Now handle "real" floating types (i.e. float, double, long double).
4965 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4966 // if we have an integer operand, the result is the real floating type.
4967 if (rhs->isIntegerType()) {
4968 // convert rhs to the lhs floating point type.
4969 return lhs;
4970 }
4971 if (rhs->isComplexIntegerType()) {
4972 // convert rhs to the complex floating point type.
4973 return getComplexType(lhs);
4974 }
4975 if (lhs->isIntegerType()) {
4976 // convert lhs to the rhs floating point type.
4977 return rhs;
4978 }
Mike Stump1eb44332009-09-09 15:08:12 +00004979 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004980 // convert lhs to the complex floating point type.
4981 return getComplexType(rhs);
4982 }
4983 // We have two real floating types, float/complex combos were handled above.
4984 // Convert the smaller operand to the bigger result.
4985 int result = getFloatingTypeOrder(lhs, rhs);
4986 if (result > 0) // convert the rhs
4987 return lhs;
4988 assert(result < 0 && "illegal float comparison");
4989 return rhs; // convert the lhs
4990 }
4991 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4992 // Handle GCC complex int extension.
4993 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4994 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4995
4996 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004997 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004998 rhsComplexInt->getElementType()) >= 0)
4999 return lhs; // convert the rhs
5000 return rhs;
5001 } else if (lhsComplexInt && rhs->isIntegerType()) {
5002 // convert the rhs to the lhs complex type.
5003 return lhs;
5004 } else if (rhsComplexInt && lhs->isIntegerType()) {
5005 // convert the lhs to the rhs complex type.
5006 return rhs;
5007 }
5008 }
5009 // Finally, we have two differing integer types.
5010 // The rules for this case are in C99 6.3.1.8
5011 int compare = getIntegerTypeOrder(lhs, rhs);
5012 bool lhsSigned = lhs->isSignedIntegerType(),
5013 rhsSigned = rhs->isSignedIntegerType();
5014 QualType destType;
5015 if (lhsSigned == rhsSigned) {
5016 // Same signedness; use the higher-ranked type
5017 destType = compare >= 0 ? lhs : rhs;
5018 } else if (compare != (lhsSigned ? 1 : -1)) {
5019 // The unsigned type has greater than or equal rank to the
5020 // signed type, so use the unsigned type
5021 destType = lhsSigned ? rhs : lhs;
5022 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5023 // The two types are different widths; if we are here, that
5024 // means the signed type is larger than the unsigned type, so
5025 // use the signed type.
5026 destType = lhsSigned ? lhs : rhs;
5027 } else {
5028 // The signed type is higher-ranked than the unsigned type,
5029 // but isn't actually any bigger (like unsigned int and long
5030 // on most 32-bit systems). Use the unsigned type corresponding
5031 // to the signed type.
5032 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5033 }
5034 return destType;
5035}