blob: 81b56add8ee02d94b2e4782abe5495f35b24b2c3 [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),
Ted Kremenek6aead3a2010-05-10 20:40:08 +000049 BuiltinInfo(builtins),
50 DeclarationNames(*this),
51 ExternalSource(0), PrintingPolicy(LOpts),
John McCallc62bb642010-03-24 05:22:00 +000052 LastSDM(0, 0) {
David Chisnall9f57c292009-08-17 16:35:33 +000053 ObjCIdRedefinitionType = QualType();
54 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian04b258c2009-11-25 23:07:42 +000055 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +000056 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +000057 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +000058 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +000059}
60
Chris Lattnerd5973eb2006-11-12 00:53:46 +000061ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +000062 // Release the DenseMaps associated with DeclContext objects.
63 // FIXME: Is this the ideal solution?
64 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +000065
66 // Release all of the memory associated with overridden C++ methods.
67 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
68 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
69 OM != OMEnd; ++OM)
70 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +000071
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000072 if (FreeMemory) {
73 // Deallocate all the types.
74 while (!Types.empty()) {
75 Types.back()->Destroy(*this);
76 Types.pop_back();
77 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +000078
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000079 for (llvm::FoldingSet<ExtQuals>::iterator
80 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
81 // Increment in loop to prevent using deallocated memory.
John McCall8ccfcb52009-09-24 19:53:00 +000082 Deallocate(&*I++);
Nuno Lopese013c7f2008-12-17 22:30:25 +000083 }
Nuno Lopese013c7f2008-12-17 22:30:25 +000084
Ted Kremenekc3015a92010-03-08 20:56:29 +000085 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
86 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
87 // Increment in loop to prevent using deallocated memory.
88 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
89 R->Destroy(*this);
90 }
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000091
Ted Kremenekc3015a92010-03-08 20:56:29 +000092 for (llvm::DenseMap<const ObjCContainerDecl*,
93 const ASTRecordLayout*>::iterator
94 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
95 // Increment in loop to prevent using deallocated memory.
96 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
97 R->Destroy(*this);
98 }
Nuno Lopese013c7f2008-12-17 22:30:25 +000099 }
100
Douglas Gregorf21eb492009-03-26 23:50:42 +0000101 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000102 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
103 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000104 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000105 NNS != NNSEnd; ) {
106 // Increment in loop to prevent using deallocated memory.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000107 (*NNS++).Destroy(*this);
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000108 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000109
110 if (GlobalNestedNameSpecifier)
111 GlobalNestedNameSpecifier->Destroy(*this);
112
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000113 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000114}
115
Mike Stump11289f42009-09-09 15:08:12 +0000116void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000117ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
118 ExternalSource.reset(Source.take());
119}
120
Chris Lattner4eb445d2007-01-26 01:27:23 +0000121void ASTContext::PrintStats() const {
122 fprintf(stderr, "*** AST Context Stats:\n");
123 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000124
Douglas Gregora30d0462009-05-26 14:40:08 +0000125 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000126#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000127#define ABSTRACT_TYPE(Name, Parent)
128#include "clang/AST/TypeNodes.def"
129 0 // Extra
130 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000131
Chris Lattner4eb445d2007-01-26 01:27:23 +0000132 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
133 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000134 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000135 }
136
Douglas Gregora30d0462009-05-26 14:40:08 +0000137 unsigned Idx = 0;
138 unsigned TotalBytes = 0;
139#define TYPE(Name, Parent) \
140 if (counts[Idx]) \
141 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
142 TotalBytes += counts[Idx] * sizeof(Name##Type); \
143 ++Idx;
144#define ABSTRACT_TYPE(Name, Parent)
145#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000146
Douglas Gregora30d0462009-05-26 14:40:08 +0000147 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148
149 if (ExternalSource.get()) {
150 fprintf(stderr, "\n");
151 ExternalSource->PrintStats();
152 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000153}
154
155
John McCall48f2d582009-10-23 23:03:21 +0000156void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000157 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000158 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000159 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000160}
161
Chris Lattner970e54e2006-11-12 00:37:36 +0000162void ASTContext::InitBuiltinTypes() {
163 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattner970e54e2006-11-12 00:37:36 +0000165 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000166 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattner970e54e2006-11-12 00:37:36 +0000168 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000169 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000170 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000171 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000172 InitBuiltinType(CharTy, BuiltinType::Char_S);
173 else
174 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000175 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000176 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
177 InitBuiltinType(ShortTy, BuiltinType::Short);
178 InitBuiltinType(IntTy, BuiltinType::Int);
179 InitBuiltinType(LongTy, BuiltinType::Long);
180 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattner970e54e2006-11-12 00:37:36 +0000182 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000183 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
184 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
185 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
186 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
187 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000188
Chris Lattner970e54e2006-11-12 00:37:36 +0000189 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000190 InitBuiltinType(FloatTy, BuiltinType::Float);
191 InitBuiltinType(DoubleTy, BuiltinType::Double);
192 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000193
Chris Lattnerf122cef2009-04-30 02:43:43 +0000194 // GNU extension, 128-bit integers.
195 InitBuiltinType(Int128Ty, BuiltinType::Int128);
196 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
197
Chris Lattner007cb022009-02-26 23:43:47 +0000198 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
199 InitBuiltinType(WCharTy, BuiltinType::WChar);
200 else // C99
201 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000202
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000203 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
204 InitBuiltinType(Char16Ty, BuiltinType::Char16);
205 else // C99
206 Char16Ty = getFromTargetType(Target.getChar16Type());
207
208 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
209 InitBuiltinType(Char32Ty, BuiltinType::Char32);
210 else // C99
211 Char32Ty = getFromTargetType(Target.getChar32Type());
212
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000213 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000214 InitBuiltinType(OverloadTy, BuiltinType::Overload);
215
216 // Placeholder type for type-dependent expressions whose type is
217 // completely unknown. No code should ever check a type against
218 // DependentTy and users should never see it; however, it is here to
219 // help diagnose failures to properly check for type-dependent
220 // expressions.
221 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000222
Mike Stump11289f42009-09-09 15:08:12 +0000223 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000224 // not yet been deduced.
225 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000226
Chris Lattner970e54e2006-11-12 00:37:36 +0000227 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000228 FloatComplexTy = getComplexType(FloatTy);
229 DoubleComplexTy = getComplexType(DoubleTy);
230 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000231
Steve Naroff66e9f332007-10-15 14:41:52 +0000232 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000233
Steve Naroff1329fa02009-07-15 18:40:39 +0000234 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
235 ObjCIdTypedefType = QualType();
236 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000237 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000238
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000239 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000240 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
241 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000242 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000243
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000244 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000245
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000246 // void * type
247 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000248
249 // nullptr type (C++0x 2.14.7)
250 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000251}
252
Douglas Gregor86d142a2009-10-08 07:24:58 +0000253MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000254ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000255 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000256 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000257 = InstantiatedFromStaticDataMember.find(Var);
258 if (Pos == InstantiatedFromStaticDataMember.end())
259 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000260
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000261 return Pos->second;
262}
263
Mike Stump11289f42009-09-09 15:08:12 +0000264void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000265ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
266 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000267 assert(Inst->isStaticDataMember() && "Not a static data member");
268 assert(Tmpl->isStaticDataMember() && "Not a static data member");
269 assert(!InstantiatedFromStaticDataMember[Inst] &&
270 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000271 InstantiatedFromStaticDataMember[Inst]
272 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000273}
274
John McCalle61f2ba2009-11-18 02:36:19 +0000275NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000276ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000277 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000278 = InstantiatedFromUsingDecl.find(UUD);
279 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000280 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000282 return Pos->second;
283}
284
285void
John McCallb96ec562009-12-04 22:46:56 +0000286ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
287 assert((isa<UsingDecl>(Pattern) ||
288 isa<UnresolvedUsingValueDecl>(Pattern) ||
289 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
290 "pattern decl is not a using decl");
291 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
292 InstantiatedFromUsingDecl[Inst] = Pattern;
293}
294
295UsingShadowDecl *
296ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
297 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
298 = InstantiatedFromUsingShadowDecl.find(Inst);
299 if (Pos == InstantiatedFromUsingShadowDecl.end())
300 return 0;
301
302 return Pos->second;
303}
304
305void
306ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
307 UsingShadowDecl *Pattern) {
308 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
309 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000310}
311
Anders Carlsson5da84842009-09-01 04:26:58 +0000312FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
313 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
314 = InstantiatedFromUnnamedFieldDecl.find(Field);
315 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
316 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Anders Carlsson5da84842009-09-01 04:26:58 +0000318 return Pos->second;
319}
320
321void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
322 FieldDecl *Tmpl) {
323 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
324 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
325 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
326 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000327
Anders Carlsson5da84842009-09-01 04:26:58 +0000328 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
329}
330
Douglas Gregor832940b2010-03-02 23:58:15 +0000331ASTContext::overridden_cxx_method_iterator
332ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
333 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
334 = OverriddenMethods.find(Method);
335 if (Pos == OverriddenMethods.end())
336 return 0;
337
338 return Pos->second.begin();
339}
340
341ASTContext::overridden_cxx_method_iterator
342ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
343 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
344 = OverriddenMethods.find(Method);
345 if (Pos == OverriddenMethods.end())
346 return 0;
347
348 return Pos->second.end();
349}
350
351void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
352 const CXXMethodDecl *Overridden) {
353 OverriddenMethods[Method].push_back(Overridden);
354}
355
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000356namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000357 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000358 : std::binary_function<SourceRange, SourceRange, bool> {
359 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000360
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000361 public:
362 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000363
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000364 bool operator()(SourceRange X, SourceRange Y) {
365 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
366 }
367 };
368}
369
Chris Lattner53cfe802007-07-18 17:52:12 +0000370//===----------------------------------------------------------------------===//
371// Type Sizing and Analysis
372//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000373
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000374/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
375/// scalar floating point type.
376const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000377 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000378 assert(BT && "Not a floating point type!");
379 switch (BT->getKind()) {
380 default: assert(0 && "Not a floating point type!");
381 case BuiltinType::Float: return Target.getFloatFormat();
382 case BuiltinType::Double: return Target.getDoubleFormat();
383 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
384 }
385}
386
Ken Dyck160146e2010-01-27 17:10:57 +0000387/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000388/// specified decl. Note that bitfields do not have a valid alignment, so
389/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000390/// If @p RefAsPointee, references are treated like their underlying type
391/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000392CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000393 unsigned Align = Target.getCharWidth();
394
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000395 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000396 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedman19a546c2009-02-22 02:56:25 +0000397
Chris Lattner68061312009-01-24 21:53:27 +0000398 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
399 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000400 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000401 if (RefAsPointee)
402 T = RT->getPointeeType();
403 else
404 T = getPointerType(RT->getPointeeType());
405 }
406 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000407 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000408 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
409 T = cast<ArrayType>(T)->getElementType();
410
411 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
412 }
Charles Davis3fc51072010-02-23 04:52:00 +0000413 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
414 // In the case of a field in a packed struct, we want the minimum
415 // of the alignment of the field and the alignment of the struct.
416 Align = std::min(Align,
417 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
418 }
Chris Lattner68061312009-01-24 21:53:27 +0000419 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000420
Ken Dyck160146e2010-01-27 17:10:57 +0000421 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000422}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000423
Chris Lattner983a8bb2007-07-13 22:13:22 +0000424/// getTypeSize - Return the size of the specified type, in bits. This method
425/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000426///
427/// FIXME: Pointers into different addr spaces could have different sizes and
428/// alignment requirements: getPointerInfo should take an AddrSpace, this
429/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000430std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000431ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000432 uint64_t Width=0;
433 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000434 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000435#define TYPE(Class, Base)
436#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000437#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000438#define DEPENDENT_TYPE(Class, Base) case Type::Class:
439#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000440 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000441 break;
442
Chris Lattner355332d2007-07-13 22:27:08 +0000443 case Type::FunctionNoProto:
444 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000445 // GCC extension: alignof(function) = 32 bits
446 Width = 0;
447 Align = 32;
448 break;
449
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000450 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000451 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000452 Width = 0;
453 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
454 break;
455
Steve Naroff5c131802007-08-30 01:06:46 +0000456 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000457 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000458
Chris Lattner37e05872008-03-05 18:54:05 +0000459 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000460 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000461 Align = EltInfo.second;
462 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000463 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000464 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000465 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000466 const VectorType *VT = cast<VectorType>(T);
467 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
468 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000469 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000470 // If the alignment is not a power of 2, round up to the next power of 2.
471 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000472 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000473 Align = llvm::NextPowerOf2(Align);
474 Width = llvm::RoundUpToAlignment(Width, Align);
475 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000476 break;
477 }
Chris Lattner647fb222007-07-18 18:26:58 +0000478
Chris Lattner7570e9c2008-03-08 08:52:55 +0000479 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000480 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000481 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000482 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000483 // GCC extension: alignof(void) = 8 bits.
484 Width = 0;
485 Align = 8;
486 break;
487
Chris Lattner6a4f7452007-12-19 19:23:28 +0000488 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000489 Width = Target.getBoolWidth();
490 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000491 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000492 case BuiltinType::Char_S:
493 case BuiltinType::Char_U:
494 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000495 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000496 Width = Target.getCharWidth();
497 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000498 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000499 case BuiltinType::WChar:
500 Width = Target.getWCharWidth();
501 Align = Target.getWCharAlign();
502 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000503 case BuiltinType::Char16:
504 Width = Target.getChar16Width();
505 Align = Target.getChar16Align();
506 break;
507 case BuiltinType::Char32:
508 Width = Target.getChar32Width();
509 Align = Target.getChar32Align();
510 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000511 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000512 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000513 Width = Target.getShortWidth();
514 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000515 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000516 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000517 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000518 Width = Target.getIntWidth();
519 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000520 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000521 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000522 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000523 Width = Target.getLongWidth();
524 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000525 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000526 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000527 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000528 Width = Target.getLongLongWidth();
529 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000530 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000531 case BuiltinType::Int128:
532 case BuiltinType::UInt128:
533 Width = 128;
534 Align = 128; // int128_t is 128-bit aligned on all targets.
535 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000536 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000537 Width = Target.getFloatWidth();
538 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000539 break;
540 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000541 Width = Target.getDoubleWidth();
542 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000543 break;
544 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000545 Width = Target.getLongDoubleWidth();
546 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000547 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000548 case BuiltinType::NullPtr:
549 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
550 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000551 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000552 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000553 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000554 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000555 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000556 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000557 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000558 case Type::BlockPointer: {
559 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
560 Width = Target.getPointerWidth(AS);
561 Align = Target.getPointerAlign(AS);
562 break;
563 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000564 case Type::LValueReference:
565 case Type::RValueReference: {
566 // alignof and sizeof should never enter this code path here, so we go
567 // the pointer route.
568 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
569 Width = Target.getPointerWidth(AS);
570 Align = Target.getPointerAlign(AS);
571 break;
572 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000573 case Type::Pointer: {
574 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000575 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000576 Align = Target.getPointerAlign(AS);
577 break;
578 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000579 case Type::MemberPointer: {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000580 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000581 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000582 getTypeInfo(getPointerDiffType());
583 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000584 if (Pointee->isFunctionType())
585 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000586 Align = PtrDiffInfo.second;
587 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000588 }
Chris Lattner647fb222007-07-18 18:26:58 +0000589 case Type::Complex: {
590 // Complex types have the same alignment as their elements, but twice the
591 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000592 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000593 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000594 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000595 Align = EltInfo.second;
596 break;
597 }
Devang Pateldbb72632008-06-04 21:54:36 +0000598 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000599 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000600 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
601 Width = Layout.getSize();
602 Align = Layout.getAlignment();
603 break;
604 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000605 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000606 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000607 const TagType *TT = cast<TagType>(T);
608
609 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000610 Width = 1;
611 Align = 1;
612 break;
613 }
Mike Stump11289f42009-09-09 15:08:12 +0000614
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000615 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000616 return getTypeInfo(ET->getDecl()->getIntegerType());
617
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000618 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000619 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
620 Width = Layout.getSize();
621 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000622 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000623 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000624
Chris Lattner63d2b362009-10-22 05:17:15 +0000625 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000626 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
627 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000628
Douglas Gregoref462e62009-04-30 17:32:17 +0000629 case Type::Typedef: {
630 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000631 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000632 Align = std::max(Aligned->getMaxAlignment(),
633 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregoref462e62009-04-30 17:32:17 +0000634 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
635 } else
636 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000637 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000638 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000639
640 case Type::TypeOfExpr:
641 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
642 .getTypePtr());
643
644 case Type::TypeOf:
645 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
646
Anders Carlsson81df7b82009-06-24 19:06:50 +0000647 case Type::Decltype:
648 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
649 .getTypePtr());
650
Abramo Bagnara6150c882010-05-11 21:36:43 +0000651 case Type::Elaborated:
652 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000653
Douglas Gregoref462e62009-04-30 17:32:17 +0000654 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000655 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000656 "Cannot request the size of a dependent type");
657 // FIXME: this is likely to be wrong once we support template
658 // aliases, since a template alias could refer to a typedef that
659 // has an __aligned__ attribute on it.
660 return getTypeInfo(getCanonicalType(T));
661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Chris Lattner53cfe802007-07-18 17:52:12 +0000663 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000664 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000665}
666
Ken Dyck8c89d592009-12-22 14:23:30 +0000667/// getTypeSizeInChars - Return the size of the specified type, in characters.
668/// This method does not work on incomplete types.
669CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000670 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000671}
672CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000673 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000674}
675
Ken Dycka6046ab2010-01-26 17:25:18 +0000676/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000677/// characters. This method does not work on incomplete types.
678CharUnits ASTContext::getTypeAlignInChars(QualType T) {
679 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
680}
681CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
682 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
683}
684
Chris Lattnera3402cd2009-01-27 18:08:34 +0000685/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
686/// type for the current target in bits. This can be different than the ABI
687/// alignment in cases where it is beneficial for performance to overalign
688/// a data type.
689unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
690 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000691
692 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000693 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000694 T = CT->getElementType().getTypePtr();
695 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
696 T->isSpecificBuiltinType(BuiltinType::LongLong))
697 return std::max(ABIAlign, (unsigned)getTypeSize(T));
698
Chris Lattnera3402cd2009-01-27 18:08:34 +0000699 return ABIAlign;
700}
701
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000702static void CollectLocalObjCIvars(ASTContext *Ctx,
703 const ObjCInterfaceDecl *OI,
704 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000705 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
706 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000707 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000708 if (!IVDecl->isInvalidDecl())
709 Fields.push_back(cast<FieldDecl>(IVDecl));
710 }
711}
712
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000713void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
714 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
715 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
716 CollectObjCIvars(SuperClass, Fields);
717 CollectLocalObjCIvars(this, OI, Fields);
718}
719
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000720/// ShallowCollectObjCIvars -
721/// Collect all ivars, including those synthesized, in the current class.
722///
723void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000724 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000725 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
726 E = OI->ivar_end(); I != E; ++I) {
727 Ivars.push_back(*I);
728 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000729
730 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000731}
732
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000733/// CollectNonClassIvars -
734/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000735/// This includes synthesized ivars (via @synthesize) and those in
736// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000737///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000738void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000739 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000740 // Find ivars declared in class extension.
741 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
742 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
743 E = CDecl->ivar_end(); I != E; ++I) {
744 Ivars.push_back(*I);
745 }
746 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000747
Ted Kremenek86838aa2010-03-11 19:44:54 +0000748 // Also add any ivar defined in this class's implementation. This
749 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000750 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
751 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
752 E = ImplDecl->ivar_end(); I != E; ++I)
753 Ivars.push_back(*I);
754 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000755}
756
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000757/// CollectInheritedProtocols - Collect all protocols in current class and
758/// those inherited by it.
759void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000760 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000761 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
762 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
763 PE = OI->protocol_end(); P != PE; ++P) {
764 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000765 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000766 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000767 PE = Proto->protocol_end(); P != PE; ++P) {
768 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000769 CollectInheritedProtocols(*P, Protocols);
770 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000771 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000772
773 // Categories of this Interface.
774 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
775 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
776 CollectInheritedProtocols(CDeclChain, Protocols);
777 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
778 while (SD) {
779 CollectInheritedProtocols(SD, Protocols);
780 SD = SD->getSuperClass();
781 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000782 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000783 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
784 PE = OC->protocol_end(); P != PE; ++P) {
785 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000786 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000787 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
788 PE = Proto->protocol_end(); P != PE; ++P)
789 CollectInheritedProtocols(*P, Protocols);
790 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000791 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000792 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
793 PE = OP->protocol_end(); P != PE; ++P) {
794 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000795 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000796 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
797 PE = Proto->protocol_end(); P != PE; ++P)
798 CollectInheritedProtocols(*P, Protocols);
799 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000800 }
801}
802
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000803unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
804 unsigned count = 0;
805 // Count ivars declared in class extension.
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000806 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension())
807 count += CDecl->ivar_size();
808
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000809 // Count ivar defined in this class's implementation. This
810 // includes synthesized ivars.
811 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000812 count += ImplDecl->ivar_size();
813
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000814 return count;
815}
816
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000817/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
818ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
819 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
820 I = ObjCImpls.find(D);
821 if (I != ObjCImpls.end())
822 return cast<ObjCImplementationDecl>(I->second);
823 return 0;
824}
825/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
826ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
827 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
828 I = ObjCImpls.find(D);
829 if (I != ObjCImpls.end())
830 return cast<ObjCCategoryImplDecl>(I->second);
831 return 0;
832}
833
834/// \brief Set the implementation of ObjCInterfaceDecl.
835void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
836 ObjCImplementationDecl *ImplD) {
837 assert(IFaceD && ImplD && "Passed null params");
838 ObjCImpls[IFaceD] = ImplD;
839}
840/// \brief Set the implementation of ObjCCategoryDecl.
841void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
842 ObjCCategoryImplDecl *ImplD) {
843 assert(CatD && ImplD && "Passed null params");
844 ObjCImpls[CatD] = ImplD;
845}
846
John McCallbcd03502009-12-07 02:54:59 +0000847/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000848///
John McCallbcd03502009-12-07 02:54:59 +0000849/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000850/// the TypeLoc wrappers.
851///
852/// \param T the type that will be the basis for type source info. This type
853/// should refer to how the declarator was written in source code, not to
854/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000855TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000856 unsigned DataSize) {
857 if (!DataSize)
858 DataSize = TypeLoc::getFullDataSizeForType(T);
859 else
860 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000861 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000862
John McCallbcd03502009-12-07 02:54:59 +0000863 TypeSourceInfo *TInfo =
864 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
865 new (TInfo) TypeSourceInfo(T);
866 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000867}
868
John McCallbcd03502009-12-07 02:54:59 +0000869TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000870 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +0000871 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +0000872 DI->getTypeLoc().initialize(L);
873 return DI;
874}
875
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000876/// getInterfaceLayoutImpl - Get or compute information about the
877/// layout of the given interface.
878///
879/// \param Impl - If given, also include the layout of the interface's
880/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000881const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000882ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
883 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000884 assert(!D->isForwardDecl() && "Invalid interface decl!");
885
Devang Pateldbb72632008-06-04 21:54:36 +0000886 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000887 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000888 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
889 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
890 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000891
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000892 // Add in synthesized ivar count if laying out an implementation.
893 if (Impl) {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000894 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000895 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000896 // entry. Note we can't cache this because we simply free all
897 // entries later; however we shouldn't look up implementations
898 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000899 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000900 return getObjCLayout(D, 0);
901 }
902
Mike Stump11289f42009-09-09 15:08:12 +0000903 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000904 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
905 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000906
Devang Pateldbb72632008-06-04 21:54:36 +0000907 return *NewEntry;
908}
909
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000910const ASTRecordLayout &
911ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
912 return getObjCLayout(D, 0);
913}
914
915const ASTRecordLayout &
916ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
917 return getObjCLayout(D->getClassInterface(), D);
918}
919
Devang Patele11664a2007-11-01 19:11:01 +0000920/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +0000921/// specified record (struct/union/class), which indicates its size and field
922/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +0000923const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000924 D = D->getDefinition();
Ted Kremenek21475702008-09-05 17:16:31 +0000925 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +0000926
Chris Lattner53cfe802007-07-18 17:52:12 +0000927 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +0000928 // Note that we can't save a reference to the entry because this function
929 // is recursive.
930 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +0000931 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +0000932
Mike Stump11289f42009-09-09 15:08:12 +0000933 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000934 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +0000935 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000936
Daniel Dunbarccabe482010-04-19 20:44:53 +0000937 if (getLangOptions().DumpRecordLayouts) {
938 llvm::errs() << "\n*** Dumping AST Record Layout\n";
939 DumpRecordLayout(D, llvm::errs());
940 }
941
Chris Lattner647fb222007-07-18 18:26:58 +0000942 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +0000943}
944
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000945const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000946 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000947 assert(RD && "Cannot get key function for forward declarations!");
948
949 const CXXMethodDecl *&Entry = KeyFunctions[RD];
950 if (!Entry)
951 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
952 else
953 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
954 "Key function changed!");
955
956 return Entry;
957}
958
Chris Lattner983a8bb2007-07-13 22:13:22 +0000959//===----------------------------------------------------------------------===//
960// Type creation/memoization methods
961//===----------------------------------------------------------------------===//
962
John McCall8ccfcb52009-09-24 19:53:00 +0000963QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
964 unsigned Fast = Quals.getFastQualifiers();
965 Quals.removeFastQualifiers();
966
967 // Check if we've already instantiated this type.
968 llvm::FoldingSetNodeID ID;
969 ExtQuals::Profile(ID, TypeNode, Quals);
970 void *InsertPos = 0;
971 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
972 assert(EQ->getQualifiers() == Quals);
973 QualType T = QualType(EQ, Fast);
974 return T;
975 }
976
John McCall90d1c2d2009-09-24 23:30:46 +0000977 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000978 ExtQualNodes.InsertNode(New, InsertPos);
979 QualType T = QualType(New, Fast);
980 return T;
981}
982
983QualType ASTContext::getVolatileType(QualType T) {
984 QualType CanT = getCanonicalType(T);
985 if (CanT.isVolatileQualified()) return T;
986
987 QualifierCollector Quals;
988 const Type *TypeNode = Quals.strip(T);
989 Quals.addVolatile();
990
991 return getExtQualType(TypeNode, Quals);
992}
993
Fariborz Jahanianece85822009-02-17 18:27:45 +0000994QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +0000995 QualType CanT = getCanonicalType(T);
996 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +0000997 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +0000998
John McCall8ccfcb52009-09-24 19:53:00 +0000999 // If we are composing extended qualifiers together, merge together
1000 // into one ExtQuals node.
1001 QualifierCollector Quals;
1002 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001003
John McCall8ccfcb52009-09-24 19:53:00 +00001004 // If this type already has an address space specified, it cannot get
1005 // another one.
1006 assert(!Quals.hasAddressSpace() &&
1007 "Type cannot be in multiple addr spaces!");
1008 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001009
John McCall8ccfcb52009-09-24 19:53:00 +00001010 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001011}
1012
Chris Lattnerd60183d2009-02-18 22:53:11 +00001013QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001014 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001015 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001016 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001017 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001018
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001019 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001020 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001021 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001022 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1023 return getPointerType(ResultType);
1024 }
1025 }
Mike Stump11289f42009-09-09 15:08:12 +00001026
John McCall8ccfcb52009-09-24 19:53:00 +00001027 // If we are composing extended qualifiers together, merge together
1028 // into one ExtQuals node.
1029 QualifierCollector Quals;
1030 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001031
John McCall8ccfcb52009-09-24 19:53:00 +00001032 // If this type already has an ObjCGC specified, it cannot get
1033 // another one.
1034 assert(!Quals.hasObjCGCAttr() &&
1035 "Type cannot have multiple ObjCGCs!");
1036 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001037
John McCall8ccfcb52009-09-24 19:53:00 +00001038 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001039}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001040
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001041static QualType getExtFunctionType(ASTContext& Context, QualType T,
1042 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001043 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001044 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1045 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001046 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001047 if (ResultType == Pointee)
1048 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001049
1050 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001051 } else if (const BlockPointerType *BlockPointer
1052 = T->getAs<BlockPointerType>()) {
1053 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001054 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001055 if (ResultType == Pointee)
1056 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001057
1058 ResultType = Context.getBlockPointerType(ResultType);
1059 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001060 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001061 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001062
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001063 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001064 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001065 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001066 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001067 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001068 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001069 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1070 FPT->getNumArgs(), FPT->isVariadic(),
1071 FPT->getTypeQuals(),
1072 FPT->hasExceptionSpec(),
1073 FPT->hasAnyExceptionSpec(),
1074 FPT->getNumExceptions(),
1075 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001076 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001077 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001078 } else
1079 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001080
1081 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1082}
1083
1084QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001085 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001086 return getExtFunctionType(*this, T,
1087 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001088}
1089
1090QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001091 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001092 return getExtFunctionType(*this, T,
1093 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001094}
1095
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001096QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1097 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1098 return getExtFunctionType(*this, T,
1099 Info.withRegParm(RegParm));
1100}
1101
Chris Lattnerc6395932007-06-22 20:56:16 +00001102/// getComplexType - Return the uniqued reference to the type for a complex
1103/// number with the specified element type.
1104QualType ASTContext::getComplexType(QualType T) {
1105 // Unique pointers, to guarantee there is only one pointer of a particular
1106 // structure.
1107 llvm::FoldingSetNodeID ID;
1108 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001109
Chris Lattnerc6395932007-06-22 20:56:16 +00001110 void *InsertPos = 0;
1111 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1112 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001113
Chris Lattnerc6395932007-06-22 20:56:16 +00001114 // If the pointee type isn't canonical, this won't be a canonical type either,
1115 // so fill in the canonical type field.
1116 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001117 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001118 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001119
Chris Lattnerc6395932007-06-22 20:56:16 +00001120 // Get the new insert position for the node we care about.
1121 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001122 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001123 }
John McCall90d1c2d2009-09-24 23:30:46 +00001124 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001125 Types.push_back(New);
1126 ComplexTypes.InsertNode(New, InsertPos);
1127 return QualType(New, 0);
1128}
1129
Chris Lattner970e54e2006-11-12 00:37:36 +00001130/// getPointerType - Return the uniqued reference to the type for a pointer to
1131/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001132QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001133 // Unique pointers, to guarantee there is only one pointer of a particular
1134 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001135 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001136 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001137
Chris Lattner67521df2007-01-27 01:29:36 +00001138 void *InsertPos = 0;
1139 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001140 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001141
Chris Lattner7ccecb92006-11-12 08:50:50 +00001142 // If the pointee type isn't canonical, this won't be a canonical type either,
1143 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001144 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001145 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001146 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001147
Chris Lattner67521df2007-01-27 01:29:36 +00001148 // Get the new insert position for the node we care about.
1149 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001150 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001151 }
John McCall90d1c2d2009-09-24 23:30:46 +00001152 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001153 Types.push_back(New);
1154 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001155 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001156}
1157
Mike Stump11289f42009-09-09 15:08:12 +00001158/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001159/// a pointer to the specified block.
1160QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001161 assert(T->isFunctionType() && "block of function types only");
1162 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001163 // structure.
1164 llvm::FoldingSetNodeID ID;
1165 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001166
Steve Naroffec33ed92008-08-27 16:04:49 +00001167 void *InsertPos = 0;
1168 if (BlockPointerType *PT =
1169 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1170 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001171
1172 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001173 // type either so fill in the canonical type field.
1174 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001175 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001176 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001177
Steve Naroffec33ed92008-08-27 16:04:49 +00001178 // Get the new insert position for the node we care about.
1179 BlockPointerType *NewIP =
1180 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001181 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001182 }
John McCall90d1c2d2009-09-24 23:30:46 +00001183 BlockPointerType *New
1184 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001185 Types.push_back(New);
1186 BlockPointerTypes.InsertNode(New, InsertPos);
1187 return QualType(New, 0);
1188}
1189
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001190/// getLValueReferenceType - Return the uniqued reference to the type for an
1191/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001192QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001193 // Unique pointers, to guarantee there is only one pointer of a particular
1194 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001195 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001196 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001197
1198 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001199 if (LValueReferenceType *RT =
1200 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001201 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001202
John McCallfc93cf92009-10-22 22:37:11 +00001203 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1204
Bill Wendling3708c182007-05-27 10:15:43 +00001205 // If the referencee type isn't canonical, this won't be a canonical type
1206 // either, so fill in the canonical type field.
1207 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001208 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1209 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1210 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001211
Bill Wendling3708c182007-05-27 10:15:43 +00001212 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001213 LValueReferenceType *NewIP =
1214 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001215 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001216 }
1217
John McCall90d1c2d2009-09-24 23:30:46 +00001218 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001219 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1220 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001221 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001222 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001223
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001224 return QualType(New, 0);
1225}
1226
1227/// getRValueReferenceType - Return the uniqued reference to the type for an
1228/// rvalue reference to the specified type.
1229QualType ASTContext::getRValueReferenceType(QualType T) {
1230 // Unique pointers, to guarantee there is only one pointer of a particular
1231 // structure.
1232 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001233 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001234
1235 void *InsertPos = 0;
1236 if (RValueReferenceType *RT =
1237 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1238 return QualType(RT, 0);
1239
John McCallfc93cf92009-10-22 22:37:11 +00001240 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1241
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001242 // If the referencee type isn't canonical, this won't be a canonical type
1243 // either, so fill in the canonical type field.
1244 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001245 if (InnerRef || !T.isCanonical()) {
1246 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1247 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001248
1249 // Get the new insert position for the node we care about.
1250 RValueReferenceType *NewIP =
1251 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1252 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1253 }
1254
John McCall90d1c2d2009-09-24 23:30:46 +00001255 RValueReferenceType *New
1256 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001257 Types.push_back(New);
1258 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001259 return QualType(New, 0);
1260}
1261
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001262/// getMemberPointerType - Return the uniqued reference to the type for a
1263/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001264QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001265 // Unique pointers, to guarantee there is only one pointer of a particular
1266 // structure.
1267 llvm::FoldingSetNodeID ID;
1268 MemberPointerType::Profile(ID, T, Cls);
1269
1270 void *InsertPos = 0;
1271 if (MemberPointerType *PT =
1272 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1273 return QualType(PT, 0);
1274
1275 // If the pointee or class type isn't canonical, this won't be a canonical
1276 // type either, so fill in the canonical type field.
1277 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001278 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001279 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1280
1281 // Get the new insert position for the node we care about.
1282 MemberPointerType *NewIP =
1283 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1284 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1285 }
John McCall90d1c2d2009-09-24 23:30:46 +00001286 MemberPointerType *New
1287 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001288 Types.push_back(New);
1289 MemberPointerTypes.InsertNode(New, InsertPos);
1290 return QualType(New, 0);
1291}
1292
Mike Stump11289f42009-09-09 15:08:12 +00001293/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001294/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001295QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001296 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001297 ArrayType::ArraySizeModifier ASM,
1298 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001299 assert((EltTy->isDependentType() ||
1300 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001301 "Constant array of VLAs is illegal!");
1302
Chris Lattnere2df3f92009-05-13 04:12:56 +00001303 // Convert the array size into a canonical width matching the pointer size for
1304 // the target.
1305 llvm::APInt ArySize(ArySizeIn);
1306 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001307
Chris Lattner23b7eb62007-06-15 23:05:46 +00001308 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001309 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001310
Chris Lattner36f8e652007-01-27 08:31:04 +00001311 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001312 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001313 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001314 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001315
Chris Lattner7ccecb92006-11-12 08:50:50 +00001316 // If the element type isn't canonical, this won't be a canonical type either,
1317 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001318 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001319 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001320 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001321 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001322 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001323 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001324 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001325 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001326 }
Mike Stump11289f42009-09-09 15:08:12 +00001327
John McCall90d1c2d2009-09-24 23:30:46 +00001328 ConstantArrayType *New = new(*this,TypeAlignment)
1329 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001330 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001331 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001332 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001333}
1334
Steve Naroffcadebd02007-08-30 18:14:25 +00001335/// getVariableArrayType - Returns a non-unique reference to the type for a
1336/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001337QualType ASTContext::getVariableArrayType(QualType EltTy,
1338 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001339 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001340 unsigned EltTypeQuals,
1341 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001342 // Since we don't unique expressions, it isn't possible to unique VLA's
1343 // that have an expression provided for their size.
1344
John McCall90d1c2d2009-09-24 23:30:46 +00001345 VariableArrayType *New = new(*this, TypeAlignment)
1346 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001347
1348 VariableArrayTypes.push_back(New);
1349 Types.push_back(New);
1350 return QualType(New, 0);
1351}
1352
Douglas Gregor4619e432008-12-05 23:32:09 +00001353/// getDependentSizedArrayType - Returns a non-unique reference to
1354/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001355/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001356QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1357 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001358 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001359 unsigned EltTypeQuals,
1360 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001361 assert((!NumElts || NumElts->isTypeDependent() ||
1362 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001363 "Size must be type- or value-dependent!");
1364
Douglas Gregorf3f95522009-07-31 00:23:35 +00001365 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001366 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001367 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001368
1369 if (NumElts) {
1370 // Dependently-sized array types that do not have a specified
1371 // number of elements will have their sizes deduced from an
1372 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001373 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1374 EltTypeQuals, NumElts);
1375
1376 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1377 }
1378
Douglas Gregorf3f95522009-07-31 00:23:35 +00001379 DependentSizedArrayType *New;
1380 if (Canon) {
1381 // We already have a canonical version of this array type; use it as
1382 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001383 New = new (*this, TypeAlignment)
1384 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1385 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001386 } else {
1387 QualType CanonEltTy = getCanonicalType(EltTy);
1388 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001389 New = new (*this, TypeAlignment)
1390 DependentSizedArrayType(*this, EltTy, QualType(),
1391 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001392
Douglas Gregorc42075a2010-02-04 18:10:26 +00001393 if (NumElts) {
1394 DependentSizedArrayType *CanonCheck
1395 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1396 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1397 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001398 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001399 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001400 } else {
1401 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1402 ASM, EltTypeQuals,
1403 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001404 New = new (*this, TypeAlignment)
1405 DependentSizedArrayType(*this, EltTy, Canon,
1406 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001407 }
1408 }
Mike Stump11289f42009-09-09 15:08:12 +00001409
Douglas Gregor4619e432008-12-05 23:32:09 +00001410 Types.push_back(New);
1411 return QualType(New, 0);
1412}
1413
Eli Friedmanbd258282008-02-15 18:16:39 +00001414QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1415 ArrayType::ArraySizeModifier ASM,
1416 unsigned EltTypeQuals) {
1417 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001418 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001419
1420 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001421 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001422 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1423 return QualType(ATP, 0);
1424
1425 // If the element type isn't canonical, this won't be a canonical type
1426 // either, so fill in the canonical type field.
1427 QualType Canonical;
1428
John McCallb692a092009-10-22 20:10:53 +00001429 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001430 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001431 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001432
1433 // Get the new insert position for the node we care about.
1434 IncompleteArrayType *NewIP =
1435 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001436 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001437 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001438
John McCall90d1c2d2009-09-24 23:30:46 +00001439 IncompleteArrayType *New = new (*this, TypeAlignment)
1440 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001441
1442 IncompleteArrayTypes.InsertNode(New, InsertPos);
1443 Types.push_back(New);
1444 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001445}
1446
Steve Naroff91fcddb2007-07-18 18:00:27 +00001447/// getVectorType - Return the unique reference to a vector type of
1448/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001449QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1450 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001451 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001452
Chris Lattner76a00cf2008-04-06 22:59:24 +00001453 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001454 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001455
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001456 // Check if we've already instantiated a vector of this type.
1457 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001458 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1459 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001460 void *InsertPos = 0;
1461 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1462 return QualType(VTP, 0);
1463
1464 // If the element type isn't canonical, this won't be a canonical type either,
1465 // so fill in the canonical type field.
1466 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001467 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1468 Canonical = getVectorType(getCanonicalType(vecType),
1469 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001470
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001471 // Get the new insert position for the node we care about.
1472 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001473 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001474 }
John McCall90d1c2d2009-09-24 23:30:46 +00001475 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001476 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001477 VectorTypes.InsertNode(New, InsertPos);
1478 Types.push_back(New);
1479 return QualType(New, 0);
1480}
1481
Nate Begemance4d7fc2008-04-18 23:10:10 +00001482/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001483/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001484QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001485 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001486
Chris Lattner76a00cf2008-04-06 22:59:24 +00001487 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001488 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001489
Steve Naroff91fcddb2007-07-18 18:00:27 +00001490 // Check if we've already instantiated a vector of this type.
1491 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001492 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001493 void *InsertPos = 0;
1494 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1495 return QualType(VTP, 0);
1496
1497 // If the element type isn't canonical, this won't be a canonical type either,
1498 // so fill in the canonical type field.
1499 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001500 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001501 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001502
Steve Naroff91fcddb2007-07-18 18:00:27 +00001503 // Get the new insert position for the node we care about.
1504 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001505 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001506 }
John McCall90d1c2d2009-09-24 23:30:46 +00001507 ExtVectorType *New = new (*this, TypeAlignment)
1508 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001509 VectorTypes.InsertNode(New, InsertPos);
1510 Types.push_back(New);
1511 return QualType(New, 0);
1512}
1513
Mike Stump11289f42009-09-09 15:08:12 +00001514QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001515 Expr *SizeExpr,
1516 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001517 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001518 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001519 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001520
Douglas Gregor352169a2009-07-31 03:54:25 +00001521 void *InsertPos = 0;
1522 DependentSizedExtVectorType *Canon
1523 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1524 DependentSizedExtVectorType *New;
1525 if (Canon) {
1526 // We already have a canonical version of this array type; use it as
1527 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001528 New = new (*this, TypeAlignment)
1529 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1530 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001531 } else {
1532 QualType CanonVecTy = getCanonicalType(vecType);
1533 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001534 New = new (*this, TypeAlignment)
1535 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1536 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001537
1538 DependentSizedExtVectorType *CanonCheck
1539 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1540 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1541 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001542 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1543 } else {
1544 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1545 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001546 New = new (*this, TypeAlignment)
1547 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001548 }
1549 }
Mike Stump11289f42009-09-09 15:08:12 +00001550
Douglas Gregor758a8692009-06-17 21:51:59 +00001551 Types.push_back(New);
1552 return QualType(New, 0);
1553}
1554
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001555/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001556///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001557QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1558 const FunctionType::ExtInfo &Info) {
1559 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001560 // Unique functions, to guarantee there is only one function of a particular
1561 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001562 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001563 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001564
Chris Lattner47955de2007-01-27 08:37:20 +00001565 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001566 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001567 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001568 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001569
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001570 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001571 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001572 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001573 Canonical =
1574 getFunctionNoProtoType(getCanonicalType(ResultTy),
1575 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001576
Chris Lattner47955de2007-01-27 08:37:20 +00001577 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001578 FunctionNoProtoType *NewIP =
1579 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001580 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001581 }
Mike Stump11289f42009-09-09 15:08:12 +00001582
John McCall90d1c2d2009-09-24 23:30:46 +00001583 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001584 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001585 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001586 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001587 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001588}
1589
1590/// getFunctionType - Return a normal function type with a typed argument
1591/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001592QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001593 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001594 unsigned TypeQuals, bool hasExceptionSpec,
1595 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001596 const QualType *ExArray,
1597 const FunctionType::ExtInfo &Info) {
1598 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001599 // Unique functions, to guarantee there is only one function of a particular
1600 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001601 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001602 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001603 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001604 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001605
1606 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001607 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001608 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001609 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001610
1611 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001612 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001613 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001614 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001615 isCanonical = false;
1616
1617 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001618 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001619 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001620 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001621 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001622 CanonicalArgs.reserve(NumArgs);
1623 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001624 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001625
Chris Lattner76a00cf2008-04-06 22:59:24 +00001626 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001627 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001628 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001629 false, 0, 0,
1630 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001631
Chris Lattnerfd4de792007-01-27 01:15:32 +00001632 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001633 FunctionProtoType *NewIP =
1634 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001635 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001636 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001637
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001638 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001639 // for two variable size arrays (for parameter and exception types) at the
1640 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001641 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001642 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1643 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001644 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001645 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001646 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001647 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001648 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001649 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001650 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001651}
Chris Lattneref51c202006-11-10 07:17:23 +00001652
John McCalle78aac42010-03-10 03:28:59 +00001653#ifndef NDEBUG
1654static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1655 if (!isa<CXXRecordDecl>(D)) return false;
1656 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1657 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1658 return true;
1659 if (RD->getDescribedClassTemplate() &&
1660 !isa<ClassTemplateSpecializationDecl>(RD))
1661 return true;
1662 return false;
1663}
1664#endif
1665
1666/// getInjectedClassNameType - Return the unique reference to the
1667/// injected class name type for the specified templated declaration.
1668QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1669 QualType TST) {
1670 assert(NeedsInjectedClassNameType(Decl));
1671 if (Decl->TypeForDecl) {
1672 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1673 } else if (CXXRecordDecl *PrevDecl
1674 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1675 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1676 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1677 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1678 } else {
John McCall2408e322010-04-27 00:57:59 +00001679 Decl->TypeForDecl =
1680 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001681 Types.push_back(Decl->TypeForDecl);
1682 }
1683 return QualType(Decl->TypeForDecl, 0);
1684}
1685
Douglas Gregor83a586e2008-04-13 21:07:44 +00001686/// getTypeDeclType - Return the unique reference to the type for the
1687/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001688QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001689 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001690 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001691
John McCall81e38502010-02-16 03:57:14 +00001692 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001693 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001694
1695 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001696 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001697 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001698
John McCall96f0b5f2010-03-10 06:48:02 +00001699 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1700 "Template type parameter types are always available.");
1701
John McCall81e38502010-02-16 03:57:14 +00001702 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001703 assert(!Record->getPreviousDeclaration() &&
1704 "struct/union has previous declaration");
1705 assert(!NeedsInjectedClassNameType(Record));
1706 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001707 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001708 assert(!Enum->getPreviousDeclaration() &&
1709 "enum has previous declaration");
1710 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001711 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001712 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1713 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001714 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001715 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001716
John McCall96f0b5f2010-03-10 06:48:02 +00001717 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001718 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001719}
1720
Chris Lattner32d920b2007-01-26 02:01:53 +00001721/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001722/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001723QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001724 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001725
Chris Lattner76a00cf2008-04-06 22:59:24 +00001726 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001727 Decl->TypeForDecl = new(*this, TypeAlignment)
1728 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001729 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001730 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001731}
1732
John McCallcebee162009-10-18 09:09:24 +00001733/// \brief Retrieve a substitution-result type.
1734QualType
1735ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1736 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001737 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001738 && "replacement types must always be canonical");
1739
1740 llvm::FoldingSetNodeID ID;
1741 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1742 void *InsertPos = 0;
1743 SubstTemplateTypeParmType *SubstParm
1744 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1745
1746 if (!SubstParm) {
1747 SubstParm = new (*this, TypeAlignment)
1748 SubstTemplateTypeParmType(Parm, Replacement);
1749 Types.push_back(SubstParm);
1750 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1751 }
1752
1753 return QualType(SubstParm, 0);
1754}
1755
Douglas Gregoreff93e02009-02-05 23:33:38 +00001756/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001757/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001758/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001759QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001760 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001761 IdentifierInfo *Name) {
1762 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001763 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001764 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001765 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001766 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1767
1768 if (TypeParm)
1769 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001770
Anders Carlsson90036dc2009-06-16 00:30:48 +00001771 if (Name) {
1772 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001773 TypeParm = new (*this, TypeAlignment)
1774 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001775
1776 TemplateTypeParmType *TypeCheck
1777 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1778 assert(!TypeCheck && "Template type parameter canonical type broken");
1779 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001780 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001781 TypeParm = new (*this, TypeAlignment)
1782 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001783
1784 Types.push_back(TypeParm);
1785 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1786
1787 return QualType(TypeParm, 0);
1788}
1789
John McCalle78aac42010-03-10 03:28:59 +00001790TypeSourceInfo *
1791ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1792 SourceLocation NameLoc,
1793 const TemplateArgumentListInfo &Args,
1794 QualType CanonType) {
1795 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1796
1797 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1798 TemplateSpecializationTypeLoc TL
1799 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1800 TL.setTemplateNameLoc(NameLoc);
1801 TL.setLAngleLoc(Args.getLAngleLoc());
1802 TL.setRAngleLoc(Args.getRAngleLoc());
1803 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1804 TL.setArgLocInfo(i, Args[i].getLocInfo());
1805 return DI;
1806}
1807
Mike Stump11289f42009-09-09 15:08:12 +00001808QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001809ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001810 const TemplateArgumentListInfo &Args,
John McCall2408e322010-04-27 00:57:59 +00001811 QualType Canon,
1812 bool IsCurrentInstantiation) {
John McCall6b51f282009-11-23 01:53:49 +00001813 unsigned NumArgs = Args.size();
1814
John McCall0ad16662009-10-29 08:12:44 +00001815 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1816 ArgVec.reserve(NumArgs);
1817 for (unsigned i = 0; i != NumArgs; ++i)
1818 ArgVec.push_back(Args[i].getArgument());
1819
John McCall2408e322010-04-27 00:57:59 +00001820 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
1821 Canon, IsCurrentInstantiation);
John McCall0ad16662009-10-29 08:12:44 +00001822}
1823
1824QualType
1825ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001826 const TemplateArgument *Args,
1827 unsigned NumArgs,
John McCall2408e322010-04-27 00:57:59 +00001828 QualType Canon,
1829 bool IsCurrentInstantiation) {
Douglas Gregor15301382009-07-30 17:40:51 +00001830 if (!Canon.isNull())
1831 Canon = getCanonicalType(Canon);
1832 else {
John McCall2408e322010-04-27 00:57:59 +00001833 assert(!IsCurrentInstantiation &&
1834 "current-instantiation specializations should always "
1835 "have a canonical type");
1836
Douglas Gregor15301382009-07-30 17:40:51 +00001837 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001838 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1839 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1840 CanonArgs.reserve(NumArgs);
1841 for (unsigned I = 0; I != NumArgs; ++I)
1842 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1843
1844 // Determine whether this canonical template specialization type already
1845 // exists.
1846 llvm::FoldingSetNodeID ID;
John McCall2408e322010-04-27 00:57:59 +00001847 TemplateSpecializationType::Profile(ID, CanonTemplate, false,
Douglas Gregor00044172009-07-29 16:09:57 +00001848 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001849
1850 void *InsertPos = 0;
1851 TemplateSpecializationType *Spec
1852 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001853
Douglas Gregora8e02e72009-07-28 23:00:59 +00001854 if (!Spec) {
1855 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001856 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001857 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001858 TypeAlignment);
John McCall2408e322010-04-27 00:57:59 +00001859 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001860 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001861 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001862 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001863 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001864 }
Mike Stump11289f42009-09-09 15:08:12 +00001865
Douglas Gregor15301382009-07-30 17:40:51 +00001866 if (Canon.isNull())
1867 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001868 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001869 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001870 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001871
Douglas Gregora8e02e72009-07-28 23:00:59 +00001872 // Allocate the (non-canonical) template specialization type, but don't
1873 // try to unique it: these types typically have location information that
1874 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001875 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001876 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001877 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001878 TemplateSpecializationType *Spec
John McCall2408e322010-04-27 00:57:59 +00001879 = new (Mem) TemplateSpecializationType(*this, Template,
1880 IsCurrentInstantiation,
1881 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001882 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001883
Douglas Gregor8bf42052009-02-09 18:46:07 +00001884 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001885 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001886}
1887
Mike Stump11289f42009-09-09 15:08:12 +00001888QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00001889ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
1890 NestedNameSpecifier *NNS,
1891 QualType NamedType) {
Douglas Gregor52537682009-03-19 00:18:19 +00001892 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001893 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001894
1895 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001896 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00001897 if (T)
1898 return QualType(T, 0);
1899
Douglas Gregorc42075a2010-02-04 18:10:26 +00001900 QualType Canon = NamedType;
1901 if (!Canon.isCanonical()) {
1902 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00001903 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1904 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00001905 (void)CheckT;
1906 }
1907
Abramo Bagnara6150c882010-05-11 21:36:43 +00001908 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001909 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00001910 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00001911 return QualType(T, 0);
1912}
1913
Douglas Gregor02085352010-03-31 20:19:30 +00001914QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1915 NestedNameSpecifier *NNS,
1916 const IdentifierInfo *Name,
1917 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001918 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1919
1920 if (Canon.isNull()) {
1921 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001922 ElaboratedTypeKeyword CanonKeyword = Keyword;
1923 if (Keyword == ETK_None)
1924 CanonKeyword = ETK_Typename;
1925
1926 if (CanonNNS != NNS || CanonKeyword != Keyword)
1927 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001928 }
1929
1930 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001931 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001932
1933 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001934 DependentNameType *T
1935 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001936 if (T)
1937 return QualType(T, 0);
1938
Douglas Gregor02085352010-03-31 20:19:30 +00001939 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00001940 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001941 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001942 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001943}
1944
Mike Stump11289f42009-09-09 15:08:12 +00001945QualType
Douglas Gregor02085352010-03-31 20:19:30 +00001946ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1947 NestedNameSpecifier *NNS,
1948 const TemplateSpecializationType *TemplateId,
1949 QualType Canon) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001950 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1951
Douglas Gregorc42075a2010-02-04 18:10:26 +00001952 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001953 DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001954
1955 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001956 DependentNameType *T
1957 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001958 if (T)
1959 return QualType(T, 0);
1960
Douglas Gregordce2b622009-04-01 00:28:59 +00001961 if (Canon.isNull()) {
1962 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1963 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
Douglas Gregor02085352010-03-31 20:19:30 +00001964 ElaboratedTypeKeyword CanonKeyword = Keyword;
1965 if (Keyword == ETK_None)
1966 CanonKeyword = ETK_Typename;
1967 if (CanonNNS != NNS || CanonKeyword != Keyword ||
1968 CanonType != QualType(TemplateId, 0)) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001969 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00001970 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00001971 assert(CanonTemplateId &&
1972 "Canonical type must also be a template specialization type");
Douglas Gregor02085352010-03-31 20:19:30 +00001973 Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
Douglas Gregordce2b622009-04-01 00:28:59 +00001974 }
Douglas Gregorc42075a2010-02-04 18:10:26 +00001975
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001976 DependentNameType *CheckT
1977 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001978 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregordce2b622009-04-01 00:28:59 +00001979 }
1980
Douglas Gregor02085352010-03-31 20:19:30 +00001981 T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00001982 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001983 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001984 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00001985}
1986
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001987/// CmpProtocolNames - Comparison predicate for sorting protocols
1988/// alphabetically.
1989static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1990 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00001991 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001992}
1993
John McCallfc93cf92009-10-22 22:37:11 +00001994static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
1995 unsigned NumProtocols) {
1996 if (NumProtocols == 0) return true;
1997
1998 for (unsigned i = 1; i != NumProtocols; ++i)
1999 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2000 return false;
2001 return true;
2002}
2003
2004static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002005 unsigned &NumProtocols) {
2006 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002007
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002008 // Sort protocols, keyed by name.
2009 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2010
2011 // Remove duplicates.
2012 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2013 NumProtocols = ProtocolsEnd-Protocols;
2014}
2015
Steve Narofffb4330f2009-06-17 22:40:22 +00002016/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2017/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002018QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00002019 ObjCProtocolDecl **Protocols,
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002020 unsigned NumProtocols,
2021 unsigned Quals) {
Steve Narofffb4330f2009-06-17 22:40:22 +00002022 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002023 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002024 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Narofffb4330f2009-06-17 22:40:22 +00002025
2026 void *InsertPos = 0;
2027 if (ObjCObjectPointerType *QT =
2028 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002029 return getQualifiedType(QualType(QT, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002030
John McCallfc93cf92009-10-22 22:37:11 +00002031 // Sort the protocol list alphabetically to canonicalize it.
2032 QualType Canonical;
2033 if (!InterfaceT.isCanonical() ||
2034 !areSortedAndUniqued(Protocols, NumProtocols)) {
2035 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002036 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2037 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002038 unsigned UniqueCount = NumProtocols;
2039
John McCallfc93cf92009-10-22 22:37:11 +00002040 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2041
2042 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2043 &Sorted[0], UniqueCount);
2044 } else {
2045 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2046 Protocols, NumProtocols);
2047 }
2048
2049 // Regenerate InsertPos.
2050 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2051 }
2052
Douglas Gregorf85bee62010-02-08 22:59:26 +00002053 // No match.
2054 unsigned Size = sizeof(ObjCObjectPointerType)
2055 + NumProtocols * sizeof(ObjCProtocolDecl *);
2056 void *Mem = Allocate(Size, TypeAlignment);
2057 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2058 InterfaceT,
2059 Protocols,
2060 NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002061
Steve Narofffb4330f2009-06-17 22:40:22 +00002062 Types.push_back(QType);
2063 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002064 return getQualifiedType(QualType(QType, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002065}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002066
Steve Naroffc277ad12009-07-18 15:33:26 +00002067/// getObjCInterfaceType - Return the unique reference to the type for the
2068/// specified ObjC interface decl. The list of protocols is optional.
2069QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002070 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002071 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00002072 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002073
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002074 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00002075 if (ObjCInterfaceType *QT =
2076 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002077 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002078
John McCallfc93cf92009-10-22 22:37:11 +00002079 // Sort the protocol list alphabetically to canonicalize it.
2080 QualType Canonical;
2081 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002082 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2083 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002084
2085 unsigned UniqueCount = NumProtocols;
2086 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2087
2088 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2089
2090 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2091 }
2092
Douglas Gregorf85bee62010-02-08 22:59:26 +00002093 unsigned Size = sizeof(ObjCInterfaceType)
2094 + NumProtocols * sizeof(ObjCProtocolDecl *);
2095 void *Mem = Allocate(Size, TypeAlignment);
2096 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2097 const_cast<ObjCInterfaceDecl*>(Decl),
2098 Protocols,
2099 NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002100
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002101 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00002102 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002103 return QualType(QType, 0);
2104}
2105
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002106/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2107/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002108/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002109/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002110/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002111QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002112 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002113 if (tofExpr->isTypeDependent()) {
2114 llvm::FoldingSetNodeID ID;
2115 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002116
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002117 void *InsertPos = 0;
2118 DependentTypeOfExprType *Canon
2119 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2120 if (Canon) {
2121 // We already have a "canonical" version of an identical, dependent
2122 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002123 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002124 QualType((TypeOfExprType*)Canon, 0));
2125 }
2126 else {
2127 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002128 Canon
2129 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002130 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2131 toe = Canon;
2132 }
2133 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002134 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002135 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002136 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002137 Types.push_back(toe);
2138 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002139}
2140
Steve Naroffa773cd52007-08-01 18:02:17 +00002141/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2142/// TypeOfType AST's. The only motivation to unique these nodes would be
2143/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002144/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002145/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002146QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002147 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002148 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002149 Types.push_back(tot);
2150 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002151}
2152
Anders Carlssonad6bd352009-06-24 21:24:56 +00002153/// getDecltypeForExpr - Given an expr, will return the decltype for that
2154/// expression, according to the rules in C++0x [dcl.type.simple]p4
2155static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002156 if (e->isTypeDependent())
2157 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002158
Anders Carlssonad6bd352009-06-24 21:24:56 +00002159 // If e is an id expression or a class member access, decltype(e) is defined
2160 // as the type of the entity named by e.
2161 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2162 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2163 return VD->getType();
2164 }
2165 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2166 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2167 return FD->getType();
2168 }
2169 // If e is a function call or an invocation of an overloaded operator,
2170 // (parentheses around e are ignored), decltype(e) is defined as the
2171 // return type of that function.
2172 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2173 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002174
Anders Carlssonad6bd352009-06-24 21:24:56 +00002175 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002176
2177 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002178 // defined as T&, otherwise decltype(e) is defined as T.
2179 if (e->isLvalue(Context) == Expr::LV_Valid)
2180 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002181
Anders Carlssonad6bd352009-06-24 21:24:56 +00002182 return T;
2183}
2184
Anders Carlsson81df7b82009-06-24 19:06:50 +00002185/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2186/// DecltypeType AST's. The only motivation to unique these nodes would be
2187/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002188/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002189/// on canonical type's (which are always unique).
2190QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002191 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002192 if (e->isTypeDependent()) {
2193 llvm::FoldingSetNodeID ID;
2194 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002195
Douglas Gregora21f6c32009-07-30 23:36:40 +00002196 void *InsertPos = 0;
2197 DependentDecltypeType *Canon
2198 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2199 if (Canon) {
2200 // We already have a "canonical" version of an equivalent, dependent
2201 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002202 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002203 QualType((DecltypeType*)Canon, 0));
2204 }
2205 else {
2206 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002207 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002208 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2209 dt = Canon;
2210 }
2211 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002212 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002213 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002214 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002215 Types.push_back(dt);
2216 return QualType(dt, 0);
2217}
2218
Chris Lattnerfb072462007-01-23 05:45:31 +00002219/// getTagDeclType - Return the unique reference to the type for the
2220/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002221QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002222 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002223 // FIXME: What is the design on getTagDeclType when it requires casting
2224 // away const? mutable?
2225 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002226}
2227
Mike Stump11289f42009-09-09 15:08:12 +00002228/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2229/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2230/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002231CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002232 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002233}
Chris Lattnerfb072462007-01-23 05:45:31 +00002234
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002235/// getSignedWCharType - Return the type of "signed wchar_t".
2236/// Used when in C++, as a GCC extension.
2237QualType ASTContext::getSignedWCharType() const {
2238 // FIXME: derive from "Target" ?
2239 return WCharTy;
2240}
2241
2242/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2243/// Used when in C++, as a GCC extension.
2244QualType ASTContext::getUnsignedWCharType() const {
2245 // FIXME: derive from "Target" ?
2246 return UnsignedIntTy;
2247}
2248
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002249/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2250/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2251QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002252 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002253}
2254
Chris Lattnera21ad802008-04-02 05:18:44 +00002255//===----------------------------------------------------------------------===//
2256// Type Operators
2257//===----------------------------------------------------------------------===//
2258
John McCallfc93cf92009-10-22 22:37:11 +00002259CanQualType ASTContext::getCanonicalParamType(QualType T) {
2260 // Push qualifiers into arrays, and then discard any remaining
2261 // qualifiers.
2262 T = getCanonicalType(T);
2263 const Type *Ty = T.getTypePtr();
2264
2265 QualType Result;
2266 if (isa<ArrayType>(Ty)) {
2267 Result = getArrayDecayedType(QualType(Ty,0));
2268 } else if (isa<FunctionType>(Ty)) {
2269 Result = getPointerType(QualType(Ty, 0));
2270 } else {
2271 Result = QualType(Ty, 0);
2272 }
2273
2274 return CanQualType::CreateUnsafe(Result);
2275}
2276
Chris Lattnered0d0792008-04-06 22:41:35 +00002277/// getCanonicalType - Return the canonical (structural) type corresponding to
2278/// the specified potentially non-canonical type. The non-canonical version
2279/// of a type may have many "decorated" versions of types. Decorators can
2280/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2281/// to be free of any of these, allowing two canonical types to be compared
2282/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002283CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002284 QualifierCollector Quals;
2285 const Type *Ptr = Quals.strip(T);
2286 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002287
John McCall8ccfcb52009-09-24 19:53:00 +00002288 // The canonical internal type will be the canonical type *except*
2289 // that we push type qualifiers down through array types.
2290
2291 // If there are no new qualifiers to push down, stop here.
2292 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002293 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002294
John McCall8ccfcb52009-09-24 19:53:00 +00002295 // If the type qualifiers are on an array type, get the canonical
2296 // type of the array with the qualifiers applied to the element
2297 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002298 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2299 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002300 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002301
Chris Lattner7adf0762008-08-04 07:31:14 +00002302 // Get the canonical version of the element with the extra qualifiers on it.
2303 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002304 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002305 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002306
Chris Lattner7adf0762008-08-04 07:31:14 +00002307 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002308 return CanQualType::CreateUnsafe(
2309 getConstantArrayType(NewEltTy, CAT->getSize(),
2310 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002311 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002312 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002313 return CanQualType::CreateUnsafe(
2314 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002315 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002316
Douglas Gregor4619e432008-12-05 23:32:09 +00002317 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002318 return CanQualType::CreateUnsafe(
2319 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002320 DSAT->getSizeExpr() ?
2321 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002322 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002323 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002324 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002325
Chris Lattner7adf0762008-08-04 07:31:14 +00002326 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002327 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002328 VAT->getSizeExpr() ?
2329 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002330 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002331 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002332 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002333}
2334
Chandler Carruth607f38e2009-12-29 07:16:59 +00002335QualType ASTContext::getUnqualifiedArrayType(QualType T,
2336 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002337 Quals = T.getQualifiers();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002338 if (!isa<ArrayType>(T)) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002339 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002340 }
2341
Chandler Carruth607f38e2009-12-29 07:16:59 +00002342 const ArrayType *AT = cast<ArrayType>(T);
2343 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002344 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002345 if (Elt == UnqualElt)
2346 return T;
2347
2348 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2349 return getConstantArrayType(UnqualElt, CAT->getSize(),
2350 CAT->getSizeModifier(), 0);
2351 }
2352
2353 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2354 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2355 }
2356
2357 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2358 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2359 DSAT->getSizeModifier(), 0,
2360 SourceRange());
2361}
2362
John McCall847e2a12009-11-24 18:42:40 +00002363DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2364 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2365 return TD->getDeclName();
2366
2367 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2368 if (DTN->isIdentifier()) {
2369 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2370 } else {
2371 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2372 }
2373 }
2374
John McCalld28ae272009-12-02 08:04:21 +00002375 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2376 assert(Storage);
2377 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002378}
2379
Douglas Gregor6bc50582009-05-07 06:41:52 +00002380TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2381 // If this template name refers to a template, the canonical
2382 // template name merely stores the template itself.
2383 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002384 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002385
John McCalld28ae272009-12-02 08:04:21 +00002386 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002387
Douglas Gregor6bc50582009-05-07 06:41:52 +00002388 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2389 assert(DTN && "Non-dependent template names must refer to template decls.");
2390 return DTN->CanonicalTemplateName;
2391}
2392
Douglas Gregoradee3e32009-11-11 23:06:43 +00002393bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2394 X = getCanonicalTemplateName(X);
2395 Y = getCanonicalTemplateName(Y);
2396 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2397}
2398
Mike Stump11289f42009-09-09 15:08:12 +00002399TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002400ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2401 switch (Arg.getKind()) {
2402 case TemplateArgument::Null:
2403 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002404
Douglas Gregora8e02e72009-07-28 23:00:59 +00002405 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002406 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002407
Douglas Gregora8e02e72009-07-28 23:00:59 +00002408 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002409 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002410
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002411 case TemplateArgument::Template:
2412 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2413
Douglas Gregora8e02e72009-07-28 23:00:59 +00002414 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002415 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002416 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002417
Douglas Gregora8e02e72009-07-28 23:00:59 +00002418 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002419 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002420
Douglas Gregora8e02e72009-07-28 23:00:59 +00002421 case TemplateArgument::Pack: {
2422 // FIXME: Allocate in ASTContext
2423 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2424 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002425 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002426 AEnd = Arg.pack_end();
2427 A != AEnd; (void)++A, ++Idx)
2428 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002429
Douglas Gregora8e02e72009-07-28 23:00:59 +00002430 TemplateArgument Result;
2431 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2432 return Result;
2433 }
2434 }
2435
2436 // Silence GCC warning
2437 assert(false && "Unhandled template argument kind");
2438 return TemplateArgument();
2439}
2440
Douglas Gregor333489b2009-03-27 23:10:48 +00002441NestedNameSpecifier *
2442ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002443 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002444 return 0;
2445
2446 switch (NNS->getKind()) {
2447 case NestedNameSpecifier::Identifier:
2448 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002449 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002450 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2451 NNS->getAsIdentifier());
2452
2453 case NestedNameSpecifier::Namespace:
2454 // A namespace is canonical; build a nested-name-specifier with
2455 // this namespace and no prefix.
2456 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2457
2458 case NestedNameSpecifier::TypeSpec:
2459 case NestedNameSpecifier::TypeSpecWithTemplate: {
2460 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002461 return NestedNameSpecifier::Create(*this, 0,
2462 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002463 T.getTypePtr());
2464 }
2465
2466 case NestedNameSpecifier::Global:
2467 // The global specifier is canonical and unique.
2468 return NNS;
2469 }
2470
2471 // Required to silence a GCC warning
2472 return 0;
2473}
2474
Chris Lattner7adf0762008-08-04 07:31:14 +00002475
2476const ArrayType *ASTContext::getAsArrayType(QualType T) {
2477 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002478 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002479 // Handle the common positive case fast.
2480 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2481 return AT;
2482 }
Mike Stump11289f42009-09-09 15:08:12 +00002483
John McCall8ccfcb52009-09-24 19:53:00 +00002484 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002485 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002486 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002487 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002488
John McCall8ccfcb52009-09-24 19:53:00 +00002489 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002490 // implements C99 6.7.3p8: "If the specification of an array type includes
2491 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002492
Chris Lattner7adf0762008-08-04 07:31:14 +00002493 // If we get here, we either have type qualifiers on the type, or we have
2494 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002495 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002496
John McCall8ccfcb52009-09-24 19:53:00 +00002497 QualifierCollector Qs;
2498 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002499
Chris Lattner7adf0762008-08-04 07:31:14 +00002500 // If we have a simple case, just return now.
2501 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002502 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002503 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002504
Chris Lattner7adf0762008-08-04 07:31:14 +00002505 // Otherwise, we have an array and we have qualifiers on it. Push the
2506 // qualifiers into the array element type and return a new array type.
2507 // Get the canonical version of the element with the extra qualifiers on it.
2508 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002509 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002510
Chris Lattner7adf0762008-08-04 07:31:14 +00002511 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2512 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2513 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002514 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002515 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2516 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2517 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002518 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002519
Mike Stump11289f42009-09-09 15:08:12 +00002520 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002521 = dyn_cast<DependentSizedArrayType>(ATy))
2522 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002523 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002524 DSAT->getSizeExpr() ?
2525 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002526 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002527 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002528 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002529
Chris Lattner7adf0762008-08-04 07:31:14 +00002530 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002531 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002532 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002533 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002534 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002535 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002536 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002537}
2538
2539
Chris Lattnera21ad802008-04-02 05:18:44 +00002540/// getArrayDecayedType - Return the properly qualified result of decaying the
2541/// specified array type to a pointer. This operation is non-trivial when
2542/// handling typedefs etc. The canonical type of "T" must be an array type,
2543/// this returns a pointer to a properly qualified element of the array.
2544///
2545/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2546QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002547 // Get the element type with 'getAsArrayType' so that we don't lose any
2548 // typedefs in the element type of the array. This also handles propagation
2549 // of type qualifiers from the array type into the element type if present
2550 // (C99 6.7.3p8).
2551 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2552 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002553
Chris Lattner7adf0762008-08-04 07:31:14 +00002554 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002555
2556 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002557 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002558}
2559
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002560QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002561 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002562 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2563 QT = AT->getElementType();
2564 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002565}
2566
Anders Carlsson4bf82142009-09-25 01:23:32 +00002567QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2568 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002569
Anders Carlsson4bf82142009-09-25 01:23:32 +00002570 if (const ArrayType *AT = getAsArrayType(ElemTy))
2571 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002572
Anders Carlssone0808df2008-12-21 03:44:36 +00002573 return ElemTy;
2574}
2575
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002576/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002577uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002578ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2579 uint64_t ElementCount = 1;
2580 do {
2581 ElementCount *= CA->getSize().getZExtValue();
2582 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2583 } while (CA);
2584 return ElementCount;
2585}
2586
Steve Naroff0af91202007-04-27 21:51:21 +00002587/// getFloatingRank - Return a relative rank for floating point types.
2588/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002589static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002590 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002591 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002592
John McCall9dd450b2009-09-21 23:43:11 +00002593 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2594 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002595 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002596 case BuiltinType::Float: return FloatRank;
2597 case BuiltinType::Double: return DoubleRank;
2598 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002599 }
2600}
2601
Mike Stump11289f42009-09-09 15:08:12 +00002602/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2603/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002604/// 'typeDomain' is a real floating point or complex type.
2605/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002606QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2607 QualType Domain) const {
2608 FloatingRank EltRank = getFloatingRank(Size);
2609 if (Domain->isComplexType()) {
2610 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002611 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002612 case FloatRank: return FloatComplexTy;
2613 case DoubleRank: return DoubleComplexTy;
2614 case LongDoubleRank: return LongDoubleComplexTy;
2615 }
Steve Naroff0af91202007-04-27 21:51:21 +00002616 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002617
2618 assert(Domain->isRealFloatingType() && "Unknown domain!");
2619 switch (EltRank) {
2620 default: assert(0 && "getFloatingRank(): illegal value for rank");
2621 case FloatRank: return FloatTy;
2622 case DoubleRank: return DoubleTy;
2623 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002624 }
Steve Naroffe4718892007-04-27 18:30:00 +00002625}
2626
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002627/// getFloatingTypeOrder - Compare the rank of the two specified floating
2628/// point types, ignoring the domain of the type (i.e. 'double' ==
2629/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002630/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002631int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2632 FloatingRank LHSR = getFloatingRank(LHS);
2633 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002634
Chris Lattnerb90739d2008-04-06 23:38:49 +00002635 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002636 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002637 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002638 return 1;
2639 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002640}
2641
Chris Lattner76a00cf2008-04-06 22:59:24 +00002642/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2643/// routine will assert if passed a built-in type that isn't an integer or enum,
2644/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002645unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002646 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002647 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002648 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002649
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002650 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2651 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2652
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002653 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2654 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2655
2656 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2657 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2658
Chris Lattner76a00cf2008-04-06 22:59:24 +00002659 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002660 default: assert(0 && "getIntegerRank(): not a built-in integer");
2661 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002662 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002663 case BuiltinType::Char_S:
2664 case BuiltinType::Char_U:
2665 case BuiltinType::SChar:
2666 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002667 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002668 case BuiltinType::Short:
2669 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002670 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002671 case BuiltinType::Int:
2672 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002673 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002674 case BuiltinType::Long:
2675 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002676 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002677 case BuiltinType::LongLong:
2678 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002679 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002680 case BuiltinType::Int128:
2681 case BuiltinType::UInt128:
2682 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002683 }
2684}
2685
Eli Friedman629ffb92009-08-20 04:21:42 +00002686/// \brief Whether this is a promotable bitfield reference according
2687/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2688///
2689/// \returns the type this bit-field will promote to, or NULL if no
2690/// promotion occurs.
2691QualType ASTContext::isPromotableBitField(Expr *E) {
2692 FieldDecl *Field = E->getBitField();
2693 if (!Field)
2694 return QualType();
2695
2696 QualType FT = Field->getType();
2697
2698 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2699 uint64_t BitWidth = BitWidthAP.getZExtValue();
2700 uint64_t IntSize = getTypeSize(IntTy);
2701 // GCC extension compatibility: if the bit-field size is less than or equal
2702 // to the size of int, it gets promoted no matter what its type is.
2703 // For instance, unsigned long bf : 4 gets promoted to signed int.
2704 if (BitWidth < IntSize)
2705 return IntTy;
2706
2707 if (BitWidth == IntSize)
2708 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2709
2710 // Types bigger than int are not subject to promotions, and therefore act
2711 // like the base type.
2712 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2713 // is ridiculous.
2714 return QualType();
2715}
2716
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002717/// getPromotedIntegerType - Returns the type that Promotable will
2718/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2719/// integer type.
2720QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2721 assert(!Promotable.isNull());
2722 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002723 if (const EnumType *ET = Promotable->getAs<EnumType>())
2724 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002725 if (Promotable->isSignedIntegerType())
2726 return IntTy;
2727 uint64_t PromotableSize = getTypeSize(Promotable);
2728 uint64_t IntSize = getTypeSize(IntTy);
2729 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2730 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2731}
2732
Mike Stump11289f42009-09-09 15:08:12 +00002733/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002734/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002735/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002736int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002737 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2738 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002739 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002740
Chris Lattner76a00cf2008-04-06 22:59:24 +00002741 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2742 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002743
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002744 unsigned LHSRank = getIntegerRank(LHSC);
2745 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002746
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002747 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2748 if (LHSRank == RHSRank) return 0;
2749 return LHSRank > RHSRank ? 1 : -1;
2750 }
Mike Stump11289f42009-09-09 15:08:12 +00002751
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002752 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2753 if (LHSUnsigned) {
2754 // If the unsigned [LHS] type is larger, return it.
2755 if (LHSRank >= RHSRank)
2756 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002757
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002758 // If the signed type can represent all values of the unsigned type, it
2759 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002760 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002761 return -1;
2762 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002763
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002764 // If the unsigned [RHS] type is larger, return it.
2765 if (RHSRank >= LHSRank)
2766 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002767
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002768 // If the signed type can represent all values of the unsigned type, it
2769 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002770 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002771 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002772}
Anders Carlsson98f07902007-08-17 05:31:46 +00002773
Anders Carlsson6d417272009-11-14 21:45:58 +00002774static RecordDecl *
2775CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2776 SourceLocation L, IdentifierInfo *Id) {
2777 if (Ctx.getLangOptions().CPlusPlus)
2778 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2779 else
2780 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2781}
2782
Mike Stump11289f42009-09-09 15:08:12 +00002783// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002784QualType ASTContext::getCFConstantStringType() {
2785 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002786 CFConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002787 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002788 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002789 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002790
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002791 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002792
Anders Carlsson98f07902007-08-17 05:31:46 +00002793 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002794 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002795 // int flags;
2796 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002797 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002798 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002799 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002800 FieldTypes[3] = LongTy;
2801
Anders Carlsson98f07902007-08-17 05:31:46 +00002802 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002803 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002804 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002805 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002806 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002807 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002808 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002809 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002810 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002811 }
2812
Douglas Gregord5058122010-02-11 01:19:42 +00002813 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002814 }
Mike Stump11289f42009-09-09 15:08:12 +00002815
Anders Carlsson98f07902007-08-17 05:31:46 +00002816 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002817}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002818
Douglas Gregor512b0772009-04-23 22:29:11 +00002819void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002820 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002821 assert(Rec && "Invalid CFConstantStringType");
2822 CFConstantStringTypeDecl = Rec->getDecl();
2823}
2824
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002825// getNSConstantStringType - Return the type used for constant NSStrings.
2826QualType ASTContext::getNSConstantStringType() {
2827 if (!NSConstantStringTypeDecl) {
2828 NSConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002829 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002830 &Idents.get("__builtin_NSString"));
2831 NSConstantStringTypeDecl->startDefinition();
2832
2833 QualType FieldTypes[3];
2834
2835 // const int *isa;
2836 FieldTypes[0] = getPointerType(IntTy.withConst());
2837 // const char *str;
2838 FieldTypes[1] = getPointerType(CharTy.withConst());
2839 // unsigned int length;
2840 FieldTypes[2] = UnsignedIntTy;
2841
2842 // Create fields
2843 for (unsigned i = 0; i < 3; ++i) {
2844 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2845 SourceLocation(), 0,
2846 FieldTypes[i], /*TInfo=*/0,
2847 /*BitWidth=*/0,
2848 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002849 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002850 NSConstantStringTypeDecl->addDecl(Field);
2851 }
2852
2853 NSConstantStringTypeDecl->completeDefinition();
2854 }
2855
2856 return getTagDeclType(NSConstantStringTypeDecl);
2857}
2858
2859void ASTContext::setNSConstantStringType(QualType T) {
2860 const RecordType *Rec = T->getAs<RecordType>();
2861 assert(Rec && "Invalid NSConstantStringType");
2862 NSConstantStringTypeDecl = Rec->getDecl();
2863}
2864
Mike Stump11289f42009-09-09 15:08:12 +00002865QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002866 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002867 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002868 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002869 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00002870 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002871
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002872 QualType FieldTypes[] = {
2873 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002874 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002875 getPointerType(UnsignedLongTy),
2876 getConstantArrayType(UnsignedLongTy,
2877 llvm::APInt(32, 5), ArrayType::Normal, 0)
2878 };
Mike Stump11289f42009-09-09 15:08:12 +00002879
Douglas Gregor91f84212008-12-11 16:49:14 +00002880 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002881 FieldDecl *Field = FieldDecl::Create(*this,
2882 ObjCFastEnumerationStateTypeDecl,
2883 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002884 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002885 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002886 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002887 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002888 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002889 }
Mike Stump11289f42009-09-09 15:08:12 +00002890
Douglas Gregord5058122010-02-11 01:19:42 +00002891 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002892 }
Mike Stump11289f42009-09-09 15:08:12 +00002893
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002894 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2895}
2896
Mike Stumpd0153282009-10-20 02:12:22 +00002897QualType ASTContext::getBlockDescriptorType() {
2898 if (BlockDescriptorType)
2899 return getTagDeclType(BlockDescriptorType);
2900
2901 RecordDecl *T;
2902 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00002903 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002904 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00002905 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002906
2907 QualType FieldTypes[] = {
2908 UnsignedLongTy,
2909 UnsignedLongTy,
2910 };
2911
2912 const char *FieldNames[] = {
2913 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002914 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002915 };
2916
2917 for (size_t i = 0; i < 2; ++i) {
2918 FieldDecl *Field = FieldDecl::Create(*this,
2919 T,
2920 SourceLocation(),
2921 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00002922 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00002923 /*BitWidth=*/0,
2924 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002925 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00002926 T->addDecl(Field);
2927 }
2928
Douglas Gregord5058122010-02-11 01:19:42 +00002929 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002930
2931 BlockDescriptorType = T;
2932
2933 return getTagDeclType(BlockDescriptorType);
2934}
2935
2936void ASTContext::setBlockDescriptorType(QualType T) {
2937 const RecordType *Rec = T->getAs<RecordType>();
2938 assert(Rec && "Invalid BlockDescriptorType");
2939 BlockDescriptorType = Rec->getDecl();
2940}
2941
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002942QualType ASTContext::getBlockDescriptorExtendedType() {
2943 if (BlockDescriptorExtendedType)
2944 return getTagDeclType(BlockDescriptorExtendedType);
2945
2946 RecordDecl *T;
2947 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00002948 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002949 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00002950 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002951
2952 QualType FieldTypes[] = {
2953 UnsignedLongTy,
2954 UnsignedLongTy,
2955 getPointerType(VoidPtrTy),
2956 getPointerType(VoidPtrTy)
2957 };
2958
2959 const char *FieldNames[] = {
2960 "reserved",
2961 "Size",
2962 "CopyFuncPtr",
2963 "DestroyFuncPtr"
2964 };
2965
2966 for (size_t i = 0; i < 4; ++i) {
2967 FieldDecl *Field = FieldDecl::Create(*this,
2968 T,
2969 SourceLocation(),
2970 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00002971 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002972 /*BitWidth=*/0,
2973 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002974 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002975 T->addDecl(Field);
2976 }
2977
Douglas Gregord5058122010-02-11 01:19:42 +00002978 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002979
2980 BlockDescriptorExtendedType = T;
2981
2982 return getTagDeclType(BlockDescriptorExtendedType);
2983}
2984
2985void ASTContext::setBlockDescriptorExtendedType(QualType T) {
2986 const RecordType *Rec = T->getAs<RecordType>();
2987 assert(Rec && "Invalid BlockDescriptorType");
2988 BlockDescriptorExtendedType = Rec->getDecl();
2989}
2990
Mike Stump94967902009-10-21 18:16:27 +00002991bool ASTContext::BlockRequiresCopying(QualType Ty) {
2992 if (Ty->isBlockPointerType())
2993 return true;
2994 if (isObjCNSObjectType(Ty))
2995 return true;
2996 if (Ty->isObjCObjectPointerType())
2997 return true;
2998 return false;
2999}
3000
3001QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3002 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003003 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003004 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003005 // unsigned int __flags;
3006 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003007 // void *__copy_helper; // as needed
3008 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003009 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003010 // } *
3011
Mike Stump94967902009-10-21 18:16:27 +00003012 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3013
3014 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003015 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003016 llvm::SmallString<36> Name;
3017 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3018 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003019 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003020 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003021 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003022 T->startDefinition();
3023 QualType Int32Ty = IntTy;
3024 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3025 QualType FieldTypes[] = {
3026 getPointerType(VoidPtrTy),
3027 getPointerType(getTagDeclType(T)),
3028 Int32Ty,
3029 Int32Ty,
3030 getPointerType(VoidPtrTy),
3031 getPointerType(VoidPtrTy),
3032 Ty
3033 };
3034
3035 const char *FieldNames[] = {
3036 "__isa",
3037 "__forwarding",
3038 "__flags",
3039 "__size",
3040 "__copy_helper",
3041 "__destroy_helper",
3042 DeclName,
3043 };
3044
3045 for (size_t i = 0; i < 7; ++i) {
3046 if (!HasCopyAndDispose && i >=4 && i <= 5)
3047 continue;
3048 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3049 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003050 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003051 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003052 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003053 T->addDecl(Field);
3054 }
3055
Douglas Gregord5058122010-02-11 01:19:42 +00003056 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003057
3058 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003059}
3060
3061
3062QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003063 bool BlockHasCopyDispose,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003064 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpd0153282009-10-20 02:12:22 +00003065 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003066 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003067 llvm::SmallString<36> Name;
3068 llvm::raw_svector_ostream(Name) << "__block_literal_"
3069 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003070 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003071 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003072 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003073 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003074 QualType FieldTypes[] = {
3075 getPointerType(VoidPtrTy),
3076 IntTy,
3077 IntTy,
3078 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003079 (BlockHasCopyDispose ?
3080 getPointerType(getBlockDescriptorExtendedType()) :
3081 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003082 };
3083
3084 const char *FieldNames[] = {
3085 "__isa",
3086 "__flags",
3087 "__reserved",
3088 "__FuncPtr",
3089 "__descriptor"
3090 };
3091
3092 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003093 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003094 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003095 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003096 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003097 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003098 T->addDecl(Field);
3099 }
3100
3101 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3102 const Expr *E = BlockDeclRefDecls[i];
3103 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3104 clang::IdentifierInfo *Name = 0;
3105 if (BDRE) {
3106 const ValueDecl *D = BDRE->getDecl();
3107 Name = &Idents.get(D->getName());
3108 }
3109 QualType FieldType = E->getType();
3110
3111 if (BDRE && BDRE->isByRef())
Mike Stump94967902009-10-21 18:16:27 +00003112 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3113 FieldType);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003114
3115 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallbcd03502009-12-07 02:54:59 +00003116 Name, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003117 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003118 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003119 T->addDecl(Field);
3120 }
3121
Douglas Gregord5058122010-02-11 01:19:42 +00003122 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003123
3124 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003125}
3126
Douglas Gregor512b0772009-04-23 22:29:11 +00003127void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003128 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003129 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3130 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3131}
3132
Anders Carlsson18acd442007-10-29 06:33:42 +00003133// This returns true if a type has been typedefed to BOOL:
3134// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003135static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003136 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003137 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3138 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003139
Anders Carlssond8499822007-10-29 05:01:08 +00003140 return false;
3141}
3142
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003143/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003144/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003145CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003146 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003147
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003148 // Make all integer and enum types at least as large as an int
Ken Dyck40775002010-01-11 17:06:35 +00003149 if (sz.isPositive() && type->isIntegralType())
3150 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003151 // Treat arrays as pointers, since that's how they're passed in.
3152 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003153 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003154 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003155}
3156
3157static inline
3158std::string charUnitsToString(const CharUnits &CU) {
3159 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003160}
3161
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003162/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003163/// declaration.
3164void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3165 std::string& S) {
3166 const BlockDecl *Decl = Expr->getBlockDecl();
3167 QualType BlockTy =
3168 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3169 // Encode result type.
3170 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3171 // Compute size of all parameters.
3172 // Start with computing size of a pointer in number of bytes.
3173 // FIXME: There might(should) be a better way of doing this computation!
3174 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003175 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3176 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003177 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003178 E = Decl->param_end(); PI != E; ++PI) {
3179 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003180 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003181 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003182 ParmOffset += sz;
3183 }
3184 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003185 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003186 // Block pointer and offset.
3187 S += "@?0";
3188 ParmOffset = PtrSize;
3189
3190 // Argument types.
3191 ParmOffset = PtrSize;
3192 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3193 Decl->param_end(); PI != E; ++PI) {
3194 ParmVarDecl *PVDecl = *PI;
3195 QualType PType = PVDecl->getOriginalType();
3196 if (const ArrayType *AT =
3197 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3198 // Use array's original type only if it has known number of
3199 // elements.
3200 if (!isa<ConstantArrayType>(AT))
3201 PType = PVDecl->getType();
3202 } else if (PType->isFunctionType())
3203 PType = PVDecl->getType();
3204 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003205 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003206 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003207 }
3208}
3209
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003210/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003211/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003212void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003213 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003214 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003215 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003216 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003217 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003218 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003219 // Compute size of all parameters.
3220 // Start with computing size of a pointer in number of bytes.
3221 // FIXME: There might(should) be a better way of doing this computation!
3222 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003223 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003224 // The first two arguments (self and _cmd) are pointers; account for
3225 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003226 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003227 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003228 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003229 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003230 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003231 assert (sz.isPositive() &&
3232 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003233 ParmOffset += sz;
3234 }
Ken Dyck40775002010-01-11 17:06:35 +00003235 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003236 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003237 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003238
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003239 // Argument types.
3240 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003241 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003242 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003243 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003244 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003245 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003246 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3247 // Use array's original type only if it has known number of
3248 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003249 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003250 PType = PVDecl->getType();
3251 } else if (PType->isFunctionType())
3252 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003253 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003254 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003255 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003256 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003257 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003258 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003259 }
3260}
3261
Daniel Dunbar4932b362008-08-28 04:38:10 +00003262/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003263/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003264/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3265/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003266/// Property attributes are stored as a comma-delimited C string. The simple
3267/// attributes readonly and bycopy are encoded as single characters. The
3268/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3269/// encoded as single characters, followed by an identifier. Property types
3270/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003271/// these attributes are defined by the following enumeration:
3272/// @code
3273/// enum PropertyAttributes {
3274/// kPropertyReadOnly = 'R', // property is read-only.
3275/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3276/// kPropertyByref = '&', // property is a reference to the value last assigned
3277/// kPropertyDynamic = 'D', // property is dynamic
3278/// kPropertyGetter = 'G', // followed by getter selector name
3279/// kPropertySetter = 'S', // followed by setter selector name
3280/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3281/// kPropertyType = 't' // followed by old-style type encoding.
3282/// kPropertyWeak = 'W' // 'weak' property
3283/// kPropertyStrong = 'P' // property GC'able
3284/// kPropertyNonAtomic = 'N' // property non-atomic
3285/// };
3286/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003287void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003288 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003289 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003290 // Collect information from the property implementation decl(s).
3291 bool Dynamic = false;
3292 ObjCPropertyImplDecl *SynthesizePID = 0;
3293
3294 // FIXME: Duplicated code due to poor abstraction.
3295 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003296 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003297 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3298 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003299 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003300 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003301 ObjCPropertyImplDecl *PID = *i;
3302 if (PID->getPropertyDecl() == PD) {
3303 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3304 Dynamic = true;
3305 } else {
3306 SynthesizePID = PID;
3307 }
3308 }
3309 }
3310 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003311 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003312 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003313 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003314 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003315 ObjCPropertyImplDecl *PID = *i;
3316 if (PID->getPropertyDecl() == PD) {
3317 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3318 Dynamic = true;
3319 } else {
3320 SynthesizePID = PID;
3321 }
3322 }
Mike Stump11289f42009-09-09 15:08:12 +00003323 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003324 }
3325 }
3326
3327 // FIXME: This is not very efficient.
3328 S = "T";
3329
3330 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003331 // GCC has some special rules regarding encoding of properties which
3332 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003333 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003334 true /* outermost type */,
3335 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003336
3337 if (PD->isReadOnly()) {
3338 S += ",R";
3339 } else {
3340 switch (PD->getSetterKind()) {
3341 case ObjCPropertyDecl::Assign: break;
3342 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003343 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003344 }
3345 }
3346
3347 // It really isn't clear at all what this means, since properties
3348 // are "dynamic by default".
3349 if (Dynamic)
3350 S += ",D";
3351
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003352 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3353 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003354
Daniel Dunbar4932b362008-08-28 04:38:10 +00003355 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3356 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003357 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003358 }
3359
3360 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3361 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003362 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003363 }
3364
3365 if (SynthesizePID) {
3366 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3367 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003368 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003369 }
3370
3371 // FIXME: OBJCGC: weak & strong
3372}
3373
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003374/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003375/// Another legacy compatibility encoding: 32-bit longs are encoded as
3376/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003377/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3378///
3379void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003380 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003381 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003382 if (BT->getKind() == BuiltinType::ULong &&
3383 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003384 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003385 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003386 if (BT->getKind() == BuiltinType::Long &&
3387 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003388 PointeeTy = IntTy;
3389 }
3390 }
3391}
3392
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003393void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003394 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003395 // We follow the behavior of gcc, expanding structures which are
3396 // directly pointed to, and expanding embedded structures. Note that
3397 // these rules are sufficient to prevent recursive encoding of the
3398 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003399 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003400 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003401}
3402
Mike Stump11289f42009-09-09 15:08:12 +00003403static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003404 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003405 const Expr *E = FD->getBitWidth();
3406 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3407 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00003408 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003409 S += 'b';
3410 S += llvm::utostr(N);
3411}
3412
Daniel Dunbar07d07852009-10-18 21:17:35 +00003413// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003414void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3415 bool ExpandPointedToStructures,
3416 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003417 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003418 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003419 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00003420 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003421 if (FD && FD->isBitField())
3422 return EncodeBitField(this, S, FD);
3423 char encoding;
3424 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003425 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00003426 case BuiltinType::Void: encoding = 'v'; break;
3427 case BuiltinType::Bool: encoding = 'B'; break;
3428 case BuiltinType::Char_U:
3429 case BuiltinType::UChar: encoding = 'C'; break;
3430 case BuiltinType::UShort: encoding = 'S'; break;
3431 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003432 case BuiltinType::ULong:
3433 encoding =
3434 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00003435 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003436 case BuiltinType::UInt128: encoding = 'T'; break;
3437 case BuiltinType::ULongLong: encoding = 'Q'; break;
3438 case BuiltinType::Char_S:
3439 case BuiltinType::SChar: encoding = 'c'; break;
3440 case BuiltinType::Short: encoding = 's'; break;
3441 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003442 case BuiltinType::Long:
3443 encoding =
3444 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003445 break;
3446 case BuiltinType::LongLong: encoding = 'q'; break;
3447 case BuiltinType::Int128: encoding = 't'; break;
3448 case BuiltinType::Float: encoding = 'f'; break;
3449 case BuiltinType::Double: encoding = 'd'; break;
3450 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003451 }
Mike Stump11289f42009-09-09 15:08:12 +00003452
Chris Lattnere7cabb92009-07-13 00:10:46 +00003453 S += encoding;
3454 return;
3455 }
Mike Stump11289f42009-09-09 15:08:12 +00003456
John McCall9dd450b2009-09-21 23:43:11 +00003457 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003458 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003459 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003460 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003461 return;
3462 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003463
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003464 // encoding for pointer or r3eference types.
3465 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003466 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003467 if (PT->isObjCSelType()) {
3468 S += ':';
3469 return;
3470 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003471 PointeeTy = PT->getPointeeType();
3472 }
3473 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3474 PointeeTy = RT->getPointeeType();
3475 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003476 bool isReadOnly = false;
3477 // For historical/compatibility reasons, the read-only qualifier of the
3478 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3479 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003480 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003481 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003482 if (OutermostType && T.isConstQualified()) {
3483 isReadOnly = true;
3484 S += 'r';
3485 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003486 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003487 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003488 while (P->getAs<PointerType>())
3489 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003490 if (P.isConstQualified()) {
3491 isReadOnly = true;
3492 S += 'r';
3493 }
3494 }
3495 if (isReadOnly) {
3496 // Another legacy compatibility encoding. Some ObjC qualifier and type
3497 // combinations need to be rearranged.
3498 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003499 if (llvm::StringRef(S).endswith("nr"))
3500 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003501 }
Mike Stump11289f42009-09-09 15:08:12 +00003502
Anders Carlssond8499822007-10-29 05:01:08 +00003503 if (PointeeTy->isCharType()) {
3504 // char pointer types should be encoded as '*' unless it is a
3505 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003506 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003507 S += '*';
3508 return;
3509 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003510 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003511 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3512 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3513 S += '#';
3514 return;
3515 }
3516 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3517 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3518 S += '@';
3519 return;
3520 }
3521 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003522 }
Anders Carlssond8499822007-10-29 05:01:08 +00003523 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003524 getLegacyIntegralTypeEncoding(PointeeTy);
3525
Mike Stump11289f42009-09-09 15:08:12 +00003526 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003527 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003528 return;
3529 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003530
Chris Lattnere7cabb92009-07-13 00:10:46 +00003531 if (const ArrayType *AT =
3532 // Ignore type qualifiers etc.
3533 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003534 if (isa<IncompleteArrayType>(AT)) {
3535 // Incomplete arrays are encoded as a pointer to the array element.
3536 S += '^';
3537
Mike Stump11289f42009-09-09 15:08:12 +00003538 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003539 false, ExpandStructures, FD);
3540 } else {
3541 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003542
Anders Carlssond05f44b2009-02-22 01:38:57 +00003543 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3544 S += llvm::utostr(CAT->getSize().getZExtValue());
3545 else {
3546 //Variable length arrays are encoded as a regular array with 0 elements.
3547 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3548 S += '0';
3549 }
Mike Stump11289f42009-09-09 15:08:12 +00003550
3551 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003552 false, ExpandStructures, FD);
3553 S += ']';
3554 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003555 return;
3556 }
Mike Stump11289f42009-09-09 15:08:12 +00003557
John McCall9dd450b2009-09-21 23:43:11 +00003558 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003559 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003560 return;
3561 }
Mike Stump11289f42009-09-09 15:08:12 +00003562
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003563 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003564 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003565 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003566 // Anonymous structures print as '?'
3567 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3568 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003569 if (ClassTemplateSpecializationDecl *Spec
3570 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3571 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3572 std::string TemplateArgsStr
3573 = TemplateSpecializationType::PrintTemplateArgumentList(
3574 TemplateArgs.getFlatArgumentList(),
3575 TemplateArgs.flat_size(),
3576 (*this).PrintingPolicy);
3577
3578 S += TemplateArgsStr;
3579 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003580 } else {
3581 S += '?';
3582 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003583 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003584 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003585 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3586 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003587 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003588 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003589 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003590 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003591 S += '"';
3592 }
Mike Stump11289f42009-09-09 15:08:12 +00003593
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003594 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003595 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003596 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003597 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003598 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003599 QualType qt = Field->getType();
3600 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003601 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003602 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003603 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003604 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003605 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003606 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003607 return;
3608 }
Mike Stump11289f42009-09-09 15:08:12 +00003609
Chris Lattnere7cabb92009-07-13 00:10:46 +00003610 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003611 if (FD && FD->isBitField())
3612 EncodeBitField(this, S, FD);
3613 else
3614 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003615 return;
3616 }
Mike Stump11289f42009-09-09 15:08:12 +00003617
Chris Lattnere7cabb92009-07-13 00:10:46 +00003618 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003619 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003620 return;
3621 }
Mike Stump11289f42009-09-09 15:08:12 +00003622
John McCall8ccfcb52009-09-24 19:53:00 +00003623 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003624 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003625 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003626 S += '{';
3627 const IdentifierInfo *II = OI->getIdentifier();
3628 S += II->getName();
3629 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003630 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003631 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003632 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003633 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003634 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003635 RecFields[i]);
3636 else
Mike Stump11289f42009-09-09 15:08:12 +00003637 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003638 FD);
3639 }
3640 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003641 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003642 }
Mike Stump11289f42009-09-09 15:08:12 +00003643
John McCall9dd450b2009-09-21 23:43:11 +00003644 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003645 if (OPT->isObjCIdType()) {
3646 S += '@';
3647 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003648 }
Mike Stump11289f42009-09-09 15:08:12 +00003649
Steve Narofff0c86112009-10-28 22:03:49 +00003650 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3651 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3652 // Since this is a binary compatibility issue, need to consult with runtime
3653 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003654 S += '#';
3655 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003656 }
Mike Stump11289f42009-09-09 15:08:12 +00003657
Chris Lattnere7cabb92009-07-13 00:10:46 +00003658 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003659 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003660 ExpandPointedToStructures,
3661 ExpandStructures, FD);
3662 if (FD || EncodingProperty) {
3663 // Note that we do extended encoding of protocol qualifer list
3664 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003665 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003666 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3667 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003668 S += '<';
3669 S += (*I)->getNameAsString();
3670 S += '>';
3671 }
3672 S += '"';
3673 }
3674 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003675 }
Mike Stump11289f42009-09-09 15:08:12 +00003676
Chris Lattnere7cabb92009-07-13 00:10:46 +00003677 QualType PointeeTy = OPT->getPointeeType();
3678 if (!EncodingProperty &&
3679 isa<TypedefType>(PointeeTy.getTypePtr())) {
3680 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003681 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003682 // {...};
3683 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003684 getObjCEncodingForTypeImpl(PointeeTy, S,
3685 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003686 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003687 return;
3688 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003689
3690 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003691 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003692 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003693 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003694 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3695 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003696 S += '<';
3697 S += (*I)->getNameAsString();
3698 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003699 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003700 S += '"';
3701 }
3702 return;
3703 }
Mike Stump11289f42009-09-09 15:08:12 +00003704
Chris Lattnere7cabb92009-07-13 00:10:46 +00003705 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003706}
3707
Mike Stump11289f42009-09-09 15:08:12 +00003708void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003709 std::string& S) const {
3710 if (QT & Decl::OBJC_TQ_In)
3711 S += 'n';
3712 if (QT & Decl::OBJC_TQ_Inout)
3713 S += 'N';
3714 if (QT & Decl::OBJC_TQ_Out)
3715 S += 'o';
3716 if (QT & Decl::OBJC_TQ_Bycopy)
3717 S += 'O';
3718 if (QT & Decl::OBJC_TQ_Byref)
3719 S += 'R';
3720 if (QT & Decl::OBJC_TQ_Oneway)
3721 S += 'V';
3722}
3723
Chris Lattnere7cabb92009-07-13 00:10:46 +00003724void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003725 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003726
Anders Carlsson87c149b2007-10-11 01:00:40 +00003727 BuiltinVaListType = T;
3728}
3729
Chris Lattnere7cabb92009-07-13 00:10:46 +00003730void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003731 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003732}
3733
Chris Lattnere7cabb92009-07-13 00:10:46 +00003734void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003735 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003736}
3737
Chris Lattnere7cabb92009-07-13 00:10:46 +00003738void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003739 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003740}
3741
Chris Lattnere7cabb92009-07-13 00:10:46 +00003742void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003743 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003744}
3745
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003746void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003747 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003748 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003749
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003750 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003751}
3752
John McCalld28ae272009-12-02 08:04:21 +00003753/// \brief Retrieve the template name that corresponds to a non-empty
3754/// lookup.
John McCallad371252010-01-20 00:46:10 +00003755TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3756 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003757 unsigned size = End - Begin;
3758 assert(size > 1 && "set is not overloaded!");
3759
3760 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3761 size * sizeof(FunctionTemplateDecl*));
3762 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3763
3764 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003765 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003766 NamedDecl *D = *I;
3767 assert(isa<FunctionTemplateDecl>(D) ||
3768 (isa<UsingShadowDecl>(D) &&
3769 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3770 *Storage++ = D;
3771 }
3772
3773 return TemplateName(OT);
3774}
3775
Douglas Gregordc572a32009-03-30 22:58:21 +00003776/// \brief Retrieve the template name that represents a qualified
3777/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003778TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003779 bool TemplateKeyword,
3780 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003781 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003782 llvm::FoldingSetNodeID ID;
3783 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3784
3785 void *InsertPos = 0;
3786 QualifiedTemplateName *QTN =
3787 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3788 if (!QTN) {
3789 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3790 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3791 }
3792
3793 return TemplateName(QTN);
3794}
3795
3796/// \brief Retrieve the template name that represents a dependent
3797/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003798TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003799 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003800 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003801 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003802
3803 llvm::FoldingSetNodeID ID;
3804 DependentTemplateName::Profile(ID, NNS, Name);
3805
3806 void *InsertPos = 0;
3807 DependentTemplateName *QTN =
3808 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3809
3810 if (QTN)
3811 return TemplateName(QTN);
3812
3813 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3814 if (CanonNNS == NNS) {
3815 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3816 } else {
3817 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3818 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003819 DependentTemplateName *CheckQTN =
3820 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3821 assert(!CheckQTN && "Dependent type name canonicalization broken");
3822 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00003823 }
3824
3825 DependentTemplateNames.InsertNode(QTN, InsertPos);
3826 return TemplateName(QTN);
3827}
3828
Douglas Gregor71395fa2009-11-04 00:56:37 +00003829/// \brief Retrieve the template name that represents a dependent
3830/// template name such as \c MetaFun::template operator+.
3831TemplateName
3832ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3833 OverloadedOperatorKind Operator) {
3834 assert((!NNS || NNS->isDependent()) &&
3835 "Nested name specifier must be dependent");
3836
3837 llvm::FoldingSetNodeID ID;
3838 DependentTemplateName::Profile(ID, NNS, Operator);
3839
3840 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00003841 DependentTemplateName *QTN
3842 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00003843
3844 if (QTN)
3845 return TemplateName(QTN);
3846
3847 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3848 if (CanonNNS == NNS) {
3849 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3850 } else {
3851 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3852 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003853
3854 DependentTemplateName *CheckQTN
3855 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3856 assert(!CheckQTN && "Dependent template name canonicalization broken");
3857 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00003858 }
3859
3860 DependentTemplateNames.InsertNode(QTN, InsertPos);
3861 return TemplateName(QTN);
3862}
3863
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003864/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003865/// TargetInfo, produce the corresponding type. The unsigned @p Type
3866/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003867CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003868 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003869 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003870 case TargetInfo::SignedShort: return ShortTy;
3871 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3872 case TargetInfo::SignedInt: return IntTy;
3873 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3874 case TargetInfo::SignedLong: return LongTy;
3875 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3876 case TargetInfo::SignedLongLong: return LongLongTy;
3877 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3878 }
3879
3880 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003881 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003882}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003883
3884//===----------------------------------------------------------------------===//
3885// Type Predicates.
3886//===----------------------------------------------------------------------===//
3887
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003888/// isObjCNSObjectType - Return true if this is an NSObject object using
3889/// NSObject attribute on a c-style pointer type.
3890/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003891/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003892///
3893bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3894 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3895 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003896 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003897 return true;
3898 }
Mike Stump11289f42009-09-09 15:08:12 +00003899 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003900}
3901
Fariborz Jahanian96207692009-02-18 21:49:28 +00003902/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3903/// garbage collection attribute.
3904///
John McCall8ccfcb52009-09-24 19:53:00 +00003905Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3906 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003907 if (getLangOptions().ObjC1 &&
3908 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003909 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003910 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003911 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003912 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003913 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003914 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003915 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003916 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003917 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003918 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003919 // Non-pointers have none gc'able attribute regardless of the attribute
3920 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003921 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003922 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003923 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003924 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003925}
3926
Chris Lattner49af6a42008-04-07 06:51:04 +00003927//===----------------------------------------------------------------------===//
3928// Type Compatibility Testing
3929//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00003930
Mike Stump11289f42009-09-09 15:08:12 +00003931/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003932/// compatible.
3933static bool areCompatVectorTypes(const VectorType *LHS,
3934 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00003935 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00003936 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00003937 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00003938}
3939
Steve Naroff8e6aee52009-07-23 01:01:38 +00003940//===----------------------------------------------------------------------===//
3941// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3942//===----------------------------------------------------------------------===//
3943
3944/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3945/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003946bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3947 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003948 if (lProto == rProto)
3949 return true;
3950 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3951 E = rProto->protocol_end(); PI != E; ++PI)
3952 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3953 return true;
3954 return false;
3955}
3956
Steve Naroff8e6aee52009-07-23 01:01:38 +00003957/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3958/// return true if lhs's protocols conform to rhs's protocol; false
3959/// otherwise.
3960bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3961 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3962 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3963 return false;
3964}
3965
3966/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3967/// ObjCQualifiedIDType.
3968bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3969 bool compare) {
3970 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00003971 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003972 lhs->isObjCIdType() || lhs->isObjCClassType())
3973 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003974 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003975 rhs->isObjCIdType() || rhs->isObjCClassType())
3976 return true;
3977
3978 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00003979 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00003980
Steve Naroff8e6aee52009-07-23 01:01:38 +00003981 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00003982
Steve Naroff8e6aee52009-07-23 01:01:38 +00003983 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003984 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00003985 // make sure we check the class hierarchy.
3986 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3987 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3988 E = lhsQID->qual_end(); I != E; ++I) {
3989 // when comparing an id<P> on lhs with a static type on rhs,
3990 // see if static class implements all of id's protocols, directly or
3991 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003992 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00003993 return false;
3994 }
3995 }
3996 // If there are no qualifiers and no interface, we have an 'id'.
3997 return true;
3998 }
Mike Stump11289f42009-09-09 15:08:12 +00003999 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004000 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4001 E = lhsQID->qual_end(); I != E; ++I) {
4002 ObjCProtocolDecl *lhsProto = *I;
4003 bool match = false;
4004
4005 // when comparing an id<P> on lhs with a static type on rhs,
4006 // see if static class implements all of id's protocols, directly or
4007 // through its super class and categories.
4008 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4009 E = rhsOPT->qual_end(); J != E; ++J) {
4010 ObjCProtocolDecl *rhsProto = *J;
4011 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4012 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4013 match = true;
4014 break;
4015 }
4016 }
Mike Stump11289f42009-09-09 15:08:12 +00004017 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004018 // make sure we check the class hierarchy.
4019 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4020 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4021 E = lhsQID->qual_end(); I != E; ++I) {
4022 // when comparing an id<P> on lhs with a static type on rhs,
4023 // see if static class implements all of id's protocols, directly or
4024 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004025 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004026 match = true;
4027 break;
4028 }
4029 }
4030 }
4031 if (!match)
4032 return false;
4033 }
Mike Stump11289f42009-09-09 15:08:12 +00004034
Steve Naroff8e6aee52009-07-23 01:01:38 +00004035 return true;
4036 }
Mike Stump11289f42009-09-09 15:08:12 +00004037
Steve Naroff8e6aee52009-07-23 01:01:38 +00004038 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4039 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4040
Mike Stump11289f42009-09-09 15:08:12 +00004041 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004042 lhs->getAsObjCInterfacePointerType()) {
4043 if (lhsOPT->qual_empty()) {
4044 bool match = false;
4045 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4046 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4047 E = rhsQID->qual_end(); I != E; ++I) {
4048 // when comparing an id<P> on lhs with a static type on rhs,
4049 // see if static class implements all of id's protocols, directly or
4050 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004051 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004052 match = true;
4053 break;
4054 }
4055 }
4056 if (!match)
4057 return false;
4058 }
4059 return true;
4060 }
Mike Stump11289f42009-09-09 15:08:12 +00004061 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004062 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4063 E = lhsOPT->qual_end(); I != E; ++I) {
4064 ObjCProtocolDecl *lhsProto = *I;
4065 bool match = false;
4066
4067 // when comparing an id<P> on lhs with a static type on rhs,
4068 // see if static class implements all of id's protocols, directly or
4069 // through its super class and categories.
4070 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4071 E = rhsQID->qual_end(); J != E; ++J) {
4072 ObjCProtocolDecl *rhsProto = *J;
4073 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4074 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4075 match = true;
4076 break;
4077 }
4078 }
4079 if (!match)
4080 return false;
4081 }
4082 return true;
4083 }
4084 return false;
4085}
4086
Eli Friedman47f77112008-08-22 00:56:42 +00004087/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004088/// compatible for assignment from RHS to LHS. This handles validation of any
4089/// protocol qualifiers on the LHS or RHS.
4090///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004091bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4092 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004093 // If either type represents the built-in 'id' or 'Class' types, return true.
4094 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004095 return true;
4096
Steve Naroff8e6aee52009-07-23 01:01:38 +00004097 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00004098 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4099 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004100 false);
4101
Steve Naroff7cae42b2009-07-10 23:34:53 +00004102 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4103 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00004104 if (LHS && RHS) // We have 2 user-defined types.
4105 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004106
Steve Naroff8e6aee52009-07-23 01:01:38 +00004107 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004108}
4109
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004110/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4111/// for providing type-safty for objective-c pointers used to pass/return
4112/// arguments in block literals. When passed as arguments, passing 'A*' where
4113/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4114/// not OK. For the return type, the opposite is not OK.
4115bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4116 const ObjCObjectPointerType *LHSOPT,
4117 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004118 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004119 return true;
4120
4121 if (LHSOPT->isObjCBuiltinType()) {
4122 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4123 }
4124
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004125 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004126 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4127 QualType(RHSOPT,0),
4128 false);
4129
4130 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4131 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4132 if (LHS && RHS) { // We have 2 user-defined types.
4133 if (LHS != RHS) {
4134 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4135 return false;
4136 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4137 return true;
4138 }
4139 else
4140 return true;
4141 }
4142 return false;
4143}
4144
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004145/// getIntersectionOfProtocols - This routine finds the intersection of set
4146/// of protocols inherited from two distinct objective-c pointer objects.
4147/// It is used to build composite qualifier list of the composite type of
4148/// the conditional expression involving two objective-c pointer objects.
4149static
4150void getIntersectionOfProtocols(ASTContext &Context,
4151 const ObjCObjectPointerType *LHSOPT,
4152 const ObjCObjectPointerType *RHSOPT,
4153 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4154
4155 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4156 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4157
4158 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4159 unsigned LHSNumProtocols = LHS->getNumProtocols();
4160 if (LHSNumProtocols > 0)
4161 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4162 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004163 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4164 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004165 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4166 LHSInheritedProtocols.end());
4167 }
4168
4169 unsigned RHSNumProtocols = RHS->getNumProtocols();
4170 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004171 ObjCProtocolDecl **RHSProtocols =
4172 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004173 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4174 if (InheritedProtocolSet.count(RHSProtocols[i]))
4175 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4176 }
4177 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004178 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004179 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004180 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4181 RHSInheritedProtocols.begin(),
4182 E = RHSInheritedProtocols.end(); I != E; ++I)
4183 if (InheritedProtocolSet.count((*I)))
4184 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004185 }
4186}
4187
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004188/// areCommonBaseCompatible - Returns common base class of the two classes if
4189/// one found. Note that this is O'2 algorithm. But it will be called as the
4190/// last type comparison in a ?-exp of ObjC pointer types before a
4191/// warning is issued. So, its invokation is extremely rare.
4192QualType ASTContext::areCommonBaseCompatible(
4193 const ObjCObjectPointerType *LHSOPT,
4194 const ObjCObjectPointerType *RHSOPT) {
4195 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4196 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4197 if (!LHS || !RHS)
4198 return QualType();
4199
4200 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4201 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4202 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004203 if (canAssignObjCInterfaces(LHS, RHS)) {
4204 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4205 getIntersectionOfProtocols(*this,
4206 LHSOPT, RHSOPT, IntersectionOfProtocols);
4207 if (IntersectionOfProtocols.empty())
4208 LHSTy = getObjCObjectPointerType(LHSTy);
4209 else
4210 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4211 IntersectionOfProtocols.size());
4212 return LHSTy;
4213 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004214 }
4215
4216 return QualType();
4217}
4218
Eli Friedman47f77112008-08-22 00:56:42 +00004219bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4220 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00004221 // Verify that the base decls are compatible: the RHS must be a subclass of
4222 // the LHS.
4223 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4224 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004225
Chris Lattner49af6a42008-04-07 06:51:04 +00004226 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4227 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004228 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004229 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004230
Chris Lattner49af6a42008-04-07 06:51:04 +00004231 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4232 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004233 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004234 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004235
Steve Naroffc277ad12009-07-18 15:33:26 +00004236 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4237 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004238 LHSPI != LHSPE; LHSPI++) {
4239 bool RHSImplementsProtocol = false;
4240
4241 // If the RHS doesn't implement the protocol on the left, the types
4242 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00004243 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004244 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004245 RHSPI != RHSPE; RHSPI++) {
4246 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004247 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004248 break;
4249 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004250 }
4251 // FIXME: For better diagnostics, consider passing back the protocol name.
4252 if (!RHSImplementsProtocol)
4253 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004254 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004255 // The RHS implements all protocols listed on the LHS.
4256 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004257}
4258
Steve Naroffb7605152009-02-12 17:52:19 +00004259bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4260 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004261 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4262 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004263
Steve Naroff7cae42b2009-07-10 23:34:53 +00004264 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004265 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004266
4267 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4268 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004269}
4270
Mike Stump11289f42009-09-09 15:08:12 +00004271/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004272/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004273/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004274/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004275bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004276 if (getLangOptions().CPlusPlus)
4277 return hasSameType(LHS, RHS);
4278
Eli Friedman47f77112008-08-22 00:56:42 +00004279 return !mergeTypes(LHS, RHS).isNull();
4280}
4281
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004282bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4283 return !mergeTypes(LHS, RHS, true).isNull();
4284}
4285
4286QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4287 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004288 const FunctionType *lbase = lhs->getAs<FunctionType>();
4289 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004290 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4291 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004292 bool allLTypes = true;
4293 bool allRTypes = true;
4294
4295 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004296 QualType retType;
4297 if (OfBlockPointer)
4298 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4299 else
4300 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004301 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004302 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004303 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004304 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004305 allRTypes = false;
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004306 // FIXME: double check this
4307 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4308 // rbase->getRegParmAttr() != 0 &&
4309 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004310 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4311 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004312 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4313 lbaseInfo.getRegParm();
4314 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4315 if (NoReturn != lbaseInfo.getNoReturn() ||
4316 RegParm != lbaseInfo.getRegParm())
4317 allLTypes = false;
4318 if (NoReturn != rbaseInfo.getNoReturn() ||
4319 RegParm != rbaseInfo.getRegParm())
4320 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004321 CallingConv lcc = lbaseInfo.getCC();
4322 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004323 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004324 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004325 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004326
Eli Friedman47f77112008-08-22 00:56:42 +00004327 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004328 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4329 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004330 unsigned lproto_nargs = lproto->getNumArgs();
4331 unsigned rproto_nargs = rproto->getNumArgs();
4332
4333 // Compatible functions must have the same number of arguments
4334 if (lproto_nargs != rproto_nargs)
4335 return QualType();
4336
4337 // Variadic and non-variadic functions aren't compatible
4338 if (lproto->isVariadic() != rproto->isVariadic())
4339 return QualType();
4340
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004341 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4342 return QualType();
4343
Eli Friedman47f77112008-08-22 00:56:42 +00004344 // Check argument compatibility
4345 llvm::SmallVector<QualType, 10> types;
4346 for (unsigned i = 0; i < lproto_nargs; i++) {
4347 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4348 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004349 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004350 if (argtype.isNull()) return QualType();
4351 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004352 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4353 allLTypes = false;
4354 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4355 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004356 }
4357 if (allLTypes) return lhs;
4358 if (allRTypes) return rhs;
4359 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004360 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004361 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004362 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004363 }
4364
4365 if (lproto) allRTypes = false;
4366 if (rproto) allLTypes = false;
4367
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004368 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004369 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004370 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004371 if (proto->isVariadic()) return QualType();
4372 // Check that the types are compatible with the types that
4373 // would result from default argument promotions (C99 6.7.5.3p15).
4374 // The only types actually affected are promotable integer
4375 // types and floats, which would be passed as a different
4376 // type depending on whether the prototype is visible.
4377 unsigned proto_nargs = proto->getNumArgs();
4378 for (unsigned i = 0; i < proto_nargs; ++i) {
4379 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004380
4381 // Look at the promotion type of enum types, since that is the type used
4382 // to pass enum values.
4383 if (const EnumType *Enum = argTy->getAs<EnumType>())
4384 argTy = Enum->getDecl()->getPromotionType();
4385
Eli Friedman47f77112008-08-22 00:56:42 +00004386 if (argTy->isPromotableIntegerType() ||
4387 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4388 return QualType();
4389 }
4390
4391 if (allLTypes) return lhs;
4392 if (allRTypes) return rhs;
4393 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004394 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004395 proto->getTypeQuals(),
4396 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004397 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004398 }
4399
4400 if (allLTypes) return lhs;
4401 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004402 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004403 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004404}
4405
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004406QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4407 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004408 // C++ [expr]: If an expression initially has the type "reference to T", the
4409 // type is adjusted to "T" prior to any further analysis, the expression
4410 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004411 // expression is an lvalue unless the reference is an rvalue reference and
4412 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004413 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4414 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4415
Eli Friedman47f77112008-08-22 00:56:42 +00004416 QualType LHSCan = getCanonicalType(LHS),
4417 RHSCan = getCanonicalType(RHS);
4418
4419 // If two types are identical, they are compatible.
4420 if (LHSCan == RHSCan)
4421 return LHS;
4422
John McCall8ccfcb52009-09-24 19:53:00 +00004423 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004424 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4425 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004426 if (LQuals != RQuals) {
4427 // If any of these qualifiers are different, we have a type
4428 // mismatch.
4429 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4430 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4431 return QualType();
4432
4433 // Exactly one GC qualifier difference is allowed: __strong is
4434 // okay if the other type has no GC qualifier but is an Objective
4435 // C object pointer (i.e. implicitly strong by default). We fix
4436 // this by pretending that the unqualified type was actually
4437 // qualified __strong.
4438 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4439 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4440 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4441
4442 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4443 return QualType();
4444
4445 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4446 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4447 }
4448 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4449 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4450 }
Eli Friedman47f77112008-08-22 00:56:42 +00004451 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004452 }
4453
4454 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004455
Eli Friedmandcca6332009-06-01 01:22:52 +00004456 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4457 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004458
Chris Lattnerfd652912008-01-14 05:45:46 +00004459 // We want to consider the two function types to be the same for these
4460 // comparisons, just force one to the other.
4461 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4462 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004463
4464 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004465 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4466 LHSClass = Type::ConstantArray;
4467 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4468 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004469
Nate Begemance4d7fc2008-04-18 23:10:10 +00004470 // Canonicalize ExtVector -> Vector.
4471 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4472 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004473
Chris Lattner95554662008-04-07 05:43:21 +00004474 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004475 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004476 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004477 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004478 // Compatibility is based on the underlying type, not the promotion
4479 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004480 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004481 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4482 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004483 }
John McCall9dd450b2009-09-21 23:43:11 +00004484 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004485 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4486 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004487 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004488
Eli Friedman47f77112008-08-22 00:56:42 +00004489 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004490 }
Eli Friedman47f77112008-08-22 00:56:42 +00004491
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004492 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004493 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004494#define TYPE(Class, Base)
4495#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004496#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004497#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4498#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4499#include "clang/AST/TypeNodes.def"
4500 assert(false && "Non-canonical and dependent types shouldn't get here");
4501 return QualType();
4502
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004503 case Type::LValueReference:
4504 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004505 case Type::MemberPointer:
4506 assert(false && "C++ should never be in mergeTypes");
4507 return QualType();
4508
4509 case Type::IncompleteArray:
4510 case Type::VariableArray:
4511 case Type::FunctionProto:
4512 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004513 assert(false && "Types are eliminated above");
4514 return QualType();
4515
Chris Lattnerfd652912008-01-14 05:45:46 +00004516 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004517 {
4518 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004519 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4520 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004521 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4522 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004523 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004524 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004525 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004526 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004527 return getPointerType(ResultType);
4528 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004529 case Type::BlockPointer:
4530 {
4531 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004532 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4533 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004534 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004535 if (ResultType.isNull()) return QualType();
4536 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4537 return LHS;
4538 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4539 return RHS;
4540 return getBlockPointerType(ResultType);
4541 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004542 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004543 {
4544 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4545 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4546 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4547 return QualType();
4548
4549 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4550 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4551 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4552 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004553 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4554 return LHS;
4555 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4556 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004557 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4558 ArrayType::ArraySizeModifier(), 0);
4559 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4560 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004561 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4562 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004563 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4564 return LHS;
4565 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4566 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004567 if (LVAT) {
4568 // FIXME: This isn't correct! But tricky to implement because
4569 // the array's size has to be the size of LHS, but the type
4570 // has to be different.
4571 return LHS;
4572 }
4573 if (RVAT) {
4574 // FIXME: This isn't correct! But tricky to implement because
4575 // the array's size has to be the size of RHS, but the type
4576 // has to be different.
4577 return RHS;
4578 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004579 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4580 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004581 return getIncompleteArrayType(ResultType,
4582 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004583 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004584 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004585 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004586 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004587 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004588 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004589 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004590 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004591 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004592 case Type::Complex:
4593 // Distinct complex types are incompatible.
4594 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004595 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004596 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004597 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4598 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004599 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004600 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004601 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00004602 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004603 // FIXME: This should be type compatibility, e.g. whether
4604 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00004605 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4606 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00004607 if (LHSIface && RHSIface &&
4608 canAssignObjCInterfaces(LHSIface, RHSIface))
4609 return LHS;
4610
Eli Friedman47f77112008-08-22 00:56:42 +00004611 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004612 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004613 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004614 if (OfBlockPointer) {
4615 if (canAssignObjCInterfacesInBlockPointer(
4616 LHS->getAs<ObjCObjectPointerType>(),
4617 RHS->getAs<ObjCObjectPointerType>()))
4618 return LHS;
4619 return QualType();
4620 }
John McCall9dd450b2009-09-21 23:43:11 +00004621 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4622 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004623 return LHS;
4624
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004625 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004626 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004627 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004628
4629 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004630}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004631
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004632//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004633// Integer Predicates
4634//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004635
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004636unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004637 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004638 return 1;
John McCall56774992009-12-09 09:09:27 +00004639 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004640 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004641 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004642 return (unsigned)getTypeSize(T);
4643}
4644
4645QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4646 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004647
4648 // Turn <4 x signed int> -> <4 x unsigned int>
4649 if (const VectorType *VTy = T->getAs<VectorType>())
4650 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004651 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004652
4653 // For enums, we return the unsigned version of the base type.
4654 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004655 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004656
4657 const BuiltinType *BTy = T->getAs<BuiltinType>();
4658 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004659 switch (BTy->getKind()) {
4660 case BuiltinType::Char_S:
4661 case BuiltinType::SChar:
4662 return UnsignedCharTy;
4663 case BuiltinType::Short:
4664 return UnsignedShortTy;
4665 case BuiltinType::Int:
4666 return UnsignedIntTy;
4667 case BuiltinType::Long:
4668 return UnsignedLongTy;
4669 case BuiltinType::LongLong:
4670 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004671 case BuiltinType::Int128:
4672 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004673 default:
4674 assert(0 && "Unexpected signed integer type");
4675 return QualType();
4676 }
4677}
4678
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004679ExternalASTSource::~ExternalASTSource() { }
4680
4681void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004682
4683
4684//===----------------------------------------------------------------------===//
4685// Builtin Type Computation
4686//===----------------------------------------------------------------------===//
4687
4688/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4689/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004690static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004691 ASTContext::GetBuiltinTypeError &Error,
4692 bool AllowTypeModifiers = true) {
4693 // Modifiers.
4694 int HowLong = 0;
4695 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004696
Chris Lattnerecd79c62009-06-14 00:45:47 +00004697 // Read the modifiers first.
4698 bool Done = false;
4699 while (!Done) {
4700 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004701 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004702 case 'S':
4703 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4704 assert(!Signed && "Can't use 'S' modifier multiple times!");
4705 Signed = true;
4706 break;
4707 case 'U':
4708 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4709 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4710 Unsigned = true;
4711 break;
4712 case 'L':
4713 assert(HowLong <= 2 && "Can't have LLLL modifier");
4714 ++HowLong;
4715 break;
4716 }
4717 }
4718
4719 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004720
Chris Lattnerecd79c62009-06-14 00:45:47 +00004721 // Read the base type.
4722 switch (*Str++) {
4723 default: assert(0 && "Unknown builtin type letter!");
4724 case 'v':
4725 assert(HowLong == 0 && !Signed && !Unsigned &&
4726 "Bad modifiers used with 'v'!");
4727 Type = Context.VoidTy;
4728 break;
4729 case 'f':
4730 assert(HowLong == 0 && !Signed && !Unsigned &&
4731 "Bad modifiers used with 'f'!");
4732 Type = Context.FloatTy;
4733 break;
4734 case 'd':
4735 assert(HowLong < 2 && !Signed && !Unsigned &&
4736 "Bad modifiers used with 'd'!");
4737 if (HowLong)
4738 Type = Context.LongDoubleTy;
4739 else
4740 Type = Context.DoubleTy;
4741 break;
4742 case 's':
4743 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4744 if (Unsigned)
4745 Type = Context.UnsignedShortTy;
4746 else
4747 Type = Context.ShortTy;
4748 break;
4749 case 'i':
4750 if (HowLong == 3)
4751 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4752 else if (HowLong == 2)
4753 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4754 else if (HowLong == 1)
4755 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4756 else
4757 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4758 break;
4759 case 'c':
4760 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4761 if (Signed)
4762 Type = Context.SignedCharTy;
4763 else if (Unsigned)
4764 Type = Context.UnsignedCharTy;
4765 else
4766 Type = Context.CharTy;
4767 break;
4768 case 'b': // boolean
4769 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4770 Type = Context.BoolTy;
4771 break;
4772 case 'z': // size_t.
4773 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4774 Type = Context.getSizeType();
4775 break;
4776 case 'F':
4777 Type = Context.getCFConstantStringType();
4778 break;
4779 case 'a':
4780 Type = Context.getBuiltinVaListType();
4781 assert(!Type.isNull() && "builtin va list type not initialized!");
4782 break;
4783 case 'A':
4784 // This is a "reference" to a va_list; however, what exactly
4785 // this means depends on how va_list is defined. There are two
4786 // different kinds of va_list: ones passed by value, and ones
4787 // passed by reference. An example of a by-value va_list is
4788 // x86, where va_list is a char*. An example of by-ref va_list
4789 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4790 // we want this argument to be a char*&; for x86-64, we want
4791 // it to be a __va_list_tag*.
4792 Type = Context.getBuiltinVaListType();
4793 assert(!Type.isNull() && "builtin va list type not initialized!");
4794 if (Type->isArrayType()) {
4795 Type = Context.getArrayDecayedType(Type);
4796 } else {
4797 Type = Context.getLValueReferenceType(Type);
4798 }
4799 break;
4800 case 'V': {
4801 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004802 unsigned NumElements = strtoul(Str, &End, 10);
4803 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004804
Chris Lattnerecd79c62009-06-14 00:45:47 +00004805 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004806
Chris Lattnerecd79c62009-06-14 00:45:47 +00004807 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00004808 // FIXME: Don't know what to do about AltiVec.
4809 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004810 break;
4811 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004812 case 'X': {
4813 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4814 Type = Context.getComplexType(ElementType);
4815 break;
4816 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004817 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004818 Type = Context.getFILEType();
4819 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004820 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004821 return QualType();
4822 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004823 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004824 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004825 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004826 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004827 else
4828 Type = Context.getjmp_bufType();
4829
Mike Stump2adb4da2009-07-28 23:47:15 +00004830 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004831 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004832 return QualType();
4833 }
4834 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004835 }
Mike Stump11289f42009-09-09 15:08:12 +00004836
Chris Lattnerecd79c62009-06-14 00:45:47 +00004837 if (!AllowTypeModifiers)
4838 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004839
Chris Lattnerecd79c62009-06-14 00:45:47 +00004840 Done = false;
4841 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00004842 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00004843 default: Done = true; --Str; break;
4844 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00004845 case '&':
John McCallb8b94662010-03-12 04:21:28 +00004846 {
4847 // Both pointers and references can have their pointee types
4848 // qualified with an address space.
4849 char *End;
4850 unsigned AddrSpace = strtoul(Str, &End, 10);
4851 if (End != Str && AddrSpace != 0) {
4852 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4853 Str = End;
4854 }
4855 }
4856 if (c == '*')
4857 Type = Context.getPointerType(Type);
4858 else
4859 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004860 break;
4861 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4862 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004863 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004864 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00004865 case 'D':
4866 Type = Context.getVolatileType(Type);
4867 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004868 }
4869 }
Mike Stump11289f42009-09-09 15:08:12 +00004870
Chris Lattnerecd79c62009-06-14 00:45:47 +00004871 return Type;
4872}
4873
4874/// GetBuiltinType - Return the type for the specified builtin.
4875QualType ASTContext::GetBuiltinType(unsigned id,
4876 GetBuiltinTypeError &Error) {
4877 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004878
Chris Lattnerecd79c62009-06-14 00:45:47 +00004879 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004880
Chris Lattnerecd79c62009-06-14 00:45:47 +00004881 Error = GE_None;
4882 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4883 if (Error != GE_None)
4884 return QualType();
4885 while (TypeStr[0] && TypeStr[0] != '.') {
4886 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4887 if (Error != GE_None)
4888 return QualType();
4889
4890 // Do array -> pointer decay. The builtin should use the decayed type.
4891 if (Ty->isArrayType())
4892 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004893
Chris Lattnerecd79c62009-06-14 00:45:47 +00004894 ArgTypes.push_back(Ty);
4895 }
4896
4897 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4898 "'.' should only occur at end of builtin type list!");
4899
4900 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4901 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4902 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00004903
4904 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00004905 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00004906 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004907 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00004908}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004909
4910QualType
4911ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4912 // Perform the usual unary conversions. We do this early so that
4913 // integral promotions to "int" can allow us to exit early, in the
4914 // lhs == rhs check. Also, for conversion purposes, we ignore any
4915 // qualifiers. For example, "const float" and "float" are
4916 // equivalent.
4917 if (lhs->isPromotableIntegerType())
4918 lhs = getPromotedIntegerType(lhs);
4919 else
4920 lhs = lhs.getUnqualifiedType();
4921 if (rhs->isPromotableIntegerType())
4922 rhs = getPromotedIntegerType(rhs);
4923 else
4924 rhs = rhs.getUnqualifiedType();
4925
4926 // If both types are identical, no conversion is needed.
4927 if (lhs == rhs)
4928 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004929
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004930 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4931 // The caller can deal with this (e.g. pointer + int).
4932 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4933 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004934
4935 // At this point, we have two different arithmetic types.
4936
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004937 // Handle complex types first (C99 6.3.1.8p1).
4938 if (lhs->isComplexType() || rhs->isComplexType()) {
4939 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00004940 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004941 // convert the rhs to the lhs complex type.
4942 return lhs;
4943 }
Mike Stump11289f42009-09-09 15:08:12 +00004944 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004945 // convert the lhs to the rhs complex type.
4946 return rhs;
4947 }
4948 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00004949 // When both operands are complex, the shorter operand is converted to the
4950 // type of the longer, and that is the type of the result. This corresponds
4951 // to what is done when combining two real floating-point operands.
4952 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004953 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00004954 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004955 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00004956 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004957 // "double _Complex" is promoted to "long double _Complex".
4958 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004959
4960 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004961 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004962 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004963 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00004964 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004965 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4966 // domains match. This is a requirement for our implementation, C99
4967 // does not require this promotion.
4968 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4969 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4970 return rhs;
4971 } else { // handle "_Complex double, double".
4972 return lhs;
4973 }
4974 }
4975 return lhs; // The domain/size match exactly.
4976 }
4977 // Now handle "real" floating types (i.e. float, double, long double).
4978 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4979 // if we have an integer operand, the result is the real floating type.
4980 if (rhs->isIntegerType()) {
4981 // convert rhs to the lhs floating point type.
4982 return lhs;
4983 }
4984 if (rhs->isComplexIntegerType()) {
4985 // convert rhs to the complex floating point type.
4986 return getComplexType(lhs);
4987 }
4988 if (lhs->isIntegerType()) {
4989 // convert lhs to the rhs floating point type.
4990 return rhs;
4991 }
Mike Stump11289f42009-09-09 15:08:12 +00004992 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004993 // convert lhs to the complex floating point type.
4994 return getComplexType(rhs);
4995 }
4996 // We have two real floating types, float/complex combos were handled above.
4997 // Convert the smaller operand to the bigger result.
4998 int result = getFloatingTypeOrder(lhs, rhs);
4999 if (result > 0) // convert the rhs
5000 return lhs;
5001 assert(result < 0 && "illegal float comparison");
5002 return rhs; // convert the lhs
5003 }
5004 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5005 // Handle GCC complex int extension.
5006 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5007 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5008
5009 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005010 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005011 rhsComplexInt->getElementType()) >= 0)
5012 return lhs; // convert the rhs
5013 return rhs;
5014 } else if (lhsComplexInt && rhs->isIntegerType()) {
5015 // convert the rhs to the lhs complex type.
5016 return lhs;
5017 } else if (rhsComplexInt && lhs->isIntegerType()) {
5018 // convert the lhs to the rhs complex type.
5019 return rhs;
5020 }
5021 }
5022 // Finally, we have two differing integer types.
5023 // The rules for this case are in C99 6.3.1.8
5024 int compare = getIntegerTypeOrder(lhs, rhs);
5025 bool lhsSigned = lhs->isSignedIntegerType(),
5026 rhsSigned = rhs->isSignedIntegerType();
5027 QualType destType;
5028 if (lhsSigned == rhsSigned) {
5029 // Same signedness; use the higher-ranked type
5030 destType = compare >= 0 ? lhs : rhs;
5031 } else if (compare != (lhsSigned ? 1 : -1)) {
5032 // The unsigned type has greater than or equal rank to the
5033 // signed type, so use the unsigned type
5034 destType = lhsSigned ? rhs : lhs;
5035 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5036 // The two types are different widths; if we are here, that
5037 // means the signed type is larger than the unsigned type, so
5038 // use the signed type.
5039 destType = lhsSigned ? lhs : rhs;
5040 } else {
5041 // The signed type is higher-ranked than the unsigned type,
5042 // but isn't actually any bigger (like unsigned int and long
5043 // on most 32-bit systems). Use the unsigned type corresponding
5044 // to the signed type.
5045 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5046 }
5047 return destType;
5048}