blob: 41ba4c2d71fff529c1ba0be20ed88afc9e08ac4d [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
John McCalle78aac42010-03-10 03:28:59 +0000656 case Type::InjectedClassName:
657 return getTypeInfo(cast<InjectedClassNameType>(T)
658 ->getUnderlyingType().getTypePtr());
659
Douglas Gregoref462e62009-04-30 17:32:17 +0000660 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000661 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000662 "Cannot request the size of a dependent type");
663 // FIXME: this is likely to be wrong once we support template
664 // aliases, since a template alias could refer to a typedef that
665 // has an __aligned__ attribute on it.
666 return getTypeInfo(getCanonicalType(T));
667 }
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattner53cfe802007-07-18 17:52:12 +0000669 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000670 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000671}
672
Ken Dyck8c89d592009-12-22 14:23:30 +0000673/// getTypeSizeInChars - Return the size of the specified type, in characters.
674/// This method does not work on incomplete types.
675CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000676 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000677}
678CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000679 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000680}
681
Ken Dycka6046ab2010-01-26 17:25:18 +0000682/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000683/// characters. This method does not work on incomplete types.
684CharUnits ASTContext::getTypeAlignInChars(QualType T) {
685 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
686}
687CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
688 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
689}
690
Chris Lattnera3402cd2009-01-27 18:08:34 +0000691/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
692/// type for the current target in bits. This can be different than the ABI
693/// alignment in cases where it is beneficial for performance to overalign
694/// a data type.
695unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
696 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000697
698 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000699 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000700 T = CT->getElementType().getTypePtr();
701 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
702 T->isSpecificBuiltinType(BuiltinType::LongLong))
703 return std::max(ABIAlign, (unsigned)getTypeSize(T));
704
Chris Lattnera3402cd2009-01-27 18:08:34 +0000705 return ABIAlign;
706}
707
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000708static void CollectLocalObjCIvars(ASTContext *Ctx,
709 const ObjCInterfaceDecl *OI,
710 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000711 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
712 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000713 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000714 if (!IVDecl->isInvalidDecl())
715 Fields.push_back(cast<FieldDecl>(IVDecl));
716 }
717}
718
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000719void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
720 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
721 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
722 CollectObjCIvars(SuperClass, Fields);
723 CollectLocalObjCIvars(this, OI, Fields);
724}
725
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000726/// ShallowCollectObjCIvars -
727/// Collect all ivars, including those synthesized, in the current class.
728///
729void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000730 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000731 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
732 E = OI->ivar_end(); I != E; ++I) {
733 Ivars.push_back(*I);
734 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000735
736 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000737}
738
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000739/// CollectNonClassIvars -
740/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000741/// This includes synthesized ivars (via @synthesize) and those in
742// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000743///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000744void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000745 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000746 // Find ivars declared in class extension.
747 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
748 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
749 E = CDecl->ivar_end(); I != E; ++I) {
750 Ivars.push_back(*I);
751 }
752 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000753
Ted Kremenek86838aa2010-03-11 19:44:54 +0000754 // Also add any ivar defined in this class's implementation. This
755 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000756 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
757 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
758 E = ImplDecl->ivar_end(); I != E; ++I)
759 Ivars.push_back(*I);
760 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000761}
762
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000763/// CollectInheritedProtocols - Collect all protocols in current class and
764/// those inherited by it.
765void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000766 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000767 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
768 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
769 PE = OI->protocol_end(); P != PE; ++P) {
770 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000771 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000772 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000773 PE = Proto->protocol_end(); P != PE; ++P) {
774 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000775 CollectInheritedProtocols(*P, Protocols);
776 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000777 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000778
779 // Categories of this Interface.
780 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
781 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
782 CollectInheritedProtocols(CDeclChain, Protocols);
783 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
784 while (SD) {
785 CollectInheritedProtocols(SD, Protocols);
786 SD = SD->getSuperClass();
787 }
788 return;
789 }
790 if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
791 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
792 PE = OC->protocol_end(); P != PE; ++P) {
793 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000794 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000795 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
796 PE = Proto->protocol_end(); P != PE; ++P)
797 CollectInheritedProtocols(*P, Protocols);
798 }
799 return;
800 }
801 if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
802 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
803 PE = OP->protocol_end(); P != PE; ++P) {
804 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000805 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000806 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
807 PE = Proto->protocol_end(); P != PE; ++P)
808 CollectInheritedProtocols(*P, Protocols);
809 }
810 return;
811 }
812}
813
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000814unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
815 unsigned count = 0;
816 // Count ivars declared in class extension.
817 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
818 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
819 E = CDecl->ivar_end(); I != E; ++I) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000820 ++count;
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000821 }
822 }
823
824 // Count ivar defined in this class's implementation. This
825 // includes synthesized ivars.
826 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
827 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
828 E = ImplDecl->ivar_end(); I != E; ++I)
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000829 ++count;
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000830 return count;
831}
832
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000833/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
834ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
835 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
836 I = ObjCImpls.find(D);
837 if (I != ObjCImpls.end())
838 return cast<ObjCImplementationDecl>(I->second);
839 return 0;
840}
841/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
842ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
843 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
844 I = ObjCImpls.find(D);
845 if (I != ObjCImpls.end())
846 return cast<ObjCCategoryImplDecl>(I->second);
847 return 0;
848}
849
850/// \brief Set the implementation of ObjCInterfaceDecl.
851void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
852 ObjCImplementationDecl *ImplD) {
853 assert(IFaceD && ImplD && "Passed null params");
854 ObjCImpls[IFaceD] = ImplD;
855}
856/// \brief Set the implementation of ObjCCategoryDecl.
857void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
858 ObjCCategoryImplDecl *ImplD) {
859 assert(CatD && ImplD && "Passed null params");
860 ObjCImpls[CatD] = ImplD;
861}
862
John McCallbcd03502009-12-07 02:54:59 +0000863/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000864///
John McCallbcd03502009-12-07 02:54:59 +0000865/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000866/// the TypeLoc wrappers.
867///
868/// \param T the type that will be the basis for type source info. This type
869/// should refer to how the declarator was written in source code, not to
870/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000871TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000872 unsigned DataSize) {
873 if (!DataSize)
874 DataSize = TypeLoc::getFullDataSizeForType(T);
875 else
876 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000877 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000878
John McCallbcd03502009-12-07 02:54:59 +0000879 TypeSourceInfo *TInfo =
880 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
881 new (TInfo) TypeSourceInfo(T);
882 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000883}
884
John McCallbcd03502009-12-07 02:54:59 +0000885TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000886 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +0000887 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +0000888 DI->getTypeLoc().initialize(L);
889 return DI;
890}
891
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000892/// getInterfaceLayoutImpl - Get or compute information about the
893/// layout of the given interface.
894///
895/// \param Impl - If given, also include the layout of the interface's
896/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000897const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000898ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
899 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000900 assert(!D->isForwardDecl() && "Invalid interface decl!");
901
Devang Pateldbb72632008-06-04 21:54:36 +0000902 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000903 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000904 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
905 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
906 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000907
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000908 // Add in synthesized ivar count if laying out an implementation.
909 if (Impl) {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000910 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000911 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000912 // entry. Note we can't cache this because we simply free all
913 // entries later; however we shouldn't look up implementations
914 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000915 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000916 return getObjCLayout(D, 0);
917 }
918
Mike Stump11289f42009-09-09 15:08:12 +0000919 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000920 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
921 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000922
Devang Pateldbb72632008-06-04 21:54:36 +0000923 return *NewEntry;
924}
925
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000926const ASTRecordLayout &
927ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
928 return getObjCLayout(D, 0);
929}
930
931const ASTRecordLayout &
932ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
933 return getObjCLayout(D->getClassInterface(), D);
934}
935
Devang Patele11664a2007-11-01 19:11:01 +0000936/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +0000937/// specified record (struct/union/class), which indicates its size and field
938/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +0000939const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000940 D = D->getDefinition();
Ted Kremenek21475702008-09-05 17:16:31 +0000941 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +0000942
Chris Lattner53cfe802007-07-18 17:52:12 +0000943 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +0000944 // Note that we can't save a reference to the entry because this function
945 // is recursive.
946 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +0000947 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +0000948
Mike Stump11289f42009-09-09 15:08:12 +0000949 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000950 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +0000951 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000952
Daniel Dunbarccabe482010-04-19 20:44:53 +0000953 if (getLangOptions().DumpRecordLayouts) {
954 llvm::errs() << "\n*** Dumping AST Record Layout\n";
955 DumpRecordLayout(D, llvm::errs());
956 }
957
Chris Lattner647fb222007-07-18 18:26:58 +0000958 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +0000959}
960
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000961const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000962 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000963 assert(RD && "Cannot get key function for forward declarations!");
964
965 const CXXMethodDecl *&Entry = KeyFunctions[RD];
966 if (!Entry)
967 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
968 else
969 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
970 "Key function changed!");
971
972 return Entry;
973}
974
Chris Lattner983a8bb2007-07-13 22:13:22 +0000975//===----------------------------------------------------------------------===//
976// Type creation/memoization methods
977//===----------------------------------------------------------------------===//
978
John McCall8ccfcb52009-09-24 19:53:00 +0000979QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
980 unsigned Fast = Quals.getFastQualifiers();
981 Quals.removeFastQualifiers();
982
983 // Check if we've already instantiated this type.
984 llvm::FoldingSetNodeID ID;
985 ExtQuals::Profile(ID, TypeNode, Quals);
986 void *InsertPos = 0;
987 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
988 assert(EQ->getQualifiers() == Quals);
989 QualType T = QualType(EQ, Fast);
990 return T;
991 }
992
John McCall90d1c2d2009-09-24 23:30:46 +0000993 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000994 ExtQualNodes.InsertNode(New, InsertPos);
995 QualType T = QualType(New, Fast);
996 return T;
997}
998
999QualType ASTContext::getVolatileType(QualType T) {
1000 QualType CanT = getCanonicalType(T);
1001 if (CanT.isVolatileQualified()) return T;
1002
1003 QualifierCollector Quals;
1004 const Type *TypeNode = Quals.strip(T);
1005 Quals.addVolatile();
1006
1007 return getExtQualType(TypeNode, Quals);
1008}
1009
Fariborz Jahanianece85822009-02-17 18:27:45 +00001010QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001011 QualType CanT = getCanonicalType(T);
1012 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001013 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001014
John McCall8ccfcb52009-09-24 19:53:00 +00001015 // If we are composing extended qualifiers together, merge together
1016 // into one ExtQuals node.
1017 QualifierCollector Quals;
1018 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001019
John McCall8ccfcb52009-09-24 19:53:00 +00001020 // If this type already has an address space specified, it cannot get
1021 // another one.
1022 assert(!Quals.hasAddressSpace() &&
1023 "Type cannot be in multiple addr spaces!");
1024 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001025
John McCall8ccfcb52009-09-24 19:53:00 +00001026 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001027}
1028
Chris Lattnerd60183d2009-02-18 22:53:11 +00001029QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001030 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001031 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001032 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001033 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001034
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001035 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001036 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001037 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001038 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1039 return getPointerType(ResultType);
1040 }
1041 }
Mike Stump11289f42009-09-09 15:08:12 +00001042
John McCall8ccfcb52009-09-24 19:53:00 +00001043 // If we are composing extended qualifiers together, merge together
1044 // into one ExtQuals node.
1045 QualifierCollector Quals;
1046 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001047
John McCall8ccfcb52009-09-24 19:53:00 +00001048 // If this type already has an ObjCGC specified, it cannot get
1049 // another one.
1050 assert(!Quals.hasObjCGCAttr() &&
1051 "Type cannot have multiple ObjCGCs!");
1052 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001053
John McCall8ccfcb52009-09-24 19:53:00 +00001054 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001055}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001056
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001057static QualType getExtFunctionType(ASTContext& Context, QualType T,
1058 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001059 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001060 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1061 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001062 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001063 if (ResultType == Pointee)
1064 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001065
1066 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001067 } else if (const BlockPointerType *BlockPointer
1068 = T->getAs<BlockPointerType>()) {
1069 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001070 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001071 if (ResultType == Pointee)
1072 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001073
1074 ResultType = Context.getBlockPointerType(ResultType);
1075 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001076 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001077 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001078
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001079 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001080 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001081 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001082 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001083 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001084 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001085 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1086 FPT->getNumArgs(), FPT->isVariadic(),
1087 FPT->getTypeQuals(),
1088 FPT->hasExceptionSpec(),
1089 FPT->hasAnyExceptionSpec(),
1090 FPT->getNumExceptions(),
1091 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001092 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001093 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001094 } else
1095 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001096
1097 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1098}
1099
1100QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001101 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001102 return getExtFunctionType(*this, T,
1103 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001104}
1105
1106QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001107 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001108 return getExtFunctionType(*this, T,
1109 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001110}
1111
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001112QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1113 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1114 return getExtFunctionType(*this, T,
1115 Info.withRegParm(RegParm));
1116}
1117
Chris Lattnerc6395932007-06-22 20:56:16 +00001118/// getComplexType - Return the uniqued reference to the type for a complex
1119/// number with the specified element type.
1120QualType ASTContext::getComplexType(QualType T) {
1121 // Unique pointers, to guarantee there is only one pointer of a particular
1122 // structure.
1123 llvm::FoldingSetNodeID ID;
1124 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001125
Chris Lattnerc6395932007-06-22 20:56:16 +00001126 void *InsertPos = 0;
1127 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1128 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001129
Chris Lattnerc6395932007-06-22 20:56:16 +00001130 // If the pointee type isn't canonical, this won't be a canonical type either,
1131 // so fill in the canonical type field.
1132 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001133 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001134 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001135
Chris Lattnerc6395932007-06-22 20:56:16 +00001136 // Get the new insert position for the node we care about.
1137 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001138 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001139 }
John McCall90d1c2d2009-09-24 23:30:46 +00001140 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001141 Types.push_back(New);
1142 ComplexTypes.InsertNode(New, InsertPos);
1143 return QualType(New, 0);
1144}
1145
Chris Lattner970e54e2006-11-12 00:37:36 +00001146/// getPointerType - Return the uniqued reference to the type for a pointer to
1147/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001148QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001149 // Unique pointers, to guarantee there is only one pointer of a particular
1150 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001151 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001152 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001153
Chris Lattner67521df2007-01-27 01:29:36 +00001154 void *InsertPos = 0;
1155 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001156 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001157
Chris Lattner7ccecb92006-11-12 08:50:50 +00001158 // If the pointee type isn't canonical, this won't be a canonical type either,
1159 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001160 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001161 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001162 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001163
Chris Lattner67521df2007-01-27 01:29:36 +00001164 // Get the new insert position for the node we care about.
1165 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001166 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001167 }
John McCall90d1c2d2009-09-24 23:30:46 +00001168 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001169 Types.push_back(New);
1170 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001171 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001172}
1173
Mike Stump11289f42009-09-09 15:08:12 +00001174/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001175/// a pointer to the specified block.
1176QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001177 assert(T->isFunctionType() && "block of function types only");
1178 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001179 // structure.
1180 llvm::FoldingSetNodeID ID;
1181 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001182
Steve Naroffec33ed92008-08-27 16:04:49 +00001183 void *InsertPos = 0;
1184 if (BlockPointerType *PT =
1185 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1186 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001187
1188 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001189 // type either so fill in the canonical type field.
1190 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001191 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001192 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001193
Steve Naroffec33ed92008-08-27 16:04:49 +00001194 // Get the new insert position for the node we care about.
1195 BlockPointerType *NewIP =
1196 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001197 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001198 }
John McCall90d1c2d2009-09-24 23:30:46 +00001199 BlockPointerType *New
1200 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001201 Types.push_back(New);
1202 BlockPointerTypes.InsertNode(New, InsertPos);
1203 return QualType(New, 0);
1204}
1205
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001206/// getLValueReferenceType - Return the uniqued reference to the type for an
1207/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001208QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001209 // Unique pointers, to guarantee there is only one pointer of a particular
1210 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001211 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001212 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001213
1214 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001215 if (LValueReferenceType *RT =
1216 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001217 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001218
John McCallfc93cf92009-10-22 22:37:11 +00001219 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1220
Bill Wendling3708c182007-05-27 10:15:43 +00001221 // If the referencee type isn't canonical, this won't be a canonical type
1222 // either, so fill in the canonical type field.
1223 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001224 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1225 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1226 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001227
Bill Wendling3708c182007-05-27 10:15:43 +00001228 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001229 LValueReferenceType *NewIP =
1230 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001231 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001232 }
1233
John McCall90d1c2d2009-09-24 23:30:46 +00001234 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001235 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1236 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001237 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001238 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001239
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001240 return QualType(New, 0);
1241}
1242
1243/// getRValueReferenceType - Return the uniqued reference to the type for an
1244/// rvalue reference to the specified type.
1245QualType ASTContext::getRValueReferenceType(QualType T) {
1246 // Unique pointers, to guarantee there is only one pointer of a particular
1247 // structure.
1248 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001249 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001250
1251 void *InsertPos = 0;
1252 if (RValueReferenceType *RT =
1253 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1254 return QualType(RT, 0);
1255
John McCallfc93cf92009-10-22 22:37:11 +00001256 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1257
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001258 // If the referencee type isn't canonical, this won't be a canonical type
1259 // either, so fill in the canonical type field.
1260 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001261 if (InnerRef || !T.isCanonical()) {
1262 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1263 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001264
1265 // Get the new insert position for the node we care about.
1266 RValueReferenceType *NewIP =
1267 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1268 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1269 }
1270
John McCall90d1c2d2009-09-24 23:30:46 +00001271 RValueReferenceType *New
1272 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001273 Types.push_back(New);
1274 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001275 return QualType(New, 0);
1276}
1277
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001278/// getMemberPointerType - Return the uniqued reference to the type for a
1279/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001280QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001281 // Unique pointers, to guarantee there is only one pointer of a particular
1282 // structure.
1283 llvm::FoldingSetNodeID ID;
1284 MemberPointerType::Profile(ID, T, Cls);
1285
1286 void *InsertPos = 0;
1287 if (MemberPointerType *PT =
1288 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1289 return QualType(PT, 0);
1290
1291 // If the pointee or class type isn't canonical, this won't be a canonical
1292 // type either, so fill in the canonical type field.
1293 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001294 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001295 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1296
1297 // Get the new insert position for the node we care about.
1298 MemberPointerType *NewIP =
1299 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1300 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1301 }
John McCall90d1c2d2009-09-24 23:30:46 +00001302 MemberPointerType *New
1303 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001304 Types.push_back(New);
1305 MemberPointerTypes.InsertNode(New, InsertPos);
1306 return QualType(New, 0);
1307}
1308
Mike Stump11289f42009-09-09 15:08:12 +00001309/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001310/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001311QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001312 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001313 ArrayType::ArraySizeModifier ASM,
1314 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001315 assert((EltTy->isDependentType() ||
1316 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001317 "Constant array of VLAs is illegal!");
1318
Chris Lattnere2df3f92009-05-13 04:12:56 +00001319 // Convert the array size into a canonical width matching the pointer size for
1320 // the target.
1321 llvm::APInt ArySize(ArySizeIn);
1322 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattner23b7eb62007-06-15 23:05:46 +00001324 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001325 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001326
Chris Lattner36f8e652007-01-27 08:31:04 +00001327 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001328 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001329 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001330 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001331
Chris Lattner7ccecb92006-11-12 08:50:50 +00001332 // If the element type isn't canonical, this won't be a canonical type either,
1333 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001334 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001335 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001336 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001337 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001338 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001339 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001340 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001341 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001342 }
Mike Stump11289f42009-09-09 15:08:12 +00001343
John McCall90d1c2d2009-09-24 23:30:46 +00001344 ConstantArrayType *New = new(*this,TypeAlignment)
1345 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001346 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001347 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001348 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001349}
1350
Steve Naroffcadebd02007-08-30 18:14:25 +00001351/// getVariableArrayType - Returns a non-unique reference to the type for a
1352/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001353QualType ASTContext::getVariableArrayType(QualType EltTy,
1354 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001355 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001356 unsigned EltTypeQuals,
1357 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001358 // Since we don't unique expressions, it isn't possible to unique VLA's
1359 // that have an expression provided for their size.
1360
John McCall90d1c2d2009-09-24 23:30:46 +00001361 VariableArrayType *New = new(*this, TypeAlignment)
1362 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001363
1364 VariableArrayTypes.push_back(New);
1365 Types.push_back(New);
1366 return QualType(New, 0);
1367}
1368
Douglas Gregor4619e432008-12-05 23:32:09 +00001369/// getDependentSizedArrayType - Returns a non-unique reference to
1370/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001371/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001372QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1373 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001374 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001375 unsigned EltTypeQuals,
1376 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001377 assert((!NumElts || NumElts->isTypeDependent() ||
1378 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001379 "Size must be type- or value-dependent!");
1380
Douglas Gregorf3f95522009-07-31 00:23:35 +00001381 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001382 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001383 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001384
1385 if (NumElts) {
1386 // Dependently-sized array types that do not have a specified
1387 // number of elements will have their sizes deduced from an
1388 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001389 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1390 EltTypeQuals, NumElts);
1391
1392 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1393 }
1394
Douglas Gregorf3f95522009-07-31 00:23:35 +00001395 DependentSizedArrayType *New;
1396 if (Canon) {
1397 // We already have a canonical version of this array type; use it as
1398 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001399 New = new (*this, TypeAlignment)
1400 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1401 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001402 } else {
1403 QualType CanonEltTy = getCanonicalType(EltTy);
1404 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001405 New = new (*this, TypeAlignment)
1406 DependentSizedArrayType(*this, EltTy, QualType(),
1407 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001408
Douglas Gregorc42075a2010-02-04 18:10:26 +00001409 if (NumElts) {
1410 DependentSizedArrayType *CanonCheck
1411 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1412 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1413 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001414 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001415 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001416 } else {
1417 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1418 ASM, EltTypeQuals,
1419 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001420 New = new (*this, TypeAlignment)
1421 DependentSizedArrayType(*this, EltTy, Canon,
1422 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001423 }
1424 }
Mike Stump11289f42009-09-09 15:08:12 +00001425
Douglas Gregor4619e432008-12-05 23:32:09 +00001426 Types.push_back(New);
1427 return QualType(New, 0);
1428}
1429
Eli Friedmanbd258282008-02-15 18:16:39 +00001430QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1431 ArrayType::ArraySizeModifier ASM,
1432 unsigned EltTypeQuals) {
1433 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001434 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001435
1436 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001437 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001438 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1439 return QualType(ATP, 0);
1440
1441 // If the element type isn't canonical, this won't be a canonical type
1442 // either, so fill in the canonical type field.
1443 QualType Canonical;
1444
John McCallb692a092009-10-22 20:10:53 +00001445 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001446 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001447 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001448
1449 // Get the new insert position for the node we care about.
1450 IncompleteArrayType *NewIP =
1451 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001452 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001453 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001454
John McCall90d1c2d2009-09-24 23:30:46 +00001455 IncompleteArrayType *New = new (*this, TypeAlignment)
1456 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001457
1458 IncompleteArrayTypes.InsertNode(New, InsertPos);
1459 Types.push_back(New);
1460 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001461}
1462
Steve Naroff91fcddb2007-07-18 18:00:27 +00001463/// getVectorType - Return the unique reference to a vector type of
1464/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001465QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1466 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001467 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001468
Chris Lattner76a00cf2008-04-06 22:59:24 +00001469 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001470 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001471
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001472 // Check if we've already instantiated a vector of this type.
1473 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001474 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1475 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001476 void *InsertPos = 0;
1477 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1478 return QualType(VTP, 0);
1479
1480 // If the element type isn't canonical, this won't be a canonical type either,
1481 // so fill in the canonical type field.
1482 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001483 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1484 Canonical = getVectorType(getCanonicalType(vecType),
1485 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001486
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001487 // Get the new insert position for the node we care about.
1488 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001489 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001490 }
John McCall90d1c2d2009-09-24 23:30:46 +00001491 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001492 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001493 VectorTypes.InsertNode(New, InsertPos);
1494 Types.push_back(New);
1495 return QualType(New, 0);
1496}
1497
Nate Begemance4d7fc2008-04-18 23:10:10 +00001498/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001499/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001500QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001501 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001502
Chris Lattner76a00cf2008-04-06 22:59:24 +00001503 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001504 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001505
Steve Naroff91fcddb2007-07-18 18:00:27 +00001506 // Check if we've already instantiated a vector of this type.
1507 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001508 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001509 void *InsertPos = 0;
1510 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1511 return QualType(VTP, 0);
1512
1513 // If the element type isn't canonical, this won't be a canonical type either,
1514 // so fill in the canonical type field.
1515 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001516 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001517 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001518
Steve Naroff91fcddb2007-07-18 18:00:27 +00001519 // Get the new insert position for the node we care about.
1520 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001521 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001522 }
John McCall90d1c2d2009-09-24 23:30:46 +00001523 ExtVectorType *New = new (*this, TypeAlignment)
1524 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001525 VectorTypes.InsertNode(New, InsertPos);
1526 Types.push_back(New);
1527 return QualType(New, 0);
1528}
1529
Mike Stump11289f42009-09-09 15:08:12 +00001530QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001531 Expr *SizeExpr,
1532 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001533 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001534 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001535 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001536
Douglas Gregor352169a2009-07-31 03:54:25 +00001537 void *InsertPos = 0;
1538 DependentSizedExtVectorType *Canon
1539 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1540 DependentSizedExtVectorType *New;
1541 if (Canon) {
1542 // We already have a canonical version of this array type; use it as
1543 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001544 New = new (*this, TypeAlignment)
1545 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1546 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001547 } else {
1548 QualType CanonVecTy = getCanonicalType(vecType);
1549 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001550 New = new (*this, TypeAlignment)
1551 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1552 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001553
1554 DependentSizedExtVectorType *CanonCheck
1555 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1556 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1557 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001558 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1559 } else {
1560 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1561 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001562 New = new (*this, TypeAlignment)
1563 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001564 }
1565 }
Mike Stump11289f42009-09-09 15:08:12 +00001566
Douglas Gregor758a8692009-06-17 21:51:59 +00001567 Types.push_back(New);
1568 return QualType(New, 0);
1569}
1570
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001571/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001572///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001573QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1574 const FunctionType::ExtInfo &Info) {
1575 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001576 // Unique functions, to guarantee there is only one function of a particular
1577 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001578 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001579 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001580
Chris Lattner47955de2007-01-27 08:37:20 +00001581 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001582 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001583 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001584 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001585
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001586 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001587 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001588 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001589 Canonical =
1590 getFunctionNoProtoType(getCanonicalType(ResultTy),
1591 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001592
Chris Lattner47955de2007-01-27 08:37:20 +00001593 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001594 FunctionNoProtoType *NewIP =
1595 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001596 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001597 }
Mike Stump11289f42009-09-09 15:08:12 +00001598
John McCall90d1c2d2009-09-24 23:30:46 +00001599 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001600 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001601 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001602 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001603 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001604}
1605
1606/// getFunctionType - Return a normal function type with a typed argument
1607/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001608QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001609 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001610 unsigned TypeQuals, bool hasExceptionSpec,
1611 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001612 const QualType *ExArray,
1613 const FunctionType::ExtInfo &Info) {
1614 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001615 // Unique functions, to guarantee there is only one function of a particular
1616 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001617 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001618 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001619 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001620 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001621
1622 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001623 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001624 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001625 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001626
1627 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001628 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001629 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001630 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001631 isCanonical = false;
1632
1633 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001634 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001635 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001636 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001637 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001638 CanonicalArgs.reserve(NumArgs);
1639 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001640 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001641
Chris Lattner76a00cf2008-04-06 22:59:24 +00001642 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001643 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001644 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001645 false, 0, 0,
1646 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001647
Chris Lattnerfd4de792007-01-27 01:15:32 +00001648 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001649 FunctionProtoType *NewIP =
1650 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001651 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001652 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001653
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001654 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001655 // for two variable size arrays (for parameter and exception types) at the
1656 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001657 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001658 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1659 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001660 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001661 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001662 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001663 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001664 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001665 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001666 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001667}
Chris Lattneref51c202006-11-10 07:17:23 +00001668
John McCalle78aac42010-03-10 03:28:59 +00001669#ifndef NDEBUG
1670static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1671 if (!isa<CXXRecordDecl>(D)) return false;
1672 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1673 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1674 return true;
1675 if (RD->getDescribedClassTemplate() &&
1676 !isa<ClassTemplateSpecializationDecl>(RD))
1677 return true;
1678 return false;
1679}
1680#endif
1681
1682/// getInjectedClassNameType - Return the unique reference to the
1683/// injected class name type for the specified templated declaration.
1684QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1685 QualType TST) {
1686 assert(NeedsInjectedClassNameType(Decl));
1687 if (Decl->TypeForDecl) {
1688 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1689 } else if (CXXRecordDecl *PrevDecl
1690 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1691 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1692 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1693 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1694 } else {
1695 Decl->TypeForDecl = new (*this, TypeAlignment)
1696 InjectedClassNameType(Decl, TST, TST->getCanonicalTypeInternal());
1697 Types.push_back(Decl->TypeForDecl);
1698 }
1699 return QualType(Decl->TypeForDecl, 0);
1700}
1701
Douglas Gregor83a586e2008-04-13 21:07:44 +00001702/// getTypeDeclType - Return the unique reference to the type for the
1703/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001704QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001705 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001706 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001707
John McCall81e38502010-02-16 03:57:14 +00001708 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001709 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001710
1711 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001712 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001713 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001714
John McCall96f0b5f2010-03-10 06:48:02 +00001715 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1716 "Template type parameter types are always available.");
1717
John McCall81e38502010-02-16 03:57:14 +00001718 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001719 assert(!Record->getPreviousDeclaration() &&
1720 "struct/union has previous declaration");
1721 assert(!NeedsInjectedClassNameType(Record));
1722 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001723 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001724 assert(!Enum->getPreviousDeclaration() &&
1725 "enum has previous declaration");
1726 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001727 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001728 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1729 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001730 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001731 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001732
John McCall96f0b5f2010-03-10 06:48:02 +00001733 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001734 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001735}
1736
Chris Lattner32d920b2007-01-26 02:01:53 +00001737/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001738/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001739QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001740 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001741
Chris Lattner76a00cf2008-04-06 22:59:24 +00001742 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001743 Decl->TypeForDecl = new(*this, TypeAlignment)
1744 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001745 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001746 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001747}
1748
John McCallcebee162009-10-18 09:09:24 +00001749/// \brief Retrieve a substitution-result type.
1750QualType
1751ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1752 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001753 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001754 && "replacement types must always be canonical");
1755
1756 llvm::FoldingSetNodeID ID;
1757 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1758 void *InsertPos = 0;
1759 SubstTemplateTypeParmType *SubstParm
1760 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1761
1762 if (!SubstParm) {
1763 SubstParm = new (*this, TypeAlignment)
1764 SubstTemplateTypeParmType(Parm, Replacement);
1765 Types.push_back(SubstParm);
1766 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1767 }
1768
1769 return QualType(SubstParm, 0);
1770}
1771
Douglas Gregoreff93e02009-02-05 23:33:38 +00001772/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001773/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001774/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001775QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001776 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001777 IdentifierInfo *Name) {
1778 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001779 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001780 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001781 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001782 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1783
1784 if (TypeParm)
1785 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001786
Anders Carlsson90036dc2009-06-16 00:30:48 +00001787 if (Name) {
1788 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001789 TypeParm = new (*this, TypeAlignment)
1790 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001791
1792 TemplateTypeParmType *TypeCheck
1793 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1794 assert(!TypeCheck && "Template type parameter canonical type broken");
1795 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001796 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001797 TypeParm = new (*this, TypeAlignment)
1798 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001799
1800 Types.push_back(TypeParm);
1801 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1802
1803 return QualType(TypeParm, 0);
1804}
1805
John McCalle78aac42010-03-10 03:28:59 +00001806TypeSourceInfo *
1807ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1808 SourceLocation NameLoc,
1809 const TemplateArgumentListInfo &Args,
1810 QualType CanonType) {
1811 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1812
1813 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1814 TemplateSpecializationTypeLoc TL
1815 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1816 TL.setTemplateNameLoc(NameLoc);
1817 TL.setLAngleLoc(Args.getLAngleLoc());
1818 TL.setRAngleLoc(Args.getRAngleLoc());
1819 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1820 TL.setArgLocInfo(i, Args[i].getLocInfo());
1821 return DI;
1822}
1823
Mike Stump11289f42009-09-09 15:08:12 +00001824QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001825ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001826 const TemplateArgumentListInfo &Args,
John McCall0ad16662009-10-29 08:12:44 +00001827 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00001828 unsigned NumArgs = Args.size();
1829
John McCall0ad16662009-10-29 08:12:44 +00001830 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1831 ArgVec.reserve(NumArgs);
1832 for (unsigned i = 0; i != NumArgs; ++i)
1833 ArgVec.push_back(Args[i].getArgument());
1834
1835 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon);
1836}
1837
1838QualType
1839ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001840 const TemplateArgument *Args,
1841 unsigned NumArgs,
1842 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001843 if (!Canon.isNull())
1844 Canon = getCanonicalType(Canon);
1845 else {
1846 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001847 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1848 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1849 CanonArgs.reserve(NumArgs);
1850 for (unsigned I = 0; I != NumArgs; ++I)
1851 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1852
1853 // Determine whether this canonical template specialization type already
1854 // exists.
1855 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001856 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor00044172009-07-29 16:09:57 +00001857 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001858
1859 void *InsertPos = 0;
1860 TemplateSpecializationType *Spec
1861 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001862
Douglas Gregora8e02e72009-07-28 23:00:59 +00001863 if (!Spec) {
1864 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001865 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001866 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001867 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001868 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001869 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001870 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001871 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001872 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001873 }
Mike Stump11289f42009-09-09 15:08:12 +00001874
Douglas Gregor15301382009-07-30 17:40:51 +00001875 if (Canon.isNull())
1876 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001877 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001878 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001879 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001880
Douglas Gregora8e02e72009-07-28 23:00:59 +00001881 // Allocate the (non-canonical) template specialization type, but don't
1882 // try to unique it: these types typically have location information that
1883 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001884 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001885 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001886 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001887 TemplateSpecializationType *Spec
1888 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001889 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001890
Douglas Gregor8bf42052009-02-09 18:46:07 +00001891 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001892 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001893}
1894
Mike Stump11289f42009-09-09 15:08:12 +00001895QualType
Douglas Gregorf21eb492009-03-26 23:50:42 +00001896ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor52537682009-03-19 00:18:19 +00001897 QualType NamedType) {
1898 llvm::FoldingSetNodeID ID;
Douglas Gregorf21eb492009-03-26 23:50:42 +00001899 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001900
1901 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001902 QualifiedNameType *T
Douglas Gregor52537682009-03-19 00:18:19 +00001903 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1904 if (T)
1905 return QualType(T, 0);
1906
Douglas Gregorc42075a2010-02-04 18:10:26 +00001907 QualType Canon = NamedType;
1908 if (!Canon.isCanonical()) {
1909 Canon = getCanonicalType(NamedType);
1910 QualifiedNameType *CheckT
1911 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1912 assert(!CheckT && "Qualified name canonical type broken");
1913 (void)CheckT;
1914 }
1915
1916 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001917 Types.push_back(T);
1918 QualifiedNameTypes.InsertNode(T, InsertPos);
1919 return QualType(T, 0);
1920}
1921
Douglas Gregor02085352010-03-31 20:19:30 +00001922QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1923 NestedNameSpecifier *NNS,
1924 const IdentifierInfo *Name,
1925 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001926 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1927
1928 if (Canon.isNull()) {
1929 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001930 ElaboratedTypeKeyword CanonKeyword = Keyword;
1931 if (Keyword == ETK_None)
1932 CanonKeyword = ETK_Typename;
1933
1934 if (CanonNNS != NNS || CanonKeyword != Keyword)
1935 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001936 }
1937
1938 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001939 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001940
1941 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001942 DependentNameType *T
1943 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001944 if (T)
1945 return QualType(T, 0);
1946
Douglas Gregor02085352010-03-31 20:19:30 +00001947 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00001948 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001949 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001950 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001951}
1952
Mike Stump11289f42009-09-09 15:08:12 +00001953QualType
Douglas Gregor02085352010-03-31 20:19:30 +00001954ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1955 NestedNameSpecifier *NNS,
1956 const TemplateSpecializationType *TemplateId,
1957 QualType Canon) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001958 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1959
Douglas Gregorc42075a2010-02-04 18:10:26 +00001960 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001961 DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001962
1963 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001964 DependentNameType *T
1965 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001966 if (T)
1967 return QualType(T, 0);
1968
Douglas Gregordce2b622009-04-01 00:28:59 +00001969 if (Canon.isNull()) {
1970 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1971 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
Douglas Gregor02085352010-03-31 20:19:30 +00001972 ElaboratedTypeKeyword CanonKeyword = Keyword;
1973 if (Keyword == ETK_None)
1974 CanonKeyword = ETK_Typename;
1975 if (CanonNNS != NNS || CanonKeyword != Keyword ||
1976 CanonType != QualType(TemplateId, 0)) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001977 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00001978 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00001979 assert(CanonTemplateId &&
1980 "Canonical type must also be a template specialization type");
Douglas Gregor02085352010-03-31 20:19:30 +00001981 Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
Douglas Gregordce2b622009-04-01 00:28:59 +00001982 }
Douglas Gregorc42075a2010-02-04 18:10:26 +00001983
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001984 DependentNameType *CheckT
1985 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001986 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregordce2b622009-04-01 00:28:59 +00001987 }
1988
Douglas Gregor02085352010-03-31 20:19:30 +00001989 T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00001990 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001991 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001992 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00001993}
1994
John McCallfcc33b02009-09-05 00:15:47 +00001995QualType
1996ASTContext::getElaboratedType(QualType UnderlyingType,
1997 ElaboratedType::TagKind Tag) {
1998 llvm::FoldingSetNodeID ID;
1999 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump11289f42009-09-09 15:08:12 +00002000
John McCallfcc33b02009-09-05 00:15:47 +00002001 void *InsertPos = 0;
2002 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2003 if (T)
2004 return QualType(T, 0);
2005
Douglas Gregorc42075a2010-02-04 18:10:26 +00002006 QualType Canon = UnderlyingType;
2007 if (!Canon.isCanonical()) {
2008 Canon = getCanonicalType(Canon);
2009 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2010 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2011 }
John McCallfcc33b02009-09-05 00:15:47 +00002012
2013 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2014 Types.push_back(T);
2015 ElaboratedTypes.InsertNode(T, InsertPos);
2016 return QualType(T, 0);
2017}
2018
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002019/// CmpProtocolNames - Comparison predicate for sorting protocols
2020/// alphabetically.
2021static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2022 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002023 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002024}
2025
John McCallfc93cf92009-10-22 22:37:11 +00002026static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2027 unsigned NumProtocols) {
2028 if (NumProtocols == 0) return true;
2029
2030 for (unsigned i = 1; i != NumProtocols; ++i)
2031 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2032 return false;
2033 return true;
2034}
2035
2036static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002037 unsigned &NumProtocols) {
2038 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002039
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002040 // Sort protocols, keyed by name.
2041 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2042
2043 // Remove duplicates.
2044 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2045 NumProtocols = ProtocolsEnd-Protocols;
2046}
2047
Steve Narofffb4330f2009-06-17 22:40:22 +00002048/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2049/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002050QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00002051 ObjCProtocolDecl **Protocols,
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002052 unsigned NumProtocols,
2053 unsigned Quals) {
Steve Narofffb4330f2009-06-17 22:40:22 +00002054 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002055 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002056 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Narofffb4330f2009-06-17 22:40:22 +00002057
2058 void *InsertPos = 0;
2059 if (ObjCObjectPointerType *QT =
2060 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002061 return getQualifiedType(QualType(QT, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002062
John McCallfc93cf92009-10-22 22:37:11 +00002063 // Sort the protocol list alphabetically to canonicalize it.
2064 QualType Canonical;
2065 if (!InterfaceT.isCanonical() ||
2066 !areSortedAndUniqued(Protocols, NumProtocols)) {
2067 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
2068 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2069 unsigned UniqueCount = NumProtocols;
2070
2071 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2072 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2073
2074 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2075 &Sorted[0], UniqueCount);
2076 } else {
2077 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2078 Protocols, NumProtocols);
2079 }
2080
2081 // Regenerate InsertPos.
2082 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2083 }
2084
Douglas Gregorf85bee62010-02-08 22:59:26 +00002085 // No match.
2086 unsigned Size = sizeof(ObjCObjectPointerType)
2087 + NumProtocols * sizeof(ObjCProtocolDecl *);
2088 void *Mem = Allocate(Size, TypeAlignment);
2089 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2090 InterfaceT,
2091 Protocols,
2092 NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002093
Steve Narofffb4330f2009-06-17 22:40:22 +00002094 Types.push_back(QType);
2095 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002096 return getQualifiedType(QualType(QType, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002097}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002098
Steve Naroffc277ad12009-07-18 15:33:26 +00002099/// getObjCInterfaceType - Return the unique reference to the type for the
2100/// specified ObjC interface decl. The list of protocols is optional.
2101QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002102 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002103 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00002104 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002105
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002106 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00002107 if (ObjCInterfaceType *QT =
2108 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002109 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002110
John McCallfc93cf92009-10-22 22:37:11 +00002111 // Sort the protocol list alphabetically to canonicalize it.
2112 QualType Canonical;
2113 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2114 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2115 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2116
2117 unsigned UniqueCount = NumProtocols;
2118 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2119
2120 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2121
2122 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2123 }
2124
Douglas Gregorf85bee62010-02-08 22:59:26 +00002125 unsigned Size = sizeof(ObjCInterfaceType)
2126 + NumProtocols * sizeof(ObjCProtocolDecl *);
2127 void *Mem = Allocate(Size, TypeAlignment);
2128 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2129 const_cast<ObjCInterfaceDecl*>(Decl),
2130 Protocols,
2131 NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002132
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002133 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00002134 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002135 return QualType(QType, 0);
2136}
2137
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002138/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2139/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002140/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002141/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002142/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002143QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002144 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002145 if (tofExpr->isTypeDependent()) {
2146 llvm::FoldingSetNodeID ID;
2147 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002148
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002149 void *InsertPos = 0;
2150 DependentTypeOfExprType *Canon
2151 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2152 if (Canon) {
2153 // We already have a "canonical" version of an identical, dependent
2154 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002155 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002156 QualType((TypeOfExprType*)Canon, 0));
2157 }
2158 else {
2159 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002160 Canon
2161 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002162 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2163 toe = Canon;
2164 }
2165 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002166 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002167 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002168 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002169 Types.push_back(toe);
2170 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002171}
2172
Steve Naroffa773cd52007-08-01 18:02:17 +00002173/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2174/// TypeOfType AST's. The only motivation to unique these nodes would be
2175/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002176/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002177/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002178QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002179 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002180 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002181 Types.push_back(tot);
2182 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002183}
2184
Anders Carlssonad6bd352009-06-24 21:24:56 +00002185/// getDecltypeForExpr - Given an expr, will return the decltype for that
2186/// expression, according to the rules in C++0x [dcl.type.simple]p4
2187static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002188 if (e->isTypeDependent())
2189 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002190
Anders Carlssonad6bd352009-06-24 21:24:56 +00002191 // If e is an id expression or a class member access, decltype(e) is defined
2192 // as the type of the entity named by e.
2193 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2194 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2195 return VD->getType();
2196 }
2197 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2198 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2199 return FD->getType();
2200 }
2201 // If e is a function call or an invocation of an overloaded operator,
2202 // (parentheses around e are ignored), decltype(e) is defined as the
2203 // return type of that function.
2204 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2205 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002206
Anders Carlssonad6bd352009-06-24 21:24:56 +00002207 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002208
2209 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002210 // defined as T&, otherwise decltype(e) is defined as T.
2211 if (e->isLvalue(Context) == Expr::LV_Valid)
2212 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002213
Anders Carlssonad6bd352009-06-24 21:24:56 +00002214 return T;
2215}
2216
Anders Carlsson81df7b82009-06-24 19:06:50 +00002217/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2218/// DecltypeType AST's. The only motivation to unique these nodes would be
2219/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002220/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002221/// on canonical type's (which are always unique).
2222QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002223 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002224 if (e->isTypeDependent()) {
2225 llvm::FoldingSetNodeID ID;
2226 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002227
Douglas Gregora21f6c32009-07-30 23:36:40 +00002228 void *InsertPos = 0;
2229 DependentDecltypeType *Canon
2230 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2231 if (Canon) {
2232 // We already have a "canonical" version of an equivalent, dependent
2233 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002234 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002235 QualType((DecltypeType*)Canon, 0));
2236 }
2237 else {
2238 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002239 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002240 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2241 dt = Canon;
2242 }
2243 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002244 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002245 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002246 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002247 Types.push_back(dt);
2248 return QualType(dt, 0);
2249}
2250
Chris Lattnerfb072462007-01-23 05:45:31 +00002251/// getTagDeclType - Return the unique reference to the type for the
2252/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002253QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002254 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002255 // FIXME: What is the design on getTagDeclType when it requires casting
2256 // away const? mutable?
2257 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002258}
2259
Mike Stump11289f42009-09-09 15:08:12 +00002260/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2261/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2262/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002263CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002264 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002265}
Chris Lattnerfb072462007-01-23 05:45:31 +00002266
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002267/// getSignedWCharType - Return the type of "signed wchar_t".
2268/// Used when in C++, as a GCC extension.
2269QualType ASTContext::getSignedWCharType() const {
2270 // FIXME: derive from "Target" ?
2271 return WCharTy;
2272}
2273
2274/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2275/// Used when in C++, as a GCC extension.
2276QualType ASTContext::getUnsignedWCharType() const {
2277 // FIXME: derive from "Target" ?
2278 return UnsignedIntTy;
2279}
2280
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002281/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2282/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2283QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002284 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002285}
2286
Chris Lattnera21ad802008-04-02 05:18:44 +00002287//===----------------------------------------------------------------------===//
2288// Type Operators
2289//===----------------------------------------------------------------------===//
2290
John McCallfc93cf92009-10-22 22:37:11 +00002291CanQualType ASTContext::getCanonicalParamType(QualType T) {
2292 // Push qualifiers into arrays, and then discard any remaining
2293 // qualifiers.
2294 T = getCanonicalType(T);
2295 const Type *Ty = T.getTypePtr();
2296
2297 QualType Result;
2298 if (isa<ArrayType>(Ty)) {
2299 Result = getArrayDecayedType(QualType(Ty,0));
2300 } else if (isa<FunctionType>(Ty)) {
2301 Result = getPointerType(QualType(Ty, 0));
2302 } else {
2303 Result = QualType(Ty, 0);
2304 }
2305
2306 return CanQualType::CreateUnsafe(Result);
2307}
2308
Chris Lattnered0d0792008-04-06 22:41:35 +00002309/// getCanonicalType - Return the canonical (structural) type corresponding to
2310/// the specified potentially non-canonical type. The non-canonical version
2311/// of a type may have many "decorated" versions of types. Decorators can
2312/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2313/// to be free of any of these, allowing two canonical types to be compared
2314/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002315CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002316 QualifierCollector Quals;
2317 const Type *Ptr = Quals.strip(T);
2318 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002319
John McCall8ccfcb52009-09-24 19:53:00 +00002320 // The canonical internal type will be the canonical type *except*
2321 // that we push type qualifiers down through array types.
2322
2323 // If there are no new qualifiers to push down, stop here.
2324 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002325 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002326
John McCall8ccfcb52009-09-24 19:53:00 +00002327 // If the type qualifiers are on an array type, get the canonical
2328 // type of the array with the qualifiers applied to the element
2329 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002330 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2331 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002332 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002333
Chris Lattner7adf0762008-08-04 07:31:14 +00002334 // Get the canonical version of the element with the extra qualifiers on it.
2335 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002336 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002337 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002338
Chris Lattner7adf0762008-08-04 07:31:14 +00002339 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002340 return CanQualType::CreateUnsafe(
2341 getConstantArrayType(NewEltTy, CAT->getSize(),
2342 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002343 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002344 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002345 return CanQualType::CreateUnsafe(
2346 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002347 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002348
Douglas Gregor4619e432008-12-05 23:32:09 +00002349 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002350 return CanQualType::CreateUnsafe(
2351 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002352 DSAT->getSizeExpr() ?
2353 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002354 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002355 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002356 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002357
Chris Lattner7adf0762008-08-04 07:31:14 +00002358 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002359 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002360 VAT->getSizeExpr() ?
2361 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002362 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002363 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002364 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002365}
2366
Chandler Carruth607f38e2009-12-29 07:16:59 +00002367QualType ASTContext::getUnqualifiedArrayType(QualType T,
2368 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002369 Quals = T.getQualifiers();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002370 if (!isa<ArrayType>(T)) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002371 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002372 }
2373
Chandler Carruth607f38e2009-12-29 07:16:59 +00002374 const ArrayType *AT = cast<ArrayType>(T);
2375 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002376 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002377 if (Elt == UnqualElt)
2378 return T;
2379
2380 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2381 return getConstantArrayType(UnqualElt, CAT->getSize(),
2382 CAT->getSizeModifier(), 0);
2383 }
2384
2385 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2386 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2387 }
2388
2389 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2390 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2391 DSAT->getSizeModifier(), 0,
2392 SourceRange());
2393}
2394
John McCall847e2a12009-11-24 18:42:40 +00002395DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2396 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2397 return TD->getDeclName();
2398
2399 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2400 if (DTN->isIdentifier()) {
2401 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2402 } else {
2403 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2404 }
2405 }
2406
John McCalld28ae272009-12-02 08:04:21 +00002407 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2408 assert(Storage);
2409 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002410}
2411
Douglas Gregor6bc50582009-05-07 06:41:52 +00002412TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2413 // If this template name refers to a template, the canonical
2414 // template name merely stores the template itself.
2415 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002416 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002417
John McCalld28ae272009-12-02 08:04:21 +00002418 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002419
Douglas Gregor6bc50582009-05-07 06:41:52 +00002420 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2421 assert(DTN && "Non-dependent template names must refer to template decls.");
2422 return DTN->CanonicalTemplateName;
2423}
2424
Douglas Gregoradee3e32009-11-11 23:06:43 +00002425bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2426 X = getCanonicalTemplateName(X);
2427 Y = getCanonicalTemplateName(Y);
2428 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2429}
2430
Mike Stump11289f42009-09-09 15:08:12 +00002431TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002432ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2433 switch (Arg.getKind()) {
2434 case TemplateArgument::Null:
2435 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002436
Douglas Gregora8e02e72009-07-28 23:00:59 +00002437 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002438 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002439
Douglas Gregora8e02e72009-07-28 23:00:59 +00002440 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002441 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002442
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002443 case TemplateArgument::Template:
2444 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2445
Douglas Gregora8e02e72009-07-28 23:00:59 +00002446 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002447 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002448 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002449
Douglas Gregora8e02e72009-07-28 23:00:59 +00002450 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002451 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002452
Douglas Gregora8e02e72009-07-28 23:00:59 +00002453 case TemplateArgument::Pack: {
2454 // FIXME: Allocate in ASTContext
2455 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2456 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002457 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002458 AEnd = Arg.pack_end();
2459 A != AEnd; (void)++A, ++Idx)
2460 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002461
Douglas Gregora8e02e72009-07-28 23:00:59 +00002462 TemplateArgument Result;
2463 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2464 return Result;
2465 }
2466 }
2467
2468 // Silence GCC warning
2469 assert(false && "Unhandled template argument kind");
2470 return TemplateArgument();
2471}
2472
Douglas Gregor333489b2009-03-27 23:10:48 +00002473NestedNameSpecifier *
2474ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002475 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002476 return 0;
2477
2478 switch (NNS->getKind()) {
2479 case NestedNameSpecifier::Identifier:
2480 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002481 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002482 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2483 NNS->getAsIdentifier());
2484
2485 case NestedNameSpecifier::Namespace:
2486 // A namespace is canonical; build a nested-name-specifier with
2487 // this namespace and no prefix.
2488 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2489
2490 case NestedNameSpecifier::TypeSpec:
2491 case NestedNameSpecifier::TypeSpecWithTemplate: {
2492 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002493 return NestedNameSpecifier::Create(*this, 0,
2494 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002495 T.getTypePtr());
2496 }
2497
2498 case NestedNameSpecifier::Global:
2499 // The global specifier is canonical and unique.
2500 return NNS;
2501 }
2502
2503 // Required to silence a GCC warning
2504 return 0;
2505}
2506
Chris Lattner7adf0762008-08-04 07:31:14 +00002507
2508const ArrayType *ASTContext::getAsArrayType(QualType T) {
2509 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002510 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002511 // Handle the common positive case fast.
2512 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2513 return AT;
2514 }
Mike Stump11289f42009-09-09 15:08:12 +00002515
John McCall8ccfcb52009-09-24 19:53:00 +00002516 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002517 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002518 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002519 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002520
John McCall8ccfcb52009-09-24 19:53:00 +00002521 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002522 // implements C99 6.7.3p8: "If the specification of an array type includes
2523 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002524
Chris Lattner7adf0762008-08-04 07:31:14 +00002525 // If we get here, we either have type qualifiers on the type, or we have
2526 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002527 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002528
John McCall8ccfcb52009-09-24 19:53:00 +00002529 QualifierCollector Qs;
2530 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002531
Chris Lattner7adf0762008-08-04 07:31:14 +00002532 // If we have a simple case, just return now.
2533 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002534 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002535 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002536
Chris Lattner7adf0762008-08-04 07:31:14 +00002537 // Otherwise, we have an array and we have qualifiers on it. Push the
2538 // qualifiers into the array element type and return a new array type.
2539 // Get the canonical version of the element with the extra qualifiers on it.
2540 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002541 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002542
Chris Lattner7adf0762008-08-04 07:31:14 +00002543 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2544 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2545 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002546 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002547 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2548 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2549 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002550 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002551
Mike Stump11289f42009-09-09 15:08:12 +00002552 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002553 = dyn_cast<DependentSizedArrayType>(ATy))
2554 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002555 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002556 DSAT->getSizeExpr() ?
2557 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002558 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002559 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002560 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002561
Chris Lattner7adf0762008-08-04 07:31:14 +00002562 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002563 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002564 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002565 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002566 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002567 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002568 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002569}
2570
2571
Chris Lattnera21ad802008-04-02 05:18:44 +00002572/// getArrayDecayedType - Return the properly qualified result of decaying the
2573/// specified array type to a pointer. This operation is non-trivial when
2574/// handling typedefs etc. The canonical type of "T" must be an array type,
2575/// this returns a pointer to a properly qualified element of the array.
2576///
2577/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2578QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002579 // Get the element type with 'getAsArrayType' so that we don't lose any
2580 // typedefs in the element type of the array. This also handles propagation
2581 // of type qualifiers from the array type into the element type if present
2582 // (C99 6.7.3p8).
2583 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2584 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002585
Chris Lattner7adf0762008-08-04 07:31:14 +00002586 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002587
2588 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002589 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002590}
2591
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002592QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002593 QualifierCollector Qs;
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002594 while (true) {
John McCall8ccfcb52009-09-24 19:53:00 +00002595 const Type *UT = Qs.strip(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002596 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2597 QT = AT->getElementType();
Mike Stumpea086c72009-07-25 23:24:03 +00002598 } else {
John McCall8ccfcb52009-09-24 19:53:00 +00002599 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002600 }
2601 }
2602}
2603
Anders Carlsson4bf82142009-09-25 01:23:32 +00002604QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2605 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002606
Anders Carlsson4bf82142009-09-25 01:23:32 +00002607 if (const ArrayType *AT = getAsArrayType(ElemTy))
2608 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002609
Anders Carlssone0808df2008-12-21 03:44:36 +00002610 return ElemTy;
2611}
2612
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002613/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002614uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002615ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2616 uint64_t ElementCount = 1;
2617 do {
2618 ElementCount *= CA->getSize().getZExtValue();
2619 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2620 } while (CA);
2621 return ElementCount;
2622}
2623
Steve Naroff0af91202007-04-27 21:51:21 +00002624/// getFloatingRank - Return a relative rank for floating point types.
2625/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002626static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002627 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002628 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002629
John McCall9dd450b2009-09-21 23:43:11 +00002630 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2631 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002632 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002633 case BuiltinType::Float: return FloatRank;
2634 case BuiltinType::Double: return DoubleRank;
2635 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002636 }
2637}
2638
Mike Stump11289f42009-09-09 15:08:12 +00002639/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2640/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002641/// 'typeDomain' is a real floating point or complex type.
2642/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002643QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2644 QualType Domain) const {
2645 FloatingRank EltRank = getFloatingRank(Size);
2646 if (Domain->isComplexType()) {
2647 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002648 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002649 case FloatRank: return FloatComplexTy;
2650 case DoubleRank: return DoubleComplexTy;
2651 case LongDoubleRank: return LongDoubleComplexTy;
2652 }
Steve Naroff0af91202007-04-27 21:51:21 +00002653 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002654
2655 assert(Domain->isRealFloatingType() && "Unknown domain!");
2656 switch (EltRank) {
2657 default: assert(0 && "getFloatingRank(): illegal value for rank");
2658 case FloatRank: return FloatTy;
2659 case DoubleRank: return DoubleTy;
2660 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002661 }
Steve Naroffe4718892007-04-27 18:30:00 +00002662}
2663
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002664/// getFloatingTypeOrder - Compare the rank of the two specified floating
2665/// point types, ignoring the domain of the type (i.e. 'double' ==
2666/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002667/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002668int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2669 FloatingRank LHSR = getFloatingRank(LHS);
2670 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002671
Chris Lattnerb90739d2008-04-06 23:38:49 +00002672 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002673 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002674 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002675 return 1;
2676 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002677}
2678
Chris Lattner76a00cf2008-04-06 22:59:24 +00002679/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2680/// routine will assert if passed a built-in type that isn't an integer or enum,
2681/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002682unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002683 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002684 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002685 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002686
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002687 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2688 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2689
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002690 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2691 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2692
2693 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2694 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2695
Chris Lattner76a00cf2008-04-06 22:59:24 +00002696 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002697 default: assert(0 && "getIntegerRank(): not a built-in integer");
2698 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002699 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002700 case BuiltinType::Char_S:
2701 case BuiltinType::Char_U:
2702 case BuiltinType::SChar:
2703 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002704 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002705 case BuiltinType::Short:
2706 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002707 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002708 case BuiltinType::Int:
2709 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002710 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002711 case BuiltinType::Long:
2712 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002713 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002714 case BuiltinType::LongLong:
2715 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002716 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002717 case BuiltinType::Int128:
2718 case BuiltinType::UInt128:
2719 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002720 }
2721}
2722
Eli Friedman629ffb92009-08-20 04:21:42 +00002723/// \brief Whether this is a promotable bitfield reference according
2724/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2725///
2726/// \returns the type this bit-field will promote to, or NULL if no
2727/// promotion occurs.
2728QualType ASTContext::isPromotableBitField(Expr *E) {
2729 FieldDecl *Field = E->getBitField();
2730 if (!Field)
2731 return QualType();
2732
2733 QualType FT = Field->getType();
2734
2735 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2736 uint64_t BitWidth = BitWidthAP.getZExtValue();
2737 uint64_t IntSize = getTypeSize(IntTy);
2738 // GCC extension compatibility: if the bit-field size is less than or equal
2739 // to the size of int, it gets promoted no matter what its type is.
2740 // For instance, unsigned long bf : 4 gets promoted to signed int.
2741 if (BitWidth < IntSize)
2742 return IntTy;
2743
2744 if (BitWidth == IntSize)
2745 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2746
2747 // Types bigger than int are not subject to promotions, and therefore act
2748 // like the base type.
2749 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2750 // is ridiculous.
2751 return QualType();
2752}
2753
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002754/// getPromotedIntegerType - Returns the type that Promotable will
2755/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2756/// integer type.
2757QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2758 assert(!Promotable.isNull());
2759 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002760 if (const EnumType *ET = Promotable->getAs<EnumType>())
2761 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002762 if (Promotable->isSignedIntegerType())
2763 return IntTy;
2764 uint64_t PromotableSize = getTypeSize(Promotable);
2765 uint64_t IntSize = getTypeSize(IntTy);
2766 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2767 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2768}
2769
Mike Stump11289f42009-09-09 15:08:12 +00002770/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002771/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002772/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002773int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002774 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2775 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002776 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002777
Chris Lattner76a00cf2008-04-06 22:59:24 +00002778 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2779 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002780
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002781 unsigned LHSRank = getIntegerRank(LHSC);
2782 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002783
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002784 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2785 if (LHSRank == RHSRank) return 0;
2786 return LHSRank > RHSRank ? 1 : -1;
2787 }
Mike Stump11289f42009-09-09 15:08:12 +00002788
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002789 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2790 if (LHSUnsigned) {
2791 // If the unsigned [LHS] type is larger, return it.
2792 if (LHSRank >= RHSRank)
2793 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002795 // If the signed type can represent all values of the unsigned type, it
2796 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002797 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002798 return -1;
2799 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002800
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002801 // If the unsigned [RHS] type is larger, return it.
2802 if (RHSRank >= LHSRank)
2803 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002804
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002805 // If the signed type can represent all values of the unsigned type, it
2806 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002807 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002808 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002809}
Anders Carlsson98f07902007-08-17 05:31:46 +00002810
Anders Carlsson6d417272009-11-14 21:45:58 +00002811static RecordDecl *
2812CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2813 SourceLocation L, IdentifierInfo *Id) {
2814 if (Ctx.getLangOptions().CPlusPlus)
2815 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2816 else
2817 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2818}
2819
Mike Stump11289f42009-09-09 15:08:12 +00002820// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002821QualType ASTContext::getCFConstantStringType() {
2822 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002823 CFConstantStringTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002824 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2825 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002826 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002827
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002828 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002829
Anders Carlsson98f07902007-08-17 05:31:46 +00002830 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002831 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002832 // int flags;
2833 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002834 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002835 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002836 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002837 FieldTypes[3] = LongTy;
2838
Anders Carlsson98f07902007-08-17 05:31:46 +00002839 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002840 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002841 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002842 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002843 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002844 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002845 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002846 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002847 }
2848
Douglas Gregord5058122010-02-11 01:19:42 +00002849 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002850 }
Mike Stump11289f42009-09-09 15:08:12 +00002851
Anders Carlsson98f07902007-08-17 05:31:46 +00002852 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002853}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002854
Douglas Gregor512b0772009-04-23 22:29:11 +00002855void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002856 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002857 assert(Rec && "Invalid CFConstantStringType");
2858 CFConstantStringTypeDecl = Rec->getDecl();
2859}
2860
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002861// getNSConstantStringType - Return the type used for constant NSStrings.
2862QualType ASTContext::getNSConstantStringType() {
2863 if (!NSConstantStringTypeDecl) {
2864 NSConstantStringTypeDecl =
2865 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2866 &Idents.get("__builtin_NSString"));
2867 NSConstantStringTypeDecl->startDefinition();
2868
2869 QualType FieldTypes[3];
2870
2871 // const int *isa;
2872 FieldTypes[0] = getPointerType(IntTy.withConst());
2873 // const char *str;
2874 FieldTypes[1] = getPointerType(CharTy.withConst());
2875 // unsigned int length;
2876 FieldTypes[2] = UnsignedIntTy;
2877
2878 // Create fields
2879 for (unsigned i = 0; i < 3; ++i) {
2880 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2881 SourceLocation(), 0,
2882 FieldTypes[i], /*TInfo=*/0,
2883 /*BitWidth=*/0,
2884 /*Mutable=*/false);
2885 NSConstantStringTypeDecl->addDecl(Field);
2886 }
2887
2888 NSConstantStringTypeDecl->completeDefinition();
2889 }
2890
2891 return getTagDeclType(NSConstantStringTypeDecl);
2892}
2893
2894void ASTContext::setNSConstantStringType(QualType T) {
2895 const RecordType *Rec = T->getAs<RecordType>();
2896 assert(Rec && "Invalid NSConstantStringType");
2897 NSConstantStringTypeDecl = Rec->getDecl();
2898}
2899
Mike Stump11289f42009-09-09 15:08:12 +00002900QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002901 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002902 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002903 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2904 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00002905 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002906
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002907 QualType FieldTypes[] = {
2908 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002909 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002910 getPointerType(UnsignedLongTy),
2911 getConstantArrayType(UnsignedLongTy,
2912 llvm::APInt(32, 5), ArrayType::Normal, 0)
2913 };
Mike Stump11289f42009-09-09 15:08:12 +00002914
Douglas Gregor91f84212008-12-11 16:49:14 +00002915 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002916 FieldDecl *Field = FieldDecl::Create(*this,
2917 ObjCFastEnumerationStateTypeDecl,
2918 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002919 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002920 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002921 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002922 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002923 }
Mike Stump11289f42009-09-09 15:08:12 +00002924
Douglas Gregord5058122010-02-11 01:19:42 +00002925 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002926 }
Mike Stump11289f42009-09-09 15:08:12 +00002927
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002928 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2929}
2930
Mike Stumpd0153282009-10-20 02:12:22 +00002931QualType ASTContext::getBlockDescriptorType() {
2932 if (BlockDescriptorType)
2933 return getTagDeclType(BlockDescriptorType);
2934
2935 RecordDecl *T;
2936 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002937 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2938 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00002939 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002940
2941 QualType FieldTypes[] = {
2942 UnsignedLongTy,
2943 UnsignedLongTy,
2944 };
2945
2946 const char *FieldNames[] = {
2947 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002948 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002949 };
2950
2951 for (size_t i = 0; i < 2; ++i) {
2952 FieldDecl *Field = FieldDecl::Create(*this,
2953 T,
2954 SourceLocation(),
2955 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00002956 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00002957 /*BitWidth=*/0,
2958 /*Mutable=*/false);
2959 T->addDecl(Field);
2960 }
2961
Douglas Gregord5058122010-02-11 01:19:42 +00002962 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002963
2964 BlockDescriptorType = T;
2965
2966 return getTagDeclType(BlockDescriptorType);
2967}
2968
2969void ASTContext::setBlockDescriptorType(QualType T) {
2970 const RecordType *Rec = T->getAs<RecordType>();
2971 assert(Rec && "Invalid BlockDescriptorType");
2972 BlockDescriptorType = Rec->getDecl();
2973}
2974
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002975QualType ASTContext::getBlockDescriptorExtendedType() {
2976 if (BlockDescriptorExtendedType)
2977 return getTagDeclType(BlockDescriptorExtendedType);
2978
2979 RecordDecl *T;
2980 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002981 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2982 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00002983 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002984
2985 QualType FieldTypes[] = {
2986 UnsignedLongTy,
2987 UnsignedLongTy,
2988 getPointerType(VoidPtrTy),
2989 getPointerType(VoidPtrTy)
2990 };
2991
2992 const char *FieldNames[] = {
2993 "reserved",
2994 "Size",
2995 "CopyFuncPtr",
2996 "DestroyFuncPtr"
2997 };
2998
2999 for (size_t i = 0; i < 4; ++i) {
3000 FieldDecl *Field = FieldDecl::Create(*this,
3001 T,
3002 SourceLocation(),
3003 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003004 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003005 /*BitWidth=*/0,
3006 /*Mutable=*/false);
3007 T->addDecl(Field);
3008 }
3009
Douglas Gregord5058122010-02-11 01:19:42 +00003010 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003011
3012 BlockDescriptorExtendedType = T;
3013
3014 return getTagDeclType(BlockDescriptorExtendedType);
3015}
3016
3017void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3018 const RecordType *Rec = T->getAs<RecordType>();
3019 assert(Rec && "Invalid BlockDescriptorType");
3020 BlockDescriptorExtendedType = Rec->getDecl();
3021}
3022
Mike Stump94967902009-10-21 18:16:27 +00003023bool ASTContext::BlockRequiresCopying(QualType Ty) {
3024 if (Ty->isBlockPointerType())
3025 return true;
3026 if (isObjCNSObjectType(Ty))
3027 return true;
3028 if (Ty->isObjCObjectPointerType())
3029 return true;
3030 return false;
3031}
3032
3033QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3034 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003035 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003036 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003037 // unsigned int __flags;
3038 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003039 // void *__copy_helper; // as needed
3040 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003041 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003042 // } *
3043
Mike Stump94967902009-10-21 18:16:27 +00003044 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3045
3046 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003047 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003048 llvm::SmallString<36> Name;
3049 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3050 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003051 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003052 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3053 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003054 T->startDefinition();
3055 QualType Int32Ty = IntTy;
3056 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3057 QualType FieldTypes[] = {
3058 getPointerType(VoidPtrTy),
3059 getPointerType(getTagDeclType(T)),
3060 Int32Ty,
3061 Int32Ty,
3062 getPointerType(VoidPtrTy),
3063 getPointerType(VoidPtrTy),
3064 Ty
3065 };
3066
3067 const char *FieldNames[] = {
3068 "__isa",
3069 "__forwarding",
3070 "__flags",
3071 "__size",
3072 "__copy_helper",
3073 "__destroy_helper",
3074 DeclName,
3075 };
3076
3077 for (size_t i = 0; i < 7; ++i) {
3078 if (!HasCopyAndDispose && i >=4 && i <= 5)
3079 continue;
3080 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3081 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003082 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003083 /*BitWidth=*/0, /*Mutable=*/false);
3084 T->addDecl(Field);
3085 }
3086
Douglas Gregord5058122010-02-11 01:19:42 +00003087 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003088
3089 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003090}
3091
3092
3093QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003094 bool BlockHasCopyDispose,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003095 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpd0153282009-10-20 02:12:22 +00003096 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003097 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003098 llvm::SmallString<36> Name;
3099 llvm::raw_svector_ostream(Name) << "__block_literal_"
3100 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003101 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003102 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3103 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003104 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003105 QualType FieldTypes[] = {
3106 getPointerType(VoidPtrTy),
3107 IntTy,
3108 IntTy,
3109 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003110 (BlockHasCopyDispose ?
3111 getPointerType(getBlockDescriptorExtendedType()) :
3112 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003113 };
3114
3115 const char *FieldNames[] = {
3116 "__isa",
3117 "__flags",
3118 "__reserved",
3119 "__FuncPtr",
3120 "__descriptor"
3121 };
3122
3123 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003124 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003125 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003126 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003127 /*BitWidth=*/0, /*Mutable=*/false);
3128 T->addDecl(Field);
3129 }
3130
3131 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3132 const Expr *E = BlockDeclRefDecls[i];
3133 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3134 clang::IdentifierInfo *Name = 0;
3135 if (BDRE) {
3136 const ValueDecl *D = BDRE->getDecl();
3137 Name = &Idents.get(D->getName());
3138 }
3139 QualType FieldType = E->getType();
3140
3141 if (BDRE && BDRE->isByRef())
Mike Stump94967902009-10-21 18:16:27 +00003142 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3143 FieldType);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003144
3145 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallbcd03502009-12-07 02:54:59 +00003146 Name, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003147 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpd0153282009-10-20 02:12:22 +00003148 T->addDecl(Field);
3149 }
3150
Douglas Gregord5058122010-02-11 01:19:42 +00003151 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003152
3153 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003154}
3155
Douglas Gregor512b0772009-04-23 22:29:11 +00003156void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003157 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003158 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3159 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3160}
3161
Anders Carlsson18acd442007-10-29 06:33:42 +00003162// This returns true if a type has been typedefed to BOOL:
3163// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003164static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003165 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003166 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3167 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003168
Anders Carlssond8499822007-10-29 05:01:08 +00003169 return false;
3170}
3171
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003172/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003173/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003174CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003175 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003176
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003177 // Make all integer and enum types at least as large as an int
Ken Dyck40775002010-01-11 17:06:35 +00003178 if (sz.isPositive() && type->isIntegralType())
3179 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003180 // Treat arrays as pointers, since that's how they're passed in.
3181 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003182 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003183 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003184}
3185
3186static inline
3187std::string charUnitsToString(const CharUnits &CU) {
3188 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003189}
3190
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003191/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003192/// declaration.
3193void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3194 std::string& S) {
3195 const BlockDecl *Decl = Expr->getBlockDecl();
3196 QualType BlockTy =
3197 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3198 // Encode result type.
3199 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3200 // Compute size of all parameters.
3201 // Start with computing size of a pointer in number of bytes.
3202 // FIXME: There might(should) be a better way of doing this computation!
3203 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003204 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3205 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003206 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003207 E = Decl->param_end(); PI != E; ++PI) {
3208 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003209 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003210 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003211 ParmOffset += sz;
3212 }
3213 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003214 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003215 // Block pointer and offset.
3216 S += "@?0";
3217 ParmOffset = PtrSize;
3218
3219 // Argument types.
3220 ParmOffset = PtrSize;
3221 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3222 Decl->param_end(); PI != E; ++PI) {
3223 ParmVarDecl *PVDecl = *PI;
3224 QualType PType = PVDecl->getOriginalType();
3225 if (const ArrayType *AT =
3226 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3227 // Use array's original type only if it has known number of
3228 // elements.
3229 if (!isa<ConstantArrayType>(AT))
3230 PType = PVDecl->getType();
3231 } else if (PType->isFunctionType())
3232 PType = PVDecl->getType();
3233 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003234 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003235 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003236 }
3237}
3238
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003239/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003240/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003241void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003242 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003243 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003244 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003245 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003246 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003247 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003248 // Compute size of all parameters.
3249 // Start with computing size of a pointer in number of bytes.
3250 // FIXME: There might(should) be a better way of doing this computation!
3251 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003252 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003253 // The first two arguments (self and _cmd) are pointers; account for
3254 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003255 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003256 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003257 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003258 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003259 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003260 assert (sz.isPositive() &&
3261 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003262 ParmOffset += sz;
3263 }
Ken Dyck40775002010-01-11 17:06:35 +00003264 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003265 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003266 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003267
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003268 // Argument types.
3269 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003270 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003271 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003272 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003273 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003274 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003275 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3276 // Use array's original type only if it has known number of
3277 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003278 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003279 PType = PVDecl->getType();
3280 } else if (PType->isFunctionType())
3281 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003282 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003283 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003284 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003285 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003286 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003287 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003288 }
3289}
3290
Daniel Dunbar4932b362008-08-28 04:38:10 +00003291/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003292/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003293/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3294/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003295/// Property attributes are stored as a comma-delimited C string. The simple
3296/// attributes readonly and bycopy are encoded as single characters. The
3297/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3298/// encoded as single characters, followed by an identifier. Property types
3299/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003300/// these attributes are defined by the following enumeration:
3301/// @code
3302/// enum PropertyAttributes {
3303/// kPropertyReadOnly = 'R', // property is read-only.
3304/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3305/// kPropertyByref = '&', // property is a reference to the value last assigned
3306/// kPropertyDynamic = 'D', // property is dynamic
3307/// kPropertyGetter = 'G', // followed by getter selector name
3308/// kPropertySetter = 'S', // followed by setter selector name
3309/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3310/// kPropertyType = 't' // followed by old-style type encoding.
3311/// kPropertyWeak = 'W' // 'weak' property
3312/// kPropertyStrong = 'P' // property GC'able
3313/// kPropertyNonAtomic = 'N' // property non-atomic
3314/// };
3315/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003316void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003317 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003318 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003319 // Collect information from the property implementation decl(s).
3320 bool Dynamic = false;
3321 ObjCPropertyImplDecl *SynthesizePID = 0;
3322
3323 // FIXME: Duplicated code due to poor abstraction.
3324 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003325 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003326 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3327 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003328 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003329 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003330 ObjCPropertyImplDecl *PID = *i;
3331 if (PID->getPropertyDecl() == PD) {
3332 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3333 Dynamic = true;
3334 } else {
3335 SynthesizePID = PID;
3336 }
3337 }
3338 }
3339 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003340 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003341 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003342 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003343 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003344 ObjCPropertyImplDecl *PID = *i;
3345 if (PID->getPropertyDecl() == PD) {
3346 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3347 Dynamic = true;
3348 } else {
3349 SynthesizePID = PID;
3350 }
3351 }
Mike Stump11289f42009-09-09 15:08:12 +00003352 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003353 }
3354 }
3355
3356 // FIXME: This is not very efficient.
3357 S = "T";
3358
3359 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003360 // GCC has some special rules regarding encoding of properties which
3361 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003362 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003363 true /* outermost type */,
3364 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003365
3366 if (PD->isReadOnly()) {
3367 S += ",R";
3368 } else {
3369 switch (PD->getSetterKind()) {
3370 case ObjCPropertyDecl::Assign: break;
3371 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003372 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003373 }
3374 }
3375
3376 // It really isn't clear at all what this means, since properties
3377 // are "dynamic by default".
3378 if (Dynamic)
3379 S += ",D";
3380
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003381 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3382 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003383
Daniel Dunbar4932b362008-08-28 04:38:10 +00003384 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3385 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003386 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003387 }
3388
3389 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3390 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003391 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003392 }
3393
3394 if (SynthesizePID) {
3395 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3396 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003397 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003398 }
3399
3400 // FIXME: OBJCGC: weak & strong
3401}
3402
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003403/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003404/// Another legacy compatibility encoding: 32-bit longs are encoded as
3405/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003406/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3407///
3408void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003409 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003410 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003411 if (BT->getKind() == BuiltinType::ULong &&
3412 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003413 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003414 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003415 if (BT->getKind() == BuiltinType::Long &&
3416 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003417 PointeeTy = IntTy;
3418 }
3419 }
3420}
3421
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003422void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003423 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003424 // We follow the behavior of gcc, expanding structures which are
3425 // directly pointed to, and expanding embedded structures. Note that
3426 // these rules are sufficient to prevent recursive encoding of the
3427 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003428 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003429 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003430}
3431
Mike Stump11289f42009-09-09 15:08:12 +00003432static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003433 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003434 const Expr *E = FD->getBitWidth();
3435 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3436 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00003437 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003438 S += 'b';
3439 S += llvm::utostr(N);
3440}
3441
Daniel Dunbar07d07852009-10-18 21:17:35 +00003442// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003443void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3444 bool ExpandPointedToStructures,
3445 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003446 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003447 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003448 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00003449 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003450 if (FD && FD->isBitField())
3451 return EncodeBitField(this, S, FD);
3452 char encoding;
3453 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003454 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00003455 case BuiltinType::Void: encoding = 'v'; break;
3456 case BuiltinType::Bool: encoding = 'B'; break;
3457 case BuiltinType::Char_U:
3458 case BuiltinType::UChar: encoding = 'C'; break;
3459 case BuiltinType::UShort: encoding = 'S'; break;
3460 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003461 case BuiltinType::ULong:
3462 encoding =
3463 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00003464 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003465 case BuiltinType::UInt128: encoding = 'T'; break;
3466 case BuiltinType::ULongLong: encoding = 'Q'; break;
3467 case BuiltinType::Char_S:
3468 case BuiltinType::SChar: encoding = 'c'; break;
3469 case BuiltinType::Short: encoding = 's'; break;
3470 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003471 case BuiltinType::Long:
3472 encoding =
3473 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003474 break;
3475 case BuiltinType::LongLong: encoding = 'q'; break;
3476 case BuiltinType::Int128: encoding = 't'; break;
3477 case BuiltinType::Float: encoding = 'f'; break;
3478 case BuiltinType::Double: encoding = 'd'; break;
3479 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003480 }
Mike Stump11289f42009-09-09 15:08:12 +00003481
Chris Lattnere7cabb92009-07-13 00:10:46 +00003482 S += encoding;
3483 return;
3484 }
Mike Stump11289f42009-09-09 15:08:12 +00003485
John McCall9dd450b2009-09-21 23:43:11 +00003486 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003487 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003488 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003489 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003490 return;
3491 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003492
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003493 // encoding for pointer or r3eference types.
3494 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003495 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003496 if (PT->isObjCSelType()) {
3497 S += ':';
3498 return;
3499 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003500 PointeeTy = PT->getPointeeType();
3501 }
3502 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3503 PointeeTy = RT->getPointeeType();
3504 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003505 bool isReadOnly = false;
3506 // For historical/compatibility reasons, the read-only qualifier of the
3507 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3508 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003509 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003510 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003511 if (OutermostType && T.isConstQualified()) {
3512 isReadOnly = true;
3513 S += 'r';
3514 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003515 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003516 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003517 while (P->getAs<PointerType>())
3518 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003519 if (P.isConstQualified()) {
3520 isReadOnly = true;
3521 S += 'r';
3522 }
3523 }
3524 if (isReadOnly) {
3525 // Another legacy compatibility encoding. Some ObjC qualifier and type
3526 // combinations need to be rearranged.
3527 // Rewrite "in const" from "nr" to "rn"
3528 const char * s = S.c_str();
3529 int len = S.length();
3530 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3531 std::string replace = "rn";
3532 S.replace(S.end()-2, S.end(), replace);
3533 }
3534 }
Mike Stump11289f42009-09-09 15:08:12 +00003535
Anders Carlssond8499822007-10-29 05:01:08 +00003536 if (PointeeTy->isCharType()) {
3537 // char pointer types should be encoded as '*' unless it is a
3538 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003539 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003540 S += '*';
3541 return;
3542 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003543 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003544 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3545 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3546 S += '#';
3547 return;
3548 }
3549 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3550 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3551 S += '@';
3552 return;
3553 }
3554 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003555 }
Anders Carlssond8499822007-10-29 05:01:08 +00003556 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003557 getLegacyIntegralTypeEncoding(PointeeTy);
3558
Mike Stump11289f42009-09-09 15:08:12 +00003559 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003560 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003561 return;
3562 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003563
Chris Lattnere7cabb92009-07-13 00:10:46 +00003564 if (const ArrayType *AT =
3565 // Ignore type qualifiers etc.
3566 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003567 if (isa<IncompleteArrayType>(AT)) {
3568 // Incomplete arrays are encoded as a pointer to the array element.
3569 S += '^';
3570
Mike Stump11289f42009-09-09 15:08:12 +00003571 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003572 false, ExpandStructures, FD);
3573 } else {
3574 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003575
Anders Carlssond05f44b2009-02-22 01:38:57 +00003576 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3577 S += llvm::utostr(CAT->getSize().getZExtValue());
3578 else {
3579 //Variable length arrays are encoded as a regular array with 0 elements.
3580 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3581 S += '0';
3582 }
Mike Stump11289f42009-09-09 15:08:12 +00003583
3584 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003585 false, ExpandStructures, FD);
3586 S += ']';
3587 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003588 return;
3589 }
Mike Stump11289f42009-09-09 15:08:12 +00003590
John McCall9dd450b2009-09-21 23:43:11 +00003591 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003592 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003593 return;
3594 }
Mike Stump11289f42009-09-09 15:08:12 +00003595
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003596 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003597 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003598 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003599 // Anonymous structures print as '?'
3600 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3601 S += II->getName();
3602 } else {
3603 S += '?';
3604 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003605 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003606 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003607 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3608 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003609 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003610 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003611 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003612 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003613 S += '"';
3614 }
Mike Stump11289f42009-09-09 15:08:12 +00003615
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003616 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003617 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003618 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003619 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003620 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003621 QualType qt = Field->getType();
3622 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003623 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003624 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003625 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003626 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003627 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003628 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003629 return;
3630 }
Mike Stump11289f42009-09-09 15:08:12 +00003631
Chris Lattnere7cabb92009-07-13 00:10:46 +00003632 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003633 if (FD && FD->isBitField())
3634 EncodeBitField(this, S, FD);
3635 else
3636 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003637 return;
3638 }
Mike Stump11289f42009-09-09 15:08:12 +00003639
Chris Lattnere7cabb92009-07-13 00:10:46 +00003640 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003641 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003642 return;
3643 }
Mike Stump11289f42009-09-09 15:08:12 +00003644
John McCall8ccfcb52009-09-24 19:53:00 +00003645 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003646 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003647 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003648 S += '{';
3649 const IdentifierInfo *II = OI->getIdentifier();
3650 S += II->getName();
3651 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003652 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003653 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003654 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003655 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003656 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003657 RecFields[i]);
3658 else
Mike Stump11289f42009-09-09 15:08:12 +00003659 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003660 FD);
3661 }
3662 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003663 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003664 }
Mike Stump11289f42009-09-09 15:08:12 +00003665
John McCall9dd450b2009-09-21 23:43:11 +00003666 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003667 if (OPT->isObjCIdType()) {
3668 S += '@';
3669 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003670 }
Mike Stump11289f42009-09-09 15:08:12 +00003671
Steve Narofff0c86112009-10-28 22:03:49 +00003672 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3673 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3674 // Since this is a binary compatibility issue, need to consult with runtime
3675 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003676 S += '#';
3677 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003678 }
Mike Stump11289f42009-09-09 15:08:12 +00003679
Chris Lattnere7cabb92009-07-13 00:10:46 +00003680 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003681 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003682 ExpandPointedToStructures,
3683 ExpandStructures, FD);
3684 if (FD || EncodingProperty) {
3685 // Note that we do extended encoding of protocol qualifer list
3686 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003687 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003688 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3689 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003690 S += '<';
3691 S += (*I)->getNameAsString();
3692 S += '>';
3693 }
3694 S += '"';
3695 }
3696 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003697 }
Mike Stump11289f42009-09-09 15:08:12 +00003698
Chris Lattnere7cabb92009-07-13 00:10:46 +00003699 QualType PointeeTy = OPT->getPointeeType();
3700 if (!EncodingProperty &&
3701 isa<TypedefType>(PointeeTy.getTypePtr())) {
3702 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003703 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003704 // {...};
3705 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003706 getObjCEncodingForTypeImpl(PointeeTy, S,
3707 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003708 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003709 return;
3710 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003711
3712 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003713 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003714 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003715 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003716 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3717 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003718 S += '<';
3719 S += (*I)->getNameAsString();
3720 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003721 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003722 S += '"';
3723 }
3724 return;
3725 }
Mike Stump11289f42009-09-09 15:08:12 +00003726
Chris Lattnere7cabb92009-07-13 00:10:46 +00003727 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003728}
3729
Mike Stump11289f42009-09-09 15:08:12 +00003730void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003731 std::string& S) const {
3732 if (QT & Decl::OBJC_TQ_In)
3733 S += 'n';
3734 if (QT & Decl::OBJC_TQ_Inout)
3735 S += 'N';
3736 if (QT & Decl::OBJC_TQ_Out)
3737 S += 'o';
3738 if (QT & Decl::OBJC_TQ_Bycopy)
3739 S += 'O';
3740 if (QT & Decl::OBJC_TQ_Byref)
3741 S += 'R';
3742 if (QT & Decl::OBJC_TQ_Oneway)
3743 S += 'V';
3744}
3745
Chris Lattnere7cabb92009-07-13 00:10:46 +00003746void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003747 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003748
Anders Carlsson87c149b2007-10-11 01:00:40 +00003749 BuiltinVaListType = T;
3750}
3751
Chris Lattnere7cabb92009-07-13 00:10:46 +00003752void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003753 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003754}
3755
Chris Lattnere7cabb92009-07-13 00:10:46 +00003756void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003757 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003758}
3759
Chris Lattnere7cabb92009-07-13 00:10:46 +00003760void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003761 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003762}
3763
Chris Lattnere7cabb92009-07-13 00:10:46 +00003764void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003765 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003766}
3767
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003768void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003769 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003770 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003771
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003772 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003773}
3774
John McCalld28ae272009-12-02 08:04:21 +00003775/// \brief Retrieve the template name that corresponds to a non-empty
3776/// lookup.
John McCallad371252010-01-20 00:46:10 +00003777TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3778 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003779 unsigned size = End - Begin;
3780 assert(size > 1 && "set is not overloaded!");
3781
3782 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3783 size * sizeof(FunctionTemplateDecl*));
3784 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3785
3786 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003787 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003788 NamedDecl *D = *I;
3789 assert(isa<FunctionTemplateDecl>(D) ||
3790 (isa<UsingShadowDecl>(D) &&
3791 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3792 *Storage++ = D;
3793 }
3794
3795 return TemplateName(OT);
3796}
3797
Douglas Gregordc572a32009-03-30 22:58:21 +00003798/// \brief Retrieve the template name that represents a qualified
3799/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003800TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003801 bool TemplateKeyword,
3802 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003803 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003804 llvm::FoldingSetNodeID ID;
3805 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3806
3807 void *InsertPos = 0;
3808 QualifiedTemplateName *QTN =
3809 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3810 if (!QTN) {
3811 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3812 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3813 }
3814
3815 return TemplateName(QTN);
3816}
3817
3818/// \brief Retrieve the template name that represents a dependent
3819/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003820TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003821 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003822 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003823 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003824
3825 llvm::FoldingSetNodeID ID;
3826 DependentTemplateName::Profile(ID, NNS, Name);
3827
3828 void *InsertPos = 0;
3829 DependentTemplateName *QTN =
3830 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3831
3832 if (QTN)
3833 return TemplateName(QTN);
3834
3835 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3836 if (CanonNNS == NNS) {
3837 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3838 } else {
3839 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3840 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003841 DependentTemplateName *CheckQTN =
3842 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3843 assert(!CheckQTN && "Dependent type name canonicalization broken");
3844 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00003845 }
3846
3847 DependentTemplateNames.InsertNode(QTN, InsertPos);
3848 return TemplateName(QTN);
3849}
3850
Douglas Gregor71395fa2009-11-04 00:56:37 +00003851/// \brief Retrieve the template name that represents a dependent
3852/// template name such as \c MetaFun::template operator+.
3853TemplateName
3854ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3855 OverloadedOperatorKind Operator) {
3856 assert((!NNS || NNS->isDependent()) &&
3857 "Nested name specifier must be dependent");
3858
3859 llvm::FoldingSetNodeID ID;
3860 DependentTemplateName::Profile(ID, NNS, Operator);
3861
3862 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00003863 DependentTemplateName *QTN
3864 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00003865
3866 if (QTN)
3867 return TemplateName(QTN);
3868
3869 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3870 if (CanonNNS == NNS) {
3871 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3872 } else {
3873 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3874 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003875
3876 DependentTemplateName *CheckQTN
3877 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3878 assert(!CheckQTN && "Dependent template name canonicalization broken");
3879 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00003880 }
3881
3882 DependentTemplateNames.InsertNode(QTN, InsertPos);
3883 return TemplateName(QTN);
3884}
3885
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003886/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003887/// TargetInfo, produce the corresponding type. The unsigned @p Type
3888/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003889CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003890 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003891 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003892 case TargetInfo::SignedShort: return ShortTy;
3893 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3894 case TargetInfo::SignedInt: return IntTy;
3895 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3896 case TargetInfo::SignedLong: return LongTy;
3897 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3898 case TargetInfo::SignedLongLong: return LongLongTy;
3899 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3900 }
3901
3902 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003903 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003904}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003905
3906//===----------------------------------------------------------------------===//
3907// Type Predicates.
3908//===----------------------------------------------------------------------===//
3909
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003910/// isObjCNSObjectType - Return true if this is an NSObject object using
3911/// NSObject attribute on a c-style pointer type.
3912/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003913/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003914///
3915bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3916 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3917 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003918 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003919 return true;
3920 }
Mike Stump11289f42009-09-09 15:08:12 +00003921 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003922}
3923
Fariborz Jahanian96207692009-02-18 21:49:28 +00003924/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3925/// garbage collection attribute.
3926///
John McCall8ccfcb52009-09-24 19:53:00 +00003927Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3928 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003929 if (getLangOptions().ObjC1 &&
3930 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003931 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003932 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003933 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003934 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003935 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003936 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003937 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003938 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003939 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003940 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003941 // Non-pointers have none gc'able attribute regardless of the attribute
3942 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003943 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003944 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003945 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003946 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003947}
3948
Chris Lattner49af6a42008-04-07 06:51:04 +00003949//===----------------------------------------------------------------------===//
3950// Type Compatibility Testing
3951//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00003952
Mike Stump11289f42009-09-09 15:08:12 +00003953/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003954/// compatible.
3955static bool areCompatVectorTypes(const VectorType *LHS,
3956 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00003957 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00003958 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00003959 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00003960}
3961
Steve Naroff8e6aee52009-07-23 01:01:38 +00003962//===----------------------------------------------------------------------===//
3963// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3964//===----------------------------------------------------------------------===//
3965
3966/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3967/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003968bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3969 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003970 if (lProto == rProto)
3971 return true;
3972 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3973 E = rProto->protocol_end(); PI != E; ++PI)
3974 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3975 return true;
3976 return false;
3977}
3978
Steve Naroff8e6aee52009-07-23 01:01:38 +00003979/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3980/// return true if lhs's protocols conform to rhs's protocol; false
3981/// otherwise.
3982bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3983 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3984 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3985 return false;
3986}
3987
3988/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3989/// ObjCQualifiedIDType.
3990bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3991 bool compare) {
3992 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00003993 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003994 lhs->isObjCIdType() || lhs->isObjCClassType())
3995 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003996 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003997 rhs->isObjCIdType() || rhs->isObjCClassType())
3998 return true;
3999
4000 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004001 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004002
Steve Naroff8e6aee52009-07-23 01:01:38 +00004003 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004004
Steve Naroff8e6aee52009-07-23 01:01:38 +00004005 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004006 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004007 // make sure we check the class hierarchy.
4008 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4009 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4010 E = lhsQID->qual_end(); I != E; ++I) {
4011 // when comparing an id<P> on lhs with a static type on rhs,
4012 // see if static class implements all of id's protocols, directly or
4013 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004014 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004015 return false;
4016 }
4017 }
4018 // If there are no qualifiers and no interface, we have an 'id'.
4019 return true;
4020 }
Mike Stump11289f42009-09-09 15:08:12 +00004021 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004022 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4023 E = lhsQID->qual_end(); I != E; ++I) {
4024 ObjCProtocolDecl *lhsProto = *I;
4025 bool match = false;
4026
4027 // when comparing an id<P> on lhs with a static type on rhs,
4028 // see if static class implements all of id's protocols, directly or
4029 // through its super class and categories.
4030 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4031 E = rhsOPT->qual_end(); J != E; ++J) {
4032 ObjCProtocolDecl *rhsProto = *J;
4033 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4034 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4035 match = true;
4036 break;
4037 }
4038 }
Mike Stump11289f42009-09-09 15:08:12 +00004039 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004040 // make sure we check the class hierarchy.
4041 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4042 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4043 E = lhsQID->qual_end(); I != E; ++I) {
4044 // when comparing an id<P> on lhs with a static type on rhs,
4045 // see if static class implements all of id's protocols, directly or
4046 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004047 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004048 match = true;
4049 break;
4050 }
4051 }
4052 }
4053 if (!match)
4054 return false;
4055 }
Mike Stump11289f42009-09-09 15:08:12 +00004056
Steve Naroff8e6aee52009-07-23 01:01:38 +00004057 return true;
4058 }
Mike Stump11289f42009-09-09 15:08:12 +00004059
Steve Naroff8e6aee52009-07-23 01:01:38 +00004060 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4061 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4062
Mike Stump11289f42009-09-09 15:08:12 +00004063 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004064 lhs->getAsObjCInterfacePointerType()) {
4065 if (lhsOPT->qual_empty()) {
4066 bool match = false;
4067 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4068 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4069 E = rhsQID->qual_end(); I != E; ++I) {
4070 // when comparing an id<P> on lhs with a static type on rhs,
4071 // see if static class implements all of id's protocols, directly or
4072 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004073 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004074 match = true;
4075 break;
4076 }
4077 }
4078 if (!match)
4079 return false;
4080 }
4081 return true;
4082 }
Mike Stump11289f42009-09-09 15:08:12 +00004083 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004084 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4085 E = lhsOPT->qual_end(); I != E; ++I) {
4086 ObjCProtocolDecl *lhsProto = *I;
4087 bool match = false;
4088
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.
4092 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4093 E = rhsQID->qual_end(); J != E; ++J) {
4094 ObjCProtocolDecl *rhsProto = *J;
4095 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4096 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4097 match = true;
4098 break;
4099 }
4100 }
4101 if (!match)
4102 return false;
4103 }
4104 return true;
4105 }
4106 return false;
4107}
4108
Eli Friedman47f77112008-08-22 00:56:42 +00004109/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004110/// compatible for assignment from RHS to LHS. This handles validation of any
4111/// protocol qualifiers on the LHS or RHS.
4112///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004113bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4114 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004115 // If either type represents the built-in 'id' or 'Class' types, return true.
4116 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004117 return true;
4118
Steve Naroff8e6aee52009-07-23 01:01:38 +00004119 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00004120 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4121 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004122 false);
4123
Steve Naroff7cae42b2009-07-10 23:34:53 +00004124 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4125 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00004126 if (LHS && RHS) // We have 2 user-defined types.
4127 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004128
Steve Naroff8e6aee52009-07-23 01:01:38 +00004129 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004130}
4131
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004132/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4133/// for providing type-safty for objective-c pointers used to pass/return
4134/// arguments in block literals. When passed as arguments, passing 'A*' where
4135/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4136/// not OK. For the return type, the opposite is not OK.
4137bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4138 const ObjCObjectPointerType *LHSOPT,
4139 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004140 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004141 return true;
4142
4143 if (LHSOPT->isObjCBuiltinType()) {
4144 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4145 }
4146
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004147 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004148 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4149 QualType(RHSOPT,0),
4150 false);
4151
4152 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4153 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4154 if (LHS && RHS) { // We have 2 user-defined types.
4155 if (LHS != RHS) {
4156 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4157 return false;
4158 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4159 return true;
4160 }
4161 else
4162 return true;
4163 }
4164 return false;
4165}
4166
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004167/// getIntersectionOfProtocols - This routine finds the intersection of set
4168/// of protocols inherited from two distinct objective-c pointer objects.
4169/// It is used to build composite qualifier list of the composite type of
4170/// the conditional expression involving two objective-c pointer objects.
4171static
4172void getIntersectionOfProtocols(ASTContext &Context,
4173 const ObjCObjectPointerType *LHSOPT,
4174 const ObjCObjectPointerType *RHSOPT,
4175 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4176
4177 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4178 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4179
4180 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4181 unsigned LHSNumProtocols = LHS->getNumProtocols();
4182 if (LHSNumProtocols > 0)
4183 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4184 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004185 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4186 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004187 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4188 LHSInheritedProtocols.end());
4189 }
4190
4191 unsigned RHSNumProtocols = RHS->getNumProtocols();
4192 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004193 ObjCProtocolDecl **RHSProtocols =
4194 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004195 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4196 if (InheritedProtocolSet.count(RHSProtocols[i]))
4197 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4198 }
4199 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004200 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004201 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004202 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4203 RHSInheritedProtocols.begin(),
4204 E = RHSInheritedProtocols.end(); I != E; ++I)
4205 if (InheritedProtocolSet.count((*I)))
4206 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004207 }
4208}
4209
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004210/// areCommonBaseCompatible - Returns common base class of the two classes if
4211/// one found. Note that this is O'2 algorithm. But it will be called as the
4212/// last type comparison in a ?-exp of ObjC pointer types before a
4213/// warning is issued. So, its invokation is extremely rare.
4214QualType ASTContext::areCommonBaseCompatible(
4215 const ObjCObjectPointerType *LHSOPT,
4216 const ObjCObjectPointerType *RHSOPT) {
4217 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4218 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4219 if (!LHS || !RHS)
4220 return QualType();
4221
4222 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4223 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4224 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004225 if (canAssignObjCInterfaces(LHS, RHS)) {
4226 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4227 getIntersectionOfProtocols(*this,
4228 LHSOPT, RHSOPT, IntersectionOfProtocols);
4229 if (IntersectionOfProtocols.empty())
4230 LHSTy = getObjCObjectPointerType(LHSTy);
4231 else
4232 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4233 IntersectionOfProtocols.size());
4234 return LHSTy;
4235 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004236 }
4237
4238 return QualType();
4239}
4240
Eli Friedman47f77112008-08-22 00:56:42 +00004241bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4242 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00004243 // Verify that the base decls are compatible: the RHS must be a subclass of
4244 // the LHS.
4245 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4246 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004247
Chris Lattner49af6a42008-04-07 06:51:04 +00004248 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4249 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004250 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004251 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004252
Chris Lattner49af6a42008-04-07 06:51:04 +00004253 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4254 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004255 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004256 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004257
Steve Naroffc277ad12009-07-18 15:33:26 +00004258 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4259 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004260 LHSPI != LHSPE; LHSPI++) {
4261 bool RHSImplementsProtocol = false;
4262
4263 // If the RHS doesn't implement the protocol on the left, the types
4264 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00004265 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004266 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004267 RHSPI != RHSPE; RHSPI++) {
4268 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004269 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004270 break;
4271 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004272 }
4273 // FIXME: For better diagnostics, consider passing back the protocol name.
4274 if (!RHSImplementsProtocol)
4275 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004276 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004277 // The RHS implements all protocols listed on the LHS.
4278 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004279}
4280
Steve Naroffb7605152009-02-12 17:52:19 +00004281bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4282 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004283 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4284 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004285
Steve Naroff7cae42b2009-07-10 23:34:53 +00004286 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004287 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004288
4289 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4290 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004291}
4292
Mike Stump11289f42009-09-09 15:08:12 +00004293/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004294/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004295/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004296/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004297bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004298 if (getLangOptions().CPlusPlus)
4299 return hasSameType(LHS, RHS);
4300
Eli Friedman47f77112008-08-22 00:56:42 +00004301 return !mergeTypes(LHS, RHS).isNull();
4302}
4303
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004304bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4305 return !mergeTypes(LHS, RHS, true).isNull();
4306}
4307
4308QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4309 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004310 const FunctionType *lbase = lhs->getAs<FunctionType>();
4311 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004312 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4313 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004314 bool allLTypes = true;
4315 bool allRTypes = true;
4316
4317 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004318 QualType retType;
4319 if (OfBlockPointer)
4320 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4321 else
4322 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004323 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004324 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004325 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004326 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004327 allRTypes = false;
Mike Stumpea086c72009-07-25 23:24:03 +00004328 // FIXME: double check this
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004329 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4330 // rbase->getRegParmAttr() != 0 &&
4331 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004332 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4333 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004334 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4335 lbaseInfo.getRegParm();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004336 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004337 if (NoReturn != lbaseInfo.getNoReturn() ||
4338 RegParm != lbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004339 allLTypes = false;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004340 if (NoReturn != rbaseInfo.getNoReturn() ||
4341 RegParm != rbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004342 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004343 CallingConv lcc = lbaseInfo.getCC();
4344 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004345 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004346 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004347 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004348
Eli Friedman47f77112008-08-22 00:56:42 +00004349 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004350 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4351 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004352 unsigned lproto_nargs = lproto->getNumArgs();
4353 unsigned rproto_nargs = rproto->getNumArgs();
4354
4355 // Compatible functions must have the same number of arguments
4356 if (lproto_nargs != rproto_nargs)
4357 return QualType();
4358
4359 // Variadic and non-variadic functions aren't compatible
4360 if (lproto->isVariadic() != rproto->isVariadic())
4361 return QualType();
4362
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004363 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4364 return QualType();
4365
Eli Friedman47f77112008-08-22 00:56:42 +00004366 // Check argument compatibility
4367 llvm::SmallVector<QualType, 10> types;
4368 for (unsigned i = 0; i < lproto_nargs; i++) {
4369 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4370 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004371 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004372 if (argtype.isNull()) return QualType();
4373 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004374 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4375 allLTypes = false;
4376 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4377 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004378 }
4379 if (allLTypes) return lhs;
4380 if (allRTypes) return rhs;
4381 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004382 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004383 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004384 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004385 }
4386
4387 if (lproto) allRTypes = false;
4388 if (rproto) allLTypes = false;
4389
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004390 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004391 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004392 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004393 if (proto->isVariadic()) return QualType();
4394 // Check that the types are compatible with the types that
4395 // would result from default argument promotions (C99 6.7.5.3p15).
4396 // The only types actually affected are promotable integer
4397 // types and floats, which would be passed as a different
4398 // type depending on whether the prototype is visible.
4399 unsigned proto_nargs = proto->getNumArgs();
4400 for (unsigned i = 0; i < proto_nargs; ++i) {
4401 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004402
4403 // Look at the promotion type of enum types, since that is the type used
4404 // to pass enum values.
4405 if (const EnumType *Enum = argTy->getAs<EnumType>())
4406 argTy = Enum->getDecl()->getPromotionType();
4407
Eli Friedman47f77112008-08-22 00:56:42 +00004408 if (argTy->isPromotableIntegerType() ||
4409 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4410 return QualType();
4411 }
4412
4413 if (allLTypes) return lhs;
4414 if (allRTypes) return rhs;
4415 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004416 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004417 proto->getTypeQuals(),
4418 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004419 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004420 }
4421
4422 if (allLTypes) return lhs;
4423 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004424 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004425 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004426}
4427
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004428QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4429 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004430 // C++ [expr]: If an expression initially has the type "reference to T", the
4431 // type is adjusted to "T" prior to any further analysis, the expression
4432 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004433 // expression is an lvalue unless the reference is an rvalue reference and
4434 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004435 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4436 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4437
Eli Friedman47f77112008-08-22 00:56:42 +00004438 QualType LHSCan = getCanonicalType(LHS),
4439 RHSCan = getCanonicalType(RHS);
4440
4441 // If two types are identical, they are compatible.
4442 if (LHSCan == RHSCan)
4443 return LHS;
4444
John McCall8ccfcb52009-09-24 19:53:00 +00004445 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004446 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4447 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004448 if (LQuals != RQuals) {
4449 // If any of these qualifiers are different, we have a type
4450 // mismatch.
4451 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4452 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4453 return QualType();
4454
4455 // Exactly one GC qualifier difference is allowed: __strong is
4456 // okay if the other type has no GC qualifier but is an Objective
4457 // C object pointer (i.e. implicitly strong by default). We fix
4458 // this by pretending that the unqualified type was actually
4459 // qualified __strong.
4460 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4461 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4462 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4463
4464 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4465 return QualType();
4466
4467 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4468 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4469 }
4470 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4471 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4472 }
Eli Friedman47f77112008-08-22 00:56:42 +00004473 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004474 }
4475
4476 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004477
Eli Friedmandcca6332009-06-01 01:22:52 +00004478 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4479 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004480
Chris Lattnerfd652912008-01-14 05:45:46 +00004481 // We want to consider the two function types to be the same for these
4482 // comparisons, just force one to the other.
4483 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4484 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004485
4486 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004487 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4488 LHSClass = Type::ConstantArray;
4489 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4490 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004491
Nate Begemance4d7fc2008-04-18 23:10:10 +00004492 // Canonicalize ExtVector -> Vector.
4493 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4494 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004495
Chris Lattner95554662008-04-07 05:43:21 +00004496 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004497 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004498 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004499 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004500 // Compatibility is based on the underlying type, not the promotion
4501 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004502 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004503 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4504 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004505 }
John McCall9dd450b2009-09-21 23:43:11 +00004506 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004507 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4508 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004509 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004510
Eli Friedman47f77112008-08-22 00:56:42 +00004511 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004512 }
Eli Friedman47f77112008-08-22 00:56:42 +00004513
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004514 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004515 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004516#define TYPE(Class, Base)
4517#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004518#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004519#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4520#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4521#include "clang/AST/TypeNodes.def"
4522 assert(false && "Non-canonical and dependent types shouldn't get here");
4523 return QualType();
4524
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004525 case Type::LValueReference:
4526 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004527 case Type::MemberPointer:
4528 assert(false && "C++ should never be in mergeTypes");
4529 return QualType();
4530
4531 case Type::IncompleteArray:
4532 case Type::VariableArray:
4533 case Type::FunctionProto:
4534 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004535 assert(false && "Types are eliminated above");
4536 return QualType();
4537
Chris Lattnerfd652912008-01-14 05:45:46 +00004538 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004539 {
4540 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004541 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4542 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004543 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4544 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004545 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004546 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004547 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004548 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004549 return getPointerType(ResultType);
4550 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004551 case Type::BlockPointer:
4552 {
4553 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004554 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4555 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004556 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004557 if (ResultType.isNull()) return QualType();
4558 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4559 return LHS;
4560 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4561 return RHS;
4562 return getBlockPointerType(ResultType);
4563 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004564 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004565 {
4566 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4567 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4568 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4569 return QualType();
4570
4571 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4572 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4573 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4574 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004575 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4576 return LHS;
4577 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4578 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004579 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4580 ArrayType::ArraySizeModifier(), 0);
4581 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4582 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004583 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4584 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004585 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4586 return LHS;
4587 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4588 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004589 if (LVAT) {
4590 // FIXME: This isn't correct! But tricky to implement because
4591 // the array's size has to be the size of LHS, but the type
4592 // has to be different.
4593 return LHS;
4594 }
4595 if (RVAT) {
4596 // FIXME: This isn't correct! But tricky to implement because
4597 // the array's size has to be the size of RHS, but the type
4598 // has to be different.
4599 return RHS;
4600 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004601 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4602 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004603 return getIncompleteArrayType(ResultType,
4604 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004605 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004606 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004607 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004608 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004609 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004610 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004611 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004612 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004613 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004614 case Type::Complex:
4615 // Distinct complex types are incompatible.
4616 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004617 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004618 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004619 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4620 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004621 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004622 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004623 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00004624 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004625 // FIXME: This should be type compatibility, e.g. whether
4626 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00004627 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4628 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00004629 if (LHSIface && RHSIface &&
4630 canAssignObjCInterfaces(LHSIface, RHSIface))
4631 return LHS;
4632
Eli Friedman47f77112008-08-22 00:56:42 +00004633 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004634 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004635 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004636 if (OfBlockPointer) {
4637 if (canAssignObjCInterfacesInBlockPointer(
4638 LHS->getAs<ObjCObjectPointerType>(),
4639 RHS->getAs<ObjCObjectPointerType>()))
4640 return LHS;
4641 return QualType();
4642 }
John McCall9dd450b2009-09-21 23:43:11 +00004643 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4644 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004645 return LHS;
4646
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004647 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004648 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004649 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004650
4651 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004652}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004653
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004654//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004655// Integer Predicates
4656//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004657
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004658unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004659 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004660 return 1;
John McCall56774992009-12-09 09:09:27 +00004661 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004662 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004663 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004664 return (unsigned)getTypeSize(T);
4665}
4666
4667QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4668 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004669
4670 // Turn <4 x signed int> -> <4 x unsigned int>
4671 if (const VectorType *VTy = T->getAs<VectorType>())
4672 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004673 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004674
4675 // For enums, we return the unsigned version of the base type.
4676 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004677 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004678
4679 const BuiltinType *BTy = T->getAs<BuiltinType>();
4680 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004681 switch (BTy->getKind()) {
4682 case BuiltinType::Char_S:
4683 case BuiltinType::SChar:
4684 return UnsignedCharTy;
4685 case BuiltinType::Short:
4686 return UnsignedShortTy;
4687 case BuiltinType::Int:
4688 return UnsignedIntTy;
4689 case BuiltinType::Long:
4690 return UnsignedLongTy;
4691 case BuiltinType::LongLong:
4692 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004693 case BuiltinType::Int128:
4694 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004695 default:
4696 assert(0 && "Unexpected signed integer type");
4697 return QualType();
4698 }
4699}
4700
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004701ExternalASTSource::~ExternalASTSource() { }
4702
4703void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004704
4705
4706//===----------------------------------------------------------------------===//
4707// Builtin Type Computation
4708//===----------------------------------------------------------------------===//
4709
4710/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4711/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004712static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004713 ASTContext::GetBuiltinTypeError &Error,
4714 bool AllowTypeModifiers = true) {
4715 // Modifiers.
4716 int HowLong = 0;
4717 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004718
Chris Lattnerecd79c62009-06-14 00:45:47 +00004719 // Read the modifiers first.
4720 bool Done = false;
4721 while (!Done) {
4722 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004723 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004724 case 'S':
4725 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4726 assert(!Signed && "Can't use 'S' modifier multiple times!");
4727 Signed = true;
4728 break;
4729 case 'U':
4730 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4731 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4732 Unsigned = true;
4733 break;
4734 case 'L':
4735 assert(HowLong <= 2 && "Can't have LLLL modifier");
4736 ++HowLong;
4737 break;
4738 }
4739 }
4740
4741 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004742
Chris Lattnerecd79c62009-06-14 00:45:47 +00004743 // Read the base type.
4744 switch (*Str++) {
4745 default: assert(0 && "Unknown builtin type letter!");
4746 case 'v':
4747 assert(HowLong == 0 && !Signed && !Unsigned &&
4748 "Bad modifiers used with 'v'!");
4749 Type = Context.VoidTy;
4750 break;
4751 case 'f':
4752 assert(HowLong == 0 && !Signed && !Unsigned &&
4753 "Bad modifiers used with 'f'!");
4754 Type = Context.FloatTy;
4755 break;
4756 case 'd':
4757 assert(HowLong < 2 && !Signed && !Unsigned &&
4758 "Bad modifiers used with 'd'!");
4759 if (HowLong)
4760 Type = Context.LongDoubleTy;
4761 else
4762 Type = Context.DoubleTy;
4763 break;
4764 case 's':
4765 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4766 if (Unsigned)
4767 Type = Context.UnsignedShortTy;
4768 else
4769 Type = Context.ShortTy;
4770 break;
4771 case 'i':
4772 if (HowLong == 3)
4773 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4774 else if (HowLong == 2)
4775 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4776 else if (HowLong == 1)
4777 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4778 else
4779 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4780 break;
4781 case 'c':
4782 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4783 if (Signed)
4784 Type = Context.SignedCharTy;
4785 else if (Unsigned)
4786 Type = Context.UnsignedCharTy;
4787 else
4788 Type = Context.CharTy;
4789 break;
4790 case 'b': // boolean
4791 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4792 Type = Context.BoolTy;
4793 break;
4794 case 'z': // size_t.
4795 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4796 Type = Context.getSizeType();
4797 break;
4798 case 'F':
4799 Type = Context.getCFConstantStringType();
4800 break;
4801 case 'a':
4802 Type = Context.getBuiltinVaListType();
4803 assert(!Type.isNull() && "builtin va list type not initialized!");
4804 break;
4805 case 'A':
4806 // This is a "reference" to a va_list; however, what exactly
4807 // this means depends on how va_list is defined. There are two
4808 // different kinds of va_list: ones passed by value, and ones
4809 // passed by reference. An example of a by-value va_list is
4810 // x86, where va_list is a char*. An example of by-ref va_list
4811 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4812 // we want this argument to be a char*&; for x86-64, we want
4813 // it to be a __va_list_tag*.
4814 Type = Context.getBuiltinVaListType();
4815 assert(!Type.isNull() && "builtin va list type not initialized!");
4816 if (Type->isArrayType()) {
4817 Type = Context.getArrayDecayedType(Type);
4818 } else {
4819 Type = Context.getLValueReferenceType(Type);
4820 }
4821 break;
4822 case 'V': {
4823 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004824 unsigned NumElements = strtoul(Str, &End, 10);
4825 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004826
Chris Lattnerecd79c62009-06-14 00:45:47 +00004827 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004828
Chris Lattnerecd79c62009-06-14 00:45:47 +00004829 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00004830 // FIXME: Don't know what to do about AltiVec.
4831 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004832 break;
4833 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004834 case 'X': {
4835 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4836 Type = Context.getComplexType(ElementType);
4837 break;
4838 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004839 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004840 Type = Context.getFILEType();
4841 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004842 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004843 return QualType();
4844 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004845 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004846 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004847 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004848 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004849 else
4850 Type = Context.getjmp_bufType();
4851
Mike Stump2adb4da2009-07-28 23:47:15 +00004852 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004853 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004854 return QualType();
4855 }
4856 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004857 }
Mike Stump11289f42009-09-09 15:08:12 +00004858
Chris Lattnerecd79c62009-06-14 00:45:47 +00004859 if (!AllowTypeModifiers)
4860 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004861
Chris Lattnerecd79c62009-06-14 00:45:47 +00004862 Done = false;
4863 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00004864 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00004865 default: Done = true; --Str; break;
4866 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00004867 case '&':
John McCallb8b94662010-03-12 04:21:28 +00004868 {
4869 // Both pointers and references can have their pointee types
4870 // qualified with an address space.
4871 char *End;
4872 unsigned AddrSpace = strtoul(Str, &End, 10);
4873 if (End != Str && AddrSpace != 0) {
4874 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4875 Str = End;
4876 }
4877 }
4878 if (c == '*')
4879 Type = Context.getPointerType(Type);
4880 else
4881 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004882 break;
4883 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4884 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004885 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004886 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00004887 case 'D':
4888 Type = Context.getVolatileType(Type);
4889 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004890 }
4891 }
Mike Stump11289f42009-09-09 15:08:12 +00004892
Chris Lattnerecd79c62009-06-14 00:45:47 +00004893 return Type;
4894}
4895
4896/// GetBuiltinType - Return the type for the specified builtin.
4897QualType ASTContext::GetBuiltinType(unsigned id,
4898 GetBuiltinTypeError &Error) {
4899 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004900
Chris Lattnerecd79c62009-06-14 00:45:47 +00004901 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004902
Chris Lattnerecd79c62009-06-14 00:45:47 +00004903 Error = GE_None;
4904 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4905 if (Error != GE_None)
4906 return QualType();
4907 while (TypeStr[0] && TypeStr[0] != '.') {
4908 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4909 if (Error != GE_None)
4910 return QualType();
4911
4912 // Do array -> pointer decay. The builtin should use the decayed type.
4913 if (Ty->isArrayType())
4914 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004915
Chris Lattnerecd79c62009-06-14 00:45:47 +00004916 ArgTypes.push_back(Ty);
4917 }
4918
4919 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4920 "'.' should only occur at end of builtin type list!");
4921
4922 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4923 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4924 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00004925
4926 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00004927 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00004928 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004929 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00004930}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004931
4932QualType
4933ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4934 // Perform the usual unary conversions. We do this early so that
4935 // integral promotions to "int" can allow us to exit early, in the
4936 // lhs == rhs check. Also, for conversion purposes, we ignore any
4937 // qualifiers. For example, "const float" and "float" are
4938 // equivalent.
4939 if (lhs->isPromotableIntegerType())
4940 lhs = getPromotedIntegerType(lhs);
4941 else
4942 lhs = lhs.getUnqualifiedType();
4943 if (rhs->isPromotableIntegerType())
4944 rhs = getPromotedIntegerType(rhs);
4945 else
4946 rhs = rhs.getUnqualifiedType();
4947
4948 // If both types are identical, no conversion is needed.
4949 if (lhs == rhs)
4950 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004951
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004952 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4953 // The caller can deal with this (e.g. pointer + int).
4954 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4955 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004956
4957 // At this point, we have two different arithmetic types.
4958
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004959 // Handle complex types first (C99 6.3.1.8p1).
4960 if (lhs->isComplexType() || rhs->isComplexType()) {
4961 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00004962 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004963 // convert the rhs to the lhs complex type.
4964 return lhs;
4965 }
Mike Stump11289f42009-09-09 15:08:12 +00004966 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004967 // convert the lhs to the rhs complex type.
4968 return rhs;
4969 }
4970 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00004971 // When both operands are complex, the shorter operand is converted to the
4972 // type of the longer, and that is the type of the result. This corresponds
4973 // to what is done when combining two real floating-point operands.
4974 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004975 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00004976 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004977 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00004978 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004979 // "double _Complex" is promoted to "long double _Complex".
4980 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004981
4982 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004983 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004984 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004985 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00004986 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004987 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4988 // domains match. This is a requirement for our implementation, C99
4989 // does not require this promotion.
4990 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4991 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4992 return rhs;
4993 } else { // handle "_Complex double, double".
4994 return lhs;
4995 }
4996 }
4997 return lhs; // The domain/size match exactly.
4998 }
4999 // Now handle "real" floating types (i.e. float, double, long double).
5000 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5001 // if we have an integer operand, the result is the real floating type.
5002 if (rhs->isIntegerType()) {
5003 // convert rhs to the lhs floating point type.
5004 return lhs;
5005 }
5006 if (rhs->isComplexIntegerType()) {
5007 // convert rhs to the complex floating point type.
5008 return getComplexType(lhs);
5009 }
5010 if (lhs->isIntegerType()) {
5011 // convert lhs to the rhs floating point type.
5012 return rhs;
5013 }
Mike Stump11289f42009-09-09 15:08:12 +00005014 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005015 // convert lhs to the complex floating point type.
5016 return getComplexType(rhs);
5017 }
5018 // We have two real floating types, float/complex combos were handled above.
5019 // Convert the smaller operand to the bigger result.
5020 int result = getFloatingTypeOrder(lhs, rhs);
5021 if (result > 0) // convert the rhs
5022 return lhs;
5023 assert(result < 0 && "illegal float comparison");
5024 return rhs; // convert the lhs
5025 }
5026 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5027 // Handle GCC complex int extension.
5028 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5029 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5030
5031 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005032 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005033 rhsComplexInt->getElementType()) >= 0)
5034 return lhs; // convert the rhs
5035 return rhs;
5036 } else if (lhsComplexInt && rhs->isIntegerType()) {
5037 // convert the rhs to the lhs complex type.
5038 return lhs;
5039 } else if (rhsComplexInt && lhs->isIntegerType()) {
5040 // convert the lhs to the rhs complex type.
5041 return rhs;
5042 }
5043 }
5044 // Finally, we have two differing integer types.
5045 // The rules for this case are in C99 6.3.1.8
5046 int compare = getIntegerTypeOrder(lhs, rhs);
5047 bool lhsSigned = lhs->isSignedIntegerType(),
5048 rhsSigned = rhs->isSignedIntegerType();
5049 QualType destType;
5050 if (lhsSigned == rhsSigned) {
5051 // Same signedness; use the higher-ranked type
5052 destType = compare >= 0 ? lhs : rhs;
5053 } else if (compare != (lhsSigned ? 1 : -1)) {
5054 // The unsigned type has greater than or equal rank to the
5055 // signed type, so use the unsigned type
5056 destType = lhsSigned ? rhs : lhs;
5057 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5058 // The two types are different widths; if we are here, that
5059 // means the signed type is larger than the unsigned type, so
5060 // use the signed type.
5061 destType = lhsSigned ? lhs : rhs;
5062 } else {
5063 // The signed type is higher-ranked than the unsigned type,
5064 // but isn't actually any bigger (like unsigned int and long
5065 // on most 32-bit systems). Use the unsigned type corresponding
5066 // to the signed type.
5067 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5068 }
5069 return destType;
5070}