blob: 91fafbabc2a2d21f75edefbf0aad15d3881c75b8 [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 +0000329ASTContext::overridden_cxx_method_iterator
330ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
331 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
332 = OverriddenMethods.find(Method);
333 if (Pos == OverriddenMethods.end())
334 return 0;
335
336 return Pos->second.begin();
337}
338
339ASTContext::overridden_cxx_method_iterator
340ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
341 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
342 = OverriddenMethods.find(Method);
343 if (Pos == OverriddenMethods.end())
344 return 0;
345
346 return Pos->second.end();
347}
348
349void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
350 const CXXMethodDecl *Overridden) {
351 OverriddenMethods[Method].push_back(Overridden);
352}
353
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000354namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000355 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000356 : std::binary_function<SourceRange, SourceRange, bool> {
357 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000358
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000359 public:
360 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000362 bool operator()(SourceRange X, SourceRange Y) {
363 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
364 }
365 };
366}
367
Chris Lattner53cfe802007-07-18 17:52:12 +0000368//===----------------------------------------------------------------------===//
369// Type Sizing and Analysis
370//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000371
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000372/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
373/// scalar floating point type.
374const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000375 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000376 assert(BT && "Not a floating point type!");
377 switch (BT->getKind()) {
378 default: assert(0 && "Not a floating point type!");
379 case BuiltinType::Float: return Target.getFloatFormat();
380 case BuiltinType::Double: return Target.getDoubleFormat();
381 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
382 }
383}
384
Ken Dyck160146e2010-01-27 17:10:57 +0000385/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000386/// specified decl. Note that bitfields do not have a valid alignment, so
387/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000388/// If @p RefAsPointee, references are treated like their underlying type
389/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000390CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000391 unsigned Align = Target.getCharWidth();
392
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000393 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000394 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedman19a546c2009-02-22 02:56:25 +0000395
Chris Lattner68061312009-01-24 21:53:27 +0000396 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
397 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000398 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000399 if (RefAsPointee)
400 T = RT->getPointeeType();
401 else
402 T = getPointerType(RT->getPointeeType());
403 }
404 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000405 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000406 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
407 T = cast<ArrayType>(T)->getElementType();
408
409 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
410 }
Charles Davis3fc51072010-02-23 04:52:00 +0000411 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
412 // In the case of a field in a packed struct, we want the minimum
413 // of the alignment of the field and the alignment of the struct.
414 Align = std::min(Align,
415 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
416 }
Chris Lattner68061312009-01-24 21:53:27 +0000417 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000418
Ken Dyck160146e2010-01-27 17:10:57 +0000419 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000420}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000421
Chris Lattner983a8bb2007-07-13 22:13:22 +0000422/// getTypeSize - Return the size of the specified type, in bits. This method
423/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000424///
425/// FIXME: Pointers into different addr spaces could have different sizes and
426/// alignment requirements: getPointerInfo should take an AddrSpace, this
427/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000428std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000429ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000430 uint64_t Width=0;
431 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000432 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000433#define TYPE(Class, Base)
434#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000435#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000436#define DEPENDENT_TYPE(Class, Base) case Type::Class:
437#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000438 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000439 break;
440
Chris Lattner355332d2007-07-13 22:27:08 +0000441 case Type::FunctionNoProto:
442 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000443 // GCC extension: alignof(function) = 32 bits
444 Width = 0;
445 Align = 32;
446 break;
447
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000448 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000449 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000450 Width = 0;
451 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
452 break;
453
Steve Naroff5c131802007-08-30 01:06:46 +0000454 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000455 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000456
Chris Lattner37e05872008-03-05 18:54:05 +0000457 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000458 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000459 Align = EltInfo.second;
460 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000461 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000462 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000463 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000464 const VectorType *VT = cast<VectorType>(T);
465 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
466 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000467 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000468 // If the alignment is not a power of 2, round up to the next power of 2.
469 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000470 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000471 Align = llvm::NextPowerOf2(Align);
472 Width = llvm::RoundUpToAlignment(Width, Align);
473 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000474 break;
475 }
Chris Lattner647fb222007-07-18 18:26:58 +0000476
Chris Lattner7570e9c2008-03-08 08:52:55 +0000477 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000478 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000479 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000480 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000481 // GCC extension: alignof(void) = 8 bits.
482 Width = 0;
483 Align = 8;
484 break;
485
Chris Lattner6a4f7452007-12-19 19:23:28 +0000486 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000487 Width = Target.getBoolWidth();
488 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000489 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000490 case BuiltinType::Char_S:
491 case BuiltinType::Char_U:
492 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000493 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000494 Width = Target.getCharWidth();
495 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000496 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000497 case BuiltinType::WChar:
498 Width = Target.getWCharWidth();
499 Align = Target.getWCharAlign();
500 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000501 case BuiltinType::Char16:
502 Width = Target.getChar16Width();
503 Align = Target.getChar16Align();
504 break;
505 case BuiltinType::Char32:
506 Width = Target.getChar32Width();
507 Align = Target.getChar32Align();
508 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000509 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000510 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000511 Width = Target.getShortWidth();
512 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000513 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000514 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000515 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000516 Width = Target.getIntWidth();
517 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000518 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000519 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000520 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000521 Width = Target.getLongWidth();
522 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000523 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000524 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000525 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000526 Width = Target.getLongLongWidth();
527 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000528 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000529 case BuiltinType::Int128:
530 case BuiltinType::UInt128:
531 Width = 128;
532 Align = 128; // int128_t is 128-bit aligned on all targets.
533 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000534 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000535 Width = Target.getFloatWidth();
536 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000537 break;
538 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000539 Width = Target.getDoubleWidth();
540 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000541 break;
542 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000543 Width = Target.getLongDoubleWidth();
544 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000545 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000546 case BuiltinType::NullPtr:
547 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
548 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000549 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000550 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000551 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000552 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000553 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000554 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000555 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000556 case Type::BlockPointer: {
557 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
558 Width = Target.getPointerWidth(AS);
559 Align = Target.getPointerAlign(AS);
560 break;
561 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000562 case Type::LValueReference:
563 case Type::RValueReference: {
564 // alignof and sizeof should never enter this code path here, so we go
565 // the pointer route.
566 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
567 Width = Target.getPointerWidth(AS);
568 Align = Target.getPointerAlign(AS);
569 break;
570 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000571 case Type::Pointer: {
572 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000573 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000574 Align = Target.getPointerAlign(AS);
575 break;
576 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000577 case Type::MemberPointer: {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000578 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000579 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000580 getTypeInfo(getPointerDiffType());
581 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000582 if (Pointee->isFunctionType())
583 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000584 Align = PtrDiffInfo.second;
585 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000586 }
Chris Lattner647fb222007-07-18 18:26:58 +0000587 case Type::Complex: {
588 // Complex types have the same alignment as their elements, but twice the
589 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000590 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000591 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000592 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000593 Align = EltInfo.second;
594 break;
595 }
Devang Pateldbb72632008-06-04 21:54:36 +0000596 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000597 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000598 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
599 Width = Layout.getSize();
600 Align = Layout.getAlignment();
601 break;
602 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000603 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000604 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000605 const TagType *TT = cast<TagType>(T);
606
607 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000608 Width = 1;
609 Align = 1;
610 break;
611 }
Mike Stump11289f42009-09-09 15:08:12 +0000612
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000613 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000614 return getTypeInfo(ET->getDecl()->getIntegerType());
615
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000616 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000617 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
618 Width = Layout.getSize();
619 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000620 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000621 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000622
Chris Lattner63d2b362009-10-22 05:17:15 +0000623 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000624 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
625 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000626
Chris Lattner63d2b362009-10-22 05:17:15 +0000627 case Type::Elaborated:
628 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
629 .getTypePtr());
John McCallfcc33b02009-09-05 00:15:47 +0000630
Douglas Gregoref462e62009-04-30 17:32:17 +0000631 case Type::Typedef: {
632 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000633 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000634 Align = std::max(Aligned->getMaxAlignment(),
635 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregoref462e62009-04-30 17:32:17 +0000636 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
637 } else
638 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000639 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000640 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000641
642 case Type::TypeOfExpr:
643 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
644 .getTypePtr());
645
646 case Type::TypeOf:
647 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
648
Anders Carlsson81df7b82009-06-24 19:06:50 +0000649 case Type::Decltype:
650 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
651 .getTypePtr());
652
Douglas Gregoref462e62009-04-30 17:32:17 +0000653 case Type::QualifiedName:
654 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000655
Douglas Gregoref462e62009-04-30 17:32:17 +0000656 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000657 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000658 "Cannot request the size of a dependent type");
659 // FIXME: this is likely to be wrong once we support template
660 // aliases, since a template alias could refer to a typedef that
661 // has an __aligned__ attribute on it.
662 return getTypeInfo(getCanonicalType(T));
663 }
Mike Stump11289f42009-09-09 15:08:12 +0000664
Chris Lattner53cfe802007-07-18 17:52:12 +0000665 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000666 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000667}
668
Ken Dyck8c89d592009-12-22 14:23:30 +0000669/// getTypeSizeInChars - Return the size of the specified type, in characters.
670/// This method does not work on incomplete types.
671CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000672 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000673}
674CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000675 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000676}
677
Ken Dycka6046ab2010-01-26 17:25:18 +0000678/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000679/// characters. This method does not work on incomplete types.
680CharUnits ASTContext::getTypeAlignInChars(QualType T) {
681 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
682}
683CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
684 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
685}
686
Chris Lattnera3402cd2009-01-27 18:08:34 +0000687/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
688/// type for the current target in bits. This can be different than the ABI
689/// alignment in cases where it is beneficial for performance to overalign
690/// a data type.
691unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
692 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000693
694 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000695 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000696 T = CT->getElementType().getTypePtr();
697 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
698 T->isSpecificBuiltinType(BuiltinType::LongLong))
699 return std::max(ABIAlign, (unsigned)getTypeSize(T));
700
Chris Lattnera3402cd2009-01-27 18:08:34 +0000701 return ABIAlign;
702}
703
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000704static void CollectLocalObjCIvars(ASTContext *Ctx,
705 const ObjCInterfaceDecl *OI,
706 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000707 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
708 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000709 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000710 if (!IVDecl->isInvalidDecl())
711 Fields.push_back(cast<FieldDecl>(IVDecl));
712 }
713}
714
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000715void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
716 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
717 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
718 CollectObjCIvars(SuperClass, Fields);
719 CollectLocalObjCIvars(this, OI, Fields);
720}
721
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000722/// ShallowCollectObjCIvars -
723/// Collect all ivars, including those synthesized, in the current class.
724///
725void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000726 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000727 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
728 E = OI->ivar_end(); I != E; ++I) {
729 Ivars.push_back(*I);
730 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000731
732 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000733}
734
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000735/// CollectNonClassIvars -
736/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000737/// This includes synthesized ivars (via @synthesize) and those in
738// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000739///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000740void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000741 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000742 // Find ivars declared in class extension.
743 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
744 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
745 E = CDecl->ivar_end(); I != E; ++I) {
746 Ivars.push_back(*I);
747 }
748 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000749
Ted Kremenek86838aa2010-03-11 19:44:54 +0000750 // Also add any ivar defined in this class's implementation. This
751 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000752 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
753 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
754 E = ImplDecl->ivar_end(); I != E; ++I)
755 Ivars.push_back(*I);
756 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000757}
758
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000759/// CollectInheritedProtocols - Collect all protocols in current class and
760/// those inherited by it.
761void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000762 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000763 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
764 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
765 PE = OI->protocol_end(); P != PE; ++P) {
766 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000767 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000768 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000769 PE = Proto->protocol_end(); P != PE; ++P) {
770 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000771 CollectInheritedProtocols(*P, Protocols);
772 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000773 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000774
775 // Categories of this Interface.
776 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
777 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
778 CollectInheritedProtocols(CDeclChain, Protocols);
779 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
780 while (SD) {
781 CollectInheritedProtocols(SD, Protocols);
782 SD = SD->getSuperClass();
783 }
784 return;
785 }
786 if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
787 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
788 PE = OC->protocol_end(); P != PE; ++P) {
789 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000790 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000791 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
792 PE = Proto->protocol_end(); P != PE; ++P)
793 CollectInheritedProtocols(*P, Protocols);
794 }
795 return;
796 }
797 if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
798 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
799 PE = OP->protocol_end(); P != PE; ++P) {
800 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000801 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000802 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
803 PE = Proto->protocol_end(); P != PE; ++P)
804 CollectInheritedProtocols(*P, Protocols);
805 }
806 return;
807 }
808}
809
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000810unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
811 unsigned count = 0;
812 // Count ivars declared in class extension.
813 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
814 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
815 E = CDecl->ivar_end(); I != E; ++I) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000816 ++count;
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000817 }
818 }
819
820 // Count ivar defined in this class's implementation. This
821 // includes synthesized ivars.
822 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
823 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
824 E = ImplDecl->ivar_end(); I != E; ++I)
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000825 ++count;
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000826 return count;
827}
828
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000829/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
830ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
831 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
832 I = ObjCImpls.find(D);
833 if (I != ObjCImpls.end())
834 return cast<ObjCImplementationDecl>(I->second);
835 return 0;
836}
837/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
838ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
839 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
840 I = ObjCImpls.find(D);
841 if (I != ObjCImpls.end())
842 return cast<ObjCCategoryImplDecl>(I->second);
843 return 0;
844}
845
846/// \brief Set the implementation of ObjCInterfaceDecl.
847void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
848 ObjCImplementationDecl *ImplD) {
849 assert(IFaceD && ImplD && "Passed null params");
850 ObjCImpls[IFaceD] = ImplD;
851}
852/// \brief Set the implementation of ObjCCategoryDecl.
853void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
854 ObjCCategoryImplDecl *ImplD) {
855 assert(CatD && ImplD && "Passed null params");
856 ObjCImpls[CatD] = ImplD;
857}
858
John McCallbcd03502009-12-07 02:54:59 +0000859/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000860///
John McCallbcd03502009-12-07 02:54:59 +0000861/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000862/// the TypeLoc wrappers.
863///
864/// \param T the type that will be the basis for type source info. This type
865/// should refer to how the declarator was written in source code, not to
866/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000867TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000868 unsigned DataSize) {
869 if (!DataSize)
870 DataSize = TypeLoc::getFullDataSizeForType(T);
871 else
872 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000873 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000874
John McCallbcd03502009-12-07 02:54:59 +0000875 TypeSourceInfo *TInfo =
876 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
877 new (TInfo) TypeSourceInfo(T);
878 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000879}
880
John McCallbcd03502009-12-07 02:54:59 +0000881TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000882 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +0000883 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +0000884 DI->getTypeLoc().initialize(L);
885 return DI;
886}
887
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000888/// getInterfaceLayoutImpl - Get or compute information about the
889/// layout of the given interface.
890///
891/// \param Impl - If given, also include the layout of the interface's
892/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000893const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000894ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
895 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000896 assert(!D->isForwardDecl() && "Invalid interface decl!");
897
Devang Pateldbb72632008-06-04 21:54:36 +0000898 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000899 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000900 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
901 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
902 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000903
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000904 // Add in synthesized ivar count if laying out an implementation.
905 if (Impl) {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000906 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000907 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000908 // entry. Note we can't cache this because we simply free all
909 // entries later; however we shouldn't look up implementations
910 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000911 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000912 return getObjCLayout(D, 0);
913 }
914
Mike Stump11289f42009-09-09 15:08:12 +0000915 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000916 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
917 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000918
Devang Pateldbb72632008-06-04 21:54:36 +0000919 return *NewEntry;
920}
921
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000922const ASTRecordLayout &
923ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
924 return getObjCLayout(D, 0);
925}
926
927const ASTRecordLayout &
928ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
929 return getObjCLayout(D->getClassInterface(), D);
930}
931
Devang Patele11664a2007-11-01 19:11:01 +0000932/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +0000933/// specified record (struct/union/class), which indicates its size and field
934/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +0000935const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000936 D = D->getDefinition();
Ted Kremenek21475702008-09-05 17:16:31 +0000937 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +0000938
Chris Lattner53cfe802007-07-18 17:52:12 +0000939 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +0000940 // Note that we can't save a reference to the entry because this function
941 // is recursive.
942 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +0000943 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +0000944
Mike Stump11289f42009-09-09 15:08:12 +0000945 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000946 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +0000947 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000948
Daniel Dunbarccabe482010-04-19 20:44:53 +0000949 if (getLangOptions().DumpRecordLayouts) {
950 llvm::errs() << "\n*** Dumping AST Record Layout\n";
951 DumpRecordLayout(D, llvm::errs());
952 }
953
Chris Lattner647fb222007-07-18 18:26:58 +0000954 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +0000955}
956
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000957const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000958 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000959 assert(RD && "Cannot get key function for forward declarations!");
960
961 const CXXMethodDecl *&Entry = KeyFunctions[RD];
962 if (!Entry)
963 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
964 else
965 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
966 "Key function changed!");
967
968 return Entry;
969}
970
Chris Lattner983a8bb2007-07-13 22:13:22 +0000971//===----------------------------------------------------------------------===//
972// Type creation/memoization methods
973//===----------------------------------------------------------------------===//
974
John McCall8ccfcb52009-09-24 19:53:00 +0000975QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
976 unsigned Fast = Quals.getFastQualifiers();
977 Quals.removeFastQualifiers();
978
979 // Check if we've already instantiated this type.
980 llvm::FoldingSetNodeID ID;
981 ExtQuals::Profile(ID, TypeNode, Quals);
982 void *InsertPos = 0;
983 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
984 assert(EQ->getQualifiers() == Quals);
985 QualType T = QualType(EQ, Fast);
986 return T;
987 }
988
John McCall90d1c2d2009-09-24 23:30:46 +0000989 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000990 ExtQualNodes.InsertNode(New, InsertPos);
991 QualType T = QualType(New, Fast);
992 return T;
993}
994
995QualType ASTContext::getVolatileType(QualType T) {
996 QualType CanT = getCanonicalType(T);
997 if (CanT.isVolatileQualified()) return T;
998
999 QualifierCollector Quals;
1000 const Type *TypeNode = Quals.strip(T);
1001 Quals.addVolatile();
1002
1003 return getExtQualType(TypeNode, Quals);
1004}
1005
Fariborz Jahanianece85822009-02-17 18:27:45 +00001006QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001007 QualType CanT = getCanonicalType(T);
1008 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001009 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001010
John McCall8ccfcb52009-09-24 19:53:00 +00001011 // If we are composing extended qualifiers together, merge together
1012 // into one ExtQuals node.
1013 QualifierCollector Quals;
1014 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001015
John McCall8ccfcb52009-09-24 19:53:00 +00001016 // If this type already has an address space specified, it cannot get
1017 // another one.
1018 assert(!Quals.hasAddressSpace() &&
1019 "Type cannot be in multiple addr spaces!");
1020 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001021
John McCall8ccfcb52009-09-24 19:53:00 +00001022 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001023}
1024
Chris Lattnerd60183d2009-02-18 22:53:11 +00001025QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001026 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001027 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001028 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001029 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001030
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001031 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001032 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001033 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001034 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1035 return getPointerType(ResultType);
1036 }
1037 }
Mike Stump11289f42009-09-09 15:08:12 +00001038
John McCall8ccfcb52009-09-24 19:53:00 +00001039 // If we are composing extended qualifiers together, merge together
1040 // into one ExtQuals node.
1041 QualifierCollector Quals;
1042 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001043
John McCall8ccfcb52009-09-24 19:53:00 +00001044 // If this type already has an ObjCGC specified, it cannot get
1045 // another one.
1046 assert(!Quals.hasObjCGCAttr() &&
1047 "Type cannot have multiple ObjCGCs!");
1048 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001049
John McCall8ccfcb52009-09-24 19:53:00 +00001050 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001051}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001052
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001053static QualType getExtFunctionType(ASTContext& Context, QualType T,
1054 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001055 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001056 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1057 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001058 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001059 if (ResultType == Pointee)
1060 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001061
1062 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001063 } else if (const BlockPointerType *BlockPointer
1064 = T->getAs<BlockPointerType>()) {
1065 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001066 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001067 if (ResultType == Pointee)
1068 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001069
1070 ResultType = Context.getBlockPointerType(ResultType);
1071 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001072 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001073 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001074
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001075 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001076 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001077 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001078 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001079 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001080 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001081 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1082 FPT->getNumArgs(), FPT->isVariadic(),
1083 FPT->getTypeQuals(),
1084 FPT->hasExceptionSpec(),
1085 FPT->hasAnyExceptionSpec(),
1086 FPT->getNumExceptions(),
1087 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001088 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001089 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001090 } else
1091 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001092
1093 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1094}
1095
1096QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001097 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001098 return getExtFunctionType(*this, T,
1099 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001100}
1101
1102QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001103 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001104 return getExtFunctionType(*this, T,
1105 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001106}
1107
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001108QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1109 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1110 return getExtFunctionType(*this, T,
1111 Info.withRegParm(RegParm));
1112}
1113
Chris Lattnerc6395932007-06-22 20:56:16 +00001114/// getComplexType - Return the uniqued reference to the type for a complex
1115/// number with the specified element type.
1116QualType ASTContext::getComplexType(QualType T) {
1117 // Unique pointers, to guarantee there is only one pointer of a particular
1118 // structure.
1119 llvm::FoldingSetNodeID ID;
1120 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001121
Chris Lattnerc6395932007-06-22 20:56:16 +00001122 void *InsertPos = 0;
1123 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1124 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001125
Chris Lattnerc6395932007-06-22 20:56:16 +00001126 // If the pointee type isn't canonical, this won't be a canonical type either,
1127 // so fill in the canonical type field.
1128 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001129 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001130 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattnerc6395932007-06-22 20:56:16 +00001132 // Get the new insert position for the node we care about.
1133 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001134 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001135 }
John McCall90d1c2d2009-09-24 23:30:46 +00001136 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001137 Types.push_back(New);
1138 ComplexTypes.InsertNode(New, InsertPos);
1139 return QualType(New, 0);
1140}
1141
Chris Lattner970e54e2006-11-12 00:37:36 +00001142/// getPointerType - Return the uniqued reference to the type for a pointer to
1143/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001144QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001145 // Unique pointers, to guarantee there is only one pointer of a particular
1146 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001147 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001148 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001149
Chris Lattner67521df2007-01-27 01:29:36 +00001150 void *InsertPos = 0;
1151 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001152 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001153
Chris Lattner7ccecb92006-11-12 08:50:50 +00001154 // If the pointee type isn't canonical, this won't be a canonical type either,
1155 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001156 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001157 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001158 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001159
Chris Lattner67521df2007-01-27 01:29:36 +00001160 // Get the new insert position for the node we care about.
1161 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001162 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001163 }
John McCall90d1c2d2009-09-24 23:30:46 +00001164 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001165 Types.push_back(New);
1166 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001167 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001168}
1169
Mike Stump11289f42009-09-09 15:08:12 +00001170/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001171/// a pointer to the specified block.
1172QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001173 assert(T->isFunctionType() && "block of function types only");
1174 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001175 // structure.
1176 llvm::FoldingSetNodeID ID;
1177 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001178
Steve Naroffec33ed92008-08-27 16:04:49 +00001179 void *InsertPos = 0;
1180 if (BlockPointerType *PT =
1181 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1182 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001183
1184 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001185 // type either so fill in the canonical type field.
1186 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001187 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001188 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001189
Steve Naroffec33ed92008-08-27 16:04:49 +00001190 // Get the new insert position for the node we care about.
1191 BlockPointerType *NewIP =
1192 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001193 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001194 }
John McCall90d1c2d2009-09-24 23:30:46 +00001195 BlockPointerType *New
1196 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001197 Types.push_back(New);
1198 BlockPointerTypes.InsertNode(New, InsertPos);
1199 return QualType(New, 0);
1200}
1201
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001202/// getLValueReferenceType - Return the uniqued reference to the type for an
1203/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001204QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001205 // Unique pointers, to guarantee there is only one pointer of a particular
1206 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001207 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001208 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001209
1210 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001211 if (LValueReferenceType *RT =
1212 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001213 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001214
John McCallfc93cf92009-10-22 22:37:11 +00001215 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1216
Bill Wendling3708c182007-05-27 10:15:43 +00001217 // If the referencee type isn't canonical, this won't be a canonical type
1218 // either, so fill in the canonical type field.
1219 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001220 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1221 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1222 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001223
Bill Wendling3708c182007-05-27 10:15:43 +00001224 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001225 LValueReferenceType *NewIP =
1226 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001227 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001228 }
1229
John McCall90d1c2d2009-09-24 23:30:46 +00001230 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001231 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1232 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001233 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001234 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001235
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001236 return QualType(New, 0);
1237}
1238
1239/// getRValueReferenceType - Return the uniqued reference to the type for an
1240/// rvalue reference to the specified type.
1241QualType ASTContext::getRValueReferenceType(QualType T) {
1242 // Unique pointers, to guarantee there is only one pointer of a particular
1243 // structure.
1244 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001245 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001246
1247 void *InsertPos = 0;
1248 if (RValueReferenceType *RT =
1249 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1250 return QualType(RT, 0);
1251
John McCallfc93cf92009-10-22 22:37:11 +00001252 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1253
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001254 // If the referencee type isn't canonical, this won't be a canonical type
1255 // either, so fill in the canonical type field.
1256 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001257 if (InnerRef || !T.isCanonical()) {
1258 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1259 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001260
1261 // Get the new insert position for the node we care about.
1262 RValueReferenceType *NewIP =
1263 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1264 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1265 }
1266
John McCall90d1c2d2009-09-24 23:30:46 +00001267 RValueReferenceType *New
1268 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001269 Types.push_back(New);
1270 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001271 return QualType(New, 0);
1272}
1273
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001274/// getMemberPointerType - Return the uniqued reference to the type for a
1275/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001276QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001277 // Unique pointers, to guarantee there is only one pointer of a particular
1278 // structure.
1279 llvm::FoldingSetNodeID ID;
1280 MemberPointerType::Profile(ID, T, Cls);
1281
1282 void *InsertPos = 0;
1283 if (MemberPointerType *PT =
1284 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1285 return QualType(PT, 0);
1286
1287 // If the pointee or class type isn't canonical, this won't be a canonical
1288 // type either, so fill in the canonical type field.
1289 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001290 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001291 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1292
1293 // Get the new insert position for the node we care about.
1294 MemberPointerType *NewIP =
1295 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1296 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1297 }
John McCall90d1c2d2009-09-24 23:30:46 +00001298 MemberPointerType *New
1299 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001300 Types.push_back(New);
1301 MemberPointerTypes.InsertNode(New, InsertPos);
1302 return QualType(New, 0);
1303}
1304
Mike Stump11289f42009-09-09 15:08:12 +00001305/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001306/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001307QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001308 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001309 ArrayType::ArraySizeModifier ASM,
1310 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001311 assert((EltTy->isDependentType() ||
1312 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001313 "Constant array of VLAs is illegal!");
1314
Chris Lattnere2df3f92009-05-13 04:12:56 +00001315 // Convert the array size into a canonical width matching the pointer size for
1316 // the target.
1317 llvm::APInt ArySize(ArySizeIn);
1318 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001319
Chris Lattner23b7eb62007-06-15 23:05:46 +00001320 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001321 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001322
Chris Lattner36f8e652007-01-27 08:31:04 +00001323 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001324 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001325 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001326 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001327
Chris Lattner7ccecb92006-11-12 08:50:50 +00001328 // If the element type isn't canonical, this won't be a canonical type either,
1329 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001330 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001331 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001332 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001333 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001334 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001335 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001336 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001337 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001338 }
Mike Stump11289f42009-09-09 15:08:12 +00001339
John McCall90d1c2d2009-09-24 23:30:46 +00001340 ConstantArrayType *New = new(*this,TypeAlignment)
1341 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001342 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001343 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001344 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001345}
1346
Steve Naroffcadebd02007-08-30 18:14:25 +00001347/// getVariableArrayType - Returns a non-unique reference to the type for a
1348/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001349QualType ASTContext::getVariableArrayType(QualType EltTy,
1350 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001351 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001352 unsigned EltTypeQuals,
1353 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001354 // Since we don't unique expressions, it isn't possible to unique VLA's
1355 // that have an expression provided for their size.
1356
John McCall90d1c2d2009-09-24 23:30:46 +00001357 VariableArrayType *New = new(*this, TypeAlignment)
1358 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001359
1360 VariableArrayTypes.push_back(New);
1361 Types.push_back(New);
1362 return QualType(New, 0);
1363}
1364
Douglas Gregor4619e432008-12-05 23:32:09 +00001365/// getDependentSizedArrayType - Returns a non-unique reference to
1366/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001367/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001368QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1369 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001370 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001371 unsigned EltTypeQuals,
1372 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001373 assert((!NumElts || NumElts->isTypeDependent() ||
1374 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001375 "Size must be type- or value-dependent!");
1376
Douglas Gregorf3f95522009-07-31 00:23:35 +00001377 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001378 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001379 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001380
1381 if (NumElts) {
1382 // Dependently-sized array types that do not have a specified
1383 // number of elements will have their sizes deduced from an
1384 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001385 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1386 EltTypeQuals, NumElts);
1387
1388 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1389 }
1390
Douglas Gregorf3f95522009-07-31 00:23:35 +00001391 DependentSizedArrayType *New;
1392 if (Canon) {
1393 // We already have a canonical version of this array type; use it as
1394 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001395 New = new (*this, TypeAlignment)
1396 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1397 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001398 } else {
1399 QualType CanonEltTy = getCanonicalType(EltTy);
1400 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001401 New = new (*this, TypeAlignment)
1402 DependentSizedArrayType(*this, EltTy, QualType(),
1403 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001404
Douglas Gregorc42075a2010-02-04 18:10:26 +00001405 if (NumElts) {
1406 DependentSizedArrayType *CanonCheck
1407 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1408 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1409 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001410 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001411 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001412 } else {
1413 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1414 ASM, EltTypeQuals,
1415 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001416 New = new (*this, TypeAlignment)
1417 DependentSizedArrayType(*this, EltTy, Canon,
1418 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001419 }
1420 }
Mike Stump11289f42009-09-09 15:08:12 +00001421
Douglas Gregor4619e432008-12-05 23:32:09 +00001422 Types.push_back(New);
1423 return QualType(New, 0);
1424}
1425
Eli Friedmanbd258282008-02-15 18:16:39 +00001426QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1427 ArrayType::ArraySizeModifier ASM,
1428 unsigned EltTypeQuals) {
1429 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001430 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001431
1432 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001433 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001434 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1435 return QualType(ATP, 0);
1436
1437 // If the element type isn't canonical, this won't be a canonical type
1438 // either, so fill in the canonical type field.
1439 QualType Canonical;
1440
John McCallb692a092009-10-22 20:10:53 +00001441 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001442 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001443 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001444
1445 // Get the new insert position for the node we care about.
1446 IncompleteArrayType *NewIP =
1447 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001448 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001449 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001450
John McCall90d1c2d2009-09-24 23:30:46 +00001451 IncompleteArrayType *New = new (*this, TypeAlignment)
1452 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001453
1454 IncompleteArrayTypes.InsertNode(New, InsertPos);
1455 Types.push_back(New);
1456 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001457}
1458
Steve Naroff91fcddb2007-07-18 18:00:27 +00001459/// getVectorType - Return the unique reference to a vector type of
1460/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001461QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1462 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001463 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001464
Chris Lattner76a00cf2008-04-06 22:59:24 +00001465 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001466 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001467
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001468 // Check if we've already instantiated a vector of this type.
1469 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001470 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1471 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001472 void *InsertPos = 0;
1473 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1474 return QualType(VTP, 0);
1475
1476 // If the element type isn't canonical, this won't be a canonical type either,
1477 // so fill in the canonical type field.
1478 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001479 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1480 Canonical = getVectorType(getCanonicalType(vecType),
1481 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001482
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001483 // Get the new insert position for the node we care about.
1484 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001485 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001486 }
John McCall90d1c2d2009-09-24 23:30:46 +00001487 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001488 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001489 VectorTypes.InsertNode(New, InsertPos);
1490 Types.push_back(New);
1491 return QualType(New, 0);
1492}
1493
Nate Begemance4d7fc2008-04-18 23:10:10 +00001494/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001495/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001496QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001497 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001498
Chris Lattner76a00cf2008-04-06 22:59:24 +00001499 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001500 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001501
Steve Naroff91fcddb2007-07-18 18:00:27 +00001502 // Check if we've already instantiated a vector of this type.
1503 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001504 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001505 void *InsertPos = 0;
1506 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1507 return QualType(VTP, 0);
1508
1509 // If the element type isn't canonical, this won't be a canonical type either,
1510 // so fill in the canonical type field.
1511 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001512 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001513 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001514
Steve Naroff91fcddb2007-07-18 18:00:27 +00001515 // Get the new insert position for the node we care about.
1516 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001517 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001518 }
John McCall90d1c2d2009-09-24 23:30:46 +00001519 ExtVectorType *New = new (*this, TypeAlignment)
1520 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001521 VectorTypes.InsertNode(New, InsertPos);
1522 Types.push_back(New);
1523 return QualType(New, 0);
1524}
1525
Mike Stump11289f42009-09-09 15:08:12 +00001526QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001527 Expr *SizeExpr,
1528 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001529 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001530 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001531 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001532
Douglas Gregor352169a2009-07-31 03:54:25 +00001533 void *InsertPos = 0;
1534 DependentSizedExtVectorType *Canon
1535 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1536 DependentSizedExtVectorType *New;
1537 if (Canon) {
1538 // We already have a canonical version of this array type; use it as
1539 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001540 New = new (*this, TypeAlignment)
1541 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1542 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001543 } else {
1544 QualType CanonVecTy = getCanonicalType(vecType);
1545 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001546 New = new (*this, TypeAlignment)
1547 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1548 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001549
1550 DependentSizedExtVectorType *CanonCheck
1551 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1552 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1553 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001554 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1555 } else {
1556 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1557 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001558 New = new (*this, TypeAlignment)
1559 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001560 }
1561 }
Mike Stump11289f42009-09-09 15:08:12 +00001562
Douglas Gregor758a8692009-06-17 21:51:59 +00001563 Types.push_back(New);
1564 return QualType(New, 0);
1565}
1566
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001567/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001568///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001569QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1570 const FunctionType::ExtInfo &Info) {
1571 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001572 // Unique functions, to guarantee there is only one function of a particular
1573 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001574 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001575 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001576
Chris Lattner47955de2007-01-27 08:37:20 +00001577 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001578 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001579 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001580 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001581
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001582 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001583 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001584 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001585 Canonical =
1586 getFunctionNoProtoType(getCanonicalType(ResultTy),
1587 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001588
Chris Lattner47955de2007-01-27 08:37:20 +00001589 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001590 FunctionNoProtoType *NewIP =
1591 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001592 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001593 }
Mike Stump11289f42009-09-09 15:08:12 +00001594
John McCall90d1c2d2009-09-24 23:30:46 +00001595 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001596 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001597 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001598 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001599 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001600}
1601
1602/// getFunctionType - Return a normal function type with a typed argument
1603/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001604QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001605 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001606 unsigned TypeQuals, bool hasExceptionSpec,
1607 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001608 const QualType *ExArray,
1609 const FunctionType::ExtInfo &Info) {
1610 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001611 // Unique functions, to guarantee there is only one function of a particular
1612 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001613 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001614 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001615 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001616 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001617
1618 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001619 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001620 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001621 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001622
1623 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001624 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001625 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001626 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001627 isCanonical = false;
1628
1629 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001630 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001631 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001632 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001633 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001634 CanonicalArgs.reserve(NumArgs);
1635 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001636 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001637
Chris Lattner76a00cf2008-04-06 22:59:24 +00001638 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001639 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001640 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001641 false, 0, 0,
1642 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001643
Chris Lattnerfd4de792007-01-27 01:15:32 +00001644 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001645 FunctionProtoType *NewIP =
1646 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001647 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001648 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001649
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001650 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001651 // for two variable size arrays (for parameter and exception types) at the
1652 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001653 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001654 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1655 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001656 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001657 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001658 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001659 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001660 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001661 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001662 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001663}
Chris Lattneref51c202006-11-10 07:17:23 +00001664
John McCalle78aac42010-03-10 03:28:59 +00001665#ifndef NDEBUG
1666static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1667 if (!isa<CXXRecordDecl>(D)) return false;
1668 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1669 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1670 return true;
1671 if (RD->getDescribedClassTemplate() &&
1672 !isa<ClassTemplateSpecializationDecl>(RD))
1673 return true;
1674 return false;
1675}
1676#endif
1677
1678/// getInjectedClassNameType - Return the unique reference to the
1679/// injected class name type for the specified templated declaration.
1680QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1681 QualType TST) {
1682 assert(NeedsInjectedClassNameType(Decl));
1683 if (Decl->TypeForDecl) {
1684 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1685 } else if (CXXRecordDecl *PrevDecl
1686 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1687 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1688 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1689 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1690 } else {
John McCall2408e322010-04-27 00:57:59 +00001691 Decl->TypeForDecl =
1692 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001693 Types.push_back(Decl->TypeForDecl);
1694 }
1695 return QualType(Decl->TypeForDecl, 0);
1696}
1697
Douglas Gregor83a586e2008-04-13 21:07:44 +00001698/// getTypeDeclType - Return the unique reference to the type for the
1699/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001700QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001701 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001702 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001703
John McCall81e38502010-02-16 03:57:14 +00001704 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001705 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001706
1707 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001708 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001709 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001710
John McCall96f0b5f2010-03-10 06:48:02 +00001711 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1712 "Template type parameter types are always available.");
1713
John McCall81e38502010-02-16 03:57:14 +00001714 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001715 assert(!Record->getPreviousDeclaration() &&
1716 "struct/union has previous declaration");
1717 assert(!NeedsInjectedClassNameType(Record));
1718 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001719 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001720 assert(!Enum->getPreviousDeclaration() &&
1721 "enum has previous declaration");
1722 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001723 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001724 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1725 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001726 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001727 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001728
John McCall96f0b5f2010-03-10 06:48:02 +00001729 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001730 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001731}
1732
Chris Lattner32d920b2007-01-26 02:01:53 +00001733/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001734/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001735QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001736 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001737
Chris Lattner76a00cf2008-04-06 22:59:24 +00001738 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001739 Decl->TypeForDecl = new(*this, TypeAlignment)
1740 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001741 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001742 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001743}
1744
John McCallcebee162009-10-18 09:09:24 +00001745/// \brief Retrieve a substitution-result type.
1746QualType
1747ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1748 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001749 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001750 && "replacement types must always be canonical");
1751
1752 llvm::FoldingSetNodeID ID;
1753 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1754 void *InsertPos = 0;
1755 SubstTemplateTypeParmType *SubstParm
1756 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1757
1758 if (!SubstParm) {
1759 SubstParm = new (*this, TypeAlignment)
1760 SubstTemplateTypeParmType(Parm, Replacement);
1761 Types.push_back(SubstParm);
1762 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1763 }
1764
1765 return QualType(SubstParm, 0);
1766}
1767
Douglas Gregoreff93e02009-02-05 23:33:38 +00001768/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001769/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001770/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001771QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001772 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001773 IdentifierInfo *Name) {
1774 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001775 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001776 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001777 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001778 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1779
1780 if (TypeParm)
1781 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001782
Anders Carlsson90036dc2009-06-16 00:30:48 +00001783 if (Name) {
1784 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001785 TypeParm = new (*this, TypeAlignment)
1786 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001787
1788 TemplateTypeParmType *TypeCheck
1789 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1790 assert(!TypeCheck && "Template type parameter canonical type broken");
1791 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001792 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001793 TypeParm = new (*this, TypeAlignment)
1794 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001795
1796 Types.push_back(TypeParm);
1797 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1798
1799 return QualType(TypeParm, 0);
1800}
1801
John McCalle78aac42010-03-10 03:28:59 +00001802TypeSourceInfo *
1803ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1804 SourceLocation NameLoc,
1805 const TemplateArgumentListInfo &Args,
1806 QualType CanonType) {
1807 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1808
1809 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1810 TemplateSpecializationTypeLoc TL
1811 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1812 TL.setTemplateNameLoc(NameLoc);
1813 TL.setLAngleLoc(Args.getLAngleLoc());
1814 TL.setRAngleLoc(Args.getRAngleLoc());
1815 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1816 TL.setArgLocInfo(i, Args[i].getLocInfo());
1817 return DI;
1818}
1819
Mike Stump11289f42009-09-09 15:08:12 +00001820QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001821ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001822 const TemplateArgumentListInfo &Args,
John McCall2408e322010-04-27 00:57:59 +00001823 QualType Canon,
1824 bool IsCurrentInstantiation) {
John McCall6b51f282009-11-23 01:53:49 +00001825 unsigned NumArgs = Args.size();
1826
John McCall0ad16662009-10-29 08:12:44 +00001827 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1828 ArgVec.reserve(NumArgs);
1829 for (unsigned i = 0; i != NumArgs; ++i)
1830 ArgVec.push_back(Args[i].getArgument());
1831
John McCall2408e322010-04-27 00:57:59 +00001832 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
1833 Canon, IsCurrentInstantiation);
John McCall0ad16662009-10-29 08:12:44 +00001834}
1835
1836QualType
1837ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001838 const TemplateArgument *Args,
1839 unsigned NumArgs,
John McCall2408e322010-04-27 00:57:59 +00001840 QualType Canon,
1841 bool IsCurrentInstantiation) {
Douglas Gregor15301382009-07-30 17:40:51 +00001842 if (!Canon.isNull())
1843 Canon = getCanonicalType(Canon);
1844 else {
John McCall2408e322010-04-27 00:57:59 +00001845 assert(!IsCurrentInstantiation &&
1846 "current-instantiation specializations should always "
1847 "have a canonical type");
1848
Douglas Gregor15301382009-07-30 17:40:51 +00001849 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001850 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1851 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1852 CanonArgs.reserve(NumArgs);
1853 for (unsigned I = 0; I != NumArgs; ++I)
1854 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1855
1856 // Determine whether this canonical template specialization type already
1857 // exists.
1858 llvm::FoldingSetNodeID ID;
John McCall2408e322010-04-27 00:57:59 +00001859 TemplateSpecializationType::Profile(ID, CanonTemplate, false,
Douglas Gregor00044172009-07-29 16:09:57 +00001860 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001861
1862 void *InsertPos = 0;
1863 TemplateSpecializationType *Spec
1864 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001865
Douglas Gregora8e02e72009-07-28 23:00:59 +00001866 if (!Spec) {
1867 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001868 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001869 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001870 TypeAlignment);
John McCall2408e322010-04-27 00:57:59 +00001871 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001872 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001873 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001874 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001875 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001876 }
Mike Stump11289f42009-09-09 15:08:12 +00001877
Douglas Gregor15301382009-07-30 17:40:51 +00001878 if (Canon.isNull())
1879 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001880 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001881 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001882 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001883
Douglas Gregora8e02e72009-07-28 23:00:59 +00001884 // Allocate the (non-canonical) template specialization type, but don't
1885 // try to unique it: these types typically have location information that
1886 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001887 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001888 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001889 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001890 TemplateSpecializationType *Spec
John McCall2408e322010-04-27 00:57:59 +00001891 = new (Mem) TemplateSpecializationType(*this, Template,
1892 IsCurrentInstantiation,
1893 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001894 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001895
Douglas Gregor8bf42052009-02-09 18:46:07 +00001896 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001897 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001898}
1899
Mike Stump11289f42009-09-09 15:08:12 +00001900QualType
Douglas Gregorf21eb492009-03-26 23:50:42 +00001901ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor52537682009-03-19 00:18:19 +00001902 QualType NamedType) {
1903 llvm::FoldingSetNodeID ID;
Douglas Gregorf21eb492009-03-26 23:50:42 +00001904 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001905
1906 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001907 QualifiedNameType *T
Douglas Gregor52537682009-03-19 00:18:19 +00001908 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1909 if (T)
1910 return QualType(T, 0);
1911
Douglas Gregorc42075a2010-02-04 18:10:26 +00001912 QualType Canon = NamedType;
1913 if (!Canon.isCanonical()) {
1914 Canon = getCanonicalType(NamedType);
1915 QualifiedNameType *CheckT
1916 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1917 assert(!CheckT && "Qualified name canonical type broken");
1918 (void)CheckT;
1919 }
1920
1921 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001922 Types.push_back(T);
1923 QualifiedNameTypes.InsertNode(T, InsertPos);
1924 return QualType(T, 0);
1925}
1926
Douglas Gregor02085352010-03-31 20:19:30 +00001927QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1928 NestedNameSpecifier *NNS,
1929 const IdentifierInfo *Name,
1930 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001931 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1932
1933 if (Canon.isNull()) {
1934 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001935 ElaboratedTypeKeyword CanonKeyword = Keyword;
1936 if (Keyword == ETK_None)
1937 CanonKeyword = ETK_Typename;
1938
1939 if (CanonNNS != NNS || CanonKeyword != Keyword)
1940 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001941 }
1942
1943 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001944 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001945
1946 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001947 DependentNameType *T
1948 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001949 if (T)
1950 return QualType(T, 0);
1951
Douglas Gregor02085352010-03-31 20:19:30 +00001952 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00001953 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001954 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001955 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001956}
1957
Mike Stump11289f42009-09-09 15:08:12 +00001958QualType
Douglas Gregor02085352010-03-31 20:19:30 +00001959ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1960 NestedNameSpecifier *NNS,
1961 const TemplateSpecializationType *TemplateId,
1962 QualType Canon) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001963 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1964
Douglas Gregorc42075a2010-02-04 18:10:26 +00001965 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001966 DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001967
1968 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001969 DependentNameType *T
1970 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001971 if (T)
1972 return QualType(T, 0);
1973
Douglas Gregordce2b622009-04-01 00:28:59 +00001974 if (Canon.isNull()) {
1975 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1976 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
Douglas Gregor02085352010-03-31 20:19:30 +00001977 ElaboratedTypeKeyword CanonKeyword = Keyword;
1978 if (Keyword == ETK_None)
1979 CanonKeyword = ETK_Typename;
1980 if (CanonNNS != NNS || CanonKeyword != Keyword ||
1981 CanonType != QualType(TemplateId, 0)) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001982 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00001983 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00001984 assert(CanonTemplateId &&
1985 "Canonical type must also be a template specialization type");
Douglas Gregor02085352010-03-31 20:19:30 +00001986 Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
Douglas Gregordce2b622009-04-01 00:28:59 +00001987 }
Douglas Gregorc42075a2010-02-04 18:10:26 +00001988
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001989 DependentNameType *CheckT
1990 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001991 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregordce2b622009-04-01 00:28:59 +00001992 }
1993
Douglas Gregor02085352010-03-31 20:19:30 +00001994 T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00001995 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001996 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001997 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00001998}
1999
John McCallfcc33b02009-09-05 00:15:47 +00002000QualType
2001ASTContext::getElaboratedType(QualType UnderlyingType,
2002 ElaboratedType::TagKind Tag) {
2003 llvm::FoldingSetNodeID ID;
2004 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump11289f42009-09-09 15:08:12 +00002005
John McCallfcc33b02009-09-05 00:15:47 +00002006 void *InsertPos = 0;
2007 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2008 if (T)
2009 return QualType(T, 0);
2010
Douglas Gregorc42075a2010-02-04 18:10:26 +00002011 QualType Canon = UnderlyingType;
2012 if (!Canon.isCanonical()) {
2013 Canon = getCanonicalType(Canon);
2014 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2015 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2016 }
John McCallfcc33b02009-09-05 00:15:47 +00002017
2018 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2019 Types.push_back(T);
2020 ElaboratedTypes.InsertNode(T, InsertPos);
2021 return QualType(T, 0);
2022}
2023
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002024/// CmpProtocolNames - Comparison predicate for sorting protocols
2025/// alphabetically.
2026static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2027 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002028 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002029}
2030
John McCallfc93cf92009-10-22 22:37:11 +00002031static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2032 unsigned NumProtocols) {
2033 if (NumProtocols == 0) return true;
2034
2035 for (unsigned i = 1; i != NumProtocols; ++i)
2036 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2037 return false;
2038 return true;
2039}
2040
2041static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002042 unsigned &NumProtocols) {
2043 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002044
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002045 // Sort protocols, keyed by name.
2046 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2047
2048 // Remove duplicates.
2049 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2050 NumProtocols = ProtocolsEnd-Protocols;
2051}
2052
Steve Narofffb4330f2009-06-17 22:40:22 +00002053/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2054/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002055QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00002056 ObjCProtocolDecl **Protocols,
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002057 unsigned NumProtocols,
2058 unsigned Quals) {
Steve Narofffb4330f2009-06-17 22:40:22 +00002059 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002060 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002061 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Narofffb4330f2009-06-17 22:40:22 +00002062
2063 void *InsertPos = 0;
2064 if (ObjCObjectPointerType *QT =
2065 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002066 return getQualifiedType(QualType(QT, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002067
John McCallfc93cf92009-10-22 22:37:11 +00002068 // Sort the protocol list alphabetically to canonicalize it.
2069 QualType Canonical;
2070 if (!InterfaceT.isCanonical() ||
2071 !areSortedAndUniqued(Protocols, NumProtocols)) {
2072 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002073 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2074 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002075 unsigned UniqueCount = NumProtocols;
2076
John McCallfc93cf92009-10-22 22:37:11 +00002077 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2078
2079 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2080 &Sorted[0], UniqueCount);
2081 } else {
2082 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2083 Protocols, NumProtocols);
2084 }
2085
2086 // Regenerate InsertPos.
2087 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2088 }
2089
Douglas Gregorf85bee62010-02-08 22:59:26 +00002090 // No match.
2091 unsigned Size = sizeof(ObjCObjectPointerType)
2092 + NumProtocols * sizeof(ObjCProtocolDecl *);
2093 void *Mem = Allocate(Size, TypeAlignment);
2094 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2095 InterfaceT,
2096 Protocols,
2097 NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002098
Steve Narofffb4330f2009-06-17 22:40:22 +00002099 Types.push_back(QType);
2100 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002101 return getQualifiedType(QualType(QType, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002102}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002103
Steve Naroffc277ad12009-07-18 15:33:26 +00002104/// getObjCInterfaceType - Return the unique reference to the type for the
2105/// specified ObjC interface decl. The list of protocols is optional.
2106QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002107 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002108 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00002109 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002110
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002111 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00002112 if (ObjCInterfaceType *QT =
2113 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002114 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002115
John McCallfc93cf92009-10-22 22:37:11 +00002116 // Sort the protocol list alphabetically to canonicalize it.
2117 QualType Canonical;
2118 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002119 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2120 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002121
2122 unsigned UniqueCount = NumProtocols;
2123 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2124
2125 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2126
2127 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2128 }
2129
Douglas Gregorf85bee62010-02-08 22:59:26 +00002130 unsigned Size = sizeof(ObjCInterfaceType)
2131 + NumProtocols * sizeof(ObjCProtocolDecl *);
2132 void *Mem = Allocate(Size, TypeAlignment);
2133 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2134 const_cast<ObjCInterfaceDecl*>(Decl),
2135 Protocols,
2136 NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002137
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002138 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00002139 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002140 return QualType(QType, 0);
2141}
2142
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002143/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2144/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002145/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002146/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002147/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002148QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002149 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002150 if (tofExpr->isTypeDependent()) {
2151 llvm::FoldingSetNodeID ID;
2152 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002153
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002154 void *InsertPos = 0;
2155 DependentTypeOfExprType *Canon
2156 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2157 if (Canon) {
2158 // We already have a "canonical" version of an identical, dependent
2159 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002160 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002161 QualType((TypeOfExprType*)Canon, 0));
2162 }
2163 else {
2164 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002165 Canon
2166 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002167 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2168 toe = Canon;
2169 }
2170 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002171 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002172 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002173 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002174 Types.push_back(toe);
2175 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002176}
2177
Steve Naroffa773cd52007-08-01 18:02:17 +00002178/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2179/// TypeOfType AST's. The only motivation to unique these nodes would be
2180/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002181/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002182/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002183QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002184 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002185 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002186 Types.push_back(tot);
2187 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002188}
2189
Anders Carlssonad6bd352009-06-24 21:24:56 +00002190/// getDecltypeForExpr - Given an expr, will return the decltype for that
2191/// expression, according to the rules in C++0x [dcl.type.simple]p4
2192static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002193 if (e->isTypeDependent())
2194 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002195
Anders Carlssonad6bd352009-06-24 21:24:56 +00002196 // If e is an id expression or a class member access, decltype(e) is defined
2197 // as the type of the entity named by e.
2198 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2199 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2200 return VD->getType();
2201 }
2202 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2203 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2204 return FD->getType();
2205 }
2206 // If e is a function call or an invocation of an overloaded operator,
2207 // (parentheses around e are ignored), decltype(e) is defined as the
2208 // return type of that function.
2209 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2210 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002211
Anders Carlssonad6bd352009-06-24 21:24:56 +00002212 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002213
2214 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002215 // defined as T&, otherwise decltype(e) is defined as T.
2216 if (e->isLvalue(Context) == Expr::LV_Valid)
2217 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002218
Anders Carlssonad6bd352009-06-24 21:24:56 +00002219 return T;
2220}
2221
Anders Carlsson81df7b82009-06-24 19:06:50 +00002222/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2223/// DecltypeType AST's. The only motivation to unique these nodes would be
2224/// memory savings. Since decltype(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
Anders Carlsson81df7b82009-06-24 19:06:50 +00002226/// on canonical type's (which are always unique).
2227QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002228 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002229 if (e->isTypeDependent()) {
2230 llvm::FoldingSetNodeID ID;
2231 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002232
Douglas Gregora21f6c32009-07-30 23:36:40 +00002233 void *InsertPos = 0;
2234 DependentDecltypeType *Canon
2235 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2236 if (Canon) {
2237 // We already have a "canonical" version of an equivalent, dependent
2238 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002239 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002240 QualType((DecltypeType*)Canon, 0));
2241 }
2242 else {
2243 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002244 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002245 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2246 dt = Canon;
2247 }
2248 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002249 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002250 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002251 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002252 Types.push_back(dt);
2253 return QualType(dt, 0);
2254}
2255
Chris Lattnerfb072462007-01-23 05:45:31 +00002256/// getTagDeclType - Return the unique reference to the type for the
2257/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002258QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002259 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002260 // FIXME: What is the design on getTagDeclType when it requires casting
2261 // away const? mutable?
2262 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002263}
2264
Mike Stump11289f42009-09-09 15:08:12 +00002265/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2266/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2267/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002268CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002269 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002270}
Chris Lattnerfb072462007-01-23 05:45:31 +00002271
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002272/// getSignedWCharType - Return the type of "signed wchar_t".
2273/// Used when in C++, as a GCC extension.
2274QualType ASTContext::getSignedWCharType() const {
2275 // FIXME: derive from "Target" ?
2276 return WCharTy;
2277}
2278
2279/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2280/// Used when in C++, as a GCC extension.
2281QualType ASTContext::getUnsignedWCharType() const {
2282 // FIXME: derive from "Target" ?
2283 return UnsignedIntTy;
2284}
2285
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002286/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2287/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2288QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002289 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002290}
2291
Chris Lattnera21ad802008-04-02 05:18:44 +00002292//===----------------------------------------------------------------------===//
2293// Type Operators
2294//===----------------------------------------------------------------------===//
2295
John McCallfc93cf92009-10-22 22:37:11 +00002296CanQualType ASTContext::getCanonicalParamType(QualType T) {
2297 // Push qualifiers into arrays, and then discard any remaining
2298 // qualifiers.
2299 T = getCanonicalType(T);
2300 const Type *Ty = T.getTypePtr();
2301
2302 QualType Result;
2303 if (isa<ArrayType>(Ty)) {
2304 Result = getArrayDecayedType(QualType(Ty,0));
2305 } else if (isa<FunctionType>(Ty)) {
2306 Result = getPointerType(QualType(Ty, 0));
2307 } else {
2308 Result = QualType(Ty, 0);
2309 }
2310
2311 return CanQualType::CreateUnsafe(Result);
2312}
2313
Chris Lattnered0d0792008-04-06 22:41:35 +00002314/// getCanonicalType - Return the canonical (structural) type corresponding to
2315/// the specified potentially non-canonical type. The non-canonical version
2316/// of a type may have many "decorated" versions of types. Decorators can
2317/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2318/// to be free of any of these, allowing two canonical types to be compared
2319/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002320CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002321 QualifierCollector Quals;
2322 const Type *Ptr = Quals.strip(T);
2323 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002324
John McCall8ccfcb52009-09-24 19:53:00 +00002325 // The canonical internal type will be the canonical type *except*
2326 // that we push type qualifiers down through array types.
2327
2328 // If there are no new qualifiers to push down, stop here.
2329 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002330 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002331
John McCall8ccfcb52009-09-24 19:53:00 +00002332 // If the type qualifiers are on an array type, get the canonical
2333 // type of the array with the qualifiers applied to the element
2334 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002335 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2336 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002337 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002338
Chris Lattner7adf0762008-08-04 07:31:14 +00002339 // Get the canonical version of the element with the extra qualifiers on it.
2340 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002341 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002342 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002343
Chris Lattner7adf0762008-08-04 07:31:14 +00002344 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002345 return CanQualType::CreateUnsafe(
2346 getConstantArrayType(NewEltTy, CAT->getSize(),
2347 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002348 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002349 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002350 return CanQualType::CreateUnsafe(
2351 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002352 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002353
Douglas Gregor4619e432008-12-05 23:32:09 +00002354 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002355 return CanQualType::CreateUnsafe(
2356 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002357 DSAT->getSizeExpr() ?
2358 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002359 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002360 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002361 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002362
Chris Lattner7adf0762008-08-04 07:31:14 +00002363 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002364 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002365 VAT->getSizeExpr() ?
2366 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002367 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002368 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002369 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002370}
2371
Chandler Carruth607f38e2009-12-29 07:16:59 +00002372QualType ASTContext::getUnqualifiedArrayType(QualType T,
2373 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002374 Quals = T.getQualifiers();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002375 if (!isa<ArrayType>(T)) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002376 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002377 }
2378
Chandler Carruth607f38e2009-12-29 07:16:59 +00002379 const ArrayType *AT = cast<ArrayType>(T);
2380 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002381 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002382 if (Elt == UnqualElt)
2383 return T;
2384
2385 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2386 return getConstantArrayType(UnqualElt, CAT->getSize(),
2387 CAT->getSizeModifier(), 0);
2388 }
2389
2390 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2391 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2392 }
2393
2394 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2395 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2396 DSAT->getSizeModifier(), 0,
2397 SourceRange());
2398}
2399
John McCall847e2a12009-11-24 18:42:40 +00002400DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2401 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2402 return TD->getDeclName();
2403
2404 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2405 if (DTN->isIdentifier()) {
2406 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2407 } else {
2408 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2409 }
2410 }
2411
John McCalld28ae272009-12-02 08:04:21 +00002412 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2413 assert(Storage);
2414 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002415}
2416
Douglas Gregor6bc50582009-05-07 06:41:52 +00002417TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2418 // If this template name refers to a template, the canonical
2419 // template name merely stores the template itself.
2420 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002421 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002422
John McCalld28ae272009-12-02 08:04:21 +00002423 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002424
Douglas Gregor6bc50582009-05-07 06:41:52 +00002425 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2426 assert(DTN && "Non-dependent template names must refer to template decls.");
2427 return DTN->CanonicalTemplateName;
2428}
2429
Douglas Gregoradee3e32009-11-11 23:06:43 +00002430bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2431 X = getCanonicalTemplateName(X);
2432 Y = getCanonicalTemplateName(Y);
2433 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2434}
2435
Mike Stump11289f42009-09-09 15:08:12 +00002436TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002437ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2438 switch (Arg.getKind()) {
2439 case TemplateArgument::Null:
2440 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002441
Douglas Gregora8e02e72009-07-28 23:00:59 +00002442 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002443 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002444
Douglas Gregora8e02e72009-07-28 23:00:59 +00002445 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002446 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002447
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002448 case TemplateArgument::Template:
2449 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2450
Douglas Gregora8e02e72009-07-28 23:00:59 +00002451 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002452 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002453 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002454
Douglas Gregora8e02e72009-07-28 23:00:59 +00002455 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002456 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002457
Douglas Gregora8e02e72009-07-28 23:00:59 +00002458 case TemplateArgument::Pack: {
2459 // FIXME: Allocate in ASTContext
2460 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2461 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002462 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002463 AEnd = Arg.pack_end();
2464 A != AEnd; (void)++A, ++Idx)
2465 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002466
Douglas Gregora8e02e72009-07-28 23:00:59 +00002467 TemplateArgument Result;
2468 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2469 return Result;
2470 }
2471 }
2472
2473 // Silence GCC warning
2474 assert(false && "Unhandled template argument kind");
2475 return TemplateArgument();
2476}
2477
Douglas Gregor333489b2009-03-27 23:10:48 +00002478NestedNameSpecifier *
2479ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002480 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002481 return 0;
2482
2483 switch (NNS->getKind()) {
2484 case NestedNameSpecifier::Identifier:
2485 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002486 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002487 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2488 NNS->getAsIdentifier());
2489
2490 case NestedNameSpecifier::Namespace:
2491 // A namespace is canonical; build a nested-name-specifier with
2492 // this namespace and no prefix.
2493 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2494
2495 case NestedNameSpecifier::TypeSpec:
2496 case NestedNameSpecifier::TypeSpecWithTemplate: {
2497 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002498 return NestedNameSpecifier::Create(*this, 0,
2499 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002500 T.getTypePtr());
2501 }
2502
2503 case NestedNameSpecifier::Global:
2504 // The global specifier is canonical and unique.
2505 return NNS;
2506 }
2507
2508 // Required to silence a GCC warning
2509 return 0;
2510}
2511
Chris Lattner7adf0762008-08-04 07:31:14 +00002512
2513const ArrayType *ASTContext::getAsArrayType(QualType T) {
2514 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002515 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002516 // Handle the common positive case fast.
2517 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2518 return AT;
2519 }
Mike Stump11289f42009-09-09 15:08:12 +00002520
John McCall8ccfcb52009-09-24 19:53:00 +00002521 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002522 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002523 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002524 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002525
John McCall8ccfcb52009-09-24 19:53:00 +00002526 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002527 // implements C99 6.7.3p8: "If the specification of an array type includes
2528 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002529
Chris Lattner7adf0762008-08-04 07:31:14 +00002530 // If we get here, we either have type qualifiers on the type, or we have
2531 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002532 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002533
John McCall8ccfcb52009-09-24 19:53:00 +00002534 QualifierCollector Qs;
2535 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002536
Chris Lattner7adf0762008-08-04 07:31:14 +00002537 // If we have a simple case, just return now.
2538 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002539 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002540 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002541
Chris Lattner7adf0762008-08-04 07:31:14 +00002542 // Otherwise, we have an array and we have qualifiers on it. Push the
2543 // qualifiers into the array element type and return a new array type.
2544 // Get the canonical version of the element with the extra qualifiers on it.
2545 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002546 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002547
Chris Lattner7adf0762008-08-04 07:31:14 +00002548 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2549 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2550 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002551 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002552 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2553 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2554 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002555 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002556
Mike Stump11289f42009-09-09 15:08:12 +00002557 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002558 = dyn_cast<DependentSizedArrayType>(ATy))
2559 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002560 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002561 DSAT->getSizeExpr() ?
2562 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002563 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002564 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002565 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002566
Chris Lattner7adf0762008-08-04 07:31:14 +00002567 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002568 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002569 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002570 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002571 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002572 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002573 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002574}
2575
2576
Chris Lattnera21ad802008-04-02 05:18:44 +00002577/// getArrayDecayedType - Return the properly qualified result of decaying the
2578/// specified array type to a pointer. This operation is non-trivial when
2579/// handling typedefs etc. The canonical type of "T" must be an array type,
2580/// this returns a pointer to a properly qualified element of the array.
2581///
2582/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2583QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002584 // Get the element type with 'getAsArrayType' so that we don't lose any
2585 // typedefs in the element type of the array. This also handles propagation
2586 // of type qualifiers from the array type into the element type if present
2587 // (C99 6.7.3p8).
2588 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2589 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002590
Chris Lattner7adf0762008-08-04 07:31:14 +00002591 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002592
2593 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002594 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002595}
2596
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002597QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002598 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002599 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2600 QT = AT->getElementType();
2601 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002602}
2603
Anders Carlsson4bf82142009-09-25 01:23:32 +00002604QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2605 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002606
Anders Carlsson4bf82142009-09-25 01:23:32 +00002607 if (const ArrayType *AT = getAsArrayType(ElemTy))
2608 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002609
Anders Carlssone0808df2008-12-21 03:44:36 +00002610 return ElemTy;
2611}
2612
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002613/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002614uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002615ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2616 uint64_t ElementCount = 1;
2617 do {
2618 ElementCount *= CA->getSize().getZExtValue();
2619 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2620 } while (CA);
2621 return ElementCount;
2622}
2623
Steve Naroff0af91202007-04-27 21:51:21 +00002624/// getFloatingRank - Return a relative rank for floating point types.
2625/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002626static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002627 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002628 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002629
John McCall9dd450b2009-09-21 23:43:11 +00002630 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2631 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002632 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002633 case BuiltinType::Float: return FloatRank;
2634 case BuiltinType::Double: return DoubleRank;
2635 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002636 }
2637}
2638
Mike Stump11289f42009-09-09 15:08:12 +00002639/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2640/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002641/// 'typeDomain' is a real floating point or complex type.
2642/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002643QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2644 QualType Domain) const {
2645 FloatingRank EltRank = getFloatingRank(Size);
2646 if (Domain->isComplexType()) {
2647 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002648 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002649 case FloatRank: return FloatComplexTy;
2650 case DoubleRank: return DoubleComplexTy;
2651 case LongDoubleRank: return LongDoubleComplexTy;
2652 }
Steve Naroff0af91202007-04-27 21:51:21 +00002653 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002654
2655 assert(Domain->isRealFloatingType() && "Unknown domain!");
2656 switch (EltRank) {
2657 default: assert(0 && "getFloatingRank(): illegal value for rank");
2658 case FloatRank: return FloatTy;
2659 case DoubleRank: return DoubleTy;
2660 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002661 }
Steve Naroffe4718892007-04-27 18:30:00 +00002662}
2663
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002664/// getFloatingTypeOrder - Compare the rank of the two specified floating
2665/// point types, ignoring the domain of the type (i.e. 'double' ==
2666/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002667/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002668int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2669 FloatingRank LHSR = getFloatingRank(LHS);
2670 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002671
Chris Lattnerb90739d2008-04-06 23:38:49 +00002672 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002673 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002674 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002675 return 1;
2676 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002677}
2678
Chris Lattner76a00cf2008-04-06 22:59:24 +00002679/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2680/// routine will assert if passed a built-in type that isn't an integer or enum,
2681/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002682unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002683 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002684 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002685 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002686
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002687 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2688 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2689
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002690 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2691 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2692
2693 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2694 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2695
Chris Lattner76a00cf2008-04-06 22:59:24 +00002696 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002697 default: assert(0 && "getIntegerRank(): not a built-in integer");
2698 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002699 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002700 case BuiltinType::Char_S:
2701 case BuiltinType::Char_U:
2702 case BuiltinType::SChar:
2703 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002704 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002705 case BuiltinType::Short:
2706 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002707 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002708 case BuiltinType::Int:
2709 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002710 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002711 case BuiltinType::Long:
2712 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002713 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002714 case BuiltinType::LongLong:
2715 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002716 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002717 case BuiltinType::Int128:
2718 case BuiltinType::UInt128:
2719 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002720 }
2721}
2722
Eli Friedman629ffb92009-08-20 04:21:42 +00002723/// \brief Whether this is a promotable bitfield reference according
2724/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2725///
2726/// \returns the type this bit-field will promote to, or NULL if no
2727/// promotion occurs.
2728QualType ASTContext::isPromotableBitField(Expr *E) {
2729 FieldDecl *Field = E->getBitField();
2730 if (!Field)
2731 return QualType();
2732
2733 QualType FT = Field->getType();
2734
2735 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2736 uint64_t BitWidth = BitWidthAP.getZExtValue();
2737 uint64_t IntSize = getTypeSize(IntTy);
2738 // GCC extension compatibility: if the bit-field size is less than or equal
2739 // to the size of int, it gets promoted no matter what its type is.
2740 // For instance, unsigned long bf : 4 gets promoted to signed int.
2741 if (BitWidth < IntSize)
2742 return IntTy;
2743
2744 if (BitWidth == IntSize)
2745 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2746
2747 // Types bigger than int are not subject to promotions, and therefore act
2748 // like the base type.
2749 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2750 // is ridiculous.
2751 return QualType();
2752}
2753
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002754/// getPromotedIntegerType - Returns the type that Promotable will
2755/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2756/// integer type.
2757QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2758 assert(!Promotable.isNull());
2759 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002760 if (const EnumType *ET = Promotable->getAs<EnumType>())
2761 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002762 if (Promotable->isSignedIntegerType())
2763 return IntTy;
2764 uint64_t PromotableSize = getTypeSize(Promotable);
2765 uint64_t IntSize = getTypeSize(IntTy);
2766 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2767 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2768}
2769
Mike Stump11289f42009-09-09 15:08:12 +00002770/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002771/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002772/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002773int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002774 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2775 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002776 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002777
Chris Lattner76a00cf2008-04-06 22:59:24 +00002778 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2779 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002780
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002781 unsigned LHSRank = getIntegerRank(LHSC);
2782 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002783
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002784 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2785 if (LHSRank == RHSRank) return 0;
2786 return LHSRank > RHSRank ? 1 : -1;
2787 }
Mike Stump11289f42009-09-09 15:08:12 +00002788
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002789 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2790 if (LHSUnsigned) {
2791 // If the unsigned [LHS] type is larger, return it.
2792 if (LHSRank >= RHSRank)
2793 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002795 // If the signed type can represent all values of the unsigned type, it
2796 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002797 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002798 return -1;
2799 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002800
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002801 // If the unsigned [RHS] type is larger, return it.
2802 if (RHSRank >= LHSRank)
2803 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002804
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002805 // If the signed type can represent all values of the unsigned type, it
2806 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002807 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002808 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002809}
Anders Carlsson98f07902007-08-17 05:31:46 +00002810
Anders Carlsson6d417272009-11-14 21:45:58 +00002811static RecordDecl *
2812CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2813 SourceLocation L, IdentifierInfo *Id) {
2814 if (Ctx.getLangOptions().CPlusPlus)
2815 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2816 else
2817 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2818}
2819
Mike Stump11289f42009-09-09 15:08:12 +00002820// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002821QualType ASTContext::getCFConstantStringType() {
2822 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002823 CFConstantStringTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002824 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2825 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002826 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002827
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002828 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002829
Anders Carlsson98f07902007-08-17 05:31:46 +00002830 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002831 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002832 // int flags;
2833 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002834 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002835 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002836 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002837 FieldTypes[3] = LongTy;
2838
Anders Carlsson98f07902007-08-17 05:31:46 +00002839 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002840 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002841 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002842 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002843 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002844 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002845 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002846 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002847 }
2848
Douglas Gregord5058122010-02-11 01:19:42 +00002849 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002850 }
Mike Stump11289f42009-09-09 15:08:12 +00002851
Anders Carlsson98f07902007-08-17 05:31:46 +00002852 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002853}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002854
Douglas Gregor512b0772009-04-23 22:29:11 +00002855void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002856 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002857 assert(Rec && "Invalid CFConstantStringType");
2858 CFConstantStringTypeDecl = Rec->getDecl();
2859}
2860
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002861// getNSConstantStringType - Return the type used for constant NSStrings.
2862QualType ASTContext::getNSConstantStringType() {
2863 if (!NSConstantStringTypeDecl) {
2864 NSConstantStringTypeDecl =
2865 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2866 &Idents.get("__builtin_NSString"));
2867 NSConstantStringTypeDecl->startDefinition();
2868
2869 QualType FieldTypes[3];
2870
2871 // const int *isa;
2872 FieldTypes[0] = getPointerType(IntTy.withConst());
2873 // const char *str;
2874 FieldTypes[1] = getPointerType(CharTy.withConst());
2875 // unsigned int length;
2876 FieldTypes[2] = UnsignedIntTy;
2877
2878 // Create fields
2879 for (unsigned i = 0; i < 3; ++i) {
2880 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2881 SourceLocation(), 0,
2882 FieldTypes[i], /*TInfo=*/0,
2883 /*BitWidth=*/0,
2884 /*Mutable=*/false);
2885 NSConstantStringTypeDecl->addDecl(Field);
2886 }
2887
2888 NSConstantStringTypeDecl->completeDefinition();
2889 }
2890
2891 return getTagDeclType(NSConstantStringTypeDecl);
2892}
2893
2894void ASTContext::setNSConstantStringType(QualType T) {
2895 const RecordType *Rec = T->getAs<RecordType>();
2896 assert(Rec && "Invalid NSConstantStringType");
2897 NSConstantStringTypeDecl = Rec->getDecl();
2898}
2899
Mike Stump11289f42009-09-09 15:08:12 +00002900QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002901 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002902 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002903 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2904 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00002905 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002906
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002907 QualType FieldTypes[] = {
2908 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002909 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002910 getPointerType(UnsignedLongTy),
2911 getConstantArrayType(UnsignedLongTy,
2912 llvm::APInt(32, 5), ArrayType::Normal, 0)
2913 };
Mike Stump11289f42009-09-09 15:08:12 +00002914
Douglas Gregor91f84212008-12-11 16:49:14 +00002915 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002916 FieldDecl *Field = FieldDecl::Create(*this,
2917 ObjCFastEnumerationStateTypeDecl,
2918 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002919 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002920 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002921 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002922 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002923 }
Mike Stump11289f42009-09-09 15:08:12 +00002924
Douglas Gregord5058122010-02-11 01:19:42 +00002925 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002926 }
Mike Stump11289f42009-09-09 15:08:12 +00002927
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002928 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2929}
2930
Mike Stumpd0153282009-10-20 02:12:22 +00002931QualType ASTContext::getBlockDescriptorType() {
2932 if (BlockDescriptorType)
2933 return getTagDeclType(BlockDescriptorType);
2934
2935 RecordDecl *T;
2936 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002937 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2938 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00002939 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002940
2941 QualType FieldTypes[] = {
2942 UnsignedLongTy,
2943 UnsignedLongTy,
2944 };
2945
2946 const char *FieldNames[] = {
2947 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002948 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002949 };
2950
2951 for (size_t i = 0; i < 2; ++i) {
2952 FieldDecl *Field = FieldDecl::Create(*this,
2953 T,
2954 SourceLocation(),
2955 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00002956 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00002957 /*BitWidth=*/0,
2958 /*Mutable=*/false);
2959 T->addDecl(Field);
2960 }
2961
Douglas Gregord5058122010-02-11 01:19:42 +00002962 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002963
2964 BlockDescriptorType = T;
2965
2966 return getTagDeclType(BlockDescriptorType);
2967}
2968
2969void ASTContext::setBlockDescriptorType(QualType T) {
2970 const RecordType *Rec = T->getAs<RecordType>();
2971 assert(Rec && "Invalid BlockDescriptorType");
2972 BlockDescriptorType = Rec->getDecl();
2973}
2974
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002975QualType ASTContext::getBlockDescriptorExtendedType() {
2976 if (BlockDescriptorExtendedType)
2977 return getTagDeclType(BlockDescriptorExtendedType);
2978
2979 RecordDecl *T;
2980 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002981 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2982 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00002983 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002984
2985 QualType FieldTypes[] = {
2986 UnsignedLongTy,
2987 UnsignedLongTy,
2988 getPointerType(VoidPtrTy),
2989 getPointerType(VoidPtrTy)
2990 };
2991
2992 const char *FieldNames[] = {
2993 "reserved",
2994 "Size",
2995 "CopyFuncPtr",
2996 "DestroyFuncPtr"
2997 };
2998
2999 for (size_t i = 0; i < 4; ++i) {
3000 FieldDecl *Field = FieldDecl::Create(*this,
3001 T,
3002 SourceLocation(),
3003 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003004 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003005 /*BitWidth=*/0,
3006 /*Mutable=*/false);
3007 T->addDecl(Field);
3008 }
3009
Douglas Gregord5058122010-02-11 01:19:42 +00003010 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003011
3012 BlockDescriptorExtendedType = T;
3013
3014 return getTagDeclType(BlockDescriptorExtendedType);
3015}
3016
3017void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3018 const RecordType *Rec = T->getAs<RecordType>();
3019 assert(Rec && "Invalid BlockDescriptorType");
3020 BlockDescriptorExtendedType = Rec->getDecl();
3021}
3022
Mike Stump94967902009-10-21 18:16:27 +00003023bool ASTContext::BlockRequiresCopying(QualType Ty) {
3024 if (Ty->isBlockPointerType())
3025 return true;
3026 if (isObjCNSObjectType(Ty))
3027 return true;
3028 if (Ty->isObjCObjectPointerType())
3029 return true;
3030 return false;
3031}
3032
3033QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3034 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003035 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003036 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003037 // unsigned int __flags;
3038 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003039 // void *__copy_helper; // as needed
3040 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003041 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003042 // } *
3043
Mike Stump94967902009-10-21 18:16:27 +00003044 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3045
3046 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003047 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003048 llvm::SmallString<36> Name;
3049 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3050 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003051 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003052 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3053 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003054 T->startDefinition();
3055 QualType Int32Ty = IntTy;
3056 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3057 QualType FieldTypes[] = {
3058 getPointerType(VoidPtrTy),
3059 getPointerType(getTagDeclType(T)),
3060 Int32Ty,
3061 Int32Ty,
3062 getPointerType(VoidPtrTy),
3063 getPointerType(VoidPtrTy),
3064 Ty
3065 };
3066
3067 const char *FieldNames[] = {
3068 "__isa",
3069 "__forwarding",
3070 "__flags",
3071 "__size",
3072 "__copy_helper",
3073 "__destroy_helper",
3074 DeclName,
3075 };
3076
3077 for (size_t i = 0; i < 7; ++i) {
3078 if (!HasCopyAndDispose && i >=4 && i <= 5)
3079 continue;
3080 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3081 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003082 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003083 /*BitWidth=*/0, /*Mutable=*/false);
3084 T->addDecl(Field);
3085 }
3086
Douglas Gregord5058122010-02-11 01:19:42 +00003087 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003088
3089 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003090}
3091
3092
3093QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003094 bool BlockHasCopyDispose,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003095 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpd0153282009-10-20 02:12:22 +00003096 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003097 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003098 llvm::SmallString<36> Name;
3099 llvm::raw_svector_ostream(Name) << "__block_literal_"
3100 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003101 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003102 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3103 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003104 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003105 QualType FieldTypes[] = {
3106 getPointerType(VoidPtrTy),
3107 IntTy,
3108 IntTy,
3109 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003110 (BlockHasCopyDispose ?
3111 getPointerType(getBlockDescriptorExtendedType()) :
3112 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003113 };
3114
3115 const char *FieldNames[] = {
3116 "__isa",
3117 "__flags",
3118 "__reserved",
3119 "__FuncPtr",
3120 "__descriptor"
3121 };
3122
3123 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003124 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003125 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003126 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003127 /*BitWidth=*/0, /*Mutable=*/false);
3128 T->addDecl(Field);
3129 }
3130
3131 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3132 const Expr *E = BlockDeclRefDecls[i];
3133 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3134 clang::IdentifierInfo *Name = 0;
3135 if (BDRE) {
3136 const ValueDecl *D = BDRE->getDecl();
3137 Name = &Idents.get(D->getName());
3138 }
3139 QualType FieldType = E->getType();
3140
3141 if (BDRE && BDRE->isByRef())
Mike Stump94967902009-10-21 18:16:27 +00003142 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3143 FieldType);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003144
3145 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallbcd03502009-12-07 02:54:59 +00003146 Name, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003147 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpd0153282009-10-20 02:12:22 +00003148 T->addDecl(Field);
3149 }
3150
Douglas Gregord5058122010-02-11 01:19:42 +00003151 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003152
3153 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003154}
3155
Douglas Gregor512b0772009-04-23 22:29:11 +00003156void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003157 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003158 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3159 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3160}
3161
Anders Carlsson18acd442007-10-29 06:33:42 +00003162// This returns true if a type has been typedefed to BOOL:
3163// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003164static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003165 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003166 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3167 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003168
Anders Carlssond8499822007-10-29 05:01:08 +00003169 return false;
3170}
3171
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003172/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003173/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003174CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003175 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003176
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003177 // Make all integer and enum types at least as large as an int
Ken Dyck40775002010-01-11 17:06:35 +00003178 if (sz.isPositive() && type->isIntegralType())
3179 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003180 // Treat arrays as pointers, since that's how they're passed in.
3181 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003182 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003183 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003184}
3185
3186static inline
3187std::string charUnitsToString(const CharUnits &CU) {
3188 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003189}
3190
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003191/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003192/// declaration.
3193void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3194 std::string& S) {
3195 const BlockDecl *Decl = Expr->getBlockDecl();
3196 QualType BlockTy =
3197 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3198 // Encode result type.
3199 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3200 // Compute size of all parameters.
3201 // Start with computing size of a pointer in number of bytes.
3202 // FIXME: There might(should) be a better way of doing this computation!
3203 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003204 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3205 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003206 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003207 E = Decl->param_end(); PI != E; ++PI) {
3208 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003209 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003210 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003211 ParmOffset += sz;
3212 }
3213 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003214 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003215 // Block pointer and offset.
3216 S += "@?0";
3217 ParmOffset = PtrSize;
3218
3219 // Argument types.
3220 ParmOffset = PtrSize;
3221 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3222 Decl->param_end(); PI != E; ++PI) {
3223 ParmVarDecl *PVDecl = *PI;
3224 QualType PType = PVDecl->getOriginalType();
3225 if (const ArrayType *AT =
3226 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3227 // Use array's original type only if it has known number of
3228 // elements.
3229 if (!isa<ConstantArrayType>(AT))
3230 PType = PVDecl->getType();
3231 } else if (PType->isFunctionType())
3232 PType = PVDecl->getType();
3233 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003234 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003235 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003236 }
3237}
3238
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003239/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003240/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003241void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003242 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003243 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003244 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003245 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003246 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003247 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003248 // Compute size of all parameters.
3249 // Start with computing size of a pointer in number of bytes.
3250 // FIXME: There might(should) be a better way of doing this computation!
3251 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003252 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003253 // The first two arguments (self and _cmd) are pointers; account for
3254 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003255 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003256 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003257 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003258 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003259 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003260 assert (sz.isPositive() &&
3261 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003262 ParmOffset += sz;
3263 }
Ken Dyck40775002010-01-11 17:06:35 +00003264 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003265 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003266 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003267
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003268 // Argument types.
3269 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003270 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003271 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003272 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003273 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003274 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003275 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3276 // Use array's original type only if it has known number of
3277 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003278 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003279 PType = PVDecl->getType();
3280 } else if (PType->isFunctionType())
3281 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003282 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003283 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003284 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003285 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003286 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003287 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003288 }
3289}
3290
Daniel Dunbar4932b362008-08-28 04:38:10 +00003291/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003292/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003293/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3294/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003295/// Property attributes are stored as a comma-delimited C string. The simple
3296/// attributes readonly and bycopy are encoded as single characters. The
3297/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3298/// encoded as single characters, followed by an identifier. Property types
3299/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003300/// these attributes are defined by the following enumeration:
3301/// @code
3302/// enum PropertyAttributes {
3303/// kPropertyReadOnly = 'R', // property is read-only.
3304/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3305/// kPropertyByref = '&', // property is a reference to the value last assigned
3306/// kPropertyDynamic = 'D', // property is dynamic
3307/// kPropertyGetter = 'G', // followed by getter selector name
3308/// kPropertySetter = 'S', // followed by setter selector name
3309/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3310/// kPropertyType = 't' // followed by old-style type encoding.
3311/// kPropertyWeak = 'W' // 'weak' property
3312/// kPropertyStrong = 'P' // property GC'able
3313/// kPropertyNonAtomic = 'N' // property non-atomic
3314/// };
3315/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003316void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003317 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003318 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003319 // Collect information from the property implementation decl(s).
3320 bool Dynamic = false;
3321 ObjCPropertyImplDecl *SynthesizePID = 0;
3322
3323 // FIXME: Duplicated code due to poor abstraction.
3324 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003325 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003326 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3327 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003328 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003329 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003330 ObjCPropertyImplDecl *PID = *i;
3331 if (PID->getPropertyDecl() == PD) {
3332 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3333 Dynamic = true;
3334 } else {
3335 SynthesizePID = PID;
3336 }
3337 }
3338 }
3339 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003340 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003341 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003342 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003343 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003344 ObjCPropertyImplDecl *PID = *i;
3345 if (PID->getPropertyDecl() == PD) {
3346 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3347 Dynamic = true;
3348 } else {
3349 SynthesizePID = PID;
3350 }
3351 }
Mike Stump11289f42009-09-09 15:08:12 +00003352 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003353 }
3354 }
3355
3356 // FIXME: This is not very efficient.
3357 S = "T";
3358
3359 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003360 // GCC has some special rules regarding encoding of properties which
3361 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003362 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003363 true /* outermost type */,
3364 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003365
3366 if (PD->isReadOnly()) {
3367 S += ",R";
3368 } else {
3369 switch (PD->getSetterKind()) {
3370 case ObjCPropertyDecl::Assign: break;
3371 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003372 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003373 }
3374 }
3375
3376 // It really isn't clear at all what this means, since properties
3377 // are "dynamic by default".
3378 if (Dynamic)
3379 S += ",D";
3380
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003381 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3382 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003383
Daniel Dunbar4932b362008-08-28 04:38:10 +00003384 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3385 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003386 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003387 }
3388
3389 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3390 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003391 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003392 }
3393
3394 if (SynthesizePID) {
3395 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3396 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003397 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003398 }
3399
3400 // FIXME: OBJCGC: weak & strong
3401}
3402
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003403/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003404/// Another legacy compatibility encoding: 32-bit longs are encoded as
3405/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003406/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3407///
3408void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003409 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003410 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003411 if (BT->getKind() == BuiltinType::ULong &&
3412 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003413 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003414 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003415 if (BT->getKind() == BuiltinType::Long &&
3416 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003417 PointeeTy = IntTy;
3418 }
3419 }
3420}
3421
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003422void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003423 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003424 // We follow the behavior of gcc, expanding structures which are
3425 // directly pointed to, and expanding embedded structures. Note that
3426 // these rules are sufficient to prevent recursive encoding of the
3427 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003428 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003429 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003430}
3431
Mike Stump11289f42009-09-09 15:08:12 +00003432static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003433 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003434 const Expr *E = FD->getBitWidth();
3435 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3436 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00003437 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003438 S += 'b';
3439 S += llvm::utostr(N);
3440}
3441
Daniel Dunbar07d07852009-10-18 21:17:35 +00003442// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003443void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3444 bool ExpandPointedToStructures,
3445 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003446 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003447 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003448 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00003449 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003450 if (FD && FD->isBitField())
3451 return EncodeBitField(this, S, FD);
3452 char encoding;
3453 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003454 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00003455 case BuiltinType::Void: encoding = 'v'; break;
3456 case BuiltinType::Bool: encoding = 'B'; break;
3457 case BuiltinType::Char_U:
3458 case BuiltinType::UChar: encoding = 'C'; break;
3459 case BuiltinType::UShort: encoding = 'S'; break;
3460 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003461 case BuiltinType::ULong:
3462 encoding =
3463 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00003464 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003465 case BuiltinType::UInt128: encoding = 'T'; break;
3466 case BuiltinType::ULongLong: encoding = 'Q'; break;
3467 case BuiltinType::Char_S:
3468 case BuiltinType::SChar: encoding = 'c'; break;
3469 case BuiltinType::Short: encoding = 's'; break;
3470 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003471 case BuiltinType::Long:
3472 encoding =
3473 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003474 break;
3475 case BuiltinType::LongLong: encoding = 'q'; break;
3476 case BuiltinType::Int128: encoding = 't'; break;
3477 case BuiltinType::Float: encoding = 'f'; break;
3478 case BuiltinType::Double: encoding = 'd'; break;
3479 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003480 }
Mike Stump11289f42009-09-09 15:08:12 +00003481
Chris Lattnere7cabb92009-07-13 00:10:46 +00003482 S += encoding;
3483 return;
3484 }
Mike Stump11289f42009-09-09 15:08:12 +00003485
John McCall9dd450b2009-09-21 23:43:11 +00003486 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003487 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003488 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003489 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003490 return;
3491 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003492
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003493 // encoding for pointer or r3eference types.
3494 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003495 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003496 if (PT->isObjCSelType()) {
3497 S += ':';
3498 return;
3499 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003500 PointeeTy = PT->getPointeeType();
3501 }
3502 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3503 PointeeTy = RT->getPointeeType();
3504 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003505 bool isReadOnly = false;
3506 // For historical/compatibility reasons, the read-only qualifier of the
3507 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3508 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003509 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003510 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003511 if (OutermostType && T.isConstQualified()) {
3512 isReadOnly = true;
3513 S += 'r';
3514 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003515 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003516 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003517 while (P->getAs<PointerType>())
3518 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003519 if (P.isConstQualified()) {
3520 isReadOnly = true;
3521 S += 'r';
3522 }
3523 }
3524 if (isReadOnly) {
3525 // Another legacy compatibility encoding. Some ObjC qualifier and type
3526 // combinations need to be rearranged.
3527 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003528 if (llvm::StringRef(S).endswith("nr"))
3529 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003530 }
Mike Stump11289f42009-09-09 15:08:12 +00003531
Anders Carlssond8499822007-10-29 05:01:08 +00003532 if (PointeeTy->isCharType()) {
3533 // char pointer types should be encoded as '*' unless it is a
3534 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003535 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003536 S += '*';
3537 return;
3538 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003539 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003540 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3541 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3542 S += '#';
3543 return;
3544 }
3545 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3546 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3547 S += '@';
3548 return;
3549 }
3550 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003551 }
Anders Carlssond8499822007-10-29 05:01:08 +00003552 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003553 getLegacyIntegralTypeEncoding(PointeeTy);
3554
Mike Stump11289f42009-09-09 15:08:12 +00003555 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003556 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003557 return;
3558 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003559
Chris Lattnere7cabb92009-07-13 00:10:46 +00003560 if (const ArrayType *AT =
3561 // Ignore type qualifiers etc.
3562 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003563 if (isa<IncompleteArrayType>(AT)) {
3564 // Incomplete arrays are encoded as a pointer to the array element.
3565 S += '^';
3566
Mike Stump11289f42009-09-09 15:08:12 +00003567 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003568 false, ExpandStructures, FD);
3569 } else {
3570 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003571
Anders Carlssond05f44b2009-02-22 01:38:57 +00003572 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3573 S += llvm::utostr(CAT->getSize().getZExtValue());
3574 else {
3575 //Variable length arrays are encoded as a regular array with 0 elements.
3576 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3577 S += '0';
3578 }
Mike Stump11289f42009-09-09 15:08:12 +00003579
3580 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003581 false, ExpandStructures, FD);
3582 S += ']';
3583 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003584 return;
3585 }
Mike Stump11289f42009-09-09 15:08:12 +00003586
John McCall9dd450b2009-09-21 23:43:11 +00003587 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003588 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003589 return;
3590 }
Mike Stump11289f42009-09-09 15:08:12 +00003591
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003592 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003593 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003594 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003595 // Anonymous structures print as '?'
3596 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3597 S += II->getName();
3598 } else {
3599 S += '?';
3600 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003601 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003602 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003603 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3604 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003605 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003606 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003607 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003608 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003609 S += '"';
3610 }
Mike Stump11289f42009-09-09 15:08:12 +00003611
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003612 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003613 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003614 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003615 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003616 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003617 QualType qt = Field->getType();
3618 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003619 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003620 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003621 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003622 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003623 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003624 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003625 return;
3626 }
Mike Stump11289f42009-09-09 15:08:12 +00003627
Chris Lattnere7cabb92009-07-13 00:10:46 +00003628 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003629 if (FD && FD->isBitField())
3630 EncodeBitField(this, S, FD);
3631 else
3632 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003633 return;
3634 }
Mike Stump11289f42009-09-09 15:08:12 +00003635
Chris Lattnere7cabb92009-07-13 00:10:46 +00003636 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003637 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003638 return;
3639 }
Mike Stump11289f42009-09-09 15:08:12 +00003640
John McCall8ccfcb52009-09-24 19:53:00 +00003641 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003642 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003643 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003644 S += '{';
3645 const IdentifierInfo *II = OI->getIdentifier();
3646 S += II->getName();
3647 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003648 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003649 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003650 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003651 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003652 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003653 RecFields[i]);
3654 else
Mike Stump11289f42009-09-09 15:08:12 +00003655 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003656 FD);
3657 }
3658 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003659 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003660 }
Mike Stump11289f42009-09-09 15:08:12 +00003661
John McCall9dd450b2009-09-21 23:43:11 +00003662 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003663 if (OPT->isObjCIdType()) {
3664 S += '@';
3665 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003666 }
Mike Stump11289f42009-09-09 15:08:12 +00003667
Steve Narofff0c86112009-10-28 22:03:49 +00003668 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3669 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3670 // Since this is a binary compatibility issue, need to consult with runtime
3671 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003672 S += '#';
3673 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003674 }
Mike Stump11289f42009-09-09 15:08:12 +00003675
Chris Lattnere7cabb92009-07-13 00:10:46 +00003676 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003677 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003678 ExpandPointedToStructures,
3679 ExpandStructures, FD);
3680 if (FD || EncodingProperty) {
3681 // Note that we do extended encoding of protocol qualifer list
3682 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003683 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003684 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3685 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003686 S += '<';
3687 S += (*I)->getNameAsString();
3688 S += '>';
3689 }
3690 S += '"';
3691 }
3692 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003693 }
Mike Stump11289f42009-09-09 15:08:12 +00003694
Chris Lattnere7cabb92009-07-13 00:10:46 +00003695 QualType PointeeTy = OPT->getPointeeType();
3696 if (!EncodingProperty &&
3697 isa<TypedefType>(PointeeTy.getTypePtr())) {
3698 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003699 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003700 // {...};
3701 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003702 getObjCEncodingForTypeImpl(PointeeTy, S,
3703 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003704 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003705 return;
3706 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003707
3708 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003709 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003710 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003711 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003712 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3713 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003714 S += '<';
3715 S += (*I)->getNameAsString();
3716 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003717 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003718 S += '"';
3719 }
3720 return;
3721 }
Mike Stump11289f42009-09-09 15:08:12 +00003722
Chris Lattnere7cabb92009-07-13 00:10:46 +00003723 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003724}
3725
Mike Stump11289f42009-09-09 15:08:12 +00003726void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003727 std::string& S) const {
3728 if (QT & Decl::OBJC_TQ_In)
3729 S += 'n';
3730 if (QT & Decl::OBJC_TQ_Inout)
3731 S += 'N';
3732 if (QT & Decl::OBJC_TQ_Out)
3733 S += 'o';
3734 if (QT & Decl::OBJC_TQ_Bycopy)
3735 S += 'O';
3736 if (QT & Decl::OBJC_TQ_Byref)
3737 S += 'R';
3738 if (QT & Decl::OBJC_TQ_Oneway)
3739 S += 'V';
3740}
3741
Chris Lattnere7cabb92009-07-13 00:10:46 +00003742void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003743 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003744
Anders Carlsson87c149b2007-10-11 01:00:40 +00003745 BuiltinVaListType = T;
3746}
3747
Chris Lattnere7cabb92009-07-13 00:10:46 +00003748void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003749 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003750}
3751
Chris Lattnere7cabb92009-07-13 00:10:46 +00003752void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003753 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003754}
3755
Chris Lattnere7cabb92009-07-13 00:10:46 +00003756void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003757 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003758}
3759
Chris Lattnere7cabb92009-07-13 00:10:46 +00003760void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003761 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003762}
3763
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003764void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003765 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003766 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003767
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003768 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003769}
3770
John McCalld28ae272009-12-02 08:04:21 +00003771/// \brief Retrieve the template name that corresponds to a non-empty
3772/// lookup.
John McCallad371252010-01-20 00:46:10 +00003773TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3774 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003775 unsigned size = End - Begin;
3776 assert(size > 1 && "set is not overloaded!");
3777
3778 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3779 size * sizeof(FunctionTemplateDecl*));
3780 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3781
3782 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003783 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003784 NamedDecl *D = *I;
3785 assert(isa<FunctionTemplateDecl>(D) ||
3786 (isa<UsingShadowDecl>(D) &&
3787 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3788 *Storage++ = D;
3789 }
3790
3791 return TemplateName(OT);
3792}
3793
Douglas Gregordc572a32009-03-30 22:58:21 +00003794/// \brief Retrieve the template name that represents a qualified
3795/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003796TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003797 bool TemplateKeyword,
3798 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003799 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003800 llvm::FoldingSetNodeID ID;
3801 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3802
3803 void *InsertPos = 0;
3804 QualifiedTemplateName *QTN =
3805 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3806 if (!QTN) {
3807 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3808 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3809 }
3810
3811 return TemplateName(QTN);
3812}
3813
3814/// \brief Retrieve the template name that represents a dependent
3815/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003816TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003817 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003818 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003819 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003820
3821 llvm::FoldingSetNodeID ID;
3822 DependentTemplateName::Profile(ID, NNS, Name);
3823
3824 void *InsertPos = 0;
3825 DependentTemplateName *QTN =
3826 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3827
3828 if (QTN)
3829 return TemplateName(QTN);
3830
3831 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3832 if (CanonNNS == NNS) {
3833 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3834 } else {
3835 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3836 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003837 DependentTemplateName *CheckQTN =
3838 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3839 assert(!CheckQTN && "Dependent type name canonicalization broken");
3840 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00003841 }
3842
3843 DependentTemplateNames.InsertNode(QTN, InsertPos);
3844 return TemplateName(QTN);
3845}
3846
Douglas Gregor71395fa2009-11-04 00:56:37 +00003847/// \brief Retrieve the template name that represents a dependent
3848/// template name such as \c MetaFun::template operator+.
3849TemplateName
3850ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3851 OverloadedOperatorKind Operator) {
3852 assert((!NNS || NNS->isDependent()) &&
3853 "Nested name specifier must be dependent");
3854
3855 llvm::FoldingSetNodeID ID;
3856 DependentTemplateName::Profile(ID, NNS, Operator);
3857
3858 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00003859 DependentTemplateName *QTN
3860 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00003861
3862 if (QTN)
3863 return TemplateName(QTN);
3864
3865 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3866 if (CanonNNS == NNS) {
3867 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3868 } else {
3869 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3870 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003871
3872 DependentTemplateName *CheckQTN
3873 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3874 assert(!CheckQTN && "Dependent template name canonicalization broken");
3875 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00003876 }
3877
3878 DependentTemplateNames.InsertNode(QTN, InsertPos);
3879 return TemplateName(QTN);
3880}
3881
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003882/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003883/// TargetInfo, produce the corresponding type. The unsigned @p Type
3884/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003885CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003886 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003887 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003888 case TargetInfo::SignedShort: return ShortTy;
3889 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3890 case TargetInfo::SignedInt: return IntTy;
3891 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3892 case TargetInfo::SignedLong: return LongTy;
3893 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3894 case TargetInfo::SignedLongLong: return LongLongTy;
3895 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3896 }
3897
3898 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003899 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003900}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003901
3902//===----------------------------------------------------------------------===//
3903// Type Predicates.
3904//===----------------------------------------------------------------------===//
3905
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003906/// isObjCNSObjectType - Return true if this is an NSObject object using
3907/// NSObject attribute on a c-style pointer type.
3908/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003909/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003910///
3911bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3912 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3913 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003914 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003915 return true;
3916 }
Mike Stump11289f42009-09-09 15:08:12 +00003917 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003918}
3919
Fariborz Jahanian96207692009-02-18 21:49:28 +00003920/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3921/// garbage collection attribute.
3922///
John McCall8ccfcb52009-09-24 19:53:00 +00003923Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3924 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003925 if (getLangOptions().ObjC1 &&
3926 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003927 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003928 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003929 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003930 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003931 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003932 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003933 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003934 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003935 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003936 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003937 // Non-pointers have none gc'able attribute regardless of the attribute
3938 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003939 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003940 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003941 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003942 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003943}
3944
Chris Lattner49af6a42008-04-07 06:51:04 +00003945//===----------------------------------------------------------------------===//
3946// Type Compatibility Testing
3947//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00003948
Mike Stump11289f42009-09-09 15:08:12 +00003949/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003950/// compatible.
3951static bool areCompatVectorTypes(const VectorType *LHS,
3952 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00003953 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00003954 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00003955 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00003956}
3957
Steve Naroff8e6aee52009-07-23 01:01:38 +00003958//===----------------------------------------------------------------------===//
3959// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3960//===----------------------------------------------------------------------===//
3961
3962/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3963/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003964bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3965 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003966 if (lProto == rProto)
3967 return true;
3968 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3969 E = rProto->protocol_end(); PI != E; ++PI)
3970 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3971 return true;
3972 return false;
3973}
3974
Steve Naroff8e6aee52009-07-23 01:01:38 +00003975/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3976/// return true if lhs's protocols conform to rhs's protocol; false
3977/// otherwise.
3978bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3979 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3980 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3981 return false;
3982}
3983
3984/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3985/// ObjCQualifiedIDType.
3986bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3987 bool compare) {
3988 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00003989 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003990 lhs->isObjCIdType() || lhs->isObjCClassType())
3991 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003992 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003993 rhs->isObjCIdType() || rhs->isObjCClassType())
3994 return true;
3995
3996 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00003997 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00003998
Steve Naroff8e6aee52009-07-23 01:01:38 +00003999 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004000
Steve Naroff8e6aee52009-07-23 01:01:38 +00004001 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004002 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004003 // make sure we check the class hierarchy.
4004 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4005 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4006 E = lhsQID->qual_end(); I != E; ++I) {
4007 // when comparing an id<P> on lhs with a static type on rhs,
4008 // see if static class implements all of id's protocols, directly or
4009 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004010 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004011 return false;
4012 }
4013 }
4014 // If there are no qualifiers and no interface, we have an 'id'.
4015 return true;
4016 }
Mike Stump11289f42009-09-09 15:08:12 +00004017 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004018 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4019 E = lhsQID->qual_end(); I != E; ++I) {
4020 ObjCProtocolDecl *lhsProto = *I;
4021 bool match = false;
4022
4023 // when comparing an id<P> on lhs with a static type on rhs,
4024 // see if static class implements all of id's protocols, directly or
4025 // through its super class and categories.
4026 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4027 E = rhsOPT->qual_end(); J != E; ++J) {
4028 ObjCProtocolDecl *rhsProto = *J;
4029 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4030 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4031 match = true;
4032 break;
4033 }
4034 }
Mike Stump11289f42009-09-09 15:08:12 +00004035 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004036 // make sure we check the class hierarchy.
4037 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4038 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4039 E = lhsQID->qual_end(); I != E; ++I) {
4040 // when comparing an id<P> on lhs with a static type on rhs,
4041 // see if static class implements all of id's protocols, directly or
4042 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004043 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004044 match = true;
4045 break;
4046 }
4047 }
4048 }
4049 if (!match)
4050 return false;
4051 }
Mike Stump11289f42009-09-09 15:08:12 +00004052
Steve Naroff8e6aee52009-07-23 01:01:38 +00004053 return true;
4054 }
Mike Stump11289f42009-09-09 15:08:12 +00004055
Steve Naroff8e6aee52009-07-23 01:01:38 +00004056 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4057 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4058
Mike Stump11289f42009-09-09 15:08:12 +00004059 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004060 lhs->getAsObjCInterfacePointerType()) {
4061 if (lhsOPT->qual_empty()) {
4062 bool match = false;
4063 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4064 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4065 E = rhsQID->qual_end(); I != E; ++I) {
4066 // when comparing an id<P> on lhs with a static type on rhs,
4067 // see if static class implements all of id's protocols, directly or
4068 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004069 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004070 match = true;
4071 break;
4072 }
4073 }
4074 if (!match)
4075 return false;
4076 }
4077 return true;
4078 }
Mike Stump11289f42009-09-09 15:08:12 +00004079 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004080 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4081 E = lhsOPT->qual_end(); I != E; ++I) {
4082 ObjCProtocolDecl *lhsProto = *I;
4083 bool match = false;
4084
4085 // when comparing an id<P> on lhs with a static type on rhs,
4086 // see if static class implements all of id's protocols, directly or
4087 // through its super class and categories.
4088 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4089 E = rhsQID->qual_end(); J != E; ++J) {
4090 ObjCProtocolDecl *rhsProto = *J;
4091 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4092 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4093 match = true;
4094 break;
4095 }
4096 }
4097 if (!match)
4098 return false;
4099 }
4100 return true;
4101 }
4102 return false;
4103}
4104
Eli Friedman47f77112008-08-22 00:56:42 +00004105/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004106/// compatible for assignment from RHS to LHS. This handles validation of any
4107/// protocol qualifiers on the LHS or RHS.
4108///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004109bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4110 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004111 // If either type represents the built-in 'id' or 'Class' types, return true.
4112 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004113 return true;
4114
Steve Naroff8e6aee52009-07-23 01:01:38 +00004115 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00004116 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4117 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004118 false);
4119
Steve Naroff7cae42b2009-07-10 23:34:53 +00004120 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4121 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00004122 if (LHS && RHS) // We have 2 user-defined types.
4123 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004124
Steve Naroff8e6aee52009-07-23 01:01:38 +00004125 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004126}
4127
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004128/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4129/// for providing type-safty for objective-c pointers used to pass/return
4130/// arguments in block literals. When passed as arguments, passing 'A*' where
4131/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4132/// not OK. For the return type, the opposite is not OK.
4133bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4134 const ObjCObjectPointerType *LHSOPT,
4135 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004136 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004137 return true;
4138
4139 if (LHSOPT->isObjCBuiltinType()) {
4140 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4141 }
4142
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004143 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004144 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4145 QualType(RHSOPT,0),
4146 false);
4147
4148 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4149 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4150 if (LHS && RHS) { // We have 2 user-defined types.
4151 if (LHS != RHS) {
4152 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4153 return false;
4154 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4155 return true;
4156 }
4157 else
4158 return true;
4159 }
4160 return false;
4161}
4162
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004163/// getIntersectionOfProtocols - This routine finds the intersection of set
4164/// of protocols inherited from two distinct objective-c pointer objects.
4165/// It is used to build composite qualifier list of the composite type of
4166/// the conditional expression involving two objective-c pointer objects.
4167static
4168void getIntersectionOfProtocols(ASTContext &Context,
4169 const ObjCObjectPointerType *LHSOPT,
4170 const ObjCObjectPointerType *RHSOPT,
4171 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4172
4173 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4174 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4175
4176 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4177 unsigned LHSNumProtocols = LHS->getNumProtocols();
4178 if (LHSNumProtocols > 0)
4179 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4180 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004181 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4182 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004183 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4184 LHSInheritedProtocols.end());
4185 }
4186
4187 unsigned RHSNumProtocols = RHS->getNumProtocols();
4188 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004189 ObjCProtocolDecl **RHSProtocols =
4190 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004191 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4192 if (InheritedProtocolSet.count(RHSProtocols[i]))
4193 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4194 }
4195 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004196 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004197 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004198 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4199 RHSInheritedProtocols.begin(),
4200 E = RHSInheritedProtocols.end(); I != E; ++I)
4201 if (InheritedProtocolSet.count((*I)))
4202 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004203 }
4204}
4205
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004206/// areCommonBaseCompatible - Returns common base class of the two classes if
4207/// one found. Note that this is O'2 algorithm. But it will be called as the
4208/// last type comparison in a ?-exp of ObjC pointer types before a
4209/// warning is issued. So, its invokation is extremely rare.
4210QualType ASTContext::areCommonBaseCompatible(
4211 const ObjCObjectPointerType *LHSOPT,
4212 const ObjCObjectPointerType *RHSOPT) {
4213 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4214 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4215 if (!LHS || !RHS)
4216 return QualType();
4217
4218 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4219 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4220 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004221 if (canAssignObjCInterfaces(LHS, RHS)) {
4222 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4223 getIntersectionOfProtocols(*this,
4224 LHSOPT, RHSOPT, IntersectionOfProtocols);
4225 if (IntersectionOfProtocols.empty())
4226 LHSTy = getObjCObjectPointerType(LHSTy);
4227 else
4228 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4229 IntersectionOfProtocols.size());
4230 return LHSTy;
4231 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004232 }
4233
4234 return QualType();
4235}
4236
Eli Friedman47f77112008-08-22 00:56:42 +00004237bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4238 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00004239 // Verify that the base decls are compatible: the RHS must be a subclass of
4240 // the LHS.
4241 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4242 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004243
Chris Lattner49af6a42008-04-07 06:51:04 +00004244 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4245 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004246 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004247 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004248
Chris Lattner49af6a42008-04-07 06:51:04 +00004249 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4250 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004251 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004252 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004253
Steve Naroffc277ad12009-07-18 15:33:26 +00004254 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4255 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004256 LHSPI != LHSPE; LHSPI++) {
4257 bool RHSImplementsProtocol = false;
4258
4259 // If the RHS doesn't implement the protocol on the left, the types
4260 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00004261 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004262 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004263 RHSPI != RHSPE; RHSPI++) {
4264 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004265 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004266 break;
4267 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004268 }
4269 // FIXME: For better diagnostics, consider passing back the protocol name.
4270 if (!RHSImplementsProtocol)
4271 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004272 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004273 // The RHS implements all protocols listed on the LHS.
4274 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004275}
4276
Steve Naroffb7605152009-02-12 17:52:19 +00004277bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4278 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004279 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4280 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004281
Steve Naroff7cae42b2009-07-10 23:34:53 +00004282 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004283 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004284
4285 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4286 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004287}
4288
Mike Stump11289f42009-09-09 15:08:12 +00004289/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004290/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004291/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004292/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004293bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004294 if (getLangOptions().CPlusPlus)
4295 return hasSameType(LHS, RHS);
4296
Eli Friedman47f77112008-08-22 00:56:42 +00004297 return !mergeTypes(LHS, RHS).isNull();
4298}
4299
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004300bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4301 return !mergeTypes(LHS, RHS, true).isNull();
4302}
4303
4304QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4305 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004306 const FunctionType *lbase = lhs->getAs<FunctionType>();
4307 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004308 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4309 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004310 bool allLTypes = true;
4311 bool allRTypes = true;
4312
4313 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004314 QualType retType;
4315 if (OfBlockPointer)
4316 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4317 else
4318 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004319 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004320 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004321 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004322 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004323 allRTypes = false;
Mike Stumpea086c72009-07-25 23:24:03 +00004324 // FIXME: double check this
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004325 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4326 // rbase->getRegParmAttr() != 0 &&
4327 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004328 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4329 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004330 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4331 lbaseInfo.getRegParm();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004332 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004333 if (NoReturn != lbaseInfo.getNoReturn() ||
4334 RegParm != lbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004335 allLTypes = false;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004336 if (NoReturn != rbaseInfo.getNoReturn() ||
4337 RegParm != rbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004338 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004339 CallingConv lcc = lbaseInfo.getCC();
4340 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004341 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004342 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004343 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004344
Eli Friedman47f77112008-08-22 00:56:42 +00004345 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004346 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4347 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004348 unsigned lproto_nargs = lproto->getNumArgs();
4349 unsigned rproto_nargs = rproto->getNumArgs();
4350
4351 // Compatible functions must have the same number of arguments
4352 if (lproto_nargs != rproto_nargs)
4353 return QualType();
4354
4355 // Variadic and non-variadic functions aren't compatible
4356 if (lproto->isVariadic() != rproto->isVariadic())
4357 return QualType();
4358
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004359 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4360 return QualType();
4361
Eli Friedman47f77112008-08-22 00:56:42 +00004362 // Check argument compatibility
4363 llvm::SmallVector<QualType, 10> types;
4364 for (unsigned i = 0; i < lproto_nargs; i++) {
4365 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4366 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004367 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004368 if (argtype.isNull()) return QualType();
4369 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004370 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4371 allLTypes = false;
4372 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4373 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004374 }
4375 if (allLTypes) return lhs;
4376 if (allRTypes) return rhs;
4377 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004378 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004379 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004380 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004381 }
4382
4383 if (lproto) allRTypes = false;
4384 if (rproto) allLTypes = false;
4385
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004386 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004387 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004388 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004389 if (proto->isVariadic()) return QualType();
4390 // Check that the types are compatible with the types that
4391 // would result from default argument promotions (C99 6.7.5.3p15).
4392 // The only types actually affected are promotable integer
4393 // types and floats, which would be passed as a different
4394 // type depending on whether the prototype is visible.
4395 unsigned proto_nargs = proto->getNumArgs();
4396 for (unsigned i = 0; i < proto_nargs; ++i) {
4397 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004398
4399 // Look at the promotion type of enum types, since that is the type used
4400 // to pass enum values.
4401 if (const EnumType *Enum = argTy->getAs<EnumType>())
4402 argTy = Enum->getDecl()->getPromotionType();
4403
Eli Friedman47f77112008-08-22 00:56:42 +00004404 if (argTy->isPromotableIntegerType() ||
4405 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4406 return QualType();
4407 }
4408
4409 if (allLTypes) return lhs;
4410 if (allRTypes) return rhs;
4411 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004412 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004413 proto->getTypeQuals(),
4414 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004415 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004416 }
4417
4418 if (allLTypes) return lhs;
4419 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004420 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004421 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004422}
4423
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004424QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4425 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004426 // C++ [expr]: If an expression initially has the type "reference to T", the
4427 // type is adjusted to "T" prior to any further analysis, the expression
4428 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004429 // expression is an lvalue unless the reference is an rvalue reference and
4430 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004431 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4432 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4433
Eli Friedman47f77112008-08-22 00:56:42 +00004434 QualType LHSCan = getCanonicalType(LHS),
4435 RHSCan = getCanonicalType(RHS);
4436
4437 // If two types are identical, they are compatible.
4438 if (LHSCan == RHSCan)
4439 return LHS;
4440
John McCall8ccfcb52009-09-24 19:53:00 +00004441 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004442 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4443 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004444 if (LQuals != RQuals) {
4445 // If any of these qualifiers are different, we have a type
4446 // mismatch.
4447 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4448 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4449 return QualType();
4450
4451 // Exactly one GC qualifier difference is allowed: __strong is
4452 // okay if the other type has no GC qualifier but is an Objective
4453 // C object pointer (i.e. implicitly strong by default). We fix
4454 // this by pretending that the unqualified type was actually
4455 // qualified __strong.
4456 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4457 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4458 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4459
4460 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4461 return QualType();
4462
4463 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4464 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4465 }
4466 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4467 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4468 }
Eli Friedman47f77112008-08-22 00:56:42 +00004469 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004470 }
4471
4472 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004473
Eli Friedmandcca6332009-06-01 01:22:52 +00004474 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4475 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004476
Chris Lattnerfd652912008-01-14 05:45:46 +00004477 // We want to consider the two function types to be the same for these
4478 // comparisons, just force one to the other.
4479 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4480 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004481
4482 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004483 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4484 LHSClass = Type::ConstantArray;
4485 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4486 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004487
Nate Begemance4d7fc2008-04-18 23:10:10 +00004488 // Canonicalize ExtVector -> Vector.
4489 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4490 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004491
Chris Lattner95554662008-04-07 05:43:21 +00004492 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004493 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004494 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004495 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004496 // Compatibility is based on the underlying type, not the promotion
4497 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004498 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004499 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4500 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004501 }
John McCall9dd450b2009-09-21 23:43:11 +00004502 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004503 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4504 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004505 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004506
Eli Friedman47f77112008-08-22 00:56:42 +00004507 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004508 }
Eli Friedman47f77112008-08-22 00:56:42 +00004509
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004510 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004511 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004512#define TYPE(Class, Base)
4513#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004514#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004515#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4516#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4517#include "clang/AST/TypeNodes.def"
4518 assert(false && "Non-canonical and dependent types shouldn't get here");
4519 return QualType();
4520
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004521 case Type::LValueReference:
4522 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004523 case Type::MemberPointer:
4524 assert(false && "C++ should never be in mergeTypes");
4525 return QualType();
4526
4527 case Type::IncompleteArray:
4528 case Type::VariableArray:
4529 case Type::FunctionProto:
4530 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004531 assert(false && "Types are eliminated above");
4532 return QualType();
4533
Chris Lattnerfd652912008-01-14 05:45:46 +00004534 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004535 {
4536 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004537 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4538 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004539 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4540 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004541 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004542 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004543 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004544 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004545 return getPointerType(ResultType);
4546 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004547 case Type::BlockPointer:
4548 {
4549 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004550 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4551 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004552 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004553 if (ResultType.isNull()) return QualType();
4554 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4555 return LHS;
4556 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4557 return RHS;
4558 return getBlockPointerType(ResultType);
4559 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004560 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004561 {
4562 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4563 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4564 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4565 return QualType();
4566
4567 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4568 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4569 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4570 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004571 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4572 return LHS;
4573 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4574 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004575 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4576 ArrayType::ArraySizeModifier(), 0);
4577 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4578 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004579 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4580 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004581 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4582 return LHS;
4583 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4584 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004585 if (LVAT) {
4586 // FIXME: This isn't correct! But tricky to implement because
4587 // the array's size has to be the size of LHS, but the type
4588 // has to be different.
4589 return LHS;
4590 }
4591 if (RVAT) {
4592 // FIXME: This isn't correct! But tricky to implement because
4593 // the array's size has to be the size of RHS, but the type
4594 // has to be different.
4595 return RHS;
4596 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004597 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4598 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004599 return getIncompleteArrayType(ResultType,
4600 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004601 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004602 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004603 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004604 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004605 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004606 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004607 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004608 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004609 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004610 case Type::Complex:
4611 // Distinct complex types are incompatible.
4612 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004613 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004614 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004615 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4616 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004617 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004618 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004619 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00004620 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004621 // FIXME: This should be type compatibility, e.g. whether
4622 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00004623 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4624 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00004625 if (LHSIface && RHSIface &&
4626 canAssignObjCInterfaces(LHSIface, RHSIface))
4627 return LHS;
4628
Eli Friedman47f77112008-08-22 00:56:42 +00004629 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004630 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004631 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004632 if (OfBlockPointer) {
4633 if (canAssignObjCInterfacesInBlockPointer(
4634 LHS->getAs<ObjCObjectPointerType>(),
4635 RHS->getAs<ObjCObjectPointerType>()))
4636 return LHS;
4637 return QualType();
4638 }
John McCall9dd450b2009-09-21 23:43:11 +00004639 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4640 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004641 return LHS;
4642
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004643 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004644 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004645 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004646
4647 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004648}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004649
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004650//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004651// Integer Predicates
4652//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004653
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004654unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004655 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004656 return 1;
John McCall56774992009-12-09 09:09:27 +00004657 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004658 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004659 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004660 return (unsigned)getTypeSize(T);
4661}
4662
4663QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4664 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004665
4666 // Turn <4 x signed int> -> <4 x unsigned int>
4667 if (const VectorType *VTy = T->getAs<VectorType>())
4668 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004669 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004670
4671 // For enums, we return the unsigned version of the base type.
4672 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004673 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004674
4675 const BuiltinType *BTy = T->getAs<BuiltinType>();
4676 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004677 switch (BTy->getKind()) {
4678 case BuiltinType::Char_S:
4679 case BuiltinType::SChar:
4680 return UnsignedCharTy;
4681 case BuiltinType::Short:
4682 return UnsignedShortTy;
4683 case BuiltinType::Int:
4684 return UnsignedIntTy;
4685 case BuiltinType::Long:
4686 return UnsignedLongTy;
4687 case BuiltinType::LongLong:
4688 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004689 case BuiltinType::Int128:
4690 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004691 default:
4692 assert(0 && "Unexpected signed integer type");
4693 return QualType();
4694 }
4695}
4696
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004697ExternalASTSource::~ExternalASTSource() { }
4698
4699void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004700
4701
4702//===----------------------------------------------------------------------===//
4703// Builtin Type Computation
4704//===----------------------------------------------------------------------===//
4705
4706/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4707/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004708static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004709 ASTContext::GetBuiltinTypeError &Error,
4710 bool AllowTypeModifiers = true) {
4711 // Modifiers.
4712 int HowLong = 0;
4713 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004714
Chris Lattnerecd79c62009-06-14 00:45:47 +00004715 // Read the modifiers first.
4716 bool Done = false;
4717 while (!Done) {
4718 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004719 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004720 case 'S':
4721 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4722 assert(!Signed && "Can't use 'S' modifier multiple times!");
4723 Signed = true;
4724 break;
4725 case 'U':
4726 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4727 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4728 Unsigned = true;
4729 break;
4730 case 'L':
4731 assert(HowLong <= 2 && "Can't have LLLL modifier");
4732 ++HowLong;
4733 break;
4734 }
4735 }
4736
4737 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004738
Chris Lattnerecd79c62009-06-14 00:45:47 +00004739 // Read the base type.
4740 switch (*Str++) {
4741 default: assert(0 && "Unknown builtin type letter!");
4742 case 'v':
4743 assert(HowLong == 0 && !Signed && !Unsigned &&
4744 "Bad modifiers used with 'v'!");
4745 Type = Context.VoidTy;
4746 break;
4747 case 'f':
4748 assert(HowLong == 0 && !Signed && !Unsigned &&
4749 "Bad modifiers used with 'f'!");
4750 Type = Context.FloatTy;
4751 break;
4752 case 'd':
4753 assert(HowLong < 2 && !Signed && !Unsigned &&
4754 "Bad modifiers used with 'd'!");
4755 if (HowLong)
4756 Type = Context.LongDoubleTy;
4757 else
4758 Type = Context.DoubleTy;
4759 break;
4760 case 's':
4761 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4762 if (Unsigned)
4763 Type = Context.UnsignedShortTy;
4764 else
4765 Type = Context.ShortTy;
4766 break;
4767 case 'i':
4768 if (HowLong == 3)
4769 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4770 else if (HowLong == 2)
4771 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4772 else if (HowLong == 1)
4773 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4774 else
4775 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4776 break;
4777 case 'c':
4778 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4779 if (Signed)
4780 Type = Context.SignedCharTy;
4781 else if (Unsigned)
4782 Type = Context.UnsignedCharTy;
4783 else
4784 Type = Context.CharTy;
4785 break;
4786 case 'b': // boolean
4787 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4788 Type = Context.BoolTy;
4789 break;
4790 case 'z': // size_t.
4791 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4792 Type = Context.getSizeType();
4793 break;
4794 case 'F':
4795 Type = Context.getCFConstantStringType();
4796 break;
4797 case 'a':
4798 Type = Context.getBuiltinVaListType();
4799 assert(!Type.isNull() && "builtin va list type not initialized!");
4800 break;
4801 case 'A':
4802 // This is a "reference" to a va_list; however, what exactly
4803 // this means depends on how va_list is defined. There are two
4804 // different kinds of va_list: ones passed by value, and ones
4805 // passed by reference. An example of a by-value va_list is
4806 // x86, where va_list is a char*. An example of by-ref va_list
4807 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4808 // we want this argument to be a char*&; for x86-64, we want
4809 // it to be a __va_list_tag*.
4810 Type = Context.getBuiltinVaListType();
4811 assert(!Type.isNull() && "builtin va list type not initialized!");
4812 if (Type->isArrayType()) {
4813 Type = Context.getArrayDecayedType(Type);
4814 } else {
4815 Type = Context.getLValueReferenceType(Type);
4816 }
4817 break;
4818 case 'V': {
4819 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004820 unsigned NumElements = strtoul(Str, &End, 10);
4821 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004822
Chris Lattnerecd79c62009-06-14 00:45:47 +00004823 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004824
Chris Lattnerecd79c62009-06-14 00:45:47 +00004825 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00004826 // FIXME: Don't know what to do about AltiVec.
4827 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004828 break;
4829 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004830 case 'X': {
4831 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4832 Type = Context.getComplexType(ElementType);
4833 break;
4834 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004835 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004836 Type = Context.getFILEType();
4837 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004838 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004839 return QualType();
4840 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004841 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004842 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004843 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004844 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004845 else
4846 Type = Context.getjmp_bufType();
4847
Mike Stump2adb4da2009-07-28 23:47:15 +00004848 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004849 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004850 return QualType();
4851 }
4852 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004853 }
Mike Stump11289f42009-09-09 15:08:12 +00004854
Chris Lattnerecd79c62009-06-14 00:45:47 +00004855 if (!AllowTypeModifiers)
4856 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004857
Chris Lattnerecd79c62009-06-14 00:45:47 +00004858 Done = false;
4859 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00004860 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00004861 default: Done = true; --Str; break;
4862 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00004863 case '&':
John McCallb8b94662010-03-12 04:21:28 +00004864 {
4865 // Both pointers and references can have their pointee types
4866 // qualified with an address space.
4867 char *End;
4868 unsigned AddrSpace = strtoul(Str, &End, 10);
4869 if (End != Str && AddrSpace != 0) {
4870 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4871 Str = End;
4872 }
4873 }
4874 if (c == '*')
4875 Type = Context.getPointerType(Type);
4876 else
4877 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004878 break;
4879 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4880 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004881 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004882 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00004883 case 'D':
4884 Type = Context.getVolatileType(Type);
4885 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004886 }
4887 }
Mike Stump11289f42009-09-09 15:08:12 +00004888
Chris Lattnerecd79c62009-06-14 00:45:47 +00004889 return Type;
4890}
4891
4892/// GetBuiltinType - Return the type for the specified builtin.
4893QualType ASTContext::GetBuiltinType(unsigned id,
4894 GetBuiltinTypeError &Error) {
4895 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004896
Chris Lattnerecd79c62009-06-14 00:45:47 +00004897 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004898
Chris Lattnerecd79c62009-06-14 00:45:47 +00004899 Error = GE_None;
4900 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4901 if (Error != GE_None)
4902 return QualType();
4903 while (TypeStr[0] && TypeStr[0] != '.') {
4904 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4905 if (Error != GE_None)
4906 return QualType();
4907
4908 // Do array -> pointer decay. The builtin should use the decayed type.
4909 if (Ty->isArrayType())
4910 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004911
Chris Lattnerecd79c62009-06-14 00:45:47 +00004912 ArgTypes.push_back(Ty);
4913 }
4914
4915 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4916 "'.' should only occur at end of builtin type list!");
4917
4918 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4919 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4920 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00004921
4922 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00004923 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00004924 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004925 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00004926}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004927
4928QualType
4929ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4930 // Perform the usual unary conversions. We do this early so that
4931 // integral promotions to "int" can allow us to exit early, in the
4932 // lhs == rhs check. Also, for conversion purposes, we ignore any
4933 // qualifiers. For example, "const float" and "float" are
4934 // equivalent.
4935 if (lhs->isPromotableIntegerType())
4936 lhs = getPromotedIntegerType(lhs);
4937 else
4938 lhs = lhs.getUnqualifiedType();
4939 if (rhs->isPromotableIntegerType())
4940 rhs = getPromotedIntegerType(rhs);
4941 else
4942 rhs = rhs.getUnqualifiedType();
4943
4944 // If both types are identical, no conversion is needed.
4945 if (lhs == rhs)
4946 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004947
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004948 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4949 // The caller can deal with this (e.g. pointer + int).
4950 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4951 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004952
4953 // At this point, we have two different arithmetic types.
4954
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004955 // Handle complex types first (C99 6.3.1.8p1).
4956 if (lhs->isComplexType() || rhs->isComplexType()) {
4957 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00004958 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004959 // convert the rhs to the lhs complex type.
4960 return lhs;
4961 }
Mike Stump11289f42009-09-09 15:08:12 +00004962 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004963 // convert the lhs to the rhs complex type.
4964 return rhs;
4965 }
4966 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00004967 // When both operands are complex, the shorter operand is converted to the
4968 // type of the longer, and that is the type of the result. This corresponds
4969 // to what is done when combining two real floating-point operands.
4970 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004971 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00004972 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004973 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00004974 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004975 // "double _Complex" is promoted to "long double _Complex".
4976 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004977
4978 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004979 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004980 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004981 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00004982 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004983 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4984 // domains match. This is a requirement for our implementation, C99
4985 // does not require this promotion.
4986 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4987 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4988 return rhs;
4989 } else { // handle "_Complex double, double".
4990 return lhs;
4991 }
4992 }
4993 return lhs; // The domain/size match exactly.
4994 }
4995 // Now handle "real" floating types (i.e. float, double, long double).
4996 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4997 // if we have an integer operand, the result is the real floating type.
4998 if (rhs->isIntegerType()) {
4999 // convert rhs to the lhs floating point type.
5000 return lhs;
5001 }
5002 if (rhs->isComplexIntegerType()) {
5003 // convert rhs to the complex floating point type.
5004 return getComplexType(lhs);
5005 }
5006 if (lhs->isIntegerType()) {
5007 // convert lhs to the rhs floating point type.
5008 return rhs;
5009 }
Mike Stump11289f42009-09-09 15:08:12 +00005010 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005011 // convert lhs to the complex floating point type.
5012 return getComplexType(rhs);
5013 }
5014 // We have two real floating types, float/complex combos were handled above.
5015 // Convert the smaller operand to the bigger result.
5016 int result = getFloatingTypeOrder(lhs, rhs);
5017 if (result > 0) // convert the rhs
5018 return lhs;
5019 assert(result < 0 && "illegal float comparison");
5020 return rhs; // convert the lhs
5021 }
5022 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5023 // Handle GCC complex int extension.
5024 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5025 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5026
5027 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005028 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005029 rhsComplexInt->getElementType()) >= 0)
5030 return lhs; // convert the rhs
5031 return rhs;
5032 } else if (lhsComplexInt && rhs->isIntegerType()) {
5033 // convert the rhs to the lhs complex type.
5034 return lhs;
5035 } else if (rhsComplexInt && lhs->isIntegerType()) {
5036 // convert the lhs to the rhs complex type.
5037 return rhs;
5038 }
5039 }
5040 // Finally, we have two differing integer types.
5041 // The rules for this case are in C99 6.3.1.8
5042 int compare = getIntegerTypeOrder(lhs, rhs);
5043 bool lhsSigned = lhs->isSignedIntegerType(),
5044 rhsSigned = rhs->isSignedIntegerType();
5045 QualType destType;
5046 if (lhsSigned == rhsSigned) {
5047 // Same signedness; use the higher-ranked type
5048 destType = compare >= 0 ? lhs : rhs;
5049 } else if (compare != (lhsSigned ? 1 : -1)) {
5050 // The unsigned type has greater than or equal rank to the
5051 // signed type, so use the unsigned type
5052 destType = lhsSigned ? rhs : lhs;
5053 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5054 // The two types are different widths; if we are here, that
5055 // means the signed type is larger than the unsigned type, so
5056 // use the signed type.
5057 destType = lhsSigned ? lhs : rhs;
5058 } else {
5059 // The signed type is higher-ranked than the unsigned type,
5060 // but isn't actually any bigger (like unsigned int and long
5061 // on most 32-bit systems). Use the unsigned type corresponding
5062 // to the signed type.
5063 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5064 }
5065 return destType;
5066}