blob: 2e7244957564426f98317fc0066caf21bbbfe8d5 [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"
John McCall87fe5d52010-05-20 01:18:31 +000021#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000025#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000026#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000027#include "llvm/ADT/SmallString.h"
Anders Carlssond8499822007-10-29 05:01:08 +000028#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000029#include "llvm/Support/MathExtras.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000030#include "llvm/Support/raw_ostream.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000031
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),
John McCall8cb7bdf2010-06-04 23:28:52 +000047 NullTypeSourceInfo(QualType()),
Douglas Gregor9507d462010-03-19 22:13:20 +000048 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000049 Idents(idents), Selectors(sels),
Ted Kremenek6aead3a2010-05-10 20:40:08 +000050 BuiltinInfo(builtins),
51 DeclarationNames(*this),
52 ExternalSource(0), PrintingPolicy(LOpts),
John McCallc62bb642010-03-24 05:22:00 +000053 LastSDM(0, 0) {
David Chisnall9f57c292009-08-17 16:35:33 +000054 ObjCIdRedefinitionType = QualType();
55 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian04b258c2009-11-25 23:07:42 +000056 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +000057 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +000058 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +000059 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +000060}
61
Chris Lattnerd5973eb2006-11-12 00:53:46 +000062ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +000063 // Release the DenseMaps associated with DeclContext objects.
64 // FIXME: Is this the ideal solution?
65 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +000066
Douglas Gregor1a809332010-05-23 18:26:36 +000067 if (!FreeMemory) {
68 // Call all of the deallocation functions.
69 for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
70 Deallocations[I].first(Deallocations[I].second);
71 }
72
Douglas Gregor832940b2010-03-02 23:58:15 +000073 // Release all of the memory associated with overridden C++ methods.
74 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
75 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
76 OM != OMEnd; ++OM)
77 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +000078
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000079 if (FreeMemory) {
80 // Deallocate all the types.
81 while (!Types.empty()) {
82 Types.back()->Destroy(*this);
83 Types.pop_back();
84 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +000085
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000086 for (llvm::FoldingSet<ExtQuals>::iterator
87 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
88 // Increment in loop to prevent using deallocated memory.
John McCall8ccfcb52009-09-24 19:53:00 +000089 Deallocate(&*I++);
Nuno Lopese013c7f2008-12-17 22:30:25 +000090 }
Nuno Lopese013c7f2008-12-17 22:30:25 +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
Ted Kremenek076baeb2010-06-08 23:00:58 +0000101 // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
102 // even when using the BumpPtrAllocator because they can contain
103 // DenseMaps.
104 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
105 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
106 // Increment in loop to prevent using deallocated memory.
107 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
108 R->Destroy(*this);
109 }
110
Douglas Gregorf21eb492009-03-26 23:50:42 +0000111 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000112 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
113 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000114 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000115 NNS != NNSEnd; ) {
116 // Increment in loop to prevent using deallocated memory.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000117 (*NNS++).Destroy(*this);
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000118 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000119
120 if (GlobalNestedNameSpecifier)
121 GlobalNestedNameSpecifier->Destroy(*this);
122
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000123 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000124}
125
Douglas Gregor1a809332010-05-23 18:26:36 +0000126void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
127 Deallocations.push_back(std::make_pair(Callback, Data));
128}
129
Mike Stump11289f42009-09-09 15:08:12 +0000130void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
132 ExternalSource.reset(Source.take());
133}
134
Chris Lattner4eb445d2007-01-26 01:27:23 +0000135void ASTContext::PrintStats() const {
136 fprintf(stderr, "*** AST Context Stats:\n");
137 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000138
Douglas Gregora30d0462009-05-26 14:40:08 +0000139 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000140#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000141#define ABSTRACT_TYPE(Name, Parent)
142#include "clang/AST/TypeNodes.def"
143 0 // Extra
144 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000145
Chris Lattner4eb445d2007-01-26 01:27:23 +0000146 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
147 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000148 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000149 }
150
Douglas Gregora30d0462009-05-26 14:40:08 +0000151 unsigned Idx = 0;
152 unsigned TotalBytes = 0;
153#define TYPE(Name, Parent) \
154 if (counts[Idx]) \
155 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
156 TotalBytes += counts[Idx] * sizeof(Name##Type); \
157 ++Idx;
158#define ABSTRACT_TYPE(Name, Parent)
159#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000160
Douglas Gregora30d0462009-05-26 14:40:08 +0000161 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162
163 if (ExternalSource.get()) {
164 fprintf(stderr, "\n");
165 ExternalSource->PrintStats();
166 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000167}
168
169
John McCall48f2d582009-10-23 23:03:21 +0000170void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000171 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000172 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000173 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000174}
175
Chris Lattner970e54e2006-11-12 00:37:36 +0000176void ASTContext::InitBuiltinTypes() {
177 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner970e54e2006-11-12 00:37:36 +0000179 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000180 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattner970e54e2006-11-12 00:37:36 +0000182 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000183 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000184 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000185 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000186 InitBuiltinType(CharTy, BuiltinType::Char_S);
187 else
188 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000189 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000190 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
191 InitBuiltinType(ShortTy, BuiltinType::Short);
192 InitBuiltinType(IntTy, BuiltinType::Int);
193 InitBuiltinType(LongTy, BuiltinType::Long);
194 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner970e54e2006-11-12 00:37:36 +0000196 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000197 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
198 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
199 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
200 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
201 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattner970e54e2006-11-12 00:37:36 +0000203 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000204 InitBuiltinType(FloatTy, BuiltinType::Float);
205 InitBuiltinType(DoubleTy, BuiltinType::Double);
206 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000207
Chris Lattnerf122cef2009-04-30 02:43:43 +0000208 // GNU extension, 128-bit integers.
209 InitBuiltinType(Int128Ty, BuiltinType::Int128);
210 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
211
Chris Lattner007cb022009-02-26 23:43:47 +0000212 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
213 InitBuiltinType(WCharTy, BuiltinType::WChar);
214 else // C99
215 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000216
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000217 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
218 InitBuiltinType(Char16Ty, BuiltinType::Char16);
219 else // C99
220 Char16Ty = getFromTargetType(Target.getChar16Type());
221
222 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
223 InitBuiltinType(Char32Ty, BuiltinType::Char32);
224 else // C99
225 Char32Ty = getFromTargetType(Target.getChar32Type());
226
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000227 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000228 InitBuiltinType(OverloadTy, BuiltinType::Overload);
229
230 // Placeholder type for type-dependent expressions whose type is
231 // completely unknown. No code should ever check a type against
232 // DependentTy and users should never see it; however, it is here to
233 // help diagnose failures to properly check for type-dependent
234 // expressions.
235 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000236
Mike Stump11289f42009-09-09 15:08:12 +0000237 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000238 // not yet been deduced.
239 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattner970e54e2006-11-12 00:37:36 +0000241 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000242 FloatComplexTy = getComplexType(FloatTy);
243 DoubleComplexTy = getComplexType(DoubleTy);
244 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000245
Steve Naroff66e9f332007-10-15 14:41:52 +0000246 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000247
Steve Naroff1329fa02009-07-15 18:40:39 +0000248 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
249 ObjCIdTypedefType = QualType();
250 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000251 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000252
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000253 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000254 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
255 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000256 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000257
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000258 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000259
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000260 // void * type
261 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000262
263 // nullptr type (C++0x 2.14.7)
264 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000265}
266
Douglas Gregor86d142a2009-10-08 07:24:58 +0000267MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000268ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000269 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000270 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000271 = InstantiatedFromStaticDataMember.find(Var);
272 if (Pos == InstantiatedFromStaticDataMember.end())
273 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000274
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000275 return Pos->second;
276}
277
Mike Stump11289f42009-09-09 15:08:12 +0000278void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000279ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
280 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000281 assert(Inst->isStaticDataMember() && "Not a static data member");
282 assert(Tmpl->isStaticDataMember() && "Not a static data member");
283 assert(!InstantiatedFromStaticDataMember[Inst] &&
284 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000285 InstantiatedFromStaticDataMember[Inst]
286 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000287}
288
John McCalle61f2ba2009-11-18 02:36:19 +0000289NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000290ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000291 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000292 = InstantiatedFromUsingDecl.find(UUD);
293 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000294 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000295
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000296 return Pos->second;
297}
298
299void
John McCallb96ec562009-12-04 22:46:56 +0000300ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
301 assert((isa<UsingDecl>(Pattern) ||
302 isa<UnresolvedUsingValueDecl>(Pattern) ||
303 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
304 "pattern decl is not a using decl");
305 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
306 InstantiatedFromUsingDecl[Inst] = Pattern;
307}
308
309UsingShadowDecl *
310ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
311 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
312 = InstantiatedFromUsingShadowDecl.find(Inst);
313 if (Pos == InstantiatedFromUsingShadowDecl.end())
314 return 0;
315
316 return Pos->second;
317}
318
319void
320ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
321 UsingShadowDecl *Pattern) {
322 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
323 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000324}
325
Anders Carlsson5da84842009-09-01 04:26:58 +0000326FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
327 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
328 = InstantiatedFromUnnamedFieldDecl.find(Field);
329 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
330 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000331
Anders Carlsson5da84842009-09-01 04:26:58 +0000332 return Pos->second;
333}
334
335void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
336 FieldDecl *Tmpl) {
337 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
338 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
339 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
340 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000341
Anders Carlsson5da84842009-09-01 04:26:58 +0000342 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
343}
344
Douglas Gregor832940b2010-03-02 23:58:15 +0000345ASTContext::overridden_cxx_method_iterator
346ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
347 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
348 = OverriddenMethods.find(Method);
349 if (Pos == OverriddenMethods.end())
350 return 0;
351
352 return Pos->second.begin();
353}
354
355ASTContext::overridden_cxx_method_iterator
356ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
357 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
358 = OverriddenMethods.find(Method);
359 if (Pos == OverriddenMethods.end())
360 return 0;
361
362 return Pos->second.end();
363}
364
365void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
366 const CXXMethodDecl *Overridden) {
367 OverriddenMethods[Method].push_back(Overridden);
368}
369
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000370namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000371 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000372 : std::binary_function<SourceRange, SourceRange, bool> {
373 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000374
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000375 public:
376 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000377
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000378 bool operator()(SourceRange X, SourceRange Y) {
379 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
380 }
381 };
382}
383
Chris Lattner53cfe802007-07-18 17:52:12 +0000384//===----------------------------------------------------------------------===//
385// Type Sizing and Analysis
386//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000387
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000388/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
389/// scalar floating point type.
390const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000391 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000392 assert(BT && "Not a floating point type!");
393 switch (BT->getKind()) {
394 default: assert(0 && "Not a floating point type!");
395 case BuiltinType::Float: return Target.getFloatFormat();
396 case BuiltinType::Double: return Target.getDoubleFormat();
397 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
398 }
399}
400
Ken Dyck160146e2010-01-27 17:10:57 +0000401/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000402/// specified decl. Note that bitfields do not have a valid alignment, so
403/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000404/// If @p RefAsPointee, references are treated like their underlying type
405/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000406CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000407 unsigned Align = Target.getCharWidth();
408
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000409 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000410 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedman19a546c2009-02-22 02:56:25 +0000411
Chris Lattner68061312009-01-24 21:53:27 +0000412 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
413 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000414 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000415 if (RefAsPointee)
416 T = RT->getPointeeType();
417 else
418 T = getPointerType(RT->getPointeeType());
419 }
420 if (!T->isIncompleteType() && !T->isFunctionType()) {
Rafael Espindolae971b9a2010-06-04 23:15:27 +0000421 unsigned MinWidth = Target.getLargeArrayMinWidth();
422 unsigned ArrayAlign = Target.getLargeArrayAlign();
423 if (isa<VariableArrayType>(T) && MinWidth != 0)
424 Align = std::max(Align, ArrayAlign);
425 if (ConstantArrayType *CT = dyn_cast<ConstantArrayType>(T)) {
426 unsigned Size = getTypeSize(CT);
427 if (MinWidth != 0 && MinWidth <= Size)
428 Align = std::max(Align, ArrayAlign);
429 }
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000430 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000431 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
432 T = cast<ArrayType>(T)->getElementType();
433
434 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
435 }
Charles Davis3fc51072010-02-23 04:52:00 +0000436 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
437 // In the case of a field in a packed struct, we want the minimum
438 // of the alignment of the field and the alignment of the struct.
439 Align = std::min(Align,
440 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
441 }
Chris Lattner68061312009-01-24 21:53:27 +0000442 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000443
Ken Dyck160146e2010-01-27 17:10:57 +0000444 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000445}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000446
John McCall87fe5d52010-05-20 01:18:31 +0000447std::pair<CharUnits, CharUnits>
448ASTContext::getTypeInfoInChars(const Type *T) {
449 std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
450 return std::make_pair(CharUnits::fromQuantity(Info.first / getCharWidth()),
451 CharUnits::fromQuantity(Info.second / getCharWidth()));
452}
453
454std::pair<CharUnits, CharUnits>
455ASTContext::getTypeInfoInChars(QualType T) {
456 return getTypeInfoInChars(T.getTypePtr());
457}
458
Chris Lattner983a8bb2007-07-13 22:13:22 +0000459/// getTypeSize - Return the size of the specified type, in bits. This method
460/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000461///
462/// FIXME: Pointers into different addr spaces could have different sizes and
463/// alignment requirements: getPointerInfo should take an AddrSpace, this
464/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000465std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000466ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000467 uint64_t Width=0;
468 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000469 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000470#define TYPE(Class, Base)
471#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000472#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000473#define DEPENDENT_TYPE(Class, Base) case Type::Class:
474#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000475 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000476 break;
477
Chris Lattner355332d2007-07-13 22:27:08 +0000478 case Type::FunctionNoProto:
479 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000480 // GCC extension: alignof(function) = 32 bits
481 Width = 0;
482 Align = 32;
483 break;
484
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000485 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000486 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000487 Width = 0;
488 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
489 break;
490
Steve Naroff5c131802007-08-30 01:06:46 +0000491 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000492 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000493
Chris Lattner37e05872008-03-05 18:54:05 +0000494 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000495 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000496 Align = EltInfo.second;
497 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000498 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000499 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000500 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000501 const VectorType *VT = cast<VectorType>(T);
502 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
503 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000504 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000505 // If the alignment is not a power of 2, round up to the next power of 2.
506 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000507 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000508 Align = llvm::NextPowerOf2(Align);
509 Width = llvm::RoundUpToAlignment(Width, Align);
510 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000511 break;
512 }
Chris Lattner647fb222007-07-18 18:26:58 +0000513
Chris Lattner7570e9c2008-03-08 08:52:55 +0000514 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000515 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000516 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000517 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000518 // GCC extension: alignof(void) = 8 bits.
519 Width = 0;
520 Align = 8;
521 break;
522
Chris Lattner6a4f7452007-12-19 19:23:28 +0000523 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000524 Width = Target.getBoolWidth();
525 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000526 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000527 case BuiltinType::Char_S:
528 case BuiltinType::Char_U:
529 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000530 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000531 Width = Target.getCharWidth();
532 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000533 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000534 case BuiltinType::WChar:
535 Width = Target.getWCharWidth();
536 Align = Target.getWCharAlign();
537 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000538 case BuiltinType::Char16:
539 Width = Target.getChar16Width();
540 Align = Target.getChar16Align();
541 break;
542 case BuiltinType::Char32:
543 Width = Target.getChar32Width();
544 Align = Target.getChar32Align();
545 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000546 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000547 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000548 Width = Target.getShortWidth();
549 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000550 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000551 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000552 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000553 Width = Target.getIntWidth();
554 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000555 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000556 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000557 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000558 Width = Target.getLongWidth();
559 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000560 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000561 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000562 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000563 Width = Target.getLongLongWidth();
564 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000565 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000566 case BuiltinType::Int128:
567 case BuiltinType::UInt128:
568 Width = 128;
569 Align = 128; // int128_t is 128-bit aligned on all targets.
570 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000571 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000572 Width = Target.getFloatWidth();
573 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000574 break;
575 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000576 Width = Target.getDoubleWidth();
577 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000578 break;
579 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000580 Width = Target.getLongDoubleWidth();
581 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000582 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000583 case BuiltinType::NullPtr:
584 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
585 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000586 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000587 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000588 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000589 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000590 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000591 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000592 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000593 case Type::BlockPointer: {
594 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
595 Width = Target.getPointerWidth(AS);
596 Align = Target.getPointerAlign(AS);
597 break;
598 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000599 case Type::LValueReference:
600 case Type::RValueReference: {
601 // alignof and sizeof should never enter this code path here, so we go
602 // the pointer route.
603 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
604 Width = Target.getPointerWidth(AS);
605 Align = Target.getPointerAlign(AS);
606 break;
607 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000608 case Type::Pointer: {
609 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000610 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000611 Align = Target.getPointerAlign(AS);
612 break;
613 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000614 case Type::MemberPointer: {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000615 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000616 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000617 getTypeInfo(getPointerDiffType());
618 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000619 if (Pointee->isFunctionType())
620 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000621 Align = PtrDiffInfo.second;
622 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000623 }
Chris Lattner647fb222007-07-18 18:26:58 +0000624 case Type::Complex: {
625 // Complex types have the same alignment as their elements, but twice the
626 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000627 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000628 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000629 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000630 Align = EltInfo.second;
631 break;
632 }
John McCall8b07ec22010-05-15 11:32:37 +0000633 case Type::ObjCObject:
634 return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
Devang Pateldbb72632008-06-04 21:54:36 +0000635 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000636 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000637 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
638 Width = Layout.getSize();
639 Align = Layout.getAlignment();
640 break;
641 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000642 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000643 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000644 const TagType *TT = cast<TagType>(T);
645
646 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000647 Width = 1;
648 Align = 1;
649 break;
650 }
Mike Stump11289f42009-09-09 15:08:12 +0000651
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000652 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000653 return getTypeInfo(ET->getDecl()->getIntegerType());
654
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000655 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000656 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
657 Width = Layout.getSize();
658 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000659 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000660 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000661
Chris Lattner63d2b362009-10-22 05:17:15 +0000662 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000663 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
664 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000665
Douglas Gregoref462e62009-04-30 17:32:17 +0000666 case Type::Typedef: {
667 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000668 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000669 Align = std::max(Aligned->getMaxAlignment(),
670 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregoref462e62009-04-30 17:32:17 +0000671 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
672 } else
673 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000674 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000675 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000676
677 case Type::TypeOfExpr:
678 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
679 .getTypePtr());
680
681 case Type::TypeOf:
682 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
683
Anders Carlsson81df7b82009-06-24 19:06:50 +0000684 case Type::Decltype:
685 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
686 .getTypePtr());
687
Abramo Bagnara6150c882010-05-11 21:36:43 +0000688 case Type::Elaborated:
689 return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000690
Douglas Gregoref462e62009-04-30 17:32:17 +0000691 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000692 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000693 "Cannot request the size of a dependent type");
694 // FIXME: this is likely to be wrong once we support template
695 // aliases, since a template alias could refer to a typedef that
696 // has an __aligned__ attribute on it.
697 return getTypeInfo(getCanonicalType(T));
698 }
Mike Stump11289f42009-09-09 15:08:12 +0000699
Chris Lattner53cfe802007-07-18 17:52:12 +0000700 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000701 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000702}
703
Ken Dyck8c89d592009-12-22 14:23:30 +0000704/// getTypeSizeInChars - Return the size of the specified type, in characters.
705/// This method does not work on incomplete types.
706CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000707 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000708}
709CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000710 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000711}
712
Ken Dycka6046ab2010-01-26 17:25:18 +0000713/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000714/// characters. This method does not work on incomplete types.
715CharUnits ASTContext::getTypeAlignInChars(QualType T) {
716 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
717}
718CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
719 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
720}
721
Chris Lattnera3402cd2009-01-27 18:08:34 +0000722/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
723/// type for the current target in bits. This can be different than the ABI
724/// alignment in cases where it is beneficial for performance to overalign
725/// a data type.
726unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
727 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000728
729 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000730 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000731 T = CT->getElementType().getTypePtr();
732 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
733 T->isSpecificBuiltinType(BuiltinType::LongLong))
734 return std::max(ABIAlign, (unsigned)getTypeSize(T));
735
Chris Lattnera3402cd2009-01-27 18:08:34 +0000736 return ABIAlign;
737}
738
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000739static void CollectLocalObjCIvars(ASTContext *Ctx,
740 const ObjCInterfaceDecl *OI,
741 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000742 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
743 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000744 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000745 if (!IVDecl->isInvalidDecl())
746 Fields.push_back(cast<FieldDecl>(IVDecl));
747 }
748}
749
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000750void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
751 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
752 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
753 CollectObjCIvars(SuperClass, Fields);
754 CollectLocalObjCIvars(this, OI, Fields);
755}
756
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000757/// ShallowCollectObjCIvars -
758/// Collect all ivars, including those synthesized, in the current class.
759///
760void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000761 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000762 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
763 E = OI->ivar_end(); I != E; ++I) {
764 Ivars.push_back(*I);
765 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000766
767 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000768}
769
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000770/// CollectNonClassIvars -
771/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000772/// This includes synthesized ivars (via @synthesize) and those in
773// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000774///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000775void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000776 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000777 // Find ivars declared in class extension.
778 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
779 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
780 E = CDecl->ivar_end(); I != E; ++I) {
781 Ivars.push_back(*I);
782 }
783 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000784
Ted Kremenek86838aa2010-03-11 19:44:54 +0000785 // Also add any ivar defined in this class's implementation. This
786 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000787 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
788 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
789 E = ImplDecl->ivar_end(); I != E; ++I)
790 Ivars.push_back(*I);
791 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000792}
793
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000794/// CollectInheritedProtocols - Collect all protocols in current class and
795/// those inherited by it.
796void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000797 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000798 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
799 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
800 PE = OI->protocol_end(); P != PE; ++P) {
801 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000802 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000803 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000804 PE = Proto->protocol_end(); P != PE; ++P) {
805 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000806 CollectInheritedProtocols(*P, Protocols);
807 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000808 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000809
810 // Categories of this Interface.
811 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
812 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
813 CollectInheritedProtocols(CDeclChain, Protocols);
814 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
815 while (SD) {
816 CollectInheritedProtocols(SD, Protocols);
817 SD = SD->getSuperClass();
818 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000819 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000820 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
821 PE = OC->protocol_end(); P != PE; ++P) {
822 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000823 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000824 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
825 PE = Proto->protocol_end(); P != PE; ++P)
826 CollectInheritedProtocols(*P, Protocols);
827 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000828 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000829 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
830 PE = OP->protocol_end(); P != PE; ++P) {
831 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000832 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000833 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
834 PE = Proto->protocol_end(); P != PE; ++P)
835 CollectInheritedProtocols(*P, Protocols);
836 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000837 }
838}
839
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000840unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
841 unsigned count = 0;
842 // Count ivars declared in class extension.
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000843 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension())
844 count += CDecl->ivar_size();
845
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000846 // Count ivar defined in this class's implementation. This
847 // includes synthesized ivars.
848 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000849 count += ImplDecl->ivar_size();
850
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000851 return count;
852}
853
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000854/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
855ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
856 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
857 I = ObjCImpls.find(D);
858 if (I != ObjCImpls.end())
859 return cast<ObjCImplementationDecl>(I->second);
860 return 0;
861}
862/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
863ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
864 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
865 I = ObjCImpls.find(D);
866 if (I != ObjCImpls.end())
867 return cast<ObjCCategoryImplDecl>(I->second);
868 return 0;
869}
870
871/// \brief Set the implementation of ObjCInterfaceDecl.
872void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
873 ObjCImplementationDecl *ImplD) {
874 assert(IFaceD && ImplD && "Passed null params");
875 ObjCImpls[IFaceD] = ImplD;
876}
877/// \brief Set the implementation of ObjCCategoryDecl.
878void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
879 ObjCCategoryImplDecl *ImplD) {
880 assert(CatD && ImplD && "Passed null params");
881 ObjCImpls[CatD] = ImplD;
882}
883
John McCallbcd03502009-12-07 02:54:59 +0000884/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000885///
John McCallbcd03502009-12-07 02:54:59 +0000886/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000887/// the TypeLoc wrappers.
888///
889/// \param T the type that will be the basis for type source info. This type
890/// should refer to how the declarator was written in source code, not to
891/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000892TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000893 unsigned DataSize) {
894 if (!DataSize)
895 DataSize = TypeLoc::getFullDataSizeForType(T);
896 else
897 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000898 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000899
John McCallbcd03502009-12-07 02:54:59 +0000900 TypeSourceInfo *TInfo =
901 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
902 new (TInfo) TypeSourceInfo(T);
903 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000904}
905
John McCallbcd03502009-12-07 02:54:59 +0000906TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000907 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +0000908 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +0000909 DI->getTypeLoc().initialize(L);
910 return DI;
911}
912
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000913const ASTRecordLayout &
914ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
915 return getObjCLayout(D, 0);
916}
917
918const ASTRecordLayout &
919ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
920 return getObjCLayout(D->getClassInterface(), D);
921}
922
Chris Lattner983a8bb2007-07-13 22:13:22 +0000923//===----------------------------------------------------------------------===//
924// Type creation/memoization methods
925//===----------------------------------------------------------------------===//
926
John McCall8ccfcb52009-09-24 19:53:00 +0000927QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
928 unsigned Fast = Quals.getFastQualifiers();
929 Quals.removeFastQualifiers();
930
931 // Check if we've already instantiated this type.
932 llvm::FoldingSetNodeID ID;
933 ExtQuals::Profile(ID, TypeNode, Quals);
934 void *InsertPos = 0;
935 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
936 assert(EQ->getQualifiers() == Quals);
937 QualType T = QualType(EQ, Fast);
938 return T;
939 }
940
John McCall90d1c2d2009-09-24 23:30:46 +0000941 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000942 ExtQualNodes.InsertNode(New, InsertPos);
943 QualType T = QualType(New, Fast);
944 return T;
945}
946
947QualType ASTContext::getVolatileType(QualType T) {
948 QualType CanT = getCanonicalType(T);
949 if (CanT.isVolatileQualified()) return T;
950
951 QualifierCollector Quals;
952 const Type *TypeNode = Quals.strip(T);
953 Quals.addVolatile();
954
955 return getExtQualType(TypeNode, Quals);
956}
957
Fariborz Jahanianece85822009-02-17 18:27:45 +0000958QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +0000959 QualType CanT = getCanonicalType(T);
960 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +0000961 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +0000962
John McCall8ccfcb52009-09-24 19:53:00 +0000963 // If we are composing extended qualifiers together, merge together
964 // into one ExtQuals node.
965 QualifierCollector Quals;
966 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +0000967
John McCall8ccfcb52009-09-24 19:53:00 +0000968 // If this type already has an address space specified, it cannot get
969 // another one.
970 assert(!Quals.hasAddressSpace() &&
971 "Type cannot be in multiple addr spaces!");
972 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +0000973
John McCall8ccfcb52009-09-24 19:53:00 +0000974 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +0000975}
976
Chris Lattnerd60183d2009-02-18 22:53:11 +0000977QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +0000978 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +0000979 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +0000980 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +0000981 return T;
Mike Stump11289f42009-09-09 15:08:12 +0000982
Fariborz Jahanianb68215c2009-06-03 17:15:17 +0000983 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000984 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +0000985 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +0000986 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
987 return getPointerType(ResultType);
988 }
989 }
Mike Stump11289f42009-09-09 15:08:12 +0000990
John McCall8ccfcb52009-09-24 19:53:00 +0000991 // If we are composing extended qualifiers together, merge together
992 // into one ExtQuals node.
993 QualifierCollector Quals;
994 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +0000995
John McCall8ccfcb52009-09-24 19:53:00 +0000996 // If this type already has an ObjCGC specified, it cannot get
997 // another one.
998 assert(!Quals.hasObjCGCAttr() &&
999 "Type cannot have multiple ObjCGCs!");
1000 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001001
John McCall8ccfcb52009-09-24 19:53:00 +00001002 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001003}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001004
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001005static QualType getExtFunctionType(ASTContext& Context, QualType T,
1006 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001007 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001008 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1009 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001010 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001011 if (ResultType == Pointee)
1012 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001013
1014 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001015 } else if (const BlockPointerType *BlockPointer
1016 = T->getAs<BlockPointerType>()) {
1017 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001018 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001019 if (ResultType == Pointee)
1020 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001021
1022 ResultType = Context.getBlockPointerType(ResultType);
1023 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001024 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001025 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001026
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001027 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001028 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001029 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001030 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001031 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001032 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001033 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1034 FPT->getNumArgs(), FPT->isVariadic(),
1035 FPT->getTypeQuals(),
1036 FPT->hasExceptionSpec(),
1037 FPT->hasAnyExceptionSpec(),
1038 FPT->getNumExceptions(),
1039 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001040 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001041 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001042 } else
1043 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001044
1045 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1046}
1047
1048QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001049 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001050 return getExtFunctionType(*this, T,
1051 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001052}
1053
1054QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001055 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001056 return getExtFunctionType(*this, T,
1057 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001058}
1059
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001060QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1061 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1062 return getExtFunctionType(*this, T,
1063 Info.withRegParm(RegParm));
1064}
1065
Chris Lattnerc6395932007-06-22 20:56:16 +00001066/// getComplexType - Return the uniqued reference to the type for a complex
1067/// number with the specified element type.
1068QualType ASTContext::getComplexType(QualType T) {
1069 // Unique pointers, to guarantee there is only one pointer of a particular
1070 // structure.
1071 llvm::FoldingSetNodeID ID;
1072 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001073
Chris Lattnerc6395932007-06-22 20:56:16 +00001074 void *InsertPos = 0;
1075 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1076 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattnerc6395932007-06-22 20:56:16 +00001078 // If the pointee type isn't canonical, this won't be a canonical type either,
1079 // so fill in the canonical type field.
1080 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001081 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001082 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattnerc6395932007-06-22 20:56:16 +00001084 // Get the new insert position for the node we care about.
1085 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001086 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001087 }
John McCall90d1c2d2009-09-24 23:30:46 +00001088 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001089 Types.push_back(New);
1090 ComplexTypes.InsertNode(New, InsertPos);
1091 return QualType(New, 0);
1092}
1093
Chris Lattner970e54e2006-11-12 00:37:36 +00001094/// getPointerType - Return the uniqued reference to the type for a pointer to
1095/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001096QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001097 // Unique pointers, to guarantee there is only one pointer of a particular
1098 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001099 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001100 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001101
Chris Lattner67521df2007-01-27 01:29:36 +00001102 void *InsertPos = 0;
1103 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001104 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner7ccecb92006-11-12 08:50:50 +00001106 // If the pointee type isn't canonical, this won't be a canonical type either,
1107 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001108 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001109 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001110 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001111
Chris Lattner67521df2007-01-27 01:29:36 +00001112 // Get the new insert position for the node we care about.
1113 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001114 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001115 }
John McCall90d1c2d2009-09-24 23:30:46 +00001116 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001117 Types.push_back(New);
1118 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001119 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001120}
1121
Mike Stump11289f42009-09-09 15:08:12 +00001122/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001123/// a pointer to the specified block.
1124QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001125 assert(T->isFunctionType() && "block of function types only");
1126 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001127 // structure.
1128 llvm::FoldingSetNodeID ID;
1129 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001130
Steve Naroffec33ed92008-08-27 16:04:49 +00001131 void *InsertPos = 0;
1132 if (BlockPointerType *PT =
1133 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1134 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001135
1136 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001137 // type either so fill in the canonical type field.
1138 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001139 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001140 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001141
Steve Naroffec33ed92008-08-27 16:04:49 +00001142 // Get the new insert position for the node we care about.
1143 BlockPointerType *NewIP =
1144 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001145 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001146 }
John McCall90d1c2d2009-09-24 23:30:46 +00001147 BlockPointerType *New
1148 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001149 Types.push_back(New);
1150 BlockPointerTypes.InsertNode(New, InsertPos);
1151 return QualType(New, 0);
1152}
1153
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001154/// getLValueReferenceType - Return the uniqued reference to the type for an
1155/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001156QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001157 // Unique pointers, to guarantee there is only one pointer of a particular
1158 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001159 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001160 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001161
1162 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001163 if (LValueReferenceType *RT =
1164 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001165 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001166
John McCallfc93cf92009-10-22 22:37:11 +00001167 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1168
Bill Wendling3708c182007-05-27 10:15:43 +00001169 // If the referencee type isn't canonical, this won't be a canonical type
1170 // either, so fill in the canonical type field.
1171 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001172 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1173 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1174 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001175
Bill Wendling3708c182007-05-27 10:15:43 +00001176 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001177 LValueReferenceType *NewIP =
1178 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001179 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001180 }
1181
John McCall90d1c2d2009-09-24 23:30:46 +00001182 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001183 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1184 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001185 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001186 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001187
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001188 return QualType(New, 0);
1189}
1190
1191/// getRValueReferenceType - Return the uniqued reference to the type for an
1192/// rvalue reference to the specified type.
1193QualType ASTContext::getRValueReferenceType(QualType T) {
1194 // Unique pointers, to guarantee there is only one pointer of a particular
1195 // structure.
1196 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001197 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001198
1199 void *InsertPos = 0;
1200 if (RValueReferenceType *RT =
1201 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1202 return QualType(RT, 0);
1203
John McCallfc93cf92009-10-22 22:37:11 +00001204 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1205
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001206 // If the referencee type isn't canonical, this won't be a canonical type
1207 // either, so fill in the canonical type field.
1208 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001209 if (InnerRef || !T.isCanonical()) {
1210 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1211 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001212
1213 // Get the new insert position for the node we care about.
1214 RValueReferenceType *NewIP =
1215 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1216 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1217 }
1218
John McCall90d1c2d2009-09-24 23:30:46 +00001219 RValueReferenceType *New
1220 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001221 Types.push_back(New);
1222 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001223 return QualType(New, 0);
1224}
1225
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001226/// getMemberPointerType - Return the uniqued reference to the type for a
1227/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001228QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001229 // Unique pointers, to guarantee there is only one pointer of a particular
1230 // structure.
1231 llvm::FoldingSetNodeID ID;
1232 MemberPointerType::Profile(ID, T, Cls);
1233
1234 void *InsertPos = 0;
1235 if (MemberPointerType *PT =
1236 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1237 return QualType(PT, 0);
1238
1239 // If the pointee or class type isn't canonical, this won't be a canonical
1240 // type either, so fill in the canonical type field.
1241 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001242 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001243 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1244
1245 // Get the new insert position for the node we care about.
1246 MemberPointerType *NewIP =
1247 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1248 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1249 }
John McCall90d1c2d2009-09-24 23:30:46 +00001250 MemberPointerType *New
1251 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001252 Types.push_back(New);
1253 MemberPointerTypes.InsertNode(New, InsertPos);
1254 return QualType(New, 0);
1255}
1256
Mike Stump11289f42009-09-09 15:08:12 +00001257/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001258/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001259QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001260 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001261 ArrayType::ArraySizeModifier ASM,
1262 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001263 assert((EltTy->isDependentType() ||
1264 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001265 "Constant array of VLAs is illegal!");
1266
Chris Lattnere2df3f92009-05-13 04:12:56 +00001267 // Convert the array size into a canonical width matching the pointer size for
1268 // the target.
1269 llvm::APInt ArySize(ArySizeIn);
1270 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001271
Chris Lattner23b7eb62007-06-15 23:05:46 +00001272 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001273 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001274
Chris Lattner36f8e652007-01-27 08:31:04 +00001275 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001276 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001277 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001278 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001279
Chris Lattner7ccecb92006-11-12 08:50:50 +00001280 // If the element type isn't canonical, this won't be a canonical type either,
1281 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001282 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001283 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001284 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001285 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001286 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001287 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001288 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001289 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001290 }
Mike Stump11289f42009-09-09 15:08:12 +00001291
John McCall90d1c2d2009-09-24 23:30:46 +00001292 ConstantArrayType *New = new(*this,TypeAlignment)
1293 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001294 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001295 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001296 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001297}
1298
Steve Naroffcadebd02007-08-30 18:14:25 +00001299/// getVariableArrayType - Returns a non-unique reference to the type for a
1300/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001301QualType ASTContext::getVariableArrayType(QualType EltTy,
1302 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001303 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001304 unsigned EltTypeQuals,
1305 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001306 // Since we don't unique expressions, it isn't possible to unique VLA's
1307 // that have an expression provided for their size.
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001308 QualType CanonType;
1309
1310 if (!EltTy.isCanonical()) {
1311 if (NumElts)
1312 NumElts->Retain();
1313 CanonType = getVariableArrayType(getCanonicalType(EltTy), NumElts, ASM,
1314 EltTypeQuals, Brackets);
1315 }
1316
John McCall90d1c2d2009-09-24 23:30:46 +00001317 VariableArrayType *New = new(*this, TypeAlignment)
Douglas Gregor5e8c8c02010-05-23 16:10:32 +00001318 VariableArrayType(EltTy, CanonType, NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001319
1320 VariableArrayTypes.push_back(New);
1321 Types.push_back(New);
1322 return QualType(New, 0);
1323}
1324
Douglas Gregor4619e432008-12-05 23:32:09 +00001325/// getDependentSizedArrayType - Returns a non-unique reference to
1326/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001327/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001328QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1329 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001330 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001331 unsigned EltTypeQuals,
1332 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001333 assert((!NumElts || NumElts->isTypeDependent() ||
1334 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001335 "Size must be type- or value-dependent!");
1336
Douglas Gregorf3f95522009-07-31 00:23:35 +00001337 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001338 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001339 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001340
1341 if (NumElts) {
1342 // Dependently-sized array types that do not have a specified
1343 // number of elements will have their sizes deduced from an
1344 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001345 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1346 EltTypeQuals, NumElts);
1347
1348 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1349 }
1350
Douglas Gregorf3f95522009-07-31 00:23:35 +00001351 DependentSizedArrayType *New;
1352 if (Canon) {
1353 // We already have a canonical version of this array type; use it as
1354 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001355 New = new (*this, TypeAlignment)
1356 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1357 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001358 } else {
1359 QualType CanonEltTy = getCanonicalType(EltTy);
1360 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001361 New = new (*this, TypeAlignment)
1362 DependentSizedArrayType(*this, EltTy, QualType(),
1363 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001364
Douglas Gregorc42075a2010-02-04 18:10:26 +00001365 if (NumElts) {
1366 DependentSizedArrayType *CanonCheck
1367 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1368 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1369 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001370 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001371 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001372 } else {
1373 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1374 ASM, EltTypeQuals,
1375 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001376 New = new (*this, TypeAlignment)
1377 DependentSizedArrayType(*this, EltTy, Canon,
1378 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001379 }
1380 }
Mike Stump11289f42009-09-09 15:08:12 +00001381
Douglas Gregor4619e432008-12-05 23:32:09 +00001382 Types.push_back(New);
1383 return QualType(New, 0);
1384}
1385
Eli Friedmanbd258282008-02-15 18:16:39 +00001386QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1387 ArrayType::ArraySizeModifier ASM,
1388 unsigned EltTypeQuals) {
1389 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001390 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001391
1392 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001393 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001394 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1395 return QualType(ATP, 0);
1396
1397 // If the element type isn't canonical, this won't be a canonical type
1398 // either, so fill in the canonical type field.
1399 QualType Canonical;
1400
John McCallb692a092009-10-22 20:10:53 +00001401 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001402 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001403 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001404
1405 // Get the new insert position for the node we care about.
1406 IncompleteArrayType *NewIP =
1407 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001408 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001409 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001410
John McCall90d1c2d2009-09-24 23:30:46 +00001411 IncompleteArrayType *New = new (*this, TypeAlignment)
1412 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001413
1414 IncompleteArrayTypes.InsertNode(New, InsertPos);
1415 Types.push_back(New);
1416 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001417}
1418
Steve Naroff91fcddb2007-07-18 18:00:27 +00001419/// getVectorType - Return the unique reference to a vector type of
1420/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001421QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1422 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001423 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001424
Chris Lattner76a00cf2008-04-06 22:59:24 +00001425 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001426 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001427
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001428 // Check if we've already instantiated a vector of this type.
1429 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001430 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1431 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001432 void *InsertPos = 0;
1433 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1434 return QualType(VTP, 0);
1435
1436 // If the element type isn't canonical, this won't be a canonical type either,
1437 // so fill in the canonical type field.
1438 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001439 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1440 Canonical = getVectorType(getCanonicalType(vecType),
1441 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001442
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001443 // Get the new insert position for the node we care about.
1444 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001445 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001446 }
John McCall90d1c2d2009-09-24 23:30:46 +00001447 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001448 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001449 VectorTypes.InsertNode(New, InsertPos);
1450 Types.push_back(New);
1451 return QualType(New, 0);
1452}
1453
Nate Begemance4d7fc2008-04-18 23:10:10 +00001454/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001455/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001456QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001457 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001458
Chris Lattner76a00cf2008-04-06 22:59:24 +00001459 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001460 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001461
Steve Naroff91fcddb2007-07-18 18:00:27 +00001462 // Check if we've already instantiated a vector of this type.
1463 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001464 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001465 void *InsertPos = 0;
1466 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1467 return QualType(VTP, 0);
1468
1469 // If the element type isn't canonical, this won't be a canonical type either,
1470 // so fill in the canonical type field.
1471 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001472 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001473 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001474
Steve Naroff91fcddb2007-07-18 18:00:27 +00001475 // Get the new insert position for the node we care about.
1476 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001477 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001478 }
John McCall90d1c2d2009-09-24 23:30:46 +00001479 ExtVectorType *New = new (*this, TypeAlignment)
1480 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001481 VectorTypes.InsertNode(New, InsertPos);
1482 Types.push_back(New);
1483 return QualType(New, 0);
1484}
1485
Mike Stump11289f42009-09-09 15:08:12 +00001486QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001487 Expr *SizeExpr,
1488 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001489 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001490 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001491 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001492
Douglas Gregor352169a2009-07-31 03:54:25 +00001493 void *InsertPos = 0;
1494 DependentSizedExtVectorType *Canon
1495 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1496 DependentSizedExtVectorType *New;
1497 if (Canon) {
1498 // We already have a canonical version of this array type; use it as
1499 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001500 New = new (*this, TypeAlignment)
1501 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1502 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001503 } else {
1504 QualType CanonVecTy = getCanonicalType(vecType);
1505 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001506 New = new (*this, TypeAlignment)
1507 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1508 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001509
1510 DependentSizedExtVectorType *CanonCheck
1511 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1512 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1513 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001514 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1515 } else {
1516 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1517 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001518 New = new (*this, TypeAlignment)
1519 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001520 }
1521 }
Mike Stump11289f42009-09-09 15:08:12 +00001522
Douglas Gregor758a8692009-06-17 21:51:59 +00001523 Types.push_back(New);
1524 return QualType(New, 0);
1525}
1526
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001527/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001528///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001529QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1530 const FunctionType::ExtInfo &Info) {
1531 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001532 // Unique functions, to guarantee there is only one function of a particular
1533 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001534 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001535 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001536
Chris Lattner47955de2007-01-27 08:37:20 +00001537 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001538 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001539 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001540 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001541
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001542 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001543 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001544 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001545 Canonical =
1546 getFunctionNoProtoType(getCanonicalType(ResultTy),
1547 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001548
Chris Lattner47955de2007-01-27 08:37:20 +00001549 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001550 FunctionNoProtoType *NewIP =
1551 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001552 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001553 }
Mike Stump11289f42009-09-09 15:08:12 +00001554
John McCall90d1c2d2009-09-24 23:30:46 +00001555 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001556 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001557 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001558 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001559 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001560}
1561
1562/// getFunctionType - Return a normal function type with a typed argument
1563/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001564QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001565 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001566 unsigned TypeQuals, bool hasExceptionSpec,
1567 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001568 const QualType *ExArray,
1569 const FunctionType::ExtInfo &Info) {
1570 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001571 // Unique functions, to guarantee there is only one function of a particular
1572 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001573 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001574 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001575 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001576 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001577
1578 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001579 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001580 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001581 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001582
1583 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001584 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001585 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001586 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001587 isCanonical = false;
1588
1589 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001590 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001591 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001592 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001593 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001594 CanonicalArgs.reserve(NumArgs);
1595 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001596 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001597
Chris Lattner76a00cf2008-04-06 22:59:24 +00001598 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001599 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001600 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001601 false, 0, 0,
1602 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001603
Chris Lattnerfd4de792007-01-27 01:15:32 +00001604 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001605 FunctionProtoType *NewIP =
1606 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001607 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001608 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001609
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001610 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001611 // for two variable size arrays (for parameter and exception types) at the
1612 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001613 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001614 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1615 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001616 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001617 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001618 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001619 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001620 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001621 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001622 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001623}
Chris Lattneref51c202006-11-10 07:17:23 +00001624
John McCalle78aac42010-03-10 03:28:59 +00001625#ifndef NDEBUG
1626static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1627 if (!isa<CXXRecordDecl>(D)) return false;
1628 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1629 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1630 return true;
1631 if (RD->getDescribedClassTemplate() &&
1632 !isa<ClassTemplateSpecializationDecl>(RD))
1633 return true;
1634 return false;
1635}
1636#endif
1637
1638/// getInjectedClassNameType - Return the unique reference to the
1639/// injected class name type for the specified templated declaration.
1640QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1641 QualType TST) {
1642 assert(NeedsInjectedClassNameType(Decl));
1643 if (Decl->TypeForDecl) {
1644 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1645 } else if (CXXRecordDecl *PrevDecl
1646 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1647 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1648 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1649 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1650 } else {
John McCall2408e322010-04-27 00:57:59 +00001651 Decl->TypeForDecl =
1652 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001653 Types.push_back(Decl->TypeForDecl);
1654 }
1655 return QualType(Decl->TypeForDecl, 0);
1656}
1657
Douglas Gregor83a586e2008-04-13 21:07:44 +00001658/// getTypeDeclType - Return the unique reference to the type for the
1659/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001660QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001661 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001662 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001663
John McCall81e38502010-02-16 03:57:14 +00001664 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001665 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001666
John McCall96f0b5f2010-03-10 06:48:02 +00001667 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1668 "Template type parameter types are always available.");
1669
John McCall81e38502010-02-16 03:57:14 +00001670 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001671 assert(!Record->getPreviousDeclaration() &&
1672 "struct/union has previous declaration");
1673 assert(!NeedsInjectedClassNameType(Record));
1674 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001675 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001676 assert(!Enum->getPreviousDeclaration() &&
1677 "enum has previous declaration");
1678 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001679 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001680 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1681 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001682 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001683 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001684
John McCall96f0b5f2010-03-10 06:48:02 +00001685 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001686 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001687}
1688
Chris Lattner32d920b2007-01-26 02:01:53 +00001689/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001690/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001691QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001692 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001693
Chris Lattner76a00cf2008-04-06 22:59:24 +00001694 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001695 Decl->TypeForDecl = new(*this, TypeAlignment)
1696 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001697 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001698 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001699}
1700
John McCallcebee162009-10-18 09:09:24 +00001701/// \brief Retrieve a substitution-result type.
1702QualType
1703ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1704 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001705 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001706 && "replacement types must always be canonical");
1707
1708 llvm::FoldingSetNodeID ID;
1709 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1710 void *InsertPos = 0;
1711 SubstTemplateTypeParmType *SubstParm
1712 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1713
1714 if (!SubstParm) {
1715 SubstParm = new (*this, TypeAlignment)
1716 SubstTemplateTypeParmType(Parm, Replacement);
1717 Types.push_back(SubstParm);
1718 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1719 }
1720
1721 return QualType(SubstParm, 0);
1722}
1723
Douglas Gregoreff93e02009-02-05 23:33:38 +00001724/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001725/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001726/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001727QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001728 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001729 IdentifierInfo *Name) {
1730 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001731 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001732 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001733 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001734 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1735
1736 if (TypeParm)
1737 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001738
Anders Carlsson90036dc2009-06-16 00:30:48 +00001739 if (Name) {
1740 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001741 TypeParm = new (*this, TypeAlignment)
1742 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001743
1744 TemplateTypeParmType *TypeCheck
1745 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1746 assert(!TypeCheck && "Template type parameter canonical type broken");
1747 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001748 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001749 TypeParm = new (*this, TypeAlignment)
1750 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001751
1752 Types.push_back(TypeParm);
1753 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1754
1755 return QualType(TypeParm, 0);
1756}
1757
John McCalle78aac42010-03-10 03:28:59 +00001758TypeSourceInfo *
1759ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1760 SourceLocation NameLoc,
1761 const TemplateArgumentListInfo &Args,
1762 QualType CanonType) {
1763 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1764
1765 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1766 TemplateSpecializationTypeLoc TL
1767 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1768 TL.setTemplateNameLoc(NameLoc);
1769 TL.setLAngleLoc(Args.getLAngleLoc());
1770 TL.setRAngleLoc(Args.getRAngleLoc());
1771 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1772 TL.setArgLocInfo(i, Args[i].getLocInfo());
1773 return DI;
1774}
1775
Mike Stump11289f42009-09-09 15:08:12 +00001776QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001777ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001778 const TemplateArgumentListInfo &Args,
John McCall2408e322010-04-27 00:57:59 +00001779 QualType Canon,
1780 bool IsCurrentInstantiation) {
John McCall6b51f282009-11-23 01:53:49 +00001781 unsigned NumArgs = Args.size();
1782
John McCall0ad16662009-10-29 08:12:44 +00001783 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1784 ArgVec.reserve(NumArgs);
1785 for (unsigned i = 0; i != NumArgs; ++i)
1786 ArgVec.push_back(Args[i].getArgument());
1787
John McCall2408e322010-04-27 00:57:59 +00001788 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
1789 Canon, IsCurrentInstantiation);
John McCall0ad16662009-10-29 08:12:44 +00001790}
1791
1792QualType
1793ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001794 const TemplateArgument *Args,
1795 unsigned NumArgs,
John McCall2408e322010-04-27 00:57:59 +00001796 QualType Canon,
1797 bool IsCurrentInstantiation) {
Douglas Gregor15301382009-07-30 17:40:51 +00001798 if (!Canon.isNull())
1799 Canon = getCanonicalType(Canon);
1800 else {
John McCall2408e322010-04-27 00:57:59 +00001801 assert(!IsCurrentInstantiation &&
1802 "current-instantiation specializations should always "
1803 "have a canonical type");
1804
Douglas Gregor15301382009-07-30 17:40:51 +00001805 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001806 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1807 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1808 CanonArgs.reserve(NumArgs);
1809 for (unsigned I = 0; I != NumArgs; ++I)
1810 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1811
1812 // Determine whether this canonical template specialization type already
1813 // exists.
1814 llvm::FoldingSetNodeID ID;
John McCall2408e322010-04-27 00:57:59 +00001815 TemplateSpecializationType::Profile(ID, CanonTemplate, false,
Douglas Gregor00044172009-07-29 16:09:57 +00001816 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001817
1818 void *InsertPos = 0;
1819 TemplateSpecializationType *Spec
1820 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001821
Douglas Gregora8e02e72009-07-28 23:00:59 +00001822 if (!Spec) {
1823 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001824 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001825 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001826 TypeAlignment);
John McCall2408e322010-04-27 00:57:59 +00001827 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001828 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001829 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001830 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001831 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001832 }
Mike Stump11289f42009-09-09 15:08:12 +00001833
Douglas Gregor15301382009-07-30 17:40:51 +00001834 if (Canon.isNull())
1835 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001836 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001837 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001838 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001839
Douglas Gregora8e02e72009-07-28 23:00:59 +00001840 // Allocate the (non-canonical) template specialization type, but don't
1841 // try to unique it: these types typically have location information that
1842 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001843 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001844 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001845 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001846 TemplateSpecializationType *Spec
John McCall2408e322010-04-27 00:57:59 +00001847 = new (Mem) TemplateSpecializationType(*this, Template,
1848 IsCurrentInstantiation,
1849 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001850 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001851
Douglas Gregor8bf42052009-02-09 18:46:07 +00001852 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001853 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001854}
1855
Mike Stump11289f42009-09-09 15:08:12 +00001856QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +00001857ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
1858 NestedNameSpecifier *NNS,
1859 QualType NamedType) {
Douglas Gregor52537682009-03-19 00:18:19 +00001860 llvm::FoldingSetNodeID ID;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001861 ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001862
1863 void *InsertPos = 0;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001864 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00001865 if (T)
1866 return QualType(T, 0);
1867
Douglas Gregorc42075a2010-02-04 18:10:26 +00001868 QualType Canon = NamedType;
1869 if (!Canon.isCanonical()) {
1870 Canon = getCanonicalType(NamedType);
Abramo Bagnara6150c882010-05-11 21:36:43 +00001871 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1872 assert(!CheckT && "Elaborated canonical type broken");
Douglas Gregorc42075a2010-02-04 18:10:26 +00001873 (void)CheckT;
1874 }
1875
Abramo Bagnara6150c882010-05-11 21:36:43 +00001876 T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001877 Types.push_back(T);
Abramo Bagnara6150c882010-05-11 21:36:43 +00001878 ElaboratedTypes.InsertNode(T, InsertPos);
Douglas Gregor52537682009-03-19 00:18:19 +00001879 return QualType(T, 0);
1880}
1881
Douglas Gregor02085352010-03-31 20:19:30 +00001882QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1883 NestedNameSpecifier *NNS,
1884 const IdentifierInfo *Name,
1885 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001886 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1887
1888 if (Canon.isNull()) {
1889 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001890 ElaboratedTypeKeyword CanonKeyword = Keyword;
1891 if (Keyword == ETK_None)
1892 CanonKeyword = ETK_Typename;
1893
1894 if (CanonNNS != NNS || CanonKeyword != Keyword)
1895 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001896 }
1897
1898 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001899 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001900
1901 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001902 DependentNameType *T
1903 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001904 if (T)
1905 return QualType(T, 0);
1906
Douglas Gregor02085352010-03-31 20:19:30 +00001907 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00001908 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001909 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001910 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001911}
1912
Mike Stump11289f42009-09-09 15:08:12 +00001913QualType
Douglas Gregor02085352010-03-31 20:19:30 +00001914ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1915 NestedNameSpecifier *NNS,
1916 const TemplateSpecializationType *TemplateId,
1917 QualType Canon) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001918 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1919
Douglas Gregorc42075a2010-02-04 18:10:26 +00001920 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001921 DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001922
1923 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001924 DependentNameType *T
1925 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001926 if (T)
1927 return QualType(T, 0);
1928
Douglas Gregordce2b622009-04-01 00:28:59 +00001929 if (Canon.isNull()) {
1930 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1931 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
Douglas Gregor02085352010-03-31 20:19:30 +00001932 ElaboratedTypeKeyword CanonKeyword = Keyword;
1933 if (Keyword == ETK_None)
1934 CanonKeyword = ETK_Typename;
1935 if (CanonNNS != NNS || CanonKeyword != Keyword ||
1936 CanonType != QualType(TemplateId, 0)) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001937 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00001938 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00001939 assert(CanonTemplateId &&
1940 "Canonical type must also be a template specialization type");
Douglas Gregor02085352010-03-31 20:19:30 +00001941 Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
Douglas Gregordce2b622009-04-01 00:28:59 +00001942 }
Douglas Gregorc42075a2010-02-04 18:10:26 +00001943
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001944 DependentNameType *CheckT
1945 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001946 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregordce2b622009-04-01 00:28:59 +00001947 }
1948
Douglas Gregor02085352010-03-31 20:19:30 +00001949 T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00001950 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001951 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001952 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00001953}
1954
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001955/// CmpProtocolNames - Comparison predicate for sorting protocols
1956/// alphabetically.
1957static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1958 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00001959 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001960}
1961
John McCall8b07ec22010-05-15 11:32:37 +00001962static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
John McCallfc93cf92009-10-22 22:37:11 +00001963 unsigned NumProtocols) {
1964 if (NumProtocols == 0) return true;
1965
1966 for (unsigned i = 1; i != NumProtocols; ++i)
1967 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
1968 return false;
1969 return true;
1970}
1971
1972static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001973 unsigned &NumProtocols) {
1974 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00001975
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001976 // Sort protocols, keyed by name.
1977 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1978
1979 // Remove duplicates.
1980 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1981 NumProtocols = ProtocolsEnd-Protocols;
1982}
1983
John McCall8b07ec22010-05-15 11:32:37 +00001984QualType ASTContext::getObjCObjectType(QualType BaseType,
1985 ObjCProtocolDecl * const *Protocols,
1986 unsigned NumProtocols) {
1987 // If the base type is an interface and there aren't any protocols
1988 // to add, then the interface type will do just fine.
1989 if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
1990 return BaseType;
1991
1992 // Look in the folding set for an existing type.
Steve Narofffb4330f2009-06-17 22:40:22 +00001993 llvm::FoldingSetNodeID ID;
John McCall8b07ec22010-05-15 11:32:37 +00001994 ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00001995 void *InsertPos = 0;
John McCall8b07ec22010-05-15 11:32:37 +00001996 if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
1997 return QualType(QT, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00001998
John McCall8b07ec22010-05-15 11:32:37 +00001999 // Build the canonical type, which has the canonical base type and
2000 // a sorted-and-uniqued list of protocols.
John McCallfc93cf92009-10-22 22:37:11 +00002001 QualType Canonical;
John McCall8b07ec22010-05-15 11:32:37 +00002002 bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2003 if (!ProtocolsSorted || !BaseType.isCanonical()) {
2004 if (!ProtocolsSorted) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002005 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2006 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002007 unsigned UniqueCount = NumProtocols;
2008
John McCallfc93cf92009-10-22 22:37:11 +00002009 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
John McCall8b07ec22010-05-15 11:32:37 +00002010 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2011 &Sorted[0], UniqueCount);
John McCallfc93cf92009-10-22 22:37:11 +00002012 } else {
John McCall8b07ec22010-05-15 11:32:37 +00002013 Canonical = getObjCObjectType(getCanonicalType(BaseType),
2014 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002015 }
2016
2017 // Regenerate InsertPos.
John McCall8b07ec22010-05-15 11:32:37 +00002018 ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
2019 }
2020
2021 unsigned Size = sizeof(ObjCObjectTypeImpl);
2022 Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2023 void *Mem = Allocate(Size, TypeAlignment);
2024 ObjCObjectTypeImpl *T =
2025 new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
2026
2027 Types.push_back(T);
2028 ObjCObjectTypes.InsertNode(T, InsertPos);
2029 return QualType(T, 0);
2030}
2031
2032/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2033/// the given object type.
2034QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) {
2035 llvm::FoldingSetNodeID ID;
2036 ObjCObjectPointerType::Profile(ID, ObjectT);
2037
2038 void *InsertPos = 0;
2039 if (ObjCObjectPointerType *QT =
2040 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2041 return QualType(QT, 0);
2042
2043 // Find the canonical object type.
2044 QualType Canonical;
2045 if (!ObjectT.isCanonical()) {
2046 Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
2047
2048 // Regenerate InsertPos.
John McCallfc93cf92009-10-22 22:37:11 +00002049 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2050 }
2051
Douglas Gregorf85bee62010-02-08 22:59:26 +00002052 // No match.
John McCall8b07ec22010-05-15 11:32:37 +00002053 void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2054 ObjCObjectPointerType *QType =
2055 new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
Mike Stump11289f42009-09-09 15:08:12 +00002056
Steve Narofffb4330f2009-06-17 22:40:22 +00002057 Types.push_back(QType);
2058 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
John McCall8b07ec22010-05-15 11:32:37 +00002059 return QualType(QType, 0);
Steve Narofffb4330f2009-06-17 22:40:22 +00002060}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002061
Steve Naroffc277ad12009-07-18 15:33:26 +00002062/// getObjCInterfaceType - Return the unique reference to the type for the
2063/// specified ObjC interface decl. The list of protocols is optional.
John McCall8b07ec22010-05-15 11:32:37 +00002064QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
2065 if (Decl->TypeForDecl)
2066 return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002067
John McCall8b07ec22010-05-15 11:32:37 +00002068 // FIXME: redeclarations?
2069 void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2070 ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2071 Decl->TypeForDecl = T;
2072 Types.push_back(T);
2073 return QualType(T, 0);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002074}
2075
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002076/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2077/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002078/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002079/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002080/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002081QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002082 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002083 if (tofExpr->isTypeDependent()) {
2084 llvm::FoldingSetNodeID ID;
2085 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002086
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002087 void *InsertPos = 0;
2088 DependentTypeOfExprType *Canon
2089 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2090 if (Canon) {
2091 // We already have a "canonical" version of an identical, dependent
2092 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002093 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002094 QualType((TypeOfExprType*)Canon, 0));
2095 }
2096 else {
2097 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002098 Canon
2099 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002100 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2101 toe = Canon;
2102 }
2103 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002104 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002105 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002106 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002107 Types.push_back(toe);
2108 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002109}
2110
Steve Naroffa773cd52007-08-01 18:02:17 +00002111/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2112/// TypeOfType AST's. The only motivation to unique these nodes would be
2113/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002114/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002115/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002116QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002117 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002118 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002119 Types.push_back(tot);
2120 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002121}
2122
Anders Carlssonad6bd352009-06-24 21:24:56 +00002123/// getDecltypeForExpr - Given an expr, will return the decltype for that
2124/// expression, according to the rules in C++0x [dcl.type.simple]p4
2125static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002126 if (e->isTypeDependent())
2127 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002128
Anders Carlssonad6bd352009-06-24 21:24:56 +00002129 // If e is an id expression or a class member access, decltype(e) is defined
2130 // as the type of the entity named by e.
2131 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2132 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2133 return VD->getType();
2134 }
2135 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2136 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2137 return FD->getType();
2138 }
2139 // If e is a function call or an invocation of an overloaded operator,
2140 // (parentheses around e are ignored), decltype(e) is defined as the
2141 // return type of that function.
2142 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2143 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002144
Anders Carlssonad6bd352009-06-24 21:24:56 +00002145 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002146
2147 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002148 // defined as T&, otherwise decltype(e) is defined as T.
2149 if (e->isLvalue(Context) == Expr::LV_Valid)
2150 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002151
Anders Carlssonad6bd352009-06-24 21:24:56 +00002152 return T;
2153}
2154
Anders Carlsson81df7b82009-06-24 19:06:50 +00002155/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2156/// DecltypeType AST's. The only motivation to unique these nodes would be
2157/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002158/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002159/// on canonical type's (which are always unique).
2160QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002161 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002162 if (e->isTypeDependent()) {
2163 llvm::FoldingSetNodeID ID;
2164 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002165
Douglas Gregora21f6c32009-07-30 23:36:40 +00002166 void *InsertPos = 0;
2167 DependentDecltypeType *Canon
2168 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2169 if (Canon) {
2170 // We already have a "canonical" version of an equivalent, dependent
2171 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002172 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002173 QualType((DecltypeType*)Canon, 0));
2174 }
2175 else {
2176 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002177 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002178 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2179 dt = Canon;
2180 }
2181 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002182 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002183 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002184 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002185 Types.push_back(dt);
2186 return QualType(dt, 0);
2187}
2188
Chris Lattnerfb072462007-01-23 05:45:31 +00002189/// getTagDeclType - Return the unique reference to the type for the
2190/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002191QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002192 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002193 // FIXME: What is the design on getTagDeclType when it requires casting
2194 // away const? mutable?
2195 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002196}
2197
Mike Stump11289f42009-09-09 15:08:12 +00002198/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2199/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2200/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002201CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002202 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002203}
Chris Lattnerfb072462007-01-23 05:45:31 +00002204
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002205/// getSignedWCharType - Return the type of "signed wchar_t".
2206/// Used when in C++, as a GCC extension.
2207QualType ASTContext::getSignedWCharType() const {
2208 // FIXME: derive from "Target" ?
2209 return WCharTy;
2210}
2211
2212/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2213/// Used when in C++, as a GCC extension.
2214QualType ASTContext::getUnsignedWCharType() const {
2215 // FIXME: derive from "Target" ?
2216 return UnsignedIntTy;
2217}
2218
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002219/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2220/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2221QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002222 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002223}
2224
Chris Lattnera21ad802008-04-02 05:18:44 +00002225//===----------------------------------------------------------------------===//
2226// Type Operators
2227//===----------------------------------------------------------------------===//
2228
John McCallfc93cf92009-10-22 22:37:11 +00002229CanQualType ASTContext::getCanonicalParamType(QualType T) {
2230 // Push qualifiers into arrays, and then discard any remaining
2231 // qualifiers.
2232 T = getCanonicalType(T);
2233 const Type *Ty = T.getTypePtr();
2234
2235 QualType Result;
2236 if (isa<ArrayType>(Ty)) {
2237 Result = getArrayDecayedType(QualType(Ty,0));
2238 } else if (isa<FunctionType>(Ty)) {
2239 Result = getPointerType(QualType(Ty, 0));
2240 } else {
2241 Result = QualType(Ty, 0);
2242 }
2243
2244 return CanQualType::CreateUnsafe(Result);
2245}
2246
Chris Lattnered0d0792008-04-06 22:41:35 +00002247/// getCanonicalType - Return the canonical (structural) type corresponding to
2248/// the specified potentially non-canonical type. The non-canonical version
2249/// of a type may have many "decorated" versions of types. Decorators can
2250/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2251/// to be free of any of these, allowing two canonical types to be compared
2252/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002253CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002254 QualifierCollector Quals;
2255 const Type *Ptr = Quals.strip(T);
2256 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002257
John McCall8ccfcb52009-09-24 19:53:00 +00002258 // The canonical internal type will be the canonical type *except*
2259 // that we push type qualifiers down through array types.
2260
2261 // If there are no new qualifiers to push down, stop here.
2262 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002263 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002264
John McCall8ccfcb52009-09-24 19:53:00 +00002265 // If the type qualifiers are on an array type, get the canonical
2266 // type of the array with the qualifiers applied to the element
2267 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002268 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2269 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002270 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002271
Chris Lattner7adf0762008-08-04 07:31:14 +00002272 // Get the canonical version of the element with the extra qualifiers on it.
2273 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002274 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002275 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002276
Chris Lattner7adf0762008-08-04 07:31:14 +00002277 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002278 return CanQualType::CreateUnsafe(
2279 getConstantArrayType(NewEltTy, CAT->getSize(),
2280 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002281 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002282 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002283 return CanQualType::CreateUnsafe(
2284 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002285 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002286
Douglas Gregor4619e432008-12-05 23:32:09 +00002287 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002288 return CanQualType::CreateUnsafe(
2289 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002290 DSAT->getSizeExpr() ?
2291 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002292 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002293 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002294 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002295
Chris Lattner7adf0762008-08-04 07:31:14 +00002296 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002297 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002298 VAT->getSizeExpr() ?
2299 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002300 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002301 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002302 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002303}
2304
Chandler Carruth607f38e2009-12-29 07:16:59 +00002305QualType ASTContext::getUnqualifiedArrayType(QualType T,
2306 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002307 Quals = T.getQualifiers();
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002308 const ArrayType *AT = getAsArrayType(T);
2309 if (!AT) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002310 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002311 }
2312
Chandler Carruth607f38e2009-12-29 07:16:59 +00002313 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002314 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002315 if (Elt == UnqualElt)
2316 return T;
2317
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002318 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002319 return getConstantArrayType(UnqualElt, CAT->getSize(),
2320 CAT->getSizeModifier(), 0);
2321 }
2322
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002323 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
Chandler Carruth607f38e2009-12-29 07:16:59 +00002324 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2325 }
2326
Douglas Gregor3b05bdb2010-05-17 18:45:21 +00002327 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
2328 return getVariableArrayType(UnqualElt,
2329 VAT->getSizeExpr() ?
2330 VAT->getSizeExpr()->Retain() : 0,
2331 VAT->getSizeModifier(),
2332 VAT->getIndexTypeCVRQualifiers(),
2333 VAT->getBracketsRange());
2334 }
2335
2336 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002337 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2338 DSAT->getSizeModifier(), 0,
2339 SourceRange());
2340}
2341
Douglas Gregor1fc3d662010-06-09 03:53:18 +00002342/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
2343/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
2344/// they point to and return true. If T1 and T2 aren't pointer types
2345/// or pointer-to-member types, or if they are not similar at this
2346/// level, returns false and leaves T1 and T2 unchanged. Top-level
2347/// qualifiers on T1 and T2 are ignored. This function will typically
2348/// be called in a loop that successively "unwraps" pointer and
2349/// pointer-to-member types to compare them at each level.
2350bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
2351 const PointerType *T1PtrType = T1->getAs<PointerType>(),
2352 *T2PtrType = T2->getAs<PointerType>();
2353 if (T1PtrType && T2PtrType) {
2354 T1 = T1PtrType->getPointeeType();
2355 T2 = T2PtrType->getPointeeType();
2356 return true;
2357 }
2358
2359 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
2360 *T2MPType = T2->getAs<MemberPointerType>();
2361 if (T1MPType && T2MPType &&
2362 hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
2363 QualType(T2MPType->getClass(), 0))) {
2364 T1 = T1MPType->getPointeeType();
2365 T2 = T2MPType->getPointeeType();
2366 return true;
2367 }
2368
2369 if (getLangOptions().ObjC1) {
2370 const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
2371 *T2OPType = T2->getAs<ObjCObjectPointerType>();
2372 if (T1OPType && T2OPType) {
2373 T1 = T1OPType->getPointeeType();
2374 T2 = T2OPType->getPointeeType();
2375 return true;
2376 }
2377 }
2378
2379 // FIXME: Block pointers, too?
2380
2381 return false;
2382}
2383
John McCall847e2a12009-11-24 18:42:40 +00002384DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2385 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2386 return TD->getDeclName();
2387
2388 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2389 if (DTN->isIdentifier()) {
2390 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2391 } else {
2392 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2393 }
2394 }
2395
John McCalld28ae272009-12-02 08:04:21 +00002396 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2397 assert(Storage);
2398 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002399}
2400
Douglas Gregor6bc50582009-05-07 06:41:52 +00002401TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2402 // If this template name refers to a template, the canonical
2403 // template name merely stores the template itself.
2404 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002405 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002406
John McCalld28ae272009-12-02 08:04:21 +00002407 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002408
Douglas Gregor6bc50582009-05-07 06:41:52 +00002409 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2410 assert(DTN && "Non-dependent template names must refer to template decls.");
2411 return DTN->CanonicalTemplateName;
2412}
2413
Douglas Gregoradee3e32009-11-11 23:06:43 +00002414bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2415 X = getCanonicalTemplateName(X);
2416 Y = getCanonicalTemplateName(Y);
2417 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2418}
2419
Mike Stump11289f42009-09-09 15:08:12 +00002420TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002421ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2422 switch (Arg.getKind()) {
2423 case TemplateArgument::Null:
2424 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002425
Douglas Gregora8e02e72009-07-28 23:00:59 +00002426 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002427 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002428
Douglas Gregora8e02e72009-07-28 23:00:59 +00002429 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002430 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002431
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002432 case TemplateArgument::Template:
2433 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2434
Douglas Gregora8e02e72009-07-28 23:00:59 +00002435 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002436 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002437 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002438
Douglas Gregora8e02e72009-07-28 23:00:59 +00002439 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002440 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002441
Douglas Gregora8e02e72009-07-28 23:00:59 +00002442 case TemplateArgument::Pack: {
2443 // FIXME: Allocate in ASTContext
2444 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2445 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002446 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002447 AEnd = Arg.pack_end();
2448 A != AEnd; (void)++A, ++Idx)
2449 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002450
Douglas Gregora8e02e72009-07-28 23:00:59 +00002451 TemplateArgument Result;
2452 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2453 return Result;
2454 }
2455 }
2456
2457 // Silence GCC warning
2458 assert(false && "Unhandled template argument kind");
2459 return TemplateArgument();
2460}
2461
Douglas Gregor333489b2009-03-27 23:10:48 +00002462NestedNameSpecifier *
2463ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002464 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002465 return 0;
2466
2467 switch (NNS->getKind()) {
2468 case NestedNameSpecifier::Identifier:
2469 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002470 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002471 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2472 NNS->getAsIdentifier());
2473
2474 case NestedNameSpecifier::Namespace:
2475 // A namespace is canonical; build a nested-name-specifier with
2476 // this namespace and no prefix.
2477 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2478
2479 case NestedNameSpecifier::TypeSpec:
2480 case NestedNameSpecifier::TypeSpecWithTemplate: {
2481 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002482 return NestedNameSpecifier::Create(*this, 0,
2483 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002484 T.getTypePtr());
2485 }
2486
2487 case NestedNameSpecifier::Global:
2488 // The global specifier is canonical and unique.
2489 return NNS;
2490 }
2491
2492 // Required to silence a GCC warning
2493 return 0;
2494}
2495
Chris Lattner7adf0762008-08-04 07:31:14 +00002496
2497const ArrayType *ASTContext::getAsArrayType(QualType T) {
2498 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002499 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002500 // Handle the common positive case fast.
2501 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2502 return AT;
2503 }
Mike Stump11289f42009-09-09 15:08:12 +00002504
John McCall8ccfcb52009-09-24 19:53:00 +00002505 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002506 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002507 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002508 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002509
John McCall8ccfcb52009-09-24 19:53:00 +00002510 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002511 // implements C99 6.7.3p8: "If the specification of an array type includes
2512 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002513
Chris Lattner7adf0762008-08-04 07:31:14 +00002514 // If we get here, we either have type qualifiers on the type, or we have
2515 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002516 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002517
John McCall8ccfcb52009-09-24 19:53:00 +00002518 QualifierCollector Qs;
2519 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002520
Chris Lattner7adf0762008-08-04 07:31:14 +00002521 // If we have a simple case, just return now.
2522 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002523 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002524 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002525
Chris Lattner7adf0762008-08-04 07:31:14 +00002526 // Otherwise, we have an array and we have qualifiers on it. Push the
2527 // qualifiers into the array element type and return a new array type.
2528 // Get the canonical version of the element with the extra qualifiers on it.
2529 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002530 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002531
Chris Lattner7adf0762008-08-04 07:31:14 +00002532 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2533 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2534 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002535 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002536 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2537 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2538 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002539 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002540
Mike Stump11289f42009-09-09 15:08:12 +00002541 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002542 = dyn_cast<DependentSizedArrayType>(ATy))
2543 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002544 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002545 DSAT->getSizeExpr() ?
2546 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002547 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002548 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002549 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002550
Chris Lattner7adf0762008-08-04 07:31:14 +00002551 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002552 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002553 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002554 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002555 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002556 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002557 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002558}
2559
2560
Chris Lattnera21ad802008-04-02 05:18:44 +00002561/// getArrayDecayedType - Return the properly qualified result of decaying the
2562/// specified array type to a pointer. This operation is non-trivial when
2563/// handling typedefs etc. The canonical type of "T" must be an array type,
2564/// this returns a pointer to a properly qualified element of the array.
2565///
2566/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2567QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002568 // Get the element type with 'getAsArrayType' so that we don't lose any
2569 // typedefs in the element type of the array. This also handles propagation
2570 // of type qualifiers from the array type into the element type if present
2571 // (C99 6.7.3p8).
2572 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2573 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002574
Chris Lattner7adf0762008-08-04 07:31:14 +00002575 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002576
2577 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002578 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002579}
2580
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002581QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002582 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002583 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2584 QT = AT->getElementType();
2585 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002586}
2587
Anders Carlsson4bf82142009-09-25 01:23:32 +00002588QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2589 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002590
Anders Carlsson4bf82142009-09-25 01:23:32 +00002591 if (const ArrayType *AT = getAsArrayType(ElemTy))
2592 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002593
Anders Carlssone0808df2008-12-21 03:44:36 +00002594 return ElemTy;
2595}
2596
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002597/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002598uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002599ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2600 uint64_t ElementCount = 1;
2601 do {
2602 ElementCount *= CA->getSize().getZExtValue();
2603 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2604 } while (CA);
2605 return ElementCount;
2606}
2607
Steve Naroff0af91202007-04-27 21:51:21 +00002608/// getFloatingRank - Return a relative rank for floating point types.
2609/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002610static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002611 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002612 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002613
John McCall9dd450b2009-09-21 23:43:11 +00002614 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2615 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002616 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002617 case BuiltinType::Float: return FloatRank;
2618 case BuiltinType::Double: return DoubleRank;
2619 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002620 }
2621}
2622
Mike Stump11289f42009-09-09 15:08:12 +00002623/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2624/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002625/// 'typeDomain' is a real floating point or complex type.
2626/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002627QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2628 QualType Domain) const {
2629 FloatingRank EltRank = getFloatingRank(Size);
2630 if (Domain->isComplexType()) {
2631 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002632 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002633 case FloatRank: return FloatComplexTy;
2634 case DoubleRank: return DoubleComplexTy;
2635 case LongDoubleRank: return LongDoubleComplexTy;
2636 }
Steve Naroff0af91202007-04-27 21:51:21 +00002637 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002638
2639 assert(Domain->isRealFloatingType() && "Unknown domain!");
2640 switch (EltRank) {
2641 default: assert(0 && "getFloatingRank(): illegal value for rank");
2642 case FloatRank: return FloatTy;
2643 case DoubleRank: return DoubleTy;
2644 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002645 }
Steve Naroffe4718892007-04-27 18:30:00 +00002646}
2647
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002648/// getFloatingTypeOrder - Compare the rank of the two specified floating
2649/// point types, ignoring the domain of the type (i.e. 'double' ==
2650/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002651/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002652int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2653 FloatingRank LHSR = getFloatingRank(LHS);
2654 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002655
Chris Lattnerb90739d2008-04-06 23:38:49 +00002656 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002657 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002658 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002659 return 1;
2660 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002661}
2662
Chris Lattner76a00cf2008-04-06 22:59:24 +00002663/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2664/// routine will assert if passed a built-in type that isn't an integer or enum,
2665/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002666unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002667 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002668 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002669 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002670
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002671 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2672 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2673
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002674 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2675 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2676
2677 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2678 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2679
Chris Lattner76a00cf2008-04-06 22:59:24 +00002680 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002681 default: assert(0 && "getIntegerRank(): not a built-in integer");
2682 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002683 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002684 case BuiltinType::Char_S:
2685 case BuiltinType::Char_U:
2686 case BuiltinType::SChar:
2687 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002688 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002689 case BuiltinType::Short:
2690 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002691 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002692 case BuiltinType::Int:
2693 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002694 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002695 case BuiltinType::Long:
2696 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002697 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002698 case BuiltinType::LongLong:
2699 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002700 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002701 case BuiltinType::Int128:
2702 case BuiltinType::UInt128:
2703 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002704 }
2705}
2706
Eli Friedman629ffb92009-08-20 04:21:42 +00002707/// \brief Whether this is a promotable bitfield reference according
2708/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2709///
2710/// \returns the type this bit-field will promote to, or NULL if no
2711/// promotion occurs.
2712QualType ASTContext::isPromotableBitField(Expr *E) {
Douglas Gregore05d3cb2010-05-24 20:13:53 +00002713 if (E->isTypeDependent() || E->isValueDependent())
2714 return QualType();
2715
Eli Friedman629ffb92009-08-20 04:21:42 +00002716 FieldDecl *Field = E->getBitField();
2717 if (!Field)
2718 return QualType();
2719
2720 QualType FT = Field->getType();
2721
2722 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2723 uint64_t BitWidth = BitWidthAP.getZExtValue();
2724 uint64_t IntSize = getTypeSize(IntTy);
2725 // GCC extension compatibility: if the bit-field size is less than or equal
2726 // to the size of int, it gets promoted no matter what its type is.
2727 // For instance, unsigned long bf : 4 gets promoted to signed int.
2728 if (BitWidth < IntSize)
2729 return IntTy;
2730
2731 if (BitWidth == IntSize)
2732 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2733
2734 // Types bigger than int are not subject to promotions, and therefore act
2735 // like the base type.
2736 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2737 // is ridiculous.
2738 return QualType();
2739}
2740
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002741/// getPromotedIntegerType - Returns the type that Promotable will
2742/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2743/// integer type.
2744QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2745 assert(!Promotable.isNull());
2746 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002747 if (const EnumType *ET = Promotable->getAs<EnumType>())
2748 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002749 if (Promotable->isSignedIntegerType())
2750 return IntTy;
2751 uint64_t PromotableSize = getTypeSize(Promotable);
2752 uint64_t IntSize = getTypeSize(IntTy);
2753 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2754 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2755}
2756
Mike Stump11289f42009-09-09 15:08:12 +00002757/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002758/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002759/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002760int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002761 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2762 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002763 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002764
Chris Lattner76a00cf2008-04-06 22:59:24 +00002765 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2766 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002767
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002768 unsigned LHSRank = getIntegerRank(LHSC);
2769 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002770
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002771 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2772 if (LHSRank == RHSRank) return 0;
2773 return LHSRank > RHSRank ? 1 : -1;
2774 }
Mike Stump11289f42009-09-09 15:08:12 +00002775
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002776 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2777 if (LHSUnsigned) {
2778 // If the unsigned [LHS] type is larger, return it.
2779 if (LHSRank >= RHSRank)
2780 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002781
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002782 // If the signed type can represent all values of the unsigned type, it
2783 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002784 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002785 return -1;
2786 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002787
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002788 // If the unsigned [RHS] type is larger, return it.
2789 if (RHSRank >= LHSRank)
2790 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002791
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002792 // If the signed type can represent all values of the unsigned type, it
2793 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002794 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002795 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002796}
Anders Carlsson98f07902007-08-17 05:31:46 +00002797
Anders Carlsson6d417272009-11-14 21:45:58 +00002798static RecordDecl *
2799CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2800 SourceLocation L, IdentifierInfo *Id) {
2801 if (Ctx.getLangOptions().CPlusPlus)
2802 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2803 else
2804 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2805}
2806
Mike Stump11289f42009-09-09 15:08:12 +00002807// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002808QualType ASTContext::getCFConstantStringType() {
2809 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002810 CFConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002811 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002812 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002813 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002814
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002815 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002816
Anders Carlsson98f07902007-08-17 05:31:46 +00002817 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002818 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002819 // int flags;
2820 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002821 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002822 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002823 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002824 FieldTypes[3] = LongTy;
2825
Anders Carlsson98f07902007-08-17 05:31:46 +00002826 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002827 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002828 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002829 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002830 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002831 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002832 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002833 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002834 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002835 }
2836
Douglas Gregord5058122010-02-11 01:19:42 +00002837 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002838 }
Mike Stump11289f42009-09-09 15:08:12 +00002839
Anders Carlsson98f07902007-08-17 05:31:46 +00002840 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002841}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002842
Douglas Gregor512b0772009-04-23 22:29:11 +00002843void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002844 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002845 assert(Rec && "Invalid CFConstantStringType");
2846 CFConstantStringTypeDecl = Rec->getDecl();
2847}
2848
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002849// getNSConstantStringType - Return the type used for constant NSStrings.
2850QualType ASTContext::getNSConstantStringType() {
2851 if (!NSConstantStringTypeDecl) {
2852 NSConstantStringTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002853 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002854 &Idents.get("__builtin_NSString"));
2855 NSConstantStringTypeDecl->startDefinition();
2856
2857 QualType FieldTypes[3];
2858
2859 // const int *isa;
2860 FieldTypes[0] = getPointerType(IntTy.withConst());
2861 // const char *str;
2862 FieldTypes[1] = getPointerType(CharTy.withConst());
2863 // unsigned int length;
2864 FieldTypes[2] = UnsignedIntTy;
2865
2866 // Create fields
2867 for (unsigned i = 0; i < 3; ++i) {
2868 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2869 SourceLocation(), 0,
2870 FieldTypes[i], /*TInfo=*/0,
2871 /*BitWidth=*/0,
2872 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002873 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002874 NSConstantStringTypeDecl->addDecl(Field);
2875 }
2876
2877 NSConstantStringTypeDecl->completeDefinition();
2878 }
2879
2880 return getTagDeclType(NSConstantStringTypeDecl);
2881}
2882
2883void ASTContext::setNSConstantStringType(QualType T) {
2884 const RecordType *Rec = T->getAs<RecordType>();
2885 assert(Rec && "Invalid NSConstantStringType");
2886 NSConstantStringTypeDecl = Rec->getDecl();
2887}
2888
Mike Stump11289f42009-09-09 15:08:12 +00002889QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002890 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002891 ObjCFastEnumerationStateTypeDecl =
Abramo Bagnara6150c882010-05-11 21:36:43 +00002892 CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002893 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00002894 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002895
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002896 QualType FieldTypes[] = {
2897 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002898 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002899 getPointerType(UnsignedLongTy),
2900 getConstantArrayType(UnsignedLongTy,
2901 llvm::APInt(32, 5), ArrayType::Normal, 0)
2902 };
Mike Stump11289f42009-09-09 15:08:12 +00002903
Douglas Gregor91f84212008-12-11 16:49:14 +00002904 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002905 FieldDecl *Field = FieldDecl::Create(*this,
2906 ObjCFastEnumerationStateTypeDecl,
2907 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002908 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002909 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002910 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002911 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002912 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002913 }
Fariborz Jahanian9ea58392010-05-27 16:05:06 +00002914 if (getLangOptions().CPlusPlus)
Fariborz Jahanianc77f0f32010-05-27 16:35:00 +00002915 if (CXXRecordDecl *CXXRD =
2916 dyn_cast<CXXRecordDecl>(ObjCFastEnumerationStateTypeDecl))
Fariborz Jahanian9ea58392010-05-27 16:05:06 +00002917 CXXRD->setEmpty(false);
Mike Stump11289f42009-09-09 15:08:12 +00002918
Douglas Gregord5058122010-02-11 01:19:42 +00002919 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002920 }
Mike Stump11289f42009-09-09 15:08:12 +00002921
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002922 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2923}
2924
Mike Stumpd0153282009-10-20 02:12:22 +00002925QualType ASTContext::getBlockDescriptorType() {
2926 if (BlockDescriptorType)
2927 return getTagDeclType(BlockDescriptorType);
2928
2929 RecordDecl *T;
2930 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00002931 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002932 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00002933 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002934
2935 QualType FieldTypes[] = {
2936 UnsignedLongTy,
2937 UnsignedLongTy,
2938 };
2939
2940 const char *FieldNames[] = {
2941 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002942 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002943 };
2944
2945 for (size_t i = 0; i < 2; ++i) {
2946 FieldDecl *Field = FieldDecl::Create(*this,
2947 T,
2948 SourceLocation(),
2949 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00002950 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00002951 /*BitWidth=*/0,
2952 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002953 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00002954 T->addDecl(Field);
2955 }
2956
Douglas Gregord5058122010-02-11 01:19:42 +00002957 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002958
2959 BlockDescriptorType = T;
2960
2961 return getTagDeclType(BlockDescriptorType);
2962}
2963
2964void ASTContext::setBlockDescriptorType(QualType T) {
2965 const RecordType *Rec = T->getAs<RecordType>();
2966 assert(Rec && "Invalid BlockDescriptorType");
2967 BlockDescriptorType = Rec->getDecl();
2968}
2969
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002970QualType ASTContext::getBlockDescriptorExtendedType() {
2971 if (BlockDescriptorExtendedType)
2972 return getTagDeclType(BlockDescriptorExtendedType);
2973
2974 RecordDecl *T;
2975 // FIXME: Needs the FlagAppleBlock bit.
Abramo Bagnara6150c882010-05-11 21:36:43 +00002976 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00002977 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00002978 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002979
2980 QualType FieldTypes[] = {
2981 UnsignedLongTy,
2982 UnsignedLongTy,
2983 getPointerType(VoidPtrTy),
2984 getPointerType(VoidPtrTy)
2985 };
2986
2987 const char *FieldNames[] = {
2988 "reserved",
2989 "Size",
2990 "CopyFuncPtr",
2991 "DestroyFuncPtr"
2992 };
2993
2994 for (size_t i = 0; i < 4; ++i) {
2995 FieldDecl *Field = FieldDecl::Create(*this,
2996 T,
2997 SourceLocation(),
2998 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00002999 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003000 /*BitWidth=*/0,
3001 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003002 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003003 T->addDecl(Field);
3004 }
3005
Douglas Gregord5058122010-02-11 01:19:42 +00003006 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003007
3008 BlockDescriptorExtendedType = T;
3009
3010 return getTagDeclType(BlockDescriptorExtendedType);
3011}
3012
3013void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3014 const RecordType *Rec = T->getAs<RecordType>();
3015 assert(Rec && "Invalid BlockDescriptorType");
3016 BlockDescriptorExtendedType = Rec->getDecl();
3017}
3018
Mike Stump94967902009-10-21 18:16:27 +00003019bool ASTContext::BlockRequiresCopying(QualType Ty) {
3020 if (Ty->isBlockPointerType())
3021 return true;
3022 if (isObjCNSObjectType(Ty))
3023 return true;
3024 if (Ty->isObjCObjectPointerType())
3025 return true;
3026 return false;
3027}
3028
3029QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3030 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003031 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003032 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003033 // unsigned int __flags;
3034 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003035 // void *__copy_helper; // as needed
3036 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003037 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003038 // } *
3039
Mike Stump94967902009-10-21 18:16:27 +00003040 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3041
3042 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003043 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003044 llvm::SmallString<36> Name;
3045 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3046 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003047 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003048 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003049 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003050 T->startDefinition();
3051 QualType Int32Ty = IntTy;
3052 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3053 QualType FieldTypes[] = {
3054 getPointerType(VoidPtrTy),
3055 getPointerType(getTagDeclType(T)),
3056 Int32Ty,
3057 Int32Ty,
3058 getPointerType(VoidPtrTy),
3059 getPointerType(VoidPtrTy),
3060 Ty
3061 };
3062
3063 const char *FieldNames[] = {
3064 "__isa",
3065 "__forwarding",
3066 "__flags",
3067 "__size",
3068 "__copy_helper",
3069 "__destroy_helper",
3070 DeclName,
3071 };
3072
3073 for (size_t i = 0; i < 7; ++i) {
3074 if (!HasCopyAndDispose && i >=4 && i <= 5)
3075 continue;
3076 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3077 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003078 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003079 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003080 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003081 T->addDecl(Field);
3082 }
3083
Douglas Gregord5058122010-02-11 01:19:42 +00003084 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003085
3086 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003087}
3088
3089
3090QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003091 bool BlockHasCopyDispose,
John McCall87fe5d52010-05-20 01:18:31 +00003092 llvm::SmallVectorImpl<const Expr *> &Layout) {
3093
Mike Stumpd0153282009-10-20 02:12:22 +00003094 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003095 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003096 llvm::SmallString<36> Name;
3097 llvm::raw_svector_ostream(Name) << "__block_literal_"
3098 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003099 RecordDecl *T;
Abramo Bagnara6150c882010-05-11 21:36:43 +00003100 T = CreateRecordDecl(*this, TTK_Struct, TUDecl, SourceLocation(),
Anders Carlsson6d417272009-11-14 21:45:58 +00003101 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003102 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003103 QualType FieldTypes[] = {
3104 getPointerType(VoidPtrTy),
3105 IntTy,
3106 IntTy,
3107 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003108 (BlockHasCopyDispose ?
3109 getPointerType(getBlockDescriptorExtendedType()) :
3110 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003111 };
3112
3113 const char *FieldNames[] = {
3114 "__isa",
3115 "__flags",
3116 "__reserved",
3117 "__FuncPtr",
3118 "__descriptor"
3119 };
3120
3121 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003122 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003123 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003124 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003125 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003126 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003127 T->addDecl(Field);
3128 }
3129
John McCall87fe5d52010-05-20 01:18:31 +00003130 for (unsigned i = 0; i < Layout.size(); ++i) {
3131 const Expr *E = Layout[i];
Mike Stump7fe9cc12009-10-21 03:49:08 +00003132
John McCall87fe5d52010-05-20 01:18:31 +00003133 QualType FieldType = E->getType();
3134 IdentifierInfo *FieldName = 0;
3135 if (isa<CXXThisExpr>(E)) {
3136 FieldName = &Idents.get("this");
3137 } else if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E)) {
3138 const ValueDecl *D = BDRE->getDecl();
3139 FieldName = D->getIdentifier();
3140 if (BDRE->isByRef())
3141 FieldType = BuildByRefType(D->getNameAsCString(), FieldType);
3142 } else {
3143 // Padding.
3144 assert(isa<ConstantArrayType>(FieldType) &&
3145 isa<DeclRefExpr>(E) &&
3146 !cast<DeclRefExpr>(E)->getDecl()->getDeclName() &&
3147 "doesn't match characteristics of padding decl");
3148 }
Mike Stump7fe9cc12009-10-21 03:49:08 +00003149
3150 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCall87fe5d52010-05-20 01:18:31 +00003151 FieldName, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003152 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003153 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003154 T->addDecl(Field);
3155 }
3156
Douglas Gregord5058122010-02-11 01:19:42 +00003157 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003158
3159 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003160}
3161
Douglas Gregor512b0772009-04-23 22:29:11 +00003162void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003163 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003164 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3165 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3166}
3167
Anders Carlsson18acd442007-10-29 06:33:42 +00003168// This returns true if a type has been typedefed to BOOL:
3169// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003170static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003171 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003172 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3173 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003174
Anders Carlssond8499822007-10-29 05:01:08 +00003175 return false;
3176}
3177
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003178/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003179/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003180CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003181 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003182
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003183 // Make all integer and enum types at least as large as an int
Ken Dyck40775002010-01-11 17:06:35 +00003184 if (sz.isPositive() && type->isIntegralType())
3185 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003186 // Treat arrays as pointers, since that's how they're passed in.
3187 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003188 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003189 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003190}
3191
3192static inline
3193std::string charUnitsToString(const CharUnits &CU) {
3194 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003195}
3196
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003197/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003198/// declaration.
3199void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3200 std::string& S) {
3201 const BlockDecl *Decl = Expr->getBlockDecl();
3202 QualType BlockTy =
3203 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3204 // Encode result type.
John McCall8e346702010-06-04 19:02:56 +00003205 getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
David Chisnall950a9512009-11-17 19:33:30 +00003206 // Compute size of all parameters.
3207 // Start with computing size of a pointer in number of bytes.
3208 // FIXME: There might(should) be a better way of doing this computation!
3209 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003210 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3211 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003212 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003213 E = Decl->param_end(); PI != E; ++PI) {
3214 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003215 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003216 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003217 ParmOffset += sz;
3218 }
3219 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003220 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003221 // Block pointer and offset.
3222 S += "@?0";
3223 ParmOffset = PtrSize;
3224
3225 // Argument types.
3226 ParmOffset = PtrSize;
3227 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3228 Decl->param_end(); PI != E; ++PI) {
3229 ParmVarDecl *PVDecl = *PI;
3230 QualType PType = PVDecl->getOriginalType();
3231 if (const ArrayType *AT =
3232 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3233 // Use array's original type only if it has known number of
3234 // elements.
3235 if (!isa<ConstantArrayType>(AT))
3236 PType = PVDecl->getType();
3237 } else if (PType->isFunctionType())
3238 PType = PVDecl->getType();
3239 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003240 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003241 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003242 }
3243}
3244
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003245/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003246/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003247void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003248 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003249 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003250 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003251 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003252 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003253 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003254 // Compute size of all parameters.
3255 // Start with computing size of a pointer in number of bytes.
3256 // FIXME: There might(should) be a better way of doing this computation!
3257 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003258 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003259 // The first two arguments (self and _cmd) are pointers; account for
3260 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003261 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003262 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003263 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003264 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003265 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003266 assert (sz.isPositive() &&
3267 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003268 ParmOffset += sz;
3269 }
Ken Dyck40775002010-01-11 17:06:35 +00003270 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003271 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003272 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003273
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003274 // Argument types.
3275 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003276 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003277 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003278 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003279 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003280 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003281 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3282 // Use array's original type only if it has known number of
3283 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003284 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003285 PType = PVDecl->getType();
3286 } else if (PType->isFunctionType())
3287 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003288 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003289 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003290 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003291 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003292 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003293 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003294 }
3295}
3296
Daniel Dunbar4932b362008-08-28 04:38:10 +00003297/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003298/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003299/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3300/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003301/// Property attributes are stored as a comma-delimited C string. The simple
3302/// attributes readonly and bycopy are encoded as single characters. The
3303/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3304/// encoded as single characters, followed by an identifier. Property types
3305/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003306/// these attributes are defined by the following enumeration:
3307/// @code
3308/// enum PropertyAttributes {
3309/// kPropertyReadOnly = 'R', // property is read-only.
3310/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3311/// kPropertyByref = '&', // property is a reference to the value last assigned
3312/// kPropertyDynamic = 'D', // property is dynamic
3313/// kPropertyGetter = 'G', // followed by getter selector name
3314/// kPropertySetter = 'S', // followed by setter selector name
3315/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3316/// kPropertyType = 't' // followed by old-style type encoding.
3317/// kPropertyWeak = 'W' // 'weak' property
3318/// kPropertyStrong = 'P' // property GC'able
3319/// kPropertyNonAtomic = 'N' // property non-atomic
3320/// };
3321/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003322void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003323 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003324 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003325 // Collect information from the property implementation decl(s).
3326 bool Dynamic = false;
3327 ObjCPropertyImplDecl *SynthesizePID = 0;
3328
3329 // FIXME: Duplicated code due to poor abstraction.
3330 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003331 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003332 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3333 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003334 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003335 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003336 ObjCPropertyImplDecl *PID = *i;
3337 if (PID->getPropertyDecl() == PD) {
3338 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3339 Dynamic = true;
3340 } else {
3341 SynthesizePID = PID;
3342 }
3343 }
3344 }
3345 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003346 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003347 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003348 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003349 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003350 ObjCPropertyImplDecl *PID = *i;
3351 if (PID->getPropertyDecl() == PD) {
3352 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3353 Dynamic = true;
3354 } else {
3355 SynthesizePID = PID;
3356 }
3357 }
Mike Stump11289f42009-09-09 15:08:12 +00003358 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003359 }
3360 }
3361
3362 // FIXME: This is not very efficient.
3363 S = "T";
3364
3365 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003366 // GCC has some special rules regarding encoding of properties which
3367 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003368 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003369 true /* outermost type */,
3370 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003371
3372 if (PD->isReadOnly()) {
3373 S += ",R";
3374 } else {
3375 switch (PD->getSetterKind()) {
3376 case ObjCPropertyDecl::Assign: break;
3377 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003378 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003379 }
3380 }
3381
3382 // It really isn't clear at all what this means, since properties
3383 // are "dynamic by default".
3384 if (Dynamic)
3385 S += ",D";
3386
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003387 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3388 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003389
Daniel Dunbar4932b362008-08-28 04:38:10 +00003390 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3391 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003392 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003393 }
3394
3395 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3396 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003397 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003398 }
3399
3400 if (SynthesizePID) {
3401 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3402 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003403 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003404 }
3405
3406 // FIXME: OBJCGC: weak & strong
3407}
3408
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003409/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003410/// Another legacy compatibility encoding: 32-bit longs are encoded as
3411/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003412/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3413///
3414void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003415 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003416 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003417 if (BT->getKind() == BuiltinType::ULong &&
3418 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003419 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003420 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003421 if (BT->getKind() == BuiltinType::Long &&
3422 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003423 PointeeTy = IntTy;
3424 }
3425 }
3426}
3427
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003428void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003429 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003430 // We follow the behavior of gcc, expanding structures which are
3431 // directly pointed to, and expanding embedded structures. Note that
3432 // these rules are sufficient to prevent recursive encoding of the
3433 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003434 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003435 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003436}
3437
David Chisnallb190a2c2010-06-04 01:10:52 +00003438static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
3439 switch (T->getAs<BuiltinType>()->getKind()) {
3440 default: assert(0 && "Unhandled builtin type kind");
3441 case BuiltinType::Void: return 'v';
3442 case BuiltinType::Bool: return 'B';
3443 case BuiltinType::Char_U:
3444 case BuiltinType::UChar: return 'C';
3445 case BuiltinType::UShort: return 'S';
3446 case BuiltinType::UInt: return 'I';
3447 case BuiltinType::ULong:
3448 return
3449 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'L' : 'Q';
3450 case BuiltinType::UInt128: return 'T';
3451 case BuiltinType::ULongLong: return 'Q';
3452 case BuiltinType::Char_S:
3453 case BuiltinType::SChar: return 'c';
3454 case BuiltinType::Short: return 's';
3455 case BuiltinType::Int: return 'i';
3456 case BuiltinType::Long:
3457 return
3458 (const_cast<ASTContext *>(C))->getIntWidth(T) == 32 ? 'l' : 'q';
3459 case BuiltinType::LongLong: return 'q';
3460 case BuiltinType::Int128: return 't';
3461 case BuiltinType::Float: return 'f';
3462 case BuiltinType::Double: return 'd';
3463 case BuiltinType::LongDouble: return 'd';
3464 }
3465}
3466
Mike Stump11289f42009-09-09 15:08:12 +00003467static void EncodeBitField(const ASTContext *Context, std::string& S,
David Chisnallb190a2c2010-06-04 01:10:52 +00003468 QualType T, const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003469 const Expr *E = FD->getBitWidth();
3470 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3471 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003472 S += 'b';
David Chisnallb190a2c2010-06-04 01:10:52 +00003473 // The NeXT runtime encodes bit fields as b followed by the number of bits.
3474 // The GNU runtime requires more information; bitfields are encoded as b,
3475 // then the offset (in bits) of the first element, then the type of the
3476 // bitfield, then the size in bits. For example, in this structure:
3477 //
3478 // struct
3479 // {
3480 // int integer;
3481 // int flags:2;
3482 // };
3483 // On a 32-bit system, the encoding for flags would be b2 for the NeXT
3484 // runtime, but b32i2 for the GNU runtime. The reason for this extra
3485 // information is not especially sensible, but we're stuck with it for
3486 // compatibility with GCC, although providing it breaks anything that
3487 // actually uses runtime introspection and wants to work on both runtimes...
3488 if (!Ctx->getLangOptions().NeXTRuntime) {
3489 const RecordDecl *RD = FD->getParent();
3490 const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
3491 // FIXME: This same linear search is also used in ExprConstant - it might
3492 // be better if the FieldDecl stored its offset. We'd be increasing the
3493 // size of the object slightly, but saving some time every time it is used.
3494 unsigned i = 0;
3495 for (RecordDecl::field_iterator Field = RD->field_begin(),
3496 FieldEnd = RD->field_end();
3497 Field != FieldEnd; (void)++Field, ++i) {
3498 if (*Field == FD)
3499 break;
3500 }
3501 S += llvm::utostr(RL.getFieldOffset(i));
3502 S += ObjCEncodingForPrimitiveKind(Context, T);
3503 }
3504 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003505 S += llvm::utostr(N);
3506}
3507
Daniel Dunbar07d07852009-10-18 21:17:35 +00003508// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003509void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3510 bool ExpandPointedToStructures,
3511 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003512 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003513 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003514 bool EncodingProperty) {
David Chisnallb190a2c2010-06-04 01:10:52 +00003515 if (T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003516 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003517 return EncodeBitField(this, S, T, FD);
3518 S += ObjCEncodingForPrimitiveKind(this, T);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003519 return;
3520 }
Mike Stump11289f42009-09-09 15:08:12 +00003521
John McCall9dd450b2009-09-21 23:43:11 +00003522 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003523 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003524 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003525 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003526 return;
3527 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003528
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003529 // encoding for pointer or r3eference types.
3530 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003531 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003532 if (PT->isObjCSelType()) {
3533 S += ':';
3534 return;
3535 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003536 PointeeTy = PT->getPointeeType();
3537 }
3538 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3539 PointeeTy = RT->getPointeeType();
3540 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003541 bool isReadOnly = false;
3542 // For historical/compatibility reasons, the read-only qualifier of the
3543 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3544 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003545 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003546 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003547 if (OutermostType && T.isConstQualified()) {
3548 isReadOnly = true;
3549 S += 'r';
3550 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003551 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003552 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003553 while (P->getAs<PointerType>())
3554 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003555 if (P.isConstQualified()) {
3556 isReadOnly = true;
3557 S += 'r';
3558 }
3559 }
3560 if (isReadOnly) {
3561 // Another legacy compatibility encoding. Some ObjC qualifier and type
3562 // combinations need to be rearranged.
3563 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003564 if (llvm::StringRef(S).endswith("nr"))
3565 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003566 }
Mike Stump11289f42009-09-09 15:08:12 +00003567
Anders Carlssond8499822007-10-29 05:01:08 +00003568 if (PointeeTy->isCharType()) {
3569 // char pointer types should be encoded as '*' unless it is a
3570 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003571 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003572 S += '*';
3573 return;
3574 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003575 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003576 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3577 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3578 S += '#';
3579 return;
3580 }
3581 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3582 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3583 S += '@';
3584 return;
3585 }
3586 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003587 }
Anders Carlssond8499822007-10-29 05:01:08 +00003588 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003589 getLegacyIntegralTypeEncoding(PointeeTy);
3590
Mike Stump11289f42009-09-09 15:08:12 +00003591 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003592 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003593 return;
3594 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003595
Chris Lattnere7cabb92009-07-13 00:10:46 +00003596 if (const ArrayType *AT =
3597 // Ignore type qualifiers etc.
3598 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003599 if (isa<IncompleteArrayType>(AT)) {
3600 // Incomplete arrays are encoded as a pointer to the array element.
3601 S += '^';
3602
Mike Stump11289f42009-09-09 15:08:12 +00003603 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003604 false, ExpandStructures, FD);
3605 } else {
3606 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003607
Anders Carlssond05f44b2009-02-22 01:38:57 +00003608 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3609 S += llvm::utostr(CAT->getSize().getZExtValue());
3610 else {
3611 //Variable length arrays are encoded as a regular array with 0 elements.
3612 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3613 S += '0';
3614 }
Mike Stump11289f42009-09-09 15:08:12 +00003615
3616 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003617 false, ExpandStructures, FD);
3618 S += ']';
3619 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003620 return;
3621 }
Mike Stump11289f42009-09-09 15:08:12 +00003622
John McCall9dd450b2009-09-21 23:43:11 +00003623 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003624 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003625 return;
3626 }
Mike Stump11289f42009-09-09 15:08:12 +00003627
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003628 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003629 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003630 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003631 // Anonymous structures print as '?'
3632 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3633 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003634 if (ClassTemplateSpecializationDecl *Spec
3635 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3636 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3637 std::string TemplateArgsStr
3638 = TemplateSpecializationType::PrintTemplateArgumentList(
3639 TemplateArgs.getFlatArgumentList(),
3640 TemplateArgs.flat_size(),
3641 (*this).PrintingPolicy);
3642
3643 S += TemplateArgsStr;
3644 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003645 } else {
3646 S += '?';
3647 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003648 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003649 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003650 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3651 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003652 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003653 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003654 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003655 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003656 S += '"';
3657 }
Mike Stump11289f42009-09-09 15:08:12 +00003658
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003659 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003660 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003661 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003662 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003663 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003664 QualType qt = Field->getType();
3665 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003666 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003667 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003668 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003669 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003670 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003671 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003672 return;
3673 }
Mike Stump11289f42009-09-09 15:08:12 +00003674
Chris Lattnere7cabb92009-07-13 00:10:46 +00003675 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003676 if (FD && FD->isBitField())
David Chisnallb190a2c2010-06-04 01:10:52 +00003677 EncodeBitField(this, S, T, FD);
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003678 else
3679 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003680 return;
3681 }
Mike Stump11289f42009-09-09 15:08:12 +00003682
Chris Lattnere7cabb92009-07-13 00:10:46 +00003683 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003684 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003685 return;
3686 }
Mike Stump11289f42009-09-09 15:08:12 +00003687
John McCall8b07ec22010-05-15 11:32:37 +00003688 // Ignore protocol qualifiers when mangling at this level.
3689 if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
3690 T = OT->getBaseType();
3691
John McCall8ccfcb52009-09-24 19:53:00 +00003692 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003693 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003694 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003695 S += '{';
3696 const IdentifierInfo *II = OI->getIdentifier();
3697 S += II->getName();
3698 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003699 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003700 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003701 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003702 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003703 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003704 RecFields[i]);
3705 else
Mike Stump11289f42009-09-09 15:08:12 +00003706 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003707 FD);
3708 }
3709 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003710 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003711 }
Mike Stump11289f42009-09-09 15:08:12 +00003712
John McCall9dd450b2009-09-21 23:43:11 +00003713 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003714 if (OPT->isObjCIdType()) {
3715 S += '@';
3716 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003717 }
Mike Stump11289f42009-09-09 15:08:12 +00003718
Steve Narofff0c86112009-10-28 22:03:49 +00003719 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3720 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3721 // Since this is a binary compatibility issue, need to consult with runtime
3722 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003723 S += '#';
3724 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003725 }
Mike Stump11289f42009-09-09 15:08:12 +00003726
Chris Lattnere7cabb92009-07-13 00:10:46 +00003727 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003728 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003729 ExpandPointedToStructures,
3730 ExpandStructures, FD);
3731 if (FD || EncodingProperty) {
3732 // Note that we do extended encoding of protocol qualifer list
3733 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003734 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003735 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3736 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003737 S += '<';
3738 S += (*I)->getNameAsString();
3739 S += '>';
3740 }
3741 S += '"';
3742 }
3743 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003744 }
Mike Stump11289f42009-09-09 15:08:12 +00003745
Chris Lattnere7cabb92009-07-13 00:10:46 +00003746 QualType PointeeTy = OPT->getPointeeType();
3747 if (!EncodingProperty &&
3748 isa<TypedefType>(PointeeTy.getTypePtr())) {
3749 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003750 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003751 // {...};
3752 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003753 getObjCEncodingForTypeImpl(PointeeTy, S,
3754 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003755 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003756 return;
3757 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003758
3759 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003760 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003761 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003762 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003763 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3764 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003765 S += '<';
3766 S += (*I)->getNameAsString();
3767 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003768 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003769 S += '"';
3770 }
3771 return;
3772 }
Mike Stump11289f42009-09-09 15:08:12 +00003773
John McCalla9e6e8d2010-05-17 23:56:34 +00003774 // gcc just blithely ignores member pointers.
3775 // TODO: maybe there should be a mangling for these
3776 if (T->getAs<MemberPointerType>())
3777 return;
3778
Chris Lattnere7cabb92009-07-13 00:10:46 +00003779 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003780}
3781
Mike Stump11289f42009-09-09 15:08:12 +00003782void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003783 std::string& S) const {
3784 if (QT & Decl::OBJC_TQ_In)
3785 S += 'n';
3786 if (QT & Decl::OBJC_TQ_Inout)
3787 S += 'N';
3788 if (QT & Decl::OBJC_TQ_Out)
3789 S += 'o';
3790 if (QT & Decl::OBJC_TQ_Bycopy)
3791 S += 'O';
3792 if (QT & Decl::OBJC_TQ_Byref)
3793 S += 'R';
3794 if (QT & Decl::OBJC_TQ_Oneway)
3795 S += 'V';
3796}
3797
Chris Lattnere7cabb92009-07-13 00:10:46 +00003798void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003799 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003800
Anders Carlsson87c149b2007-10-11 01:00:40 +00003801 BuiltinVaListType = T;
3802}
3803
Chris Lattnere7cabb92009-07-13 00:10:46 +00003804void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003805 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003806}
3807
Chris Lattnere7cabb92009-07-13 00:10:46 +00003808void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003809 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003810}
3811
Chris Lattnere7cabb92009-07-13 00:10:46 +00003812void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003813 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003814}
3815
Chris Lattnere7cabb92009-07-13 00:10:46 +00003816void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003817 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003818}
3819
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003820void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003821 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003822 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003823
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003824 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003825}
3826
John McCalld28ae272009-12-02 08:04:21 +00003827/// \brief Retrieve the template name that corresponds to a non-empty
3828/// lookup.
John McCallad371252010-01-20 00:46:10 +00003829TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3830 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003831 unsigned size = End - Begin;
3832 assert(size > 1 && "set is not overloaded!");
3833
3834 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3835 size * sizeof(FunctionTemplateDecl*));
3836 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3837
3838 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003839 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003840 NamedDecl *D = *I;
3841 assert(isa<FunctionTemplateDecl>(D) ||
3842 (isa<UsingShadowDecl>(D) &&
3843 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3844 *Storage++ = D;
3845 }
3846
3847 return TemplateName(OT);
3848}
3849
Douglas Gregordc572a32009-03-30 22:58:21 +00003850/// \brief Retrieve the template name that represents a qualified
3851/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003852TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003853 bool TemplateKeyword,
3854 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003855 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003856 llvm::FoldingSetNodeID ID;
3857 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3858
3859 void *InsertPos = 0;
3860 QualifiedTemplateName *QTN =
3861 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3862 if (!QTN) {
3863 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3864 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3865 }
3866
3867 return TemplateName(QTN);
3868}
3869
3870/// \brief Retrieve the template name that represents a dependent
3871/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003872TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003873 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003874 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003875 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003876
3877 llvm::FoldingSetNodeID ID;
3878 DependentTemplateName::Profile(ID, NNS, Name);
3879
3880 void *InsertPos = 0;
3881 DependentTemplateName *QTN =
3882 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3883
3884 if (QTN)
3885 return TemplateName(QTN);
3886
3887 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3888 if (CanonNNS == NNS) {
3889 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3890 } else {
3891 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3892 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003893 DependentTemplateName *CheckQTN =
3894 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3895 assert(!CheckQTN && "Dependent type name canonicalization broken");
3896 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00003897 }
3898
3899 DependentTemplateNames.InsertNode(QTN, InsertPos);
3900 return TemplateName(QTN);
3901}
3902
Douglas Gregor71395fa2009-11-04 00:56:37 +00003903/// \brief Retrieve the template name that represents a dependent
3904/// template name such as \c MetaFun::template operator+.
3905TemplateName
3906ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3907 OverloadedOperatorKind Operator) {
3908 assert((!NNS || NNS->isDependent()) &&
3909 "Nested name specifier must be dependent");
3910
3911 llvm::FoldingSetNodeID ID;
3912 DependentTemplateName::Profile(ID, NNS, Operator);
3913
3914 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00003915 DependentTemplateName *QTN
3916 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00003917
3918 if (QTN)
3919 return TemplateName(QTN);
3920
3921 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3922 if (CanonNNS == NNS) {
3923 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3924 } else {
3925 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3926 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003927
3928 DependentTemplateName *CheckQTN
3929 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3930 assert(!CheckQTN && "Dependent template name canonicalization broken");
3931 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00003932 }
3933
3934 DependentTemplateNames.InsertNode(QTN, InsertPos);
3935 return TemplateName(QTN);
3936}
3937
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003938/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003939/// TargetInfo, produce the corresponding type. The unsigned @p Type
3940/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003941CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003942 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003943 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003944 case TargetInfo::SignedShort: return ShortTy;
3945 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3946 case TargetInfo::SignedInt: return IntTy;
3947 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3948 case TargetInfo::SignedLong: return LongTy;
3949 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3950 case TargetInfo::SignedLongLong: return LongLongTy;
3951 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3952 }
3953
3954 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003955 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003956}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003957
3958//===----------------------------------------------------------------------===//
3959// Type Predicates.
3960//===----------------------------------------------------------------------===//
3961
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003962/// isObjCNSObjectType - Return true if this is an NSObject object using
3963/// NSObject attribute on a c-style pointer type.
3964/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003965/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003966///
3967bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3968 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3969 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003970 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003971 return true;
3972 }
Mike Stump11289f42009-09-09 15:08:12 +00003973 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003974}
3975
Fariborz Jahanian96207692009-02-18 21:49:28 +00003976/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3977/// garbage collection attribute.
3978///
John McCall8ccfcb52009-09-24 19:53:00 +00003979Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3980 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003981 if (getLangOptions().ObjC1 &&
3982 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003983 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003984 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003985 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003986 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003987 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003988 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003989 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003990 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003991 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003992 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003993 // Non-pointers have none gc'able attribute regardless of the attribute
3994 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003995 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003996 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003997 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003998 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003999}
4000
Chris Lattner49af6a42008-04-07 06:51:04 +00004001//===----------------------------------------------------------------------===//
4002// Type Compatibility Testing
4003//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00004004
Mike Stump11289f42009-09-09 15:08:12 +00004005/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004006/// compatible.
4007static bool areCompatVectorTypes(const VectorType *LHS,
4008 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00004009 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00004010 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00004011 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00004012}
4013
Steve Naroff8e6aee52009-07-23 01:01:38 +00004014//===----------------------------------------------------------------------===//
4015// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4016//===----------------------------------------------------------------------===//
4017
4018/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4019/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004020bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4021 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004022 if (lProto == rProto)
4023 return true;
4024 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4025 E = rProto->protocol_end(); PI != E; ++PI)
4026 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4027 return true;
4028 return false;
4029}
4030
Steve Naroff8e6aee52009-07-23 01:01:38 +00004031/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4032/// return true if lhs's protocols conform to rhs's protocol; false
4033/// otherwise.
4034bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4035 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4036 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4037 return false;
4038}
4039
4040/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4041/// ObjCQualifiedIDType.
4042bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4043 bool compare) {
4044 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004045 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004046 lhs->isObjCIdType() || lhs->isObjCClassType())
4047 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004048 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004049 rhs->isObjCIdType() || rhs->isObjCClassType())
4050 return true;
4051
4052 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004053 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004054
Steve Naroff8e6aee52009-07-23 01:01:38 +00004055 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004056
Steve Naroff8e6aee52009-07-23 01:01:38 +00004057 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004058 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004059 // make sure we check the class hierarchy.
4060 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4061 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4062 E = lhsQID->qual_end(); I != E; ++I) {
4063 // when comparing an id<P> on lhs with a static type on rhs,
4064 // see if static class implements all of id's protocols, directly or
4065 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004066 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004067 return false;
4068 }
4069 }
4070 // If there are no qualifiers and no interface, we have an 'id'.
4071 return true;
4072 }
Mike Stump11289f42009-09-09 15:08:12 +00004073 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004074 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4075 E = lhsQID->qual_end(); I != E; ++I) {
4076 ObjCProtocolDecl *lhsProto = *I;
4077 bool match = false;
4078
4079 // when comparing an id<P> on lhs with a static type on rhs,
4080 // see if static class implements all of id's protocols, directly or
4081 // through its super class and categories.
4082 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4083 E = rhsOPT->qual_end(); J != E; ++J) {
4084 ObjCProtocolDecl *rhsProto = *J;
4085 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4086 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4087 match = true;
4088 break;
4089 }
4090 }
Mike Stump11289f42009-09-09 15:08:12 +00004091 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004092 // make sure we check the class hierarchy.
4093 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4094 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4095 E = lhsQID->qual_end(); I != E; ++I) {
4096 // when comparing an id<P> on lhs with a static type on rhs,
4097 // see if static class implements all of id's protocols, directly or
4098 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004099 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004100 match = true;
4101 break;
4102 }
4103 }
4104 }
4105 if (!match)
4106 return false;
4107 }
Mike Stump11289f42009-09-09 15:08:12 +00004108
Steve Naroff8e6aee52009-07-23 01:01:38 +00004109 return true;
4110 }
Mike Stump11289f42009-09-09 15:08:12 +00004111
Steve Naroff8e6aee52009-07-23 01:01:38 +00004112 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4113 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4114
Mike Stump11289f42009-09-09 15:08:12 +00004115 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004116 lhs->getAsObjCInterfacePointerType()) {
4117 if (lhsOPT->qual_empty()) {
4118 bool match = false;
4119 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4120 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4121 E = rhsQID->qual_end(); I != E; ++I) {
4122 // when comparing an id<P> on lhs with a static type on rhs,
4123 // see if static class implements all of id's protocols, directly or
4124 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004125 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004126 match = true;
4127 break;
4128 }
4129 }
4130 if (!match)
4131 return false;
4132 }
4133 return true;
4134 }
Mike Stump11289f42009-09-09 15:08:12 +00004135 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004136 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4137 E = lhsOPT->qual_end(); I != E; ++I) {
4138 ObjCProtocolDecl *lhsProto = *I;
4139 bool match = false;
4140
4141 // when comparing an id<P> on lhs with a static type on rhs,
4142 // see if static class implements all of id's protocols, directly or
4143 // through its super class and categories.
4144 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4145 E = rhsQID->qual_end(); J != E; ++J) {
4146 ObjCProtocolDecl *rhsProto = *J;
4147 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4148 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4149 match = true;
4150 break;
4151 }
4152 }
4153 if (!match)
4154 return false;
4155 }
4156 return true;
4157 }
4158 return false;
4159}
4160
Eli Friedman47f77112008-08-22 00:56:42 +00004161/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004162/// compatible for assignment from RHS to LHS. This handles validation of any
4163/// protocol qualifiers on the LHS or RHS.
4164///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004165bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4166 const ObjCObjectPointerType *RHSOPT) {
John McCall8b07ec22010-05-15 11:32:37 +00004167 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4168 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4169
Steve Naroff1329fa02009-07-15 18:40:39 +00004170 // If either type represents the built-in 'id' or 'Class' types, return true.
John McCall8b07ec22010-05-15 11:32:37 +00004171 if (LHS->isObjCUnqualifiedIdOrClass() ||
4172 RHS->isObjCUnqualifiedIdOrClass())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004173 return true;
4174
John McCall8b07ec22010-05-15 11:32:37 +00004175 if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
Mike Stump11289f42009-09-09 15:08:12 +00004176 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4177 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004178 false);
4179
John McCall8b07ec22010-05-15 11:32:37 +00004180 // If we have 2 user-defined types, fall into that path.
4181 if (LHS->getInterface() && RHS->getInterface())
Steve Naroff8e6aee52009-07-23 01:01:38 +00004182 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004183
Steve Naroff8e6aee52009-07-23 01:01:38 +00004184 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004185}
4186
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004187/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4188/// for providing type-safty for objective-c pointers used to pass/return
4189/// arguments in block literals. When passed as arguments, passing 'A*' where
4190/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4191/// not OK. For the return type, the opposite is not OK.
4192bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4193 const ObjCObjectPointerType *LHSOPT,
4194 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004195 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004196 return true;
4197
4198 if (LHSOPT->isObjCBuiltinType()) {
4199 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4200 }
4201
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004202 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004203 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4204 QualType(RHSOPT,0),
4205 false);
4206
4207 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4208 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4209 if (LHS && RHS) { // We have 2 user-defined types.
4210 if (LHS != RHS) {
4211 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4212 return false;
4213 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4214 return true;
4215 }
4216 else
4217 return true;
4218 }
4219 return false;
4220}
4221
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004222/// getIntersectionOfProtocols - This routine finds the intersection of set
4223/// of protocols inherited from two distinct objective-c pointer objects.
4224/// It is used to build composite qualifier list of the composite type of
4225/// the conditional expression involving two objective-c pointer objects.
4226static
4227void getIntersectionOfProtocols(ASTContext &Context,
4228 const ObjCObjectPointerType *LHSOPT,
4229 const ObjCObjectPointerType *RHSOPT,
4230 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4231
John McCall8b07ec22010-05-15 11:32:37 +00004232 const ObjCObjectType* LHS = LHSOPT->getObjectType();
4233 const ObjCObjectType* RHS = RHSOPT->getObjectType();
4234 assert(LHS->getInterface() && "LHS must have an interface base");
4235 assert(RHS->getInterface() && "RHS must have an interface base");
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004236
4237 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4238 unsigned LHSNumProtocols = LHS->getNumProtocols();
4239 if (LHSNumProtocols > 0)
4240 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4241 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004242 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004243 Context.CollectInheritedProtocols(LHS->getInterface(),
4244 LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004245 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4246 LHSInheritedProtocols.end());
4247 }
4248
4249 unsigned RHSNumProtocols = RHS->getNumProtocols();
4250 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004251 ObjCProtocolDecl **RHSProtocols =
4252 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004253 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4254 if (InheritedProtocolSet.count(RHSProtocols[i]))
4255 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4256 }
4257 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004258 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
John McCall8b07ec22010-05-15 11:32:37 +00004259 Context.CollectInheritedProtocols(RHS->getInterface(),
4260 RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004261 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4262 RHSInheritedProtocols.begin(),
4263 E = RHSInheritedProtocols.end(); I != E; ++I)
4264 if (InheritedProtocolSet.count((*I)))
4265 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004266 }
4267}
4268
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004269/// areCommonBaseCompatible - Returns common base class of the two classes if
4270/// one found. Note that this is O'2 algorithm. But it will be called as the
4271/// last type comparison in a ?-exp of ObjC pointer types before a
4272/// warning is issued. So, its invokation is extremely rare.
4273QualType ASTContext::areCommonBaseCompatible(
John McCall8b07ec22010-05-15 11:32:37 +00004274 const ObjCObjectPointerType *Lptr,
4275 const ObjCObjectPointerType *Rptr) {
4276 const ObjCObjectType *LHS = Lptr->getObjectType();
4277 const ObjCObjectType *RHS = Rptr->getObjectType();
4278 const ObjCInterfaceDecl* LDecl = LHS->getInterface();
4279 const ObjCInterfaceDecl* RDecl = RHS->getInterface();
4280 if (!LDecl || !RDecl)
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004281 return QualType();
4282
John McCall8b07ec22010-05-15 11:32:37 +00004283 while ((LDecl = LDecl->getSuperClass())) {
4284 LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004285 if (canAssignObjCInterfaces(LHS, RHS)) {
John McCall8b07ec22010-05-15 11:32:37 +00004286 llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
4287 getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
4288
4289 QualType Result = QualType(LHS, 0);
4290 if (!Protocols.empty())
4291 Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
4292 Result = getObjCObjectPointerType(Result);
4293 return Result;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004294 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004295 }
4296
4297 return QualType();
4298}
4299
John McCall8b07ec22010-05-15 11:32:37 +00004300bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
4301 const ObjCObjectType *RHS) {
4302 assert(LHS->getInterface() && "LHS is not an interface type");
4303 assert(RHS->getInterface() && "RHS is not an interface type");
4304
Chris Lattner49af6a42008-04-07 06:51:04 +00004305 // Verify that the base decls are compatible: the RHS must be a subclass of
4306 // the LHS.
John McCall8b07ec22010-05-15 11:32:37 +00004307 if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
Chris Lattner49af6a42008-04-07 06:51:04 +00004308 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004309
Chris Lattner49af6a42008-04-07 06:51:04 +00004310 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4311 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004312 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004313 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004314
Chris Lattner49af6a42008-04-07 06:51:04 +00004315 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4316 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004317 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004318 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004319
John McCall8b07ec22010-05-15 11:32:37 +00004320 for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
4321 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004322 LHSPI != LHSPE; LHSPI++) {
4323 bool RHSImplementsProtocol = false;
4324
4325 // If the RHS doesn't implement the protocol on the left, the types
4326 // are incompatible.
John McCall8b07ec22010-05-15 11:32:37 +00004327 for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
4328 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004329 RHSPI != RHSPE; RHSPI++) {
4330 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004331 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004332 break;
4333 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004334 }
4335 // FIXME: For better diagnostics, consider passing back the protocol name.
4336 if (!RHSImplementsProtocol)
4337 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004338 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004339 // The RHS implements all protocols listed on the LHS.
4340 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004341}
4342
Steve Naroffb7605152009-02-12 17:52:19 +00004343bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4344 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004345 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4346 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004347
Steve Naroff7cae42b2009-07-10 23:34:53 +00004348 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004349 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004350
4351 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4352 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004353}
4354
Mike Stump11289f42009-09-09 15:08:12 +00004355/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004356/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004357/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004358/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004359bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004360 if (getLangOptions().CPlusPlus)
4361 return hasSameType(LHS, RHS);
4362
Eli Friedman47f77112008-08-22 00:56:42 +00004363 return !mergeTypes(LHS, RHS).isNull();
4364}
4365
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004366bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4367 return !mergeTypes(LHS, RHS, true).isNull();
4368}
4369
4370QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4371 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004372 const FunctionType *lbase = lhs->getAs<FunctionType>();
4373 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004374 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4375 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004376 bool allLTypes = true;
4377 bool allRTypes = true;
4378
4379 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004380 QualType retType;
4381 if (OfBlockPointer)
4382 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4383 else
4384 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004385 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004386 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004387 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004388 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004389 allRTypes = false;
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004390 // FIXME: double check this
4391 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4392 // rbase->getRegParmAttr() != 0 &&
4393 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004394 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4395 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004396 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4397 lbaseInfo.getRegParm();
4398 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4399 if (NoReturn != lbaseInfo.getNoReturn() ||
4400 RegParm != lbaseInfo.getRegParm())
4401 allLTypes = false;
4402 if (NoReturn != rbaseInfo.getNoReturn() ||
4403 RegParm != rbaseInfo.getRegParm())
4404 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004405 CallingConv lcc = lbaseInfo.getCC();
4406 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004407 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004408 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004409 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004410
Eli Friedman47f77112008-08-22 00:56:42 +00004411 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004412 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4413 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004414 unsigned lproto_nargs = lproto->getNumArgs();
4415 unsigned rproto_nargs = rproto->getNumArgs();
4416
4417 // Compatible functions must have the same number of arguments
4418 if (lproto_nargs != rproto_nargs)
4419 return QualType();
4420
4421 // Variadic and non-variadic functions aren't compatible
4422 if (lproto->isVariadic() != rproto->isVariadic())
4423 return QualType();
4424
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004425 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4426 return QualType();
4427
Eli Friedman47f77112008-08-22 00:56:42 +00004428 // Check argument compatibility
4429 llvm::SmallVector<QualType, 10> types;
4430 for (unsigned i = 0; i < lproto_nargs; i++) {
4431 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4432 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004433 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004434 if (argtype.isNull()) return QualType();
4435 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004436 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4437 allLTypes = false;
4438 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4439 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004440 }
4441 if (allLTypes) return lhs;
4442 if (allRTypes) return rhs;
4443 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004444 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004445 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004446 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004447 }
4448
4449 if (lproto) allRTypes = false;
4450 if (rproto) allLTypes = false;
4451
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004452 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004453 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004454 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004455 if (proto->isVariadic()) return QualType();
4456 // Check that the types are compatible with the types that
4457 // would result from default argument promotions (C99 6.7.5.3p15).
4458 // The only types actually affected are promotable integer
4459 // types and floats, which would be passed as a different
4460 // type depending on whether the prototype is visible.
4461 unsigned proto_nargs = proto->getNumArgs();
4462 for (unsigned i = 0; i < proto_nargs; ++i) {
4463 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004464
4465 // Look at the promotion type of enum types, since that is the type used
4466 // to pass enum values.
4467 if (const EnumType *Enum = argTy->getAs<EnumType>())
4468 argTy = Enum->getDecl()->getPromotionType();
4469
Eli Friedman47f77112008-08-22 00:56:42 +00004470 if (argTy->isPromotableIntegerType() ||
4471 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4472 return QualType();
4473 }
4474
4475 if (allLTypes) return lhs;
4476 if (allRTypes) return rhs;
4477 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004478 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004479 proto->getTypeQuals(),
4480 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004481 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004482 }
4483
4484 if (allLTypes) return lhs;
4485 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004486 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004487 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004488}
4489
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004490QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4491 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004492 // C++ [expr]: If an expression initially has the type "reference to T", the
4493 // type is adjusted to "T" prior to any further analysis, the expression
4494 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004495 // expression is an lvalue unless the reference is an rvalue reference and
4496 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004497 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4498 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4499
Eli Friedman47f77112008-08-22 00:56:42 +00004500 QualType LHSCan = getCanonicalType(LHS),
4501 RHSCan = getCanonicalType(RHS);
4502
4503 // If two types are identical, they are compatible.
4504 if (LHSCan == RHSCan)
4505 return LHS;
4506
John McCall8ccfcb52009-09-24 19:53:00 +00004507 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004508 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4509 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004510 if (LQuals != RQuals) {
4511 // If any of these qualifiers are different, we have a type
4512 // mismatch.
4513 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4514 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4515 return QualType();
4516
4517 // Exactly one GC qualifier difference is allowed: __strong is
4518 // okay if the other type has no GC qualifier but is an Objective
4519 // C object pointer (i.e. implicitly strong by default). We fix
4520 // this by pretending that the unqualified type was actually
4521 // qualified __strong.
4522 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4523 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4524 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4525
4526 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4527 return QualType();
4528
4529 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4530 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4531 }
4532 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4533 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4534 }
Eli Friedman47f77112008-08-22 00:56:42 +00004535 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004536 }
4537
4538 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004539
Eli Friedmandcca6332009-06-01 01:22:52 +00004540 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4541 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004542
Chris Lattnerfd652912008-01-14 05:45:46 +00004543 // We want to consider the two function types to be the same for these
4544 // comparisons, just force one to the other.
4545 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4546 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004547
4548 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004549 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4550 LHSClass = Type::ConstantArray;
4551 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4552 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004553
John McCall8b07ec22010-05-15 11:32:37 +00004554 // ObjCInterfaces are just specialized ObjCObjects.
4555 if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
4556 if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
4557
Nate Begemance4d7fc2008-04-18 23:10:10 +00004558 // Canonicalize ExtVector -> Vector.
4559 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4560 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004561
Chris Lattner95554662008-04-07 05:43:21 +00004562 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004563 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004564 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004565 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004566 // Compatibility is based on the underlying type, not the promotion
4567 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004568 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004569 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4570 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004571 }
John McCall9dd450b2009-09-21 23:43:11 +00004572 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004573 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4574 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004575 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004576
Eli Friedman47f77112008-08-22 00:56:42 +00004577 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004578 }
Eli Friedman47f77112008-08-22 00:56:42 +00004579
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004580 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004581 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004582#define TYPE(Class, Base)
4583#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004584#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004585#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4586#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4587#include "clang/AST/TypeNodes.def"
4588 assert(false && "Non-canonical and dependent types shouldn't get here");
4589 return QualType();
4590
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004591 case Type::LValueReference:
4592 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004593 case Type::MemberPointer:
4594 assert(false && "C++ should never be in mergeTypes");
4595 return QualType();
4596
John McCall8b07ec22010-05-15 11:32:37 +00004597 case Type::ObjCInterface:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004598 case Type::IncompleteArray:
4599 case Type::VariableArray:
4600 case Type::FunctionProto:
4601 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004602 assert(false && "Types are eliminated above");
4603 return QualType();
4604
Chris Lattnerfd652912008-01-14 05:45:46 +00004605 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004606 {
4607 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004608 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4609 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004610 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4611 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004612 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004613 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004614 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004615 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004616 return getPointerType(ResultType);
4617 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004618 case Type::BlockPointer:
4619 {
4620 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004621 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4622 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004623 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004624 if (ResultType.isNull()) return QualType();
4625 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4626 return LHS;
4627 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4628 return RHS;
4629 return getBlockPointerType(ResultType);
4630 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004631 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004632 {
4633 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4634 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4635 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4636 return QualType();
4637
4638 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4639 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4640 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4641 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004642 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4643 return LHS;
4644 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4645 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004646 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4647 ArrayType::ArraySizeModifier(), 0);
4648 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4649 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004650 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4651 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004652 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4653 return LHS;
4654 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4655 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004656 if (LVAT) {
4657 // FIXME: This isn't correct! But tricky to implement because
4658 // the array's size has to be the size of LHS, but the type
4659 // has to be different.
4660 return LHS;
4661 }
4662 if (RVAT) {
4663 // FIXME: This isn't correct! But tricky to implement because
4664 // the array's size has to be the size of RHS, but the type
4665 // has to be different.
4666 return RHS;
4667 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004668 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4669 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004670 return getIncompleteArrayType(ResultType,
4671 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004672 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004673 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004674 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004675 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004676 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004677 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004678 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004679 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004680 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004681 case Type::Complex:
4682 // Distinct complex types are incompatible.
4683 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004684 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004685 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004686 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4687 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004688 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004689 return QualType();
John McCall8b07ec22010-05-15 11:32:37 +00004690 case Type::ObjCObject: {
4691 // Check if the types are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004692 // FIXME: This should be type compatibility, e.g. whether
4693 // "LHS x; RHS x;" at global scope is legal.
John McCall8b07ec22010-05-15 11:32:37 +00004694 const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
4695 const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
4696 if (canAssignObjCInterfaces(LHSIface, RHSIface))
Steve Naroff7a7814c2009-02-21 16:18:07 +00004697 return LHS;
4698
Eli Friedman47f77112008-08-22 00:56:42 +00004699 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004700 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004701 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004702 if (OfBlockPointer) {
4703 if (canAssignObjCInterfacesInBlockPointer(
4704 LHS->getAs<ObjCObjectPointerType>(),
4705 RHS->getAs<ObjCObjectPointerType>()))
4706 return LHS;
4707 return QualType();
4708 }
John McCall9dd450b2009-09-21 23:43:11 +00004709 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4710 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004711 return LHS;
4712
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004713 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004714 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004715 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004716
4717 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004718}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004719
Fariborz Jahanianf633ebd2010-05-19 21:37:30 +00004720/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
4721/// 'RHS' attributes and returns the merged version; including for function
4722/// return types.
4723QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
4724 QualType LHSCan = getCanonicalType(LHS),
4725 RHSCan = getCanonicalType(RHS);
4726 // If two types are identical, they are compatible.
4727 if (LHSCan == RHSCan)
4728 return LHS;
4729 if (RHSCan->isFunctionType()) {
4730 if (!LHSCan->isFunctionType())
4731 return QualType();
4732 QualType OldReturnType =
4733 cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
4734 QualType NewReturnType =
4735 cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
4736 QualType ResReturnType =
4737 mergeObjCGCQualifiers(NewReturnType, OldReturnType);
4738 if (ResReturnType.isNull())
4739 return QualType();
4740 if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
4741 // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
4742 // In either case, use OldReturnType to build the new function type.
4743 const FunctionType *F = LHS->getAs<FunctionType>();
4744 if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
4745 FunctionType::ExtInfo Info = getFunctionExtInfo(LHS);
4746 QualType ResultType
4747 = getFunctionType(OldReturnType, FPT->arg_type_begin(),
4748 FPT->getNumArgs(), FPT->isVariadic(),
4749 FPT->getTypeQuals(),
4750 FPT->hasExceptionSpec(),
4751 FPT->hasAnyExceptionSpec(),
4752 FPT->getNumExceptions(),
4753 FPT->exception_begin(),
4754 Info);
4755 return ResultType;
4756 }
4757 }
4758 return QualType();
4759 }
4760
4761 // If the qualifiers are different, the types can still be merged.
4762 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4763 Qualifiers RQuals = RHSCan.getLocalQualifiers();
4764 if (LQuals != RQuals) {
4765 // If any of these qualifiers are different, we have a type mismatch.
4766 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4767 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4768 return QualType();
4769
4770 // Exactly one GC qualifier difference is allowed: __strong is
4771 // okay if the other type has no GC qualifier but is an Objective
4772 // C object pointer (i.e. implicitly strong by default). We fix
4773 // this by pretending that the unqualified type was actually
4774 // qualified __strong.
4775 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4776 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4777 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4778
4779 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4780 return QualType();
4781
4782 if (GC_L == Qualifiers::Strong)
4783 return LHS;
4784 if (GC_R == Qualifiers::Strong)
4785 return RHS;
4786 return QualType();
4787 }
4788
4789 if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
4790 QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
4791 QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
4792 QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
4793 if (ResQT == LHSBaseQT)
4794 return LHS;
4795 if (ResQT == RHSBaseQT)
4796 return RHS;
4797 }
4798 return QualType();
4799}
4800
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004801//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004802// Integer Predicates
4803//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004804
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004805unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004806 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004807 return 1;
John McCall56774992009-12-09 09:09:27 +00004808 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004809 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004810 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004811 return (unsigned)getTypeSize(T);
4812}
4813
4814QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4815 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004816
4817 // Turn <4 x signed int> -> <4 x unsigned int>
4818 if (const VectorType *VTy = T->getAs<VectorType>())
4819 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004820 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004821
4822 // For enums, we return the unsigned version of the base type.
4823 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004824 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004825
4826 const BuiltinType *BTy = T->getAs<BuiltinType>();
4827 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004828 switch (BTy->getKind()) {
4829 case BuiltinType::Char_S:
4830 case BuiltinType::SChar:
4831 return UnsignedCharTy;
4832 case BuiltinType::Short:
4833 return UnsignedShortTy;
4834 case BuiltinType::Int:
4835 return UnsignedIntTy;
4836 case BuiltinType::Long:
4837 return UnsignedLongTy;
4838 case BuiltinType::LongLong:
4839 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004840 case BuiltinType::Int128:
4841 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004842 default:
4843 assert(0 && "Unexpected signed integer type");
4844 return QualType();
4845 }
4846}
4847
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004848ExternalASTSource::~ExternalASTSource() { }
4849
4850void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004851
4852
4853//===----------------------------------------------------------------------===//
4854// Builtin Type Computation
4855//===----------------------------------------------------------------------===//
4856
4857/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4858/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004859static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004860 ASTContext::GetBuiltinTypeError &Error,
4861 bool AllowTypeModifiers = true) {
4862 // Modifiers.
4863 int HowLong = 0;
4864 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004865
Chris Lattnerecd79c62009-06-14 00:45:47 +00004866 // Read the modifiers first.
4867 bool Done = false;
4868 while (!Done) {
4869 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004870 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004871 case 'S':
4872 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4873 assert(!Signed && "Can't use 'S' modifier multiple times!");
4874 Signed = true;
4875 break;
4876 case 'U':
4877 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4878 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4879 Unsigned = true;
4880 break;
4881 case 'L':
4882 assert(HowLong <= 2 && "Can't have LLLL modifier");
4883 ++HowLong;
4884 break;
4885 }
4886 }
4887
4888 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004889
Chris Lattnerecd79c62009-06-14 00:45:47 +00004890 // Read the base type.
4891 switch (*Str++) {
4892 default: assert(0 && "Unknown builtin type letter!");
4893 case 'v':
4894 assert(HowLong == 0 && !Signed && !Unsigned &&
4895 "Bad modifiers used with 'v'!");
4896 Type = Context.VoidTy;
4897 break;
4898 case 'f':
4899 assert(HowLong == 0 && !Signed && !Unsigned &&
4900 "Bad modifiers used with 'f'!");
4901 Type = Context.FloatTy;
4902 break;
4903 case 'd':
4904 assert(HowLong < 2 && !Signed && !Unsigned &&
4905 "Bad modifiers used with 'd'!");
4906 if (HowLong)
4907 Type = Context.LongDoubleTy;
4908 else
4909 Type = Context.DoubleTy;
4910 break;
4911 case 's':
4912 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4913 if (Unsigned)
4914 Type = Context.UnsignedShortTy;
4915 else
4916 Type = Context.ShortTy;
4917 break;
4918 case 'i':
4919 if (HowLong == 3)
4920 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4921 else if (HowLong == 2)
4922 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4923 else if (HowLong == 1)
4924 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4925 else
4926 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4927 break;
4928 case 'c':
4929 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4930 if (Signed)
4931 Type = Context.SignedCharTy;
4932 else if (Unsigned)
4933 Type = Context.UnsignedCharTy;
4934 else
4935 Type = Context.CharTy;
4936 break;
4937 case 'b': // boolean
4938 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4939 Type = Context.BoolTy;
4940 break;
4941 case 'z': // size_t.
4942 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4943 Type = Context.getSizeType();
4944 break;
4945 case 'F':
4946 Type = Context.getCFConstantStringType();
4947 break;
4948 case 'a':
4949 Type = Context.getBuiltinVaListType();
4950 assert(!Type.isNull() && "builtin va list type not initialized!");
4951 break;
4952 case 'A':
4953 // This is a "reference" to a va_list; however, what exactly
4954 // this means depends on how va_list is defined. There are two
4955 // different kinds of va_list: ones passed by value, and ones
4956 // passed by reference. An example of a by-value va_list is
4957 // x86, where va_list is a char*. An example of by-ref va_list
4958 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4959 // we want this argument to be a char*&; for x86-64, we want
4960 // it to be a __va_list_tag*.
4961 Type = Context.getBuiltinVaListType();
4962 assert(!Type.isNull() && "builtin va list type not initialized!");
4963 if (Type->isArrayType()) {
4964 Type = Context.getArrayDecayedType(Type);
4965 } else {
4966 Type = Context.getLValueReferenceType(Type);
4967 }
4968 break;
4969 case 'V': {
4970 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004971 unsigned NumElements = strtoul(Str, &End, 10);
4972 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004973
Chris Lattnerecd79c62009-06-14 00:45:47 +00004974 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004975
Chris Lattnerecd79c62009-06-14 00:45:47 +00004976 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00004977 // FIXME: Don't know what to do about AltiVec.
4978 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004979 break;
4980 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004981 case 'X': {
4982 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4983 Type = Context.getComplexType(ElementType);
4984 break;
4985 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004986 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004987 Type = Context.getFILEType();
4988 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004989 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004990 return QualType();
4991 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004992 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004993 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004994 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004995 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004996 else
4997 Type = Context.getjmp_bufType();
4998
Mike Stump2adb4da2009-07-28 23:47:15 +00004999 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00005000 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00005001 return QualType();
5002 }
5003 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00005004 }
Mike Stump11289f42009-09-09 15:08:12 +00005005
Chris Lattnerecd79c62009-06-14 00:45:47 +00005006 if (!AllowTypeModifiers)
5007 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00005008
Chris Lattnerecd79c62009-06-14 00:45:47 +00005009 Done = false;
5010 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00005011 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00005012 default: Done = true; --Str; break;
5013 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00005014 case '&':
John McCallb8b94662010-03-12 04:21:28 +00005015 {
5016 // Both pointers and references can have their pointee types
5017 // qualified with an address space.
5018 char *End;
5019 unsigned AddrSpace = strtoul(Str, &End, 10);
5020 if (End != Str && AddrSpace != 0) {
5021 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
5022 Str = End;
5023 }
5024 }
5025 if (c == '*')
5026 Type = Context.getPointerType(Type);
5027 else
5028 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00005029 break;
5030 // FIXME: There's no way to have a built-in with an rvalue ref arg.
5031 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00005032 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00005033 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00005034 case 'D':
5035 Type = Context.getVolatileType(Type);
5036 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00005037 }
5038 }
Mike Stump11289f42009-09-09 15:08:12 +00005039
Chris Lattnerecd79c62009-06-14 00:45:47 +00005040 return Type;
5041}
5042
5043/// GetBuiltinType - Return the type for the specified builtin.
5044QualType ASTContext::GetBuiltinType(unsigned id,
5045 GetBuiltinTypeError &Error) {
5046 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00005047
Chris Lattnerecd79c62009-06-14 00:45:47 +00005048 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00005049
Chris Lattnerecd79c62009-06-14 00:45:47 +00005050 Error = GE_None;
5051 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
5052 if (Error != GE_None)
5053 return QualType();
5054 while (TypeStr[0] && TypeStr[0] != '.') {
5055 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
5056 if (Error != GE_None)
5057 return QualType();
5058
5059 // Do array -> pointer decay. The builtin should use the decayed type.
5060 if (Ty->isArrayType())
5061 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00005062
Chris Lattnerecd79c62009-06-14 00:45:47 +00005063 ArgTypes.push_back(Ty);
5064 }
5065
5066 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
5067 "'.' should only occur at end of builtin type list!");
5068
5069 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
5070 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
5071 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00005072
5073 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00005074 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00005075 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005076 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00005077}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005078
5079QualType
5080ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
5081 // Perform the usual unary conversions. We do this early so that
5082 // integral promotions to "int" can allow us to exit early, in the
5083 // lhs == rhs check. Also, for conversion purposes, we ignore any
5084 // qualifiers. For example, "const float" and "float" are
5085 // equivalent.
5086 if (lhs->isPromotableIntegerType())
5087 lhs = getPromotedIntegerType(lhs);
5088 else
5089 lhs = lhs.getUnqualifiedType();
5090 if (rhs->isPromotableIntegerType())
5091 rhs = getPromotedIntegerType(rhs);
5092 else
5093 rhs = rhs.getUnqualifiedType();
5094
5095 // If both types are identical, no conversion is needed.
5096 if (lhs == rhs)
5097 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005098
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005099 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
5100 // The caller can deal with this (e.g. pointer + int).
5101 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
5102 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00005103
5104 // At this point, we have two different arithmetic types.
5105
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005106 // Handle complex types first (C99 6.3.1.8p1).
5107 if (lhs->isComplexType() || rhs->isComplexType()) {
5108 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00005109 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005110 // convert the rhs to the lhs complex type.
5111 return lhs;
5112 }
Mike Stump11289f42009-09-09 15:08:12 +00005113 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005114 // convert the lhs to the rhs complex type.
5115 return rhs;
5116 }
5117 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00005118 // When both operands are complex, the shorter operand is converted to the
5119 // type of the longer, and that is the type of the result. This corresponds
5120 // to what is done when combining two real floating-point operands.
5121 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005122 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00005123 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005124 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00005125 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005126 // "double _Complex" is promoted to "long double _Complex".
5127 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005128
5129 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005130 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00005131 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005132 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00005133 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005134 // At this point, lhs and rhs have the same rank/size. Now, make sure the
5135 // domains match. This is a requirement for our implementation, C99
5136 // does not require this promotion.
5137 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5138 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5139 return rhs;
5140 } else { // handle "_Complex double, double".
5141 return lhs;
5142 }
5143 }
5144 return lhs; // The domain/size match exactly.
5145 }
5146 // Now handle "real" floating types (i.e. float, double, long double).
5147 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5148 // if we have an integer operand, the result is the real floating type.
5149 if (rhs->isIntegerType()) {
5150 // convert rhs to the lhs floating point type.
5151 return lhs;
5152 }
5153 if (rhs->isComplexIntegerType()) {
5154 // convert rhs to the complex floating point type.
5155 return getComplexType(lhs);
5156 }
5157 if (lhs->isIntegerType()) {
5158 // convert lhs to the rhs floating point type.
5159 return rhs;
5160 }
Mike Stump11289f42009-09-09 15:08:12 +00005161 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005162 // convert lhs to the complex floating point type.
5163 return getComplexType(rhs);
5164 }
5165 // We have two real floating types, float/complex combos were handled above.
5166 // Convert the smaller operand to the bigger result.
5167 int result = getFloatingTypeOrder(lhs, rhs);
5168 if (result > 0) // convert the rhs
5169 return lhs;
5170 assert(result < 0 && "illegal float comparison");
5171 return rhs; // convert the lhs
5172 }
5173 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5174 // Handle GCC complex int extension.
5175 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5176 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5177
5178 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005179 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005180 rhsComplexInt->getElementType()) >= 0)
5181 return lhs; // convert the rhs
5182 return rhs;
5183 } else if (lhsComplexInt && rhs->isIntegerType()) {
5184 // convert the rhs to the lhs complex type.
5185 return lhs;
5186 } else if (rhsComplexInt && lhs->isIntegerType()) {
5187 // convert the lhs to the rhs complex type.
5188 return rhs;
5189 }
5190 }
5191 // Finally, we have two differing integer types.
5192 // The rules for this case are in C99 6.3.1.8
5193 int compare = getIntegerTypeOrder(lhs, rhs);
5194 bool lhsSigned = lhs->isSignedIntegerType(),
5195 rhsSigned = rhs->isSignedIntegerType();
5196 QualType destType;
5197 if (lhsSigned == rhsSigned) {
5198 // Same signedness; use the higher-ranked type
5199 destType = compare >= 0 ? lhs : rhs;
5200 } else if (compare != (lhsSigned ? 1 : -1)) {
5201 // The unsigned type has greater than or equal rank to the
5202 // signed type, so use the unsigned type
5203 destType = lhsSigned ? rhs : lhs;
5204 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5205 // The two types are different widths; if we are here, that
5206 // means the signed type is larger than the unsigned type, so
5207 // use the signed type.
5208 destType = lhsSigned ? lhs : rhs;
5209 } else {
5210 // The signed type is higher-ranked than the unsigned type,
5211 // but isn't actually any bigger (like unsigned int and long
5212 // on most 32-bit systems). Use the unsigned type corresponding
5213 // to the signed type.
5214 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5215 }
5216 return destType;
5217}