blob: 7f2e35b263bcdb6eb590eddd2e42088a3fcd4de3 [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),
John McCall0c01d182010-03-24 05:22:00 +000048 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts),
49 LastSDM(0, 0) {
David Chisnall0f436562009-08-17 16:35:33 +000050 ObjCIdRedefinitionType = QualType();
51 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +000052 ObjCSelRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000053 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000054 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000055 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000056}
57
Reid Spencer5f016e22007-07-11 17:01:13 +000058ASTContext::~ASTContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +000059 // Release the DenseMaps associated with DeclContext objects.
60 // FIXME: Is this the ideal solution?
61 ReleaseDeclContextMaps();
Douglas Gregor7d10b7e2010-03-02 23:58:15 +000062
63 // Release all of the memory associated with overridden C++ methods.
64 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
65 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
66 OM != OMEnd; ++OM)
67 OM->second.Destroy();
Ted Kremenek3478eb62010-02-11 07:12:28 +000068
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000069 if (FreeMemory) {
70 // Deallocate all the types.
71 while (!Types.empty()) {
72 Types.back()->Destroy(*this);
73 Types.pop_back();
74 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000075
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000076 for (llvm::FoldingSet<ExtQuals>::iterator
77 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
78 // Increment in loop to prevent using deallocated memory.
John McCall0953e762009-09-24 19:53:00 +000079 Deallocate(&*I++);
Nuno Lopesb74668e2008-12-17 22:30:25 +000080 }
Nuno Lopesb74668e2008-12-17 22:30:25 +000081
Ted Kremenek503524a2010-03-08 20:56:29 +000082 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
83 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
84 // Increment in loop to prevent using deallocated memory.
85 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
86 R->Destroy(*this);
87 }
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000088
Ted Kremenek503524a2010-03-08 20:56:29 +000089 for (llvm::DenseMap<const ObjCContainerDecl*,
90 const ASTRecordLayout*>::iterator
91 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
92 // Increment in loop to prevent using deallocated memory.
93 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
94 R->Destroy(*this);
95 }
Nuno Lopesb74668e2008-12-17 22:30:25 +000096 }
97
Douglas Gregorab452ba2009-03-26 23:50:42 +000098 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000099 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
100 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +0000101 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenekbbfd68d2009-12-23 21:13:52 +0000102 NNS != NNSEnd; ) {
103 // Increment in loop to prevent using deallocated memory.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +0000104 (*NNS++).Destroy(*this);
Ted Kremenekbbfd68d2009-12-23 21:13:52 +0000105 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000106
107 if (GlobalNestedNameSpecifier)
108 GlobalNestedNameSpecifier->Destroy(*this);
109
Eli Friedmanb26153c2008-05-27 03:08:09 +0000110 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000111}
112
Mike Stump1eb44332009-09-09 15:08:12 +0000113void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
115 ExternalSource.reset(Source.take());
116}
117
Reid Spencer5f016e22007-07-11 17:01:13 +0000118void ASTContext::PrintStats() const {
119 fprintf(stderr, "*** AST Context Stats:\n");
120 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000121
Douglas Gregordbe833d2009-05-26 14:40:08 +0000122 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000123#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000124#define ABSTRACT_TYPE(Name, Parent)
125#include "clang/AST/TypeNodes.def"
126 0 // Extra
127 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
130 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000131 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 }
133
Douglas Gregordbe833d2009-05-26 14:40:08 +0000134 unsigned Idx = 0;
135 unsigned TotalBytes = 0;
136#define TYPE(Name, Parent) \
137 if (counts[Idx]) \
138 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
139 TotalBytes += counts[Idx] * sizeof(Name##Type); \
140 ++Idx;
141#define ABSTRACT_TYPE(Name, Parent)
142#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Douglas Gregordbe833d2009-05-26 14:40:08 +0000144 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145
146 if (ExternalSource.get()) {
147 fprintf(stderr, "\n");
148 ExternalSource->PrintStats();
149 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000150}
151
152
John McCalle27ec8a2009-10-23 23:03:21 +0000153void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000154 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000155 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000156 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157}
158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159void ASTContext::InitBuiltinTypes() {
160 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 // C99 6.2.5p19.
163 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 // C99 6.2.5p2.
166 InitBuiltinType(BoolTy, BuiltinType::Bool);
167 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000168 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 InitBuiltinType(CharTy, BuiltinType::Char_S);
170 else
171 InitBuiltinType(CharTy, BuiltinType::Char_U);
172 // C99 6.2.5p4.
173 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
174 InitBuiltinType(ShortTy, BuiltinType::Short);
175 InitBuiltinType(IntTy, BuiltinType::Int);
176 InitBuiltinType(LongTy, BuiltinType::Long);
177 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // C99 6.2.5p6.
180 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
181 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
182 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
183 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
184 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // C99 6.2.5p10.
187 InitBuiltinType(FloatTy, BuiltinType::Float);
188 InitBuiltinType(DoubleTy, BuiltinType::Double);
189 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000190
Chris Lattner2df9ced2009-04-30 02:43:43 +0000191 // GNU extension, 128-bit integers.
192 InitBuiltinType(Int128Ty, BuiltinType::Int128);
193 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
194
Chris Lattner3a250322009-02-26 23:43:47 +0000195 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
196 InitBuiltinType(WCharTy, BuiltinType::WChar);
197 else // C99
198 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000199
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000200 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
201 InitBuiltinType(Char16Ty, BuiltinType::Char16);
202 else // C99
203 Char16Ty = getFromTargetType(Target.getChar16Type());
204
205 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
206 InitBuiltinType(Char32Ty, BuiltinType::Char32);
207 else // C99
208 Char32Ty = getFromTargetType(Target.getChar32Type());
209
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000210 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000211 InitBuiltinType(OverloadTy, BuiltinType::Overload);
212
213 // Placeholder type for type-dependent expressions whose type is
214 // completely unknown. No code should ever check a type against
215 // DependentTy and users should never see it; however, it is here to
216 // help diagnose failures to properly check for type-dependent
217 // expressions.
218 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000219
Mike Stump1eb44332009-09-09 15:08:12 +0000220 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000221 // not yet been deduced.
222 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // C99 6.2.5p11.
225 FloatComplexTy = getComplexType(FloatTy);
226 DoubleComplexTy = getComplexType(DoubleTy);
227 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000228
Steve Naroff7e219e42007-10-15 14:41:52 +0000229 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Steve Naroffde2e22d2009-07-15 18:40:39 +0000231 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
232 ObjCIdTypedefType = QualType();
233 ObjCClassTypedefType = QualType();
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000234 ObjCSelTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000236 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroffde2e22d2009-07-15 18:40:39 +0000237 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
238 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000239 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff14108da2009-07-10 23:34:53 +0000240
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000241 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000243 // void * type
244 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000245
246 // nullptr type (C++0x 2.14.7)
247 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000248}
249
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000250MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000251ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000252 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000253 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000254 = InstantiatedFromStaticDataMember.find(Var);
255 if (Pos == InstantiatedFromStaticDataMember.end())
256 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Douglas Gregor7caa6822009-07-24 20:34:43 +0000258 return Pos->second;
259}
260
Mike Stump1eb44332009-09-09 15:08:12 +0000261void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000262ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
263 TemplateSpecializationKind TSK) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000264 assert(Inst->isStaticDataMember() && "Not a static data member");
265 assert(Tmpl->isStaticDataMember() && "Not a static data member");
266 assert(!InstantiatedFromStaticDataMember[Inst] &&
267 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000268 InstantiatedFromStaticDataMember[Inst]
269 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000270}
271
John McCall7ba107a2009-11-18 02:36:19 +0000272NamedDecl *
John McCalled976492009-12-04 22:46:56 +0000273ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCall7ba107a2009-11-18 02:36:19 +0000274 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCalled976492009-12-04 22:46:56 +0000275 = InstantiatedFromUsingDecl.find(UUD);
276 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson0d8df782009-08-29 19:37:28 +0000277 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Anders Carlsson0d8df782009-08-29 19:37:28 +0000279 return Pos->second;
280}
281
282void
John McCalled976492009-12-04 22:46:56 +0000283ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
284 assert((isa<UsingDecl>(Pattern) ||
285 isa<UnresolvedUsingValueDecl>(Pattern) ||
286 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
287 "pattern decl is not a using decl");
288 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
289 InstantiatedFromUsingDecl[Inst] = Pattern;
290}
291
292UsingShadowDecl *
293ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
294 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
295 = InstantiatedFromUsingShadowDecl.find(Inst);
296 if (Pos == InstantiatedFromUsingShadowDecl.end())
297 return 0;
298
299 return Pos->second;
300}
301
302void
303ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
304 UsingShadowDecl *Pattern) {
305 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
306 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +0000307}
308
Anders Carlssond8b285f2009-09-01 04:26:58 +0000309FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
310 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
311 = InstantiatedFromUnnamedFieldDecl.find(Field);
312 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
313 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Anders Carlssond8b285f2009-09-01 04:26:58 +0000315 return Pos->second;
316}
317
318void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
319 FieldDecl *Tmpl) {
320 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
321 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
322 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
323 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Anders Carlssond8b285f2009-09-01 04:26:58 +0000325 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
326}
327
Douglas Gregor7d10b7e2010-03-02 23:58:15 +0000328CXXMethodVector::iterator CXXMethodVector::begin() const {
329 if ((Storage & 0x01) == 0)
330 return reinterpret_cast<iterator>(&Storage);
331
332 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
333 return &Vec->front();
334}
335
336CXXMethodVector::iterator CXXMethodVector::end() const {
337 if ((Storage & 0x01) == 0) {
338 if (Storage == 0)
339 return reinterpret_cast<iterator>(&Storage);
340
341 return reinterpret_cast<iterator>(&Storage) + 1;
342 }
343
344 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
345 return &Vec->front() + Vec->size();
346}
347
348void CXXMethodVector::push_back(const CXXMethodDecl *Method) {
349 if (Storage == 0) {
350 // 0 -> 1 element.
351 Storage = reinterpret_cast<uintptr_t>(Method);
352 return;
353 }
354
355 vector_type *Vec;
356 if ((Storage & 0x01) == 0) {
357 // 1 -> 2 elements. Allocate a new vector and push the element into that
358 // vector.
359 Vec = new vector_type;
360 Vec->push_back(reinterpret_cast<const CXXMethodDecl *>(Storage));
361 Storage = reinterpret_cast<uintptr_t>(Vec) | 0x01;
362 } else
363 Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
364
365 // Add the new method to the vector.
366 Vec->push_back(Method);
367}
368
369void CXXMethodVector::Destroy() {
370 if (Storage & 0x01)
371 delete reinterpret_cast<vector_type *>(Storage & ~0x01);
372
373 Storage = 0;
374}
375
376
377ASTContext::overridden_cxx_method_iterator
378ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
379 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
380 = OverriddenMethods.find(Method);
381 if (Pos == OverriddenMethods.end())
382 return 0;
383
384 return Pos->second.begin();
385}
386
387ASTContext::overridden_cxx_method_iterator
388ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
389 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
390 = OverriddenMethods.find(Method);
391 if (Pos == OverriddenMethods.end())
392 return 0;
393
394 return Pos->second.end();
395}
396
397void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
398 const CXXMethodDecl *Overridden) {
399 OverriddenMethods[Method].push_back(Overridden);
400}
401
Douglas Gregor2e222532009-07-02 17:08:52 +0000402namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000403 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000404 : std::binary_function<SourceRange, SourceRange, bool> {
405 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor2e222532009-07-02 17:08:52 +0000407 public:
408 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregor2e222532009-07-02 17:08:52 +0000410 bool operator()(SourceRange X, SourceRange Y) {
411 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
412 }
413 };
414}
415
Chris Lattner464175b2007-07-18 17:52:12 +0000416//===----------------------------------------------------------------------===//
417// Type Sizing and Analysis
418//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000419
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000420/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
421/// scalar floating point type.
422const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000423 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000424 assert(BT && "Not a floating point type!");
425 switch (BT->getKind()) {
426 default: assert(0 && "Not a floating point type!");
427 case BuiltinType::Float: return Target.getFloatFormat();
428 case BuiltinType::Double: return Target.getDoubleFormat();
429 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
430 }
431}
432
Ken Dyck8b752f12010-01-27 17:10:57 +0000433/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000434/// specified decl. Note that bitfields do not have a valid alignment, so
435/// this method will assert on them.
Sebastian Redl5d484e82009-11-23 17:18:46 +0000436/// If @p RefAsPointee, references are treated like their underlying type
437/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck8b752f12010-01-27 17:10:57 +0000438CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000439 unsigned Align = Target.getCharWidth();
440
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000441 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Sean Huntbbd37c62009-11-21 08:43:09 +0000442 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedmandcdafb62009-02-22 02:56:25 +0000443
Chris Lattneraf707ab2009-01-24 21:53:27 +0000444 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
445 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000446 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl5d484e82009-11-23 17:18:46 +0000447 if (RefAsPointee)
448 T = RT->getPointeeType();
449 else
450 T = getPointerType(RT->getPointeeType());
451 }
452 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000453 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000454 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
455 T = cast<ArrayType>(T)->getElementType();
456
457 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
458 }
Charles Davis05f62472010-02-23 04:52:00 +0000459 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
460 // In the case of a field in a packed struct, we want the minimum
461 // of the alignment of the field and the alignment of the struct.
462 Align = std::min(Align,
463 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
464 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000465 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000466
Ken Dyck8b752f12010-01-27 17:10:57 +0000467 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000468}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000469
Chris Lattnera7674d82007-07-13 22:13:22 +0000470/// getTypeSize - Return the size of the specified type, in bits. This method
471/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000472///
473/// FIXME: Pointers into different addr spaces could have different sizes and
474/// alignment requirements: getPointerInfo should take an AddrSpace, this
475/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000476std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000477ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000478 uint64_t Width=0;
479 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000480 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000481#define TYPE(Class, Base)
482#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000483#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000484#define DEPENDENT_TYPE(Class, Base) case Type::Class:
485#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000486 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000487 break;
488
Chris Lattner692233e2007-07-13 22:27:08 +0000489 case Type::FunctionNoProto:
490 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000491 // GCC extension: alignof(function) = 32 bits
492 Width = 0;
493 Align = 32;
494 break;
495
Douglas Gregor72564e72009-02-26 23:50:07 +0000496 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000497 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000498 Width = 0;
499 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
500 break;
501
Steve Narofffb22d962007-08-30 01:06:46 +0000502 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000503 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattner98be4942008-03-05 18:54:05 +0000505 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000506 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000507 Align = EltInfo.second;
508 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000509 }
Nate Begeman213541a2008-04-18 23:10:10 +0000510 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000511 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000512 const VectorType *VT = cast<VectorType>(T);
513 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
514 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000515 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000516 // If the alignment is not a power of 2, round up to the next power of 2.
517 // This happens for non-power-of-2 length vectors.
Chris Lattner9fcfe922009-10-22 05:17:15 +0000518 if (VT->getNumElements() & (VT->getNumElements()-1)) {
519 Align = llvm::NextPowerOf2(Align);
520 Width = llvm::RoundUpToAlignment(Width, Align);
521 }
Chris Lattner030d8842007-07-19 22:06:24 +0000522 break;
523 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000524
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000525 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000526 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000527 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000528 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000529 // GCC extension: alignof(void) = 8 bits.
530 Width = 0;
531 Align = 8;
532 break;
533
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000534 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000535 Width = Target.getBoolWidth();
536 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000537 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000538 case BuiltinType::Char_S:
539 case BuiltinType::Char_U:
540 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000541 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000542 Width = Target.getCharWidth();
543 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000544 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000545 case BuiltinType::WChar:
546 Width = Target.getWCharWidth();
547 Align = Target.getWCharAlign();
548 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000549 case BuiltinType::Char16:
550 Width = Target.getChar16Width();
551 Align = Target.getChar16Align();
552 break;
553 case BuiltinType::Char32:
554 Width = Target.getChar32Width();
555 Align = Target.getChar32Align();
556 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000557 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000558 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000559 Width = Target.getShortWidth();
560 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000561 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000562 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000563 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000564 Width = Target.getIntWidth();
565 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000566 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000567 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000568 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000569 Width = Target.getLongWidth();
570 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000571 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000572 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000573 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000574 Width = Target.getLongLongWidth();
575 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000576 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000577 case BuiltinType::Int128:
578 case BuiltinType::UInt128:
579 Width = 128;
580 Align = 128; // int128_t is 128-bit aligned on all targets.
581 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000582 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000583 Width = Target.getFloatWidth();
584 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000585 break;
586 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000587 Width = Target.getDoubleWidth();
588 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000589 break;
590 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000591 Width = Target.getLongDoubleWidth();
592 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000593 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000594 case BuiltinType::NullPtr:
595 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
596 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000597 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000598 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000599 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000600 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000601 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000602 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000603 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000604 case Type::BlockPointer: {
605 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
606 Width = Target.getPointerWidth(AS);
607 Align = Target.getPointerAlign(AS);
608 break;
609 }
Sebastian Redl5d484e82009-11-23 17:18:46 +0000610 case Type::LValueReference:
611 case Type::RValueReference: {
612 // alignof and sizeof should never enter this code path here, so we go
613 // the pointer route.
614 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
615 Width = Target.getPointerWidth(AS);
616 Align = Target.getPointerAlign(AS);
617 break;
618 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000619 case Type::Pointer: {
620 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000621 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000622 Align = Target.getPointerAlign(AS);
623 break;
624 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000625 case Type::MemberPointer: {
Sebastian Redlf30208a2009-01-24 21:16:55 +0000626 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000627 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000628 getTypeInfo(getPointerDiffType());
629 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000630 if (Pointee->isFunctionType())
631 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000632 Align = PtrDiffInfo.second;
633 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000634 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000635 case Type::Complex: {
636 // Complex types have the same alignment as their elements, but twice the
637 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000638 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000639 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000640 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000641 Align = EltInfo.second;
642 break;
643 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000644 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000645 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000646 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
647 Width = Layout.getSize();
648 Align = Layout.getAlignment();
649 break;
650 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000651 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000652 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000653 const TagType *TT = cast<TagType>(T);
654
655 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000656 Width = 1;
657 Align = 1;
658 break;
659 }
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Daniel Dunbar1d751182008-11-08 05:48:37 +0000661 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000662 return getTypeInfo(ET->getDecl()->getIntegerType());
663
Daniel Dunbar1d751182008-11-08 05:48:37 +0000664 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000665 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
666 Width = Layout.getSize();
667 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000668 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000669 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000670
Chris Lattner9fcfe922009-10-22 05:17:15 +0000671 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000672 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
673 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000674
Chris Lattner9fcfe922009-10-22 05:17:15 +0000675 case Type::Elaborated:
676 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
677 .getTypePtr());
John McCall7da24312009-09-05 00:15:47 +0000678
Douglas Gregor18857642009-04-30 17:32:17 +0000679 case Type::Typedef: {
680 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000681 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000682 Align = std::max(Aligned->getMaxAlignment(),
683 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregor18857642009-04-30 17:32:17 +0000684 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
685 } else
686 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000687 break;
Chris Lattner71763312008-04-06 22:05:18 +0000688 }
Douglas Gregor18857642009-04-30 17:32:17 +0000689
690 case Type::TypeOfExpr:
691 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
692 .getTypePtr());
693
694 case Type::TypeOf:
695 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
696
Anders Carlsson395b4752009-06-24 19:06:50 +0000697 case Type::Decltype:
698 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
699 .getTypePtr());
700
Douglas Gregor18857642009-04-30 17:32:17 +0000701 case Type::QualifiedName:
702 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000703
John McCall3cb0ebd2010-03-10 03:28:59 +0000704 case Type::InjectedClassName:
705 return getTypeInfo(cast<InjectedClassNameType>(T)
706 ->getUnderlyingType().getTypePtr());
707
Douglas Gregor18857642009-04-30 17:32:17 +0000708 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000709 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000710 "Cannot request the size of a dependent type");
711 // FIXME: this is likely to be wrong once we support template
712 // aliases, since a template alias could refer to a typedef that
713 // has an __aligned__ attribute on it.
714 return getTypeInfo(getCanonicalType(T));
715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Chris Lattner464175b2007-07-18 17:52:12 +0000717 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000718 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000719}
720
Ken Dyckbdc601b2009-12-22 14:23:30 +0000721/// getTypeSizeInChars - Return the size of the specified type, in characters.
722/// This method does not work on incomplete types.
723CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000724 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000725}
726CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000727 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000728}
729
Ken Dyck16e20cc2010-01-26 17:25:18 +0000730/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck86fa4312010-01-26 17:22:55 +0000731/// characters. This method does not work on incomplete types.
732CharUnits ASTContext::getTypeAlignInChars(QualType T) {
733 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
734}
735CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
736 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
737}
738
Chris Lattner34ebde42009-01-27 18:08:34 +0000739/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
740/// type for the current target in bits. This can be different than the ABI
741/// alignment in cases where it is beneficial for performance to overalign
742/// a data type.
743unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
744 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000745
746 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000747 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000748 T = CT->getElementType().getTypePtr();
749 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
750 T->isSpecificBuiltinType(BuiltinType::LongLong))
751 return std::max(ABIAlign, (unsigned)getTypeSize(T));
752
Chris Lattner34ebde42009-01-27 18:08:34 +0000753 return ABIAlign;
754}
755
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000756static void CollectLocalObjCIvars(ASTContext *Ctx,
757 const ObjCInterfaceDecl *OI,
758 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000759 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
760 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000761 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000762 if (!IVDecl->isInvalidDecl())
763 Fields.push_back(cast<FieldDecl>(IVDecl));
764 }
765}
766
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000767void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
768 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
769 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
770 CollectObjCIvars(SuperClass, Fields);
771 CollectLocalObjCIvars(this, OI, Fields);
772}
773
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000774/// ShallowCollectObjCIvars -
775/// Collect all ivars, including those synthesized, in the current class.
776///
777void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000778 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000779 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
780 E = OI->ivar_end(); I != E; ++I) {
781 Ivars.push_back(*I);
782 }
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000783
784 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000785}
786
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000787/// CollectNonClassIvars -
788/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek7d2aa112010-03-11 19:44:54 +0000789/// This includes synthesized ivars (via @synthesize) and those in
790// class's @implementation.
Fariborz Jahanian98200742009-05-12 18:14:29 +0000791///
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000792void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian98200742009-05-12 18:14:29 +0000793 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000794 // Find ivars declared in class extension.
795 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
796 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
797 E = CDecl->ivar_end(); I != E; ++I) {
798 Ivars.push_back(*I);
799 }
800 }
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000801
Ted Kremenek7d2aa112010-03-11 19:44:54 +0000802 // Also add any ivar defined in this class's implementation. This
803 // includes synthesized ivars.
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000804 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
805 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
806 E = ImplDecl->ivar_end(); I != E; ++I)
807 Ivars.push_back(*I);
808 }
Fariborz Jahanian98200742009-05-12 18:14:29 +0000809}
810
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000811/// CollectInheritedProtocols - Collect all protocols in current class and
812/// those inherited by it.
813void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000814 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000815 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
816 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
817 PE = OI->protocol_end(); P != PE; ++P) {
818 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000819 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000820 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000821 PE = Proto->protocol_end(); P != PE; ++P) {
822 Protocols.insert(*P);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000823 CollectInheritedProtocols(*P, Protocols);
824 }
Fariborz Jahanianb2f81212010-02-25 18:24:33 +0000825 }
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000826
827 // Categories of this Interface.
828 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
829 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
830 CollectInheritedProtocols(CDeclChain, Protocols);
831 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
832 while (SD) {
833 CollectInheritedProtocols(SD, Protocols);
834 SD = SD->getSuperClass();
835 }
836 return;
837 }
838 if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
839 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
840 PE = OC->protocol_end(); P != PE; ++P) {
841 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000842 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000843 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
844 PE = Proto->protocol_end(); P != PE; ++P)
845 CollectInheritedProtocols(*P, Protocols);
846 }
847 return;
848 }
849 if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
850 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
851 PE = OP->protocol_end(); P != PE; ++P) {
852 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000853 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000854 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
855 PE = Proto->protocol_end(); P != PE; ++P)
856 CollectInheritedProtocols(*P, Protocols);
857 }
858 return;
859 }
860}
861
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000862unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
863 unsigned count = 0;
864 // Count ivars declared in class extension.
865 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
866 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
867 E = CDecl->ivar_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000868 ++count;
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000869 }
870 }
871
872 // Count ivar defined in this class's implementation. This
873 // includes synthesized ivars.
874 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
875 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
876 E = ImplDecl->ivar_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000877 ++count;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000878 return count;
879}
880
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000881/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
882ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
883 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
884 I = ObjCImpls.find(D);
885 if (I != ObjCImpls.end())
886 return cast<ObjCImplementationDecl>(I->second);
887 return 0;
888}
889/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
890ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
891 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
892 I = ObjCImpls.find(D);
893 if (I != ObjCImpls.end())
894 return cast<ObjCCategoryImplDecl>(I->second);
895 return 0;
896}
897
898/// \brief Set the implementation of ObjCInterfaceDecl.
899void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
900 ObjCImplementationDecl *ImplD) {
901 assert(IFaceD && ImplD && "Passed null params");
902 ObjCImpls[IFaceD] = ImplD;
903}
904/// \brief Set the implementation of ObjCCategoryDecl.
905void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
906 ObjCCategoryImplDecl *ImplD) {
907 assert(CatD && ImplD && "Passed null params");
908 ObjCImpls[CatD] = ImplD;
909}
910
John McCalla93c9342009-12-07 02:54:59 +0000911/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000912///
John McCalla93c9342009-12-07 02:54:59 +0000913/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000914/// the TypeLoc wrappers.
915///
916/// \param T the type that will be the basis for type source info. This type
917/// should refer to how the declarator was written in source code, not to
918/// what type semantic analysis resolved the declarator to.
John McCalla93c9342009-12-07 02:54:59 +0000919TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall109de5e2009-10-21 00:23:54 +0000920 unsigned DataSize) {
921 if (!DataSize)
922 DataSize = TypeLoc::getFullDataSizeForType(T);
923 else
924 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCalla93c9342009-12-07 02:54:59 +0000925 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall109de5e2009-10-21 00:23:54 +0000926
John McCalla93c9342009-12-07 02:54:59 +0000927 TypeSourceInfo *TInfo =
928 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
929 new (TInfo) TypeSourceInfo(T);
930 return TInfo;
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000931}
932
John McCalla93c9342009-12-07 02:54:59 +0000933TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCalla4eb74d2009-10-23 21:14:09 +0000934 SourceLocation L) {
John McCalla93c9342009-12-07 02:54:59 +0000935 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCalla4eb74d2009-10-23 21:14:09 +0000936 DI->getTypeLoc().initialize(L);
937 return DI;
938}
939
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000940/// getInterfaceLayoutImpl - Get or compute information about the
941/// layout of the given interface.
942///
943/// \param Impl - If given, also include the layout of the interface's
944/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000945const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000946ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
947 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000948 assert(!D->isForwardDecl() && "Invalid interface decl!");
949
Devang Patel44a3dde2008-06-04 21:54:36 +0000950 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000951 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000952 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
953 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
954 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000955
Daniel Dunbar453addb2009-05-03 11:16:44 +0000956 // Add in synthesized ivar count if laying out an implementation.
957 if (Impl) {
Fariborz Jahanian3bfacdf2010-03-22 18:25:57 +0000958 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000959 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000960 // entry. Note we can't cache this because we simply free all
961 // entries later; however we shouldn't look up implementations
962 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000963 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000964 return getObjCLayout(D, 0);
965 }
966
Mike Stump1eb44332009-09-09 15:08:12 +0000967 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000968 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
969 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Devang Patel44a3dde2008-06-04 21:54:36 +0000971 return *NewEntry;
972}
973
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000974const ASTRecordLayout &
975ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
976 return getObjCLayout(D, 0);
977}
978
979const ASTRecordLayout &
980ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
981 return getObjCLayout(D->getClassInterface(), D);
982}
983
Devang Patel88a981b2007-11-01 19:11:01 +0000984/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000985/// specified record (struct/union/class), which indicates its size and field
986/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000987const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000988 D = D->getDefinition();
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000989 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000990
Chris Lattner464175b2007-07-18 17:52:12 +0000991 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +0000992 // Note that we can't save a reference to the entry because this function
993 // is recursive.
994 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000995 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000996
Mike Stump1eb44332009-09-09 15:08:12 +0000997 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000998 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +0000999 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattner5d2a6302007-07-18 18:26:58 +00001001 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001002}
1003
Anders Carlssonf53df232009-12-07 04:35:11 +00001004const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor952b0172010-02-11 01:04:33 +00001005 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlssonf53df232009-12-07 04:35:11 +00001006 assert(RD && "Cannot get key function for forward declarations!");
1007
1008 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1009 if (!Entry)
1010 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
1011 else
1012 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
1013 "Key function changed!");
1014
1015 return Entry;
1016}
1017
Chris Lattnera7674d82007-07-13 22:13:22 +00001018//===----------------------------------------------------------------------===//
1019// Type creation/memoization methods
1020//===----------------------------------------------------------------------===//
1021
John McCall0953e762009-09-24 19:53:00 +00001022QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1023 unsigned Fast = Quals.getFastQualifiers();
1024 Quals.removeFastQualifiers();
1025
1026 // Check if we've already instantiated this type.
1027 llvm::FoldingSetNodeID ID;
1028 ExtQuals::Profile(ID, TypeNode, Quals);
1029 void *InsertPos = 0;
1030 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1031 assert(EQ->getQualifiers() == Quals);
1032 QualType T = QualType(EQ, Fast);
1033 return T;
1034 }
1035
John McCall6b304a02009-09-24 23:30:46 +00001036 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001037 ExtQualNodes.InsertNode(New, InsertPos);
1038 QualType T = QualType(New, Fast);
1039 return T;
1040}
1041
1042QualType ASTContext::getVolatileType(QualType T) {
1043 QualType CanT = getCanonicalType(T);
1044 if (CanT.isVolatileQualified()) return T;
1045
1046 QualifierCollector Quals;
1047 const Type *TypeNode = Quals.strip(T);
1048 Quals.addVolatile();
1049
1050 return getExtQualType(TypeNode, Quals);
1051}
1052
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001053QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001054 QualType CanT = getCanonicalType(T);
1055 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001056 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001057
John McCall0953e762009-09-24 19:53:00 +00001058 // If we are composing extended qualifiers together, merge together
1059 // into one ExtQuals node.
1060 QualifierCollector Quals;
1061 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001062
John McCall0953e762009-09-24 19:53:00 +00001063 // If this type already has an address space specified, it cannot get
1064 // another one.
1065 assert(!Quals.hasAddressSpace() &&
1066 "Type cannot be in multiple addr spaces!");
1067 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001068
John McCall0953e762009-09-24 19:53:00 +00001069 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001070}
1071
Chris Lattnerb7d25532009-02-18 22:53:11 +00001072QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001073 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001074 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001075 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001076 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001078 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001079 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001080 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001081 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1082 return getPointerType(ResultType);
1083 }
1084 }
Mike Stump1eb44332009-09-09 15:08:12 +00001085
John McCall0953e762009-09-24 19:53:00 +00001086 // If we are composing extended qualifiers together, merge together
1087 // into one ExtQuals node.
1088 QualifierCollector Quals;
1089 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001090
John McCall0953e762009-09-24 19:53:00 +00001091 // If this type already has an ObjCGC specified, it cannot get
1092 // another one.
1093 assert(!Quals.hasObjCGCAttr() &&
1094 "Type cannot have multiple ObjCGCs!");
1095 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001096
John McCall0953e762009-09-24 19:53:00 +00001097 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001098}
Chris Lattnera7674d82007-07-13 22:13:22 +00001099
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001100static QualType getNoReturnCallConvType(ASTContext& Context, QualType T,
1101 bool AddNoReturn,
1102 CallingConv CallConv) {
John McCall0953e762009-09-24 19:53:00 +00001103 QualType ResultType;
Douglas Gregor43c79c22009-12-09 00:47:37 +00001104 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1105 QualType Pointee = Pointer->getPointeeType();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001106 ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn,
1107 CallConv);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001108 if (ResultType == Pointee)
1109 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001110
1111 ResultType = Context.getPointerType(ResultType);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001112 } else if (const BlockPointerType *BlockPointer
1113 = T->getAs<BlockPointerType>()) {
1114 QualType Pointee = BlockPointer->getPointeeType();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001115 ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn,
1116 CallConv);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001117 if (ResultType == Pointee)
1118 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001119
1120 ResultType = Context.getBlockPointerType(ResultType);
1121 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
1122 if (F->getNoReturnAttr() == AddNoReturn && F->getCallConv() == CallConv)
Douglas Gregor43c79c22009-12-09 00:47:37 +00001123 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001124
Douglas Gregor43c79c22009-12-09 00:47:37 +00001125 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001126 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
1127 AddNoReturn, CallConv);
John McCall0953e762009-09-24 19:53:00 +00001128 } else {
Douglas Gregor43c79c22009-12-09 00:47:37 +00001129 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall0953e762009-09-24 19:53:00 +00001130 ResultType
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001131 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1132 FPT->getNumArgs(), FPT->isVariadic(),
1133 FPT->getTypeQuals(),
1134 FPT->hasExceptionSpec(),
1135 FPT->hasAnyExceptionSpec(),
1136 FPT->getNumExceptions(),
1137 FPT->exception_begin(),
1138 AddNoReturn, CallConv);
John McCall0953e762009-09-24 19:53:00 +00001139 }
Douglas Gregor43c79c22009-12-09 00:47:37 +00001140 } else
1141 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001142
1143 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1144}
1145
1146QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
1147 return getNoReturnCallConvType(*this, T, AddNoReturn, T.getCallConv());
1148}
1149
1150QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
1151 return getNoReturnCallConvType(*this, T, T.getNoReturnAttr(), CallConv);
Mike Stump24556362009-07-25 21:26:53 +00001152}
1153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154/// getComplexType - Return the uniqued reference to the type for a complex
1155/// number with the specified element type.
1156QualType ASTContext::getComplexType(QualType T) {
1157 // Unique pointers, to guarantee there is only one pointer of a particular
1158 // structure.
1159 llvm::FoldingSetNodeID ID;
1160 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 void *InsertPos = 0;
1163 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1164 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 // If the pointee type isn't canonical, this won't be a canonical type either,
1167 // so fill in the canonical type field.
1168 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001169 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001170 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 // Get the new insert position for the node we care about.
1173 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001174 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 }
John McCall6b304a02009-09-24 23:30:46 +00001176 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 Types.push_back(New);
1178 ComplexTypes.InsertNode(New, InsertPos);
1179 return QualType(New, 0);
1180}
1181
Reid Spencer5f016e22007-07-11 17:01:13 +00001182/// getPointerType - Return the uniqued reference to the type for a pointer to
1183/// the specified type.
1184QualType ASTContext::getPointerType(QualType T) {
1185 // Unique pointers, to guarantee there is only one pointer of a particular
1186 // structure.
1187 llvm::FoldingSetNodeID ID;
1188 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 void *InsertPos = 0;
1191 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1192 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 // If the pointee type isn't canonical, this won't be a canonical type either,
1195 // so fill in the canonical type field.
1196 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001197 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001198 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 // Get the new insert position for the node we care about.
1201 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001202 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 }
John McCall6b304a02009-09-24 23:30:46 +00001204 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 Types.push_back(New);
1206 PointerTypes.InsertNode(New, InsertPos);
1207 return QualType(New, 0);
1208}
1209
Mike Stump1eb44332009-09-09 15:08:12 +00001210/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001211/// a pointer to the specified block.
1212QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001213 assert(T->isFunctionType() && "block of function types only");
1214 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001215 // structure.
1216 llvm::FoldingSetNodeID ID;
1217 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Steve Naroff5618bd42008-08-27 16:04:49 +00001219 void *InsertPos = 0;
1220 if (BlockPointerType *PT =
1221 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1222 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001223
1224 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001225 // type either so fill in the canonical type field.
1226 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001227 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001228 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Steve Naroff5618bd42008-08-27 16:04:49 +00001230 // Get the new insert position for the node we care about.
1231 BlockPointerType *NewIP =
1232 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001233 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001234 }
John McCall6b304a02009-09-24 23:30:46 +00001235 BlockPointerType *New
1236 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001237 Types.push_back(New);
1238 BlockPointerTypes.InsertNode(New, InsertPos);
1239 return QualType(New, 0);
1240}
1241
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001242/// getLValueReferenceType - Return the uniqued reference to the type for an
1243/// lvalue reference to the specified type.
John McCall54e14c42009-10-22 22:37:11 +00001244QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 // Unique pointers, to guarantee there is only one pointer of a particular
1246 // structure.
1247 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001248 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001249
1250 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001251 if (LValueReferenceType *RT =
1252 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001254
John McCall54e14c42009-10-22 22:37:11 +00001255 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1256
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 // If the referencee type isn't canonical, this won't be a canonical type
1258 // either, so fill in the canonical type field.
1259 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001260 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1261 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1262 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001263
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001265 LValueReferenceType *NewIP =
1266 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001267 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 }
1269
John McCall6b304a02009-09-24 23:30:46 +00001270 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001271 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1272 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001274 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001275
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001276 return QualType(New, 0);
1277}
1278
1279/// getRValueReferenceType - Return the uniqued reference to the type for an
1280/// rvalue reference to the specified type.
1281QualType ASTContext::getRValueReferenceType(QualType T) {
1282 // Unique pointers, to guarantee there is only one pointer of a particular
1283 // structure.
1284 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001285 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001286
1287 void *InsertPos = 0;
1288 if (RValueReferenceType *RT =
1289 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1290 return QualType(RT, 0);
1291
John McCall54e14c42009-10-22 22:37:11 +00001292 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1293
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001294 // If the referencee type isn't canonical, this won't be a canonical type
1295 // either, so fill in the canonical type field.
1296 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001297 if (InnerRef || !T.isCanonical()) {
1298 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1299 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001300
1301 // Get the new insert position for the node we care about.
1302 RValueReferenceType *NewIP =
1303 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1304 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1305 }
1306
John McCall6b304a02009-09-24 23:30:46 +00001307 RValueReferenceType *New
1308 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001309 Types.push_back(New);
1310 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 return QualType(New, 0);
1312}
1313
Sebastian Redlf30208a2009-01-24 21:16:55 +00001314/// getMemberPointerType - Return the uniqued reference to the type for a
1315/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001316QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001317 // Unique pointers, to guarantee there is only one pointer of a particular
1318 // structure.
1319 llvm::FoldingSetNodeID ID;
1320 MemberPointerType::Profile(ID, T, Cls);
1321
1322 void *InsertPos = 0;
1323 if (MemberPointerType *PT =
1324 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1325 return QualType(PT, 0);
1326
1327 // If the pointee or class type isn't canonical, this won't be a canonical
1328 // type either, so fill in the canonical type field.
1329 QualType Canonical;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001330 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001331 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1332
1333 // Get the new insert position for the node we care about.
1334 MemberPointerType *NewIP =
1335 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1336 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1337 }
John McCall6b304a02009-09-24 23:30:46 +00001338 MemberPointerType *New
1339 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001340 Types.push_back(New);
1341 MemberPointerTypes.InsertNode(New, InsertPos);
1342 return QualType(New, 0);
1343}
1344
Mike Stump1eb44332009-09-09 15:08:12 +00001345/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001346/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001347QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001348 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001349 ArrayType::ArraySizeModifier ASM,
1350 unsigned EltTypeQuals) {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001351 assert((EltTy->isDependentType() ||
1352 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedman587cbdf2009-05-29 20:17:55 +00001353 "Constant array of VLAs is illegal!");
1354
Chris Lattner38aeec72009-05-13 04:12:56 +00001355 // Convert the array size into a canonical width matching the pointer size for
1356 // the target.
1357 llvm::APInt ArySize(ArySizeIn);
1358 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001361 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001364 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001365 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001366 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Reid Spencer5f016e22007-07-11 17:01:13 +00001368 // If the element type isn't canonical, this won't be a canonical type either,
1369 // so fill in the canonical type field.
1370 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001371 if (!EltTy.isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001372 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001373 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001375 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001376 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001377 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 }
Mike Stump1eb44332009-09-09 15:08:12 +00001379
John McCall6b304a02009-09-24 23:30:46 +00001380 ConstantArrayType *New = new(*this,TypeAlignment)
1381 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001382 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 Types.push_back(New);
1384 return QualType(New, 0);
1385}
1386
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001387/// getVariableArrayType - Returns a non-unique reference to the type for a
1388/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001389QualType ASTContext::getVariableArrayType(QualType EltTy,
1390 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001391 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001392 unsigned EltTypeQuals,
1393 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001394 // Since we don't unique expressions, it isn't possible to unique VLA's
1395 // that have an expression provided for their size.
1396
John McCall6b304a02009-09-24 23:30:46 +00001397 VariableArrayType *New = new(*this, TypeAlignment)
1398 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001399
1400 VariableArrayTypes.push_back(New);
1401 Types.push_back(New);
1402 return QualType(New, 0);
1403}
1404
Douglas Gregor898574e2008-12-05 23:32:09 +00001405/// getDependentSizedArrayType - Returns a non-unique reference to
1406/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001407/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001408QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1409 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001410 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001411 unsigned EltTypeQuals,
1412 SourceRange Brackets) {
Douglas Gregorcb78d882009-11-19 18:03:26 +00001413 assert((!NumElts || NumElts->isTypeDependent() ||
1414 NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001415 "Size must be type- or value-dependent!");
1416
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001417 void *InsertPos = 0;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001418 DependentSizedArrayType *Canon = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00001419 llvm::FoldingSetNodeID ID;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001420
1421 if (NumElts) {
1422 // Dependently-sized array types that do not have a specified
1423 // number of elements will have their sizes deduced from an
1424 // initializer.
Douglas Gregorcb78d882009-11-19 18:03:26 +00001425 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1426 EltTypeQuals, NumElts);
1427
1428 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1429 }
1430
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001431 DependentSizedArrayType *New;
1432 if (Canon) {
1433 // We already have a canonical version of this array type; use it as
1434 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001435 New = new (*this, TypeAlignment)
1436 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1437 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001438 } else {
1439 QualType CanonEltTy = getCanonicalType(EltTy);
1440 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001441 New = new (*this, TypeAlignment)
1442 DependentSizedArrayType(*this, EltTy, QualType(),
1443 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001444
Douglas Gregor789b1f62010-02-04 18:10:26 +00001445 if (NumElts) {
1446 DependentSizedArrayType *CanonCheck
1447 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1448 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1449 (void)CanonCheck;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001450 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001451 }
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001452 } else {
1453 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1454 ASM, EltTypeQuals,
1455 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001456 New = new (*this, TypeAlignment)
1457 DependentSizedArrayType(*this, EltTy, Canon,
1458 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001459 }
1460 }
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregor898574e2008-12-05 23:32:09 +00001462 Types.push_back(New);
1463 return QualType(New, 0);
1464}
1465
Eli Friedmanc5773c42008-02-15 18:16:39 +00001466QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1467 ArrayType::ArraySizeModifier ASM,
1468 unsigned EltTypeQuals) {
1469 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001470 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001471
1472 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001473 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001474 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1475 return QualType(ATP, 0);
1476
1477 // If the element type isn't canonical, this won't be a canonical type
1478 // either, so fill in the canonical type field.
1479 QualType Canonical;
1480
John McCall467b27b2009-10-22 20:10:53 +00001481 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001482 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001483 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001484
1485 // Get the new insert position for the node we care about.
1486 IncompleteArrayType *NewIP =
1487 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001488 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001489 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001490
John McCall6b304a02009-09-24 23:30:46 +00001491 IncompleteArrayType *New = new (*this, TypeAlignment)
1492 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001493
1494 IncompleteArrayTypes.InsertNode(New, InsertPos);
1495 Types.push_back(New);
1496 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001497}
1498
Steve Naroff73322922007-07-18 18:00:27 +00001499/// getVectorType - Return the unique reference to a vector type of
1500/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001501QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1502 bool IsAltiVec, bool IsPixel) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Chris Lattnerf52ab252008-04-06 22:59:24 +00001505 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001506 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 // Check if we've already instantiated a vector of this type.
1509 llvm::FoldingSetNodeID ID;
John Thompson82287d12010-02-05 00:12:22 +00001510 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1511 IsAltiVec, IsPixel);
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 void *InsertPos = 0;
1513 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1514 return QualType(VTP, 0);
1515
1516 // If the element type isn't canonical, this won't be a canonical type either,
1517 // so fill in the canonical type field.
1518 QualType Canonical;
John Thompson82287d12010-02-05 00:12:22 +00001519 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1520 Canonical = getVectorType(getCanonicalType(vecType),
1521 NumElts, false, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 // Get the new insert position for the node we care about.
1524 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001525 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 }
John McCall6b304a02009-09-24 23:30:46 +00001527 VectorType *New = new (*this, TypeAlignment)
John Thompson82287d12010-02-05 00:12:22 +00001528 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 VectorTypes.InsertNode(New, InsertPos);
1530 Types.push_back(New);
1531 return QualType(New, 0);
1532}
1533
Nate Begeman213541a2008-04-18 23:10:10 +00001534/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001535/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001536QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001537 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Chris Lattnerf52ab252008-04-06 22:59:24 +00001539 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001540 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Steve Naroff73322922007-07-18 18:00:27 +00001542 // Check if we've already instantiated a vector of this type.
1543 llvm::FoldingSetNodeID ID;
John Thompson82287d12010-02-05 00:12:22 +00001544 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff73322922007-07-18 18:00:27 +00001545 void *InsertPos = 0;
1546 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1547 return QualType(VTP, 0);
1548
1549 // If the element type isn't canonical, this won't be a canonical type either,
1550 // so fill in the canonical type field.
1551 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001552 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001553 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Steve Naroff73322922007-07-18 18:00:27 +00001555 // Get the new insert position for the node we care about.
1556 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001557 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001558 }
John McCall6b304a02009-09-24 23:30:46 +00001559 ExtVectorType *New = new (*this, TypeAlignment)
1560 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001561 VectorTypes.InsertNode(New, InsertPos);
1562 Types.push_back(New);
1563 return QualType(New, 0);
1564}
1565
Mike Stump1eb44332009-09-09 15:08:12 +00001566QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001567 Expr *SizeExpr,
1568 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001569 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001570 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001571 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001573 void *InsertPos = 0;
1574 DependentSizedExtVectorType *Canon
1575 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1576 DependentSizedExtVectorType *New;
1577 if (Canon) {
1578 // We already have a canonical version of this array type; use it as
1579 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001580 New = new (*this, TypeAlignment)
1581 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1582 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001583 } else {
1584 QualType CanonVecTy = getCanonicalType(vecType);
1585 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001586 New = new (*this, TypeAlignment)
1587 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1588 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001589
1590 DependentSizedExtVectorType *CanonCheck
1591 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1592 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1593 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001594 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1595 } else {
1596 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1597 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001598 New = new (*this, TypeAlignment)
1599 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001600 }
1601 }
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001603 Types.push_back(New);
1604 return QualType(New, 0);
1605}
1606
Douglas Gregor72564e72009-02-26 23:50:07 +00001607/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001608///
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001609QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn,
1610 CallingConv CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 // Unique functions, to guarantee there is only one function of a particular
1612 // structure.
1613 llvm::FoldingSetNodeID ID;
John McCallf82b4e82010-02-04 05:44:44 +00001614 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn, CallConv);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001617 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001618 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Reid Spencer5f016e22007-07-11 17:01:13 +00001621 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001622 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001623 getCanonicalCallConv(CallConv) != CallConv) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001624 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn,
John McCall04a67a62010-02-05 21:31:56 +00001625 getCanonicalCallConv(CallConv));
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001628 FunctionNoProtoType *NewIP =
1629 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001630 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 }
Mike Stump1eb44332009-09-09 15:08:12 +00001632
John McCall6b304a02009-09-24 23:30:46 +00001633 FunctionNoProtoType *New = new (*this, TypeAlignment)
John McCallf82b4e82010-02-04 05:44:44 +00001634 FunctionNoProtoType(ResultTy, Canonical, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001636 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 return QualType(New, 0);
1638}
1639
1640/// getFunctionType - Return a normal function type with a typed argument
1641/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001642QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001643 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001644 unsigned TypeQuals, bool hasExceptionSpec,
1645 bool hasAnyExceptionSpec, unsigned NumExs,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001646 const QualType *ExArray, bool NoReturn,
1647 CallingConv CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 // Unique functions, to guarantee there is only one function of a particular
1649 // structure.
1650 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001651 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001652 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
John McCallf82b4e82010-02-04 05:44:44 +00001653 NumExs, ExArray, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001654
1655 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001656 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001657 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001658 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001659
1660 // Determine whether the type being created is already canonical or not.
John McCall54e14c42009-10-22 22:37:11 +00001661 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001663 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001664 isCanonical = false;
1665
1666 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001667 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001669 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 llvm::SmallVector<QualType, 16> CanonicalArgs;
1671 CanonicalArgs.reserve(NumArgs);
1672 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001673 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001674
Chris Lattnerf52ab252008-04-06 22:59:24 +00001675 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001676 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001677 isVariadic, TypeQuals, false,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001678 false, 0, 0, NoReturn,
John McCall04a67a62010-02-05 21:31:56 +00001679 getCanonicalCallConv(CallConv));
Sebastian Redl465226e2009-05-27 22:11:52 +00001680
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001682 FunctionProtoType *NewIP =
1683 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001684 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001686
Douglas Gregor72564e72009-02-26 23:50:07 +00001687 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001688 // for two variable size arrays (for parameter and exception types) at the
1689 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001690 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001691 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1692 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001693 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001694 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001695 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001696 ExArray, NumExs, Canonical, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001698 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 return QualType(FTP, 0);
1700}
1701
John McCall3cb0ebd2010-03-10 03:28:59 +00001702#ifndef NDEBUG
1703static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1704 if (!isa<CXXRecordDecl>(D)) return false;
1705 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1706 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1707 return true;
1708 if (RD->getDescribedClassTemplate() &&
1709 !isa<ClassTemplateSpecializationDecl>(RD))
1710 return true;
1711 return false;
1712}
1713#endif
1714
1715/// getInjectedClassNameType - Return the unique reference to the
1716/// injected class name type for the specified templated declaration.
1717QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1718 QualType TST) {
1719 assert(NeedsInjectedClassNameType(Decl));
1720 if (Decl->TypeForDecl) {
1721 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1722 } else if (CXXRecordDecl *PrevDecl
1723 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1724 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1725 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1726 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1727 } else {
1728 Decl->TypeForDecl = new (*this, TypeAlignment)
1729 InjectedClassNameType(Decl, TST, TST->getCanonicalTypeInternal());
1730 Types.push_back(Decl->TypeForDecl);
1731 }
1732 return QualType(Decl->TypeForDecl, 0);
1733}
1734
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001735/// getTypeDeclType - Return the unique reference to the type for the
1736/// specified type declaration.
John McCallbecb8d52010-03-10 06:48:02 +00001737QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001738 assert(Decl && "Passed null for Decl param");
John McCallbecb8d52010-03-10 06:48:02 +00001739 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump1eb44332009-09-09 15:08:12 +00001740
John McCall19c85762010-02-16 03:57:14 +00001741 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001742 return getTypedefType(Typedef);
John McCallbecb8d52010-03-10 06:48:02 +00001743
1744 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stump9fdbab32009-07-31 02:02:20 +00001745 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001746 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001747
John McCallbecb8d52010-03-10 06:48:02 +00001748 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1749 "Template type parameter types are always available.");
1750
John McCall19c85762010-02-16 03:57:14 +00001751 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001752 assert(!Record->getPreviousDeclaration() &&
1753 "struct/union has previous declaration");
1754 assert(!NeedsInjectedClassNameType(Record));
1755 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00001756 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCallbecb8d52010-03-10 06:48:02 +00001757 assert(!Enum->getPreviousDeclaration() &&
1758 "enum has previous declaration");
1759 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00001760 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00001761 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1762 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stump9fdbab32009-07-31 02:02:20 +00001763 } else
John McCallbecb8d52010-03-10 06:48:02 +00001764 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001765
John McCallbecb8d52010-03-10 06:48:02 +00001766 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001767 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001768}
1769
Reid Spencer5f016e22007-07-11 17:01:13 +00001770/// getTypedefType - Return the unique reference to the type for the
1771/// specified typename decl.
John McCall19c85762010-02-16 03:57:14 +00001772QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001773 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Chris Lattnerf52ab252008-04-06 22:59:24 +00001775 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001776 Decl->TypeForDecl = new(*this, TypeAlignment)
1777 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001778 Types.push_back(Decl->TypeForDecl);
1779 return QualType(Decl->TypeForDecl, 0);
1780}
1781
John McCall49a832b2009-10-18 09:09:24 +00001782/// \brief Retrieve a substitution-result type.
1783QualType
1784ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1785 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001786 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001787 && "replacement types must always be canonical");
1788
1789 llvm::FoldingSetNodeID ID;
1790 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1791 void *InsertPos = 0;
1792 SubstTemplateTypeParmType *SubstParm
1793 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1794
1795 if (!SubstParm) {
1796 SubstParm = new (*this, TypeAlignment)
1797 SubstTemplateTypeParmType(Parm, Replacement);
1798 Types.push_back(SubstParm);
1799 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1800 }
1801
1802 return QualType(SubstParm, 0);
1803}
1804
Douglas Gregorfab9d672009-02-05 23:33:38 +00001805/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001806/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001807/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001808QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001809 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001810 IdentifierInfo *Name) {
1811 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001812 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001813 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001814 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001815 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1816
1817 if (TypeParm)
1818 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001820 if (Name) {
1821 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001822 TypeParm = new (*this, TypeAlignment)
1823 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001824
1825 TemplateTypeParmType *TypeCheck
1826 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1827 assert(!TypeCheck && "Template type parameter canonical type broken");
1828 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001829 } else
John McCall6b304a02009-09-24 23:30:46 +00001830 TypeParm = new (*this, TypeAlignment)
1831 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001832
1833 Types.push_back(TypeParm);
1834 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1835
1836 return QualType(TypeParm, 0);
1837}
1838
John McCall3cb0ebd2010-03-10 03:28:59 +00001839TypeSourceInfo *
1840ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1841 SourceLocation NameLoc,
1842 const TemplateArgumentListInfo &Args,
1843 QualType CanonType) {
1844 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1845
1846 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1847 TemplateSpecializationTypeLoc TL
1848 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1849 TL.setTemplateNameLoc(NameLoc);
1850 TL.setLAngleLoc(Args.getLAngleLoc());
1851 TL.setRAngleLoc(Args.getRAngleLoc());
1852 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1853 TL.setArgLocInfo(i, Args[i].getLocInfo());
1854 return DI;
1855}
1856
Mike Stump1eb44332009-09-09 15:08:12 +00001857QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001858ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00001859 const TemplateArgumentListInfo &Args,
John McCall833ca992009-10-29 08:12:44 +00001860 QualType Canon) {
John McCalld5532b62009-11-23 01:53:49 +00001861 unsigned NumArgs = Args.size();
1862
John McCall833ca992009-10-29 08:12:44 +00001863 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1864 ArgVec.reserve(NumArgs);
1865 for (unsigned i = 0; i != NumArgs; ++i)
1866 ArgVec.push_back(Args[i].getArgument());
1867
1868 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon);
1869}
1870
1871QualType
1872ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001873 const TemplateArgument *Args,
1874 unsigned NumArgs,
1875 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001876 if (!Canon.isNull())
1877 Canon = getCanonicalType(Canon);
1878 else {
1879 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001880 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1881 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1882 CanonArgs.reserve(NumArgs);
1883 for (unsigned I = 0; I != NumArgs; ++I)
1884 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1885
1886 // Determine whether this canonical template specialization type already
1887 // exists.
1888 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001889 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001890 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001891
1892 void *InsertPos = 0;
1893 TemplateSpecializationType *Spec
1894 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Douglas Gregor1275ae02009-07-28 23:00:59 +00001896 if (!Spec) {
1897 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001898 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001899 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001900 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001901 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001902 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001903 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001904 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001905 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001906 }
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Douglas Gregorb88e8882009-07-30 17:40:51 +00001908 if (Canon.isNull())
1909 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001910 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001911 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001912 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001913
Douglas Gregor1275ae02009-07-28 23:00:59 +00001914 // Allocate the (non-canonical) template specialization type, but don't
1915 // try to unique it: these types typically have location information that
1916 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001917 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001918 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001919 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001920 TemplateSpecializationType *Spec
1921 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001922 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Douglas Gregor55f6b142009-02-09 18:46:07 +00001924 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001925 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001926}
1927
Mike Stump1eb44332009-09-09 15:08:12 +00001928QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001929ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001930 QualType NamedType) {
1931 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001932 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001933
1934 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001935 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001936 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1937 if (T)
1938 return QualType(T, 0);
1939
Douglas Gregor789b1f62010-02-04 18:10:26 +00001940 QualType Canon = NamedType;
1941 if (!Canon.isCanonical()) {
1942 Canon = getCanonicalType(NamedType);
1943 QualifiedNameType *CheckT
1944 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1945 assert(!CheckT && "Qualified name canonical type broken");
1946 (void)CheckT;
1947 }
1948
1949 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001950 Types.push_back(T);
1951 QualifiedNameTypes.InsertNode(T, InsertPos);
1952 return QualType(T, 0);
1953}
1954
Mike Stump1eb44332009-09-09 15:08:12 +00001955QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001956 const IdentifierInfo *Name,
1957 QualType Canon) {
1958 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1959
1960 if (Canon.isNull()) {
1961 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1962 if (CanonNNS != NNS)
1963 Canon = getTypenameType(CanonNNS, Name);
1964 }
1965
1966 llvm::FoldingSetNodeID ID;
1967 TypenameType::Profile(ID, NNS, Name);
1968
1969 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001970 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001971 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1972 if (T)
1973 return QualType(T, 0);
1974
1975 T = new (*this) TypenameType(NNS, Name, Canon);
1976 Types.push_back(T);
1977 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001978 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001979}
1980
Mike Stump1eb44332009-09-09 15:08:12 +00001981QualType
1982ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001983 const TemplateSpecializationType *TemplateId,
1984 QualType Canon) {
1985 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1986
Douglas Gregor789b1f62010-02-04 18:10:26 +00001987 llvm::FoldingSetNodeID ID;
1988 TypenameType::Profile(ID, NNS, TemplateId);
1989
1990 void *InsertPos = 0;
1991 TypenameType *T
1992 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1993 if (T)
1994 return QualType(T, 0);
1995
Douglas Gregor17343172009-04-01 00:28:59 +00001996 if (Canon.isNull()) {
1997 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1998 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1999 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
2000 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00002001 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00002002 assert(CanonTemplateId &&
2003 "Canonical type must also be a template specialization type");
2004 Canon = getTypenameType(CanonNNS, CanonTemplateId);
2005 }
Douglas Gregor789b1f62010-02-04 18:10:26 +00002006
2007 TypenameType *CheckT
2008 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
2009 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregor17343172009-04-01 00:28:59 +00002010 }
2011
Douglas Gregor17343172009-04-01 00:28:59 +00002012 T = new (*this) TypenameType(NNS, TemplateId, Canon);
2013 Types.push_back(T);
2014 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002015 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002016}
2017
John McCall7da24312009-09-05 00:15:47 +00002018QualType
2019ASTContext::getElaboratedType(QualType UnderlyingType,
2020 ElaboratedType::TagKind Tag) {
2021 llvm::FoldingSetNodeID ID;
2022 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00002023
John McCall7da24312009-09-05 00:15:47 +00002024 void *InsertPos = 0;
2025 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2026 if (T)
2027 return QualType(T, 0);
2028
Douglas Gregor789b1f62010-02-04 18:10:26 +00002029 QualType Canon = UnderlyingType;
2030 if (!Canon.isCanonical()) {
2031 Canon = getCanonicalType(Canon);
2032 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2033 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2034 }
John McCall7da24312009-09-05 00:15:47 +00002035
2036 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2037 Types.push_back(T);
2038 ElaboratedTypes.InsertNode(T, InsertPos);
2039 return QualType(T, 0);
2040}
2041
Chris Lattner88cb27a2008-04-07 04:56:42 +00002042/// CmpProtocolNames - Comparison predicate for sorting protocols
2043/// alphabetically.
2044static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2045 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002046 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00002047}
2048
John McCall54e14c42009-10-22 22:37:11 +00002049static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2050 unsigned NumProtocols) {
2051 if (NumProtocols == 0) return true;
2052
2053 for (unsigned i = 1; i != NumProtocols; ++i)
2054 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2055 return false;
2056 return true;
2057}
2058
2059static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00002060 unsigned &NumProtocols) {
2061 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Chris Lattner88cb27a2008-04-07 04:56:42 +00002063 // Sort protocols, keyed by name.
2064 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2065
2066 // Remove duplicates.
2067 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2068 NumProtocols = ProtocolsEnd-Protocols;
2069}
2070
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002071/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2072/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00002073QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00002074 ObjCProtocolDecl **Protocols,
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002075 unsigned NumProtocols,
2076 unsigned Quals) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002077 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00002078 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002079 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002080
2081 void *InsertPos = 0;
2082 if (ObjCObjectPointerType *QT =
2083 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002084 return getQualifiedType(QualType(QT, 0), Qs);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002085
John McCall54e14c42009-10-22 22:37:11 +00002086 // Sort the protocol list alphabetically to canonicalize it.
2087 QualType Canonical;
2088 if (!InterfaceT.isCanonical() ||
2089 !areSortedAndUniqued(Protocols, NumProtocols)) {
2090 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
2091 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2092 unsigned UniqueCount = NumProtocols;
2093
2094 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2095 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2096
2097 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2098 &Sorted[0], UniqueCount);
2099 } else {
2100 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2101 Protocols, NumProtocols);
2102 }
2103
2104 // Regenerate InsertPos.
2105 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2106 }
2107
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002108 // No match.
2109 unsigned Size = sizeof(ObjCObjectPointerType)
2110 + NumProtocols * sizeof(ObjCProtocolDecl *);
2111 void *Mem = Allocate(Size, TypeAlignment);
2112 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2113 InterfaceT,
2114 Protocols,
2115 NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00002116
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002117 Types.push_back(QType);
2118 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniana4228642010-03-05 22:42:55 +00002119 return getQualifiedType(QualType(QType, 0), Qs);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002120}
Chris Lattner88cb27a2008-04-07 04:56:42 +00002121
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002122/// getObjCInterfaceType - Return the unique reference to the type for the
2123/// specified ObjC interface decl. The list of protocols is optional.
2124QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002125 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002126 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002127 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002129 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002130 if (ObjCInterfaceType *QT =
2131 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002132 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002133
John McCall54e14c42009-10-22 22:37:11 +00002134 // Sort the protocol list alphabetically to canonicalize it.
2135 QualType Canonical;
2136 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2137 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2138 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2139
2140 unsigned UniqueCount = NumProtocols;
2141 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2142
2143 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2144
2145 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2146 }
2147
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002148 unsigned Size = sizeof(ObjCInterfaceType)
2149 + NumProtocols * sizeof(ObjCProtocolDecl *);
2150 void *Mem = Allocate(Size, TypeAlignment);
2151 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2152 const_cast<ObjCInterfaceDecl*>(Decl),
2153 Protocols,
2154 NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002155
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002156 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002157 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002158 return QualType(QType, 0);
2159}
2160
Douglas Gregor72564e72009-02-26 23:50:07 +00002161/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2162/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002163/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002164/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002165/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002166QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002167 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002168 if (tofExpr->isTypeDependent()) {
2169 llvm::FoldingSetNodeID ID;
2170 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002171
Douglas Gregorb1975722009-07-30 23:18:24 +00002172 void *InsertPos = 0;
2173 DependentTypeOfExprType *Canon
2174 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2175 if (Canon) {
2176 // We already have a "canonical" version of an identical, dependent
2177 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002178 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002179 QualType((TypeOfExprType*)Canon, 0));
2180 }
2181 else {
2182 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002183 Canon
2184 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002185 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2186 toe = Canon;
2187 }
2188 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002189 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002190 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002191 }
Steve Naroff9752f252007-08-01 18:02:17 +00002192 Types.push_back(toe);
2193 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002194}
2195
Steve Naroff9752f252007-08-01 18:02:17 +00002196/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2197/// TypeOfType AST's. The only motivation to unique these nodes would be
2198/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002199/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002200/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002201QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002202 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002203 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002204 Types.push_back(tot);
2205 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002206}
2207
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002208/// getDecltypeForExpr - Given an expr, will return the decltype for that
2209/// expression, according to the rules in C++0x [dcl.type.simple]p4
2210static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002211 if (e->isTypeDependent())
2212 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002213
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002214 // If e is an id expression or a class member access, decltype(e) is defined
2215 // as the type of the entity named by e.
2216 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2217 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2218 return VD->getType();
2219 }
2220 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2221 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2222 return FD->getType();
2223 }
2224 // If e is a function call or an invocation of an overloaded operator,
2225 // (parentheses around e are ignored), decltype(e) is defined as the
2226 // return type of that function.
2227 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2228 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002230 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002231
2232 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002233 // defined as T&, otherwise decltype(e) is defined as T.
2234 if (e->isLvalue(Context) == Expr::LV_Valid)
2235 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002236
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002237 return T;
2238}
2239
Anders Carlsson395b4752009-06-24 19:06:50 +00002240/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2241/// DecltypeType AST's. The only motivation to unique these nodes would be
2242/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002243/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002244/// on canonical type's (which are always unique).
2245QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002246 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002247 if (e->isTypeDependent()) {
2248 llvm::FoldingSetNodeID ID;
2249 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002251 void *InsertPos = 0;
2252 DependentDecltypeType *Canon
2253 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2254 if (Canon) {
2255 // We already have a "canonical" version of an equivalent, dependent
2256 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002257 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002258 QualType((DecltypeType*)Canon, 0));
2259 }
2260 else {
2261 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002262 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002263 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2264 dt = Canon;
2265 }
2266 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002267 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002268 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002269 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002270 Types.push_back(dt);
2271 return QualType(dt, 0);
2272}
2273
Reid Spencer5f016e22007-07-11 17:01:13 +00002274/// getTagDeclType - Return the unique reference to the type for the
2275/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002276QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002277 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002278 // FIXME: What is the design on getTagDeclType when it requires casting
2279 // away const? mutable?
2280 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002281}
2282
Mike Stump1eb44332009-09-09 15:08:12 +00002283/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2284/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2285/// needs to agree with the definition in <stddef.h>.
Anders Carlssona3ccda52009-12-12 00:26:23 +00002286CanQualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002287 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002288}
2289
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002290/// getSignedWCharType - Return the type of "signed wchar_t".
2291/// Used when in C++, as a GCC extension.
2292QualType ASTContext::getSignedWCharType() const {
2293 // FIXME: derive from "Target" ?
2294 return WCharTy;
2295}
2296
2297/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2298/// Used when in C++, as a GCC extension.
2299QualType ASTContext::getUnsignedWCharType() const {
2300 // FIXME: derive from "Target" ?
2301 return UnsignedIntTy;
2302}
2303
Chris Lattner8b9023b2007-07-13 03:05:23 +00002304/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2305/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2306QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002307 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002308}
2309
Chris Lattnere6327742008-04-02 05:18:44 +00002310//===----------------------------------------------------------------------===//
2311// Type Operators
2312//===----------------------------------------------------------------------===//
2313
John McCall54e14c42009-10-22 22:37:11 +00002314CanQualType ASTContext::getCanonicalParamType(QualType T) {
2315 // Push qualifiers into arrays, and then discard any remaining
2316 // qualifiers.
2317 T = getCanonicalType(T);
2318 const Type *Ty = T.getTypePtr();
2319
2320 QualType Result;
2321 if (isa<ArrayType>(Ty)) {
2322 Result = getArrayDecayedType(QualType(Ty,0));
2323 } else if (isa<FunctionType>(Ty)) {
2324 Result = getPointerType(QualType(Ty, 0));
2325 } else {
2326 Result = QualType(Ty, 0);
2327 }
2328
2329 return CanQualType::CreateUnsafe(Result);
2330}
2331
Chris Lattner77c96472008-04-06 22:41:35 +00002332/// getCanonicalType - Return the canonical (structural) type corresponding to
2333/// the specified potentially non-canonical type. The non-canonical version
2334/// of a type may have many "decorated" versions of types. Decorators can
2335/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2336/// to be free of any of these, allowing two canonical types to be compared
2337/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002338CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002339 QualifierCollector Quals;
2340 const Type *Ptr = Quals.strip(T);
2341 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002342
John McCall0953e762009-09-24 19:53:00 +00002343 // The canonical internal type will be the canonical type *except*
2344 // that we push type qualifiers down through array types.
2345
2346 // If there are no new qualifiers to push down, stop here.
2347 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002348 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002349
John McCall0953e762009-09-24 19:53:00 +00002350 // If the type qualifiers are on an array type, get the canonical
2351 // type of the array with the qualifiers applied to the element
2352 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002353 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2354 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002355 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002357 // Get the canonical version of the element with the extra qualifiers on it.
2358 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002359 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002360 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002362 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002363 return CanQualType::CreateUnsafe(
2364 getConstantArrayType(NewEltTy, CAT->getSize(),
2365 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002366 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002367 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002368 return CanQualType::CreateUnsafe(
2369 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002370 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002371
Douglas Gregor898574e2008-12-05 23:32:09 +00002372 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002373 return CanQualType::CreateUnsafe(
2374 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002375 DSAT->getSizeExpr() ?
2376 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002377 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002378 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor87a924e2009-10-30 22:56:57 +00002379 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor898574e2008-12-05 23:32:09 +00002380
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002381 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002382 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002383 VAT->getSizeExpr() ?
2384 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002385 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002386 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002387 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002388}
2389
Chandler Carruth28e318c2009-12-29 07:16:59 +00002390QualType ASTContext::getUnqualifiedArrayType(QualType T,
2391 Qualifiers &Quals) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002392 Quals = T.getQualifiers();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002393 if (!isa<ArrayType>(T)) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002394 return T.getUnqualifiedType();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002395 }
2396
Chandler Carruth28e318c2009-12-29 07:16:59 +00002397 const ArrayType *AT = cast<ArrayType>(T);
2398 QualType Elt = AT->getElementType();
Zhongxing Xuc1ae0a82010-01-05 08:15:06 +00002399 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002400 if (Elt == UnqualElt)
2401 return T;
2402
2403 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2404 return getConstantArrayType(UnqualElt, CAT->getSize(),
2405 CAT->getSizeModifier(), 0);
2406 }
2407
2408 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2409 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2410 }
2411
2412 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2413 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2414 DSAT->getSizeModifier(), 0,
2415 SourceRange());
2416}
2417
John McCall80ad16f2009-11-24 18:42:40 +00002418DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2419 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2420 return TD->getDeclName();
2421
2422 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2423 if (DTN->isIdentifier()) {
2424 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2425 } else {
2426 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2427 }
2428 }
2429
John McCall0bd6feb2009-12-02 08:04:21 +00002430 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2431 assert(Storage);
2432 return (*Storage->begin())->getDeclName();
John McCall80ad16f2009-11-24 18:42:40 +00002433}
2434
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002435TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2436 // If this template name refers to a template, the canonical
2437 // template name merely stores the template itself.
2438 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002439 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002440
John McCall0bd6feb2009-12-02 08:04:21 +00002441 assert(!Name.getAsOverloadedTemplate());
Mike Stump1eb44332009-09-09 15:08:12 +00002442
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002443 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2444 assert(DTN && "Non-dependent template names must refer to template decls.");
2445 return DTN->CanonicalTemplateName;
2446}
2447
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002448bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2449 X = getCanonicalTemplateName(X);
2450 Y = getCanonicalTemplateName(Y);
2451 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2452}
2453
Mike Stump1eb44332009-09-09 15:08:12 +00002454TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002455ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2456 switch (Arg.getKind()) {
2457 case TemplateArgument::Null:
2458 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Douglas Gregor1275ae02009-07-28 23:00:59 +00002460 case TemplateArgument::Expression:
Douglas Gregor1275ae02009-07-28 23:00:59 +00002461 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002462
Douglas Gregor1275ae02009-07-28 23:00:59 +00002463 case TemplateArgument::Declaration:
John McCall833ca992009-10-29 08:12:44 +00002464 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002465
Douglas Gregor788cd062009-11-11 01:00:40 +00002466 case TemplateArgument::Template:
2467 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2468
Douglas Gregor1275ae02009-07-28 23:00:59 +00002469 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002470 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002471 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002472
Douglas Gregor1275ae02009-07-28 23:00:59 +00002473 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00002474 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002475
Douglas Gregor1275ae02009-07-28 23:00:59 +00002476 case TemplateArgument::Pack: {
2477 // FIXME: Allocate in ASTContext
2478 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2479 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002480 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002481 AEnd = Arg.pack_end();
2482 A != AEnd; (void)++A, ++Idx)
2483 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Douglas Gregor1275ae02009-07-28 23:00:59 +00002485 TemplateArgument Result;
2486 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2487 return Result;
2488 }
2489 }
2490
2491 // Silence GCC warning
2492 assert(false && "Unhandled template argument kind");
2493 return TemplateArgument();
2494}
2495
Douglas Gregord57959a2009-03-27 23:10:48 +00002496NestedNameSpecifier *
2497ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002498 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002499 return 0;
2500
2501 switch (NNS->getKind()) {
2502 case NestedNameSpecifier::Identifier:
2503 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002504 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002505 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2506 NNS->getAsIdentifier());
2507
2508 case NestedNameSpecifier::Namespace:
2509 // A namespace is canonical; build a nested-name-specifier with
2510 // this namespace and no prefix.
2511 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2512
2513 case NestedNameSpecifier::TypeSpec:
2514 case NestedNameSpecifier::TypeSpecWithTemplate: {
2515 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002516 return NestedNameSpecifier::Create(*this, 0,
2517 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002518 T.getTypePtr());
2519 }
2520
2521 case NestedNameSpecifier::Global:
2522 // The global specifier is canonical and unique.
2523 return NNS;
2524 }
2525
2526 // Required to silence a GCC warning
2527 return 0;
2528}
2529
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002530
2531const ArrayType *ASTContext::getAsArrayType(QualType T) {
2532 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00002533 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002534 // Handle the common positive case fast.
2535 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2536 return AT;
2537 }
Mike Stump1eb44332009-09-09 15:08:12 +00002538
John McCall0953e762009-09-24 19:53:00 +00002539 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002540 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002541 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002542 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002543
John McCall0953e762009-09-24 19:53:00 +00002544 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002545 // implements C99 6.7.3p8: "If the specification of an array type includes
2546 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002547
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002548 // If we get here, we either have type qualifiers on the type, or we have
2549 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002550 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002551
John McCall0953e762009-09-24 19:53:00 +00002552 QualifierCollector Qs;
2553 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002554
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002555 // If we have a simple case, just return now.
2556 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002557 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002558 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002560 // Otherwise, we have an array and we have qualifiers on it. Push the
2561 // qualifiers into the array element type and return a new array type.
2562 // Get the canonical version of the element with the extra qualifiers on it.
2563 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002564 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002565
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002566 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2567 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2568 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002569 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002570 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2571 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2572 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002573 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002574
Mike Stump1eb44332009-09-09 15:08:12 +00002575 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002576 = dyn_cast<DependentSizedArrayType>(ATy))
2577 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002578 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002579 DSAT->getSizeExpr() ?
2580 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002581 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002582 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002583 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002585 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002586 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002587 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002588 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002589 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002590 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002591 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002592}
2593
2594
Chris Lattnere6327742008-04-02 05:18:44 +00002595/// getArrayDecayedType - Return the properly qualified result of decaying the
2596/// specified array type to a pointer. This operation is non-trivial when
2597/// handling typedefs etc. The canonical type of "T" must be an array type,
2598/// this returns a pointer to a properly qualified element of the array.
2599///
2600/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2601QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002602 // Get the element type with 'getAsArrayType' so that we don't lose any
2603 // typedefs in the element type of the array. This also handles propagation
2604 // of type qualifiers from the array type into the element type if present
2605 // (C99 6.7.3p8).
2606 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2607 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002609 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002610
2611 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002612 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002613}
2614
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002615QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002616 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002617 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002618 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002619 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2620 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002621 } else {
John McCall0953e762009-09-24 19:53:00 +00002622 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002623 }
2624 }
2625}
2626
Anders Carlssonfbbce492009-09-25 01:23:32 +00002627QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2628 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002629
Anders Carlssonfbbce492009-09-25 01:23:32 +00002630 if (const ArrayType *AT = getAsArrayType(ElemTy))
2631 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002632
Anders Carlsson6183a992008-12-21 03:44:36 +00002633 return ElemTy;
2634}
2635
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002636/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002637uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002638ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2639 uint64_t ElementCount = 1;
2640 do {
2641 ElementCount *= CA->getSize().getZExtValue();
2642 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2643 } while (CA);
2644 return ElementCount;
2645}
2646
Reid Spencer5f016e22007-07-11 17:01:13 +00002647/// getFloatingRank - Return a relative rank for floating point types.
2648/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002649static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002650 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002651 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002652
John McCall183700f2009-09-21 23:43:11 +00002653 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2654 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002655 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002656 case BuiltinType::Float: return FloatRank;
2657 case BuiltinType::Double: return DoubleRank;
2658 case BuiltinType::LongDouble: return LongDoubleRank;
2659 }
2660}
2661
Mike Stump1eb44332009-09-09 15:08:12 +00002662/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2663/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002664/// 'typeDomain' is a real floating point or complex type.
2665/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002666QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2667 QualType Domain) const {
2668 FloatingRank EltRank = getFloatingRank(Size);
2669 if (Domain->isComplexType()) {
2670 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002671 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002672 case FloatRank: return FloatComplexTy;
2673 case DoubleRank: return DoubleComplexTy;
2674 case LongDoubleRank: return LongDoubleComplexTy;
2675 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002676 }
Chris Lattner1361b112008-04-06 23:58:54 +00002677
2678 assert(Domain->isRealFloatingType() && "Unknown domain!");
2679 switch (EltRank) {
2680 default: assert(0 && "getFloatingRank(): illegal value for rank");
2681 case FloatRank: return FloatTy;
2682 case DoubleRank: return DoubleTy;
2683 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002684 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002685}
2686
Chris Lattner7cfeb082008-04-06 23:55:33 +00002687/// getFloatingTypeOrder - Compare the rank of the two specified floating
2688/// point types, ignoring the domain of the type (i.e. 'double' ==
2689/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002690/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002691int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2692 FloatingRank LHSR = getFloatingRank(LHS);
2693 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002694
Chris Lattnera75cea32008-04-06 23:38:49 +00002695 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002696 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002697 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002698 return 1;
2699 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002700}
2701
Chris Lattnerf52ab252008-04-06 22:59:24 +00002702/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2703/// routine will assert if passed a built-in type that isn't an integer or enum,
2704/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002705unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002706 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002707 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00002708 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00002709
Eli Friedmana3426752009-07-05 23:44:27 +00002710 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2711 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2712
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002713 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2714 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2715
2716 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2717 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2718
Chris Lattnerf52ab252008-04-06 22:59:24 +00002719 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002720 default: assert(0 && "getIntegerRank(): not a built-in integer");
2721 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002722 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002723 case BuiltinType::Char_S:
2724 case BuiltinType::Char_U:
2725 case BuiltinType::SChar:
2726 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002727 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002728 case BuiltinType::Short:
2729 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002730 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002731 case BuiltinType::Int:
2732 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002733 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002734 case BuiltinType::Long:
2735 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002736 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002737 case BuiltinType::LongLong:
2738 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002739 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002740 case BuiltinType::Int128:
2741 case BuiltinType::UInt128:
2742 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002743 }
2744}
2745
Eli Friedman04e83572009-08-20 04:21:42 +00002746/// \brief Whether this is a promotable bitfield reference according
2747/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2748///
2749/// \returns the type this bit-field will promote to, or NULL if no
2750/// promotion occurs.
2751QualType ASTContext::isPromotableBitField(Expr *E) {
2752 FieldDecl *Field = E->getBitField();
2753 if (!Field)
2754 return QualType();
2755
2756 QualType FT = Field->getType();
2757
2758 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2759 uint64_t BitWidth = BitWidthAP.getZExtValue();
2760 uint64_t IntSize = getTypeSize(IntTy);
2761 // GCC extension compatibility: if the bit-field size is less than or equal
2762 // to the size of int, it gets promoted no matter what its type is.
2763 // For instance, unsigned long bf : 4 gets promoted to signed int.
2764 if (BitWidth < IntSize)
2765 return IntTy;
2766
2767 if (BitWidth == IntSize)
2768 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2769
2770 // Types bigger than int are not subject to promotions, and therefore act
2771 // like the base type.
2772 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2773 // is ridiculous.
2774 return QualType();
2775}
2776
Eli Friedmana95d7572009-08-19 07:44:53 +00002777/// getPromotedIntegerType - Returns the type that Promotable will
2778/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2779/// integer type.
2780QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2781 assert(!Promotable.isNull());
2782 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00002783 if (const EnumType *ET = Promotable->getAs<EnumType>())
2784 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00002785 if (Promotable->isSignedIntegerType())
2786 return IntTy;
2787 uint64_t PromotableSize = getTypeSize(Promotable);
2788 uint64_t IntSize = getTypeSize(IntTy);
2789 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2790 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2791}
2792
Mike Stump1eb44332009-09-09 15:08:12 +00002793/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002794/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002795/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002796int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002797 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2798 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002799 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002800
Chris Lattnerf52ab252008-04-06 22:59:24 +00002801 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2802 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002803
Chris Lattner7cfeb082008-04-06 23:55:33 +00002804 unsigned LHSRank = getIntegerRank(LHSC);
2805 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Chris Lattner7cfeb082008-04-06 23:55:33 +00002807 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2808 if (LHSRank == RHSRank) return 0;
2809 return LHSRank > RHSRank ? 1 : -1;
2810 }
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Chris Lattner7cfeb082008-04-06 23:55:33 +00002812 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2813 if (LHSUnsigned) {
2814 // If the unsigned [LHS] type is larger, return it.
2815 if (LHSRank >= RHSRank)
2816 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002817
Chris Lattner7cfeb082008-04-06 23:55:33 +00002818 // If the signed type can represent all values of the unsigned type, it
2819 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002820 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002821 return -1;
2822 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002823
Chris Lattner7cfeb082008-04-06 23:55:33 +00002824 // If the unsigned [RHS] type is larger, return it.
2825 if (RHSRank >= LHSRank)
2826 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Chris Lattner7cfeb082008-04-06 23:55:33 +00002828 // If the signed type can represent all values of the unsigned type, it
2829 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002830 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002831 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002832}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002833
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002834static RecordDecl *
2835CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2836 SourceLocation L, IdentifierInfo *Id) {
2837 if (Ctx.getLangOptions().CPlusPlus)
2838 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2839 else
2840 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2841}
2842
Mike Stump1eb44332009-09-09 15:08:12 +00002843// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002844QualType ASTContext::getCFConstantStringType() {
2845 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002846 CFConstantStringTypeDecl =
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002847 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2848 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00002849 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002850
Anders Carlssonf06273f2007-11-19 00:25:30 +00002851 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Anders Carlsson71993dd2007-08-17 05:31:46 +00002853 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002854 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002855 // int flags;
2856 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002857 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002858 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002859 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002860 FieldTypes[3] = LongTy;
2861
Anders Carlsson71993dd2007-08-17 05:31:46 +00002862 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002863 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002864 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002865 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00002866 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002867 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002868 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002869 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002870 }
2871
Douglas Gregor838db382010-02-11 01:19:42 +00002872 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00002873 }
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Anders Carlsson71993dd2007-08-17 05:31:46 +00002875 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002876}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002877
Douglas Gregor319ac892009-04-23 22:29:11 +00002878void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002879 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002880 assert(Rec && "Invalid CFConstantStringType");
2881 CFConstantStringTypeDecl = Rec->getDecl();
2882}
2883
Mike Stump1eb44332009-09-09 15:08:12 +00002884QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002885 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002886 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002887 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2888 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00002889 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002890
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002891 QualType FieldTypes[] = {
2892 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002893 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002894 getPointerType(UnsignedLongTy),
2895 getConstantArrayType(UnsignedLongTy,
2896 llvm::APInt(32, 5), ArrayType::Normal, 0)
2897 };
Mike Stump1eb44332009-09-09 15:08:12 +00002898
Douglas Gregor44b43212008-12-11 16:49:14 +00002899 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002900 FieldDecl *Field = FieldDecl::Create(*this,
2901 ObjCFastEnumerationStateTypeDecl,
2902 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00002903 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002904 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002905 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002906 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002907 }
Mike Stump1eb44332009-09-09 15:08:12 +00002908
Douglas Gregor838db382010-02-11 01:19:42 +00002909 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002910 }
Mike Stump1eb44332009-09-09 15:08:12 +00002911
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002912 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2913}
2914
Mike Stumpadaaad32009-10-20 02:12:22 +00002915QualType ASTContext::getBlockDescriptorType() {
2916 if (BlockDescriptorType)
2917 return getTagDeclType(BlockDescriptorType);
2918
2919 RecordDecl *T;
2920 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002921 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2922 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00002923 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00002924
2925 QualType FieldTypes[] = {
2926 UnsignedLongTy,
2927 UnsignedLongTy,
2928 };
2929
2930 const char *FieldNames[] = {
2931 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00002932 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00002933 };
2934
2935 for (size_t i = 0; i < 2; ++i) {
2936 FieldDecl *Field = FieldDecl::Create(*this,
2937 T,
2938 SourceLocation(),
2939 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00002940 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00002941 /*BitWidth=*/0,
2942 /*Mutable=*/false);
2943 T->addDecl(Field);
2944 }
2945
Douglas Gregor838db382010-02-11 01:19:42 +00002946 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00002947
2948 BlockDescriptorType = T;
2949
2950 return getTagDeclType(BlockDescriptorType);
2951}
2952
2953void ASTContext::setBlockDescriptorType(QualType T) {
2954 const RecordType *Rec = T->getAs<RecordType>();
2955 assert(Rec && "Invalid BlockDescriptorType");
2956 BlockDescriptorType = Rec->getDecl();
2957}
2958
Mike Stump083c25e2009-10-22 00:49:09 +00002959QualType ASTContext::getBlockDescriptorExtendedType() {
2960 if (BlockDescriptorExtendedType)
2961 return getTagDeclType(BlockDescriptorExtendedType);
2962
2963 RecordDecl *T;
2964 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002965 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2966 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00002967 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00002968
2969 QualType FieldTypes[] = {
2970 UnsignedLongTy,
2971 UnsignedLongTy,
2972 getPointerType(VoidPtrTy),
2973 getPointerType(VoidPtrTy)
2974 };
2975
2976 const char *FieldNames[] = {
2977 "reserved",
2978 "Size",
2979 "CopyFuncPtr",
2980 "DestroyFuncPtr"
2981 };
2982
2983 for (size_t i = 0; i < 4; ++i) {
2984 FieldDecl *Field = FieldDecl::Create(*this,
2985 T,
2986 SourceLocation(),
2987 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00002988 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00002989 /*BitWidth=*/0,
2990 /*Mutable=*/false);
2991 T->addDecl(Field);
2992 }
2993
Douglas Gregor838db382010-02-11 01:19:42 +00002994 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00002995
2996 BlockDescriptorExtendedType = T;
2997
2998 return getTagDeclType(BlockDescriptorExtendedType);
2999}
3000
3001void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3002 const RecordType *Rec = T->getAs<RecordType>();
3003 assert(Rec && "Invalid BlockDescriptorType");
3004 BlockDescriptorExtendedType = Rec->getDecl();
3005}
3006
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003007bool ASTContext::BlockRequiresCopying(QualType Ty) {
3008 if (Ty->isBlockPointerType())
3009 return true;
3010 if (isObjCNSObjectType(Ty))
3011 return true;
3012 if (Ty->isObjCObjectPointerType())
3013 return true;
3014 return false;
3015}
3016
3017QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3018 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003019 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003020 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003021 // unsigned int __flags;
3022 // unsigned int __size;
Mike Stump38e16272009-10-21 22:01:24 +00003023 // void *__copy_helper; // as needed
3024 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003025 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003026 // } *
3027
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003028 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3029
3030 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00003031 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003032 llvm::SmallString<36> Name;
3033 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3034 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003035 RecordDecl *T;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003036 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3037 &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003038 T->startDefinition();
3039 QualType Int32Ty = IntTy;
3040 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3041 QualType FieldTypes[] = {
3042 getPointerType(VoidPtrTy),
3043 getPointerType(getTagDeclType(T)),
3044 Int32Ty,
3045 Int32Ty,
3046 getPointerType(VoidPtrTy),
3047 getPointerType(VoidPtrTy),
3048 Ty
3049 };
3050
3051 const char *FieldNames[] = {
3052 "__isa",
3053 "__forwarding",
3054 "__flags",
3055 "__size",
3056 "__copy_helper",
3057 "__destroy_helper",
3058 DeclName,
3059 };
3060
3061 for (size_t i = 0; i < 7; ++i) {
3062 if (!HasCopyAndDispose && i >=4 && i <= 5)
3063 continue;
3064 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3065 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003066 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003067 /*BitWidth=*/0, /*Mutable=*/false);
3068 T->addDecl(Field);
3069 }
3070
Douglas Gregor838db382010-02-11 01:19:42 +00003071 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003072
3073 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003074}
3075
3076
3077QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00003078 bool BlockHasCopyDispose,
Mike Stumpea26cb52009-10-21 03:49:08 +00003079 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpadaaad32009-10-20 02:12:22 +00003080 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00003081 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003082 llvm::SmallString<36> Name;
3083 llvm::raw_svector_ostream(Name) << "__block_literal_"
3084 << ++UniqueBlockParmTypeID;
Mike Stumpadaaad32009-10-20 02:12:22 +00003085 RecordDecl *T;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003086 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3087 &Idents.get(Name.str()));
John McCall5cfa0112010-02-05 01:33:36 +00003088 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003089 QualType FieldTypes[] = {
3090 getPointerType(VoidPtrTy),
3091 IntTy,
3092 IntTy,
3093 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00003094 (BlockHasCopyDispose ?
3095 getPointerType(getBlockDescriptorExtendedType()) :
3096 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00003097 };
3098
3099 const char *FieldNames[] = {
3100 "__isa",
3101 "__flags",
3102 "__reserved",
3103 "__FuncPtr",
3104 "__descriptor"
3105 };
3106
3107 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00003108 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003109 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003110 FieldTypes[i], /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003111 /*BitWidth=*/0, /*Mutable=*/false);
3112 T->addDecl(Field);
3113 }
3114
3115 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3116 const Expr *E = BlockDeclRefDecls[i];
3117 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3118 clang::IdentifierInfo *Name = 0;
3119 if (BDRE) {
3120 const ValueDecl *D = BDRE->getDecl();
3121 Name = &Idents.get(D->getName());
3122 }
3123 QualType FieldType = E->getType();
3124
3125 if (BDRE && BDRE->isByRef())
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003126 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3127 FieldType);
Mike Stumpea26cb52009-10-21 03:49:08 +00003128
3129 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCalla93c9342009-12-07 02:54:59 +00003130 Name, FieldType, /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003131 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpadaaad32009-10-20 02:12:22 +00003132 T->addDecl(Field);
3133 }
3134
Douglas Gregor838db382010-02-11 01:19:42 +00003135 T->completeDefinition();
Mike Stumpea26cb52009-10-21 03:49:08 +00003136
3137 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00003138}
3139
Douglas Gregor319ac892009-04-23 22:29:11 +00003140void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003141 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003142 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3143 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3144}
3145
Anders Carlssone8c49532007-10-29 06:33:42 +00003146// This returns true if a type has been typedefed to BOOL:
3147// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003148static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003149 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003150 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3151 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003152
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003153 return false;
3154}
3155
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003156/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003157/// purpose.
Ken Dyckaa8741a2010-01-11 19:19:56 +00003158CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck199c3d62010-01-11 17:06:35 +00003159 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003160
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003161 // Make all integer and enum types at least as large as an int
Ken Dyck199c3d62010-01-11 17:06:35 +00003162 if (sz.isPositive() && type->isIntegralType())
3163 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003164 // Treat arrays as pointers, since that's how they're passed in.
3165 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003166 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003167 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003168}
3169
3170static inline
3171std::string charUnitsToString(const CharUnits &CU) {
3172 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003173}
3174
David Chisnall5e530af2009-11-17 19:33:30 +00003175/// getObjCEncodingForBlockDecl - Return the encoded type for this method
3176/// declaration.
3177void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3178 std::string& S) {
3179 const BlockDecl *Decl = Expr->getBlockDecl();
3180 QualType BlockTy =
3181 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3182 // Encode result type.
3183 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3184 // Compute size of all parameters.
3185 // Start with computing size of a pointer in number of bytes.
3186 // FIXME: There might(should) be a better way of doing this computation!
3187 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003188 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3189 CharUnits ParmOffset = PtrSize;
David Chisnall5e530af2009-11-17 19:33:30 +00003190 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3191 E = Decl->param_end(); PI != E; ++PI) {
3192 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003193 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003194 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003195 ParmOffset += sz;
3196 }
3197 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003198 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003199 // Block pointer and offset.
3200 S += "@?0";
3201 ParmOffset = PtrSize;
3202
3203 // Argument types.
3204 ParmOffset = PtrSize;
3205 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3206 Decl->param_end(); PI != E; ++PI) {
3207 ParmVarDecl *PVDecl = *PI;
3208 QualType PType = PVDecl->getOriginalType();
3209 if (const ArrayType *AT =
3210 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3211 // Use array's original type only if it has known number of
3212 // elements.
3213 if (!isa<ConstantArrayType>(AT))
3214 PType = PVDecl->getType();
3215 } else if (PType->isFunctionType())
3216 PType = PVDecl->getType();
3217 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003218 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003219 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003220 }
3221}
3222
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003223/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003224/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003225void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003226 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003227 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003228 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003229 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003230 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003231 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003232 // Compute size of all parameters.
3233 // Start with computing size of a pointer in number of bytes.
3234 // FIXME: There might(should) be a better way of doing this computation!
3235 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003236 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003237 // The first two arguments (self and _cmd) are pointers; account for
3238 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003239 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003240 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3241 E = Decl->param_end(); PI != E; ++PI) {
3242 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003243 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003244 assert (sz.isPositive() &&
3245 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003246 ParmOffset += sz;
3247 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003248 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003249 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003250 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003251
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003252 // Argument types.
3253 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003254 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3255 E = Decl->param_end(); PI != E; ++PI) {
3256 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003257 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003258 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003259 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3260 // Use array's original type only if it has known number of
3261 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003262 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003263 PType = PVDecl->getType();
3264 } else if (PType->isFunctionType())
3265 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003266 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003267 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003268 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003269 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003270 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003271 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003272 }
3273}
3274
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003275/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003276/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003277/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3278/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003279/// Property attributes are stored as a comma-delimited C string. The simple
3280/// attributes readonly and bycopy are encoded as single characters. The
3281/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3282/// encoded as single characters, followed by an identifier. Property types
3283/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003284/// these attributes are defined by the following enumeration:
3285/// @code
3286/// enum PropertyAttributes {
3287/// kPropertyReadOnly = 'R', // property is read-only.
3288/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3289/// kPropertyByref = '&', // property is a reference to the value last assigned
3290/// kPropertyDynamic = 'D', // property is dynamic
3291/// kPropertyGetter = 'G', // followed by getter selector name
3292/// kPropertySetter = 'S', // followed by setter selector name
3293/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3294/// kPropertyType = 't' // followed by old-style type encoding.
3295/// kPropertyWeak = 'W' // 'weak' property
3296/// kPropertyStrong = 'P' // property GC'able
3297/// kPropertyNonAtomic = 'N' // property non-atomic
3298/// };
3299/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003300void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003301 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003302 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003303 // Collect information from the property implementation decl(s).
3304 bool Dynamic = false;
3305 ObjCPropertyImplDecl *SynthesizePID = 0;
3306
3307 // FIXME: Duplicated code due to poor abstraction.
3308 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003309 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003310 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3311 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003312 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003313 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003314 ObjCPropertyImplDecl *PID = *i;
3315 if (PID->getPropertyDecl() == PD) {
3316 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3317 Dynamic = true;
3318 } else {
3319 SynthesizePID = PID;
3320 }
3321 }
3322 }
3323 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003324 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003325 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003326 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003327 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003328 ObjCPropertyImplDecl *PID = *i;
3329 if (PID->getPropertyDecl() == PD) {
3330 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3331 Dynamic = true;
3332 } else {
3333 SynthesizePID = PID;
3334 }
3335 }
Mike Stump1eb44332009-09-09 15:08:12 +00003336 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003337 }
3338 }
3339
3340 // FIXME: This is not very efficient.
3341 S = "T";
3342
3343 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003344 // GCC has some special rules regarding encoding of properties which
3345 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003346 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003347 true /* outermost type */,
3348 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003349
3350 if (PD->isReadOnly()) {
3351 S += ",R";
3352 } else {
3353 switch (PD->getSetterKind()) {
3354 case ObjCPropertyDecl::Assign: break;
3355 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003356 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003357 }
3358 }
3359
3360 // It really isn't clear at all what this means, since properties
3361 // are "dynamic by default".
3362 if (Dynamic)
3363 S += ",D";
3364
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003365 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3366 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003367
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003368 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3369 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003370 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003371 }
3372
3373 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3374 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003375 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003376 }
3377
3378 if (SynthesizePID) {
3379 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3380 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003381 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003382 }
3383
3384 // FIXME: OBJCGC: weak & strong
3385}
3386
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003387/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003388/// Another legacy compatibility encoding: 32-bit longs are encoded as
3389/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003390/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3391///
3392void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003393 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003394 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003395 if (BT->getKind() == BuiltinType::ULong &&
3396 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003397 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003398 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003399 if (BT->getKind() == BuiltinType::Long &&
3400 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003401 PointeeTy = IntTy;
3402 }
3403 }
3404}
3405
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003406void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003407 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003408 // We follow the behavior of gcc, expanding structures which are
3409 // directly pointed to, and expanding embedded structures. Note that
3410 // these rules are sufficient to prevent recursive encoding of the
3411 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003412 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003413 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003414}
3415
Mike Stump1eb44332009-09-09 15:08:12 +00003416static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003417 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003418 const Expr *E = FD->getBitWidth();
3419 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3420 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00003421 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003422 S += 'b';
3423 S += llvm::utostr(N);
3424}
3425
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003426// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003427void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3428 bool ExpandPointedToStructures,
3429 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003430 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003431 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003432 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00003433 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003434 if (FD && FD->isBitField())
3435 return EncodeBitField(this, S, FD);
3436 char encoding;
3437 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003438 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003439 case BuiltinType::Void: encoding = 'v'; break;
3440 case BuiltinType::Bool: encoding = 'B'; break;
3441 case BuiltinType::Char_U:
3442 case BuiltinType::UChar: encoding = 'C'; break;
3443 case BuiltinType::UShort: encoding = 'S'; break;
3444 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003445 case BuiltinType::ULong:
3446 encoding =
3447 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00003448 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003449 case BuiltinType::UInt128: encoding = 'T'; break;
3450 case BuiltinType::ULongLong: encoding = 'Q'; break;
3451 case BuiltinType::Char_S:
3452 case BuiltinType::SChar: encoding = 'c'; break;
3453 case BuiltinType::Short: encoding = 's'; break;
3454 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003455 case BuiltinType::Long:
3456 encoding =
3457 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003458 break;
3459 case BuiltinType::LongLong: encoding = 'q'; break;
3460 case BuiltinType::Int128: encoding = 't'; break;
3461 case BuiltinType::Float: encoding = 'f'; break;
3462 case BuiltinType::Double: encoding = 'd'; break;
3463 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003464 }
Mike Stump1eb44332009-09-09 15:08:12 +00003465
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003466 S += encoding;
3467 return;
3468 }
Mike Stump1eb44332009-09-09 15:08:12 +00003469
John McCall183700f2009-09-21 23:43:11 +00003470 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003471 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003472 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003473 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003474 return;
3475 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00003476
Ted Kremenek6217b802009-07-29 21:53:49 +00003477 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003478 if (PT->isObjCSelType()) {
3479 S += ':';
3480 return;
3481 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003482 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003483
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003484 bool isReadOnly = false;
3485 // For historical/compatibility reasons, the read-only qualifier of the
3486 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3487 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003488 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003489 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003490 if (OutermostType && T.isConstQualified()) {
3491 isReadOnly = true;
3492 S += 'r';
3493 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003494 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003495 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003496 while (P->getAs<PointerType>())
3497 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003498 if (P.isConstQualified()) {
3499 isReadOnly = true;
3500 S += 'r';
3501 }
3502 }
3503 if (isReadOnly) {
3504 // Another legacy compatibility encoding. Some ObjC qualifier and type
3505 // combinations need to be rearranged.
3506 // Rewrite "in const" from "nr" to "rn"
3507 const char * s = S.c_str();
3508 int len = S.length();
3509 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3510 std::string replace = "rn";
3511 S.replace(S.end()-2, S.end(), replace);
3512 }
3513 }
Mike Stump1eb44332009-09-09 15:08:12 +00003514
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003515 if (PointeeTy->isCharType()) {
3516 // char pointer types should be encoded as '*' unless it is a
3517 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003518 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003519 S += '*';
3520 return;
3521 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003522 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003523 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3524 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3525 S += '#';
3526 return;
3527 }
3528 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3529 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3530 S += '@';
3531 return;
3532 }
3533 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003534 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003535 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003536 getLegacyIntegralTypeEncoding(PointeeTy);
3537
Mike Stump1eb44332009-09-09 15:08:12 +00003538 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003539 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003540 return;
3541 }
Mike Stump1eb44332009-09-09 15:08:12 +00003542
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003543 if (const ArrayType *AT =
3544 // Ignore type qualifiers etc.
3545 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003546 if (isa<IncompleteArrayType>(AT)) {
3547 // Incomplete arrays are encoded as a pointer to the array element.
3548 S += '^';
3549
Mike Stump1eb44332009-09-09 15:08:12 +00003550 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003551 false, ExpandStructures, FD);
3552 } else {
3553 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003554
Anders Carlsson559a8332009-02-22 01:38:57 +00003555 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3556 S += llvm::utostr(CAT->getSize().getZExtValue());
3557 else {
3558 //Variable length arrays are encoded as a regular array with 0 elements.
3559 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3560 S += '0';
3561 }
Mike Stump1eb44332009-09-09 15:08:12 +00003562
3563 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003564 false, ExpandStructures, FD);
3565 S += ']';
3566 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003567 return;
3568 }
Mike Stump1eb44332009-09-09 15:08:12 +00003569
John McCall183700f2009-09-21 23:43:11 +00003570 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003571 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003572 return;
3573 }
Mike Stump1eb44332009-09-09 15:08:12 +00003574
Ted Kremenek6217b802009-07-29 21:53:49 +00003575 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003576 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003577 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003578 // Anonymous structures print as '?'
3579 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3580 S += II->getName();
3581 } else {
3582 S += '?';
3583 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003584 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003585 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003586 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3587 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003588 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003589 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003590 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003591 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003592 S += '"';
3593 }
Mike Stump1eb44332009-09-09 15:08:12 +00003594
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003595 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003596 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003597 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003598 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003599 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003600 QualType qt = Field->getType();
3601 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003602 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003603 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003604 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003605 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003606 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003607 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003608 return;
3609 }
Mike Stump1eb44332009-09-09 15:08:12 +00003610
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003611 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003612 if (FD && FD->isBitField())
3613 EncodeBitField(this, S, FD);
3614 else
3615 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003616 return;
3617 }
Mike Stump1eb44332009-09-09 15:08:12 +00003618
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003619 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003620 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003621 return;
3622 }
Mike Stump1eb44332009-09-09 15:08:12 +00003623
John McCall0953e762009-09-24 19:53:00 +00003624 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003625 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003626 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003627 S += '{';
3628 const IdentifierInfo *II = OI->getIdentifier();
3629 S += II->getName();
3630 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003631 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003632 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003633 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003634 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003635 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003636 RecFields[i]);
3637 else
Mike Stump1eb44332009-09-09 15:08:12 +00003638 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003639 FD);
3640 }
3641 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003642 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003643 }
Mike Stump1eb44332009-09-09 15:08:12 +00003644
John McCall183700f2009-09-21 23:43:11 +00003645 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003646 if (OPT->isObjCIdType()) {
3647 S += '@';
3648 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003649 }
Mike Stump1eb44332009-09-09 15:08:12 +00003650
Steve Naroff27d20a22009-10-28 22:03:49 +00003651 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3652 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3653 // Since this is a binary compatibility issue, need to consult with runtime
3654 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00003655 S += '#';
3656 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003657 }
Mike Stump1eb44332009-09-09 15:08:12 +00003658
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003659 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003660 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003661 ExpandPointedToStructures,
3662 ExpandStructures, FD);
3663 if (FD || EncodingProperty) {
3664 // Note that we do extended encoding of protocol qualifer list
3665 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003666 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003667 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3668 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003669 S += '<';
3670 S += (*I)->getNameAsString();
3671 S += '>';
3672 }
3673 S += '"';
3674 }
3675 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003676 }
Mike Stump1eb44332009-09-09 15:08:12 +00003677
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003678 QualType PointeeTy = OPT->getPointeeType();
3679 if (!EncodingProperty &&
3680 isa<TypedefType>(PointeeTy.getTypePtr())) {
3681 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003682 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003683 // {...};
3684 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003685 getObjCEncodingForTypeImpl(PointeeTy, S,
3686 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003687 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003688 return;
3689 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003690
3691 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00003692 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003693 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00003694 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003695 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3696 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003697 S += '<';
3698 S += (*I)->getNameAsString();
3699 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003700 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003701 S += '"';
3702 }
3703 return;
3704 }
Mike Stump1eb44332009-09-09 15:08:12 +00003705
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003706 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003707}
3708
Mike Stump1eb44332009-09-09 15:08:12 +00003709void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003710 std::string& S) const {
3711 if (QT & Decl::OBJC_TQ_In)
3712 S += 'n';
3713 if (QT & Decl::OBJC_TQ_Inout)
3714 S += 'N';
3715 if (QT & Decl::OBJC_TQ_Out)
3716 S += 'o';
3717 if (QT & Decl::OBJC_TQ_Bycopy)
3718 S += 'O';
3719 if (QT & Decl::OBJC_TQ_Byref)
3720 S += 'R';
3721 if (QT & Decl::OBJC_TQ_Oneway)
3722 S += 'V';
3723}
3724
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003725void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003726 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003727
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003728 BuiltinVaListType = T;
3729}
3730
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003731void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003732 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003733}
3734
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003735void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00003736 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003737}
3738
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003739void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003740 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003741}
3742
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003743void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003744 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003745}
3746
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003747void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003748 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003749 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003750
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003751 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003752}
3753
John McCall0bd6feb2009-12-02 08:04:21 +00003754/// \brief Retrieve the template name that corresponds to a non-empty
3755/// lookup.
John McCalleec51cf2010-01-20 00:46:10 +00003756TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3757 UnresolvedSetIterator End) {
John McCall0bd6feb2009-12-02 08:04:21 +00003758 unsigned size = End - Begin;
3759 assert(size > 1 && "set is not overloaded!");
3760
3761 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3762 size * sizeof(FunctionTemplateDecl*));
3763 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3764
3765 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00003766 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00003767 NamedDecl *D = *I;
3768 assert(isa<FunctionTemplateDecl>(D) ||
3769 (isa<UsingShadowDecl>(D) &&
3770 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3771 *Storage++ = D;
3772 }
3773
3774 return TemplateName(OT);
3775}
3776
Douglas Gregor7532dc62009-03-30 22:58:21 +00003777/// \brief Retrieve the template name that represents a qualified
3778/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003779TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003780 bool TemplateKeyword,
3781 TemplateDecl *Template) {
Douglas Gregor789b1f62010-02-04 18:10:26 +00003782 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00003783 llvm::FoldingSetNodeID ID;
3784 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3785
3786 void *InsertPos = 0;
3787 QualifiedTemplateName *QTN =
3788 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3789 if (!QTN) {
3790 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3791 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3792 }
3793
3794 return TemplateName(QTN);
3795}
3796
3797/// \brief Retrieve the template name that represents a dependent
3798/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003799TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003800 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003801 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003802 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003803
3804 llvm::FoldingSetNodeID ID;
3805 DependentTemplateName::Profile(ID, NNS, Name);
3806
3807 void *InsertPos = 0;
3808 DependentTemplateName *QTN =
3809 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3810
3811 if (QTN)
3812 return TemplateName(QTN);
3813
3814 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3815 if (CanonNNS == NNS) {
3816 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3817 } else {
3818 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3819 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00003820 DependentTemplateName *CheckQTN =
3821 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3822 assert(!CheckQTN && "Dependent type name canonicalization broken");
3823 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00003824 }
3825
3826 DependentTemplateNames.InsertNode(QTN, InsertPos);
3827 return TemplateName(QTN);
3828}
3829
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003830/// \brief Retrieve the template name that represents a dependent
3831/// template name such as \c MetaFun::template operator+.
3832TemplateName
3833ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3834 OverloadedOperatorKind Operator) {
3835 assert((!NNS || NNS->isDependent()) &&
3836 "Nested name specifier must be dependent");
3837
3838 llvm::FoldingSetNodeID ID;
3839 DependentTemplateName::Profile(ID, NNS, Operator);
3840
3841 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00003842 DependentTemplateName *QTN
3843 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003844
3845 if (QTN)
3846 return TemplateName(QTN);
3847
3848 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3849 if (CanonNNS == NNS) {
3850 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3851 } else {
3852 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3853 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00003854
3855 DependentTemplateName *CheckQTN
3856 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3857 assert(!CheckQTN && "Dependent template name canonicalization broken");
3858 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003859 }
3860
3861 DependentTemplateNames.InsertNode(QTN, InsertPos);
3862 return TemplateName(QTN);
3863}
3864
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003865/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003866/// TargetInfo, produce the corresponding type. The unsigned @p Type
3867/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00003868CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003869 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00003870 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003871 case TargetInfo::SignedShort: return ShortTy;
3872 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3873 case TargetInfo::SignedInt: return IntTy;
3874 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3875 case TargetInfo::SignedLong: return LongTy;
3876 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3877 case TargetInfo::SignedLongLong: return LongLongTy;
3878 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3879 }
3880
3881 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00003882 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003883}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003884
3885//===----------------------------------------------------------------------===//
3886// Type Predicates.
3887//===----------------------------------------------------------------------===//
3888
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003889/// isObjCNSObjectType - Return true if this is an NSObject object using
3890/// NSObject attribute on a c-style pointer type.
3891/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003892/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003893///
3894bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3895 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3896 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003897 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003898 return true;
3899 }
Mike Stump1eb44332009-09-09 15:08:12 +00003900 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003901}
3902
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003903/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3904/// garbage collection attribute.
3905///
John McCall0953e762009-09-24 19:53:00 +00003906Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3907 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003908 if (getLangOptions().ObjC1 &&
3909 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003910 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003911 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003912 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003913 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003914 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003915 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003916 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003917 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003918 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003919 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003920 // Non-pointers have none gc'able attribute regardless of the attribute
3921 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003922 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003923 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003924 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003925 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003926}
3927
Chris Lattner6ac46a42008-04-07 06:51:04 +00003928//===----------------------------------------------------------------------===//
3929// Type Compatibility Testing
3930//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003931
Mike Stump1eb44332009-09-09 15:08:12 +00003932/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003933/// compatible.
3934static bool areCompatVectorTypes(const VectorType *LHS,
3935 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00003936 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00003937 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003938 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003939}
3940
Steve Naroff4084c302009-07-23 01:01:38 +00003941//===----------------------------------------------------------------------===//
3942// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3943//===----------------------------------------------------------------------===//
3944
3945/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3946/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003947bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3948 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003949 if (lProto == rProto)
3950 return true;
3951 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3952 E = rProto->protocol_end(); PI != E; ++PI)
3953 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3954 return true;
3955 return false;
3956}
3957
Steve Naroff4084c302009-07-23 01:01:38 +00003958/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3959/// return true if lhs's protocols conform to rhs's protocol; false
3960/// otherwise.
3961bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3962 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3963 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3964 return false;
3965}
3966
3967/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3968/// ObjCQualifiedIDType.
3969bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3970 bool compare) {
3971 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003972 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003973 lhs->isObjCIdType() || lhs->isObjCClassType())
3974 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003975 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003976 rhs->isObjCIdType() || rhs->isObjCClassType())
3977 return true;
3978
3979 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003980 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003981
Steve Naroff4084c302009-07-23 01:01:38 +00003982 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003983
Steve Naroff4084c302009-07-23 01:01:38 +00003984 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003985 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003986 // make sure we check the class hierarchy.
3987 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3988 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3989 E = lhsQID->qual_end(); I != E; ++I) {
3990 // when comparing an id<P> on lhs with a static type on rhs,
3991 // see if static class implements all of id's protocols, directly or
3992 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003993 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003994 return false;
3995 }
3996 }
3997 // If there are no qualifiers and no interface, we have an 'id'.
3998 return true;
3999 }
Mike Stump1eb44332009-09-09 15:08:12 +00004000 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004001 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4002 E = lhsQID->qual_end(); I != E; ++I) {
4003 ObjCProtocolDecl *lhsProto = *I;
4004 bool match = false;
4005
4006 // when comparing an id<P> on lhs with a static type on rhs,
4007 // see if static class implements all of id's protocols, directly or
4008 // through its super class and categories.
4009 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4010 E = rhsOPT->qual_end(); J != E; ++J) {
4011 ObjCProtocolDecl *rhsProto = *J;
4012 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4013 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4014 match = true;
4015 break;
4016 }
4017 }
Mike Stump1eb44332009-09-09 15:08:12 +00004018 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004019 // make sure we check the class hierarchy.
4020 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4021 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4022 E = lhsQID->qual_end(); I != E; ++I) {
4023 // when comparing an id<P> on lhs with a static type on rhs,
4024 // see if static class implements all of id's protocols, directly or
4025 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004026 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004027 match = true;
4028 break;
4029 }
4030 }
4031 }
4032 if (!match)
4033 return false;
4034 }
Mike Stump1eb44332009-09-09 15:08:12 +00004035
Steve Naroff4084c302009-07-23 01:01:38 +00004036 return true;
4037 }
Mike Stump1eb44332009-09-09 15:08:12 +00004038
Steve Naroff4084c302009-07-23 01:01:38 +00004039 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4040 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4041
Mike Stump1eb44332009-09-09 15:08:12 +00004042 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004043 lhs->getAsObjCInterfacePointerType()) {
4044 if (lhsOPT->qual_empty()) {
4045 bool match = false;
4046 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4047 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4048 E = rhsQID->qual_end(); I != E; ++I) {
4049 // when comparing an id<P> on lhs with a static type on rhs,
4050 // see if static class implements all of id's protocols, directly or
4051 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004052 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004053 match = true;
4054 break;
4055 }
4056 }
4057 if (!match)
4058 return false;
4059 }
4060 return true;
4061 }
Mike Stump1eb44332009-09-09 15:08:12 +00004062 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004063 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4064 E = lhsOPT->qual_end(); I != E; ++I) {
4065 ObjCProtocolDecl *lhsProto = *I;
4066 bool match = false;
4067
4068 // when comparing an id<P> on lhs with a static type on rhs,
4069 // see if static class implements all of id's protocols, directly or
4070 // through its super class and categories.
4071 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4072 E = rhsQID->qual_end(); J != E; ++J) {
4073 ObjCProtocolDecl *rhsProto = *J;
4074 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4075 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4076 match = true;
4077 break;
4078 }
4079 }
4080 if (!match)
4081 return false;
4082 }
4083 return true;
4084 }
4085 return false;
4086}
4087
Eli Friedman3d815e72008-08-22 00:56:42 +00004088/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004089/// compatible for assignment from RHS to LHS. This handles validation of any
4090/// protocol qualifiers on the LHS or RHS.
4091///
Steve Naroff14108da2009-07-10 23:34:53 +00004092bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4093 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004094 // If either type represents the built-in 'id' or 'Class' types, return true.
4095 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00004096 return true;
4097
Steve Naroff4084c302009-07-23 01:01:38 +00004098 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00004099 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4100 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004101 false);
4102
Steve Naroff14108da2009-07-10 23:34:53 +00004103 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4104 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00004105 if (LHS && RHS) // We have 2 user-defined types.
4106 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004107
Steve Naroff4084c302009-07-23 01:01:38 +00004108 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004109}
4110
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004111/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4112/// for providing type-safty for objective-c pointers used to pass/return
4113/// arguments in block literals. When passed as arguments, passing 'A*' where
4114/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4115/// not OK. For the return type, the opposite is not OK.
4116bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4117 const ObjCObjectPointerType *LHSOPT,
4118 const ObjCObjectPointerType *RHSOPT) {
4119 if (RHSOPT->isObjCBuiltinType())
4120 return true;
4121
4122 if (LHSOPT->isObjCBuiltinType()) {
4123 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4124 }
4125
4126 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
4127 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4128 QualType(RHSOPT,0),
4129 false);
4130
4131 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4132 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4133 if (LHS && RHS) { // We have 2 user-defined types.
4134 if (LHS != RHS) {
4135 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4136 return false;
4137 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4138 return true;
4139 }
4140 else
4141 return true;
4142 }
4143 return false;
4144}
4145
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004146/// getIntersectionOfProtocols - This routine finds the intersection of set
4147/// of protocols inherited from two distinct objective-c pointer objects.
4148/// It is used to build composite qualifier list of the composite type of
4149/// the conditional expression involving two objective-c pointer objects.
4150static
4151void getIntersectionOfProtocols(ASTContext &Context,
4152 const ObjCObjectPointerType *LHSOPT,
4153 const ObjCObjectPointerType *RHSOPT,
4154 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4155
4156 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4157 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4158
4159 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4160 unsigned LHSNumProtocols = LHS->getNumProtocols();
4161 if (LHSNumProtocols > 0)
4162 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4163 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004164 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4165 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004166 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4167 LHSInheritedProtocols.end());
4168 }
4169
4170 unsigned RHSNumProtocols = RHS->getNumProtocols();
4171 if (RHSNumProtocols > 0) {
4172 ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin();
4173 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4174 if (InheritedProtocolSet.count(RHSProtocols[i]))
4175 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4176 }
4177 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004178 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004179 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004180 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4181 RHSInheritedProtocols.begin(),
4182 E = RHSInheritedProtocols.end(); I != E; ++I)
4183 if (InheritedProtocolSet.count((*I)))
4184 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004185 }
4186}
4187
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004188/// areCommonBaseCompatible - Returns common base class of the two classes if
4189/// one found. Note that this is O'2 algorithm. But it will be called as the
4190/// last type comparison in a ?-exp of ObjC pointer types before a
4191/// warning is issued. So, its invokation is extremely rare.
4192QualType ASTContext::areCommonBaseCompatible(
4193 const ObjCObjectPointerType *LHSOPT,
4194 const ObjCObjectPointerType *RHSOPT) {
4195 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4196 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4197 if (!LHS || !RHS)
4198 return QualType();
4199
4200 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4201 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4202 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004203 if (canAssignObjCInterfaces(LHS, RHS)) {
4204 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4205 getIntersectionOfProtocols(*this,
4206 LHSOPT, RHSOPT, IntersectionOfProtocols);
4207 if (IntersectionOfProtocols.empty())
4208 LHSTy = getObjCObjectPointerType(LHSTy);
4209 else
4210 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4211 IntersectionOfProtocols.size());
4212 return LHSTy;
4213 }
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004214 }
4215
4216 return QualType();
4217}
4218
Eli Friedman3d815e72008-08-22 00:56:42 +00004219bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4220 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00004221 // Verify that the base decls are compatible: the RHS must be a subclass of
4222 // the LHS.
4223 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4224 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004225
Chris Lattner6ac46a42008-04-07 06:51:04 +00004226 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4227 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004228 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004229 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004230
Chris Lattner6ac46a42008-04-07 06:51:04 +00004231 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4232 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004233 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004234 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00004235
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004236 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4237 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004238 LHSPI != LHSPE; LHSPI++) {
4239 bool RHSImplementsProtocol = false;
4240
4241 // If the RHS doesn't implement the protocol on the left, the types
4242 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004243 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00004244 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00004245 RHSPI != RHSPE; RHSPI++) {
4246 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004247 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00004248 break;
4249 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004250 }
4251 // FIXME: For better diagnostics, consider passing back the protocol name.
4252 if (!RHSImplementsProtocol)
4253 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004254 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004255 // The RHS implements all protocols listed on the LHS.
4256 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004257}
4258
Steve Naroff389bf462009-02-12 17:52:19 +00004259bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4260 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00004261 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4262 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004263
Steve Naroff14108da2009-07-10 23:34:53 +00004264 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00004265 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004266
4267 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4268 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00004269}
4270
Mike Stump1eb44332009-09-09 15:08:12 +00004271/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00004272/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00004273/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00004274/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00004275bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004276 if (getLangOptions().CPlusPlus)
4277 return hasSameType(LHS, RHS);
4278
Eli Friedman3d815e72008-08-22 00:56:42 +00004279 return !mergeTypes(LHS, RHS).isNull();
4280}
4281
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004282bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4283 return !mergeTypes(LHS, RHS, true).isNull();
4284}
4285
4286QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4287 bool OfBlockPointer) {
John McCall183700f2009-09-21 23:43:11 +00004288 const FunctionType *lbase = lhs->getAs<FunctionType>();
4289 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00004290 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4291 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00004292 bool allLTypes = true;
4293 bool allRTypes = true;
4294
4295 // Check return type
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004296 QualType retType;
4297 if (OfBlockPointer)
4298 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4299 else
4300 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman3d815e72008-08-22 00:56:42 +00004301 if (retType.isNull()) return QualType();
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004302 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner61710852008-10-05 17:34:18 +00004303 allLTypes = false;
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004304 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner61710852008-10-05 17:34:18 +00004305 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00004306 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00004307 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
4308 if (NoReturn != lbase->getNoReturnAttr())
4309 allLTypes = false;
4310 if (NoReturn != rbase->getNoReturnAttr())
4311 allRTypes = false;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004312 CallingConv lcc = lbase->getCallConv();
4313 CallingConv rcc = rbase->getCallConv();
4314 // Compatible functions must have compatible calling conventions
John McCall04a67a62010-02-05 21:31:56 +00004315 if (!isSameCallConv(lcc, rcc))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004316 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004317
Eli Friedman3d815e72008-08-22 00:56:42 +00004318 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00004319 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4320 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004321 unsigned lproto_nargs = lproto->getNumArgs();
4322 unsigned rproto_nargs = rproto->getNumArgs();
4323
4324 // Compatible functions must have the same number of arguments
4325 if (lproto_nargs != rproto_nargs)
4326 return QualType();
4327
4328 // Variadic and non-variadic functions aren't compatible
4329 if (lproto->isVariadic() != rproto->isVariadic())
4330 return QualType();
4331
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00004332 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4333 return QualType();
4334
Eli Friedman3d815e72008-08-22 00:56:42 +00004335 // Check argument compatibility
4336 llvm::SmallVector<QualType, 10> types;
4337 for (unsigned i = 0; i < lproto_nargs; i++) {
4338 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4339 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004340 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman3d815e72008-08-22 00:56:42 +00004341 if (argtype.isNull()) return QualType();
4342 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00004343 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4344 allLTypes = false;
4345 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4346 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00004347 }
4348 if (allLTypes) return lhs;
4349 if (allRTypes) return rhs;
4350 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00004351 lproto->isVariadic(), lproto->getTypeQuals(),
Douglas Gregor1bf40242010-02-12 17:17:28 +00004352 false, false, 0, 0, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004353 }
4354
4355 if (lproto) allRTypes = false;
4356 if (rproto) allLTypes = false;
4357
Douglas Gregor72564e72009-02-26 23:50:07 +00004358 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00004359 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00004360 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004361 if (proto->isVariadic()) return QualType();
4362 // Check that the types are compatible with the types that
4363 // would result from default argument promotions (C99 6.7.5.3p15).
4364 // The only types actually affected are promotable integer
4365 // types and floats, which would be passed as a different
4366 // type depending on whether the prototype is visible.
4367 unsigned proto_nargs = proto->getNumArgs();
4368 for (unsigned i = 0; i < proto_nargs; ++i) {
4369 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00004370
4371 // Look at the promotion type of enum types, since that is the type used
4372 // to pass enum values.
4373 if (const EnumType *Enum = argTy->getAs<EnumType>())
4374 argTy = Enum->getDecl()->getPromotionType();
4375
Eli Friedman3d815e72008-08-22 00:56:42 +00004376 if (argTy->isPromotableIntegerType() ||
4377 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4378 return QualType();
4379 }
4380
4381 if (allLTypes) return lhs;
4382 if (allRTypes) return rhs;
4383 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00004384 proto->getNumArgs(), proto->isVariadic(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00004385 proto->getTypeQuals(),
4386 false, false, 0, 0, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004387 }
4388
4389 if (allLTypes) return lhs;
4390 if (allRTypes) return rhs;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004391 return getFunctionNoProtoType(retType, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004392}
4393
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004394QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4395 bool OfBlockPointer) {
Bill Wendling43d69752007-12-03 07:33:35 +00004396 // C++ [expr]: If an expression initially has the type "reference to T", the
4397 // type is adjusted to "T" prior to any further analysis, the expression
4398 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004399 // expression is an lvalue unless the reference is an rvalue reference and
4400 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004401 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4402 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4403
Eli Friedman3d815e72008-08-22 00:56:42 +00004404 QualType LHSCan = getCanonicalType(LHS),
4405 RHSCan = getCanonicalType(RHS);
4406
4407 // If two types are identical, they are compatible.
4408 if (LHSCan == RHSCan)
4409 return LHS;
4410
John McCall0953e762009-09-24 19:53:00 +00004411 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00004412 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4413 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00004414 if (LQuals != RQuals) {
4415 // If any of these qualifiers are different, we have a type
4416 // mismatch.
4417 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4418 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4419 return QualType();
4420
4421 // Exactly one GC qualifier difference is allowed: __strong is
4422 // okay if the other type has no GC qualifier but is an Objective
4423 // C object pointer (i.e. implicitly strong by default). We fix
4424 // this by pretending that the unqualified type was actually
4425 // qualified __strong.
4426 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4427 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4428 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4429
4430 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4431 return QualType();
4432
4433 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4434 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4435 }
4436 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4437 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4438 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004439 return QualType();
John McCall0953e762009-09-24 19:53:00 +00004440 }
4441
4442 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00004443
Eli Friedman852d63b2009-06-01 01:22:52 +00004444 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4445 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00004446
Chris Lattner1adb8832008-01-14 05:45:46 +00004447 // We want to consider the two function types to be the same for these
4448 // comparisons, just force one to the other.
4449 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4450 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00004451
4452 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00004453 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4454 LHSClass = Type::ConstantArray;
4455 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4456 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00004457
Nate Begeman213541a2008-04-18 23:10:10 +00004458 // Canonicalize ExtVector -> Vector.
4459 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4460 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00004461
Chris Lattnera36a61f2008-04-07 05:43:21 +00004462 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004463 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00004464 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00004465 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00004466 // Compatibility is based on the underlying type, not the promotion
4467 // type.
John McCall183700f2009-09-21 23:43:11 +00004468 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004469 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4470 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004471 }
John McCall183700f2009-09-21 23:43:11 +00004472 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004473 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4474 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004475 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004476
Eli Friedman3d815e72008-08-22 00:56:42 +00004477 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004478 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004479
Steve Naroff4a746782008-01-09 22:43:08 +00004480 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004481 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00004482#define TYPE(Class, Base)
4483#define ABSTRACT_TYPE(Class, Base)
John McCallad5e7382010-03-01 23:49:17 +00004484#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregor72564e72009-02-26 23:50:07 +00004485#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4486#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4487#include "clang/AST/TypeNodes.def"
4488 assert(false && "Non-canonical and dependent types shouldn't get here");
4489 return QualType();
4490
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004491 case Type::LValueReference:
4492 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00004493 case Type::MemberPointer:
4494 assert(false && "C++ should never be in mergeTypes");
4495 return QualType();
4496
4497 case Type::IncompleteArray:
4498 case Type::VariableArray:
4499 case Type::FunctionProto:
4500 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00004501 assert(false && "Types are eliminated above");
4502 return QualType();
4503
Chris Lattner1adb8832008-01-14 05:45:46 +00004504 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00004505 {
4506 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004507 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4508 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00004509 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4510 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00004511 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004512 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00004513 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004514 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004515 return getPointerType(ResultType);
4516 }
Steve Naroffc0febd52008-12-10 17:49:55 +00004517 case Type::BlockPointer:
4518 {
4519 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004520 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4521 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004522 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroffc0febd52008-12-10 17:49:55 +00004523 if (ResultType.isNull()) return QualType();
4524 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4525 return LHS;
4526 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4527 return RHS;
4528 return getBlockPointerType(ResultType);
4529 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004530 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00004531 {
4532 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4533 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4534 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4535 return QualType();
4536
4537 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4538 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4539 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4540 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00004541 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4542 return LHS;
4543 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4544 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00004545 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4546 ArrayType::ArraySizeModifier(), 0);
4547 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4548 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004549 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4550 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00004551 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4552 return LHS;
4553 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4554 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004555 if (LVAT) {
4556 // FIXME: This isn't correct! But tricky to implement because
4557 // the array's size has to be the size of LHS, but the type
4558 // has to be different.
4559 return LHS;
4560 }
4561 if (RVAT) {
4562 // FIXME: This isn't correct! But tricky to implement because
4563 // the array's size has to be the size of RHS, but the type
4564 // has to be different.
4565 return RHS;
4566 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00004567 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4568 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004569 return getIncompleteArrayType(ResultType,
4570 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004571 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004572 case Type::FunctionNoProto:
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004573 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregor72564e72009-02-26 23:50:07 +00004574 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00004575 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00004576 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00004577 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004578 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00004579 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00004580 case Type::Complex:
4581 // Distinct complex types are incompatible.
4582 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004583 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004584 // FIXME: The merged type should be an ExtVector!
John McCall1c471f32010-03-12 23:14:13 +00004585 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4586 RHSCan->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00004587 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00004588 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004589 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00004590 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004591 // FIXME: This should be type compatibility, e.g. whether
4592 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00004593 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4594 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00004595 if (LHSIface && RHSIface &&
4596 canAssignObjCInterfaces(LHSIface, RHSIface))
4597 return LHS;
4598
Eli Friedman3d815e72008-08-22 00:56:42 +00004599 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004600 }
Steve Naroff14108da2009-07-10 23:34:53 +00004601 case Type::ObjCObjectPointer: {
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004602 if (OfBlockPointer) {
4603 if (canAssignObjCInterfacesInBlockPointer(
4604 LHS->getAs<ObjCObjectPointerType>(),
4605 RHS->getAs<ObjCObjectPointerType>()))
4606 return LHS;
4607 return QualType();
4608 }
John McCall183700f2009-09-21 23:43:11 +00004609 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4610 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00004611 return LHS;
4612
Steve Naroffbc76dd02008-12-10 22:14:21 +00004613 return QualType();
Fariborz Jahanian132f2a22010-03-17 00:20:01 +00004614 }
Steve Naroffec0550f2007-10-15 20:41:53 +00004615 }
Douglas Gregor72564e72009-02-26 23:50:07 +00004616
4617 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004618}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00004619
Chris Lattner5426bf62008-04-07 07:01:58 +00004620//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00004621// Integer Predicates
4622//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00004623
Eli Friedmanad74a752008-06-28 06:23:08 +00004624unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl632d7722009-11-05 21:10:57 +00004625 if (T->isBooleanType())
Eli Friedmanad74a752008-06-28 06:23:08 +00004626 return 1;
John McCall842aef82009-12-09 09:09:27 +00004627 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00004628 T = ET->getDecl()->getIntegerType();
Eli Friedmanf98aba32009-02-13 02:31:07 +00004629 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00004630 return (unsigned)getTypeSize(T);
4631}
4632
4633QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4634 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00004635
4636 // Turn <4 x signed int> -> <4 x unsigned int>
4637 if (const VectorType *VTy = T->getAs<VectorType>())
4638 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson82287d12010-02-05 00:12:22 +00004639 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattner6a2b9262009-10-17 20:33:28 +00004640
4641 // For enums, we return the unsigned version of the base type.
4642 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00004643 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00004644
4645 const BuiltinType *BTy = T->getAs<BuiltinType>();
4646 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00004647 switch (BTy->getKind()) {
4648 case BuiltinType::Char_S:
4649 case BuiltinType::SChar:
4650 return UnsignedCharTy;
4651 case BuiltinType::Short:
4652 return UnsignedShortTy;
4653 case BuiltinType::Int:
4654 return UnsignedIntTy;
4655 case BuiltinType::Long:
4656 return UnsignedLongTy;
4657 case BuiltinType::LongLong:
4658 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004659 case BuiltinType::Int128:
4660 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004661 default:
4662 assert(0 && "Unexpected signed integer type");
4663 return QualType();
4664 }
4665}
4666
Douglas Gregor2cf26342009-04-09 22:27:44 +00004667ExternalASTSource::~ExternalASTSource() { }
4668
4669void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004670
4671
4672//===----------------------------------------------------------------------===//
4673// Builtin Type Computation
4674//===----------------------------------------------------------------------===//
4675
4676/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4677/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004678static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004679 ASTContext::GetBuiltinTypeError &Error,
4680 bool AllowTypeModifiers = true) {
4681 // Modifiers.
4682 int HowLong = 0;
4683 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004684
Chris Lattner86df27b2009-06-14 00:45:47 +00004685 // Read the modifiers first.
4686 bool Done = false;
4687 while (!Done) {
4688 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004689 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004690 case 'S':
4691 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4692 assert(!Signed && "Can't use 'S' modifier multiple times!");
4693 Signed = true;
4694 break;
4695 case 'U':
4696 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4697 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4698 Unsigned = true;
4699 break;
4700 case 'L':
4701 assert(HowLong <= 2 && "Can't have LLLL modifier");
4702 ++HowLong;
4703 break;
4704 }
4705 }
4706
4707 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004708
Chris Lattner86df27b2009-06-14 00:45:47 +00004709 // Read the base type.
4710 switch (*Str++) {
4711 default: assert(0 && "Unknown builtin type letter!");
4712 case 'v':
4713 assert(HowLong == 0 && !Signed && !Unsigned &&
4714 "Bad modifiers used with 'v'!");
4715 Type = Context.VoidTy;
4716 break;
4717 case 'f':
4718 assert(HowLong == 0 && !Signed && !Unsigned &&
4719 "Bad modifiers used with 'f'!");
4720 Type = Context.FloatTy;
4721 break;
4722 case 'd':
4723 assert(HowLong < 2 && !Signed && !Unsigned &&
4724 "Bad modifiers used with 'd'!");
4725 if (HowLong)
4726 Type = Context.LongDoubleTy;
4727 else
4728 Type = Context.DoubleTy;
4729 break;
4730 case 's':
4731 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4732 if (Unsigned)
4733 Type = Context.UnsignedShortTy;
4734 else
4735 Type = Context.ShortTy;
4736 break;
4737 case 'i':
4738 if (HowLong == 3)
4739 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4740 else if (HowLong == 2)
4741 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4742 else if (HowLong == 1)
4743 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4744 else
4745 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4746 break;
4747 case 'c':
4748 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4749 if (Signed)
4750 Type = Context.SignedCharTy;
4751 else if (Unsigned)
4752 Type = Context.UnsignedCharTy;
4753 else
4754 Type = Context.CharTy;
4755 break;
4756 case 'b': // boolean
4757 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4758 Type = Context.BoolTy;
4759 break;
4760 case 'z': // size_t.
4761 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4762 Type = Context.getSizeType();
4763 break;
4764 case 'F':
4765 Type = Context.getCFConstantStringType();
4766 break;
4767 case 'a':
4768 Type = Context.getBuiltinVaListType();
4769 assert(!Type.isNull() && "builtin va list type not initialized!");
4770 break;
4771 case 'A':
4772 // This is a "reference" to a va_list; however, what exactly
4773 // this means depends on how va_list is defined. There are two
4774 // different kinds of va_list: ones passed by value, and ones
4775 // passed by reference. An example of a by-value va_list is
4776 // x86, where va_list is a char*. An example of by-ref va_list
4777 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4778 // we want this argument to be a char*&; for x86-64, we want
4779 // it to be a __va_list_tag*.
4780 Type = Context.getBuiltinVaListType();
4781 assert(!Type.isNull() && "builtin va list type not initialized!");
4782 if (Type->isArrayType()) {
4783 Type = Context.getArrayDecayedType(Type);
4784 } else {
4785 Type = Context.getLValueReferenceType(Type);
4786 }
4787 break;
4788 case 'V': {
4789 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004790 unsigned NumElements = strtoul(Str, &End, 10);
4791 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004792
Chris Lattner86df27b2009-06-14 00:45:47 +00004793 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004794
Chris Lattner86df27b2009-06-14 00:45:47 +00004795 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson82287d12010-02-05 00:12:22 +00004796 // FIXME: Don't know what to do about AltiVec.
4797 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattner86df27b2009-06-14 00:45:47 +00004798 break;
4799 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004800 case 'X': {
4801 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4802 Type = Context.getComplexType(ElementType);
4803 break;
4804 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004805 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004806 Type = Context.getFILEType();
4807 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004808 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004809 return QualType();
4810 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004811 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004812 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004813 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004814 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004815 else
4816 Type = Context.getjmp_bufType();
4817
Mike Stumpfd612db2009-07-28 23:47:15 +00004818 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004819 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004820 return QualType();
4821 }
4822 break;
Mike Stump782fa302009-07-28 02:25:19 +00004823 }
Mike Stump1eb44332009-09-09 15:08:12 +00004824
Chris Lattner86df27b2009-06-14 00:45:47 +00004825 if (!AllowTypeModifiers)
4826 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004827
Chris Lattner86df27b2009-06-14 00:45:47 +00004828 Done = false;
4829 while (!Done) {
John McCall187ab372010-03-12 04:21:28 +00004830 switch (char c = *Str++) {
Chris Lattner86df27b2009-06-14 00:45:47 +00004831 default: Done = true; --Str; break;
4832 case '*':
Chris Lattner86df27b2009-06-14 00:45:47 +00004833 case '&':
John McCall187ab372010-03-12 04:21:28 +00004834 {
4835 // Both pointers and references can have their pointee types
4836 // qualified with an address space.
4837 char *End;
4838 unsigned AddrSpace = strtoul(Str, &End, 10);
4839 if (End != Str && AddrSpace != 0) {
4840 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4841 Str = End;
4842 }
4843 }
4844 if (c == '*')
4845 Type = Context.getPointerType(Type);
4846 else
4847 Type = Context.getLValueReferenceType(Type);
Chris Lattner86df27b2009-06-14 00:45:47 +00004848 break;
4849 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4850 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004851 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004852 break;
Fariborz Jahanian013af392010-01-26 22:48:42 +00004853 case 'D':
4854 Type = Context.getVolatileType(Type);
4855 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004856 }
4857 }
Mike Stump1eb44332009-09-09 15:08:12 +00004858
Chris Lattner86df27b2009-06-14 00:45:47 +00004859 return Type;
4860}
4861
4862/// GetBuiltinType - Return the type for the specified builtin.
4863QualType ASTContext::GetBuiltinType(unsigned id,
4864 GetBuiltinTypeError &Error) {
4865 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004866
Chris Lattner86df27b2009-06-14 00:45:47 +00004867 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004868
Chris Lattner86df27b2009-06-14 00:45:47 +00004869 Error = GE_None;
4870 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4871 if (Error != GE_None)
4872 return QualType();
4873 while (TypeStr[0] && TypeStr[0] != '.') {
4874 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4875 if (Error != GE_None)
4876 return QualType();
4877
4878 // Do array -> pointer decay. The builtin should use the decayed type.
4879 if (Ty->isArrayType())
4880 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004881
Chris Lattner86df27b2009-06-14 00:45:47 +00004882 ArgTypes.push_back(Ty);
4883 }
4884
4885 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4886 "'.' should only occur at end of builtin type list!");
4887
4888 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4889 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4890 return getFunctionNoProtoType(ResType);
Douglas Gregorce056bc2010-02-21 22:15:06 +00004891
4892 // FIXME: Should we create noreturn types?
Chris Lattner86df27b2009-06-14 00:45:47 +00004893 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00004894 TypeStr[0] == '.', 0, false, false, 0, 0,
4895 false, CC_Default);
Chris Lattner86df27b2009-06-14 00:45:47 +00004896}
Eli Friedmana95d7572009-08-19 07:44:53 +00004897
4898QualType
4899ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4900 // Perform the usual unary conversions. We do this early so that
4901 // integral promotions to "int" can allow us to exit early, in the
4902 // lhs == rhs check. Also, for conversion purposes, we ignore any
4903 // qualifiers. For example, "const float" and "float" are
4904 // equivalent.
4905 if (lhs->isPromotableIntegerType())
4906 lhs = getPromotedIntegerType(lhs);
4907 else
4908 lhs = lhs.getUnqualifiedType();
4909 if (rhs->isPromotableIntegerType())
4910 rhs = getPromotedIntegerType(rhs);
4911 else
4912 rhs = rhs.getUnqualifiedType();
4913
4914 // If both types are identical, no conversion is needed.
4915 if (lhs == rhs)
4916 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004917
Eli Friedmana95d7572009-08-19 07:44:53 +00004918 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4919 // The caller can deal with this (e.g. pointer + int).
4920 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4921 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004922
4923 // At this point, we have two different arithmetic types.
4924
Eli Friedmana95d7572009-08-19 07:44:53 +00004925 // Handle complex types first (C99 6.3.1.8p1).
4926 if (lhs->isComplexType() || rhs->isComplexType()) {
4927 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004928 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004929 // convert the rhs to the lhs complex type.
4930 return lhs;
4931 }
Mike Stump1eb44332009-09-09 15:08:12 +00004932 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004933 // convert the lhs to the rhs complex type.
4934 return rhs;
4935 }
4936 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004937 // When both operands are complex, the shorter operand is converted to the
4938 // type of the longer, and that is the type of the result. This corresponds
4939 // to what is done when combining two real floating-point operands.
4940 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004941 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004942 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004943 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004944 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004945 // "double _Complex" is promoted to "long double _Complex".
4946 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004947
4948 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004949 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004950 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004951 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004952 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004953 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4954 // domains match. This is a requirement for our implementation, C99
4955 // does not require this promotion.
4956 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4957 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4958 return rhs;
4959 } else { // handle "_Complex double, double".
4960 return lhs;
4961 }
4962 }
4963 return lhs; // The domain/size match exactly.
4964 }
4965 // Now handle "real" floating types (i.e. float, double, long double).
4966 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4967 // if we have an integer operand, the result is the real floating type.
4968 if (rhs->isIntegerType()) {
4969 // convert rhs to the lhs floating point type.
4970 return lhs;
4971 }
4972 if (rhs->isComplexIntegerType()) {
4973 // convert rhs to the complex floating point type.
4974 return getComplexType(lhs);
4975 }
4976 if (lhs->isIntegerType()) {
4977 // convert lhs to the rhs floating point type.
4978 return rhs;
4979 }
Mike Stump1eb44332009-09-09 15:08:12 +00004980 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004981 // convert lhs to the complex floating point type.
4982 return getComplexType(rhs);
4983 }
4984 // We have two real floating types, float/complex combos were handled above.
4985 // Convert the smaller operand to the bigger result.
4986 int result = getFloatingTypeOrder(lhs, rhs);
4987 if (result > 0) // convert the rhs
4988 return lhs;
4989 assert(result < 0 && "illegal float comparison");
4990 return rhs; // convert the lhs
4991 }
4992 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4993 // Handle GCC complex int extension.
4994 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4995 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4996
4997 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004998 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004999 rhsComplexInt->getElementType()) >= 0)
5000 return lhs; // convert the rhs
5001 return rhs;
5002 } else if (lhsComplexInt && rhs->isIntegerType()) {
5003 // convert the rhs to the lhs complex type.
5004 return lhs;
5005 } else if (rhsComplexInt && lhs->isIntegerType()) {
5006 // convert the lhs to the rhs complex type.
5007 return rhs;
5008 }
5009 }
5010 // Finally, we have two differing integer types.
5011 // The rules for this case are in C99 6.3.1.8
5012 int compare = getIntegerTypeOrder(lhs, rhs);
5013 bool lhsSigned = lhs->isSignedIntegerType(),
5014 rhsSigned = rhs->isSignedIntegerType();
5015 QualType destType;
5016 if (lhsSigned == rhsSigned) {
5017 // Same signedness; use the higher-ranked type
5018 destType = compare >= 0 ? lhs : rhs;
5019 } else if (compare != (lhsSigned ? 1 : -1)) {
5020 // The unsigned type has greater than or equal rank to the
5021 // signed type, so use the unsigned type
5022 destType = lhsSigned ? rhs : lhs;
5023 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5024 // The two types are different widths; if we are here, that
5025 // means the signed type is larger than the unsigned type, so
5026 // use the signed type.
5027 destType = lhsSigned ? lhs : rhs;
5028 } else {
5029 // The signed type is higher-ranked than the unsigned type,
5030 // but isn't actually any bigger (like unsigned int and long
5031 // on most 32-bit systems). Use the unsigned type corresponding
5032 // to the signed type.
5033 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5034 }
5035 return destType;
5036}