blob: 27373478573e3838cb8bcde7efd7ffc8ebc4fbf3 [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerddc135e2006-11-10 06:34:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyck8c89d592009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff67391b82007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/ExternalASTSource.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000024#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000026#include "llvm/ADT/SmallString.h"
Anders Carlssond8499822007-10-29 05:01:08 +000027#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000028#include "llvm/Support/MathExtras.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000029#include "llvm/Support/raw_ostream.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000030#include "RecordLayoutBuilder.h"
31
Chris Lattnerddc135e2006-11-10 06:34:16 +000032using namespace clang;
33
Steve Naroff0af91202007-04-27 21:51:21 +000034enum FloatingRank {
35 FloatRank, DoubleRank, LongDoubleRank
36};
37
Chris Lattner465fa322008-10-05 17:34:18 +000038ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar1b444192009-11-13 05:51:54 +000039 const TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +000040 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +000041 Builtin::Context &builtins,
Mike Stump11289f42009-09-09 15:08:12 +000042 bool FreeMem, unsigned size_reserve) :
43 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Fariborz Jahaniane804c282010-04-23 17:41:07 +000044 NSConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +000045 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +000046 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
Douglas Gregor9507d462010-03-19 22:13:20 +000047 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000048 Idents(idents), Selectors(sels),
John McCallc62bb642010-03-24 05:22:00 +000049 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts),
50 LastSDM(0, 0) {
David Chisnall9f57c292009-08-17 16:35:33 +000051 ObjCIdRedefinitionType = QualType();
52 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian04b258c2009-11-25 23:07:42 +000053 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +000054 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +000055 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +000056 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +000057}
58
Chris Lattnerd5973eb2006-11-12 00:53:46 +000059ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +000060 // Release the DenseMaps associated with DeclContext objects.
61 // FIXME: Is this the ideal solution?
62 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +000063
64 // Release all of the memory associated with overridden C++ methods.
65 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
66 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
67 OM != OMEnd; ++OM)
68 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +000069
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000070 if (FreeMemory) {
71 // Deallocate all the types.
72 while (!Types.empty()) {
73 Types.back()->Destroy(*this);
74 Types.pop_back();
75 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +000076
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000077 for (llvm::FoldingSet<ExtQuals>::iterator
78 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
79 // Increment in loop to prevent using deallocated memory.
John McCall8ccfcb52009-09-24 19:53:00 +000080 Deallocate(&*I++);
Nuno Lopese013c7f2008-12-17 22:30:25 +000081 }
Nuno Lopese013c7f2008-12-17 22:30:25 +000082
Ted Kremenekc3015a92010-03-08 20:56:29 +000083 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
84 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
85 // Increment in loop to prevent using deallocated memory.
86 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
87 R->Destroy(*this);
88 }
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000089
Ted Kremenekc3015a92010-03-08 20:56:29 +000090 for (llvm::DenseMap<const ObjCContainerDecl*,
91 const ASTRecordLayout*>::iterator
92 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
93 // Increment in loop to prevent using deallocated memory.
94 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
95 R->Destroy(*this);
96 }
Nuno Lopese013c7f2008-12-17 22:30:25 +000097 }
98
Douglas Gregorf21eb492009-03-26 23:50:42 +000099 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000100 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
101 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000102 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000103 NNS != NNSEnd; ) {
104 // Increment in loop to prevent using deallocated memory.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000105 (*NNS++).Destroy(*this);
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000106 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000107
108 if (GlobalNestedNameSpecifier)
109 GlobalNestedNameSpecifier->Destroy(*this);
110
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000111 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000112}
113
Mike Stump11289f42009-09-09 15:08:12 +0000114void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
116 ExternalSource.reset(Source.take());
117}
118
Chris Lattner4eb445d2007-01-26 01:27:23 +0000119void ASTContext::PrintStats() const {
120 fprintf(stderr, "*** AST Context Stats:\n");
121 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000122
Douglas Gregora30d0462009-05-26 14:40:08 +0000123 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000124#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000125#define ABSTRACT_TYPE(Name, Parent)
126#include "clang/AST/TypeNodes.def"
127 0 // Extra
128 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000129
Chris Lattner4eb445d2007-01-26 01:27:23 +0000130 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
131 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000132 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000133 }
134
Douglas Gregora30d0462009-05-26 14:40:08 +0000135 unsigned Idx = 0;
136 unsigned TotalBytes = 0;
137#define TYPE(Name, Parent) \
138 if (counts[Idx]) \
139 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
140 TotalBytes += counts[Idx] * sizeof(Name##Type); \
141 ++Idx;
142#define ABSTRACT_TYPE(Name, Parent)
143#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000144
Douglas Gregora30d0462009-05-26 14:40:08 +0000145 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146
147 if (ExternalSource.get()) {
148 fprintf(stderr, "\n");
149 ExternalSource->PrintStats();
150 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000151}
152
153
John McCall48f2d582009-10-23 23:03:21 +0000154void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000155 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000156 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000157 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000158}
159
Chris Lattner970e54e2006-11-12 00:37:36 +0000160void ASTContext::InitBuiltinTypes() {
161 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattner970e54e2006-11-12 00:37:36 +0000163 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000164 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000165
Chris Lattner970e54e2006-11-12 00:37:36 +0000166 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000167 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000168 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000169 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000170 InitBuiltinType(CharTy, BuiltinType::Char_S);
171 else
172 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000173 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000174 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
175 InitBuiltinType(ShortTy, BuiltinType::Short);
176 InitBuiltinType(IntTy, BuiltinType::Int);
177 InitBuiltinType(LongTy, BuiltinType::Long);
178 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattner970e54e2006-11-12 00:37:36 +0000180 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000181 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
182 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
183 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
184 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
185 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattner970e54e2006-11-12 00:37:36 +0000187 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000188 InitBuiltinType(FloatTy, BuiltinType::Float);
189 InitBuiltinType(DoubleTy, BuiltinType::Double);
190 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000191
Chris Lattnerf122cef2009-04-30 02:43:43 +0000192 // GNU extension, 128-bit integers.
193 InitBuiltinType(Int128Ty, BuiltinType::Int128);
194 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
195
Chris Lattner007cb022009-02-26 23:43:47 +0000196 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
197 InitBuiltinType(WCharTy, BuiltinType::WChar);
198 else // C99
199 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000200
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000201 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
202 InitBuiltinType(Char16Ty, BuiltinType::Char16);
203 else // C99
204 Char16Ty = getFromTargetType(Target.getChar16Type());
205
206 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
207 InitBuiltinType(Char32Ty, BuiltinType::Char32);
208 else // C99
209 Char32Ty = getFromTargetType(Target.getChar32Type());
210
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000211 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000212 InitBuiltinType(OverloadTy, BuiltinType::Overload);
213
214 // Placeholder type for type-dependent expressions whose type is
215 // completely unknown. No code should ever check a type against
216 // DependentTy and users should never see it; however, it is here to
217 // help diagnose failures to properly check for type-dependent
218 // expressions.
219 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000220
Mike Stump11289f42009-09-09 15:08:12 +0000221 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000222 // not yet been deduced.
223 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000224
Chris Lattner970e54e2006-11-12 00:37:36 +0000225 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000226 FloatComplexTy = getComplexType(FloatTy);
227 DoubleComplexTy = getComplexType(DoubleTy);
228 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000229
Steve Naroff66e9f332007-10-15 14:41:52 +0000230 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000231
Steve Naroff1329fa02009-07-15 18:40:39 +0000232 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
233 ObjCIdTypedefType = QualType();
234 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000235 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000236
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000237 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000238 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
239 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000240 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000241
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000242 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000243
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000244 // void * type
245 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000246
247 // nullptr type (C++0x 2.14.7)
248 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000249}
250
Douglas Gregor86d142a2009-10-08 07:24:58 +0000251MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000252ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000253 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000254 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000255 = InstantiatedFromStaticDataMember.find(Var);
256 if (Pos == InstantiatedFromStaticDataMember.end())
257 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000258
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000259 return Pos->second;
260}
261
Mike Stump11289f42009-09-09 15:08:12 +0000262void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000263ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
264 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000265 assert(Inst->isStaticDataMember() && "Not a static data member");
266 assert(Tmpl->isStaticDataMember() && "Not a static data member");
267 assert(!InstantiatedFromStaticDataMember[Inst] &&
268 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000269 InstantiatedFromStaticDataMember[Inst]
270 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000271}
272
John McCalle61f2ba2009-11-18 02:36:19 +0000273NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000274ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000275 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000276 = InstantiatedFromUsingDecl.find(UUD);
277 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000278 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000279
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000280 return Pos->second;
281}
282
283void
John McCallb96ec562009-12-04 22:46:56 +0000284ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
285 assert((isa<UsingDecl>(Pattern) ||
286 isa<UnresolvedUsingValueDecl>(Pattern) ||
287 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
288 "pattern decl is not a using decl");
289 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
290 InstantiatedFromUsingDecl[Inst] = Pattern;
291}
292
293UsingShadowDecl *
294ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
295 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
296 = InstantiatedFromUsingShadowDecl.find(Inst);
297 if (Pos == InstantiatedFromUsingShadowDecl.end())
298 return 0;
299
300 return Pos->second;
301}
302
303void
304ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
305 UsingShadowDecl *Pattern) {
306 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
307 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000308}
309
Anders Carlsson5da84842009-09-01 04:26:58 +0000310FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
311 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
312 = InstantiatedFromUnnamedFieldDecl.find(Field);
313 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
314 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000315
Anders Carlsson5da84842009-09-01 04:26:58 +0000316 return Pos->second;
317}
318
319void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
320 FieldDecl *Tmpl) {
321 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
322 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
323 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
324 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000325
Anders Carlsson5da84842009-09-01 04:26:58 +0000326 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
327}
328
Douglas Gregor832940b2010-03-02 23:58:15 +0000329CXXMethodVector::iterator CXXMethodVector::begin() const {
330 if ((Storage & 0x01) == 0)
331 return reinterpret_cast<iterator>(&Storage);
332
333 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
334 return &Vec->front();
335}
336
337CXXMethodVector::iterator CXXMethodVector::end() const {
338 if ((Storage & 0x01) == 0) {
339 if (Storage == 0)
340 return reinterpret_cast<iterator>(&Storage);
341
342 return reinterpret_cast<iterator>(&Storage) + 1;
343 }
344
345 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
346 return &Vec->front() + Vec->size();
347}
348
349void CXXMethodVector::push_back(const CXXMethodDecl *Method) {
350 if (Storage == 0) {
351 // 0 -> 1 element.
352 Storage = reinterpret_cast<uintptr_t>(Method);
353 return;
354 }
355
356 vector_type *Vec;
357 if ((Storage & 0x01) == 0) {
358 // 1 -> 2 elements. Allocate a new vector and push the element into that
359 // vector.
360 Vec = new vector_type;
361 Vec->push_back(reinterpret_cast<const CXXMethodDecl *>(Storage));
362 Storage = reinterpret_cast<uintptr_t>(Vec) | 0x01;
363 } else
364 Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
365
366 // Add the new method to the vector.
367 Vec->push_back(Method);
368}
369
370void CXXMethodVector::Destroy() {
371 if (Storage & 0x01)
372 delete reinterpret_cast<vector_type *>(Storage & ~0x01);
373
374 Storage = 0;
375}
376
377
378ASTContext::overridden_cxx_method_iterator
379ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
380 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
381 = OverriddenMethods.find(Method);
382 if (Pos == OverriddenMethods.end())
383 return 0;
384
385 return Pos->second.begin();
386}
387
388ASTContext::overridden_cxx_method_iterator
389ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
390 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
391 = OverriddenMethods.find(Method);
392 if (Pos == OverriddenMethods.end())
393 return 0;
394
395 return Pos->second.end();
396}
397
398void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
399 const CXXMethodDecl *Overridden) {
400 OverriddenMethods[Method].push_back(Overridden);
401}
402
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000403namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000404 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000405 : std::binary_function<SourceRange, SourceRange, bool> {
406 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000407
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000408 public:
409 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000410
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000411 bool operator()(SourceRange X, SourceRange Y) {
412 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
413 }
414 };
415}
416
Chris Lattner53cfe802007-07-18 17:52:12 +0000417//===----------------------------------------------------------------------===//
418// Type Sizing and Analysis
419//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000420
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000421/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
422/// scalar floating point type.
423const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000424 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000425 assert(BT && "Not a floating point type!");
426 switch (BT->getKind()) {
427 default: assert(0 && "Not a floating point type!");
428 case BuiltinType::Float: return Target.getFloatFormat();
429 case BuiltinType::Double: return Target.getDoubleFormat();
430 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
431 }
432}
433
Ken Dyck160146e2010-01-27 17:10:57 +0000434/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000435/// specified decl. Note that bitfields do not have a valid alignment, so
436/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000437/// If @p RefAsPointee, references are treated like their underlying type
438/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000439CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000440 unsigned Align = Target.getCharWidth();
441
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000442 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000443 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedman19a546c2009-02-22 02:56:25 +0000444
Chris Lattner68061312009-01-24 21:53:27 +0000445 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
446 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000447 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000448 if (RefAsPointee)
449 T = RT->getPointeeType();
450 else
451 T = getPointerType(RT->getPointeeType());
452 }
453 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000454 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000455 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
456 T = cast<ArrayType>(T)->getElementType();
457
458 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
459 }
Charles Davis3fc51072010-02-23 04:52:00 +0000460 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
461 // In the case of a field in a packed struct, we want the minimum
462 // of the alignment of the field and the alignment of the struct.
463 Align = std::min(Align,
464 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
465 }
Chris Lattner68061312009-01-24 21:53:27 +0000466 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000467
Ken Dyck160146e2010-01-27 17:10:57 +0000468 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000469}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000470
Chris Lattner983a8bb2007-07-13 22:13:22 +0000471/// getTypeSize - Return the size of the specified type, in bits. This method
472/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000473///
474/// FIXME: Pointers into different addr spaces could have different sizes and
475/// alignment requirements: getPointerInfo should take an AddrSpace, this
476/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000477std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000478ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000479 uint64_t Width=0;
480 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000481 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000482#define TYPE(Class, Base)
483#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000484#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000485#define DEPENDENT_TYPE(Class, Base) case Type::Class:
486#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000487 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000488 break;
489
Chris Lattner355332d2007-07-13 22:27:08 +0000490 case Type::FunctionNoProto:
491 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000492 // GCC extension: alignof(function) = 32 bits
493 Width = 0;
494 Align = 32;
495 break;
496
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000497 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000498 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000499 Width = 0;
500 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
501 break;
502
Steve Naroff5c131802007-08-30 01:06:46 +0000503 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000504 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattner37e05872008-03-05 18:54:05 +0000506 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000507 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000508 Align = EltInfo.second;
509 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000510 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000511 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000512 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000513 const VectorType *VT = cast<VectorType>(T);
514 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
515 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000516 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000517 // If the alignment is not a power of 2, round up to the next power of 2.
518 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000519 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000520 Align = llvm::NextPowerOf2(Align);
521 Width = llvm::RoundUpToAlignment(Width, Align);
522 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000523 break;
524 }
Chris Lattner647fb222007-07-18 18:26:58 +0000525
Chris Lattner7570e9c2008-03-08 08:52:55 +0000526 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000527 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000528 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000529 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000530 // GCC extension: alignof(void) = 8 bits.
531 Width = 0;
532 Align = 8;
533 break;
534
Chris Lattner6a4f7452007-12-19 19:23:28 +0000535 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000536 Width = Target.getBoolWidth();
537 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000538 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000539 case BuiltinType::Char_S:
540 case BuiltinType::Char_U:
541 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000542 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000543 Width = Target.getCharWidth();
544 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000545 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000546 case BuiltinType::WChar:
547 Width = Target.getWCharWidth();
548 Align = Target.getWCharAlign();
549 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000550 case BuiltinType::Char16:
551 Width = Target.getChar16Width();
552 Align = Target.getChar16Align();
553 break;
554 case BuiltinType::Char32:
555 Width = Target.getChar32Width();
556 Align = Target.getChar32Align();
557 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000558 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000559 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000560 Width = Target.getShortWidth();
561 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000562 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000563 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000564 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000565 Width = Target.getIntWidth();
566 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000567 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000568 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000569 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000570 Width = Target.getLongWidth();
571 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000572 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000573 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000574 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000575 Width = Target.getLongLongWidth();
576 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000577 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000578 case BuiltinType::Int128:
579 case BuiltinType::UInt128:
580 Width = 128;
581 Align = 128; // int128_t is 128-bit aligned on all targets.
582 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000583 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000584 Width = Target.getFloatWidth();
585 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000586 break;
587 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000588 Width = Target.getDoubleWidth();
589 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000590 break;
591 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000592 Width = Target.getLongDoubleWidth();
593 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000594 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000595 case BuiltinType::NullPtr:
596 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
597 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000598 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000599 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000600 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000601 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000602 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000603 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000604 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000605 case Type::BlockPointer: {
606 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
607 Width = Target.getPointerWidth(AS);
608 Align = Target.getPointerAlign(AS);
609 break;
610 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000611 case Type::LValueReference:
612 case Type::RValueReference: {
613 // alignof and sizeof should never enter this code path here, so we go
614 // the pointer route.
615 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
616 Width = Target.getPointerWidth(AS);
617 Align = Target.getPointerAlign(AS);
618 break;
619 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000620 case Type::Pointer: {
621 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000622 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000623 Align = Target.getPointerAlign(AS);
624 break;
625 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000626 case Type::MemberPointer: {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000627 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000628 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000629 getTypeInfo(getPointerDiffType());
630 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000631 if (Pointee->isFunctionType())
632 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000633 Align = PtrDiffInfo.second;
634 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000635 }
Chris Lattner647fb222007-07-18 18:26:58 +0000636 case Type::Complex: {
637 // Complex types have the same alignment as their elements, but twice the
638 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000639 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000640 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000641 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000642 Align = EltInfo.second;
643 break;
644 }
Devang Pateldbb72632008-06-04 21:54:36 +0000645 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000646 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000647 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
648 Width = Layout.getSize();
649 Align = Layout.getAlignment();
650 break;
651 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000652 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000653 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000654 const TagType *TT = cast<TagType>(T);
655
656 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000657 Width = 1;
658 Align = 1;
659 break;
660 }
Mike Stump11289f42009-09-09 15:08:12 +0000661
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000662 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000663 return getTypeInfo(ET->getDecl()->getIntegerType());
664
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000665 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000666 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
667 Width = Layout.getSize();
668 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000669 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000670 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000671
Chris Lattner63d2b362009-10-22 05:17:15 +0000672 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000673 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
674 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000675
Chris Lattner63d2b362009-10-22 05:17:15 +0000676 case Type::Elaborated:
677 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
678 .getTypePtr());
John McCallfcc33b02009-09-05 00:15:47 +0000679
Douglas Gregoref462e62009-04-30 17:32:17 +0000680 case Type::Typedef: {
681 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000682 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000683 Align = std::max(Aligned->getMaxAlignment(),
684 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregoref462e62009-04-30 17:32:17 +0000685 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
686 } else
687 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000688 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000689 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000690
691 case Type::TypeOfExpr:
692 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
693 .getTypePtr());
694
695 case Type::TypeOf:
696 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
697
Anders Carlsson81df7b82009-06-24 19:06:50 +0000698 case Type::Decltype:
699 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
700 .getTypePtr());
701
Douglas Gregoref462e62009-04-30 17:32:17 +0000702 case Type::QualifiedName:
703 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000704
John McCalle78aac42010-03-10 03:28:59 +0000705 case Type::InjectedClassName:
706 return getTypeInfo(cast<InjectedClassNameType>(T)
707 ->getUnderlyingType().getTypePtr());
708
Douglas Gregoref462e62009-04-30 17:32:17 +0000709 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000710 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000711 "Cannot request the size of a dependent type");
712 // FIXME: this is likely to be wrong once we support template
713 // aliases, since a template alias could refer to a typedef that
714 // has an __aligned__ attribute on it.
715 return getTypeInfo(getCanonicalType(T));
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattner53cfe802007-07-18 17:52:12 +0000718 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000719 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000720}
721
Ken Dyck8c89d592009-12-22 14:23:30 +0000722/// getTypeSizeInChars - Return the size of the specified type, in characters.
723/// This method does not work on incomplete types.
724CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000725 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000726}
727CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000728 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000729}
730
Ken Dycka6046ab2010-01-26 17:25:18 +0000731/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000732/// characters. This method does not work on incomplete types.
733CharUnits ASTContext::getTypeAlignInChars(QualType T) {
734 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
735}
736CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
737 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
738}
739
Chris Lattnera3402cd2009-01-27 18:08:34 +0000740/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
741/// type for the current target in bits. This can be different than the ABI
742/// alignment in cases where it is beneficial for performance to overalign
743/// a data type.
744unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
745 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000746
747 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000748 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000749 T = CT->getElementType().getTypePtr();
750 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
751 T->isSpecificBuiltinType(BuiltinType::LongLong))
752 return std::max(ABIAlign, (unsigned)getTypeSize(T));
753
Chris Lattnera3402cd2009-01-27 18:08:34 +0000754 return ABIAlign;
755}
756
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000757static void CollectLocalObjCIvars(ASTContext *Ctx,
758 const ObjCInterfaceDecl *OI,
759 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000760 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
761 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000762 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000763 if (!IVDecl->isInvalidDecl())
764 Fields.push_back(cast<FieldDecl>(IVDecl));
765 }
766}
767
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000768void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
769 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
770 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
771 CollectObjCIvars(SuperClass, Fields);
772 CollectLocalObjCIvars(this, OI, Fields);
773}
774
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000775/// ShallowCollectObjCIvars -
776/// Collect all ivars, including those synthesized, in the current class.
777///
778void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000779 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000780 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
781 E = OI->ivar_end(); I != E; ++I) {
782 Ivars.push_back(*I);
783 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000784
785 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000786}
787
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000788/// CollectNonClassIvars -
789/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000790/// This includes synthesized ivars (via @synthesize) and those in
791// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000792///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000793void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000794 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000795 // Find ivars declared in class extension.
796 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
797 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
798 E = CDecl->ivar_end(); I != E; ++I) {
799 Ivars.push_back(*I);
800 }
801 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000802
Ted Kremenek86838aa2010-03-11 19:44:54 +0000803 // Also add any ivar defined in this class's implementation. This
804 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000805 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
806 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
807 E = ImplDecl->ivar_end(); I != E; ++I)
808 Ivars.push_back(*I);
809 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000810}
811
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000812/// CollectInheritedProtocols - Collect all protocols in current class and
813/// those inherited by it.
814void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000815 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000816 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
817 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
818 PE = OI->protocol_end(); P != PE; ++P) {
819 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000820 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000821 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000822 PE = Proto->protocol_end(); P != PE; ++P) {
823 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000824 CollectInheritedProtocols(*P, Protocols);
825 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000826 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000827
828 // Categories of this Interface.
829 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
830 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
831 CollectInheritedProtocols(CDeclChain, Protocols);
832 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
833 while (SD) {
834 CollectInheritedProtocols(SD, Protocols);
835 SD = SD->getSuperClass();
836 }
837 return;
838 }
839 if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
840 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
841 PE = OC->protocol_end(); P != PE; ++P) {
842 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000843 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000844 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
845 PE = Proto->protocol_end(); P != PE; ++P)
846 CollectInheritedProtocols(*P, Protocols);
847 }
848 return;
849 }
850 if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
851 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
852 PE = OP->protocol_end(); P != PE; ++P) {
853 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000854 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000855 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
856 PE = Proto->protocol_end(); P != PE; ++P)
857 CollectInheritedProtocols(*P, Protocols);
858 }
859 return;
860 }
861}
862
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000863unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
864 unsigned count = 0;
865 // Count ivars declared in class extension.
866 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
867 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
868 E = CDecl->ivar_end(); I != E; ++I) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000869 ++count;
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000870 }
871 }
872
873 // Count ivar defined in this class's implementation. This
874 // includes synthesized ivars.
875 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
876 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
877 E = ImplDecl->ivar_end(); I != E; ++I)
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000878 ++count;
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000879 return count;
880}
881
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000882/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
883ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
884 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
885 I = ObjCImpls.find(D);
886 if (I != ObjCImpls.end())
887 return cast<ObjCImplementationDecl>(I->second);
888 return 0;
889}
890/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
891ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
892 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
893 I = ObjCImpls.find(D);
894 if (I != ObjCImpls.end())
895 return cast<ObjCCategoryImplDecl>(I->second);
896 return 0;
897}
898
899/// \brief Set the implementation of ObjCInterfaceDecl.
900void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
901 ObjCImplementationDecl *ImplD) {
902 assert(IFaceD && ImplD && "Passed null params");
903 ObjCImpls[IFaceD] = ImplD;
904}
905/// \brief Set the implementation of ObjCCategoryDecl.
906void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
907 ObjCCategoryImplDecl *ImplD) {
908 assert(CatD && ImplD && "Passed null params");
909 ObjCImpls[CatD] = ImplD;
910}
911
John McCallbcd03502009-12-07 02:54:59 +0000912/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000913///
John McCallbcd03502009-12-07 02:54:59 +0000914/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000915/// the TypeLoc wrappers.
916///
917/// \param T the type that will be the basis for type source info. This type
918/// should refer to how the declarator was written in source code, not to
919/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000920TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000921 unsigned DataSize) {
922 if (!DataSize)
923 DataSize = TypeLoc::getFullDataSizeForType(T);
924 else
925 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000926 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000927
John McCallbcd03502009-12-07 02:54:59 +0000928 TypeSourceInfo *TInfo =
929 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
930 new (TInfo) TypeSourceInfo(T);
931 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000932}
933
John McCallbcd03502009-12-07 02:54:59 +0000934TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000935 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +0000936 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +0000937 DI->getTypeLoc().initialize(L);
938 return DI;
939}
940
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000941/// getInterfaceLayoutImpl - Get or compute information about the
942/// layout of the given interface.
943///
944/// \param Impl - If given, also include the layout of the interface's
945/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000946const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000947ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
948 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000949 assert(!D->isForwardDecl() && "Invalid interface decl!");
950
Devang Pateldbb72632008-06-04 21:54:36 +0000951 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000952 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000953 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
954 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
955 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000956
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000957 // Add in synthesized ivar count if laying out an implementation.
958 if (Impl) {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000959 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000960 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000961 // entry. Note we can't cache this because we simply free all
962 // entries later; however we shouldn't look up implementations
963 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000964 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000965 return getObjCLayout(D, 0);
966 }
967
Mike Stump11289f42009-09-09 15:08:12 +0000968 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000969 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
970 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000971
Devang Pateldbb72632008-06-04 21:54:36 +0000972 return *NewEntry;
973}
974
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000975const ASTRecordLayout &
976ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
977 return getObjCLayout(D, 0);
978}
979
980const ASTRecordLayout &
981ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
982 return getObjCLayout(D->getClassInterface(), D);
983}
984
Devang Patele11664a2007-11-01 19:11:01 +0000985/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +0000986/// specified record (struct/union/class), which indicates its size and field
987/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +0000988const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000989 D = D->getDefinition();
Ted Kremenek21475702008-09-05 17:16:31 +0000990 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +0000991
Chris Lattner53cfe802007-07-18 17:52:12 +0000992 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +0000993 // Note that we can't save a reference to the entry because this function
994 // is recursive.
995 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +0000996 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +0000997
Mike Stump11289f42009-09-09 15:08:12 +0000998 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000999 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +00001000 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +00001001
Daniel Dunbarccabe482010-04-19 20:44:53 +00001002 if (getLangOptions().DumpRecordLayouts) {
1003 llvm::errs() << "\n*** Dumping AST Record Layout\n";
1004 DumpRecordLayout(D, llvm::errs());
1005 }
1006
Chris Lattner647fb222007-07-18 18:26:58 +00001007 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +00001008}
1009
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001010const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001011 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001012 assert(RD && "Cannot get key function for forward declarations!");
1013
1014 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1015 if (!Entry)
1016 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
1017 else
1018 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
1019 "Key function changed!");
1020
1021 return Entry;
1022}
1023
Chris Lattner983a8bb2007-07-13 22:13:22 +00001024//===----------------------------------------------------------------------===//
1025// Type creation/memoization methods
1026//===----------------------------------------------------------------------===//
1027
John McCall8ccfcb52009-09-24 19:53:00 +00001028QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1029 unsigned Fast = Quals.getFastQualifiers();
1030 Quals.removeFastQualifiers();
1031
1032 // Check if we've already instantiated this type.
1033 llvm::FoldingSetNodeID ID;
1034 ExtQuals::Profile(ID, TypeNode, Quals);
1035 void *InsertPos = 0;
1036 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1037 assert(EQ->getQualifiers() == Quals);
1038 QualType T = QualType(EQ, Fast);
1039 return T;
1040 }
1041
John McCall90d1c2d2009-09-24 23:30:46 +00001042 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001043 ExtQualNodes.InsertNode(New, InsertPos);
1044 QualType T = QualType(New, Fast);
1045 return T;
1046}
1047
1048QualType ASTContext::getVolatileType(QualType T) {
1049 QualType CanT = getCanonicalType(T);
1050 if (CanT.isVolatileQualified()) return T;
1051
1052 QualifierCollector Quals;
1053 const Type *TypeNode = Quals.strip(T);
1054 Quals.addVolatile();
1055
1056 return getExtQualType(TypeNode, Quals);
1057}
1058
Fariborz Jahanianece85822009-02-17 18:27:45 +00001059QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001060 QualType CanT = getCanonicalType(T);
1061 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001062 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001063
John McCall8ccfcb52009-09-24 19:53:00 +00001064 // If we are composing extended qualifiers together, merge together
1065 // into one ExtQuals node.
1066 QualifierCollector Quals;
1067 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001068
John McCall8ccfcb52009-09-24 19:53:00 +00001069 // If this type already has an address space specified, it cannot get
1070 // another one.
1071 assert(!Quals.hasAddressSpace() &&
1072 "Type cannot be in multiple addr spaces!");
1073 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001074
John McCall8ccfcb52009-09-24 19:53:00 +00001075 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001076}
1077
Chris Lattnerd60183d2009-02-18 22:53:11 +00001078QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001079 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001080 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001081 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001082 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001083
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001084 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001085 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001086 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001087 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1088 return getPointerType(ResultType);
1089 }
1090 }
Mike Stump11289f42009-09-09 15:08:12 +00001091
John McCall8ccfcb52009-09-24 19:53:00 +00001092 // If we are composing extended qualifiers together, merge together
1093 // into one ExtQuals node.
1094 QualifierCollector Quals;
1095 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001096
John McCall8ccfcb52009-09-24 19:53:00 +00001097 // If this type already has an ObjCGC specified, it cannot get
1098 // another one.
1099 assert(!Quals.hasObjCGCAttr() &&
1100 "Type cannot have multiple ObjCGCs!");
1101 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001102
John McCall8ccfcb52009-09-24 19:53:00 +00001103 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001104}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001105
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001106static QualType getExtFunctionType(ASTContext& Context, QualType T,
1107 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001108 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001109 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1110 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001111 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001112 if (ResultType == Pointee)
1113 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001114
1115 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001116 } else if (const BlockPointerType *BlockPointer
1117 = T->getAs<BlockPointerType>()) {
1118 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001119 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001120 if (ResultType == Pointee)
1121 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001122
1123 ResultType = Context.getBlockPointerType(ResultType);
1124 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001125 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001126 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001127
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001128 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001129 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001130 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001131 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001132 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001133 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001134 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1135 FPT->getNumArgs(), FPT->isVariadic(),
1136 FPT->getTypeQuals(),
1137 FPT->hasExceptionSpec(),
1138 FPT->hasAnyExceptionSpec(),
1139 FPT->getNumExceptions(),
1140 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001141 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001142 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001143 } else
1144 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001145
1146 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1147}
1148
1149QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001150 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001151 return getExtFunctionType(*this, T,
1152 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001153}
1154
1155QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001156 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001157 return getExtFunctionType(*this, T,
1158 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001159}
1160
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001161QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1162 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1163 return getExtFunctionType(*this, T,
1164 Info.withRegParm(RegParm));
1165}
1166
Chris Lattnerc6395932007-06-22 20:56:16 +00001167/// getComplexType - Return the uniqued reference to the type for a complex
1168/// number with the specified element type.
1169QualType ASTContext::getComplexType(QualType T) {
1170 // Unique pointers, to guarantee there is only one pointer of a particular
1171 // structure.
1172 llvm::FoldingSetNodeID ID;
1173 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattnerc6395932007-06-22 20:56:16 +00001175 void *InsertPos = 0;
1176 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1177 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Chris Lattnerc6395932007-06-22 20:56:16 +00001179 // If the pointee type isn't canonical, this won't be a canonical type either,
1180 // so fill in the canonical type field.
1181 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001182 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001183 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001184
Chris Lattnerc6395932007-06-22 20:56:16 +00001185 // Get the new insert position for the node we care about.
1186 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001187 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001188 }
John McCall90d1c2d2009-09-24 23:30:46 +00001189 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001190 Types.push_back(New);
1191 ComplexTypes.InsertNode(New, InsertPos);
1192 return QualType(New, 0);
1193}
1194
Chris Lattner970e54e2006-11-12 00:37:36 +00001195/// getPointerType - Return the uniqued reference to the type for a pointer to
1196/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001197QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001198 // Unique pointers, to guarantee there is only one pointer of a particular
1199 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001200 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001201 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001202
Chris Lattner67521df2007-01-27 01:29:36 +00001203 void *InsertPos = 0;
1204 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001205 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001206
Chris Lattner7ccecb92006-11-12 08:50:50 +00001207 // If the pointee type isn't canonical, this won't be a canonical type either,
1208 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001209 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001210 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001211 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001212
Chris Lattner67521df2007-01-27 01:29:36 +00001213 // Get the new insert position for the node we care about.
1214 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001215 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001216 }
John McCall90d1c2d2009-09-24 23:30:46 +00001217 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001218 Types.push_back(New);
1219 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001220 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001221}
1222
Mike Stump11289f42009-09-09 15:08:12 +00001223/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001224/// a pointer to the specified block.
1225QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001226 assert(T->isFunctionType() && "block of function types only");
1227 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001228 // structure.
1229 llvm::FoldingSetNodeID ID;
1230 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001231
Steve Naroffec33ed92008-08-27 16:04:49 +00001232 void *InsertPos = 0;
1233 if (BlockPointerType *PT =
1234 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1235 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001236
1237 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001238 // type either so fill in the canonical type field.
1239 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001240 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001241 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001242
Steve Naroffec33ed92008-08-27 16:04:49 +00001243 // Get the new insert position for the node we care about.
1244 BlockPointerType *NewIP =
1245 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001246 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001247 }
John McCall90d1c2d2009-09-24 23:30:46 +00001248 BlockPointerType *New
1249 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001250 Types.push_back(New);
1251 BlockPointerTypes.InsertNode(New, InsertPos);
1252 return QualType(New, 0);
1253}
1254
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001255/// getLValueReferenceType - Return the uniqued reference to the type for an
1256/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001257QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001258 // Unique pointers, to guarantee there is only one pointer of a particular
1259 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001260 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001261 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001262
1263 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001264 if (LValueReferenceType *RT =
1265 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001266 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001267
John McCallfc93cf92009-10-22 22:37:11 +00001268 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1269
Bill Wendling3708c182007-05-27 10:15:43 +00001270 // If the referencee type isn't canonical, this won't be a canonical type
1271 // either, so fill in the canonical type field.
1272 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001273 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1274 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1275 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001276
Bill Wendling3708c182007-05-27 10:15:43 +00001277 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001278 LValueReferenceType *NewIP =
1279 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001280 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001281 }
1282
John McCall90d1c2d2009-09-24 23:30:46 +00001283 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001284 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1285 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001286 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001287 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001288
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001289 return QualType(New, 0);
1290}
1291
1292/// getRValueReferenceType - Return the uniqued reference to the type for an
1293/// rvalue reference to the specified type.
1294QualType ASTContext::getRValueReferenceType(QualType T) {
1295 // Unique pointers, to guarantee there is only one pointer of a particular
1296 // structure.
1297 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001298 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001299
1300 void *InsertPos = 0;
1301 if (RValueReferenceType *RT =
1302 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1303 return QualType(RT, 0);
1304
John McCallfc93cf92009-10-22 22:37:11 +00001305 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1306
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001307 // If the referencee type isn't canonical, this won't be a canonical type
1308 // either, so fill in the canonical type field.
1309 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001310 if (InnerRef || !T.isCanonical()) {
1311 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1312 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001313
1314 // Get the new insert position for the node we care about.
1315 RValueReferenceType *NewIP =
1316 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1317 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1318 }
1319
John McCall90d1c2d2009-09-24 23:30:46 +00001320 RValueReferenceType *New
1321 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001322 Types.push_back(New);
1323 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001324 return QualType(New, 0);
1325}
1326
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001327/// getMemberPointerType - Return the uniqued reference to the type for a
1328/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001329QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001330 // Unique pointers, to guarantee there is only one pointer of a particular
1331 // structure.
1332 llvm::FoldingSetNodeID ID;
1333 MemberPointerType::Profile(ID, T, Cls);
1334
1335 void *InsertPos = 0;
1336 if (MemberPointerType *PT =
1337 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1338 return QualType(PT, 0);
1339
1340 // If the pointee or class type isn't canonical, this won't be a canonical
1341 // type either, so fill in the canonical type field.
1342 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001343 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001344 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1345
1346 // Get the new insert position for the node we care about.
1347 MemberPointerType *NewIP =
1348 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1349 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1350 }
John McCall90d1c2d2009-09-24 23:30:46 +00001351 MemberPointerType *New
1352 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001353 Types.push_back(New);
1354 MemberPointerTypes.InsertNode(New, InsertPos);
1355 return QualType(New, 0);
1356}
1357
Mike Stump11289f42009-09-09 15:08:12 +00001358/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001359/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001360QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001361 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001362 ArrayType::ArraySizeModifier ASM,
1363 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001364 assert((EltTy->isDependentType() ||
1365 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001366 "Constant array of VLAs is illegal!");
1367
Chris Lattnere2df3f92009-05-13 04:12:56 +00001368 // Convert the array size into a canonical width matching the pointer size for
1369 // the target.
1370 llvm::APInt ArySize(ArySizeIn);
1371 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001372
Chris Lattner23b7eb62007-06-15 23:05:46 +00001373 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001374 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001375
Chris Lattner36f8e652007-01-27 08:31:04 +00001376 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001377 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001378 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001379 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001380
Chris Lattner7ccecb92006-11-12 08:50:50 +00001381 // If the element type isn't canonical, this won't be a canonical type either,
1382 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001383 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001384 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001385 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001386 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001387 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001388 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001389 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001390 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001391 }
Mike Stump11289f42009-09-09 15:08:12 +00001392
John McCall90d1c2d2009-09-24 23:30:46 +00001393 ConstantArrayType *New = new(*this,TypeAlignment)
1394 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001395 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001396 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001397 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001398}
1399
Steve Naroffcadebd02007-08-30 18:14:25 +00001400/// getVariableArrayType - Returns a non-unique reference to the type for a
1401/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001402QualType ASTContext::getVariableArrayType(QualType EltTy,
1403 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001404 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001405 unsigned EltTypeQuals,
1406 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001407 // Since we don't unique expressions, it isn't possible to unique VLA's
1408 // that have an expression provided for their size.
1409
John McCall90d1c2d2009-09-24 23:30:46 +00001410 VariableArrayType *New = new(*this, TypeAlignment)
1411 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001412
1413 VariableArrayTypes.push_back(New);
1414 Types.push_back(New);
1415 return QualType(New, 0);
1416}
1417
Douglas Gregor4619e432008-12-05 23:32:09 +00001418/// getDependentSizedArrayType - Returns a non-unique reference to
1419/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001420/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001421QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1422 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001423 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001424 unsigned EltTypeQuals,
1425 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001426 assert((!NumElts || NumElts->isTypeDependent() ||
1427 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001428 "Size must be type- or value-dependent!");
1429
Douglas Gregorf3f95522009-07-31 00:23:35 +00001430 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001431 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001432 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001433
1434 if (NumElts) {
1435 // Dependently-sized array types that do not have a specified
1436 // number of elements will have their sizes deduced from an
1437 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001438 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1439 EltTypeQuals, NumElts);
1440
1441 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1442 }
1443
Douglas Gregorf3f95522009-07-31 00:23:35 +00001444 DependentSizedArrayType *New;
1445 if (Canon) {
1446 // We already have a canonical version of this array type; use it as
1447 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001448 New = new (*this, TypeAlignment)
1449 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1450 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001451 } else {
1452 QualType CanonEltTy = getCanonicalType(EltTy);
1453 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001454 New = new (*this, TypeAlignment)
1455 DependentSizedArrayType(*this, EltTy, QualType(),
1456 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001457
Douglas Gregorc42075a2010-02-04 18:10:26 +00001458 if (NumElts) {
1459 DependentSizedArrayType *CanonCheck
1460 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1461 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1462 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001463 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001464 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001465 } else {
1466 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1467 ASM, EltTypeQuals,
1468 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001469 New = new (*this, TypeAlignment)
1470 DependentSizedArrayType(*this, EltTy, Canon,
1471 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001472 }
1473 }
Mike Stump11289f42009-09-09 15:08:12 +00001474
Douglas Gregor4619e432008-12-05 23:32:09 +00001475 Types.push_back(New);
1476 return QualType(New, 0);
1477}
1478
Eli Friedmanbd258282008-02-15 18:16:39 +00001479QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1480 ArrayType::ArraySizeModifier ASM,
1481 unsigned EltTypeQuals) {
1482 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001483 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001484
1485 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001486 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001487 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1488 return QualType(ATP, 0);
1489
1490 // If the element type isn't canonical, this won't be a canonical type
1491 // either, so fill in the canonical type field.
1492 QualType Canonical;
1493
John McCallb692a092009-10-22 20:10:53 +00001494 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001495 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001496 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001497
1498 // Get the new insert position for the node we care about.
1499 IncompleteArrayType *NewIP =
1500 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001501 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001502 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001503
John McCall90d1c2d2009-09-24 23:30:46 +00001504 IncompleteArrayType *New = new (*this, TypeAlignment)
1505 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001506
1507 IncompleteArrayTypes.InsertNode(New, InsertPos);
1508 Types.push_back(New);
1509 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001510}
1511
Steve Naroff91fcddb2007-07-18 18:00:27 +00001512/// getVectorType - Return the unique reference to a vector type of
1513/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001514QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1515 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001516 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001517
Chris Lattner76a00cf2008-04-06 22:59:24 +00001518 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001519 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001520
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001521 // Check if we've already instantiated a vector of this type.
1522 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001523 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1524 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001525 void *InsertPos = 0;
1526 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1527 return QualType(VTP, 0);
1528
1529 // If the element type isn't canonical, this won't be a canonical type either,
1530 // so fill in the canonical type field.
1531 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001532 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1533 Canonical = getVectorType(getCanonicalType(vecType),
1534 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001535
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001536 // Get the new insert position for the node we care about.
1537 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001538 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001539 }
John McCall90d1c2d2009-09-24 23:30:46 +00001540 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001541 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001542 VectorTypes.InsertNode(New, InsertPos);
1543 Types.push_back(New);
1544 return QualType(New, 0);
1545}
1546
Nate Begemance4d7fc2008-04-18 23:10:10 +00001547/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001548/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001549QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001550 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001551
Chris Lattner76a00cf2008-04-06 22:59:24 +00001552 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001553 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001554
Steve Naroff91fcddb2007-07-18 18:00:27 +00001555 // Check if we've already instantiated a vector of this type.
1556 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001557 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001558 void *InsertPos = 0;
1559 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1560 return QualType(VTP, 0);
1561
1562 // If the element type isn't canonical, this won't be a canonical type either,
1563 // so fill in the canonical type field.
1564 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001565 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001566 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001567
Steve Naroff91fcddb2007-07-18 18:00:27 +00001568 // Get the new insert position for the node we care about.
1569 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001570 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001571 }
John McCall90d1c2d2009-09-24 23:30:46 +00001572 ExtVectorType *New = new (*this, TypeAlignment)
1573 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001574 VectorTypes.InsertNode(New, InsertPos);
1575 Types.push_back(New);
1576 return QualType(New, 0);
1577}
1578
Mike Stump11289f42009-09-09 15:08:12 +00001579QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001580 Expr *SizeExpr,
1581 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001582 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001583 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001584 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001585
Douglas Gregor352169a2009-07-31 03:54:25 +00001586 void *InsertPos = 0;
1587 DependentSizedExtVectorType *Canon
1588 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1589 DependentSizedExtVectorType *New;
1590 if (Canon) {
1591 // We already have a canonical version of this array type; use it as
1592 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001593 New = new (*this, TypeAlignment)
1594 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1595 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001596 } else {
1597 QualType CanonVecTy = getCanonicalType(vecType);
1598 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001599 New = new (*this, TypeAlignment)
1600 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1601 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001602
1603 DependentSizedExtVectorType *CanonCheck
1604 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1605 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1606 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001607 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1608 } else {
1609 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1610 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001611 New = new (*this, TypeAlignment)
1612 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001613 }
1614 }
Mike Stump11289f42009-09-09 15:08:12 +00001615
Douglas Gregor758a8692009-06-17 21:51:59 +00001616 Types.push_back(New);
1617 return QualType(New, 0);
1618}
1619
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001620/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001621///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001622QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1623 const FunctionType::ExtInfo &Info) {
1624 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001625 // Unique functions, to guarantee there is only one function of a particular
1626 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001627 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001628 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001629
Chris Lattner47955de2007-01-27 08:37:20 +00001630 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001631 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001632 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001633 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001634
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001635 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001636 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001637 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001638 Canonical =
1639 getFunctionNoProtoType(getCanonicalType(ResultTy),
1640 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001641
Chris Lattner47955de2007-01-27 08:37:20 +00001642 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001643 FunctionNoProtoType *NewIP =
1644 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001645 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001646 }
Mike Stump11289f42009-09-09 15:08:12 +00001647
John McCall90d1c2d2009-09-24 23:30:46 +00001648 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001649 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001650 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001651 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001652 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001653}
1654
1655/// getFunctionType - Return a normal function type with a typed argument
1656/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001657QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001658 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001659 unsigned TypeQuals, bool hasExceptionSpec,
1660 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001661 const QualType *ExArray,
1662 const FunctionType::ExtInfo &Info) {
1663 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001664 // Unique functions, to guarantee there is only one function of a particular
1665 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001666 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001667 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001668 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001669 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001670
1671 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001672 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001673 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001674 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001675
1676 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001677 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001678 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001679 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001680 isCanonical = false;
1681
1682 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001683 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001684 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001685 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001686 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001687 CanonicalArgs.reserve(NumArgs);
1688 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001689 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001690
Chris Lattner76a00cf2008-04-06 22:59:24 +00001691 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001692 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001693 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001694 false, 0, 0,
1695 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001696
Chris Lattnerfd4de792007-01-27 01:15:32 +00001697 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001698 FunctionProtoType *NewIP =
1699 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001700 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001701 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001702
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001703 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001704 // for two variable size arrays (for parameter and exception types) at the
1705 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001706 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001707 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1708 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001709 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001710 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001711 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001712 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001713 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001714 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001715 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001716}
Chris Lattneref51c202006-11-10 07:17:23 +00001717
John McCalle78aac42010-03-10 03:28:59 +00001718#ifndef NDEBUG
1719static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1720 if (!isa<CXXRecordDecl>(D)) return false;
1721 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1722 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1723 return true;
1724 if (RD->getDescribedClassTemplate() &&
1725 !isa<ClassTemplateSpecializationDecl>(RD))
1726 return true;
1727 return false;
1728}
1729#endif
1730
1731/// getInjectedClassNameType - Return the unique reference to the
1732/// injected class name type for the specified templated declaration.
1733QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1734 QualType TST) {
1735 assert(NeedsInjectedClassNameType(Decl));
1736 if (Decl->TypeForDecl) {
1737 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1738 } else if (CXXRecordDecl *PrevDecl
1739 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1740 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1741 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1742 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1743 } else {
1744 Decl->TypeForDecl = new (*this, TypeAlignment)
1745 InjectedClassNameType(Decl, TST, TST->getCanonicalTypeInternal());
1746 Types.push_back(Decl->TypeForDecl);
1747 }
1748 return QualType(Decl->TypeForDecl, 0);
1749}
1750
Douglas Gregor83a586e2008-04-13 21:07:44 +00001751/// getTypeDeclType - Return the unique reference to the type for the
1752/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001753QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001754 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001755 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001756
John McCall81e38502010-02-16 03:57:14 +00001757 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001758 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001759
1760 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001761 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001762 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001763
John McCall96f0b5f2010-03-10 06:48:02 +00001764 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1765 "Template type parameter types are always available.");
1766
John McCall81e38502010-02-16 03:57:14 +00001767 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001768 assert(!Record->getPreviousDeclaration() &&
1769 "struct/union has previous declaration");
1770 assert(!NeedsInjectedClassNameType(Record));
1771 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001772 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001773 assert(!Enum->getPreviousDeclaration() &&
1774 "enum has previous declaration");
1775 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001776 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001777 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1778 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001779 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001780 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001781
John McCall96f0b5f2010-03-10 06:48:02 +00001782 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001783 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001784}
1785
Chris Lattner32d920b2007-01-26 02:01:53 +00001786/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001787/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001788QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001789 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001790
Chris Lattner76a00cf2008-04-06 22:59:24 +00001791 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001792 Decl->TypeForDecl = new(*this, TypeAlignment)
1793 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001794 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001795 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001796}
1797
John McCallcebee162009-10-18 09:09:24 +00001798/// \brief Retrieve a substitution-result type.
1799QualType
1800ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1801 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001802 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001803 && "replacement types must always be canonical");
1804
1805 llvm::FoldingSetNodeID ID;
1806 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1807 void *InsertPos = 0;
1808 SubstTemplateTypeParmType *SubstParm
1809 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1810
1811 if (!SubstParm) {
1812 SubstParm = new (*this, TypeAlignment)
1813 SubstTemplateTypeParmType(Parm, Replacement);
1814 Types.push_back(SubstParm);
1815 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1816 }
1817
1818 return QualType(SubstParm, 0);
1819}
1820
Douglas Gregoreff93e02009-02-05 23:33:38 +00001821/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001822/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001823/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001824QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001825 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001826 IdentifierInfo *Name) {
1827 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001828 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001829 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001830 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001831 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1832
1833 if (TypeParm)
1834 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001835
Anders Carlsson90036dc2009-06-16 00:30:48 +00001836 if (Name) {
1837 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001838 TypeParm = new (*this, TypeAlignment)
1839 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001840
1841 TemplateTypeParmType *TypeCheck
1842 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1843 assert(!TypeCheck && "Template type parameter canonical type broken");
1844 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001845 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001846 TypeParm = new (*this, TypeAlignment)
1847 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001848
1849 Types.push_back(TypeParm);
1850 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1851
1852 return QualType(TypeParm, 0);
1853}
1854
John McCalle78aac42010-03-10 03:28:59 +00001855TypeSourceInfo *
1856ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1857 SourceLocation NameLoc,
1858 const TemplateArgumentListInfo &Args,
1859 QualType CanonType) {
1860 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1861
1862 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1863 TemplateSpecializationTypeLoc TL
1864 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1865 TL.setTemplateNameLoc(NameLoc);
1866 TL.setLAngleLoc(Args.getLAngleLoc());
1867 TL.setRAngleLoc(Args.getRAngleLoc());
1868 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1869 TL.setArgLocInfo(i, Args[i].getLocInfo());
1870 return DI;
1871}
1872
Mike Stump11289f42009-09-09 15:08:12 +00001873QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001874ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001875 const TemplateArgumentListInfo &Args,
John McCall0ad16662009-10-29 08:12:44 +00001876 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00001877 unsigned NumArgs = Args.size();
1878
John McCall0ad16662009-10-29 08:12:44 +00001879 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1880 ArgVec.reserve(NumArgs);
1881 for (unsigned i = 0; i != NumArgs; ++i)
1882 ArgVec.push_back(Args[i].getArgument());
1883
1884 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon);
1885}
1886
1887QualType
1888ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001889 const TemplateArgument *Args,
1890 unsigned NumArgs,
1891 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001892 if (!Canon.isNull())
1893 Canon = getCanonicalType(Canon);
1894 else {
1895 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001896 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1897 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1898 CanonArgs.reserve(NumArgs);
1899 for (unsigned I = 0; I != NumArgs; ++I)
1900 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1901
1902 // Determine whether this canonical template specialization type already
1903 // exists.
1904 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001905 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor00044172009-07-29 16:09:57 +00001906 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001907
1908 void *InsertPos = 0;
1909 TemplateSpecializationType *Spec
1910 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001911
Douglas Gregora8e02e72009-07-28 23:00:59 +00001912 if (!Spec) {
1913 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001914 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001915 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001916 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001917 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001918 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001919 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001920 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001921 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001922 }
Mike Stump11289f42009-09-09 15:08:12 +00001923
Douglas Gregor15301382009-07-30 17:40:51 +00001924 if (Canon.isNull())
1925 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001926 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001927 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001928 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001929
Douglas Gregora8e02e72009-07-28 23:00:59 +00001930 // Allocate the (non-canonical) template specialization type, but don't
1931 // try to unique it: these types typically have location information that
1932 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001933 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001934 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001935 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001936 TemplateSpecializationType *Spec
1937 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001938 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001939
Douglas Gregor8bf42052009-02-09 18:46:07 +00001940 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001941 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001942}
1943
Mike Stump11289f42009-09-09 15:08:12 +00001944QualType
Douglas Gregorf21eb492009-03-26 23:50:42 +00001945ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor52537682009-03-19 00:18:19 +00001946 QualType NamedType) {
1947 llvm::FoldingSetNodeID ID;
Douglas Gregorf21eb492009-03-26 23:50:42 +00001948 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001949
1950 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001951 QualifiedNameType *T
Douglas Gregor52537682009-03-19 00:18:19 +00001952 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1953 if (T)
1954 return QualType(T, 0);
1955
Douglas Gregorc42075a2010-02-04 18:10:26 +00001956 QualType Canon = NamedType;
1957 if (!Canon.isCanonical()) {
1958 Canon = getCanonicalType(NamedType);
1959 QualifiedNameType *CheckT
1960 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1961 assert(!CheckT && "Qualified name canonical type broken");
1962 (void)CheckT;
1963 }
1964
1965 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001966 Types.push_back(T);
1967 QualifiedNameTypes.InsertNode(T, InsertPos);
1968 return QualType(T, 0);
1969}
1970
Douglas Gregor02085352010-03-31 20:19:30 +00001971QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1972 NestedNameSpecifier *NNS,
1973 const IdentifierInfo *Name,
1974 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001975 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1976
1977 if (Canon.isNull()) {
1978 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001979 ElaboratedTypeKeyword CanonKeyword = Keyword;
1980 if (Keyword == ETK_None)
1981 CanonKeyword = ETK_Typename;
1982
1983 if (CanonNNS != NNS || CanonKeyword != Keyword)
1984 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001985 }
1986
1987 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001988 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001989
1990 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001991 DependentNameType *T
1992 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001993 if (T)
1994 return QualType(T, 0);
1995
Douglas Gregor02085352010-03-31 20:19:30 +00001996 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00001997 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001998 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001999 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002000}
2001
Mike Stump11289f42009-09-09 15:08:12 +00002002QualType
Douglas Gregor02085352010-03-31 20:19:30 +00002003ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2004 NestedNameSpecifier *NNS,
2005 const TemplateSpecializationType *TemplateId,
2006 QualType Canon) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002007 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2008
Douglas Gregorc42075a2010-02-04 18:10:26 +00002009 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002010 DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002011
2012 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002013 DependentNameType *T
2014 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002015 if (T)
2016 return QualType(T, 0);
2017
Douglas Gregordce2b622009-04-01 00:28:59 +00002018 if (Canon.isNull()) {
2019 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2020 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
Douglas Gregor02085352010-03-31 20:19:30 +00002021 ElaboratedTypeKeyword CanonKeyword = Keyword;
2022 if (Keyword == ETK_None)
2023 CanonKeyword = ETK_Typename;
2024 if (CanonNNS != NNS || CanonKeyword != Keyword ||
2025 CanonType != QualType(TemplateId, 0)) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002026 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00002027 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00002028 assert(CanonTemplateId &&
2029 "Canonical type must also be a template specialization type");
Douglas Gregor02085352010-03-31 20:19:30 +00002030 Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
Douglas Gregordce2b622009-04-01 00:28:59 +00002031 }
Douglas Gregorc42075a2010-02-04 18:10:26 +00002032
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002033 DependentNameType *CheckT
2034 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002035 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregordce2b622009-04-01 00:28:59 +00002036 }
2037
Douglas Gregor02085352010-03-31 20:19:30 +00002038 T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002039 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002040 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002041 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002042}
2043
John McCallfcc33b02009-09-05 00:15:47 +00002044QualType
2045ASTContext::getElaboratedType(QualType UnderlyingType,
2046 ElaboratedType::TagKind Tag) {
2047 llvm::FoldingSetNodeID ID;
2048 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump11289f42009-09-09 15:08:12 +00002049
John McCallfcc33b02009-09-05 00:15:47 +00002050 void *InsertPos = 0;
2051 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2052 if (T)
2053 return QualType(T, 0);
2054
Douglas Gregorc42075a2010-02-04 18:10:26 +00002055 QualType Canon = UnderlyingType;
2056 if (!Canon.isCanonical()) {
2057 Canon = getCanonicalType(Canon);
2058 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2059 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2060 }
John McCallfcc33b02009-09-05 00:15:47 +00002061
2062 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2063 Types.push_back(T);
2064 ElaboratedTypes.InsertNode(T, InsertPos);
2065 return QualType(T, 0);
2066}
2067
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002068/// CmpProtocolNames - Comparison predicate for sorting protocols
2069/// alphabetically.
2070static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2071 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002072 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002073}
2074
John McCallfc93cf92009-10-22 22:37:11 +00002075static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2076 unsigned NumProtocols) {
2077 if (NumProtocols == 0) return true;
2078
2079 for (unsigned i = 1; i != NumProtocols; ++i)
2080 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2081 return false;
2082 return true;
2083}
2084
2085static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002086 unsigned &NumProtocols) {
2087 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002088
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002089 // Sort protocols, keyed by name.
2090 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2091
2092 // Remove duplicates.
2093 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2094 NumProtocols = ProtocolsEnd-Protocols;
2095}
2096
Steve Narofffb4330f2009-06-17 22:40:22 +00002097/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2098/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002099QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00002100 ObjCProtocolDecl **Protocols,
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002101 unsigned NumProtocols,
2102 unsigned Quals) {
Steve Narofffb4330f2009-06-17 22:40:22 +00002103 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002104 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002105 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Narofffb4330f2009-06-17 22:40:22 +00002106
2107 void *InsertPos = 0;
2108 if (ObjCObjectPointerType *QT =
2109 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002110 return getQualifiedType(QualType(QT, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002111
John McCallfc93cf92009-10-22 22:37:11 +00002112 // Sort the protocol list alphabetically to canonicalize it.
2113 QualType Canonical;
2114 if (!InterfaceT.isCanonical() ||
2115 !areSortedAndUniqued(Protocols, NumProtocols)) {
2116 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
2117 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2118 unsigned UniqueCount = NumProtocols;
2119
2120 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2121 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2122
2123 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2124 &Sorted[0], UniqueCount);
2125 } else {
2126 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2127 Protocols, NumProtocols);
2128 }
2129
2130 // Regenerate InsertPos.
2131 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2132 }
2133
Douglas Gregorf85bee62010-02-08 22:59:26 +00002134 // No match.
2135 unsigned Size = sizeof(ObjCObjectPointerType)
2136 + NumProtocols * sizeof(ObjCProtocolDecl *);
2137 void *Mem = Allocate(Size, TypeAlignment);
2138 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2139 InterfaceT,
2140 Protocols,
2141 NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002142
Steve Narofffb4330f2009-06-17 22:40:22 +00002143 Types.push_back(QType);
2144 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002145 return getQualifiedType(QualType(QType, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002146}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002147
Steve Naroffc277ad12009-07-18 15:33:26 +00002148/// getObjCInterfaceType - Return the unique reference to the type for the
2149/// specified ObjC interface decl. The list of protocols is optional.
2150QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002151 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002152 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00002153 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002154
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002155 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00002156 if (ObjCInterfaceType *QT =
2157 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002158 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002159
John McCallfc93cf92009-10-22 22:37:11 +00002160 // Sort the protocol list alphabetically to canonicalize it.
2161 QualType Canonical;
2162 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2163 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2164 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2165
2166 unsigned UniqueCount = NumProtocols;
2167 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2168
2169 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2170
2171 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2172 }
2173
Douglas Gregorf85bee62010-02-08 22:59:26 +00002174 unsigned Size = sizeof(ObjCInterfaceType)
2175 + NumProtocols * sizeof(ObjCProtocolDecl *);
2176 void *Mem = Allocate(Size, TypeAlignment);
2177 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2178 const_cast<ObjCInterfaceDecl*>(Decl),
2179 Protocols,
2180 NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002181
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002182 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00002183 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002184 return QualType(QType, 0);
2185}
2186
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002187/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2188/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002189/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002190/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002191/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002192QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002193 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002194 if (tofExpr->isTypeDependent()) {
2195 llvm::FoldingSetNodeID ID;
2196 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002197
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002198 void *InsertPos = 0;
2199 DependentTypeOfExprType *Canon
2200 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2201 if (Canon) {
2202 // We already have a "canonical" version of an identical, dependent
2203 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002204 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002205 QualType((TypeOfExprType*)Canon, 0));
2206 }
2207 else {
2208 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002209 Canon
2210 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002211 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2212 toe = Canon;
2213 }
2214 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002215 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002216 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002217 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002218 Types.push_back(toe);
2219 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002220}
2221
Steve Naroffa773cd52007-08-01 18:02:17 +00002222/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2223/// TypeOfType AST's. The only motivation to unique these nodes would be
2224/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002225/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002226/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002227QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002228 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002229 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002230 Types.push_back(tot);
2231 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002232}
2233
Anders Carlssonad6bd352009-06-24 21:24:56 +00002234/// getDecltypeForExpr - Given an expr, will return the decltype for that
2235/// expression, according to the rules in C++0x [dcl.type.simple]p4
2236static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002237 if (e->isTypeDependent())
2238 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002239
Anders Carlssonad6bd352009-06-24 21:24:56 +00002240 // If e is an id expression or a class member access, decltype(e) is defined
2241 // as the type of the entity named by e.
2242 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2243 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2244 return VD->getType();
2245 }
2246 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2247 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2248 return FD->getType();
2249 }
2250 // If e is a function call or an invocation of an overloaded operator,
2251 // (parentheses around e are ignored), decltype(e) is defined as the
2252 // return type of that function.
2253 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2254 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002255
Anders Carlssonad6bd352009-06-24 21:24:56 +00002256 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002257
2258 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002259 // defined as T&, otherwise decltype(e) is defined as T.
2260 if (e->isLvalue(Context) == Expr::LV_Valid)
2261 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002262
Anders Carlssonad6bd352009-06-24 21:24:56 +00002263 return T;
2264}
2265
Anders Carlsson81df7b82009-06-24 19:06:50 +00002266/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2267/// DecltypeType AST's. The only motivation to unique these nodes would be
2268/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002269/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002270/// on canonical type's (which are always unique).
2271QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002272 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002273 if (e->isTypeDependent()) {
2274 llvm::FoldingSetNodeID ID;
2275 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002276
Douglas Gregora21f6c32009-07-30 23:36:40 +00002277 void *InsertPos = 0;
2278 DependentDecltypeType *Canon
2279 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2280 if (Canon) {
2281 // We already have a "canonical" version of an equivalent, dependent
2282 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002283 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002284 QualType((DecltypeType*)Canon, 0));
2285 }
2286 else {
2287 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002288 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002289 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2290 dt = Canon;
2291 }
2292 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002293 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002294 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002295 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002296 Types.push_back(dt);
2297 return QualType(dt, 0);
2298}
2299
Chris Lattnerfb072462007-01-23 05:45:31 +00002300/// getTagDeclType - Return the unique reference to the type for the
2301/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002302QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002303 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002304 // FIXME: What is the design on getTagDeclType when it requires casting
2305 // away const? mutable?
2306 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002307}
2308
Mike Stump11289f42009-09-09 15:08:12 +00002309/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2310/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2311/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002312CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002313 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002314}
Chris Lattnerfb072462007-01-23 05:45:31 +00002315
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002316/// getSignedWCharType - Return the type of "signed wchar_t".
2317/// Used when in C++, as a GCC extension.
2318QualType ASTContext::getSignedWCharType() const {
2319 // FIXME: derive from "Target" ?
2320 return WCharTy;
2321}
2322
2323/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2324/// Used when in C++, as a GCC extension.
2325QualType ASTContext::getUnsignedWCharType() const {
2326 // FIXME: derive from "Target" ?
2327 return UnsignedIntTy;
2328}
2329
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002330/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2331/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2332QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002333 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002334}
2335
Chris Lattnera21ad802008-04-02 05:18:44 +00002336//===----------------------------------------------------------------------===//
2337// Type Operators
2338//===----------------------------------------------------------------------===//
2339
John McCallfc93cf92009-10-22 22:37:11 +00002340CanQualType ASTContext::getCanonicalParamType(QualType T) {
2341 // Push qualifiers into arrays, and then discard any remaining
2342 // qualifiers.
2343 T = getCanonicalType(T);
2344 const Type *Ty = T.getTypePtr();
2345
2346 QualType Result;
2347 if (isa<ArrayType>(Ty)) {
2348 Result = getArrayDecayedType(QualType(Ty,0));
2349 } else if (isa<FunctionType>(Ty)) {
2350 Result = getPointerType(QualType(Ty, 0));
2351 } else {
2352 Result = QualType(Ty, 0);
2353 }
2354
2355 return CanQualType::CreateUnsafe(Result);
2356}
2357
Chris Lattnered0d0792008-04-06 22:41:35 +00002358/// getCanonicalType - Return the canonical (structural) type corresponding to
2359/// the specified potentially non-canonical type. The non-canonical version
2360/// of a type may have many "decorated" versions of types. Decorators can
2361/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2362/// to be free of any of these, allowing two canonical types to be compared
2363/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002364CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002365 QualifierCollector Quals;
2366 const Type *Ptr = Quals.strip(T);
2367 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002368
John McCall8ccfcb52009-09-24 19:53:00 +00002369 // The canonical internal type will be the canonical type *except*
2370 // that we push type qualifiers down through array types.
2371
2372 // If there are no new qualifiers to push down, stop here.
2373 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002374 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002375
John McCall8ccfcb52009-09-24 19:53:00 +00002376 // If the type qualifiers are on an array type, get the canonical
2377 // type of the array with the qualifiers applied to the element
2378 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002379 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2380 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002381 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002382
Chris Lattner7adf0762008-08-04 07:31:14 +00002383 // Get the canonical version of the element with the extra qualifiers on it.
2384 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002385 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002386 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002387
Chris Lattner7adf0762008-08-04 07:31:14 +00002388 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002389 return CanQualType::CreateUnsafe(
2390 getConstantArrayType(NewEltTy, CAT->getSize(),
2391 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002392 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002393 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002394 return CanQualType::CreateUnsafe(
2395 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002396 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002397
Douglas Gregor4619e432008-12-05 23:32:09 +00002398 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002399 return CanQualType::CreateUnsafe(
2400 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002401 DSAT->getSizeExpr() ?
2402 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002403 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002404 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002405 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002406
Chris Lattner7adf0762008-08-04 07:31:14 +00002407 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002408 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002409 VAT->getSizeExpr() ?
2410 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002411 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002412 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002413 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002414}
2415
Chandler Carruth607f38e2009-12-29 07:16:59 +00002416QualType ASTContext::getUnqualifiedArrayType(QualType T,
2417 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002418 Quals = T.getQualifiers();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002419 if (!isa<ArrayType>(T)) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002420 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002421 }
2422
Chandler Carruth607f38e2009-12-29 07:16:59 +00002423 const ArrayType *AT = cast<ArrayType>(T);
2424 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002425 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002426 if (Elt == UnqualElt)
2427 return T;
2428
2429 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2430 return getConstantArrayType(UnqualElt, CAT->getSize(),
2431 CAT->getSizeModifier(), 0);
2432 }
2433
2434 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2435 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2436 }
2437
2438 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2439 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2440 DSAT->getSizeModifier(), 0,
2441 SourceRange());
2442}
2443
John McCall847e2a12009-11-24 18:42:40 +00002444DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2445 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2446 return TD->getDeclName();
2447
2448 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2449 if (DTN->isIdentifier()) {
2450 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2451 } else {
2452 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2453 }
2454 }
2455
John McCalld28ae272009-12-02 08:04:21 +00002456 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2457 assert(Storage);
2458 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002459}
2460
Douglas Gregor6bc50582009-05-07 06:41:52 +00002461TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2462 // If this template name refers to a template, the canonical
2463 // template name merely stores the template itself.
2464 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002465 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002466
John McCalld28ae272009-12-02 08:04:21 +00002467 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002468
Douglas Gregor6bc50582009-05-07 06:41:52 +00002469 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2470 assert(DTN && "Non-dependent template names must refer to template decls.");
2471 return DTN->CanonicalTemplateName;
2472}
2473
Douglas Gregoradee3e32009-11-11 23:06:43 +00002474bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2475 X = getCanonicalTemplateName(X);
2476 Y = getCanonicalTemplateName(Y);
2477 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2478}
2479
Mike Stump11289f42009-09-09 15:08:12 +00002480TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002481ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2482 switch (Arg.getKind()) {
2483 case TemplateArgument::Null:
2484 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002485
Douglas Gregora8e02e72009-07-28 23:00:59 +00002486 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002487 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002488
Douglas Gregora8e02e72009-07-28 23:00:59 +00002489 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002490 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002491
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002492 case TemplateArgument::Template:
2493 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2494
Douglas Gregora8e02e72009-07-28 23:00:59 +00002495 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002496 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002497 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002498
Douglas Gregora8e02e72009-07-28 23:00:59 +00002499 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002500 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002501
Douglas Gregora8e02e72009-07-28 23:00:59 +00002502 case TemplateArgument::Pack: {
2503 // FIXME: Allocate in ASTContext
2504 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2505 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002506 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002507 AEnd = Arg.pack_end();
2508 A != AEnd; (void)++A, ++Idx)
2509 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002510
Douglas Gregora8e02e72009-07-28 23:00:59 +00002511 TemplateArgument Result;
2512 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2513 return Result;
2514 }
2515 }
2516
2517 // Silence GCC warning
2518 assert(false && "Unhandled template argument kind");
2519 return TemplateArgument();
2520}
2521
Douglas Gregor333489b2009-03-27 23:10:48 +00002522NestedNameSpecifier *
2523ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002524 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002525 return 0;
2526
2527 switch (NNS->getKind()) {
2528 case NestedNameSpecifier::Identifier:
2529 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002530 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002531 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2532 NNS->getAsIdentifier());
2533
2534 case NestedNameSpecifier::Namespace:
2535 // A namespace is canonical; build a nested-name-specifier with
2536 // this namespace and no prefix.
2537 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2538
2539 case NestedNameSpecifier::TypeSpec:
2540 case NestedNameSpecifier::TypeSpecWithTemplate: {
2541 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002542 return NestedNameSpecifier::Create(*this, 0,
2543 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002544 T.getTypePtr());
2545 }
2546
2547 case NestedNameSpecifier::Global:
2548 // The global specifier is canonical and unique.
2549 return NNS;
2550 }
2551
2552 // Required to silence a GCC warning
2553 return 0;
2554}
2555
Chris Lattner7adf0762008-08-04 07:31:14 +00002556
2557const ArrayType *ASTContext::getAsArrayType(QualType T) {
2558 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002559 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002560 // Handle the common positive case fast.
2561 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2562 return AT;
2563 }
Mike Stump11289f42009-09-09 15:08:12 +00002564
John McCall8ccfcb52009-09-24 19:53:00 +00002565 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002566 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002567 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002568 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002569
John McCall8ccfcb52009-09-24 19:53:00 +00002570 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002571 // implements C99 6.7.3p8: "If the specification of an array type includes
2572 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002573
Chris Lattner7adf0762008-08-04 07:31:14 +00002574 // If we get here, we either have type qualifiers on the type, or we have
2575 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002576 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002577
John McCall8ccfcb52009-09-24 19:53:00 +00002578 QualifierCollector Qs;
2579 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002580
Chris Lattner7adf0762008-08-04 07:31:14 +00002581 // If we have a simple case, just return now.
2582 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002583 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002584 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002585
Chris Lattner7adf0762008-08-04 07:31:14 +00002586 // Otherwise, we have an array and we have qualifiers on it. Push the
2587 // qualifiers into the array element type and return a new array type.
2588 // Get the canonical version of the element with the extra qualifiers on it.
2589 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002590 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002591
Chris Lattner7adf0762008-08-04 07:31:14 +00002592 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2593 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2594 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002595 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002596 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2597 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2598 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002599 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002600
Mike Stump11289f42009-09-09 15:08:12 +00002601 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002602 = dyn_cast<DependentSizedArrayType>(ATy))
2603 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002604 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002605 DSAT->getSizeExpr() ?
2606 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002607 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002608 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002609 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002610
Chris Lattner7adf0762008-08-04 07:31:14 +00002611 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002612 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002613 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002614 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002615 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002616 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002617 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002618}
2619
2620
Chris Lattnera21ad802008-04-02 05:18:44 +00002621/// getArrayDecayedType - Return the properly qualified result of decaying the
2622/// specified array type to a pointer. This operation is non-trivial when
2623/// handling typedefs etc. The canonical type of "T" must be an array type,
2624/// this returns a pointer to a properly qualified element of the array.
2625///
2626/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2627QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002628 // Get the element type with 'getAsArrayType' so that we don't lose any
2629 // typedefs in the element type of the array. This also handles propagation
2630 // of type qualifiers from the array type into the element type if present
2631 // (C99 6.7.3p8).
2632 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2633 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002634
Chris Lattner7adf0762008-08-04 07:31:14 +00002635 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002636
2637 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002638 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002639}
2640
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002641QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002642 QualifierCollector Qs;
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002643 while (true) {
John McCall8ccfcb52009-09-24 19:53:00 +00002644 const Type *UT = Qs.strip(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002645 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2646 QT = AT->getElementType();
Mike Stumpea086c72009-07-25 23:24:03 +00002647 } else {
John McCall8ccfcb52009-09-24 19:53:00 +00002648 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002649 }
2650 }
2651}
2652
Anders Carlsson4bf82142009-09-25 01:23:32 +00002653QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2654 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002655
Anders Carlsson4bf82142009-09-25 01:23:32 +00002656 if (const ArrayType *AT = getAsArrayType(ElemTy))
2657 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002658
Anders Carlssone0808df2008-12-21 03:44:36 +00002659 return ElemTy;
2660}
2661
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002662/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002663uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002664ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2665 uint64_t ElementCount = 1;
2666 do {
2667 ElementCount *= CA->getSize().getZExtValue();
2668 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2669 } while (CA);
2670 return ElementCount;
2671}
2672
Steve Naroff0af91202007-04-27 21:51:21 +00002673/// getFloatingRank - Return a relative rank for floating point types.
2674/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002675static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002676 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002677 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002678
John McCall9dd450b2009-09-21 23:43:11 +00002679 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2680 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002681 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002682 case BuiltinType::Float: return FloatRank;
2683 case BuiltinType::Double: return DoubleRank;
2684 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002685 }
2686}
2687
Mike Stump11289f42009-09-09 15:08:12 +00002688/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2689/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002690/// 'typeDomain' is a real floating point or complex type.
2691/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002692QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2693 QualType Domain) const {
2694 FloatingRank EltRank = getFloatingRank(Size);
2695 if (Domain->isComplexType()) {
2696 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002697 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002698 case FloatRank: return FloatComplexTy;
2699 case DoubleRank: return DoubleComplexTy;
2700 case LongDoubleRank: return LongDoubleComplexTy;
2701 }
Steve Naroff0af91202007-04-27 21:51:21 +00002702 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002703
2704 assert(Domain->isRealFloatingType() && "Unknown domain!");
2705 switch (EltRank) {
2706 default: assert(0 && "getFloatingRank(): illegal value for rank");
2707 case FloatRank: return FloatTy;
2708 case DoubleRank: return DoubleTy;
2709 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002710 }
Steve Naroffe4718892007-04-27 18:30:00 +00002711}
2712
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002713/// getFloatingTypeOrder - Compare the rank of the two specified floating
2714/// point types, ignoring the domain of the type (i.e. 'double' ==
2715/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002716/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002717int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2718 FloatingRank LHSR = getFloatingRank(LHS);
2719 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002720
Chris Lattnerb90739d2008-04-06 23:38:49 +00002721 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002722 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002723 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002724 return 1;
2725 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002726}
2727
Chris Lattner76a00cf2008-04-06 22:59:24 +00002728/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2729/// routine will assert if passed a built-in type that isn't an integer or enum,
2730/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002731unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002732 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002733 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002734 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002735
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002736 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2737 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2738
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002739 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2740 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2741
2742 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2743 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2744
Chris Lattner76a00cf2008-04-06 22:59:24 +00002745 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002746 default: assert(0 && "getIntegerRank(): not a built-in integer");
2747 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002748 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002749 case BuiltinType::Char_S:
2750 case BuiltinType::Char_U:
2751 case BuiltinType::SChar:
2752 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002753 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002754 case BuiltinType::Short:
2755 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002756 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002757 case BuiltinType::Int:
2758 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002759 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002760 case BuiltinType::Long:
2761 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002762 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002763 case BuiltinType::LongLong:
2764 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002765 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002766 case BuiltinType::Int128:
2767 case BuiltinType::UInt128:
2768 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002769 }
2770}
2771
Eli Friedman629ffb92009-08-20 04:21:42 +00002772/// \brief Whether this is a promotable bitfield reference according
2773/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2774///
2775/// \returns the type this bit-field will promote to, or NULL if no
2776/// promotion occurs.
2777QualType ASTContext::isPromotableBitField(Expr *E) {
2778 FieldDecl *Field = E->getBitField();
2779 if (!Field)
2780 return QualType();
2781
2782 QualType FT = Field->getType();
2783
2784 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2785 uint64_t BitWidth = BitWidthAP.getZExtValue();
2786 uint64_t IntSize = getTypeSize(IntTy);
2787 // GCC extension compatibility: if the bit-field size is less than or equal
2788 // to the size of int, it gets promoted no matter what its type is.
2789 // For instance, unsigned long bf : 4 gets promoted to signed int.
2790 if (BitWidth < IntSize)
2791 return IntTy;
2792
2793 if (BitWidth == IntSize)
2794 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2795
2796 // Types bigger than int are not subject to promotions, and therefore act
2797 // like the base type.
2798 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2799 // is ridiculous.
2800 return QualType();
2801}
2802
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002803/// getPromotedIntegerType - Returns the type that Promotable will
2804/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2805/// integer type.
2806QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2807 assert(!Promotable.isNull());
2808 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002809 if (const EnumType *ET = Promotable->getAs<EnumType>())
2810 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002811 if (Promotable->isSignedIntegerType())
2812 return IntTy;
2813 uint64_t PromotableSize = getTypeSize(Promotable);
2814 uint64_t IntSize = getTypeSize(IntTy);
2815 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2816 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2817}
2818
Mike Stump11289f42009-09-09 15:08:12 +00002819/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002820/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002821/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002822int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002823 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2824 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002825 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002826
Chris Lattner76a00cf2008-04-06 22:59:24 +00002827 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2828 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002829
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002830 unsigned LHSRank = getIntegerRank(LHSC);
2831 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002832
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002833 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2834 if (LHSRank == RHSRank) return 0;
2835 return LHSRank > RHSRank ? 1 : -1;
2836 }
Mike Stump11289f42009-09-09 15:08:12 +00002837
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002838 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2839 if (LHSUnsigned) {
2840 // If the unsigned [LHS] type is larger, return it.
2841 if (LHSRank >= RHSRank)
2842 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002843
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002844 // If the signed type can represent all values of the unsigned type, it
2845 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002846 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002847 return -1;
2848 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002849
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002850 // If the unsigned [RHS] type is larger, return it.
2851 if (RHSRank >= LHSRank)
2852 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002853
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002854 // If the signed type can represent all values of the unsigned type, it
2855 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002856 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002857 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002858}
Anders Carlsson98f07902007-08-17 05:31:46 +00002859
Anders Carlsson6d417272009-11-14 21:45:58 +00002860static RecordDecl *
2861CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2862 SourceLocation L, IdentifierInfo *Id) {
2863 if (Ctx.getLangOptions().CPlusPlus)
2864 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2865 else
2866 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2867}
2868
Mike Stump11289f42009-09-09 15:08:12 +00002869// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002870QualType ASTContext::getCFConstantStringType() {
2871 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002872 CFConstantStringTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002873 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2874 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002875 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002876
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002877 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002878
Anders Carlsson98f07902007-08-17 05:31:46 +00002879 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002880 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002881 // int flags;
2882 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002883 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002884 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002885 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002886 FieldTypes[3] = LongTy;
2887
Anders Carlsson98f07902007-08-17 05:31:46 +00002888 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002889 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002890 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002891 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002892 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002893 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002894 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002895 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002896 }
2897
Douglas Gregord5058122010-02-11 01:19:42 +00002898 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002899 }
Mike Stump11289f42009-09-09 15:08:12 +00002900
Anders Carlsson98f07902007-08-17 05:31:46 +00002901 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002902}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002903
Douglas Gregor512b0772009-04-23 22:29:11 +00002904void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002905 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002906 assert(Rec && "Invalid CFConstantStringType");
2907 CFConstantStringTypeDecl = Rec->getDecl();
2908}
2909
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002910// getNSConstantStringType - Return the type used for constant NSStrings.
2911QualType ASTContext::getNSConstantStringType() {
2912 if (!NSConstantStringTypeDecl) {
2913 NSConstantStringTypeDecl =
2914 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2915 &Idents.get("__builtin_NSString"));
2916 NSConstantStringTypeDecl->startDefinition();
2917
2918 QualType FieldTypes[3];
2919
2920 // const int *isa;
2921 FieldTypes[0] = getPointerType(IntTy.withConst());
2922 // const char *str;
2923 FieldTypes[1] = getPointerType(CharTy.withConst());
2924 // unsigned int length;
2925 FieldTypes[2] = UnsignedIntTy;
2926
2927 // Create fields
2928 for (unsigned i = 0; i < 3; ++i) {
2929 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2930 SourceLocation(), 0,
2931 FieldTypes[i], /*TInfo=*/0,
2932 /*BitWidth=*/0,
2933 /*Mutable=*/false);
2934 NSConstantStringTypeDecl->addDecl(Field);
2935 }
2936
2937 NSConstantStringTypeDecl->completeDefinition();
2938 }
2939
2940 return getTagDeclType(NSConstantStringTypeDecl);
2941}
2942
2943void ASTContext::setNSConstantStringType(QualType T) {
2944 const RecordType *Rec = T->getAs<RecordType>();
2945 assert(Rec && "Invalid NSConstantStringType");
2946 NSConstantStringTypeDecl = Rec->getDecl();
2947}
2948
Mike Stump11289f42009-09-09 15:08:12 +00002949QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002950 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002951 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002952 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2953 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00002954 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002955
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002956 QualType FieldTypes[] = {
2957 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002958 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002959 getPointerType(UnsignedLongTy),
2960 getConstantArrayType(UnsignedLongTy,
2961 llvm::APInt(32, 5), ArrayType::Normal, 0)
2962 };
Mike Stump11289f42009-09-09 15:08:12 +00002963
Douglas Gregor91f84212008-12-11 16:49:14 +00002964 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002965 FieldDecl *Field = FieldDecl::Create(*this,
2966 ObjCFastEnumerationStateTypeDecl,
2967 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002968 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002969 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002970 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002971 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002972 }
Mike Stump11289f42009-09-09 15:08:12 +00002973
Douglas Gregord5058122010-02-11 01:19:42 +00002974 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002975 }
Mike Stump11289f42009-09-09 15:08:12 +00002976
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002977 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2978}
2979
Mike Stumpd0153282009-10-20 02:12:22 +00002980QualType ASTContext::getBlockDescriptorType() {
2981 if (BlockDescriptorType)
2982 return getTagDeclType(BlockDescriptorType);
2983
2984 RecordDecl *T;
2985 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002986 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2987 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00002988 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002989
2990 QualType FieldTypes[] = {
2991 UnsignedLongTy,
2992 UnsignedLongTy,
2993 };
2994
2995 const char *FieldNames[] = {
2996 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002997 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002998 };
2999
3000 for (size_t i = 0; i < 2; ++i) {
3001 FieldDecl *Field = FieldDecl::Create(*this,
3002 T,
3003 SourceLocation(),
3004 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003005 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003006 /*BitWidth=*/0,
3007 /*Mutable=*/false);
3008 T->addDecl(Field);
3009 }
3010
Douglas Gregord5058122010-02-11 01:19:42 +00003011 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003012
3013 BlockDescriptorType = T;
3014
3015 return getTagDeclType(BlockDescriptorType);
3016}
3017
3018void ASTContext::setBlockDescriptorType(QualType T) {
3019 const RecordType *Rec = T->getAs<RecordType>();
3020 assert(Rec && "Invalid BlockDescriptorType");
3021 BlockDescriptorType = Rec->getDecl();
3022}
3023
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003024QualType ASTContext::getBlockDescriptorExtendedType() {
3025 if (BlockDescriptorExtendedType)
3026 return getTagDeclType(BlockDescriptorExtendedType);
3027
3028 RecordDecl *T;
3029 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00003030 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3031 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003032 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003033
3034 QualType FieldTypes[] = {
3035 UnsignedLongTy,
3036 UnsignedLongTy,
3037 getPointerType(VoidPtrTy),
3038 getPointerType(VoidPtrTy)
3039 };
3040
3041 const char *FieldNames[] = {
3042 "reserved",
3043 "Size",
3044 "CopyFuncPtr",
3045 "DestroyFuncPtr"
3046 };
3047
3048 for (size_t i = 0; i < 4; ++i) {
3049 FieldDecl *Field = FieldDecl::Create(*this,
3050 T,
3051 SourceLocation(),
3052 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003053 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003054 /*BitWidth=*/0,
3055 /*Mutable=*/false);
3056 T->addDecl(Field);
3057 }
3058
Douglas Gregord5058122010-02-11 01:19:42 +00003059 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003060
3061 BlockDescriptorExtendedType = T;
3062
3063 return getTagDeclType(BlockDescriptorExtendedType);
3064}
3065
3066void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3067 const RecordType *Rec = T->getAs<RecordType>();
3068 assert(Rec && "Invalid BlockDescriptorType");
3069 BlockDescriptorExtendedType = Rec->getDecl();
3070}
3071
Mike Stump94967902009-10-21 18:16:27 +00003072bool ASTContext::BlockRequiresCopying(QualType Ty) {
3073 if (Ty->isBlockPointerType())
3074 return true;
3075 if (isObjCNSObjectType(Ty))
3076 return true;
3077 if (Ty->isObjCObjectPointerType())
3078 return true;
3079 return false;
3080}
3081
3082QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3083 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003084 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003085 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003086 // unsigned int __flags;
3087 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003088 // void *__copy_helper; // as needed
3089 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003090 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003091 // } *
3092
Mike Stump94967902009-10-21 18:16:27 +00003093 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3094
3095 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003096 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003097 llvm::SmallString<36> Name;
3098 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3099 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003100 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003101 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3102 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003103 T->startDefinition();
3104 QualType Int32Ty = IntTy;
3105 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3106 QualType FieldTypes[] = {
3107 getPointerType(VoidPtrTy),
3108 getPointerType(getTagDeclType(T)),
3109 Int32Ty,
3110 Int32Ty,
3111 getPointerType(VoidPtrTy),
3112 getPointerType(VoidPtrTy),
3113 Ty
3114 };
3115
3116 const char *FieldNames[] = {
3117 "__isa",
3118 "__forwarding",
3119 "__flags",
3120 "__size",
3121 "__copy_helper",
3122 "__destroy_helper",
3123 DeclName,
3124 };
3125
3126 for (size_t i = 0; i < 7; ++i) {
3127 if (!HasCopyAndDispose && i >=4 && i <= 5)
3128 continue;
3129 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3130 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003131 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003132 /*BitWidth=*/0, /*Mutable=*/false);
3133 T->addDecl(Field);
3134 }
3135
Douglas Gregord5058122010-02-11 01:19:42 +00003136 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003137
3138 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003139}
3140
3141
3142QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003143 bool BlockHasCopyDispose,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003144 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpd0153282009-10-20 02:12:22 +00003145 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003146 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003147 llvm::SmallString<36> Name;
3148 llvm::raw_svector_ostream(Name) << "__block_literal_"
3149 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003150 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003151 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3152 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003153 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003154 QualType FieldTypes[] = {
3155 getPointerType(VoidPtrTy),
3156 IntTy,
3157 IntTy,
3158 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003159 (BlockHasCopyDispose ?
3160 getPointerType(getBlockDescriptorExtendedType()) :
3161 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003162 };
3163
3164 const char *FieldNames[] = {
3165 "__isa",
3166 "__flags",
3167 "__reserved",
3168 "__FuncPtr",
3169 "__descriptor"
3170 };
3171
3172 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003173 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003174 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003175 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003176 /*BitWidth=*/0, /*Mutable=*/false);
3177 T->addDecl(Field);
3178 }
3179
3180 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3181 const Expr *E = BlockDeclRefDecls[i];
3182 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3183 clang::IdentifierInfo *Name = 0;
3184 if (BDRE) {
3185 const ValueDecl *D = BDRE->getDecl();
3186 Name = &Idents.get(D->getName());
3187 }
3188 QualType FieldType = E->getType();
3189
3190 if (BDRE && BDRE->isByRef())
Mike Stump94967902009-10-21 18:16:27 +00003191 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3192 FieldType);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003193
3194 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallbcd03502009-12-07 02:54:59 +00003195 Name, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003196 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpd0153282009-10-20 02:12:22 +00003197 T->addDecl(Field);
3198 }
3199
Douglas Gregord5058122010-02-11 01:19:42 +00003200 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003201
3202 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003203}
3204
Douglas Gregor512b0772009-04-23 22:29:11 +00003205void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003206 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003207 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3208 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3209}
3210
Anders Carlsson18acd442007-10-29 06:33:42 +00003211// This returns true if a type has been typedefed to BOOL:
3212// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003213static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003214 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003215 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3216 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003217
Anders Carlssond8499822007-10-29 05:01:08 +00003218 return false;
3219}
3220
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003221/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003222/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003223CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003224 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003225
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003226 // Make all integer and enum types at least as large as an int
Ken Dyck40775002010-01-11 17:06:35 +00003227 if (sz.isPositive() && type->isIntegralType())
3228 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003229 // Treat arrays as pointers, since that's how they're passed in.
3230 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003231 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003232 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003233}
3234
3235static inline
3236std::string charUnitsToString(const CharUnits &CU) {
3237 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003238}
3239
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003240/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003241/// declaration.
3242void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3243 std::string& S) {
3244 const BlockDecl *Decl = Expr->getBlockDecl();
3245 QualType BlockTy =
3246 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3247 // Encode result type.
3248 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3249 // Compute size of all parameters.
3250 // Start with computing size of a pointer in number of bytes.
3251 // FIXME: There might(should) be a better way of doing this computation!
3252 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003253 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3254 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003255 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003256 E = Decl->param_end(); PI != E; ++PI) {
3257 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003258 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003259 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003260 ParmOffset += sz;
3261 }
3262 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003263 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003264 // Block pointer and offset.
3265 S += "@?0";
3266 ParmOffset = PtrSize;
3267
3268 // Argument types.
3269 ParmOffset = PtrSize;
3270 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3271 Decl->param_end(); PI != E; ++PI) {
3272 ParmVarDecl *PVDecl = *PI;
3273 QualType PType = PVDecl->getOriginalType();
3274 if (const ArrayType *AT =
3275 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3276 // Use array's original type only if it has known number of
3277 // elements.
3278 if (!isa<ConstantArrayType>(AT))
3279 PType = PVDecl->getType();
3280 } else if (PType->isFunctionType())
3281 PType = PVDecl->getType();
3282 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003283 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003284 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003285 }
3286}
3287
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003288/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003289/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003290void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003291 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003292 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003293 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003294 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003295 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003296 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003297 // Compute size of all parameters.
3298 // Start with computing size of a pointer in number of bytes.
3299 // FIXME: There might(should) be a better way of doing this computation!
3300 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003301 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003302 // The first two arguments (self and _cmd) are pointers; account for
3303 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003304 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003305 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003306 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003307 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003308 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003309 assert (sz.isPositive() &&
3310 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003311 ParmOffset += sz;
3312 }
Ken Dyck40775002010-01-11 17:06:35 +00003313 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003314 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003315 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003316
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003317 // Argument types.
3318 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003319 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003320 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003321 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003322 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003323 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003324 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3325 // Use array's original type only if it has known number of
3326 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003327 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003328 PType = PVDecl->getType();
3329 } else if (PType->isFunctionType())
3330 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003331 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003332 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003333 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003334 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003335 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003336 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003337 }
3338}
3339
Daniel Dunbar4932b362008-08-28 04:38:10 +00003340/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003341/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003342/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3343/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003344/// Property attributes are stored as a comma-delimited C string. The simple
3345/// attributes readonly and bycopy are encoded as single characters. The
3346/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3347/// encoded as single characters, followed by an identifier. Property types
3348/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003349/// these attributes are defined by the following enumeration:
3350/// @code
3351/// enum PropertyAttributes {
3352/// kPropertyReadOnly = 'R', // property is read-only.
3353/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3354/// kPropertyByref = '&', // property is a reference to the value last assigned
3355/// kPropertyDynamic = 'D', // property is dynamic
3356/// kPropertyGetter = 'G', // followed by getter selector name
3357/// kPropertySetter = 'S', // followed by setter selector name
3358/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3359/// kPropertyType = 't' // followed by old-style type encoding.
3360/// kPropertyWeak = 'W' // 'weak' property
3361/// kPropertyStrong = 'P' // property GC'able
3362/// kPropertyNonAtomic = 'N' // property non-atomic
3363/// };
3364/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003365void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003366 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003367 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003368 // Collect information from the property implementation decl(s).
3369 bool Dynamic = false;
3370 ObjCPropertyImplDecl *SynthesizePID = 0;
3371
3372 // FIXME: Duplicated code due to poor abstraction.
3373 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003374 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003375 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3376 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003377 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003378 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003379 ObjCPropertyImplDecl *PID = *i;
3380 if (PID->getPropertyDecl() == PD) {
3381 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3382 Dynamic = true;
3383 } else {
3384 SynthesizePID = PID;
3385 }
3386 }
3387 }
3388 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003389 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003390 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003391 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003392 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003393 ObjCPropertyImplDecl *PID = *i;
3394 if (PID->getPropertyDecl() == PD) {
3395 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3396 Dynamic = true;
3397 } else {
3398 SynthesizePID = PID;
3399 }
3400 }
Mike Stump11289f42009-09-09 15:08:12 +00003401 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003402 }
3403 }
3404
3405 // FIXME: This is not very efficient.
3406 S = "T";
3407
3408 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003409 // GCC has some special rules regarding encoding of properties which
3410 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003411 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003412 true /* outermost type */,
3413 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003414
3415 if (PD->isReadOnly()) {
3416 S += ",R";
3417 } else {
3418 switch (PD->getSetterKind()) {
3419 case ObjCPropertyDecl::Assign: break;
3420 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003421 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003422 }
3423 }
3424
3425 // It really isn't clear at all what this means, since properties
3426 // are "dynamic by default".
3427 if (Dynamic)
3428 S += ",D";
3429
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003430 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3431 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003432
Daniel Dunbar4932b362008-08-28 04:38:10 +00003433 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3434 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003435 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003436 }
3437
3438 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3439 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003440 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003441 }
3442
3443 if (SynthesizePID) {
3444 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3445 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003446 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003447 }
3448
3449 // FIXME: OBJCGC: weak & strong
3450}
3451
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003452/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003453/// Another legacy compatibility encoding: 32-bit longs are encoded as
3454/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003455/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3456///
3457void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003458 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003459 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003460 if (BT->getKind() == BuiltinType::ULong &&
3461 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003462 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003463 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003464 if (BT->getKind() == BuiltinType::Long &&
3465 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003466 PointeeTy = IntTy;
3467 }
3468 }
3469}
3470
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003471void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003472 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003473 // We follow the behavior of gcc, expanding structures which are
3474 // directly pointed to, and expanding embedded structures. Note that
3475 // these rules are sufficient to prevent recursive encoding of the
3476 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003477 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003478 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003479}
3480
Mike Stump11289f42009-09-09 15:08:12 +00003481static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003482 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003483 const Expr *E = FD->getBitWidth();
3484 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3485 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00003486 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003487 S += 'b';
3488 S += llvm::utostr(N);
3489}
3490
Daniel Dunbar07d07852009-10-18 21:17:35 +00003491// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003492void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3493 bool ExpandPointedToStructures,
3494 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003495 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003496 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003497 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00003498 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003499 if (FD && FD->isBitField())
3500 return EncodeBitField(this, S, FD);
3501 char encoding;
3502 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003503 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00003504 case BuiltinType::Void: encoding = 'v'; break;
3505 case BuiltinType::Bool: encoding = 'B'; break;
3506 case BuiltinType::Char_U:
3507 case BuiltinType::UChar: encoding = 'C'; break;
3508 case BuiltinType::UShort: encoding = 'S'; break;
3509 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003510 case BuiltinType::ULong:
3511 encoding =
3512 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00003513 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003514 case BuiltinType::UInt128: encoding = 'T'; break;
3515 case BuiltinType::ULongLong: encoding = 'Q'; break;
3516 case BuiltinType::Char_S:
3517 case BuiltinType::SChar: encoding = 'c'; break;
3518 case BuiltinType::Short: encoding = 's'; break;
3519 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003520 case BuiltinType::Long:
3521 encoding =
3522 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003523 break;
3524 case BuiltinType::LongLong: encoding = 'q'; break;
3525 case BuiltinType::Int128: encoding = 't'; break;
3526 case BuiltinType::Float: encoding = 'f'; break;
3527 case BuiltinType::Double: encoding = 'd'; break;
3528 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003529 }
Mike Stump11289f42009-09-09 15:08:12 +00003530
Chris Lattnere7cabb92009-07-13 00:10:46 +00003531 S += encoding;
3532 return;
3533 }
Mike Stump11289f42009-09-09 15:08:12 +00003534
John McCall9dd450b2009-09-21 23:43:11 +00003535 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003536 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003537 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003538 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003539 return;
3540 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003541
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003542 // encoding for pointer or r3eference types.
3543 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003544 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003545 if (PT->isObjCSelType()) {
3546 S += ':';
3547 return;
3548 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003549 PointeeTy = PT->getPointeeType();
3550 }
3551 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3552 PointeeTy = RT->getPointeeType();
3553 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003554 bool isReadOnly = false;
3555 // For historical/compatibility reasons, the read-only qualifier of the
3556 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3557 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003558 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003559 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003560 if (OutermostType && T.isConstQualified()) {
3561 isReadOnly = true;
3562 S += 'r';
3563 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003564 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003565 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003566 while (P->getAs<PointerType>())
3567 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003568 if (P.isConstQualified()) {
3569 isReadOnly = true;
3570 S += 'r';
3571 }
3572 }
3573 if (isReadOnly) {
3574 // Another legacy compatibility encoding. Some ObjC qualifier and type
3575 // combinations need to be rearranged.
3576 // Rewrite "in const" from "nr" to "rn"
3577 const char * s = S.c_str();
3578 int len = S.length();
3579 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3580 std::string replace = "rn";
3581 S.replace(S.end()-2, S.end(), replace);
3582 }
3583 }
Mike Stump11289f42009-09-09 15:08:12 +00003584
Anders Carlssond8499822007-10-29 05:01:08 +00003585 if (PointeeTy->isCharType()) {
3586 // char pointer types should be encoded as '*' unless it is a
3587 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003588 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003589 S += '*';
3590 return;
3591 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003592 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003593 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3594 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3595 S += '#';
3596 return;
3597 }
3598 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3599 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3600 S += '@';
3601 return;
3602 }
3603 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003604 }
Anders Carlssond8499822007-10-29 05:01:08 +00003605 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003606 getLegacyIntegralTypeEncoding(PointeeTy);
3607
Mike Stump11289f42009-09-09 15:08:12 +00003608 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003609 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003610 return;
3611 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003612
Chris Lattnere7cabb92009-07-13 00:10:46 +00003613 if (const ArrayType *AT =
3614 // Ignore type qualifiers etc.
3615 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003616 if (isa<IncompleteArrayType>(AT)) {
3617 // Incomplete arrays are encoded as a pointer to the array element.
3618 S += '^';
3619
Mike Stump11289f42009-09-09 15:08:12 +00003620 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003621 false, ExpandStructures, FD);
3622 } else {
3623 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003624
Anders Carlssond05f44b2009-02-22 01:38:57 +00003625 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3626 S += llvm::utostr(CAT->getSize().getZExtValue());
3627 else {
3628 //Variable length arrays are encoded as a regular array with 0 elements.
3629 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3630 S += '0';
3631 }
Mike Stump11289f42009-09-09 15:08:12 +00003632
3633 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003634 false, ExpandStructures, FD);
3635 S += ']';
3636 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003637 return;
3638 }
Mike Stump11289f42009-09-09 15:08:12 +00003639
John McCall9dd450b2009-09-21 23:43:11 +00003640 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003641 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003642 return;
3643 }
Mike Stump11289f42009-09-09 15:08:12 +00003644
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003645 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003646 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003647 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003648 // Anonymous structures print as '?'
3649 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3650 S += II->getName();
3651 } else {
3652 S += '?';
3653 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003654 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003655 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003656 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3657 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003658 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003659 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003660 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003661 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003662 S += '"';
3663 }
Mike Stump11289f42009-09-09 15:08:12 +00003664
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003665 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003666 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003667 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003668 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003669 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003670 QualType qt = Field->getType();
3671 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003672 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003673 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003674 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003675 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003676 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003677 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003678 return;
3679 }
Mike Stump11289f42009-09-09 15:08:12 +00003680
Chris Lattnere7cabb92009-07-13 00:10:46 +00003681 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003682 if (FD && FD->isBitField())
3683 EncodeBitField(this, S, FD);
3684 else
3685 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003686 return;
3687 }
Mike Stump11289f42009-09-09 15:08:12 +00003688
Chris Lattnere7cabb92009-07-13 00:10:46 +00003689 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003690 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003691 return;
3692 }
Mike Stump11289f42009-09-09 15:08:12 +00003693
John McCall8ccfcb52009-09-24 19:53:00 +00003694 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003695 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003696 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003697 S += '{';
3698 const IdentifierInfo *II = OI->getIdentifier();
3699 S += II->getName();
3700 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003701 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003702 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003703 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003704 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003705 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003706 RecFields[i]);
3707 else
Mike Stump11289f42009-09-09 15:08:12 +00003708 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003709 FD);
3710 }
3711 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003712 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003713 }
Mike Stump11289f42009-09-09 15:08:12 +00003714
John McCall9dd450b2009-09-21 23:43:11 +00003715 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003716 if (OPT->isObjCIdType()) {
3717 S += '@';
3718 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003719 }
Mike Stump11289f42009-09-09 15:08:12 +00003720
Steve Narofff0c86112009-10-28 22:03:49 +00003721 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3722 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3723 // Since this is a binary compatibility issue, need to consult with runtime
3724 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003725 S += '#';
3726 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003727 }
Mike Stump11289f42009-09-09 15:08:12 +00003728
Chris Lattnere7cabb92009-07-13 00:10:46 +00003729 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003730 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003731 ExpandPointedToStructures,
3732 ExpandStructures, FD);
3733 if (FD || EncodingProperty) {
3734 // Note that we do extended encoding of protocol qualifer list
3735 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003736 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003737 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3738 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003739 S += '<';
3740 S += (*I)->getNameAsString();
3741 S += '>';
3742 }
3743 S += '"';
3744 }
3745 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003746 }
Mike Stump11289f42009-09-09 15:08:12 +00003747
Chris Lattnere7cabb92009-07-13 00:10:46 +00003748 QualType PointeeTy = OPT->getPointeeType();
3749 if (!EncodingProperty &&
3750 isa<TypedefType>(PointeeTy.getTypePtr())) {
3751 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003752 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003753 // {...};
3754 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003755 getObjCEncodingForTypeImpl(PointeeTy, S,
3756 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003757 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003758 return;
3759 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003760
3761 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003762 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003763 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003764 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003765 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3766 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003767 S += '<';
3768 S += (*I)->getNameAsString();
3769 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003770 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003771 S += '"';
3772 }
3773 return;
3774 }
Mike Stump11289f42009-09-09 15:08:12 +00003775
Chris Lattnere7cabb92009-07-13 00:10:46 +00003776 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003777}
3778
Mike Stump11289f42009-09-09 15:08:12 +00003779void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003780 std::string& S) const {
3781 if (QT & Decl::OBJC_TQ_In)
3782 S += 'n';
3783 if (QT & Decl::OBJC_TQ_Inout)
3784 S += 'N';
3785 if (QT & Decl::OBJC_TQ_Out)
3786 S += 'o';
3787 if (QT & Decl::OBJC_TQ_Bycopy)
3788 S += 'O';
3789 if (QT & Decl::OBJC_TQ_Byref)
3790 S += 'R';
3791 if (QT & Decl::OBJC_TQ_Oneway)
3792 S += 'V';
3793}
3794
Chris Lattnere7cabb92009-07-13 00:10:46 +00003795void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003796 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003797
Anders Carlsson87c149b2007-10-11 01:00:40 +00003798 BuiltinVaListType = T;
3799}
3800
Chris Lattnere7cabb92009-07-13 00:10:46 +00003801void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003802 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003803}
3804
Chris Lattnere7cabb92009-07-13 00:10:46 +00003805void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003806 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003807}
3808
Chris Lattnere7cabb92009-07-13 00:10:46 +00003809void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003810 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003811}
3812
Chris Lattnere7cabb92009-07-13 00:10:46 +00003813void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003814 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003815}
3816
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003817void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003818 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003819 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003820
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003821 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003822}
3823
John McCalld28ae272009-12-02 08:04:21 +00003824/// \brief Retrieve the template name that corresponds to a non-empty
3825/// lookup.
John McCallad371252010-01-20 00:46:10 +00003826TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3827 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003828 unsigned size = End - Begin;
3829 assert(size > 1 && "set is not overloaded!");
3830
3831 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3832 size * sizeof(FunctionTemplateDecl*));
3833 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3834
3835 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003836 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003837 NamedDecl *D = *I;
3838 assert(isa<FunctionTemplateDecl>(D) ||
3839 (isa<UsingShadowDecl>(D) &&
3840 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3841 *Storage++ = D;
3842 }
3843
3844 return TemplateName(OT);
3845}
3846
Douglas Gregordc572a32009-03-30 22:58:21 +00003847/// \brief Retrieve the template name that represents a qualified
3848/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003849TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003850 bool TemplateKeyword,
3851 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003852 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003853 llvm::FoldingSetNodeID ID;
3854 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3855
3856 void *InsertPos = 0;
3857 QualifiedTemplateName *QTN =
3858 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3859 if (!QTN) {
3860 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3861 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3862 }
3863
3864 return TemplateName(QTN);
3865}
3866
3867/// \brief Retrieve the template name that represents a dependent
3868/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003869TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003870 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003871 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003872 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003873
3874 llvm::FoldingSetNodeID ID;
3875 DependentTemplateName::Profile(ID, NNS, Name);
3876
3877 void *InsertPos = 0;
3878 DependentTemplateName *QTN =
3879 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3880
3881 if (QTN)
3882 return TemplateName(QTN);
3883
3884 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3885 if (CanonNNS == NNS) {
3886 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3887 } else {
3888 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3889 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003890 DependentTemplateName *CheckQTN =
3891 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3892 assert(!CheckQTN && "Dependent type name canonicalization broken");
3893 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00003894 }
3895
3896 DependentTemplateNames.InsertNode(QTN, InsertPos);
3897 return TemplateName(QTN);
3898}
3899
Douglas Gregor71395fa2009-11-04 00:56:37 +00003900/// \brief Retrieve the template name that represents a dependent
3901/// template name such as \c MetaFun::template operator+.
3902TemplateName
3903ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3904 OverloadedOperatorKind Operator) {
3905 assert((!NNS || NNS->isDependent()) &&
3906 "Nested name specifier must be dependent");
3907
3908 llvm::FoldingSetNodeID ID;
3909 DependentTemplateName::Profile(ID, NNS, Operator);
3910
3911 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00003912 DependentTemplateName *QTN
3913 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00003914
3915 if (QTN)
3916 return TemplateName(QTN);
3917
3918 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3919 if (CanonNNS == NNS) {
3920 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3921 } else {
3922 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3923 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003924
3925 DependentTemplateName *CheckQTN
3926 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3927 assert(!CheckQTN && "Dependent template name canonicalization broken");
3928 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00003929 }
3930
3931 DependentTemplateNames.InsertNode(QTN, InsertPos);
3932 return TemplateName(QTN);
3933}
3934
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003935/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003936/// TargetInfo, produce the corresponding type. The unsigned @p Type
3937/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003938CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003939 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003940 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003941 case TargetInfo::SignedShort: return ShortTy;
3942 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3943 case TargetInfo::SignedInt: return IntTy;
3944 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3945 case TargetInfo::SignedLong: return LongTy;
3946 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3947 case TargetInfo::SignedLongLong: return LongLongTy;
3948 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3949 }
3950
3951 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003952 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003953}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003954
3955//===----------------------------------------------------------------------===//
3956// Type Predicates.
3957//===----------------------------------------------------------------------===//
3958
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003959/// isObjCNSObjectType - Return true if this is an NSObject object using
3960/// NSObject attribute on a c-style pointer type.
3961/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003962/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003963///
3964bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3965 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3966 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003967 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003968 return true;
3969 }
Mike Stump11289f42009-09-09 15:08:12 +00003970 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003971}
3972
Fariborz Jahanian96207692009-02-18 21:49:28 +00003973/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3974/// garbage collection attribute.
3975///
John McCall8ccfcb52009-09-24 19:53:00 +00003976Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3977 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003978 if (getLangOptions().ObjC1 &&
3979 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003980 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003981 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003982 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003983 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003984 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003985 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003986 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003987 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003988 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003989 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003990 // Non-pointers have none gc'able attribute regardless of the attribute
3991 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003992 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003993 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003994 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003995 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003996}
3997
Chris Lattner49af6a42008-04-07 06:51:04 +00003998//===----------------------------------------------------------------------===//
3999// Type Compatibility Testing
4000//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004001
Mike Stump11289f42009-09-09 15:08:12 +00004002/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004003/// compatible.
4004static bool areCompatVectorTypes(const VectorType *LHS,
4005 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004006 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004007 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004008 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004009}
4010
Steve Naroff8e6aee52009-07-23 01:01:38 +00004011//===----------------------------------------------------------------------===//
4012// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4013//===----------------------------------------------------------------------===//
4014
4015/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4016/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004017bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4018 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004019 if (lProto == rProto)
4020 return true;
4021 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4022 E = rProto->protocol_end(); PI != E; ++PI)
4023 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4024 return true;
4025 return false;
4026}
4027
Steve Naroff8e6aee52009-07-23 01:01:38 +00004028/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4029/// return true if lhs's protocols conform to rhs's protocol; false
4030/// otherwise.
4031bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4032 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4033 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4034 return false;
4035}
4036
4037/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4038/// ObjCQualifiedIDType.
4039bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4040 bool compare) {
4041 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004042 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004043 lhs->isObjCIdType() || lhs->isObjCClassType())
4044 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004045 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004046 rhs->isObjCIdType() || rhs->isObjCClassType())
4047 return true;
4048
4049 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004050 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004051
Steve Naroff8e6aee52009-07-23 01:01:38 +00004052 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004053
Steve Naroff8e6aee52009-07-23 01:01:38 +00004054 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004055 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004056 // make sure we check the class hierarchy.
4057 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4058 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4059 E = lhsQID->qual_end(); I != E; ++I) {
4060 // when comparing an id<P> on lhs with a static type on rhs,
4061 // see if static class implements all of id's protocols, directly or
4062 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004063 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004064 return false;
4065 }
4066 }
4067 // If there are no qualifiers and no interface, we have an 'id'.
4068 return true;
4069 }
Mike Stump11289f42009-09-09 15:08:12 +00004070 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004071 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4072 E = lhsQID->qual_end(); I != E; ++I) {
4073 ObjCProtocolDecl *lhsProto = *I;
4074 bool match = false;
4075
4076 // when comparing an id<P> on lhs with a static type on rhs,
4077 // see if static class implements all of id's protocols, directly or
4078 // through its super class and categories.
4079 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4080 E = rhsOPT->qual_end(); J != E; ++J) {
4081 ObjCProtocolDecl *rhsProto = *J;
4082 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4083 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4084 match = true;
4085 break;
4086 }
4087 }
Mike Stump11289f42009-09-09 15:08:12 +00004088 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004089 // make sure we check the class hierarchy.
4090 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4091 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4092 E = lhsQID->qual_end(); I != E; ++I) {
4093 // when comparing an id<P> on lhs with a static type on rhs,
4094 // see if static class implements all of id's protocols, directly or
4095 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004096 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004097 match = true;
4098 break;
4099 }
4100 }
4101 }
4102 if (!match)
4103 return false;
4104 }
Mike Stump11289f42009-09-09 15:08:12 +00004105
Steve Naroff8e6aee52009-07-23 01:01:38 +00004106 return true;
4107 }
Mike Stump11289f42009-09-09 15:08:12 +00004108
Steve Naroff8e6aee52009-07-23 01:01:38 +00004109 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4110 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4111
Mike Stump11289f42009-09-09 15:08:12 +00004112 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004113 lhs->getAsObjCInterfacePointerType()) {
4114 if (lhsOPT->qual_empty()) {
4115 bool match = false;
4116 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4117 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4118 E = rhsQID->qual_end(); I != E; ++I) {
4119 // when comparing an id<P> on lhs with a static type on rhs,
4120 // see if static class implements all of id's protocols, directly or
4121 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004122 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004123 match = true;
4124 break;
4125 }
4126 }
4127 if (!match)
4128 return false;
4129 }
4130 return true;
4131 }
Mike Stump11289f42009-09-09 15:08:12 +00004132 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004133 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4134 E = lhsOPT->qual_end(); I != E; ++I) {
4135 ObjCProtocolDecl *lhsProto = *I;
4136 bool match = false;
4137
4138 // when comparing an id<P> on lhs with a static type on rhs,
4139 // see if static class implements all of id's protocols, directly or
4140 // through its super class and categories.
4141 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4142 E = rhsQID->qual_end(); J != E; ++J) {
4143 ObjCProtocolDecl *rhsProto = *J;
4144 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4145 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4146 match = true;
4147 break;
4148 }
4149 }
4150 if (!match)
4151 return false;
4152 }
4153 return true;
4154 }
4155 return false;
4156}
4157
Eli Friedman47f77112008-08-22 00:56:42 +00004158/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004159/// compatible for assignment from RHS to LHS. This handles validation of any
4160/// protocol qualifiers on the LHS or RHS.
4161///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004162bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4163 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004164 // If either type represents the built-in 'id' or 'Class' types, return true.
4165 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004166 return true;
4167
Steve Naroff8e6aee52009-07-23 01:01:38 +00004168 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00004169 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4170 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004171 false);
4172
Steve Naroff7cae42b2009-07-10 23:34:53 +00004173 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4174 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00004175 if (LHS && RHS) // We have 2 user-defined types.
4176 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004177
Steve Naroff8e6aee52009-07-23 01:01:38 +00004178 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004179}
4180
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004181/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4182/// for providing type-safty for objective-c pointers used to pass/return
4183/// arguments in block literals. When passed as arguments, passing 'A*' where
4184/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4185/// not OK. For the return type, the opposite is not OK.
4186bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4187 const ObjCObjectPointerType *LHSOPT,
4188 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004189 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004190 return true;
4191
4192 if (LHSOPT->isObjCBuiltinType()) {
4193 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4194 }
4195
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004196 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004197 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4198 QualType(RHSOPT,0),
4199 false);
4200
4201 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4202 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4203 if (LHS && RHS) { // We have 2 user-defined types.
4204 if (LHS != RHS) {
4205 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4206 return false;
4207 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4208 return true;
4209 }
4210 else
4211 return true;
4212 }
4213 return false;
4214}
4215
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004216/// getIntersectionOfProtocols - This routine finds the intersection of set
4217/// of protocols inherited from two distinct objective-c pointer objects.
4218/// It is used to build composite qualifier list of the composite type of
4219/// the conditional expression involving two objective-c pointer objects.
4220static
4221void getIntersectionOfProtocols(ASTContext &Context,
4222 const ObjCObjectPointerType *LHSOPT,
4223 const ObjCObjectPointerType *RHSOPT,
4224 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4225
4226 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4227 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4228
4229 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4230 unsigned LHSNumProtocols = LHS->getNumProtocols();
4231 if (LHSNumProtocols > 0)
4232 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4233 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004234 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4235 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004236 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4237 LHSInheritedProtocols.end());
4238 }
4239
4240 unsigned RHSNumProtocols = RHS->getNumProtocols();
4241 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004242 ObjCProtocolDecl **RHSProtocols =
4243 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004244 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4245 if (InheritedProtocolSet.count(RHSProtocols[i]))
4246 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4247 }
4248 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004249 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004250 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004251 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4252 RHSInheritedProtocols.begin(),
4253 E = RHSInheritedProtocols.end(); I != E; ++I)
4254 if (InheritedProtocolSet.count((*I)))
4255 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004256 }
4257}
4258
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004259/// areCommonBaseCompatible - Returns common base class of the two classes if
4260/// one found. Note that this is O'2 algorithm. But it will be called as the
4261/// last type comparison in a ?-exp of ObjC pointer types before a
4262/// warning is issued. So, its invokation is extremely rare.
4263QualType ASTContext::areCommonBaseCompatible(
4264 const ObjCObjectPointerType *LHSOPT,
4265 const ObjCObjectPointerType *RHSOPT) {
4266 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4267 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4268 if (!LHS || !RHS)
4269 return QualType();
4270
4271 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4272 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4273 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004274 if (canAssignObjCInterfaces(LHS, RHS)) {
4275 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4276 getIntersectionOfProtocols(*this,
4277 LHSOPT, RHSOPT, IntersectionOfProtocols);
4278 if (IntersectionOfProtocols.empty())
4279 LHSTy = getObjCObjectPointerType(LHSTy);
4280 else
4281 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4282 IntersectionOfProtocols.size());
4283 return LHSTy;
4284 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004285 }
4286
4287 return QualType();
4288}
4289
Eli Friedman47f77112008-08-22 00:56:42 +00004290bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4291 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00004292 // Verify that the base decls are compatible: the RHS must be a subclass of
4293 // the LHS.
4294 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4295 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004296
Chris Lattner49af6a42008-04-07 06:51:04 +00004297 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4298 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004299 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004300 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004301
Chris Lattner49af6a42008-04-07 06:51:04 +00004302 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4303 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004304 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004305 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004306
Steve Naroffc277ad12009-07-18 15:33:26 +00004307 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4308 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004309 LHSPI != LHSPE; LHSPI++) {
4310 bool RHSImplementsProtocol = false;
4311
4312 // If the RHS doesn't implement the protocol on the left, the types
4313 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00004314 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004315 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004316 RHSPI != RHSPE; RHSPI++) {
4317 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004318 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004319 break;
4320 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004321 }
4322 // FIXME: For better diagnostics, consider passing back the protocol name.
4323 if (!RHSImplementsProtocol)
4324 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004325 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004326 // The RHS implements all protocols listed on the LHS.
4327 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004328}
4329
Steve Naroffb7605152009-02-12 17:52:19 +00004330bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4331 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004332 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4333 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004334
Steve Naroff7cae42b2009-07-10 23:34:53 +00004335 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004336 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004337
4338 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4339 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004340}
4341
Mike Stump11289f42009-09-09 15:08:12 +00004342/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004343/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004344/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004345/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004346bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004347 if (getLangOptions().CPlusPlus)
4348 return hasSameType(LHS, RHS);
4349
Eli Friedman47f77112008-08-22 00:56:42 +00004350 return !mergeTypes(LHS, RHS).isNull();
4351}
4352
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004353bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4354 return !mergeTypes(LHS, RHS, true).isNull();
4355}
4356
4357QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4358 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004359 const FunctionType *lbase = lhs->getAs<FunctionType>();
4360 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004361 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4362 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004363 bool allLTypes = true;
4364 bool allRTypes = true;
4365
4366 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004367 QualType retType;
4368 if (OfBlockPointer)
4369 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4370 else
4371 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004372 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004373 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004374 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004375 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004376 allRTypes = false;
Mike Stumpea086c72009-07-25 23:24:03 +00004377 // FIXME: double check this
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004378 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4379 // rbase->getRegParmAttr() != 0 &&
4380 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004381 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4382 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004383 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4384 lbaseInfo.getRegParm();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004385 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004386 if (NoReturn != lbaseInfo.getNoReturn() ||
4387 RegParm != lbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004388 allLTypes = false;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004389 if (NoReturn != rbaseInfo.getNoReturn() ||
4390 RegParm != rbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004391 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004392 CallingConv lcc = lbaseInfo.getCC();
4393 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004394 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004395 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004396 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004397
Eli Friedman47f77112008-08-22 00:56:42 +00004398 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004399 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4400 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004401 unsigned lproto_nargs = lproto->getNumArgs();
4402 unsigned rproto_nargs = rproto->getNumArgs();
4403
4404 // Compatible functions must have the same number of arguments
4405 if (lproto_nargs != rproto_nargs)
4406 return QualType();
4407
4408 // Variadic and non-variadic functions aren't compatible
4409 if (lproto->isVariadic() != rproto->isVariadic())
4410 return QualType();
4411
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004412 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4413 return QualType();
4414
Eli Friedman47f77112008-08-22 00:56:42 +00004415 // Check argument compatibility
4416 llvm::SmallVector<QualType, 10> types;
4417 for (unsigned i = 0; i < lproto_nargs; i++) {
4418 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4419 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004420 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004421 if (argtype.isNull()) return QualType();
4422 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004423 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4424 allLTypes = false;
4425 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4426 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004427 }
4428 if (allLTypes) return lhs;
4429 if (allRTypes) return rhs;
4430 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004431 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004432 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004433 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004434 }
4435
4436 if (lproto) allRTypes = false;
4437 if (rproto) allLTypes = false;
4438
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004439 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004440 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004441 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004442 if (proto->isVariadic()) return QualType();
4443 // Check that the types are compatible with the types that
4444 // would result from default argument promotions (C99 6.7.5.3p15).
4445 // The only types actually affected are promotable integer
4446 // types and floats, which would be passed as a different
4447 // type depending on whether the prototype is visible.
4448 unsigned proto_nargs = proto->getNumArgs();
4449 for (unsigned i = 0; i < proto_nargs; ++i) {
4450 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004451
4452 // Look at the promotion type of enum types, since that is the type used
4453 // to pass enum values.
4454 if (const EnumType *Enum = argTy->getAs<EnumType>())
4455 argTy = Enum->getDecl()->getPromotionType();
4456
Eli Friedman47f77112008-08-22 00:56:42 +00004457 if (argTy->isPromotableIntegerType() ||
4458 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4459 return QualType();
4460 }
4461
4462 if (allLTypes) return lhs;
4463 if (allRTypes) return rhs;
4464 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004465 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004466 proto->getTypeQuals(),
4467 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004468 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004469 }
4470
4471 if (allLTypes) return lhs;
4472 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004473 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004474 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004475}
4476
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004477QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4478 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004479 // C++ [expr]: If an expression initially has the type "reference to T", the
4480 // type is adjusted to "T" prior to any further analysis, the expression
4481 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004482 // expression is an lvalue unless the reference is an rvalue reference and
4483 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004484 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4485 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4486
Eli Friedman47f77112008-08-22 00:56:42 +00004487 QualType LHSCan = getCanonicalType(LHS),
4488 RHSCan = getCanonicalType(RHS);
4489
4490 // If two types are identical, they are compatible.
4491 if (LHSCan == RHSCan)
4492 return LHS;
4493
John McCall8ccfcb52009-09-24 19:53:00 +00004494 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004495 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4496 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004497 if (LQuals != RQuals) {
4498 // If any of these qualifiers are different, we have a type
4499 // mismatch.
4500 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4501 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4502 return QualType();
4503
4504 // Exactly one GC qualifier difference is allowed: __strong is
4505 // okay if the other type has no GC qualifier but is an Objective
4506 // C object pointer (i.e. implicitly strong by default). We fix
4507 // this by pretending that the unqualified type was actually
4508 // qualified __strong.
4509 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4510 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4511 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4512
4513 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4514 return QualType();
4515
4516 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4517 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4518 }
4519 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4520 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4521 }
Eli Friedman47f77112008-08-22 00:56:42 +00004522 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004523 }
4524
4525 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004526
Eli Friedmandcca6332009-06-01 01:22:52 +00004527 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4528 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004529
Chris Lattnerfd652912008-01-14 05:45:46 +00004530 // We want to consider the two function types to be the same for these
4531 // comparisons, just force one to the other.
4532 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4533 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004534
4535 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004536 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4537 LHSClass = Type::ConstantArray;
4538 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4539 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004540
Nate Begemance4d7fc2008-04-18 23:10:10 +00004541 // Canonicalize ExtVector -> Vector.
4542 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4543 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004544
Chris Lattner95554662008-04-07 05:43:21 +00004545 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004546 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004547 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004548 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004549 // Compatibility is based on the underlying type, not the promotion
4550 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004551 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004552 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4553 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004554 }
John McCall9dd450b2009-09-21 23:43:11 +00004555 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004556 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4557 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004558 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004559
Eli Friedman47f77112008-08-22 00:56:42 +00004560 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004561 }
Eli Friedman47f77112008-08-22 00:56:42 +00004562
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004563 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004564 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004565#define TYPE(Class, Base)
4566#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004567#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004568#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4569#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4570#include "clang/AST/TypeNodes.def"
4571 assert(false && "Non-canonical and dependent types shouldn't get here");
4572 return QualType();
4573
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004574 case Type::LValueReference:
4575 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004576 case Type::MemberPointer:
4577 assert(false && "C++ should never be in mergeTypes");
4578 return QualType();
4579
4580 case Type::IncompleteArray:
4581 case Type::VariableArray:
4582 case Type::FunctionProto:
4583 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004584 assert(false && "Types are eliminated above");
4585 return QualType();
4586
Chris Lattnerfd652912008-01-14 05:45:46 +00004587 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004588 {
4589 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004590 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4591 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004592 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4593 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004594 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004595 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004596 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004597 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004598 return getPointerType(ResultType);
4599 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004600 case Type::BlockPointer:
4601 {
4602 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004603 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4604 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004605 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004606 if (ResultType.isNull()) return QualType();
4607 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4608 return LHS;
4609 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4610 return RHS;
4611 return getBlockPointerType(ResultType);
4612 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004613 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004614 {
4615 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4616 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4617 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4618 return QualType();
4619
4620 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4621 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4622 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4623 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004624 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4625 return LHS;
4626 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4627 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004628 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4629 ArrayType::ArraySizeModifier(), 0);
4630 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4631 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004632 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4633 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004634 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4635 return LHS;
4636 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4637 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004638 if (LVAT) {
4639 // FIXME: This isn't correct! But tricky to implement because
4640 // the array's size has to be the size of LHS, but the type
4641 // has to be different.
4642 return LHS;
4643 }
4644 if (RVAT) {
4645 // FIXME: This isn't correct! But tricky to implement because
4646 // the array's size has to be the size of RHS, but the type
4647 // has to be different.
4648 return RHS;
4649 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004650 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4651 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004652 return getIncompleteArrayType(ResultType,
4653 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004654 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004655 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004656 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004657 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004658 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004659 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004660 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004661 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004662 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004663 case Type::Complex:
4664 // Distinct complex types are incompatible.
4665 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004666 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004667 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004668 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4669 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004670 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004671 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004672 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00004673 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004674 // FIXME: This should be type compatibility, e.g. whether
4675 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00004676 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4677 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00004678 if (LHSIface && RHSIface &&
4679 canAssignObjCInterfaces(LHSIface, RHSIface))
4680 return LHS;
4681
Eli Friedman47f77112008-08-22 00:56:42 +00004682 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004683 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004684 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004685 if (OfBlockPointer) {
4686 if (canAssignObjCInterfacesInBlockPointer(
4687 LHS->getAs<ObjCObjectPointerType>(),
4688 RHS->getAs<ObjCObjectPointerType>()))
4689 return LHS;
4690 return QualType();
4691 }
John McCall9dd450b2009-09-21 23:43:11 +00004692 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4693 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004694 return LHS;
4695
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004696 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004697 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004698 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004699
4700 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004701}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004702
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004703//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004704// Integer Predicates
4705//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004706
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004707unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004708 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004709 return 1;
John McCall56774992009-12-09 09:09:27 +00004710 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004711 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004712 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004713 return (unsigned)getTypeSize(T);
4714}
4715
4716QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4717 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004718
4719 // Turn <4 x signed int> -> <4 x unsigned int>
4720 if (const VectorType *VTy = T->getAs<VectorType>())
4721 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004722 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004723
4724 // For enums, we return the unsigned version of the base type.
4725 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004726 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004727
4728 const BuiltinType *BTy = T->getAs<BuiltinType>();
4729 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004730 switch (BTy->getKind()) {
4731 case BuiltinType::Char_S:
4732 case BuiltinType::SChar:
4733 return UnsignedCharTy;
4734 case BuiltinType::Short:
4735 return UnsignedShortTy;
4736 case BuiltinType::Int:
4737 return UnsignedIntTy;
4738 case BuiltinType::Long:
4739 return UnsignedLongTy;
4740 case BuiltinType::LongLong:
4741 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004742 case BuiltinType::Int128:
4743 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004744 default:
4745 assert(0 && "Unexpected signed integer type");
4746 return QualType();
4747 }
4748}
4749
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004750ExternalASTSource::~ExternalASTSource() { }
4751
4752void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004753
4754
4755//===----------------------------------------------------------------------===//
4756// Builtin Type Computation
4757//===----------------------------------------------------------------------===//
4758
4759/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4760/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004761static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004762 ASTContext::GetBuiltinTypeError &Error,
4763 bool AllowTypeModifiers = true) {
4764 // Modifiers.
4765 int HowLong = 0;
4766 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004767
Chris Lattnerecd79c62009-06-14 00:45:47 +00004768 // Read the modifiers first.
4769 bool Done = false;
4770 while (!Done) {
4771 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004772 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004773 case 'S':
4774 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4775 assert(!Signed && "Can't use 'S' modifier multiple times!");
4776 Signed = true;
4777 break;
4778 case 'U':
4779 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4780 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4781 Unsigned = true;
4782 break;
4783 case 'L':
4784 assert(HowLong <= 2 && "Can't have LLLL modifier");
4785 ++HowLong;
4786 break;
4787 }
4788 }
4789
4790 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004791
Chris Lattnerecd79c62009-06-14 00:45:47 +00004792 // Read the base type.
4793 switch (*Str++) {
4794 default: assert(0 && "Unknown builtin type letter!");
4795 case 'v':
4796 assert(HowLong == 0 && !Signed && !Unsigned &&
4797 "Bad modifiers used with 'v'!");
4798 Type = Context.VoidTy;
4799 break;
4800 case 'f':
4801 assert(HowLong == 0 && !Signed && !Unsigned &&
4802 "Bad modifiers used with 'f'!");
4803 Type = Context.FloatTy;
4804 break;
4805 case 'd':
4806 assert(HowLong < 2 && !Signed && !Unsigned &&
4807 "Bad modifiers used with 'd'!");
4808 if (HowLong)
4809 Type = Context.LongDoubleTy;
4810 else
4811 Type = Context.DoubleTy;
4812 break;
4813 case 's':
4814 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4815 if (Unsigned)
4816 Type = Context.UnsignedShortTy;
4817 else
4818 Type = Context.ShortTy;
4819 break;
4820 case 'i':
4821 if (HowLong == 3)
4822 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4823 else if (HowLong == 2)
4824 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4825 else if (HowLong == 1)
4826 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4827 else
4828 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4829 break;
4830 case 'c':
4831 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4832 if (Signed)
4833 Type = Context.SignedCharTy;
4834 else if (Unsigned)
4835 Type = Context.UnsignedCharTy;
4836 else
4837 Type = Context.CharTy;
4838 break;
4839 case 'b': // boolean
4840 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4841 Type = Context.BoolTy;
4842 break;
4843 case 'z': // size_t.
4844 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4845 Type = Context.getSizeType();
4846 break;
4847 case 'F':
4848 Type = Context.getCFConstantStringType();
4849 break;
4850 case 'a':
4851 Type = Context.getBuiltinVaListType();
4852 assert(!Type.isNull() && "builtin va list type not initialized!");
4853 break;
4854 case 'A':
4855 // This is a "reference" to a va_list; however, what exactly
4856 // this means depends on how va_list is defined. There are two
4857 // different kinds of va_list: ones passed by value, and ones
4858 // passed by reference. An example of a by-value va_list is
4859 // x86, where va_list is a char*. An example of by-ref va_list
4860 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4861 // we want this argument to be a char*&; for x86-64, we want
4862 // it to be a __va_list_tag*.
4863 Type = Context.getBuiltinVaListType();
4864 assert(!Type.isNull() && "builtin va list type not initialized!");
4865 if (Type->isArrayType()) {
4866 Type = Context.getArrayDecayedType(Type);
4867 } else {
4868 Type = Context.getLValueReferenceType(Type);
4869 }
4870 break;
4871 case 'V': {
4872 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004873 unsigned NumElements = strtoul(Str, &End, 10);
4874 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004875
Chris Lattnerecd79c62009-06-14 00:45:47 +00004876 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004877
Chris Lattnerecd79c62009-06-14 00:45:47 +00004878 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00004879 // FIXME: Don't know what to do about AltiVec.
4880 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004881 break;
4882 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004883 case 'X': {
4884 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4885 Type = Context.getComplexType(ElementType);
4886 break;
4887 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004888 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004889 Type = Context.getFILEType();
4890 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004891 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004892 return QualType();
4893 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004894 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004895 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004896 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004897 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004898 else
4899 Type = Context.getjmp_bufType();
4900
Mike Stump2adb4da2009-07-28 23:47:15 +00004901 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004902 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004903 return QualType();
4904 }
4905 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004906 }
Mike Stump11289f42009-09-09 15:08:12 +00004907
Chris Lattnerecd79c62009-06-14 00:45:47 +00004908 if (!AllowTypeModifiers)
4909 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004910
Chris Lattnerecd79c62009-06-14 00:45:47 +00004911 Done = false;
4912 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00004913 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00004914 default: Done = true; --Str; break;
4915 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00004916 case '&':
John McCallb8b94662010-03-12 04:21:28 +00004917 {
4918 // Both pointers and references can have their pointee types
4919 // qualified with an address space.
4920 char *End;
4921 unsigned AddrSpace = strtoul(Str, &End, 10);
4922 if (End != Str && AddrSpace != 0) {
4923 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4924 Str = End;
4925 }
4926 }
4927 if (c == '*')
4928 Type = Context.getPointerType(Type);
4929 else
4930 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004931 break;
4932 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4933 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004934 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004935 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00004936 case 'D':
4937 Type = Context.getVolatileType(Type);
4938 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004939 }
4940 }
Mike Stump11289f42009-09-09 15:08:12 +00004941
Chris Lattnerecd79c62009-06-14 00:45:47 +00004942 return Type;
4943}
4944
4945/// GetBuiltinType - Return the type for the specified builtin.
4946QualType ASTContext::GetBuiltinType(unsigned id,
4947 GetBuiltinTypeError &Error) {
4948 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004949
Chris Lattnerecd79c62009-06-14 00:45:47 +00004950 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004951
Chris Lattnerecd79c62009-06-14 00:45:47 +00004952 Error = GE_None;
4953 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4954 if (Error != GE_None)
4955 return QualType();
4956 while (TypeStr[0] && TypeStr[0] != '.') {
4957 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4958 if (Error != GE_None)
4959 return QualType();
4960
4961 // Do array -> pointer decay. The builtin should use the decayed type.
4962 if (Ty->isArrayType())
4963 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004964
Chris Lattnerecd79c62009-06-14 00:45:47 +00004965 ArgTypes.push_back(Ty);
4966 }
4967
4968 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4969 "'.' should only occur at end of builtin type list!");
4970
4971 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4972 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4973 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00004974
4975 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00004976 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00004977 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004978 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00004979}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004980
4981QualType
4982ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4983 // Perform the usual unary conversions. We do this early so that
4984 // integral promotions to "int" can allow us to exit early, in the
4985 // lhs == rhs check. Also, for conversion purposes, we ignore any
4986 // qualifiers. For example, "const float" and "float" are
4987 // equivalent.
4988 if (lhs->isPromotableIntegerType())
4989 lhs = getPromotedIntegerType(lhs);
4990 else
4991 lhs = lhs.getUnqualifiedType();
4992 if (rhs->isPromotableIntegerType())
4993 rhs = getPromotedIntegerType(rhs);
4994 else
4995 rhs = rhs.getUnqualifiedType();
4996
4997 // If both types are identical, no conversion is needed.
4998 if (lhs == rhs)
4999 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005000
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005001 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
5002 // The caller can deal with this (e.g. pointer + int).
5003 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5004 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005005
5006 // At this point, we have two different arithmetic types.
5007
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005008 // Handle complex types first (C99 6.3.1.8p1).
5009 if (lhs->isComplexType() || rhs->isComplexType()) {
5010 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00005011 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005012 // convert the rhs to the lhs complex type.
5013 return lhs;
5014 }
Mike Stump11289f42009-09-09 15:08:12 +00005015 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005016 // convert the lhs to the rhs complex type.
5017 return rhs;
5018 }
5019 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00005020 // When both operands are complex, the shorter operand is converted to the
5021 // type of the longer, and that is the type of the result. This corresponds
5022 // to what is done when combining two real floating-point operands.
5023 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005024 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00005025 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005026 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00005027 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005028 // "double _Complex" is promoted to "long double _Complex".
5029 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005030
5031 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005032 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005033 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005034 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00005035 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005036 // At this point, lhs and rhs have the same rank/size. Now, make sure the
5037 // domains match. This is a requirement for our implementation, C99
5038 // does not require this promotion.
5039 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5040 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5041 return rhs;
5042 } else { // handle "_Complex double, double".
5043 return lhs;
5044 }
5045 }
5046 return lhs; // The domain/size match exactly.
5047 }
5048 // Now handle "real" floating types (i.e. float, double, long double).
5049 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5050 // if we have an integer operand, the result is the real floating type.
5051 if (rhs->isIntegerType()) {
5052 // convert rhs to the lhs floating point type.
5053 return lhs;
5054 }
5055 if (rhs->isComplexIntegerType()) {
5056 // convert rhs to the complex floating point type.
5057 return getComplexType(lhs);
5058 }
5059 if (lhs->isIntegerType()) {
5060 // convert lhs to the rhs floating point type.
5061 return rhs;
5062 }
Mike Stump11289f42009-09-09 15:08:12 +00005063 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005064 // convert lhs to the complex floating point type.
5065 return getComplexType(rhs);
5066 }
5067 // We have two real floating types, float/complex combos were handled above.
5068 // Convert the smaller operand to the bigger result.
5069 int result = getFloatingTypeOrder(lhs, rhs);
5070 if (result > 0) // convert the rhs
5071 return lhs;
5072 assert(result < 0 && "illegal float comparison");
5073 return rhs; // convert the lhs
5074 }
5075 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5076 // Handle GCC complex int extension.
5077 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5078 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5079
5080 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005081 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005082 rhsComplexInt->getElementType()) >= 0)
5083 return lhs; // convert the rhs
5084 return rhs;
5085 } else if (lhsComplexInt && rhs->isIntegerType()) {
5086 // convert the rhs to the lhs complex type.
5087 return lhs;
5088 } else if (rhsComplexInt && lhs->isIntegerType()) {
5089 // convert the lhs to the rhs complex type.
5090 return rhs;
5091 }
5092 }
5093 // Finally, we have two differing integer types.
5094 // The rules for this case are in C99 6.3.1.8
5095 int compare = getIntegerTypeOrder(lhs, rhs);
5096 bool lhsSigned = lhs->isSignedIntegerType(),
5097 rhsSigned = rhs->isSignedIntegerType();
5098 QualType destType;
5099 if (lhsSigned == rhsSigned) {
5100 // Same signedness; use the higher-ranked type
5101 destType = compare >= 0 ? lhs : rhs;
5102 } else if (compare != (lhsSigned ? 1 : -1)) {
5103 // The unsigned type has greater than or equal rank to the
5104 // signed type, so use the unsigned type
5105 destType = lhsSigned ? rhs : lhs;
5106 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5107 // The two types are different widths; if we are here, that
5108 // means the signed type is larger than the unsigned type, so
5109 // use the signed type.
5110 destType = lhsSigned ? lhs : rhs;
5111 } else {
5112 // The signed type is higher-ranked than the unsigned type,
5113 // but isn't actually any bigger (like unsigned int and long
5114 // on most 32-bit systems). Use the unsigned type corresponding
5115 // to the signed type.
5116 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5117 }
5118 return destType;
5119}