blob: 0a39575c43186ec23ca612b52dc245d4108b5704 [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 Jahanianb3102592010-04-26 23:51:25 +0000810/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
811/// construction (construct=true) or destruction (construct=false)
812///
813void ASTContext::CollectIvarsToConstructOrDestruct(const ObjCInterfaceDecl *OI,
814 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
815 bool construct) {
816 if (!getLangOptions().CPlusPlus)
817 return;
818 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
819 E = OI->ivar_end(); I != E; ++I) {
820 ObjCIvarDecl *Iv = (*I);
821 if (const RecordType *RT = Iv->getType()->getAs<RecordType>()) {
822 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
823 if (construct && !RD->hasTrivialConstructor() ||
824 !construct && !RD->hasTrivialDestructor())
825 Ivars.push_back(*I);
826 }
827 }
828
829 // Find ivars to construct/destruct in class extension.
830 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
831 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
832 E = CDecl->ivar_end(); I != E; ++I) {
833 ObjCIvarDecl *Iv = (*I);
834 if (const RecordType *RT = Iv->getType()->getAs<RecordType>()) {
835 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
836 if (construct && !RD->hasTrivialConstructor() ||
837 !construct && !RD->hasTrivialDestructor())
838 Ivars.push_back(*I);
839 }
840 }
841 }
842
843 // Also add any ivar defined in this class's implementation. This
844 // includes synthesized ivars.
845 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
846 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
847 E = ImplDecl->ivar_end(); I != E; ++I) {
848 ObjCIvarDecl *Iv = (*I);
849 if (const RecordType *RT = Iv->getType()->getAs<RecordType>()) {
850 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
851 if (construct && !RD->hasTrivialConstructor() ||
852 !construct && !RD->hasTrivialDestructor())
853 Ivars.push_back(*I);
854 }
855 }
856 }
857}
858
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000859unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
860 unsigned count = 0;
861 // Count ivars declared in class extension.
862 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
863 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
864 E = CDecl->ivar_end(); I != E; ++I) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000865 ++count;
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000866 }
867 }
868
869 // Count ivar defined in this class's implementation. This
870 // includes synthesized ivars.
871 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
872 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
873 E = ImplDecl->ivar_end(); I != E; ++I)
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000874 ++count;
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000875 return count;
876}
877
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000878/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
879ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
880 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
881 I = ObjCImpls.find(D);
882 if (I != ObjCImpls.end())
883 return cast<ObjCImplementationDecl>(I->second);
884 return 0;
885}
886/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
887ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
888 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
889 I = ObjCImpls.find(D);
890 if (I != ObjCImpls.end())
891 return cast<ObjCCategoryImplDecl>(I->second);
892 return 0;
893}
894
895/// \brief Set the implementation of ObjCInterfaceDecl.
896void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
897 ObjCImplementationDecl *ImplD) {
898 assert(IFaceD && ImplD && "Passed null params");
899 ObjCImpls[IFaceD] = ImplD;
900}
901/// \brief Set the implementation of ObjCCategoryDecl.
902void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
903 ObjCCategoryImplDecl *ImplD) {
904 assert(CatD && ImplD && "Passed null params");
905 ObjCImpls[CatD] = ImplD;
906}
907
John McCallbcd03502009-12-07 02:54:59 +0000908/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000909///
John McCallbcd03502009-12-07 02:54:59 +0000910/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000911/// the TypeLoc wrappers.
912///
913/// \param T the type that will be the basis for type source info. This type
914/// should refer to how the declarator was written in source code, not to
915/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000916TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000917 unsigned DataSize) {
918 if (!DataSize)
919 DataSize = TypeLoc::getFullDataSizeForType(T);
920 else
921 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000922 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000923
John McCallbcd03502009-12-07 02:54:59 +0000924 TypeSourceInfo *TInfo =
925 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
926 new (TInfo) TypeSourceInfo(T);
927 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000928}
929
John McCallbcd03502009-12-07 02:54:59 +0000930TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000931 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +0000932 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +0000933 DI->getTypeLoc().initialize(L);
934 return DI;
935}
936
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000937/// getInterfaceLayoutImpl - Get or compute information about the
938/// layout of the given interface.
939///
940/// \param Impl - If given, also include the layout of the interface's
941/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000942const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000943ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
944 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000945 assert(!D->isForwardDecl() && "Invalid interface decl!");
946
Devang Pateldbb72632008-06-04 21:54:36 +0000947 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000948 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000949 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
950 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
951 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000952
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000953 // Add in synthesized ivar count if laying out an implementation.
954 if (Impl) {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000955 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000956 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000957 // entry. Note we can't cache this because we simply free all
958 // entries later; however we shouldn't look up implementations
959 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000960 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000961 return getObjCLayout(D, 0);
962 }
963
Mike Stump11289f42009-09-09 15:08:12 +0000964 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000965 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
966 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000967
Devang Pateldbb72632008-06-04 21:54:36 +0000968 return *NewEntry;
969}
970
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000971const ASTRecordLayout &
972ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
973 return getObjCLayout(D, 0);
974}
975
976const ASTRecordLayout &
977ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
978 return getObjCLayout(D->getClassInterface(), D);
979}
980
Devang Patele11664a2007-11-01 19:11:01 +0000981/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +0000982/// specified record (struct/union/class), which indicates its size and field
983/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +0000984const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000985 D = D->getDefinition();
Ted Kremenek21475702008-09-05 17:16:31 +0000986 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +0000987
Chris Lattner53cfe802007-07-18 17:52:12 +0000988 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +0000989 // Note that we can't save a reference to the entry because this function
990 // is recursive.
991 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +0000992 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +0000993
Mike Stump11289f42009-09-09 15:08:12 +0000994 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000995 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +0000996 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000997
Daniel Dunbarccabe482010-04-19 20:44:53 +0000998 if (getLangOptions().DumpRecordLayouts) {
999 llvm::errs() << "\n*** Dumping AST Record Layout\n";
1000 DumpRecordLayout(D, llvm::errs());
1001 }
1002
Chris Lattner647fb222007-07-18 18:26:58 +00001003 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +00001004}
1005
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001006const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001007 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001008 assert(RD && "Cannot get key function for forward declarations!");
1009
1010 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1011 if (!Entry)
1012 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
1013 else
1014 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
1015 "Key function changed!");
1016
1017 return Entry;
1018}
1019
Chris Lattner983a8bb2007-07-13 22:13:22 +00001020//===----------------------------------------------------------------------===//
1021// Type creation/memoization methods
1022//===----------------------------------------------------------------------===//
1023
John McCall8ccfcb52009-09-24 19:53:00 +00001024QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1025 unsigned Fast = Quals.getFastQualifiers();
1026 Quals.removeFastQualifiers();
1027
1028 // Check if we've already instantiated this type.
1029 llvm::FoldingSetNodeID ID;
1030 ExtQuals::Profile(ID, TypeNode, Quals);
1031 void *InsertPos = 0;
1032 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1033 assert(EQ->getQualifiers() == Quals);
1034 QualType T = QualType(EQ, Fast);
1035 return T;
1036 }
1037
John McCall90d1c2d2009-09-24 23:30:46 +00001038 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001039 ExtQualNodes.InsertNode(New, InsertPos);
1040 QualType T = QualType(New, Fast);
1041 return T;
1042}
1043
1044QualType ASTContext::getVolatileType(QualType T) {
1045 QualType CanT = getCanonicalType(T);
1046 if (CanT.isVolatileQualified()) return T;
1047
1048 QualifierCollector Quals;
1049 const Type *TypeNode = Quals.strip(T);
1050 Quals.addVolatile();
1051
1052 return getExtQualType(TypeNode, Quals);
1053}
1054
Fariborz Jahanianece85822009-02-17 18:27:45 +00001055QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001056 QualType CanT = getCanonicalType(T);
1057 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001058 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001059
John McCall8ccfcb52009-09-24 19:53:00 +00001060 // If we are composing extended qualifiers together, merge together
1061 // into one ExtQuals node.
1062 QualifierCollector Quals;
1063 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001064
John McCall8ccfcb52009-09-24 19:53:00 +00001065 // If this type already has an address space specified, it cannot get
1066 // another one.
1067 assert(!Quals.hasAddressSpace() &&
1068 "Type cannot be in multiple addr spaces!");
1069 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001070
John McCall8ccfcb52009-09-24 19:53:00 +00001071 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001072}
1073
Chris Lattnerd60183d2009-02-18 22:53:11 +00001074QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001075 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001076 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001077 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001078 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001079
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001080 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001081 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001082 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001083 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1084 return getPointerType(ResultType);
1085 }
1086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
John McCall8ccfcb52009-09-24 19:53:00 +00001088 // If we are composing extended qualifiers together, merge together
1089 // into one ExtQuals node.
1090 QualifierCollector Quals;
1091 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001092
John McCall8ccfcb52009-09-24 19:53:00 +00001093 // If this type already has an ObjCGC specified, it cannot get
1094 // another one.
1095 assert(!Quals.hasObjCGCAttr() &&
1096 "Type cannot have multiple ObjCGCs!");
1097 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001098
John McCall8ccfcb52009-09-24 19:53:00 +00001099 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001100}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001101
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001102static QualType getExtFunctionType(ASTContext& Context, QualType T,
1103 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001104 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001105 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1106 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001107 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001108 if (ResultType == Pointee)
1109 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001110
1111 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001112 } else if (const BlockPointerType *BlockPointer
1113 = T->getAs<BlockPointerType>()) {
1114 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001115 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001116 if (ResultType == Pointee)
1117 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001118
1119 ResultType = Context.getBlockPointerType(ResultType);
1120 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001121 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001122 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001123
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001124 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001125 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001126 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001127 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001128 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001129 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001130 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1131 FPT->getNumArgs(), FPT->isVariadic(),
1132 FPT->getTypeQuals(),
1133 FPT->hasExceptionSpec(),
1134 FPT->hasAnyExceptionSpec(),
1135 FPT->getNumExceptions(),
1136 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001137 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001138 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001139 } else
1140 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001141
1142 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1143}
1144
1145QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001146 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001147 return getExtFunctionType(*this, T,
1148 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001149}
1150
1151QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001152 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001153 return getExtFunctionType(*this, T,
1154 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001155}
1156
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001157QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1158 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1159 return getExtFunctionType(*this, T,
1160 Info.withRegParm(RegParm));
1161}
1162
Chris Lattnerc6395932007-06-22 20:56:16 +00001163/// getComplexType - Return the uniqued reference to the type for a complex
1164/// number with the specified element type.
1165QualType ASTContext::getComplexType(QualType T) {
1166 // Unique pointers, to guarantee there is only one pointer of a particular
1167 // structure.
1168 llvm::FoldingSetNodeID ID;
1169 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001170
Chris Lattnerc6395932007-06-22 20:56:16 +00001171 void *InsertPos = 0;
1172 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1173 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattnerc6395932007-06-22 20:56:16 +00001175 // If the pointee type isn't canonical, this won't be a canonical type either,
1176 // so fill in the canonical type field.
1177 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001178 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001179 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001180
Chris Lattnerc6395932007-06-22 20:56:16 +00001181 // Get the new insert position for the node we care about.
1182 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001183 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001184 }
John McCall90d1c2d2009-09-24 23:30:46 +00001185 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001186 Types.push_back(New);
1187 ComplexTypes.InsertNode(New, InsertPos);
1188 return QualType(New, 0);
1189}
1190
Chris Lattner970e54e2006-11-12 00:37:36 +00001191/// getPointerType - Return the uniqued reference to the type for a pointer to
1192/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001193QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001194 // Unique pointers, to guarantee there is only one pointer of a particular
1195 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001196 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001197 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattner67521df2007-01-27 01:29:36 +00001199 void *InsertPos = 0;
1200 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001201 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001202
Chris Lattner7ccecb92006-11-12 08:50:50 +00001203 // If the pointee type isn't canonical, this won't be a canonical type either,
1204 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001205 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001206 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001207 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattner67521df2007-01-27 01:29:36 +00001209 // Get the new insert position for the node we care about.
1210 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001211 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001212 }
John McCall90d1c2d2009-09-24 23:30:46 +00001213 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001214 Types.push_back(New);
1215 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001216 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001217}
1218
Mike Stump11289f42009-09-09 15:08:12 +00001219/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001220/// a pointer to the specified block.
1221QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001222 assert(T->isFunctionType() && "block of function types only");
1223 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001224 // structure.
1225 llvm::FoldingSetNodeID ID;
1226 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001227
Steve Naroffec33ed92008-08-27 16:04:49 +00001228 void *InsertPos = 0;
1229 if (BlockPointerType *PT =
1230 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1231 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001232
1233 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001234 // type either so fill in the canonical type field.
1235 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001236 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001237 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001238
Steve Naroffec33ed92008-08-27 16:04:49 +00001239 // Get the new insert position for the node we care about.
1240 BlockPointerType *NewIP =
1241 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001242 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001243 }
John McCall90d1c2d2009-09-24 23:30:46 +00001244 BlockPointerType *New
1245 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001246 Types.push_back(New);
1247 BlockPointerTypes.InsertNode(New, InsertPos);
1248 return QualType(New, 0);
1249}
1250
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001251/// getLValueReferenceType - Return the uniqued reference to the type for an
1252/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001253QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001254 // Unique pointers, to guarantee there is only one pointer of a particular
1255 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001256 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001257 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001258
1259 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001260 if (LValueReferenceType *RT =
1261 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001262 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001263
John McCallfc93cf92009-10-22 22:37:11 +00001264 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1265
Bill Wendling3708c182007-05-27 10:15:43 +00001266 // If the referencee type isn't canonical, this won't be a canonical type
1267 // either, so fill in the canonical type field.
1268 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001269 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1270 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1271 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001272
Bill Wendling3708c182007-05-27 10:15:43 +00001273 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001274 LValueReferenceType *NewIP =
1275 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001276 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001277 }
1278
John McCall90d1c2d2009-09-24 23:30:46 +00001279 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001280 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1281 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001282 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001283 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001284
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001285 return QualType(New, 0);
1286}
1287
1288/// getRValueReferenceType - Return the uniqued reference to the type for an
1289/// rvalue reference to the specified type.
1290QualType ASTContext::getRValueReferenceType(QualType T) {
1291 // Unique pointers, to guarantee there is only one pointer of a particular
1292 // structure.
1293 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001294 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001295
1296 void *InsertPos = 0;
1297 if (RValueReferenceType *RT =
1298 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1299 return QualType(RT, 0);
1300
John McCallfc93cf92009-10-22 22:37:11 +00001301 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1302
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001303 // If the referencee type isn't canonical, this won't be a canonical type
1304 // either, so fill in the canonical type field.
1305 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001306 if (InnerRef || !T.isCanonical()) {
1307 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1308 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001309
1310 // Get the new insert position for the node we care about.
1311 RValueReferenceType *NewIP =
1312 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1313 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1314 }
1315
John McCall90d1c2d2009-09-24 23:30:46 +00001316 RValueReferenceType *New
1317 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001318 Types.push_back(New);
1319 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001320 return QualType(New, 0);
1321}
1322
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001323/// getMemberPointerType - Return the uniqued reference to the type for a
1324/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001325QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001326 // Unique pointers, to guarantee there is only one pointer of a particular
1327 // structure.
1328 llvm::FoldingSetNodeID ID;
1329 MemberPointerType::Profile(ID, T, Cls);
1330
1331 void *InsertPos = 0;
1332 if (MemberPointerType *PT =
1333 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1334 return QualType(PT, 0);
1335
1336 // If the pointee or class type isn't canonical, this won't be a canonical
1337 // type either, so fill in the canonical type field.
1338 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001339 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001340 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1341
1342 // Get the new insert position for the node we care about.
1343 MemberPointerType *NewIP =
1344 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1345 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1346 }
John McCall90d1c2d2009-09-24 23:30:46 +00001347 MemberPointerType *New
1348 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001349 Types.push_back(New);
1350 MemberPointerTypes.InsertNode(New, InsertPos);
1351 return QualType(New, 0);
1352}
1353
Mike Stump11289f42009-09-09 15:08:12 +00001354/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001355/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001356QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001357 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001358 ArrayType::ArraySizeModifier ASM,
1359 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001360 assert((EltTy->isDependentType() ||
1361 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001362 "Constant array of VLAs is illegal!");
1363
Chris Lattnere2df3f92009-05-13 04:12:56 +00001364 // Convert the array size into a canonical width matching the pointer size for
1365 // the target.
1366 llvm::APInt ArySize(ArySizeIn);
1367 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001368
Chris Lattner23b7eb62007-06-15 23:05:46 +00001369 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001370 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattner36f8e652007-01-27 08:31:04 +00001372 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001373 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001374 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001375 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattner7ccecb92006-11-12 08:50:50 +00001377 // If the element type isn't canonical, this won't be a canonical type either,
1378 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001379 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001380 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001381 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001382 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001383 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001384 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001385 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001386 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001387 }
Mike Stump11289f42009-09-09 15:08:12 +00001388
John McCall90d1c2d2009-09-24 23:30:46 +00001389 ConstantArrayType *New = new(*this,TypeAlignment)
1390 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001391 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001392 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001393 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001394}
1395
Steve Naroffcadebd02007-08-30 18:14:25 +00001396/// getVariableArrayType - Returns a non-unique reference to the type for a
1397/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001398QualType ASTContext::getVariableArrayType(QualType EltTy,
1399 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001400 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001401 unsigned EltTypeQuals,
1402 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001403 // Since we don't unique expressions, it isn't possible to unique VLA's
1404 // that have an expression provided for their size.
1405
John McCall90d1c2d2009-09-24 23:30:46 +00001406 VariableArrayType *New = new(*this, TypeAlignment)
1407 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001408
1409 VariableArrayTypes.push_back(New);
1410 Types.push_back(New);
1411 return QualType(New, 0);
1412}
1413
Douglas Gregor4619e432008-12-05 23:32:09 +00001414/// getDependentSizedArrayType - Returns a non-unique reference to
1415/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001416/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001417QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1418 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001419 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001420 unsigned EltTypeQuals,
1421 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001422 assert((!NumElts || NumElts->isTypeDependent() ||
1423 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001424 "Size must be type- or value-dependent!");
1425
Douglas Gregorf3f95522009-07-31 00:23:35 +00001426 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001427 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001428 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001429
1430 if (NumElts) {
1431 // Dependently-sized array types that do not have a specified
1432 // number of elements will have their sizes deduced from an
1433 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001434 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1435 EltTypeQuals, NumElts);
1436
1437 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1438 }
1439
Douglas Gregorf3f95522009-07-31 00:23:35 +00001440 DependentSizedArrayType *New;
1441 if (Canon) {
1442 // We already have a canonical version of this array type; use it as
1443 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001444 New = new (*this, TypeAlignment)
1445 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1446 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001447 } else {
1448 QualType CanonEltTy = getCanonicalType(EltTy);
1449 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001450 New = new (*this, TypeAlignment)
1451 DependentSizedArrayType(*this, EltTy, QualType(),
1452 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001453
Douglas Gregorc42075a2010-02-04 18:10:26 +00001454 if (NumElts) {
1455 DependentSizedArrayType *CanonCheck
1456 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1457 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1458 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001459 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001460 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001461 } else {
1462 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1463 ASM, EltTypeQuals,
1464 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001465 New = new (*this, TypeAlignment)
1466 DependentSizedArrayType(*this, EltTy, Canon,
1467 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001468 }
1469 }
Mike Stump11289f42009-09-09 15:08:12 +00001470
Douglas Gregor4619e432008-12-05 23:32:09 +00001471 Types.push_back(New);
1472 return QualType(New, 0);
1473}
1474
Eli Friedmanbd258282008-02-15 18:16:39 +00001475QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1476 ArrayType::ArraySizeModifier ASM,
1477 unsigned EltTypeQuals) {
1478 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001479 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001480
1481 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001482 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001483 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1484 return QualType(ATP, 0);
1485
1486 // If the element type isn't canonical, this won't be a canonical type
1487 // either, so fill in the canonical type field.
1488 QualType Canonical;
1489
John McCallb692a092009-10-22 20:10:53 +00001490 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001491 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001492 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001493
1494 // Get the new insert position for the node we care about.
1495 IncompleteArrayType *NewIP =
1496 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001497 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001498 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001499
John McCall90d1c2d2009-09-24 23:30:46 +00001500 IncompleteArrayType *New = new (*this, TypeAlignment)
1501 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001502
1503 IncompleteArrayTypes.InsertNode(New, InsertPos);
1504 Types.push_back(New);
1505 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001506}
1507
Steve Naroff91fcddb2007-07-18 18:00:27 +00001508/// getVectorType - Return the unique reference to a vector type of
1509/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001510QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1511 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001512 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001513
Chris Lattner76a00cf2008-04-06 22:59:24 +00001514 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001515 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001516
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001517 // Check if we've already instantiated a vector of this type.
1518 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001519 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1520 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001521 void *InsertPos = 0;
1522 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1523 return QualType(VTP, 0);
1524
1525 // If the element type isn't canonical, this won't be a canonical type either,
1526 // so fill in the canonical type field.
1527 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001528 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1529 Canonical = getVectorType(getCanonicalType(vecType),
1530 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001531
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001532 // Get the new insert position for the node we care about.
1533 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001534 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001535 }
John McCall90d1c2d2009-09-24 23:30:46 +00001536 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001537 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001538 VectorTypes.InsertNode(New, InsertPos);
1539 Types.push_back(New);
1540 return QualType(New, 0);
1541}
1542
Nate Begemance4d7fc2008-04-18 23:10:10 +00001543/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001544/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001545QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001546 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001547
Chris Lattner76a00cf2008-04-06 22:59:24 +00001548 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001549 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001550
Steve Naroff91fcddb2007-07-18 18:00:27 +00001551 // Check if we've already instantiated a vector of this type.
1552 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001553 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001554 void *InsertPos = 0;
1555 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1556 return QualType(VTP, 0);
1557
1558 // If the element type isn't canonical, this won't be a canonical type either,
1559 // so fill in the canonical type field.
1560 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001561 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001562 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001563
Steve Naroff91fcddb2007-07-18 18:00:27 +00001564 // Get the new insert position for the node we care about.
1565 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001566 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001567 }
John McCall90d1c2d2009-09-24 23:30:46 +00001568 ExtVectorType *New = new (*this, TypeAlignment)
1569 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001570 VectorTypes.InsertNode(New, InsertPos);
1571 Types.push_back(New);
1572 return QualType(New, 0);
1573}
1574
Mike Stump11289f42009-09-09 15:08:12 +00001575QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001576 Expr *SizeExpr,
1577 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001578 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001579 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001580 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001581
Douglas Gregor352169a2009-07-31 03:54:25 +00001582 void *InsertPos = 0;
1583 DependentSizedExtVectorType *Canon
1584 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1585 DependentSizedExtVectorType *New;
1586 if (Canon) {
1587 // We already have a canonical version of this array type; use it as
1588 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001589 New = new (*this, TypeAlignment)
1590 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1591 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001592 } else {
1593 QualType CanonVecTy = getCanonicalType(vecType);
1594 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001595 New = new (*this, TypeAlignment)
1596 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1597 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001598
1599 DependentSizedExtVectorType *CanonCheck
1600 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1601 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1602 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001603 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1604 } else {
1605 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1606 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001607 New = new (*this, TypeAlignment)
1608 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001609 }
1610 }
Mike Stump11289f42009-09-09 15:08:12 +00001611
Douglas Gregor758a8692009-06-17 21:51:59 +00001612 Types.push_back(New);
1613 return QualType(New, 0);
1614}
1615
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001616/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001617///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001618QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1619 const FunctionType::ExtInfo &Info) {
1620 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001621 // Unique functions, to guarantee there is only one function of a particular
1622 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001623 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001624 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001625
Chris Lattner47955de2007-01-27 08:37:20 +00001626 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001627 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001628 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001629 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001630
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001631 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001632 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001633 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001634 Canonical =
1635 getFunctionNoProtoType(getCanonicalType(ResultTy),
1636 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001637
Chris Lattner47955de2007-01-27 08:37:20 +00001638 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001639 FunctionNoProtoType *NewIP =
1640 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001641 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001642 }
Mike Stump11289f42009-09-09 15:08:12 +00001643
John McCall90d1c2d2009-09-24 23:30:46 +00001644 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001645 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001646 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001647 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001648 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001649}
1650
1651/// getFunctionType - Return a normal function type with a typed argument
1652/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001653QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001654 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001655 unsigned TypeQuals, bool hasExceptionSpec,
1656 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001657 const QualType *ExArray,
1658 const FunctionType::ExtInfo &Info) {
1659 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001660 // Unique functions, to guarantee there is only one function of a particular
1661 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001662 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001663 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001664 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001665 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001666
1667 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001668 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001669 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001670 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001671
1672 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001673 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001674 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001675 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001676 isCanonical = false;
1677
1678 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001679 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001680 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001681 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001682 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001683 CanonicalArgs.reserve(NumArgs);
1684 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001685 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001686
Chris Lattner76a00cf2008-04-06 22:59:24 +00001687 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001688 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001689 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001690 false, 0, 0,
1691 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001692
Chris Lattnerfd4de792007-01-27 01:15:32 +00001693 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001694 FunctionProtoType *NewIP =
1695 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001696 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001697 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001698
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001699 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001700 // for two variable size arrays (for parameter and exception types) at the
1701 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001702 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001703 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1704 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001705 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001706 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001707 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001708 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001709 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001710 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001711 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001712}
Chris Lattneref51c202006-11-10 07:17:23 +00001713
John McCalle78aac42010-03-10 03:28:59 +00001714#ifndef NDEBUG
1715static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1716 if (!isa<CXXRecordDecl>(D)) return false;
1717 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1718 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1719 return true;
1720 if (RD->getDescribedClassTemplate() &&
1721 !isa<ClassTemplateSpecializationDecl>(RD))
1722 return true;
1723 return false;
1724}
1725#endif
1726
1727/// getInjectedClassNameType - Return the unique reference to the
1728/// injected class name type for the specified templated declaration.
1729QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1730 QualType TST) {
1731 assert(NeedsInjectedClassNameType(Decl));
1732 if (Decl->TypeForDecl) {
1733 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1734 } else if (CXXRecordDecl *PrevDecl
1735 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1736 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1737 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1738 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1739 } else {
John McCall2408e322010-04-27 00:57:59 +00001740 Decl->TypeForDecl =
1741 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001742 Types.push_back(Decl->TypeForDecl);
1743 }
1744 return QualType(Decl->TypeForDecl, 0);
1745}
1746
Douglas Gregor83a586e2008-04-13 21:07:44 +00001747/// getTypeDeclType - Return the unique reference to the type for the
1748/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001749QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001750 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001751 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001752
John McCall81e38502010-02-16 03:57:14 +00001753 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001754 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001755
1756 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001757 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001758 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001759
John McCall96f0b5f2010-03-10 06:48:02 +00001760 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1761 "Template type parameter types are always available.");
1762
John McCall81e38502010-02-16 03:57:14 +00001763 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001764 assert(!Record->getPreviousDeclaration() &&
1765 "struct/union has previous declaration");
1766 assert(!NeedsInjectedClassNameType(Record));
1767 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001768 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001769 assert(!Enum->getPreviousDeclaration() &&
1770 "enum has previous declaration");
1771 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001772 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001773 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1774 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001775 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001776 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001777
John McCall96f0b5f2010-03-10 06:48:02 +00001778 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001779 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001780}
1781
Chris Lattner32d920b2007-01-26 02:01:53 +00001782/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001783/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001784QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001785 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001786
Chris Lattner76a00cf2008-04-06 22:59:24 +00001787 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001788 Decl->TypeForDecl = new(*this, TypeAlignment)
1789 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001790 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001791 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001792}
1793
John McCallcebee162009-10-18 09:09:24 +00001794/// \brief Retrieve a substitution-result type.
1795QualType
1796ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1797 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001798 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001799 && "replacement types must always be canonical");
1800
1801 llvm::FoldingSetNodeID ID;
1802 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1803 void *InsertPos = 0;
1804 SubstTemplateTypeParmType *SubstParm
1805 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1806
1807 if (!SubstParm) {
1808 SubstParm = new (*this, TypeAlignment)
1809 SubstTemplateTypeParmType(Parm, Replacement);
1810 Types.push_back(SubstParm);
1811 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1812 }
1813
1814 return QualType(SubstParm, 0);
1815}
1816
Douglas Gregoreff93e02009-02-05 23:33:38 +00001817/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001818/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001819/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001820QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001821 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001822 IdentifierInfo *Name) {
1823 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001824 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001825 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001826 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001827 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1828
1829 if (TypeParm)
1830 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001831
Anders Carlsson90036dc2009-06-16 00:30:48 +00001832 if (Name) {
1833 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001834 TypeParm = new (*this, TypeAlignment)
1835 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001836
1837 TemplateTypeParmType *TypeCheck
1838 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1839 assert(!TypeCheck && "Template type parameter canonical type broken");
1840 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001841 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001842 TypeParm = new (*this, TypeAlignment)
1843 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001844
1845 Types.push_back(TypeParm);
1846 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1847
1848 return QualType(TypeParm, 0);
1849}
1850
John McCalle78aac42010-03-10 03:28:59 +00001851TypeSourceInfo *
1852ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1853 SourceLocation NameLoc,
1854 const TemplateArgumentListInfo &Args,
1855 QualType CanonType) {
1856 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1857
1858 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1859 TemplateSpecializationTypeLoc TL
1860 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1861 TL.setTemplateNameLoc(NameLoc);
1862 TL.setLAngleLoc(Args.getLAngleLoc());
1863 TL.setRAngleLoc(Args.getRAngleLoc());
1864 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1865 TL.setArgLocInfo(i, Args[i].getLocInfo());
1866 return DI;
1867}
1868
Mike Stump11289f42009-09-09 15:08:12 +00001869QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001870ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001871 const TemplateArgumentListInfo &Args,
John McCall2408e322010-04-27 00:57:59 +00001872 QualType Canon,
1873 bool IsCurrentInstantiation) {
John McCall6b51f282009-11-23 01:53:49 +00001874 unsigned NumArgs = Args.size();
1875
John McCall0ad16662009-10-29 08:12:44 +00001876 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1877 ArgVec.reserve(NumArgs);
1878 for (unsigned i = 0; i != NumArgs; ++i)
1879 ArgVec.push_back(Args[i].getArgument());
1880
John McCall2408e322010-04-27 00:57:59 +00001881 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
1882 Canon, IsCurrentInstantiation);
John McCall0ad16662009-10-29 08:12:44 +00001883}
1884
1885QualType
1886ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001887 const TemplateArgument *Args,
1888 unsigned NumArgs,
John McCall2408e322010-04-27 00:57:59 +00001889 QualType Canon,
1890 bool IsCurrentInstantiation) {
Douglas Gregor15301382009-07-30 17:40:51 +00001891 if (!Canon.isNull())
1892 Canon = getCanonicalType(Canon);
1893 else {
John McCall2408e322010-04-27 00:57:59 +00001894 assert(!IsCurrentInstantiation &&
1895 "current-instantiation specializations should always "
1896 "have a canonical type");
1897
Douglas Gregor15301382009-07-30 17:40:51 +00001898 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001899 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1900 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1901 CanonArgs.reserve(NumArgs);
1902 for (unsigned I = 0; I != NumArgs; ++I)
1903 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1904
1905 // Determine whether this canonical template specialization type already
1906 // exists.
1907 llvm::FoldingSetNodeID ID;
John McCall2408e322010-04-27 00:57:59 +00001908 TemplateSpecializationType::Profile(ID, CanonTemplate, false,
Douglas Gregor00044172009-07-29 16:09:57 +00001909 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001910
1911 void *InsertPos = 0;
1912 TemplateSpecializationType *Spec
1913 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001914
Douglas Gregora8e02e72009-07-28 23:00:59 +00001915 if (!Spec) {
1916 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001917 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001918 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001919 TypeAlignment);
John McCall2408e322010-04-27 00:57:59 +00001920 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001921 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001922 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001923 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001924 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001925 }
Mike Stump11289f42009-09-09 15:08:12 +00001926
Douglas Gregor15301382009-07-30 17:40:51 +00001927 if (Canon.isNull())
1928 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001929 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001930 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001931 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001932
Douglas Gregora8e02e72009-07-28 23:00:59 +00001933 // Allocate the (non-canonical) template specialization type, but don't
1934 // try to unique it: these types typically have location information that
1935 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001936 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001937 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001938 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001939 TemplateSpecializationType *Spec
John McCall2408e322010-04-27 00:57:59 +00001940 = new (Mem) TemplateSpecializationType(*this, Template,
1941 IsCurrentInstantiation,
1942 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001943 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001944
Douglas Gregor8bf42052009-02-09 18:46:07 +00001945 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001946 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001947}
1948
Mike Stump11289f42009-09-09 15:08:12 +00001949QualType
Douglas Gregorf21eb492009-03-26 23:50:42 +00001950ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor52537682009-03-19 00:18:19 +00001951 QualType NamedType) {
1952 llvm::FoldingSetNodeID ID;
Douglas Gregorf21eb492009-03-26 23:50:42 +00001953 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001954
1955 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001956 QualifiedNameType *T
Douglas Gregor52537682009-03-19 00:18:19 +00001957 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1958 if (T)
1959 return QualType(T, 0);
1960
Douglas Gregorc42075a2010-02-04 18:10:26 +00001961 QualType Canon = NamedType;
1962 if (!Canon.isCanonical()) {
1963 Canon = getCanonicalType(NamedType);
1964 QualifiedNameType *CheckT
1965 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1966 assert(!CheckT && "Qualified name canonical type broken");
1967 (void)CheckT;
1968 }
1969
1970 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001971 Types.push_back(T);
1972 QualifiedNameTypes.InsertNode(T, InsertPos);
1973 return QualType(T, 0);
1974}
1975
Douglas Gregor02085352010-03-31 20:19:30 +00001976QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1977 NestedNameSpecifier *NNS,
1978 const IdentifierInfo *Name,
1979 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001980 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1981
1982 if (Canon.isNull()) {
1983 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001984 ElaboratedTypeKeyword CanonKeyword = Keyword;
1985 if (Keyword == ETK_None)
1986 CanonKeyword = ETK_Typename;
1987
1988 if (CanonNNS != NNS || CanonKeyword != Keyword)
1989 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001990 }
1991
1992 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001993 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001994
1995 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001996 DependentNameType *T
1997 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001998 if (T)
1999 return QualType(T, 0);
2000
Douglas Gregor02085352010-03-31 20:19:30 +00002001 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00002002 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002003 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002004 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00002005}
2006
Mike Stump11289f42009-09-09 15:08:12 +00002007QualType
Douglas Gregor02085352010-03-31 20:19:30 +00002008ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2009 NestedNameSpecifier *NNS,
2010 const TemplateSpecializationType *TemplateId,
2011 QualType Canon) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002012 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2013
Douglas Gregorc42075a2010-02-04 18:10:26 +00002014 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002015 DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002016
2017 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002018 DependentNameType *T
2019 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002020 if (T)
2021 return QualType(T, 0);
2022
Douglas Gregordce2b622009-04-01 00:28:59 +00002023 if (Canon.isNull()) {
2024 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2025 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
Douglas Gregor02085352010-03-31 20:19:30 +00002026 ElaboratedTypeKeyword CanonKeyword = Keyword;
2027 if (Keyword == ETK_None)
2028 CanonKeyword = ETK_Typename;
2029 if (CanonNNS != NNS || CanonKeyword != Keyword ||
2030 CanonType != QualType(TemplateId, 0)) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002031 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00002032 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00002033 assert(CanonTemplateId &&
2034 "Canonical type must also be a template specialization type");
Douglas Gregor02085352010-03-31 20:19:30 +00002035 Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
Douglas Gregordce2b622009-04-01 00:28:59 +00002036 }
Douglas Gregorc42075a2010-02-04 18:10:26 +00002037
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002038 DependentNameType *CheckT
2039 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002040 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregordce2b622009-04-01 00:28:59 +00002041 }
2042
Douglas Gregor02085352010-03-31 20:19:30 +00002043 T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002044 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002045 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002046 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002047}
2048
John McCallfcc33b02009-09-05 00:15:47 +00002049QualType
2050ASTContext::getElaboratedType(QualType UnderlyingType,
2051 ElaboratedType::TagKind Tag) {
2052 llvm::FoldingSetNodeID ID;
2053 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump11289f42009-09-09 15:08:12 +00002054
John McCallfcc33b02009-09-05 00:15:47 +00002055 void *InsertPos = 0;
2056 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2057 if (T)
2058 return QualType(T, 0);
2059
Douglas Gregorc42075a2010-02-04 18:10:26 +00002060 QualType Canon = UnderlyingType;
2061 if (!Canon.isCanonical()) {
2062 Canon = getCanonicalType(Canon);
2063 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2064 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2065 }
John McCallfcc33b02009-09-05 00:15:47 +00002066
2067 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2068 Types.push_back(T);
2069 ElaboratedTypes.InsertNode(T, InsertPos);
2070 return QualType(T, 0);
2071}
2072
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002073/// CmpProtocolNames - Comparison predicate for sorting protocols
2074/// alphabetically.
2075static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2076 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002077 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002078}
2079
John McCallfc93cf92009-10-22 22:37:11 +00002080static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2081 unsigned NumProtocols) {
2082 if (NumProtocols == 0) return true;
2083
2084 for (unsigned i = 1; i != NumProtocols; ++i)
2085 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2086 return false;
2087 return true;
2088}
2089
2090static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002091 unsigned &NumProtocols) {
2092 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002093
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002094 // Sort protocols, keyed by name.
2095 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2096
2097 // Remove duplicates.
2098 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2099 NumProtocols = ProtocolsEnd-Protocols;
2100}
2101
Steve Narofffb4330f2009-06-17 22:40:22 +00002102/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2103/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002104QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00002105 ObjCProtocolDecl **Protocols,
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002106 unsigned NumProtocols,
2107 unsigned Quals) {
Steve Narofffb4330f2009-06-17 22:40:22 +00002108 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002109 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002110 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Narofffb4330f2009-06-17 22:40:22 +00002111
2112 void *InsertPos = 0;
2113 if (ObjCObjectPointerType *QT =
2114 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002115 return getQualifiedType(QualType(QT, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002116
John McCallfc93cf92009-10-22 22:37:11 +00002117 // Sort the protocol list alphabetically to canonicalize it.
2118 QualType Canonical;
2119 if (!InterfaceT.isCanonical() ||
2120 !areSortedAndUniqued(Protocols, NumProtocols)) {
2121 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002122 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2123 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002124 unsigned UniqueCount = NumProtocols;
2125
John McCallfc93cf92009-10-22 22:37:11 +00002126 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2127
2128 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2129 &Sorted[0], UniqueCount);
2130 } else {
2131 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2132 Protocols, NumProtocols);
2133 }
2134
2135 // Regenerate InsertPos.
2136 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2137 }
2138
Douglas Gregorf85bee62010-02-08 22:59:26 +00002139 // No match.
2140 unsigned Size = sizeof(ObjCObjectPointerType)
2141 + NumProtocols * sizeof(ObjCProtocolDecl *);
2142 void *Mem = Allocate(Size, TypeAlignment);
2143 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2144 InterfaceT,
2145 Protocols,
2146 NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002147
Steve Narofffb4330f2009-06-17 22:40:22 +00002148 Types.push_back(QType);
2149 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002150 return getQualifiedType(QualType(QType, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002151}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002152
Steve Naroffc277ad12009-07-18 15:33:26 +00002153/// getObjCInterfaceType - Return the unique reference to the type for the
2154/// specified ObjC interface decl. The list of protocols is optional.
2155QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002156 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002157 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00002158 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002159
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002160 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00002161 if (ObjCInterfaceType *QT =
2162 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002163 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002164
John McCallfc93cf92009-10-22 22:37:11 +00002165 // Sort the protocol list alphabetically to canonicalize it.
2166 QualType Canonical;
2167 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002168 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2169 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002170
2171 unsigned UniqueCount = NumProtocols;
2172 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2173
2174 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2175
2176 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2177 }
2178
Douglas Gregorf85bee62010-02-08 22:59:26 +00002179 unsigned Size = sizeof(ObjCInterfaceType)
2180 + NumProtocols * sizeof(ObjCProtocolDecl *);
2181 void *Mem = Allocate(Size, TypeAlignment);
2182 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2183 const_cast<ObjCInterfaceDecl*>(Decl),
2184 Protocols,
2185 NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002186
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002187 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00002188 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002189 return QualType(QType, 0);
2190}
2191
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002192/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2193/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002194/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002195/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002196/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002197QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002198 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002199 if (tofExpr->isTypeDependent()) {
2200 llvm::FoldingSetNodeID ID;
2201 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002202
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002203 void *InsertPos = 0;
2204 DependentTypeOfExprType *Canon
2205 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2206 if (Canon) {
2207 // We already have a "canonical" version of an identical, dependent
2208 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002209 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002210 QualType((TypeOfExprType*)Canon, 0));
2211 }
2212 else {
2213 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002214 Canon
2215 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002216 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2217 toe = Canon;
2218 }
2219 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002220 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002221 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002222 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002223 Types.push_back(toe);
2224 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002225}
2226
Steve Naroffa773cd52007-08-01 18:02:17 +00002227/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2228/// TypeOfType AST's. The only motivation to unique these nodes would be
2229/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002230/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002231/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002232QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002233 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002234 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002235 Types.push_back(tot);
2236 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002237}
2238
Anders Carlssonad6bd352009-06-24 21:24:56 +00002239/// getDecltypeForExpr - Given an expr, will return the decltype for that
2240/// expression, according to the rules in C++0x [dcl.type.simple]p4
2241static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002242 if (e->isTypeDependent())
2243 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002244
Anders Carlssonad6bd352009-06-24 21:24:56 +00002245 // If e is an id expression or a class member access, decltype(e) is defined
2246 // as the type of the entity named by e.
2247 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2248 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2249 return VD->getType();
2250 }
2251 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2252 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2253 return FD->getType();
2254 }
2255 // If e is a function call or an invocation of an overloaded operator,
2256 // (parentheses around e are ignored), decltype(e) is defined as the
2257 // return type of that function.
2258 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2259 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002260
Anders Carlssonad6bd352009-06-24 21:24:56 +00002261 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002262
2263 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002264 // defined as T&, otherwise decltype(e) is defined as T.
2265 if (e->isLvalue(Context) == Expr::LV_Valid)
2266 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002267
Anders Carlssonad6bd352009-06-24 21:24:56 +00002268 return T;
2269}
2270
Anders Carlsson81df7b82009-06-24 19:06:50 +00002271/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2272/// DecltypeType AST's. The only motivation to unique these nodes would be
2273/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002274/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002275/// on canonical type's (which are always unique).
2276QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002277 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002278 if (e->isTypeDependent()) {
2279 llvm::FoldingSetNodeID ID;
2280 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002281
Douglas Gregora21f6c32009-07-30 23:36:40 +00002282 void *InsertPos = 0;
2283 DependentDecltypeType *Canon
2284 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2285 if (Canon) {
2286 // We already have a "canonical" version of an equivalent, dependent
2287 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002288 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002289 QualType((DecltypeType*)Canon, 0));
2290 }
2291 else {
2292 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002293 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002294 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2295 dt = Canon;
2296 }
2297 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002298 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002299 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002300 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002301 Types.push_back(dt);
2302 return QualType(dt, 0);
2303}
2304
Chris Lattnerfb072462007-01-23 05:45:31 +00002305/// getTagDeclType - Return the unique reference to the type for the
2306/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002307QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002308 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002309 // FIXME: What is the design on getTagDeclType when it requires casting
2310 // away const? mutable?
2311 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002312}
2313
Mike Stump11289f42009-09-09 15:08:12 +00002314/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2315/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2316/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002317CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002318 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002319}
Chris Lattnerfb072462007-01-23 05:45:31 +00002320
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002321/// getSignedWCharType - Return the type of "signed wchar_t".
2322/// Used when in C++, as a GCC extension.
2323QualType ASTContext::getSignedWCharType() const {
2324 // FIXME: derive from "Target" ?
2325 return WCharTy;
2326}
2327
2328/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2329/// Used when in C++, as a GCC extension.
2330QualType ASTContext::getUnsignedWCharType() const {
2331 // FIXME: derive from "Target" ?
2332 return UnsignedIntTy;
2333}
2334
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002335/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2336/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2337QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002338 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002339}
2340
Chris Lattnera21ad802008-04-02 05:18:44 +00002341//===----------------------------------------------------------------------===//
2342// Type Operators
2343//===----------------------------------------------------------------------===//
2344
John McCallfc93cf92009-10-22 22:37:11 +00002345CanQualType ASTContext::getCanonicalParamType(QualType T) {
2346 // Push qualifiers into arrays, and then discard any remaining
2347 // qualifiers.
2348 T = getCanonicalType(T);
2349 const Type *Ty = T.getTypePtr();
2350
2351 QualType Result;
2352 if (isa<ArrayType>(Ty)) {
2353 Result = getArrayDecayedType(QualType(Ty,0));
2354 } else if (isa<FunctionType>(Ty)) {
2355 Result = getPointerType(QualType(Ty, 0));
2356 } else {
2357 Result = QualType(Ty, 0);
2358 }
2359
2360 return CanQualType::CreateUnsafe(Result);
2361}
2362
Chris Lattnered0d0792008-04-06 22:41:35 +00002363/// getCanonicalType - Return the canonical (structural) type corresponding to
2364/// the specified potentially non-canonical type. The non-canonical version
2365/// of a type may have many "decorated" versions of types. Decorators can
2366/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2367/// to be free of any of these, allowing two canonical types to be compared
2368/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002369CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002370 QualifierCollector Quals;
2371 const Type *Ptr = Quals.strip(T);
2372 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002373
John McCall8ccfcb52009-09-24 19:53:00 +00002374 // The canonical internal type will be the canonical type *except*
2375 // that we push type qualifiers down through array types.
2376
2377 // If there are no new qualifiers to push down, stop here.
2378 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002379 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002380
John McCall8ccfcb52009-09-24 19:53:00 +00002381 // If the type qualifiers are on an array type, get the canonical
2382 // type of the array with the qualifiers applied to the element
2383 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002384 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2385 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002386 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002387
Chris Lattner7adf0762008-08-04 07:31:14 +00002388 // Get the canonical version of the element with the extra qualifiers on it.
2389 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002390 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002391 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002392
Chris Lattner7adf0762008-08-04 07:31:14 +00002393 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002394 return CanQualType::CreateUnsafe(
2395 getConstantArrayType(NewEltTy, CAT->getSize(),
2396 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002397 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002398 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002399 return CanQualType::CreateUnsafe(
2400 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002401 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002402
Douglas Gregor4619e432008-12-05 23:32:09 +00002403 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002404 return CanQualType::CreateUnsafe(
2405 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002406 DSAT->getSizeExpr() ?
2407 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002408 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002409 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002410 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002411
Chris Lattner7adf0762008-08-04 07:31:14 +00002412 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002413 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002414 VAT->getSizeExpr() ?
2415 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002416 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002417 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002418 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002419}
2420
Chandler Carruth607f38e2009-12-29 07:16:59 +00002421QualType ASTContext::getUnqualifiedArrayType(QualType T,
2422 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002423 Quals = T.getQualifiers();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002424 if (!isa<ArrayType>(T)) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002425 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002426 }
2427
Chandler Carruth607f38e2009-12-29 07:16:59 +00002428 const ArrayType *AT = cast<ArrayType>(T);
2429 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002430 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002431 if (Elt == UnqualElt)
2432 return T;
2433
2434 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2435 return getConstantArrayType(UnqualElt, CAT->getSize(),
2436 CAT->getSizeModifier(), 0);
2437 }
2438
2439 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2440 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2441 }
2442
2443 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2444 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2445 DSAT->getSizeModifier(), 0,
2446 SourceRange());
2447}
2448
John McCall847e2a12009-11-24 18:42:40 +00002449DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2450 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2451 return TD->getDeclName();
2452
2453 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2454 if (DTN->isIdentifier()) {
2455 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2456 } else {
2457 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2458 }
2459 }
2460
John McCalld28ae272009-12-02 08:04:21 +00002461 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2462 assert(Storage);
2463 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002464}
2465
Douglas Gregor6bc50582009-05-07 06:41:52 +00002466TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2467 // If this template name refers to a template, the canonical
2468 // template name merely stores the template itself.
2469 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002470 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002471
John McCalld28ae272009-12-02 08:04:21 +00002472 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002473
Douglas Gregor6bc50582009-05-07 06:41:52 +00002474 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2475 assert(DTN && "Non-dependent template names must refer to template decls.");
2476 return DTN->CanonicalTemplateName;
2477}
2478
Douglas Gregoradee3e32009-11-11 23:06:43 +00002479bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2480 X = getCanonicalTemplateName(X);
2481 Y = getCanonicalTemplateName(Y);
2482 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2483}
2484
Mike Stump11289f42009-09-09 15:08:12 +00002485TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002486ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2487 switch (Arg.getKind()) {
2488 case TemplateArgument::Null:
2489 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002490
Douglas Gregora8e02e72009-07-28 23:00:59 +00002491 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002492 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002493
Douglas Gregora8e02e72009-07-28 23:00:59 +00002494 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002495 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002496
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002497 case TemplateArgument::Template:
2498 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2499
Douglas Gregora8e02e72009-07-28 23:00:59 +00002500 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002501 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002502 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002503
Douglas Gregora8e02e72009-07-28 23:00:59 +00002504 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002505 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002506
Douglas Gregora8e02e72009-07-28 23:00:59 +00002507 case TemplateArgument::Pack: {
2508 // FIXME: Allocate in ASTContext
2509 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2510 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002511 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002512 AEnd = Arg.pack_end();
2513 A != AEnd; (void)++A, ++Idx)
2514 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002515
Douglas Gregora8e02e72009-07-28 23:00:59 +00002516 TemplateArgument Result;
2517 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2518 return Result;
2519 }
2520 }
2521
2522 // Silence GCC warning
2523 assert(false && "Unhandled template argument kind");
2524 return TemplateArgument();
2525}
2526
Douglas Gregor333489b2009-03-27 23:10:48 +00002527NestedNameSpecifier *
2528ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002529 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002530 return 0;
2531
2532 switch (NNS->getKind()) {
2533 case NestedNameSpecifier::Identifier:
2534 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002535 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002536 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2537 NNS->getAsIdentifier());
2538
2539 case NestedNameSpecifier::Namespace:
2540 // A namespace is canonical; build a nested-name-specifier with
2541 // this namespace and no prefix.
2542 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2543
2544 case NestedNameSpecifier::TypeSpec:
2545 case NestedNameSpecifier::TypeSpecWithTemplate: {
2546 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002547 return NestedNameSpecifier::Create(*this, 0,
2548 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002549 T.getTypePtr());
2550 }
2551
2552 case NestedNameSpecifier::Global:
2553 // The global specifier is canonical and unique.
2554 return NNS;
2555 }
2556
2557 // Required to silence a GCC warning
2558 return 0;
2559}
2560
Chris Lattner7adf0762008-08-04 07:31:14 +00002561
2562const ArrayType *ASTContext::getAsArrayType(QualType T) {
2563 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002564 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002565 // Handle the common positive case fast.
2566 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2567 return AT;
2568 }
Mike Stump11289f42009-09-09 15:08:12 +00002569
John McCall8ccfcb52009-09-24 19:53:00 +00002570 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002571 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002572 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002573 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002574
John McCall8ccfcb52009-09-24 19:53:00 +00002575 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002576 // implements C99 6.7.3p8: "If the specification of an array type includes
2577 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002578
Chris Lattner7adf0762008-08-04 07:31:14 +00002579 // If we get here, we either have type qualifiers on the type, or we have
2580 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002581 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002582
John McCall8ccfcb52009-09-24 19:53:00 +00002583 QualifierCollector Qs;
2584 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002585
Chris Lattner7adf0762008-08-04 07:31:14 +00002586 // If we have a simple case, just return now.
2587 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002588 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002589 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002590
Chris Lattner7adf0762008-08-04 07:31:14 +00002591 // Otherwise, we have an array and we have qualifiers on it. Push the
2592 // qualifiers into the array element type and return a new array type.
2593 // Get the canonical version of the element with the extra qualifiers on it.
2594 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002595 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002596
Chris Lattner7adf0762008-08-04 07:31:14 +00002597 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2598 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2599 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002600 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002601 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2602 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2603 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002604 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002605
Mike Stump11289f42009-09-09 15:08:12 +00002606 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002607 = dyn_cast<DependentSizedArrayType>(ATy))
2608 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002609 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002610 DSAT->getSizeExpr() ?
2611 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002612 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002613 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002614 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002615
Chris Lattner7adf0762008-08-04 07:31:14 +00002616 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002617 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002618 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002619 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002620 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002621 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002622 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002623}
2624
2625
Chris Lattnera21ad802008-04-02 05:18:44 +00002626/// getArrayDecayedType - Return the properly qualified result of decaying the
2627/// specified array type to a pointer. This operation is non-trivial when
2628/// handling typedefs etc. The canonical type of "T" must be an array type,
2629/// this returns a pointer to a properly qualified element of the array.
2630///
2631/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2632QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002633 // Get the element type with 'getAsArrayType' so that we don't lose any
2634 // typedefs in the element type of the array. This also handles propagation
2635 // of type qualifiers from the array type into the element type if present
2636 // (C99 6.7.3p8).
2637 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2638 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002639
Chris Lattner7adf0762008-08-04 07:31:14 +00002640 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002641
2642 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002643 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002644}
2645
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002646QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002647 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002648 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2649 QT = AT->getElementType();
2650 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002651}
2652
Anders Carlsson4bf82142009-09-25 01:23:32 +00002653QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2654 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002655
Anders Carlsson4bf82142009-09-25 01:23:32 +00002656 if (const ArrayType *AT = getAsArrayType(ElemTy))
2657 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002658
Anders Carlssone0808df2008-12-21 03:44:36 +00002659 return ElemTy;
2660}
2661
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002662/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002663uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002664ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2665 uint64_t ElementCount = 1;
2666 do {
2667 ElementCount *= CA->getSize().getZExtValue();
2668 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2669 } while (CA);
2670 return ElementCount;
2671}
2672
Steve Naroff0af91202007-04-27 21:51:21 +00002673/// getFloatingRank - Return a relative rank for floating point types.
2674/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002675static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002676 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002677 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002678
John McCall9dd450b2009-09-21 23:43:11 +00002679 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2680 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002681 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002682 case BuiltinType::Float: return FloatRank;
2683 case BuiltinType::Double: return DoubleRank;
2684 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002685 }
2686}
2687
Mike Stump11289f42009-09-09 15:08:12 +00002688/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2689/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002690/// 'typeDomain' is a real floating point or complex type.
2691/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002692QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2693 QualType Domain) const {
2694 FloatingRank EltRank = getFloatingRank(Size);
2695 if (Domain->isComplexType()) {
2696 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002697 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002698 case FloatRank: return FloatComplexTy;
2699 case DoubleRank: return DoubleComplexTy;
2700 case LongDoubleRank: return LongDoubleComplexTy;
2701 }
Steve Naroff0af91202007-04-27 21:51:21 +00002702 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002703
2704 assert(Domain->isRealFloatingType() && "Unknown domain!");
2705 switch (EltRank) {
2706 default: assert(0 && "getFloatingRank(): illegal value for rank");
2707 case FloatRank: return FloatTy;
2708 case DoubleRank: return DoubleTy;
2709 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002710 }
Steve Naroffe4718892007-04-27 18:30:00 +00002711}
2712
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002713/// getFloatingTypeOrder - Compare the rank of the two specified floating
2714/// point types, ignoring the domain of the type (i.e. 'double' ==
2715/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002716/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002717int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2718 FloatingRank LHSR = getFloatingRank(LHS);
2719 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002720
Chris Lattnerb90739d2008-04-06 23:38:49 +00002721 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002722 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002723 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002724 return 1;
2725 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002726}
2727
Chris Lattner76a00cf2008-04-06 22:59:24 +00002728/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2729/// routine will assert if passed a built-in type that isn't an integer or enum,
2730/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002731unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002732 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002733 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002734 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002735
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002736 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2737 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2738
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002739 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2740 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2741
2742 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2743 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2744
Chris Lattner76a00cf2008-04-06 22:59:24 +00002745 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002746 default: assert(0 && "getIntegerRank(): not a built-in integer");
2747 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002748 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002749 case BuiltinType::Char_S:
2750 case BuiltinType::Char_U:
2751 case BuiltinType::SChar:
2752 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002753 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002754 case BuiltinType::Short:
2755 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002756 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002757 case BuiltinType::Int:
2758 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002759 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002760 case BuiltinType::Long:
2761 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002762 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002763 case BuiltinType::LongLong:
2764 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002765 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002766 case BuiltinType::Int128:
2767 case BuiltinType::UInt128:
2768 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002769 }
2770}
2771
Eli Friedman629ffb92009-08-20 04:21:42 +00002772/// \brief Whether this is a promotable bitfield reference according
2773/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2774///
2775/// \returns the type this bit-field will promote to, or NULL if no
2776/// promotion occurs.
2777QualType ASTContext::isPromotableBitField(Expr *E) {
2778 FieldDecl *Field = E->getBitField();
2779 if (!Field)
2780 return QualType();
2781
2782 QualType FT = Field->getType();
2783
2784 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2785 uint64_t BitWidth = BitWidthAP.getZExtValue();
2786 uint64_t IntSize = getTypeSize(IntTy);
2787 // GCC extension compatibility: if the bit-field size is less than or equal
2788 // to the size of int, it gets promoted no matter what its type is.
2789 // For instance, unsigned long bf : 4 gets promoted to signed int.
2790 if (BitWidth < IntSize)
2791 return IntTy;
2792
2793 if (BitWidth == IntSize)
2794 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2795
2796 // Types bigger than int are not subject to promotions, and therefore act
2797 // like the base type.
2798 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2799 // is ridiculous.
2800 return QualType();
2801}
2802
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002803/// getPromotedIntegerType - Returns the type that Promotable will
2804/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2805/// integer type.
2806QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2807 assert(!Promotable.isNull());
2808 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002809 if (const EnumType *ET = Promotable->getAs<EnumType>())
2810 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002811 if (Promotable->isSignedIntegerType())
2812 return IntTy;
2813 uint64_t PromotableSize = getTypeSize(Promotable);
2814 uint64_t IntSize = getTypeSize(IntTy);
2815 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2816 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2817}
2818
Mike Stump11289f42009-09-09 15:08:12 +00002819/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002820/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002821/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002822int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002823 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2824 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002825 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002826
Chris Lattner76a00cf2008-04-06 22:59:24 +00002827 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2828 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002829
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002830 unsigned LHSRank = getIntegerRank(LHSC);
2831 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002832
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002833 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2834 if (LHSRank == RHSRank) return 0;
2835 return LHSRank > RHSRank ? 1 : -1;
2836 }
Mike Stump11289f42009-09-09 15:08:12 +00002837
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002838 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2839 if (LHSUnsigned) {
2840 // If the unsigned [LHS] type is larger, return it.
2841 if (LHSRank >= RHSRank)
2842 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002843
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002844 // If the signed type can represent all values of the unsigned type, it
2845 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002846 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002847 return -1;
2848 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002849
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002850 // If the unsigned [RHS] type is larger, return it.
2851 if (RHSRank >= LHSRank)
2852 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002853
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002854 // If the signed type can represent all values of the unsigned type, it
2855 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002856 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002857 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002858}
Anders Carlsson98f07902007-08-17 05:31:46 +00002859
Anders Carlsson6d417272009-11-14 21:45:58 +00002860static RecordDecl *
2861CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2862 SourceLocation L, IdentifierInfo *Id) {
2863 if (Ctx.getLangOptions().CPlusPlus)
2864 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2865 else
2866 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2867}
2868
Mike Stump11289f42009-09-09 15:08:12 +00002869// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002870QualType ASTContext::getCFConstantStringType() {
2871 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002872 CFConstantStringTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002873 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2874 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002875 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002876
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002877 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002878
Anders Carlsson98f07902007-08-17 05:31:46 +00002879 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002880 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002881 // int flags;
2882 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002883 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002884 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002885 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002886 FieldTypes[3] = LongTy;
2887
Anders Carlsson98f07902007-08-17 05:31:46 +00002888 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002889 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002890 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002891 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002892 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002893 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002894 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002895 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002896 }
2897
Douglas Gregord5058122010-02-11 01:19:42 +00002898 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002899 }
Mike Stump11289f42009-09-09 15:08:12 +00002900
Anders Carlsson98f07902007-08-17 05:31:46 +00002901 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002902}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002903
Douglas Gregor512b0772009-04-23 22:29:11 +00002904void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002905 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002906 assert(Rec && "Invalid CFConstantStringType");
2907 CFConstantStringTypeDecl = Rec->getDecl();
2908}
2909
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002910// getNSConstantStringType - Return the type used for constant NSStrings.
2911QualType ASTContext::getNSConstantStringType() {
2912 if (!NSConstantStringTypeDecl) {
2913 NSConstantStringTypeDecl =
2914 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2915 &Idents.get("__builtin_NSString"));
2916 NSConstantStringTypeDecl->startDefinition();
2917
2918 QualType FieldTypes[3];
2919
2920 // const int *isa;
2921 FieldTypes[0] = getPointerType(IntTy.withConst());
2922 // const char *str;
2923 FieldTypes[1] = getPointerType(CharTy.withConst());
2924 // unsigned int length;
2925 FieldTypes[2] = UnsignedIntTy;
2926
2927 // Create fields
2928 for (unsigned i = 0; i < 3; ++i) {
2929 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2930 SourceLocation(), 0,
2931 FieldTypes[i], /*TInfo=*/0,
2932 /*BitWidth=*/0,
2933 /*Mutable=*/false);
2934 NSConstantStringTypeDecl->addDecl(Field);
2935 }
2936
2937 NSConstantStringTypeDecl->completeDefinition();
2938 }
2939
2940 return getTagDeclType(NSConstantStringTypeDecl);
2941}
2942
2943void ASTContext::setNSConstantStringType(QualType T) {
2944 const RecordType *Rec = T->getAs<RecordType>();
2945 assert(Rec && "Invalid NSConstantStringType");
2946 NSConstantStringTypeDecl = Rec->getDecl();
2947}
2948
Mike Stump11289f42009-09-09 15:08:12 +00002949QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002950 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002951 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002952 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2953 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00002954 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002955
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002956 QualType FieldTypes[] = {
2957 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002958 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002959 getPointerType(UnsignedLongTy),
2960 getConstantArrayType(UnsignedLongTy,
2961 llvm::APInt(32, 5), ArrayType::Normal, 0)
2962 };
Mike Stump11289f42009-09-09 15:08:12 +00002963
Douglas Gregor91f84212008-12-11 16:49:14 +00002964 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002965 FieldDecl *Field = FieldDecl::Create(*this,
2966 ObjCFastEnumerationStateTypeDecl,
2967 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002968 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002969 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002970 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002971 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002972 }
Mike Stump11289f42009-09-09 15:08:12 +00002973
Douglas Gregord5058122010-02-11 01:19:42 +00002974 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002975 }
Mike Stump11289f42009-09-09 15:08:12 +00002976
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002977 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2978}
2979
Mike Stumpd0153282009-10-20 02:12:22 +00002980QualType ASTContext::getBlockDescriptorType() {
2981 if (BlockDescriptorType)
2982 return getTagDeclType(BlockDescriptorType);
2983
2984 RecordDecl *T;
2985 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002986 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2987 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00002988 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002989
2990 QualType FieldTypes[] = {
2991 UnsignedLongTy,
2992 UnsignedLongTy,
2993 };
2994
2995 const char *FieldNames[] = {
2996 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002997 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002998 };
2999
3000 for (size_t i = 0; i < 2; ++i) {
3001 FieldDecl *Field = FieldDecl::Create(*this,
3002 T,
3003 SourceLocation(),
3004 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003005 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00003006 /*BitWidth=*/0,
3007 /*Mutable=*/false);
3008 T->addDecl(Field);
3009 }
3010
Douglas Gregord5058122010-02-11 01:19:42 +00003011 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003012
3013 BlockDescriptorType = T;
3014
3015 return getTagDeclType(BlockDescriptorType);
3016}
3017
3018void ASTContext::setBlockDescriptorType(QualType T) {
3019 const RecordType *Rec = T->getAs<RecordType>();
3020 assert(Rec && "Invalid BlockDescriptorType");
3021 BlockDescriptorType = Rec->getDecl();
3022}
3023
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003024QualType ASTContext::getBlockDescriptorExtendedType() {
3025 if (BlockDescriptorExtendedType)
3026 return getTagDeclType(BlockDescriptorExtendedType);
3027
3028 RecordDecl *T;
3029 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00003030 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3031 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00003032 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003033
3034 QualType FieldTypes[] = {
3035 UnsignedLongTy,
3036 UnsignedLongTy,
3037 getPointerType(VoidPtrTy),
3038 getPointerType(VoidPtrTy)
3039 };
3040
3041 const char *FieldNames[] = {
3042 "reserved",
3043 "Size",
3044 "CopyFuncPtr",
3045 "DestroyFuncPtr"
3046 };
3047
3048 for (size_t i = 0; i < 4; ++i) {
3049 FieldDecl *Field = FieldDecl::Create(*this,
3050 T,
3051 SourceLocation(),
3052 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003053 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003054 /*BitWidth=*/0,
3055 /*Mutable=*/false);
3056 T->addDecl(Field);
3057 }
3058
Douglas Gregord5058122010-02-11 01:19:42 +00003059 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003060
3061 BlockDescriptorExtendedType = T;
3062
3063 return getTagDeclType(BlockDescriptorExtendedType);
3064}
3065
3066void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3067 const RecordType *Rec = T->getAs<RecordType>();
3068 assert(Rec && "Invalid BlockDescriptorType");
3069 BlockDescriptorExtendedType = Rec->getDecl();
3070}
3071
Mike Stump94967902009-10-21 18:16:27 +00003072bool ASTContext::BlockRequiresCopying(QualType Ty) {
3073 if (Ty->isBlockPointerType())
3074 return true;
3075 if (isObjCNSObjectType(Ty))
3076 return true;
3077 if (Ty->isObjCObjectPointerType())
3078 return true;
3079 return false;
3080}
3081
3082QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3083 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003084 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003085 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003086 // unsigned int __flags;
3087 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003088 // void *__copy_helper; // as needed
3089 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003090 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003091 // } *
3092
Mike Stump94967902009-10-21 18:16:27 +00003093 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3094
3095 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003096 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003097 llvm::SmallString<36> Name;
3098 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3099 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003100 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003101 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3102 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003103 T->startDefinition();
3104 QualType Int32Ty = IntTy;
3105 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3106 QualType FieldTypes[] = {
3107 getPointerType(VoidPtrTy),
3108 getPointerType(getTagDeclType(T)),
3109 Int32Ty,
3110 Int32Ty,
3111 getPointerType(VoidPtrTy),
3112 getPointerType(VoidPtrTy),
3113 Ty
3114 };
3115
3116 const char *FieldNames[] = {
3117 "__isa",
3118 "__forwarding",
3119 "__flags",
3120 "__size",
3121 "__copy_helper",
3122 "__destroy_helper",
3123 DeclName,
3124 };
3125
3126 for (size_t i = 0; i < 7; ++i) {
3127 if (!HasCopyAndDispose && i >=4 && i <= 5)
3128 continue;
3129 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3130 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003131 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003132 /*BitWidth=*/0, /*Mutable=*/false);
3133 T->addDecl(Field);
3134 }
3135
Douglas Gregord5058122010-02-11 01:19:42 +00003136 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003137
3138 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003139}
3140
3141
3142QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003143 bool BlockHasCopyDispose,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003144 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpd0153282009-10-20 02:12:22 +00003145 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003146 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003147 llvm::SmallString<36> Name;
3148 llvm::raw_svector_ostream(Name) << "__block_literal_"
3149 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003150 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003151 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3152 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003153 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003154 QualType FieldTypes[] = {
3155 getPointerType(VoidPtrTy),
3156 IntTy,
3157 IntTy,
3158 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003159 (BlockHasCopyDispose ?
3160 getPointerType(getBlockDescriptorExtendedType()) :
3161 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003162 };
3163
3164 const char *FieldNames[] = {
3165 "__isa",
3166 "__flags",
3167 "__reserved",
3168 "__FuncPtr",
3169 "__descriptor"
3170 };
3171
3172 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003173 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003174 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003175 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003176 /*BitWidth=*/0, /*Mutable=*/false);
3177 T->addDecl(Field);
3178 }
3179
3180 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3181 const Expr *E = BlockDeclRefDecls[i];
3182 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3183 clang::IdentifierInfo *Name = 0;
3184 if (BDRE) {
3185 const ValueDecl *D = BDRE->getDecl();
3186 Name = &Idents.get(D->getName());
3187 }
3188 QualType FieldType = E->getType();
3189
3190 if (BDRE && BDRE->isByRef())
Mike Stump94967902009-10-21 18:16:27 +00003191 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3192 FieldType);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003193
3194 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallbcd03502009-12-07 02:54:59 +00003195 Name, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003196 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpd0153282009-10-20 02:12:22 +00003197 T->addDecl(Field);
3198 }
3199
Douglas Gregord5058122010-02-11 01:19:42 +00003200 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003201
3202 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003203}
3204
Douglas Gregor512b0772009-04-23 22:29:11 +00003205void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003206 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003207 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3208 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3209}
3210
Anders Carlsson18acd442007-10-29 06:33:42 +00003211// This returns true if a type has been typedefed to BOOL:
3212// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003213static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003214 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003215 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3216 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003217
Anders Carlssond8499822007-10-29 05:01:08 +00003218 return false;
3219}
3220
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003221/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003222/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003223CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003224 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003225
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003226 // Make all integer and enum types at least as large as an int
Ken Dyck40775002010-01-11 17:06:35 +00003227 if (sz.isPositive() && type->isIntegralType())
3228 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003229 // Treat arrays as pointers, since that's how they're passed in.
3230 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003231 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003232 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003233}
3234
3235static inline
3236std::string charUnitsToString(const CharUnits &CU) {
3237 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003238}
3239
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003240/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003241/// declaration.
3242void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3243 std::string& S) {
3244 const BlockDecl *Decl = Expr->getBlockDecl();
3245 QualType BlockTy =
3246 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3247 // Encode result type.
3248 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3249 // Compute size of all parameters.
3250 // Start with computing size of a pointer in number of bytes.
3251 // FIXME: There might(should) be a better way of doing this computation!
3252 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003253 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3254 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003255 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003256 E = Decl->param_end(); PI != E; ++PI) {
3257 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003258 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003259 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003260 ParmOffset += sz;
3261 }
3262 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003263 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003264 // Block pointer and offset.
3265 S += "@?0";
3266 ParmOffset = PtrSize;
3267
3268 // Argument types.
3269 ParmOffset = PtrSize;
3270 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3271 Decl->param_end(); PI != E; ++PI) {
3272 ParmVarDecl *PVDecl = *PI;
3273 QualType PType = PVDecl->getOriginalType();
3274 if (const ArrayType *AT =
3275 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3276 // Use array's original type only if it has known number of
3277 // elements.
3278 if (!isa<ConstantArrayType>(AT))
3279 PType = PVDecl->getType();
3280 } else if (PType->isFunctionType())
3281 PType = PVDecl->getType();
3282 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003283 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003284 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003285 }
3286}
3287
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003288/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003289/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003290void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003291 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003292 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003293 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003294 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003295 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003296 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003297 // Compute size of all parameters.
3298 // Start with computing size of a pointer in number of bytes.
3299 // FIXME: There might(should) be a better way of doing this computation!
3300 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003301 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003302 // The first two arguments (self and _cmd) are pointers; account for
3303 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003304 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003305 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003306 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003307 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003308 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003309 assert (sz.isPositive() &&
3310 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003311 ParmOffset += sz;
3312 }
Ken Dyck40775002010-01-11 17:06:35 +00003313 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003314 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003315 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003316
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003317 // Argument types.
3318 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003319 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003320 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003321 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003322 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003323 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003324 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3325 // Use array's original type only if it has known number of
3326 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003327 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003328 PType = PVDecl->getType();
3329 } else if (PType->isFunctionType())
3330 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003331 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003332 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003333 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003334 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003335 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003336 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003337 }
3338}
3339
Daniel Dunbar4932b362008-08-28 04:38:10 +00003340/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003341/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003342/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3343/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003344/// Property attributes are stored as a comma-delimited C string. The simple
3345/// attributes readonly and bycopy are encoded as single characters. The
3346/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3347/// encoded as single characters, followed by an identifier. Property types
3348/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003349/// these attributes are defined by the following enumeration:
3350/// @code
3351/// enum PropertyAttributes {
3352/// kPropertyReadOnly = 'R', // property is read-only.
3353/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3354/// kPropertyByref = '&', // property is a reference to the value last assigned
3355/// kPropertyDynamic = 'D', // property is dynamic
3356/// kPropertyGetter = 'G', // followed by getter selector name
3357/// kPropertySetter = 'S', // followed by setter selector name
3358/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3359/// kPropertyType = 't' // followed by old-style type encoding.
3360/// kPropertyWeak = 'W' // 'weak' property
3361/// kPropertyStrong = 'P' // property GC'able
3362/// kPropertyNonAtomic = 'N' // property non-atomic
3363/// };
3364/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003365void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003366 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003367 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003368 // Collect information from the property implementation decl(s).
3369 bool Dynamic = false;
3370 ObjCPropertyImplDecl *SynthesizePID = 0;
3371
3372 // FIXME: Duplicated code due to poor abstraction.
3373 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003374 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003375 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3376 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003377 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003378 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003379 ObjCPropertyImplDecl *PID = *i;
3380 if (PID->getPropertyDecl() == PD) {
3381 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3382 Dynamic = true;
3383 } else {
3384 SynthesizePID = PID;
3385 }
3386 }
3387 }
3388 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003389 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003390 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003391 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003392 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003393 ObjCPropertyImplDecl *PID = *i;
3394 if (PID->getPropertyDecl() == PD) {
3395 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3396 Dynamic = true;
3397 } else {
3398 SynthesizePID = PID;
3399 }
3400 }
Mike Stump11289f42009-09-09 15:08:12 +00003401 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003402 }
3403 }
3404
3405 // FIXME: This is not very efficient.
3406 S = "T";
3407
3408 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003409 // GCC has some special rules regarding encoding of properties which
3410 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003411 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003412 true /* outermost type */,
3413 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003414
3415 if (PD->isReadOnly()) {
3416 S += ",R";
3417 } else {
3418 switch (PD->getSetterKind()) {
3419 case ObjCPropertyDecl::Assign: break;
3420 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003421 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003422 }
3423 }
3424
3425 // It really isn't clear at all what this means, since properties
3426 // are "dynamic by default".
3427 if (Dynamic)
3428 S += ",D";
3429
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003430 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3431 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003432
Daniel Dunbar4932b362008-08-28 04:38:10 +00003433 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3434 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003435 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003436 }
3437
3438 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3439 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003440 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003441 }
3442
3443 if (SynthesizePID) {
3444 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3445 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003446 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003447 }
3448
3449 // FIXME: OBJCGC: weak & strong
3450}
3451
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003452/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003453/// Another legacy compatibility encoding: 32-bit longs are encoded as
3454/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003455/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3456///
3457void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003458 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003459 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003460 if (BT->getKind() == BuiltinType::ULong &&
3461 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003462 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003463 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003464 if (BT->getKind() == BuiltinType::Long &&
3465 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003466 PointeeTy = IntTy;
3467 }
3468 }
3469}
3470
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003471void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003472 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003473 // We follow the behavior of gcc, expanding structures which are
3474 // directly pointed to, and expanding embedded structures. Note that
3475 // these rules are sufficient to prevent recursive encoding of the
3476 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003477 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003478 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003479}
3480
Mike Stump11289f42009-09-09 15:08:12 +00003481static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003482 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003483 const Expr *E = FD->getBitWidth();
3484 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3485 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00003486 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003487 S += 'b';
3488 S += llvm::utostr(N);
3489}
3490
Daniel Dunbar07d07852009-10-18 21:17:35 +00003491// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003492void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3493 bool ExpandPointedToStructures,
3494 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003495 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003496 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003497 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00003498 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003499 if (FD && FD->isBitField())
3500 return EncodeBitField(this, S, FD);
3501 char encoding;
3502 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003503 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00003504 case BuiltinType::Void: encoding = 'v'; break;
3505 case BuiltinType::Bool: encoding = 'B'; break;
3506 case BuiltinType::Char_U:
3507 case BuiltinType::UChar: encoding = 'C'; break;
3508 case BuiltinType::UShort: encoding = 'S'; break;
3509 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003510 case BuiltinType::ULong:
3511 encoding =
3512 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00003513 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003514 case BuiltinType::UInt128: encoding = 'T'; break;
3515 case BuiltinType::ULongLong: encoding = 'Q'; break;
3516 case BuiltinType::Char_S:
3517 case BuiltinType::SChar: encoding = 'c'; break;
3518 case BuiltinType::Short: encoding = 's'; break;
3519 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003520 case BuiltinType::Long:
3521 encoding =
3522 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003523 break;
3524 case BuiltinType::LongLong: encoding = 'q'; break;
3525 case BuiltinType::Int128: encoding = 't'; break;
3526 case BuiltinType::Float: encoding = 'f'; break;
3527 case BuiltinType::Double: encoding = 'd'; break;
3528 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003529 }
Mike Stump11289f42009-09-09 15:08:12 +00003530
Chris Lattnere7cabb92009-07-13 00:10:46 +00003531 S += encoding;
3532 return;
3533 }
Mike Stump11289f42009-09-09 15:08:12 +00003534
John McCall9dd450b2009-09-21 23:43:11 +00003535 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003536 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003537 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003538 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003539 return;
3540 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003541
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003542 // encoding for pointer or r3eference types.
3543 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003544 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003545 if (PT->isObjCSelType()) {
3546 S += ':';
3547 return;
3548 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003549 PointeeTy = PT->getPointeeType();
3550 }
3551 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3552 PointeeTy = RT->getPointeeType();
3553 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003554 bool isReadOnly = false;
3555 // For historical/compatibility reasons, the read-only qualifier of the
3556 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3557 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003558 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003559 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003560 if (OutermostType && T.isConstQualified()) {
3561 isReadOnly = true;
3562 S += 'r';
3563 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003564 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003565 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003566 while (P->getAs<PointerType>())
3567 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003568 if (P.isConstQualified()) {
3569 isReadOnly = true;
3570 S += 'r';
3571 }
3572 }
3573 if (isReadOnly) {
3574 // Another legacy compatibility encoding. Some ObjC qualifier and type
3575 // combinations need to be rearranged.
3576 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003577 if (llvm::StringRef(S).endswith("nr"))
3578 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003579 }
Mike Stump11289f42009-09-09 15:08:12 +00003580
Anders Carlssond8499822007-10-29 05:01:08 +00003581 if (PointeeTy->isCharType()) {
3582 // char pointer types should be encoded as '*' unless it is a
3583 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003584 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003585 S += '*';
3586 return;
3587 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003588 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003589 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3590 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3591 S += '#';
3592 return;
3593 }
3594 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3595 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3596 S += '@';
3597 return;
3598 }
3599 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003600 }
Anders Carlssond8499822007-10-29 05:01:08 +00003601 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003602 getLegacyIntegralTypeEncoding(PointeeTy);
3603
Mike Stump11289f42009-09-09 15:08:12 +00003604 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003605 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003606 return;
3607 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003608
Chris Lattnere7cabb92009-07-13 00:10:46 +00003609 if (const ArrayType *AT =
3610 // Ignore type qualifiers etc.
3611 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003612 if (isa<IncompleteArrayType>(AT)) {
3613 // Incomplete arrays are encoded as a pointer to the array element.
3614 S += '^';
3615
Mike Stump11289f42009-09-09 15:08:12 +00003616 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003617 false, ExpandStructures, FD);
3618 } else {
3619 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003620
Anders Carlssond05f44b2009-02-22 01:38:57 +00003621 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3622 S += llvm::utostr(CAT->getSize().getZExtValue());
3623 else {
3624 //Variable length arrays are encoded as a regular array with 0 elements.
3625 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3626 S += '0';
3627 }
Mike Stump11289f42009-09-09 15:08:12 +00003628
3629 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003630 false, ExpandStructures, FD);
3631 S += ']';
3632 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003633 return;
3634 }
Mike Stump11289f42009-09-09 15:08:12 +00003635
John McCall9dd450b2009-09-21 23:43:11 +00003636 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003637 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003638 return;
3639 }
Mike Stump11289f42009-09-09 15:08:12 +00003640
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003641 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003642 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003643 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003644 // Anonymous structures print as '?'
3645 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3646 S += II->getName();
3647 } else {
3648 S += '?';
3649 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003650 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003651 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003652 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3653 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003654 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003655 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003656 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003657 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003658 S += '"';
3659 }
Mike Stump11289f42009-09-09 15:08:12 +00003660
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003661 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003662 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003663 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003664 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003665 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003666 QualType qt = Field->getType();
3667 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003668 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003669 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003670 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003671 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003672 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003673 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003674 return;
3675 }
Mike Stump11289f42009-09-09 15:08:12 +00003676
Chris Lattnere7cabb92009-07-13 00:10:46 +00003677 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003678 if (FD && FD->isBitField())
3679 EncodeBitField(this, S, FD);
3680 else
3681 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003682 return;
3683 }
Mike Stump11289f42009-09-09 15:08:12 +00003684
Chris Lattnere7cabb92009-07-13 00:10:46 +00003685 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003686 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003687 return;
3688 }
Mike Stump11289f42009-09-09 15:08:12 +00003689
John McCall8ccfcb52009-09-24 19:53:00 +00003690 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003691 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003692 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003693 S += '{';
3694 const IdentifierInfo *II = OI->getIdentifier();
3695 S += II->getName();
3696 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003697 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003698 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003699 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003700 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003701 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003702 RecFields[i]);
3703 else
Mike Stump11289f42009-09-09 15:08:12 +00003704 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003705 FD);
3706 }
3707 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003708 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003709 }
Mike Stump11289f42009-09-09 15:08:12 +00003710
John McCall9dd450b2009-09-21 23:43:11 +00003711 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003712 if (OPT->isObjCIdType()) {
3713 S += '@';
3714 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003715 }
Mike Stump11289f42009-09-09 15:08:12 +00003716
Steve Narofff0c86112009-10-28 22:03:49 +00003717 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3718 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3719 // Since this is a binary compatibility issue, need to consult with runtime
3720 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003721 S += '#';
3722 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003723 }
Mike Stump11289f42009-09-09 15:08:12 +00003724
Chris Lattnere7cabb92009-07-13 00:10:46 +00003725 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003726 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003727 ExpandPointedToStructures,
3728 ExpandStructures, FD);
3729 if (FD || EncodingProperty) {
3730 // Note that we do extended encoding of protocol qualifer list
3731 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003732 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003733 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3734 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003735 S += '<';
3736 S += (*I)->getNameAsString();
3737 S += '>';
3738 }
3739 S += '"';
3740 }
3741 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003742 }
Mike Stump11289f42009-09-09 15:08:12 +00003743
Chris Lattnere7cabb92009-07-13 00:10:46 +00003744 QualType PointeeTy = OPT->getPointeeType();
3745 if (!EncodingProperty &&
3746 isa<TypedefType>(PointeeTy.getTypePtr())) {
3747 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003748 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003749 // {...};
3750 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003751 getObjCEncodingForTypeImpl(PointeeTy, S,
3752 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003753 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003754 return;
3755 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003756
3757 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003758 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003759 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003760 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003761 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3762 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003763 S += '<';
3764 S += (*I)->getNameAsString();
3765 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003766 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003767 S += '"';
3768 }
3769 return;
3770 }
Mike Stump11289f42009-09-09 15:08:12 +00003771
Chris Lattnere7cabb92009-07-13 00:10:46 +00003772 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003773}
3774
Mike Stump11289f42009-09-09 15:08:12 +00003775void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003776 std::string& S) const {
3777 if (QT & Decl::OBJC_TQ_In)
3778 S += 'n';
3779 if (QT & Decl::OBJC_TQ_Inout)
3780 S += 'N';
3781 if (QT & Decl::OBJC_TQ_Out)
3782 S += 'o';
3783 if (QT & Decl::OBJC_TQ_Bycopy)
3784 S += 'O';
3785 if (QT & Decl::OBJC_TQ_Byref)
3786 S += 'R';
3787 if (QT & Decl::OBJC_TQ_Oneway)
3788 S += 'V';
3789}
3790
Chris Lattnere7cabb92009-07-13 00:10:46 +00003791void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003792 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003793
Anders Carlsson87c149b2007-10-11 01:00:40 +00003794 BuiltinVaListType = T;
3795}
3796
Chris Lattnere7cabb92009-07-13 00:10:46 +00003797void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003798 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003799}
3800
Chris Lattnere7cabb92009-07-13 00:10:46 +00003801void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003802 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003803}
3804
Chris Lattnere7cabb92009-07-13 00:10:46 +00003805void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003806 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003807}
3808
Chris Lattnere7cabb92009-07-13 00:10:46 +00003809void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003810 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003811}
3812
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003813void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003814 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003815 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003816
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003817 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003818}
3819
John McCalld28ae272009-12-02 08:04:21 +00003820/// \brief Retrieve the template name that corresponds to a non-empty
3821/// lookup.
John McCallad371252010-01-20 00:46:10 +00003822TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3823 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003824 unsigned size = End - Begin;
3825 assert(size > 1 && "set is not overloaded!");
3826
3827 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3828 size * sizeof(FunctionTemplateDecl*));
3829 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3830
3831 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003832 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003833 NamedDecl *D = *I;
3834 assert(isa<FunctionTemplateDecl>(D) ||
3835 (isa<UsingShadowDecl>(D) &&
3836 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3837 *Storage++ = D;
3838 }
3839
3840 return TemplateName(OT);
3841}
3842
Douglas Gregordc572a32009-03-30 22:58:21 +00003843/// \brief Retrieve the template name that represents a qualified
3844/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003845TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003846 bool TemplateKeyword,
3847 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003848 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003849 llvm::FoldingSetNodeID ID;
3850 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3851
3852 void *InsertPos = 0;
3853 QualifiedTemplateName *QTN =
3854 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3855 if (!QTN) {
3856 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3857 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3858 }
3859
3860 return TemplateName(QTN);
3861}
3862
3863/// \brief Retrieve the template name that represents a dependent
3864/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003865TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003866 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003867 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003868 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003869
3870 llvm::FoldingSetNodeID ID;
3871 DependentTemplateName::Profile(ID, NNS, Name);
3872
3873 void *InsertPos = 0;
3874 DependentTemplateName *QTN =
3875 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3876
3877 if (QTN)
3878 return TemplateName(QTN);
3879
3880 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3881 if (CanonNNS == NNS) {
3882 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3883 } else {
3884 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3885 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003886 DependentTemplateName *CheckQTN =
3887 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3888 assert(!CheckQTN && "Dependent type name canonicalization broken");
3889 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00003890 }
3891
3892 DependentTemplateNames.InsertNode(QTN, InsertPos);
3893 return TemplateName(QTN);
3894}
3895
Douglas Gregor71395fa2009-11-04 00:56:37 +00003896/// \brief Retrieve the template name that represents a dependent
3897/// template name such as \c MetaFun::template operator+.
3898TemplateName
3899ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3900 OverloadedOperatorKind Operator) {
3901 assert((!NNS || NNS->isDependent()) &&
3902 "Nested name specifier must be dependent");
3903
3904 llvm::FoldingSetNodeID ID;
3905 DependentTemplateName::Profile(ID, NNS, Operator);
3906
3907 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00003908 DependentTemplateName *QTN
3909 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00003910
3911 if (QTN)
3912 return TemplateName(QTN);
3913
3914 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3915 if (CanonNNS == NNS) {
3916 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3917 } else {
3918 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3919 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003920
3921 DependentTemplateName *CheckQTN
3922 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3923 assert(!CheckQTN && "Dependent template name canonicalization broken");
3924 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00003925 }
3926
3927 DependentTemplateNames.InsertNode(QTN, InsertPos);
3928 return TemplateName(QTN);
3929}
3930
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003931/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003932/// TargetInfo, produce the corresponding type. The unsigned @p Type
3933/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003934CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003935 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003936 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003937 case TargetInfo::SignedShort: return ShortTy;
3938 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3939 case TargetInfo::SignedInt: return IntTy;
3940 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3941 case TargetInfo::SignedLong: return LongTy;
3942 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3943 case TargetInfo::SignedLongLong: return LongLongTy;
3944 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3945 }
3946
3947 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003948 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003949}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003950
3951//===----------------------------------------------------------------------===//
3952// Type Predicates.
3953//===----------------------------------------------------------------------===//
3954
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003955/// isObjCNSObjectType - Return true if this is an NSObject object using
3956/// NSObject attribute on a c-style pointer type.
3957/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003958/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003959///
3960bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3961 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3962 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003963 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003964 return true;
3965 }
Mike Stump11289f42009-09-09 15:08:12 +00003966 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003967}
3968
Fariborz Jahanian96207692009-02-18 21:49:28 +00003969/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3970/// garbage collection attribute.
3971///
John McCall8ccfcb52009-09-24 19:53:00 +00003972Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3973 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003974 if (getLangOptions().ObjC1 &&
3975 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003976 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003977 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003978 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003979 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003980 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003981 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003982 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003983 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003984 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003985 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003986 // Non-pointers have none gc'able attribute regardless of the attribute
3987 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003988 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003989 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003990 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003991 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003992}
3993
Chris Lattner49af6a42008-04-07 06:51:04 +00003994//===----------------------------------------------------------------------===//
3995// Type Compatibility Testing
3996//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00003997
Mike Stump11289f42009-09-09 15:08:12 +00003998/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003999/// compatible.
4000static bool areCompatVectorTypes(const VectorType *LHS,
4001 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004002 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004003 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004004 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004005}
4006
Steve Naroff8e6aee52009-07-23 01:01:38 +00004007//===----------------------------------------------------------------------===//
4008// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4009//===----------------------------------------------------------------------===//
4010
4011/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4012/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004013bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4014 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004015 if (lProto == rProto)
4016 return true;
4017 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4018 E = rProto->protocol_end(); PI != E; ++PI)
4019 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4020 return true;
4021 return false;
4022}
4023
Steve Naroff8e6aee52009-07-23 01:01:38 +00004024/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4025/// return true if lhs's protocols conform to rhs's protocol; false
4026/// otherwise.
4027bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4028 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4029 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4030 return false;
4031}
4032
4033/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4034/// ObjCQualifiedIDType.
4035bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4036 bool compare) {
4037 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004038 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004039 lhs->isObjCIdType() || lhs->isObjCClassType())
4040 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004041 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004042 rhs->isObjCIdType() || rhs->isObjCClassType())
4043 return true;
4044
4045 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004046 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004047
Steve Naroff8e6aee52009-07-23 01:01:38 +00004048 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004049
Steve Naroff8e6aee52009-07-23 01:01:38 +00004050 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004051 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004052 // make sure we check the class hierarchy.
4053 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4054 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4055 E = lhsQID->qual_end(); I != E; ++I) {
4056 // when comparing an id<P> on lhs with a static type on rhs,
4057 // see if static class implements all of id's protocols, directly or
4058 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004059 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004060 return false;
4061 }
4062 }
4063 // If there are no qualifiers and no interface, we have an 'id'.
4064 return true;
4065 }
Mike Stump11289f42009-09-09 15:08:12 +00004066 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004067 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4068 E = lhsQID->qual_end(); I != E; ++I) {
4069 ObjCProtocolDecl *lhsProto = *I;
4070 bool match = false;
4071
4072 // when comparing an id<P> on lhs with a static type on rhs,
4073 // see if static class implements all of id's protocols, directly or
4074 // through its super class and categories.
4075 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4076 E = rhsOPT->qual_end(); J != E; ++J) {
4077 ObjCProtocolDecl *rhsProto = *J;
4078 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4079 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4080 match = true;
4081 break;
4082 }
4083 }
Mike Stump11289f42009-09-09 15:08:12 +00004084 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004085 // make sure we check the class hierarchy.
4086 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4087 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4088 E = lhsQID->qual_end(); I != E; ++I) {
4089 // when comparing an id<P> on lhs with a static type on rhs,
4090 // see if static class implements all of id's protocols, directly or
4091 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004092 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004093 match = true;
4094 break;
4095 }
4096 }
4097 }
4098 if (!match)
4099 return false;
4100 }
Mike Stump11289f42009-09-09 15:08:12 +00004101
Steve Naroff8e6aee52009-07-23 01:01:38 +00004102 return true;
4103 }
Mike Stump11289f42009-09-09 15:08:12 +00004104
Steve Naroff8e6aee52009-07-23 01:01:38 +00004105 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4106 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4107
Mike Stump11289f42009-09-09 15:08:12 +00004108 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004109 lhs->getAsObjCInterfacePointerType()) {
4110 if (lhsOPT->qual_empty()) {
4111 bool match = false;
4112 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4113 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4114 E = rhsQID->qual_end(); I != E; ++I) {
4115 // when comparing an id<P> on lhs with a static type on rhs,
4116 // see if static class implements all of id's protocols, directly or
4117 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004118 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004119 match = true;
4120 break;
4121 }
4122 }
4123 if (!match)
4124 return false;
4125 }
4126 return true;
4127 }
Mike Stump11289f42009-09-09 15:08:12 +00004128 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004129 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4130 E = lhsOPT->qual_end(); I != E; ++I) {
4131 ObjCProtocolDecl *lhsProto = *I;
4132 bool match = false;
4133
4134 // when comparing an id<P> on lhs with a static type on rhs,
4135 // see if static class implements all of id's protocols, directly or
4136 // through its super class and categories.
4137 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4138 E = rhsQID->qual_end(); J != E; ++J) {
4139 ObjCProtocolDecl *rhsProto = *J;
4140 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4141 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4142 match = true;
4143 break;
4144 }
4145 }
4146 if (!match)
4147 return false;
4148 }
4149 return true;
4150 }
4151 return false;
4152}
4153
Eli Friedman47f77112008-08-22 00:56:42 +00004154/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004155/// compatible for assignment from RHS to LHS. This handles validation of any
4156/// protocol qualifiers on the LHS or RHS.
4157///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004158bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4159 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004160 // If either type represents the built-in 'id' or 'Class' types, return true.
4161 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004162 return true;
4163
Steve Naroff8e6aee52009-07-23 01:01:38 +00004164 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00004165 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4166 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004167 false);
4168
Steve Naroff7cae42b2009-07-10 23:34:53 +00004169 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4170 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00004171 if (LHS && RHS) // We have 2 user-defined types.
4172 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004173
Steve Naroff8e6aee52009-07-23 01:01:38 +00004174 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004175}
4176
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004177/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4178/// for providing type-safty for objective-c pointers used to pass/return
4179/// arguments in block literals. When passed as arguments, passing 'A*' where
4180/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4181/// not OK. For the return type, the opposite is not OK.
4182bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4183 const ObjCObjectPointerType *LHSOPT,
4184 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004185 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004186 return true;
4187
4188 if (LHSOPT->isObjCBuiltinType()) {
4189 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4190 }
4191
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004192 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004193 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4194 QualType(RHSOPT,0),
4195 false);
4196
4197 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4198 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4199 if (LHS && RHS) { // We have 2 user-defined types.
4200 if (LHS != RHS) {
4201 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4202 return false;
4203 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4204 return true;
4205 }
4206 else
4207 return true;
4208 }
4209 return false;
4210}
4211
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004212/// getIntersectionOfProtocols - This routine finds the intersection of set
4213/// of protocols inherited from two distinct objective-c pointer objects.
4214/// It is used to build composite qualifier list of the composite type of
4215/// the conditional expression involving two objective-c pointer objects.
4216static
4217void getIntersectionOfProtocols(ASTContext &Context,
4218 const ObjCObjectPointerType *LHSOPT,
4219 const ObjCObjectPointerType *RHSOPT,
4220 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4221
4222 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4223 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4224
4225 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4226 unsigned LHSNumProtocols = LHS->getNumProtocols();
4227 if (LHSNumProtocols > 0)
4228 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4229 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004230 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4231 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004232 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4233 LHSInheritedProtocols.end());
4234 }
4235
4236 unsigned RHSNumProtocols = RHS->getNumProtocols();
4237 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004238 ObjCProtocolDecl **RHSProtocols =
4239 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004240 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4241 if (InheritedProtocolSet.count(RHSProtocols[i]))
4242 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4243 }
4244 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004245 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004246 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004247 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4248 RHSInheritedProtocols.begin(),
4249 E = RHSInheritedProtocols.end(); I != E; ++I)
4250 if (InheritedProtocolSet.count((*I)))
4251 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004252 }
4253}
4254
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004255/// areCommonBaseCompatible - Returns common base class of the two classes if
4256/// one found. Note that this is O'2 algorithm. But it will be called as the
4257/// last type comparison in a ?-exp of ObjC pointer types before a
4258/// warning is issued. So, its invokation is extremely rare.
4259QualType ASTContext::areCommonBaseCompatible(
4260 const ObjCObjectPointerType *LHSOPT,
4261 const ObjCObjectPointerType *RHSOPT) {
4262 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4263 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4264 if (!LHS || !RHS)
4265 return QualType();
4266
4267 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4268 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4269 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004270 if (canAssignObjCInterfaces(LHS, RHS)) {
4271 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4272 getIntersectionOfProtocols(*this,
4273 LHSOPT, RHSOPT, IntersectionOfProtocols);
4274 if (IntersectionOfProtocols.empty())
4275 LHSTy = getObjCObjectPointerType(LHSTy);
4276 else
4277 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4278 IntersectionOfProtocols.size());
4279 return LHSTy;
4280 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004281 }
4282
4283 return QualType();
4284}
4285
Eli Friedman47f77112008-08-22 00:56:42 +00004286bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4287 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00004288 // Verify that the base decls are compatible: the RHS must be a subclass of
4289 // the LHS.
4290 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4291 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004292
Chris Lattner49af6a42008-04-07 06:51:04 +00004293 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4294 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004295 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004296 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004297
Chris Lattner49af6a42008-04-07 06:51:04 +00004298 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4299 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004300 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004301 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004302
Steve Naroffc277ad12009-07-18 15:33:26 +00004303 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4304 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004305 LHSPI != LHSPE; LHSPI++) {
4306 bool RHSImplementsProtocol = false;
4307
4308 // If the RHS doesn't implement the protocol on the left, the types
4309 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00004310 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004311 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004312 RHSPI != RHSPE; RHSPI++) {
4313 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004314 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004315 break;
4316 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004317 }
4318 // FIXME: For better diagnostics, consider passing back the protocol name.
4319 if (!RHSImplementsProtocol)
4320 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004321 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004322 // The RHS implements all protocols listed on the LHS.
4323 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004324}
4325
Steve Naroffb7605152009-02-12 17:52:19 +00004326bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4327 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004328 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4329 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004330
Steve Naroff7cae42b2009-07-10 23:34:53 +00004331 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004332 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004333
4334 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4335 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004336}
4337
Mike Stump11289f42009-09-09 15:08:12 +00004338/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004339/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004340/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004341/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004342bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004343 if (getLangOptions().CPlusPlus)
4344 return hasSameType(LHS, RHS);
4345
Eli Friedman47f77112008-08-22 00:56:42 +00004346 return !mergeTypes(LHS, RHS).isNull();
4347}
4348
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004349bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4350 return !mergeTypes(LHS, RHS, true).isNull();
4351}
4352
4353QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4354 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004355 const FunctionType *lbase = lhs->getAs<FunctionType>();
4356 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004357 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4358 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004359 bool allLTypes = true;
4360 bool allRTypes = true;
4361
4362 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004363 QualType retType;
4364 if (OfBlockPointer)
4365 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4366 else
4367 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004368 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004369 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004370 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004371 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004372 allRTypes = false;
Mike Stumpea086c72009-07-25 23:24:03 +00004373 // FIXME: double check this
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004374 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4375 // rbase->getRegParmAttr() != 0 &&
4376 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004377 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4378 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004379 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4380 lbaseInfo.getRegParm();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004381 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004382 if (NoReturn != lbaseInfo.getNoReturn() ||
4383 RegParm != lbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004384 allLTypes = false;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004385 if (NoReturn != rbaseInfo.getNoReturn() ||
4386 RegParm != rbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004387 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004388 CallingConv lcc = lbaseInfo.getCC();
4389 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004390 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004391 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004392 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004393
Eli Friedman47f77112008-08-22 00:56:42 +00004394 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004395 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4396 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004397 unsigned lproto_nargs = lproto->getNumArgs();
4398 unsigned rproto_nargs = rproto->getNumArgs();
4399
4400 // Compatible functions must have the same number of arguments
4401 if (lproto_nargs != rproto_nargs)
4402 return QualType();
4403
4404 // Variadic and non-variadic functions aren't compatible
4405 if (lproto->isVariadic() != rproto->isVariadic())
4406 return QualType();
4407
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004408 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4409 return QualType();
4410
Eli Friedman47f77112008-08-22 00:56:42 +00004411 // Check argument compatibility
4412 llvm::SmallVector<QualType, 10> types;
4413 for (unsigned i = 0; i < lproto_nargs; i++) {
4414 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4415 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004416 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004417 if (argtype.isNull()) return QualType();
4418 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004419 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4420 allLTypes = false;
4421 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4422 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004423 }
4424 if (allLTypes) return lhs;
4425 if (allRTypes) return rhs;
4426 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004427 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004428 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004429 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004430 }
4431
4432 if (lproto) allRTypes = false;
4433 if (rproto) allLTypes = false;
4434
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004435 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004436 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004437 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004438 if (proto->isVariadic()) return QualType();
4439 // Check that the types are compatible with the types that
4440 // would result from default argument promotions (C99 6.7.5.3p15).
4441 // The only types actually affected are promotable integer
4442 // types and floats, which would be passed as a different
4443 // type depending on whether the prototype is visible.
4444 unsigned proto_nargs = proto->getNumArgs();
4445 for (unsigned i = 0; i < proto_nargs; ++i) {
4446 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004447
4448 // Look at the promotion type of enum types, since that is the type used
4449 // to pass enum values.
4450 if (const EnumType *Enum = argTy->getAs<EnumType>())
4451 argTy = Enum->getDecl()->getPromotionType();
4452
Eli Friedman47f77112008-08-22 00:56:42 +00004453 if (argTy->isPromotableIntegerType() ||
4454 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4455 return QualType();
4456 }
4457
4458 if (allLTypes) return lhs;
4459 if (allRTypes) return rhs;
4460 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004461 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004462 proto->getTypeQuals(),
4463 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004464 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004465 }
4466
4467 if (allLTypes) return lhs;
4468 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004469 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004470 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004471}
4472
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004473QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4474 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004475 // C++ [expr]: If an expression initially has the type "reference to T", the
4476 // type is adjusted to "T" prior to any further analysis, the expression
4477 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004478 // expression is an lvalue unless the reference is an rvalue reference and
4479 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004480 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4481 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4482
Eli Friedman47f77112008-08-22 00:56:42 +00004483 QualType LHSCan = getCanonicalType(LHS),
4484 RHSCan = getCanonicalType(RHS);
4485
4486 // If two types are identical, they are compatible.
4487 if (LHSCan == RHSCan)
4488 return LHS;
4489
John McCall8ccfcb52009-09-24 19:53:00 +00004490 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004491 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4492 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004493 if (LQuals != RQuals) {
4494 // If any of these qualifiers are different, we have a type
4495 // mismatch.
4496 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4497 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4498 return QualType();
4499
4500 // Exactly one GC qualifier difference is allowed: __strong is
4501 // okay if the other type has no GC qualifier but is an Objective
4502 // C object pointer (i.e. implicitly strong by default). We fix
4503 // this by pretending that the unqualified type was actually
4504 // qualified __strong.
4505 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4506 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4507 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4508
4509 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4510 return QualType();
4511
4512 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4513 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4514 }
4515 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4516 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4517 }
Eli Friedman47f77112008-08-22 00:56:42 +00004518 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004519 }
4520
4521 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004522
Eli Friedmandcca6332009-06-01 01:22:52 +00004523 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4524 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004525
Chris Lattnerfd652912008-01-14 05:45:46 +00004526 // We want to consider the two function types to be the same for these
4527 // comparisons, just force one to the other.
4528 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4529 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004530
4531 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004532 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4533 LHSClass = Type::ConstantArray;
4534 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4535 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004536
Nate Begemance4d7fc2008-04-18 23:10:10 +00004537 // Canonicalize ExtVector -> Vector.
4538 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4539 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004540
Chris Lattner95554662008-04-07 05:43:21 +00004541 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004542 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004543 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004544 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004545 // Compatibility is based on the underlying type, not the promotion
4546 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004547 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004548 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4549 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004550 }
John McCall9dd450b2009-09-21 23:43:11 +00004551 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004552 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4553 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004554 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004555
Eli Friedman47f77112008-08-22 00:56:42 +00004556 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004557 }
Eli Friedman47f77112008-08-22 00:56:42 +00004558
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004559 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004560 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004561#define TYPE(Class, Base)
4562#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004563#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004564#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4565#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4566#include "clang/AST/TypeNodes.def"
4567 assert(false && "Non-canonical and dependent types shouldn't get here");
4568 return QualType();
4569
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004570 case Type::LValueReference:
4571 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004572 case Type::MemberPointer:
4573 assert(false && "C++ should never be in mergeTypes");
4574 return QualType();
4575
4576 case Type::IncompleteArray:
4577 case Type::VariableArray:
4578 case Type::FunctionProto:
4579 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004580 assert(false && "Types are eliminated above");
4581 return QualType();
4582
Chris Lattnerfd652912008-01-14 05:45:46 +00004583 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004584 {
4585 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004586 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4587 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004588 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4589 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004590 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004591 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004592 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004593 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004594 return getPointerType(ResultType);
4595 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004596 case Type::BlockPointer:
4597 {
4598 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004599 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4600 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004601 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004602 if (ResultType.isNull()) return QualType();
4603 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4604 return LHS;
4605 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4606 return RHS;
4607 return getBlockPointerType(ResultType);
4608 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004609 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004610 {
4611 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4612 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4613 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4614 return QualType();
4615
4616 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4617 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4618 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4619 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004620 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4621 return LHS;
4622 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4623 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004624 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4625 ArrayType::ArraySizeModifier(), 0);
4626 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4627 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004628 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4629 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004630 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4631 return LHS;
4632 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4633 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004634 if (LVAT) {
4635 // FIXME: This isn't correct! But tricky to implement because
4636 // the array's size has to be the size of LHS, but the type
4637 // has to be different.
4638 return LHS;
4639 }
4640 if (RVAT) {
4641 // FIXME: This isn't correct! But tricky to implement because
4642 // the array's size has to be the size of RHS, but the type
4643 // has to be different.
4644 return RHS;
4645 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004646 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4647 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004648 return getIncompleteArrayType(ResultType,
4649 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004650 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004651 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004652 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004653 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004654 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004655 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004656 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004657 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004658 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004659 case Type::Complex:
4660 // Distinct complex types are incompatible.
4661 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004662 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004663 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004664 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4665 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004666 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004667 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004668 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00004669 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004670 // FIXME: This should be type compatibility, e.g. whether
4671 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00004672 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4673 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00004674 if (LHSIface && RHSIface &&
4675 canAssignObjCInterfaces(LHSIface, RHSIface))
4676 return LHS;
4677
Eli Friedman47f77112008-08-22 00:56:42 +00004678 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004679 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004680 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004681 if (OfBlockPointer) {
4682 if (canAssignObjCInterfacesInBlockPointer(
4683 LHS->getAs<ObjCObjectPointerType>(),
4684 RHS->getAs<ObjCObjectPointerType>()))
4685 return LHS;
4686 return QualType();
4687 }
John McCall9dd450b2009-09-21 23:43:11 +00004688 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4689 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004690 return LHS;
4691
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004692 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004693 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004694 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004695
4696 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004697}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004698
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004699//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004700// Integer Predicates
4701//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004702
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004703unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004704 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004705 return 1;
John McCall56774992009-12-09 09:09:27 +00004706 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004707 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004708 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004709 return (unsigned)getTypeSize(T);
4710}
4711
4712QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4713 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004714
4715 // Turn <4 x signed int> -> <4 x unsigned int>
4716 if (const VectorType *VTy = T->getAs<VectorType>())
4717 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004718 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004719
4720 // For enums, we return the unsigned version of the base type.
4721 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004722 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004723
4724 const BuiltinType *BTy = T->getAs<BuiltinType>();
4725 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004726 switch (BTy->getKind()) {
4727 case BuiltinType::Char_S:
4728 case BuiltinType::SChar:
4729 return UnsignedCharTy;
4730 case BuiltinType::Short:
4731 return UnsignedShortTy;
4732 case BuiltinType::Int:
4733 return UnsignedIntTy;
4734 case BuiltinType::Long:
4735 return UnsignedLongTy;
4736 case BuiltinType::LongLong:
4737 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004738 case BuiltinType::Int128:
4739 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004740 default:
4741 assert(0 && "Unexpected signed integer type");
4742 return QualType();
4743 }
4744}
4745
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004746ExternalASTSource::~ExternalASTSource() { }
4747
4748void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004749
4750
4751//===----------------------------------------------------------------------===//
4752// Builtin Type Computation
4753//===----------------------------------------------------------------------===//
4754
4755/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4756/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004757static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004758 ASTContext::GetBuiltinTypeError &Error,
4759 bool AllowTypeModifiers = true) {
4760 // Modifiers.
4761 int HowLong = 0;
4762 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004763
Chris Lattnerecd79c62009-06-14 00:45:47 +00004764 // Read the modifiers first.
4765 bool Done = false;
4766 while (!Done) {
4767 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004768 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004769 case 'S':
4770 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4771 assert(!Signed && "Can't use 'S' modifier multiple times!");
4772 Signed = true;
4773 break;
4774 case 'U':
4775 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4776 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4777 Unsigned = true;
4778 break;
4779 case 'L':
4780 assert(HowLong <= 2 && "Can't have LLLL modifier");
4781 ++HowLong;
4782 break;
4783 }
4784 }
4785
4786 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004787
Chris Lattnerecd79c62009-06-14 00:45:47 +00004788 // Read the base type.
4789 switch (*Str++) {
4790 default: assert(0 && "Unknown builtin type letter!");
4791 case 'v':
4792 assert(HowLong == 0 && !Signed && !Unsigned &&
4793 "Bad modifiers used with 'v'!");
4794 Type = Context.VoidTy;
4795 break;
4796 case 'f':
4797 assert(HowLong == 0 && !Signed && !Unsigned &&
4798 "Bad modifiers used with 'f'!");
4799 Type = Context.FloatTy;
4800 break;
4801 case 'd':
4802 assert(HowLong < 2 && !Signed && !Unsigned &&
4803 "Bad modifiers used with 'd'!");
4804 if (HowLong)
4805 Type = Context.LongDoubleTy;
4806 else
4807 Type = Context.DoubleTy;
4808 break;
4809 case 's':
4810 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4811 if (Unsigned)
4812 Type = Context.UnsignedShortTy;
4813 else
4814 Type = Context.ShortTy;
4815 break;
4816 case 'i':
4817 if (HowLong == 3)
4818 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4819 else if (HowLong == 2)
4820 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4821 else if (HowLong == 1)
4822 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4823 else
4824 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4825 break;
4826 case 'c':
4827 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4828 if (Signed)
4829 Type = Context.SignedCharTy;
4830 else if (Unsigned)
4831 Type = Context.UnsignedCharTy;
4832 else
4833 Type = Context.CharTy;
4834 break;
4835 case 'b': // boolean
4836 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4837 Type = Context.BoolTy;
4838 break;
4839 case 'z': // size_t.
4840 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4841 Type = Context.getSizeType();
4842 break;
4843 case 'F':
4844 Type = Context.getCFConstantStringType();
4845 break;
4846 case 'a':
4847 Type = Context.getBuiltinVaListType();
4848 assert(!Type.isNull() && "builtin va list type not initialized!");
4849 break;
4850 case 'A':
4851 // This is a "reference" to a va_list; however, what exactly
4852 // this means depends on how va_list is defined. There are two
4853 // different kinds of va_list: ones passed by value, and ones
4854 // passed by reference. An example of a by-value va_list is
4855 // x86, where va_list is a char*. An example of by-ref va_list
4856 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4857 // we want this argument to be a char*&; for x86-64, we want
4858 // it to be a __va_list_tag*.
4859 Type = Context.getBuiltinVaListType();
4860 assert(!Type.isNull() && "builtin va list type not initialized!");
4861 if (Type->isArrayType()) {
4862 Type = Context.getArrayDecayedType(Type);
4863 } else {
4864 Type = Context.getLValueReferenceType(Type);
4865 }
4866 break;
4867 case 'V': {
4868 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004869 unsigned NumElements = strtoul(Str, &End, 10);
4870 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004871
Chris Lattnerecd79c62009-06-14 00:45:47 +00004872 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004873
Chris Lattnerecd79c62009-06-14 00:45:47 +00004874 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00004875 // FIXME: Don't know what to do about AltiVec.
4876 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004877 break;
4878 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004879 case 'X': {
4880 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4881 Type = Context.getComplexType(ElementType);
4882 break;
4883 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004884 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004885 Type = Context.getFILEType();
4886 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004887 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004888 return QualType();
4889 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004890 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004891 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004892 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004893 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004894 else
4895 Type = Context.getjmp_bufType();
4896
Mike Stump2adb4da2009-07-28 23:47:15 +00004897 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004898 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004899 return QualType();
4900 }
4901 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004902 }
Mike Stump11289f42009-09-09 15:08:12 +00004903
Chris Lattnerecd79c62009-06-14 00:45:47 +00004904 if (!AllowTypeModifiers)
4905 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004906
Chris Lattnerecd79c62009-06-14 00:45:47 +00004907 Done = false;
4908 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00004909 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00004910 default: Done = true; --Str; break;
4911 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00004912 case '&':
John McCallb8b94662010-03-12 04:21:28 +00004913 {
4914 // Both pointers and references can have their pointee types
4915 // qualified with an address space.
4916 char *End;
4917 unsigned AddrSpace = strtoul(Str, &End, 10);
4918 if (End != Str && AddrSpace != 0) {
4919 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4920 Str = End;
4921 }
4922 }
4923 if (c == '*')
4924 Type = Context.getPointerType(Type);
4925 else
4926 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004927 break;
4928 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4929 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004930 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004931 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00004932 case 'D':
4933 Type = Context.getVolatileType(Type);
4934 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004935 }
4936 }
Mike Stump11289f42009-09-09 15:08:12 +00004937
Chris Lattnerecd79c62009-06-14 00:45:47 +00004938 return Type;
4939}
4940
4941/// GetBuiltinType - Return the type for the specified builtin.
4942QualType ASTContext::GetBuiltinType(unsigned id,
4943 GetBuiltinTypeError &Error) {
4944 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004945
Chris Lattnerecd79c62009-06-14 00:45:47 +00004946 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004947
Chris Lattnerecd79c62009-06-14 00:45:47 +00004948 Error = GE_None;
4949 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4950 if (Error != GE_None)
4951 return QualType();
4952 while (TypeStr[0] && TypeStr[0] != '.') {
4953 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4954 if (Error != GE_None)
4955 return QualType();
4956
4957 // Do array -> pointer decay. The builtin should use the decayed type.
4958 if (Ty->isArrayType())
4959 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004960
Chris Lattnerecd79c62009-06-14 00:45:47 +00004961 ArgTypes.push_back(Ty);
4962 }
4963
4964 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4965 "'.' should only occur at end of builtin type list!");
4966
4967 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4968 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4969 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00004970
4971 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00004972 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00004973 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004974 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00004975}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004976
4977QualType
4978ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4979 // Perform the usual unary conversions. We do this early so that
4980 // integral promotions to "int" can allow us to exit early, in the
4981 // lhs == rhs check. Also, for conversion purposes, we ignore any
4982 // qualifiers. For example, "const float" and "float" are
4983 // equivalent.
4984 if (lhs->isPromotableIntegerType())
4985 lhs = getPromotedIntegerType(lhs);
4986 else
4987 lhs = lhs.getUnqualifiedType();
4988 if (rhs->isPromotableIntegerType())
4989 rhs = getPromotedIntegerType(rhs);
4990 else
4991 rhs = rhs.getUnqualifiedType();
4992
4993 // If both types are identical, no conversion is needed.
4994 if (lhs == rhs)
4995 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004996
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004997 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4998 // The caller can deal with this (e.g. pointer + int).
4999 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5000 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005001
5002 // At this point, we have two different arithmetic types.
5003
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005004 // Handle complex types first (C99 6.3.1.8p1).
5005 if (lhs->isComplexType() || rhs->isComplexType()) {
5006 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00005007 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005008 // convert the rhs to the lhs complex type.
5009 return lhs;
5010 }
Mike Stump11289f42009-09-09 15:08:12 +00005011 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005012 // convert the lhs to the rhs complex type.
5013 return rhs;
5014 }
5015 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00005016 // When both operands are complex, the shorter operand is converted to the
5017 // type of the longer, and that is the type of the result. This corresponds
5018 // to what is done when combining two real floating-point operands.
5019 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005020 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00005021 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005022 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00005023 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005024 // "double _Complex" is promoted to "long double _Complex".
5025 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005026
5027 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005028 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005029 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005030 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00005031 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005032 // At this point, lhs and rhs have the same rank/size. Now, make sure the
5033 // domains match. This is a requirement for our implementation, C99
5034 // does not require this promotion.
5035 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5036 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5037 return rhs;
5038 } else { // handle "_Complex double, double".
5039 return lhs;
5040 }
5041 }
5042 return lhs; // The domain/size match exactly.
5043 }
5044 // Now handle "real" floating types (i.e. float, double, long double).
5045 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5046 // if we have an integer operand, the result is the real floating type.
5047 if (rhs->isIntegerType()) {
5048 // convert rhs to the lhs floating point type.
5049 return lhs;
5050 }
5051 if (rhs->isComplexIntegerType()) {
5052 // convert rhs to the complex floating point type.
5053 return getComplexType(lhs);
5054 }
5055 if (lhs->isIntegerType()) {
5056 // convert lhs to the rhs floating point type.
5057 return rhs;
5058 }
Mike Stump11289f42009-09-09 15:08:12 +00005059 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005060 // convert lhs to the complex floating point type.
5061 return getComplexType(rhs);
5062 }
5063 // We have two real floating types, float/complex combos were handled above.
5064 // Convert the smaller operand to the bigger result.
5065 int result = getFloatingTypeOrder(lhs, rhs);
5066 if (result > 0) // convert the rhs
5067 return lhs;
5068 assert(result < 0 && "illegal float comparison");
5069 return rhs; // convert the lhs
5070 }
5071 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5072 // Handle GCC complex int extension.
5073 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5074 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5075
5076 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005077 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005078 rhsComplexInt->getElementType()) >= 0)
5079 return lhs; // convert the rhs
5080 return rhs;
5081 } else if (lhsComplexInt && rhs->isIntegerType()) {
5082 // convert the rhs to the lhs complex type.
5083 return lhs;
5084 } else if (rhsComplexInt && lhs->isIntegerType()) {
5085 // convert the lhs to the rhs complex type.
5086 return rhs;
5087 }
5088 }
5089 // Finally, we have two differing integer types.
5090 // The rules for this case are in C99 6.3.1.8
5091 int compare = getIntegerTypeOrder(lhs, rhs);
5092 bool lhsSigned = lhs->isSignedIntegerType(),
5093 rhsSigned = rhs->isSignedIntegerType();
5094 QualType destType;
5095 if (lhsSigned == rhsSigned) {
5096 // Same signedness; use the higher-ranked type
5097 destType = compare >= 0 ? lhs : rhs;
5098 } else if (compare != (lhsSigned ? 1 : -1)) {
5099 // The unsigned type has greater than or equal rank to the
5100 // signed type, so use the unsigned type
5101 destType = lhsSigned ? rhs : lhs;
5102 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5103 // The two types are different widths; if we are here, that
5104 // means the signed type is larger than the unsigned type, so
5105 // use the signed type.
5106 destType = lhsSigned ? lhs : rhs;
5107 } else {
5108 // The signed type is higher-ranked than the unsigned type,
5109 // but isn't actually any bigger (like unsigned int and long
5110 // on most 32-bit systems). Use the unsigned type corresponding
5111 // to the signed type.
5112 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5113 }
5114 return destType;
5115}