blob: 7431a23cdd5a90d775cd356f03f85b11efc9b204 [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerddc135e2006-11-10 06:34:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyck8c89d592009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff67391b82007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/ExternalASTSource.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000024#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000026#include "llvm/ADT/SmallString.h"
Anders Carlssond8499822007-10-29 05:01:08 +000027#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000028#include "llvm/Support/MathExtras.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000029#include "llvm/Support/raw_ostream.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000030#include "RecordLayoutBuilder.h"
31
Chris Lattnerddc135e2006-11-10 06:34:16 +000032using namespace clang;
33
Steve Naroff0af91202007-04-27 21:51:21 +000034enum FloatingRank {
35 FloatRank, DoubleRank, LongDoubleRank
36};
37
Chris Lattner465fa322008-10-05 17:34:18 +000038ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar1b444192009-11-13 05:51:54 +000039 const TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +000040 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +000041 Builtin::Context &builtins,
Mike Stump11289f42009-09-09 15:08:12 +000042 bool FreeMem, unsigned size_reserve) :
43 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +000044 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +000045 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
Douglas Gregor9507d462010-03-19 22:13:20 +000046 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000047 Idents(idents), Selectors(sels),
John McCallc62bb642010-03-24 05:22:00 +000048 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts),
49 LastSDM(0, 0) {
David Chisnall9f57c292009-08-17 16:35:33 +000050 ObjCIdRedefinitionType = QualType();
51 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian04b258c2009-11-25 23:07:42 +000052 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +000053 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +000054 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +000055 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +000056}
57
Chris Lattnerd5973eb2006-11-12 00:53:46 +000058ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +000059 // Release the DenseMaps associated with DeclContext objects.
60 // FIXME: Is this the ideal solution?
61 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +000062
63 // Release all of the memory associated with overridden C++ methods.
64 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
65 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
66 OM != OMEnd; ++OM)
67 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +000068
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000069 if (FreeMemory) {
70 // Deallocate all the types.
71 while (!Types.empty()) {
72 Types.back()->Destroy(*this);
73 Types.pop_back();
74 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +000075
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000076 for (llvm::FoldingSet<ExtQuals>::iterator
77 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
78 // Increment in loop to prevent using deallocated memory.
John McCall8ccfcb52009-09-24 19:53:00 +000079 Deallocate(&*I++);
Nuno Lopese013c7f2008-12-17 22:30:25 +000080 }
Nuno Lopese013c7f2008-12-17 22:30:25 +000081
Ted Kremenekc3015a92010-03-08 20:56:29 +000082 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
83 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
84 // Increment in loop to prevent using deallocated memory.
85 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
86 R->Destroy(*this);
87 }
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000088
Ted Kremenekc3015a92010-03-08 20:56:29 +000089 for (llvm::DenseMap<const ObjCContainerDecl*,
90 const ASTRecordLayout*>::iterator
91 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
92 // Increment in loop to prevent using deallocated memory.
93 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
94 R->Destroy(*this);
95 }
Nuno Lopese013c7f2008-12-17 22:30:25 +000096 }
97
Douglas Gregorf21eb492009-03-26 23:50:42 +000098 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +000099 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
100 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000101 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000102 NNS != NNSEnd; ) {
103 // Increment in loop to prevent using deallocated memory.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000104 (*NNS++).Destroy(*this);
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000105 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000106
107 if (GlobalNestedNameSpecifier)
108 GlobalNestedNameSpecifier->Destroy(*this);
109
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000110 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000111}
112
Mike Stump11289f42009-09-09 15:08:12 +0000113void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
115 ExternalSource.reset(Source.take());
116}
117
Chris Lattner4eb445d2007-01-26 01:27:23 +0000118void ASTContext::PrintStats() const {
119 fprintf(stderr, "*** AST Context Stats:\n");
120 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000121
Douglas Gregora30d0462009-05-26 14:40:08 +0000122 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000123#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000124#define ABSTRACT_TYPE(Name, Parent)
125#include "clang/AST/TypeNodes.def"
126 0 // Extra
127 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000128
Chris Lattner4eb445d2007-01-26 01:27:23 +0000129 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
130 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000131 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000132 }
133
Douglas Gregora30d0462009-05-26 14:40:08 +0000134 unsigned Idx = 0;
135 unsigned TotalBytes = 0;
136#define TYPE(Name, Parent) \
137 if (counts[Idx]) \
138 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
139 TotalBytes += counts[Idx] * sizeof(Name##Type); \
140 ++Idx;
141#define ABSTRACT_TYPE(Name, Parent)
142#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000143
Douglas Gregora30d0462009-05-26 14:40:08 +0000144 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145
146 if (ExternalSource.get()) {
147 fprintf(stderr, "\n");
148 ExternalSource->PrintStats();
149 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000150}
151
152
John McCall48f2d582009-10-23 23:03:21 +0000153void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000154 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000155 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000156 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000157}
158
Chris Lattner970e54e2006-11-12 00:37:36 +0000159void ASTContext::InitBuiltinTypes() {
160 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000161
Chris Lattner970e54e2006-11-12 00:37:36 +0000162 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000163 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattner970e54e2006-11-12 00:37:36 +0000165 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000166 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000167 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000168 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000169 InitBuiltinType(CharTy, BuiltinType::Char_S);
170 else
171 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000172 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000173 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
174 InitBuiltinType(ShortTy, BuiltinType::Short);
175 InitBuiltinType(IntTy, BuiltinType::Int);
176 InitBuiltinType(LongTy, BuiltinType::Long);
177 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner970e54e2006-11-12 00:37:36 +0000179 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000180 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
181 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
182 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
183 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
184 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner970e54e2006-11-12 00:37:36 +0000186 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000187 InitBuiltinType(FloatTy, BuiltinType::Float);
188 InitBuiltinType(DoubleTy, BuiltinType::Double);
189 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000190
Chris Lattnerf122cef2009-04-30 02:43:43 +0000191 // GNU extension, 128-bit integers.
192 InitBuiltinType(Int128Ty, BuiltinType::Int128);
193 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
194
Chris Lattner007cb022009-02-26 23:43:47 +0000195 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
196 InitBuiltinType(WCharTy, BuiltinType::WChar);
197 else // C99
198 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000199
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000200 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
201 InitBuiltinType(Char16Ty, BuiltinType::Char16);
202 else // C99
203 Char16Ty = getFromTargetType(Target.getChar16Type());
204
205 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
206 InitBuiltinType(Char32Ty, BuiltinType::Char32);
207 else // C99
208 Char32Ty = getFromTargetType(Target.getChar32Type());
209
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000210 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000211 InitBuiltinType(OverloadTy, BuiltinType::Overload);
212
213 // Placeholder type for type-dependent expressions whose type is
214 // completely unknown. No code should ever check a type against
215 // DependentTy and users should never see it; however, it is here to
216 // help diagnose failures to properly check for type-dependent
217 // expressions.
218 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000219
Mike Stump11289f42009-09-09 15:08:12 +0000220 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000221 // not yet been deduced.
222 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000223
Chris Lattner970e54e2006-11-12 00:37:36 +0000224 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000225 FloatComplexTy = getComplexType(FloatTy);
226 DoubleComplexTy = getComplexType(DoubleTy);
227 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000228
Steve Naroff66e9f332007-10-15 14:41:52 +0000229 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000230
Steve Naroff1329fa02009-07-15 18:40:39 +0000231 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
232 ObjCIdTypedefType = QualType();
233 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000234 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000235
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000236 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000237 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
238 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000239 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000240
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000241 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000242
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000243 // void * type
244 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000245
246 // nullptr type (C++0x 2.14.7)
247 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000248}
249
Douglas Gregor86d142a2009-10-08 07:24:58 +0000250MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000251ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000252 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000253 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000254 = InstantiatedFromStaticDataMember.find(Var);
255 if (Pos == InstantiatedFromStaticDataMember.end())
256 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000257
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000258 return Pos->second;
259}
260
Mike Stump11289f42009-09-09 15:08:12 +0000261void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000262ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
263 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000264 assert(Inst->isStaticDataMember() && "Not a static data member");
265 assert(Tmpl->isStaticDataMember() && "Not a static data member");
266 assert(!InstantiatedFromStaticDataMember[Inst] &&
267 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000268 InstantiatedFromStaticDataMember[Inst]
269 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000270}
271
John McCalle61f2ba2009-11-18 02:36:19 +0000272NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000273ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000274 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000275 = InstantiatedFromUsingDecl.find(UUD);
276 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000277 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000278
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000279 return Pos->second;
280}
281
282void
John McCallb96ec562009-12-04 22:46:56 +0000283ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
284 assert((isa<UsingDecl>(Pattern) ||
285 isa<UnresolvedUsingValueDecl>(Pattern) ||
286 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
287 "pattern decl is not a using decl");
288 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
289 InstantiatedFromUsingDecl[Inst] = Pattern;
290}
291
292UsingShadowDecl *
293ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
294 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
295 = InstantiatedFromUsingShadowDecl.find(Inst);
296 if (Pos == InstantiatedFromUsingShadowDecl.end())
297 return 0;
298
299 return Pos->second;
300}
301
302void
303ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
304 UsingShadowDecl *Pattern) {
305 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
306 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000307}
308
Anders Carlsson5da84842009-09-01 04:26:58 +0000309FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
310 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
311 = InstantiatedFromUnnamedFieldDecl.find(Field);
312 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
313 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000314
Anders Carlsson5da84842009-09-01 04:26:58 +0000315 return Pos->second;
316}
317
318void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
319 FieldDecl *Tmpl) {
320 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
321 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
322 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
323 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000324
Anders Carlsson5da84842009-09-01 04:26:58 +0000325 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
326}
327
Douglas Gregor832940b2010-03-02 23:58:15 +0000328CXXMethodVector::iterator CXXMethodVector::begin() const {
329 if ((Storage & 0x01) == 0)
330 return reinterpret_cast<iterator>(&Storage);
331
332 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
333 return &Vec->front();
334}
335
336CXXMethodVector::iterator CXXMethodVector::end() const {
337 if ((Storage & 0x01) == 0) {
338 if (Storage == 0)
339 return reinterpret_cast<iterator>(&Storage);
340
341 return reinterpret_cast<iterator>(&Storage) + 1;
342 }
343
344 vector_type *Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
345 return &Vec->front() + Vec->size();
346}
347
348void CXXMethodVector::push_back(const CXXMethodDecl *Method) {
349 if (Storage == 0) {
350 // 0 -> 1 element.
351 Storage = reinterpret_cast<uintptr_t>(Method);
352 return;
353 }
354
355 vector_type *Vec;
356 if ((Storage & 0x01) == 0) {
357 // 1 -> 2 elements. Allocate a new vector and push the element into that
358 // vector.
359 Vec = new vector_type;
360 Vec->push_back(reinterpret_cast<const CXXMethodDecl *>(Storage));
361 Storage = reinterpret_cast<uintptr_t>(Vec) | 0x01;
362 } else
363 Vec = reinterpret_cast<vector_type *>(Storage & ~0x01);
364
365 // Add the new method to the vector.
366 Vec->push_back(Method);
367}
368
369void CXXMethodVector::Destroy() {
370 if (Storage & 0x01)
371 delete reinterpret_cast<vector_type *>(Storage & ~0x01);
372
373 Storage = 0;
374}
375
376
377ASTContext::overridden_cxx_method_iterator
378ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
379 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
380 = OverriddenMethods.find(Method);
381 if (Pos == OverriddenMethods.end())
382 return 0;
383
384 return Pos->second.begin();
385}
386
387ASTContext::overridden_cxx_method_iterator
388ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
389 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
390 = OverriddenMethods.find(Method);
391 if (Pos == OverriddenMethods.end())
392 return 0;
393
394 return Pos->second.end();
395}
396
397void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
398 const CXXMethodDecl *Overridden) {
399 OverriddenMethods[Method].push_back(Overridden);
400}
401
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000402namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000403 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000404 : std::binary_function<SourceRange, SourceRange, bool> {
405 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000406
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000407 public:
408 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000409
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000410 bool operator()(SourceRange X, SourceRange Y) {
411 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
412 }
413 };
414}
415
Chris Lattner53cfe802007-07-18 17:52:12 +0000416//===----------------------------------------------------------------------===//
417// Type Sizing and Analysis
418//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000419
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000420/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
421/// scalar floating point type.
422const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000423 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000424 assert(BT && "Not a floating point type!");
425 switch (BT->getKind()) {
426 default: assert(0 && "Not a floating point type!");
427 case BuiltinType::Float: return Target.getFloatFormat();
428 case BuiltinType::Double: return Target.getDoubleFormat();
429 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
430 }
431}
432
Ken Dyck160146e2010-01-27 17:10:57 +0000433/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000434/// specified decl. Note that bitfields do not have a valid alignment, so
435/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000436/// If @p RefAsPointee, references are treated like their underlying type
437/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000438CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000439 unsigned Align = Target.getCharWidth();
440
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000441 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000442 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedman19a546c2009-02-22 02:56:25 +0000443
Chris Lattner68061312009-01-24 21:53:27 +0000444 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
445 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000446 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000447 if (RefAsPointee)
448 T = RT->getPointeeType();
449 else
450 T = getPointerType(RT->getPointeeType());
451 }
452 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000453 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000454 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
455 T = cast<ArrayType>(T)->getElementType();
456
457 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
458 }
Charles Davis3fc51072010-02-23 04:52:00 +0000459 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
460 // In the case of a field in a packed struct, we want the minimum
461 // of the alignment of the field and the alignment of the struct.
462 Align = std::min(Align,
463 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
464 }
Chris Lattner68061312009-01-24 21:53:27 +0000465 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000466
Ken Dyck160146e2010-01-27 17:10:57 +0000467 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000468}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000469
Chris Lattner983a8bb2007-07-13 22:13:22 +0000470/// getTypeSize - Return the size of the specified type, in bits. This method
471/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000472///
473/// FIXME: Pointers into different addr spaces could have different sizes and
474/// alignment requirements: getPointerInfo should take an AddrSpace, this
475/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000476std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000477ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000478 uint64_t Width=0;
479 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000480 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000481#define TYPE(Class, Base)
482#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000483#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000484#define DEPENDENT_TYPE(Class, Base) case Type::Class:
485#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000486 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000487 break;
488
Chris Lattner355332d2007-07-13 22:27:08 +0000489 case Type::FunctionNoProto:
490 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000491 // GCC extension: alignof(function) = 32 bits
492 Width = 0;
493 Align = 32;
494 break;
495
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000496 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000497 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000498 Width = 0;
499 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
500 break;
501
Steve Naroff5c131802007-08-30 01:06:46 +0000502 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000503 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattner37e05872008-03-05 18:54:05 +0000505 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000506 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000507 Align = EltInfo.second;
508 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000509 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000510 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000511 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000512 const VectorType *VT = cast<VectorType>(T);
513 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
514 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000515 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000516 // If the alignment is not a power of 2, round up to the next power of 2.
517 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000518 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000519 Align = llvm::NextPowerOf2(Align);
520 Width = llvm::RoundUpToAlignment(Width, Align);
521 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000522 break;
523 }
Chris Lattner647fb222007-07-18 18:26:58 +0000524
Chris Lattner7570e9c2008-03-08 08:52:55 +0000525 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000526 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000527 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000528 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000529 // GCC extension: alignof(void) = 8 bits.
530 Width = 0;
531 Align = 8;
532 break;
533
Chris Lattner6a4f7452007-12-19 19:23:28 +0000534 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000535 Width = Target.getBoolWidth();
536 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000537 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000538 case BuiltinType::Char_S:
539 case BuiltinType::Char_U:
540 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000541 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000542 Width = Target.getCharWidth();
543 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000544 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000545 case BuiltinType::WChar:
546 Width = Target.getWCharWidth();
547 Align = Target.getWCharAlign();
548 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000549 case BuiltinType::Char16:
550 Width = Target.getChar16Width();
551 Align = Target.getChar16Align();
552 break;
553 case BuiltinType::Char32:
554 Width = Target.getChar32Width();
555 Align = Target.getChar32Align();
556 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000557 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000558 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000559 Width = Target.getShortWidth();
560 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000561 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000562 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000563 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000564 Width = Target.getIntWidth();
565 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000566 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000567 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000568 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000569 Width = Target.getLongWidth();
570 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000571 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000572 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000573 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000574 Width = Target.getLongLongWidth();
575 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000576 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000577 case BuiltinType::Int128:
578 case BuiltinType::UInt128:
579 Width = 128;
580 Align = 128; // int128_t is 128-bit aligned on all targets.
581 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000582 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000583 Width = Target.getFloatWidth();
584 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000585 break;
586 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000587 Width = Target.getDoubleWidth();
588 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000589 break;
590 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000591 Width = Target.getLongDoubleWidth();
592 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000593 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000594 case BuiltinType::NullPtr:
595 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
596 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000597 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000598 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000599 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000600 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000601 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000602 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000603 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000604 case Type::BlockPointer: {
605 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
606 Width = Target.getPointerWidth(AS);
607 Align = Target.getPointerAlign(AS);
608 break;
609 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000610 case Type::LValueReference:
611 case Type::RValueReference: {
612 // alignof and sizeof should never enter this code path here, so we go
613 // the pointer route.
614 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
615 Width = Target.getPointerWidth(AS);
616 Align = Target.getPointerAlign(AS);
617 break;
618 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000619 case Type::Pointer: {
620 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000621 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000622 Align = Target.getPointerAlign(AS);
623 break;
624 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000625 case Type::MemberPointer: {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000626 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000627 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000628 getTypeInfo(getPointerDiffType());
629 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000630 if (Pointee->isFunctionType())
631 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000632 Align = PtrDiffInfo.second;
633 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000634 }
Chris Lattner647fb222007-07-18 18:26:58 +0000635 case Type::Complex: {
636 // Complex types have the same alignment as their elements, but twice the
637 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000638 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000639 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000640 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000641 Align = EltInfo.second;
642 break;
643 }
Devang Pateldbb72632008-06-04 21:54:36 +0000644 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000645 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000646 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
647 Width = Layout.getSize();
648 Align = Layout.getAlignment();
649 break;
650 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000651 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000652 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000653 const TagType *TT = cast<TagType>(T);
654
655 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000656 Width = 1;
657 Align = 1;
658 break;
659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000661 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000662 return getTypeInfo(ET->getDecl()->getIntegerType());
663
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000664 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000665 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
666 Width = Layout.getSize();
667 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000668 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000669 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000670
Chris Lattner63d2b362009-10-22 05:17:15 +0000671 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000672 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
673 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000674
Chris Lattner63d2b362009-10-22 05:17:15 +0000675 case Type::Elaborated:
676 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
677 .getTypePtr());
John McCallfcc33b02009-09-05 00:15:47 +0000678
Douglas Gregoref462e62009-04-30 17:32:17 +0000679 case Type::Typedef: {
680 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000681 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000682 Align = std::max(Aligned->getMaxAlignment(),
683 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregoref462e62009-04-30 17:32:17 +0000684 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
685 } else
686 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000687 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000688 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000689
690 case Type::TypeOfExpr:
691 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
692 .getTypePtr());
693
694 case Type::TypeOf:
695 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
696
Anders Carlsson81df7b82009-06-24 19:06:50 +0000697 case Type::Decltype:
698 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
699 .getTypePtr());
700
Douglas Gregoref462e62009-04-30 17:32:17 +0000701 case Type::QualifiedName:
702 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000703
John McCalle78aac42010-03-10 03:28:59 +0000704 case Type::InjectedClassName:
705 return getTypeInfo(cast<InjectedClassNameType>(T)
706 ->getUnderlyingType().getTypePtr());
707
Douglas Gregoref462e62009-04-30 17:32:17 +0000708 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000709 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000710 "Cannot request the size of a dependent type");
711 // FIXME: this is likely to be wrong once we support template
712 // aliases, since a template alias could refer to a typedef that
713 // has an __aligned__ attribute on it.
714 return getTypeInfo(getCanonicalType(T));
715 }
Mike Stump11289f42009-09-09 15:08:12 +0000716
Chris Lattner53cfe802007-07-18 17:52:12 +0000717 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000718 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000719}
720
Ken Dyck8c89d592009-12-22 14:23:30 +0000721/// getTypeSizeInChars - Return the size of the specified type, in characters.
722/// This method does not work on incomplete types.
723CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000724 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000725}
726CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000727 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000728}
729
Ken Dycka6046ab2010-01-26 17:25:18 +0000730/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000731/// characters. This method does not work on incomplete types.
732CharUnits ASTContext::getTypeAlignInChars(QualType T) {
733 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
734}
735CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
736 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
737}
738
Chris Lattnera3402cd2009-01-27 18:08:34 +0000739/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
740/// type for the current target in bits. This can be different than the ABI
741/// alignment in cases where it is beneficial for performance to overalign
742/// a data type.
743unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
744 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000745
746 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000747 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000748 T = CT->getElementType().getTypePtr();
749 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
750 T->isSpecificBuiltinType(BuiltinType::LongLong))
751 return std::max(ABIAlign, (unsigned)getTypeSize(T));
752
Chris Lattnera3402cd2009-01-27 18:08:34 +0000753 return ABIAlign;
754}
755
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000756static void CollectLocalObjCIvars(ASTContext *Ctx,
757 const ObjCInterfaceDecl *OI,
758 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000759 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
760 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000761 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000762 if (!IVDecl->isInvalidDecl())
763 Fields.push_back(cast<FieldDecl>(IVDecl));
764 }
765}
766
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000767void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
768 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
769 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
770 CollectObjCIvars(SuperClass, Fields);
771 CollectLocalObjCIvars(this, OI, Fields);
772}
773
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000774/// ShallowCollectObjCIvars -
775/// Collect all ivars, including those synthesized, in the current class.
776///
777void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000778 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000779 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
780 E = OI->ivar_end(); I != E; ++I) {
781 Ivars.push_back(*I);
782 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000783
784 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000785}
786
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000787/// CollectNonClassIvars -
788/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000789/// This includes synthesized ivars (via @synthesize) and those in
790// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000791///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000792void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000793 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000794 // Find ivars declared in class extension.
795 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
796 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
797 E = CDecl->ivar_end(); I != E; ++I) {
798 Ivars.push_back(*I);
799 }
800 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000801
Ted Kremenek86838aa2010-03-11 19:44:54 +0000802 // Also add any ivar defined in this class's implementation. This
803 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000804 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
805 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
806 E = ImplDecl->ivar_end(); I != E; ++I)
807 Ivars.push_back(*I);
808 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000809}
810
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000811/// CollectInheritedProtocols - Collect all protocols in current class and
812/// those inherited by it.
813void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000814 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000815 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
816 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
817 PE = OI->protocol_end(); P != PE; ++P) {
818 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000819 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000820 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000821 PE = Proto->protocol_end(); P != PE; ++P) {
822 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000823 CollectInheritedProtocols(*P, Protocols);
824 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000825 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000826
827 // Categories of this Interface.
828 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
829 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
830 CollectInheritedProtocols(CDeclChain, Protocols);
831 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
832 while (SD) {
833 CollectInheritedProtocols(SD, Protocols);
834 SD = SD->getSuperClass();
835 }
836 return;
837 }
838 if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
839 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
840 PE = OC->protocol_end(); P != PE; ++P) {
841 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000842 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000843 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
844 PE = Proto->protocol_end(); P != PE; ++P)
845 CollectInheritedProtocols(*P, Protocols);
846 }
847 return;
848 }
849 if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
850 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
851 PE = OP->protocol_end(); P != PE; ++P) {
852 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000853 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000854 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
855 PE = Proto->protocol_end(); P != PE; ++P)
856 CollectInheritedProtocols(*P, Protocols);
857 }
858 return;
859 }
860}
861
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000862unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
863 unsigned count = 0;
864 // Count ivars declared in class extension.
865 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
866 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
867 E = CDecl->ivar_end(); I != E; ++I) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000868 ++count;
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000869 }
870 }
871
872 // Count ivar defined in this class's implementation. This
873 // includes synthesized ivars.
874 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
875 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
876 E = ImplDecl->ivar_end(); I != E; ++I)
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000877 ++count;
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000878 return count;
879}
880
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000881/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
882ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
883 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
884 I = ObjCImpls.find(D);
885 if (I != ObjCImpls.end())
886 return cast<ObjCImplementationDecl>(I->second);
887 return 0;
888}
889/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
890ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
891 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
892 I = ObjCImpls.find(D);
893 if (I != ObjCImpls.end())
894 return cast<ObjCCategoryImplDecl>(I->second);
895 return 0;
896}
897
898/// \brief Set the implementation of ObjCInterfaceDecl.
899void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
900 ObjCImplementationDecl *ImplD) {
901 assert(IFaceD && ImplD && "Passed null params");
902 ObjCImpls[IFaceD] = ImplD;
903}
904/// \brief Set the implementation of ObjCCategoryDecl.
905void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
906 ObjCCategoryImplDecl *ImplD) {
907 assert(CatD && ImplD && "Passed null params");
908 ObjCImpls[CatD] = ImplD;
909}
910
John McCallbcd03502009-12-07 02:54:59 +0000911/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000912///
John McCallbcd03502009-12-07 02:54:59 +0000913/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000914/// the TypeLoc wrappers.
915///
916/// \param T the type that will be the basis for type source info. This type
917/// should refer to how the declarator was written in source code, not to
918/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000919TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000920 unsigned DataSize) {
921 if (!DataSize)
922 DataSize = TypeLoc::getFullDataSizeForType(T);
923 else
924 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000925 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000926
John McCallbcd03502009-12-07 02:54:59 +0000927 TypeSourceInfo *TInfo =
928 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
929 new (TInfo) TypeSourceInfo(T);
930 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000931}
932
John McCallbcd03502009-12-07 02:54:59 +0000933TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000934 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +0000935 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +0000936 DI->getTypeLoc().initialize(L);
937 return DI;
938}
939
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000940/// getInterfaceLayoutImpl - Get or compute information about the
941/// layout of the given interface.
942///
943/// \param Impl - If given, also include the layout of the interface's
944/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000945const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000946ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
947 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000948 assert(!D->isForwardDecl() && "Invalid interface decl!");
949
Devang Pateldbb72632008-06-04 21:54:36 +0000950 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000951 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000952 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
953 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
954 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000955
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000956 // Add in synthesized ivar count if laying out an implementation.
957 if (Impl) {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000958 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000959 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000960 // entry. Note we can't cache this because we simply free all
961 // entries later; however we shouldn't look up implementations
962 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000963 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000964 return getObjCLayout(D, 0);
965 }
966
Mike Stump11289f42009-09-09 15:08:12 +0000967 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000968 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
969 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000970
Devang Pateldbb72632008-06-04 21:54:36 +0000971 return *NewEntry;
972}
973
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000974const ASTRecordLayout &
975ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
976 return getObjCLayout(D, 0);
977}
978
979const ASTRecordLayout &
980ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
981 return getObjCLayout(D->getClassInterface(), D);
982}
983
Devang Patele11664a2007-11-01 19:11:01 +0000984/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +0000985/// specified record (struct/union/class), which indicates its size and field
986/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +0000987const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000988 D = D->getDefinition();
Ted Kremenek21475702008-09-05 17:16:31 +0000989 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +0000990
Chris Lattner53cfe802007-07-18 17:52:12 +0000991 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +0000992 // Note that we can't save a reference to the entry because this function
993 // is recursive.
994 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +0000995 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +0000996
Mike Stump11289f42009-09-09 15:08:12 +0000997 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000998 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +0000999 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +00001000
Daniel Dunbarccabe482010-04-19 20:44:53 +00001001 if (getLangOptions().DumpRecordLayouts) {
1002 llvm::errs() << "\n*** Dumping AST Record Layout\n";
1003 DumpRecordLayout(D, llvm::errs());
1004 }
1005
Chris Lattner647fb222007-07-18 18:26:58 +00001006 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +00001007}
1008
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001009const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001010 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001011 assert(RD && "Cannot get key function for forward declarations!");
1012
1013 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1014 if (!Entry)
1015 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
1016 else
1017 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
1018 "Key function changed!");
1019
1020 return Entry;
1021}
1022
Chris Lattner983a8bb2007-07-13 22:13:22 +00001023//===----------------------------------------------------------------------===//
1024// Type creation/memoization methods
1025//===----------------------------------------------------------------------===//
1026
John McCall8ccfcb52009-09-24 19:53:00 +00001027QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1028 unsigned Fast = Quals.getFastQualifiers();
1029 Quals.removeFastQualifiers();
1030
1031 // Check if we've already instantiated this type.
1032 llvm::FoldingSetNodeID ID;
1033 ExtQuals::Profile(ID, TypeNode, Quals);
1034 void *InsertPos = 0;
1035 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1036 assert(EQ->getQualifiers() == Quals);
1037 QualType T = QualType(EQ, Fast);
1038 return T;
1039 }
1040
John McCall90d1c2d2009-09-24 23:30:46 +00001041 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001042 ExtQualNodes.InsertNode(New, InsertPos);
1043 QualType T = QualType(New, Fast);
1044 return T;
1045}
1046
1047QualType ASTContext::getVolatileType(QualType T) {
1048 QualType CanT = getCanonicalType(T);
1049 if (CanT.isVolatileQualified()) return T;
1050
1051 QualifierCollector Quals;
1052 const Type *TypeNode = Quals.strip(T);
1053 Quals.addVolatile();
1054
1055 return getExtQualType(TypeNode, Quals);
1056}
1057
Fariborz Jahanianece85822009-02-17 18:27:45 +00001058QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001059 QualType CanT = getCanonicalType(T);
1060 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001061 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001062
John McCall8ccfcb52009-09-24 19:53:00 +00001063 // If we are composing extended qualifiers together, merge together
1064 // into one ExtQuals node.
1065 QualifierCollector Quals;
1066 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001067
John McCall8ccfcb52009-09-24 19:53:00 +00001068 // If this type already has an address space specified, it cannot get
1069 // another one.
1070 assert(!Quals.hasAddressSpace() &&
1071 "Type cannot be in multiple addr spaces!");
1072 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001073
John McCall8ccfcb52009-09-24 19:53:00 +00001074 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001075}
1076
Chris Lattnerd60183d2009-02-18 22:53:11 +00001077QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001078 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001079 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001080 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001081 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001082
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001083 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001084 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001085 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001086 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1087 return getPointerType(ResultType);
1088 }
1089 }
Mike Stump11289f42009-09-09 15:08:12 +00001090
John McCall8ccfcb52009-09-24 19:53:00 +00001091 // If we are composing extended qualifiers together, merge together
1092 // into one ExtQuals node.
1093 QualifierCollector Quals;
1094 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001095
John McCall8ccfcb52009-09-24 19:53:00 +00001096 // If this type already has an ObjCGC specified, it cannot get
1097 // another one.
1098 assert(!Quals.hasObjCGCAttr() &&
1099 "Type cannot have multiple ObjCGCs!");
1100 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001101
John McCall8ccfcb52009-09-24 19:53:00 +00001102 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001103}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001104
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001105static QualType getExtFunctionType(ASTContext& Context, QualType T,
1106 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001107 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001108 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1109 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001110 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001111 if (ResultType == Pointee)
1112 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001113
1114 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001115 } else if (const BlockPointerType *BlockPointer
1116 = T->getAs<BlockPointerType>()) {
1117 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001118 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001119 if (ResultType == Pointee)
1120 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001121
1122 ResultType = Context.getBlockPointerType(ResultType);
1123 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001124 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001125 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001126
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001127 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001128 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001129 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001130 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001131 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001132 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001133 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1134 FPT->getNumArgs(), FPT->isVariadic(),
1135 FPT->getTypeQuals(),
1136 FPT->hasExceptionSpec(),
1137 FPT->hasAnyExceptionSpec(),
1138 FPT->getNumExceptions(),
1139 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001140 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001141 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001142 } else
1143 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001144
1145 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1146}
1147
1148QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001149 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001150 return getExtFunctionType(*this, T,
1151 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001152}
1153
1154QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001155 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001156 return getExtFunctionType(*this, T,
1157 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001158}
1159
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001160QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1161 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1162 return getExtFunctionType(*this, T,
1163 Info.withRegParm(RegParm));
1164}
1165
Chris Lattnerc6395932007-06-22 20:56:16 +00001166/// getComplexType - Return the uniqued reference to the type for a complex
1167/// number with the specified element type.
1168QualType ASTContext::getComplexType(QualType T) {
1169 // Unique pointers, to guarantee there is only one pointer of a particular
1170 // structure.
1171 llvm::FoldingSetNodeID ID;
1172 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001173
Chris Lattnerc6395932007-06-22 20:56:16 +00001174 void *InsertPos = 0;
1175 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1176 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001177
Chris Lattnerc6395932007-06-22 20:56:16 +00001178 // If the pointee type isn't canonical, this won't be a canonical type either,
1179 // so fill in the canonical type field.
1180 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001181 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001182 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001183
Chris Lattnerc6395932007-06-22 20:56:16 +00001184 // Get the new insert position for the node we care about.
1185 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001186 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001187 }
John McCall90d1c2d2009-09-24 23:30:46 +00001188 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001189 Types.push_back(New);
1190 ComplexTypes.InsertNode(New, InsertPos);
1191 return QualType(New, 0);
1192}
1193
Chris Lattner970e54e2006-11-12 00:37:36 +00001194/// getPointerType - Return the uniqued reference to the type for a pointer to
1195/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001196QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001197 // Unique pointers, to guarantee there is only one pointer of a particular
1198 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001199 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001200 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001201
Chris Lattner67521df2007-01-27 01:29:36 +00001202 void *InsertPos = 0;
1203 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001204 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001205
Chris Lattner7ccecb92006-11-12 08:50:50 +00001206 // If the pointee type isn't canonical, this won't be a canonical type either,
1207 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001208 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001209 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001210 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001211
Chris Lattner67521df2007-01-27 01:29:36 +00001212 // Get the new insert position for the node we care about.
1213 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001214 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001215 }
John McCall90d1c2d2009-09-24 23:30:46 +00001216 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001217 Types.push_back(New);
1218 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001219 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001220}
1221
Mike Stump11289f42009-09-09 15:08:12 +00001222/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001223/// a pointer to the specified block.
1224QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001225 assert(T->isFunctionType() && "block of function types only");
1226 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001227 // structure.
1228 llvm::FoldingSetNodeID ID;
1229 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001230
Steve Naroffec33ed92008-08-27 16:04:49 +00001231 void *InsertPos = 0;
1232 if (BlockPointerType *PT =
1233 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1234 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001235
1236 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001237 // type either so fill in the canonical type field.
1238 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001239 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001240 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001241
Steve Naroffec33ed92008-08-27 16:04:49 +00001242 // Get the new insert position for the node we care about.
1243 BlockPointerType *NewIP =
1244 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001245 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001246 }
John McCall90d1c2d2009-09-24 23:30:46 +00001247 BlockPointerType *New
1248 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001249 Types.push_back(New);
1250 BlockPointerTypes.InsertNode(New, InsertPos);
1251 return QualType(New, 0);
1252}
1253
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001254/// getLValueReferenceType - Return the uniqued reference to the type for an
1255/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001256QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001257 // Unique pointers, to guarantee there is only one pointer of a particular
1258 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001259 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001260 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001261
1262 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001263 if (LValueReferenceType *RT =
1264 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001265 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001266
John McCallfc93cf92009-10-22 22:37:11 +00001267 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1268
Bill Wendling3708c182007-05-27 10:15:43 +00001269 // If the referencee type isn't canonical, this won't be a canonical type
1270 // either, so fill in the canonical type field.
1271 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001272 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1273 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1274 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001275
Bill Wendling3708c182007-05-27 10:15:43 +00001276 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001277 LValueReferenceType *NewIP =
1278 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001279 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001280 }
1281
John McCall90d1c2d2009-09-24 23:30:46 +00001282 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001283 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1284 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001285 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001286 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001287
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001288 return QualType(New, 0);
1289}
1290
1291/// getRValueReferenceType - Return the uniqued reference to the type for an
1292/// rvalue reference to the specified type.
1293QualType ASTContext::getRValueReferenceType(QualType T) {
1294 // Unique pointers, to guarantee there is only one pointer of a particular
1295 // structure.
1296 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001297 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001298
1299 void *InsertPos = 0;
1300 if (RValueReferenceType *RT =
1301 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1302 return QualType(RT, 0);
1303
John McCallfc93cf92009-10-22 22:37:11 +00001304 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1305
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001306 // If the referencee type isn't canonical, this won't be a canonical type
1307 // either, so fill in the canonical type field.
1308 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001309 if (InnerRef || !T.isCanonical()) {
1310 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1311 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001312
1313 // Get the new insert position for the node we care about.
1314 RValueReferenceType *NewIP =
1315 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1316 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1317 }
1318
John McCall90d1c2d2009-09-24 23:30:46 +00001319 RValueReferenceType *New
1320 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001321 Types.push_back(New);
1322 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001323 return QualType(New, 0);
1324}
1325
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001326/// getMemberPointerType - Return the uniqued reference to the type for a
1327/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001328QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001329 // Unique pointers, to guarantee there is only one pointer of a particular
1330 // structure.
1331 llvm::FoldingSetNodeID ID;
1332 MemberPointerType::Profile(ID, T, Cls);
1333
1334 void *InsertPos = 0;
1335 if (MemberPointerType *PT =
1336 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1337 return QualType(PT, 0);
1338
1339 // If the pointee or class type isn't canonical, this won't be a canonical
1340 // type either, so fill in the canonical type field.
1341 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001342 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001343 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1344
1345 // Get the new insert position for the node we care about.
1346 MemberPointerType *NewIP =
1347 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1348 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1349 }
John McCall90d1c2d2009-09-24 23:30:46 +00001350 MemberPointerType *New
1351 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001352 Types.push_back(New);
1353 MemberPointerTypes.InsertNode(New, InsertPos);
1354 return QualType(New, 0);
1355}
1356
Mike Stump11289f42009-09-09 15:08:12 +00001357/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001358/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001359QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001360 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001361 ArrayType::ArraySizeModifier ASM,
1362 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001363 assert((EltTy->isDependentType() ||
1364 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001365 "Constant array of VLAs is illegal!");
1366
Chris Lattnere2df3f92009-05-13 04:12:56 +00001367 // Convert the array size into a canonical width matching the pointer size for
1368 // the target.
1369 llvm::APInt ArySize(ArySizeIn);
1370 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattner23b7eb62007-06-15 23:05:46 +00001372 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001373 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001374
Chris Lattner36f8e652007-01-27 08:31:04 +00001375 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001376 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001377 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001378 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001379
Chris Lattner7ccecb92006-11-12 08:50:50 +00001380 // If the element type isn't canonical, this won't be a canonical type either,
1381 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001382 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001383 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001384 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001385 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001386 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001387 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001388 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001389 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001390 }
Mike Stump11289f42009-09-09 15:08:12 +00001391
John McCall90d1c2d2009-09-24 23:30:46 +00001392 ConstantArrayType *New = new(*this,TypeAlignment)
1393 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001394 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001395 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001396 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001397}
1398
Steve Naroffcadebd02007-08-30 18:14:25 +00001399/// getVariableArrayType - Returns a non-unique reference to the type for a
1400/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001401QualType ASTContext::getVariableArrayType(QualType EltTy,
1402 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001403 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001404 unsigned EltTypeQuals,
1405 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001406 // Since we don't unique expressions, it isn't possible to unique VLA's
1407 // that have an expression provided for their size.
1408
John McCall90d1c2d2009-09-24 23:30:46 +00001409 VariableArrayType *New = new(*this, TypeAlignment)
1410 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001411
1412 VariableArrayTypes.push_back(New);
1413 Types.push_back(New);
1414 return QualType(New, 0);
1415}
1416
Douglas Gregor4619e432008-12-05 23:32:09 +00001417/// getDependentSizedArrayType - Returns a non-unique reference to
1418/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001419/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001420QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1421 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001422 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001423 unsigned EltTypeQuals,
1424 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001425 assert((!NumElts || NumElts->isTypeDependent() ||
1426 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001427 "Size must be type- or value-dependent!");
1428
Douglas Gregorf3f95522009-07-31 00:23:35 +00001429 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001430 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001431 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001432
1433 if (NumElts) {
1434 // Dependently-sized array types that do not have a specified
1435 // number of elements will have their sizes deduced from an
1436 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001437 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1438 EltTypeQuals, NumElts);
1439
1440 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1441 }
1442
Douglas Gregorf3f95522009-07-31 00:23:35 +00001443 DependentSizedArrayType *New;
1444 if (Canon) {
1445 // We already have a canonical version of this array type; use it as
1446 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001447 New = new (*this, TypeAlignment)
1448 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1449 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001450 } else {
1451 QualType CanonEltTy = getCanonicalType(EltTy);
1452 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001453 New = new (*this, TypeAlignment)
1454 DependentSizedArrayType(*this, EltTy, QualType(),
1455 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001456
Douglas Gregorc42075a2010-02-04 18:10:26 +00001457 if (NumElts) {
1458 DependentSizedArrayType *CanonCheck
1459 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1460 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1461 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001462 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001463 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001464 } else {
1465 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1466 ASM, EltTypeQuals,
1467 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001468 New = new (*this, TypeAlignment)
1469 DependentSizedArrayType(*this, EltTy, Canon,
1470 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001471 }
1472 }
Mike Stump11289f42009-09-09 15:08:12 +00001473
Douglas Gregor4619e432008-12-05 23:32:09 +00001474 Types.push_back(New);
1475 return QualType(New, 0);
1476}
1477
Eli Friedmanbd258282008-02-15 18:16:39 +00001478QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1479 ArrayType::ArraySizeModifier ASM,
1480 unsigned EltTypeQuals) {
1481 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001482 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001483
1484 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001485 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001486 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1487 return QualType(ATP, 0);
1488
1489 // If the element type isn't canonical, this won't be a canonical type
1490 // either, so fill in the canonical type field.
1491 QualType Canonical;
1492
John McCallb692a092009-10-22 20:10:53 +00001493 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001494 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001495 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001496
1497 // Get the new insert position for the node we care about.
1498 IncompleteArrayType *NewIP =
1499 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001500 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001501 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001502
John McCall90d1c2d2009-09-24 23:30:46 +00001503 IncompleteArrayType *New = new (*this, TypeAlignment)
1504 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001505
1506 IncompleteArrayTypes.InsertNode(New, InsertPos);
1507 Types.push_back(New);
1508 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001509}
1510
Steve Naroff91fcddb2007-07-18 18:00:27 +00001511/// getVectorType - Return the unique reference to a vector type of
1512/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001513QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1514 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001515 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001516
Chris Lattner76a00cf2008-04-06 22:59:24 +00001517 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001518 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001519
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001520 // Check if we've already instantiated a vector of this type.
1521 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001522 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1523 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001524 void *InsertPos = 0;
1525 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1526 return QualType(VTP, 0);
1527
1528 // If the element type isn't canonical, this won't be a canonical type either,
1529 // so fill in the canonical type field.
1530 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001531 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1532 Canonical = getVectorType(getCanonicalType(vecType),
1533 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001534
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001535 // Get the new insert position for the node we care about.
1536 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001537 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001538 }
John McCall90d1c2d2009-09-24 23:30:46 +00001539 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001540 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001541 VectorTypes.InsertNode(New, InsertPos);
1542 Types.push_back(New);
1543 return QualType(New, 0);
1544}
1545
Nate Begemance4d7fc2008-04-18 23:10:10 +00001546/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001547/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001548QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001549 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001550
Chris Lattner76a00cf2008-04-06 22:59:24 +00001551 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001552 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001553
Steve Naroff91fcddb2007-07-18 18:00:27 +00001554 // Check if we've already instantiated a vector of this type.
1555 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001556 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001557 void *InsertPos = 0;
1558 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1559 return QualType(VTP, 0);
1560
1561 // If the element type isn't canonical, this won't be a canonical type either,
1562 // so fill in the canonical type field.
1563 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001564 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001565 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001566
Steve Naroff91fcddb2007-07-18 18:00:27 +00001567 // Get the new insert position for the node we care about.
1568 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001569 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001570 }
John McCall90d1c2d2009-09-24 23:30:46 +00001571 ExtVectorType *New = new (*this, TypeAlignment)
1572 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001573 VectorTypes.InsertNode(New, InsertPos);
1574 Types.push_back(New);
1575 return QualType(New, 0);
1576}
1577
Mike Stump11289f42009-09-09 15:08:12 +00001578QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001579 Expr *SizeExpr,
1580 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001581 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001582 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001583 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001584
Douglas Gregor352169a2009-07-31 03:54:25 +00001585 void *InsertPos = 0;
1586 DependentSizedExtVectorType *Canon
1587 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1588 DependentSizedExtVectorType *New;
1589 if (Canon) {
1590 // We already have a canonical version of this array type; use it as
1591 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001592 New = new (*this, TypeAlignment)
1593 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1594 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001595 } else {
1596 QualType CanonVecTy = getCanonicalType(vecType);
1597 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001598 New = new (*this, TypeAlignment)
1599 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1600 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001601
1602 DependentSizedExtVectorType *CanonCheck
1603 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1604 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1605 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001606 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1607 } else {
1608 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1609 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001610 New = new (*this, TypeAlignment)
1611 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001612 }
1613 }
Mike Stump11289f42009-09-09 15:08:12 +00001614
Douglas Gregor758a8692009-06-17 21:51:59 +00001615 Types.push_back(New);
1616 return QualType(New, 0);
1617}
1618
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001619/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001620///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001621QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1622 const FunctionType::ExtInfo &Info) {
1623 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001624 // Unique functions, to guarantee there is only one function of a particular
1625 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001626 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001627 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001628
Chris Lattner47955de2007-01-27 08:37:20 +00001629 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001630 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001631 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001632 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001633
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001634 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001635 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001636 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001637 Canonical =
1638 getFunctionNoProtoType(getCanonicalType(ResultTy),
1639 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001640
Chris Lattner47955de2007-01-27 08:37:20 +00001641 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001642 FunctionNoProtoType *NewIP =
1643 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001644 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001645 }
Mike Stump11289f42009-09-09 15:08:12 +00001646
John McCall90d1c2d2009-09-24 23:30:46 +00001647 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001648 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001649 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001650 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001651 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001652}
1653
1654/// getFunctionType - Return a normal function type with a typed argument
1655/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001656QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001657 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001658 unsigned TypeQuals, bool hasExceptionSpec,
1659 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001660 const QualType *ExArray,
1661 const FunctionType::ExtInfo &Info) {
1662 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001663 // Unique functions, to guarantee there is only one function of a particular
1664 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001665 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001666 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001667 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001668 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001669
1670 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001671 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001672 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001673 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001674
1675 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001676 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001677 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001678 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001679 isCanonical = false;
1680
1681 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001682 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001683 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001684 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001685 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001686 CanonicalArgs.reserve(NumArgs);
1687 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001688 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001689
Chris Lattner76a00cf2008-04-06 22:59:24 +00001690 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001691 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001692 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001693 false, 0, 0,
1694 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001695
Chris Lattnerfd4de792007-01-27 01:15:32 +00001696 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001697 FunctionProtoType *NewIP =
1698 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001699 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001700 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001701
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001702 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001703 // for two variable size arrays (for parameter and exception types) at the
1704 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001705 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001706 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1707 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001708 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001709 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001710 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001711 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001712 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001713 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001714 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001715}
Chris Lattneref51c202006-11-10 07:17:23 +00001716
John McCalle78aac42010-03-10 03:28:59 +00001717#ifndef NDEBUG
1718static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1719 if (!isa<CXXRecordDecl>(D)) return false;
1720 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1721 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1722 return true;
1723 if (RD->getDescribedClassTemplate() &&
1724 !isa<ClassTemplateSpecializationDecl>(RD))
1725 return true;
1726 return false;
1727}
1728#endif
1729
1730/// getInjectedClassNameType - Return the unique reference to the
1731/// injected class name type for the specified templated declaration.
1732QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1733 QualType TST) {
1734 assert(NeedsInjectedClassNameType(Decl));
1735 if (Decl->TypeForDecl) {
1736 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1737 } else if (CXXRecordDecl *PrevDecl
1738 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1739 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1740 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1741 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1742 } else {
1743 Decl->TypeForDecl = new (*this, TypeAlignment)
1744 InjectedClassNameType(Decl, TST, TST->getCanonicalTypeInternal());
1745 Types.push_back(Decl->TypeForDecl);
1746 }
1747 return QualType(Decl->TypeForDecl, 0);
1748}
1749
Douglas Gregor83a586e2008-04-13 21:07:44 +00001750/// getTypeDeclType - Return the unique reference to the type for the
1751/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001752QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001753 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001754 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001755
John McCall81e38502010-02-16 03:57:14 +00001756 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001757 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001758
1759 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001760 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001761 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001762
John McCall96f0b5f2010-03-10 06:48:02 +00001763 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1764 "Template type parameter types are always available.");
1765
John McCall81e38502010-02-16 03:57:14 +00001766 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001767 assert(!Record->getPreviousDeclaration() &&
1768 "struct/union has previous declaration");
1769 assert(!NeedsInjectedClassNameType(Record));
1770 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001771 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001772 assert(!Enum->getPreviousDeclaration() &&
1773 "enum has previous declaration");
1774 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001775 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001776 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1777 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001778 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001779 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001780
John McCall96f0b5f2010-03-10 06:48:02 +00001781 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001782 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001783}
1784
Chris Lattner32d920b2007-01-26 02:01:53 +00001785/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001786/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001787QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001788 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001789
Chris Lattner76a00cf2008-04-06 22:59:24 +00001790 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001791 Decl->TypeForDecl = new(*this, TypeAlignment)
1792 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001793 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001794 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001795}
1796
John McCallcebee162009-10-18 09:09:24 +00001797/// \brief Retrieve a substitution-result type.
1798QualType
1799ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1800 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001801 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001802 && "replacement types must always be canonical");
1803
1804 llvm::FoldingSetNodeID ID;
1805 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1806 void *InsertPos = 0;
1807 SubstTemplateTypeParmType *SubstParm
1808 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1809
1810 if (!SubstParm) {
1811 SubstParm = new (*this, TypeAlignment)
1812 SubstTemplateTypeParmType(Parm, Replacement);
1813 Types.push_back(SubstParm);
1814 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1815 }
1816
1817 return QualType(SubstParm, 0);
1818}
1819
Douglas Gregoreff93e02009-02-05 23:33:38 +00001820/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001821/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001822/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001823QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001824 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001825 IdentifierInfo *Name) {
1826 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001827 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001828 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001829 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001830 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1831
1832 if (TypeParm)
1833 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001834
Anders Carlsson90036dc2009-06-16 00:30:48 +00001835 if (Name) {
1836 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001837 TypeParm = new (*this, TypeAlignment)
1838 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001839
1840 TemplateTypeParmType *TypeCheck
1841 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1842 assert(!TypeCheck && "Template type parameter canonical type broken");
1843 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001844 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001845 TypeParm = new (*this, TypeAlignment)
1846 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001847
1848 Types.push_back(TypeParm);
1849 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1850
1851 return QualType(TypeParm, 0);
1852}
1853
John McCalle78aac42010-03-10 03:28:59 +00001854TypeSourceInfo *
1855ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1856 SourceLocation NameLoc,
1857 const TemplateArgumentListInfo &Args,
1858 QualType CanonType) {
1859 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1860
1861 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1862 TemplateSpecializationTypeLoc TL
1863 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1864 TL.setTemplateNameLoc(NameLoc);
1865 TL.setLAngleLoc(Args.getLAngleLoc());
1866 TL.setRAngleLoc(Args.getRAngleLoc());
1867 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1868 TL.setArgLocInfo(i, Args[i].getLocInfo());
1869 return DI;
1870}
1871
Mike Stump11289f42009-09-09 15:08:12 +00001872QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001873ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001874 const TemplateArgumentListInfo &Args,
John McCall0ad16662009-10-29 08:12:44 +00001875 QualType Canon) {
John McCall6b51f282009-11-23 01:53:49 +00001876 unsigned NumArgs = Args.size();
1877
John McCall0ad16662009-10-29 08:12:44 +00001878 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1879 ArgVec.reserve(NumArgs);
1880 for (unsigned i = 0; i != NumArgs; ++i)
1881 ArgVec.push_back(Args[i].getArgument());
1882
1883 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon);
1884}
1885
1886QualType
1887ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001888 const TemplateArgument *Args,
1889 unsigned NumArgs,
1890 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001891 if (!Canon.isNull())
1892 Canon = getCanonicalType(Canon);
1893 else {
1894 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001895 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1896 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1897 CanonArgs.reserve(NumArgs);
1898 for (unsigned I = 0; I != NumArgs; ++I)
1899 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1900
1901 // Determine whether this canonical template specialization type already
1902 // exists.
1903 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001904 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor00044172009-07-29 16:09:57 +00001905 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001906
1907 void *InsertPos = 0;
1908 TemplateSpecializationType *Spec
1909 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001910
Douglas Gregora8e02e72009-07-28 23:00:59 +00001911 if (!Spec) {
1912 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001913 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001914 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001915 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001916 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001917 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001918 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001919 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001920 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001921 }
Mike Stump11289f42009-09-09 15:08:12 +00001922
Douglas Gregor15301382009-07-30 17:40:51 +00001923 if (Canon.isNull())
1924 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001925 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001926 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001927 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001928
Douglas Gregora8e02e72009-07-28 23:00:59 +00001929 // Allocate the (non-canonical) template specialization type, but don't
1930 // try to unique it: these types typically have location information that
1931 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001932 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001933 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001934 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001935 TemplateSpecializationType *Spec
1936 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001937 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001938
Douglas Gregor8bf42052009-02-09 18:46:07 +00001939 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001940 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001941}
1942
Mike Stump11289f42009-09-09 15:08:12 +00001943QualType
Douglas Gregorf21eb492009-03-26 23:50:42 +00001944ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor52537682009-03-19 00:18:19 +00001945 QualType NamedType) {
1946 llvm::FoldingSetNodeID ID;
Douglas Gregorf21eb492009-03-26 23:50:42 +00001947 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001948
1949 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001950 QualifiedNameType *T
Douglas Gregor52537682009-03-19 00:18:19 +00001951 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1952 if (T)
1953 return QualType(T, 0);
1954
Douglas Gregorc42075a2010-02-04 18:10:26 +00001955 QualType Canon = NamedType;
1956 if (!Canon.isCanonical()) {
1957 Canon = getCanonicalType(NamedType);
1958 QualifiedNameType *CheckT
1959 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1960 assert(!CheckT && "Qualified name canonical type broken");
1961 (void)CheckT;
1962 }
1963
1964 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001965 Types.push_back(T);
1966 QualifiedNameTypes.InsertNode(T, InsertPos);
1967 return QualType(T, 0);
1968}
1969
Douglas Gregor02085352010-03-31 20:19:30 +00001970QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1971 NestedNameSpecifier *NNS,
1972 const IdentifierInfo *Name,
1973 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001974 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1975
1976 if (Canon.isNull()) {
1977 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001978 ElaboratedTypeKeyword CanonKeyword = Keyword;
1979 if (Keyword == ETK_None)
1980 CanonKeyword = ETK_Typename;
1981
1982 if (CanonNNS != NNS || CanonKeyword != Keyword)
1983 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001984 }
1985
1986 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001987 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001988
1989 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001990 DependentNameType *T
1991 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001992 if (T)
1993 return QualType(T, 0);
1994
Douglas Gregor02085352010-03-31 20:19:30 +00001995 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00001996 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001997 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001998 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001999}
2000
Mike Stump11289f42009-09-09 15:08:12 +00002001QualType
Douglas Gregor02085352010-03-31 20:19:30 +00002002ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
2003 NestedNameSpecifier *NNS,
2004 const TemplateSpecializationType *TemplateId,
2005 QualType Canon) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002006 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2007
Douglas Gregorc42075a2010-02-04 18:10:26 +00002008 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00002009 DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002010
2011 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002012 DependentNameType *T
2013 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002014 if (T)
2015 return QualType(T, 0);
2016
Douglas Gregordce2b622009-04-01 00:28:59 +00002017 if (Canon.isNull()) {
2018 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2019 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
Douglas Gregor02085352010-03-31 20:19:30 +00002020 ElaboratedTypeKeyword CanonKeyword = Keyword;
2021 if (Keyword == ETK_None)
2022 CanonKeyword = ETK_Typename;
2023 if (CanonNNS != NNS || CanonKeyword != Keyword ||
2024 CanonType != QualType(TemplateId, 0)) {
Douglas Gregordce2b622009-04-01 00:28:59 +00002025 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00002026 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00002027 assert(CanonTemplateId &&
2028 "Canonical type must also be a template specialization type");
Douglas Gregor02085352010-03-31 20:19:30 +00002029 Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
Douglas Gregordce2b622009-04-01 00:28:59 +00002030 }
Douglas Gregorc42075a2010-02-04 18:10:26 +00002031
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002032 DependentNameType *CheckT
2033 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00002034 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregordce2b622009-04-01 00:28:59 +00002035 }
2036
Douglas Gregor02085352010-03-31 20:19:30 +00002037 T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00002038 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002039 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00002040 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00002041}
2042
John McCallfcc33b02009-09-05 00:15:47 +00002043QualType
2044ASTContext::getElaboratedType(QualType UnderlyingType,
2045 ElaboratedType::TagKind Tag) {
2046 llvm::FoldingSetNodeID ID;
2047 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump11289f42009-09-09 15:08:12 +00002048
John McCallfcc33b02009-09-05 00:15:47 +00002049 void *InsertPos = 0;
2050 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2051 if (T)
2052 return QualType(T, 0);
2053
Douglas Gregorc42075a2010-02-04 18:10:26 +00002054 QualType Canon = UnderlyingType;
2055 if (!Canon.isCanonical()) {
2056 Canon = getCanonicalType(Canon);
2057 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2058 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2059 }
John McCallfcc33b02009-09-05 00:15:47 +00002060
2061 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2062 Types.push_back(T);
2063 ElaboratedTypes.InsertNode(T, InsertPos);
2064 return QualType(T, 0);
2065}
2066
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002067/// CmpProtocolNames - Comparison predicate for sorting protocols
2068/// alphabetically.
2069static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2070 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002071 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002072}
2073
John McCallfc93cf92009-10-22 22:37:11 +00002074static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2075 unsigned NumProtocols) {
2076 if (NumProtocols == 0) return true;
2077
2078 for (unsigned i = 1; i != NumProtocols; ++i)
2079 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2080 return false;
2081 return true;
2082}
2083
2084static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002085 unsigned &NumProtocols) {
2086 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002087
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002088 // Sort protocols, keyed by name.
2089 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2090
2091 // Remove duplicates.
2092 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2093 NumProtocols = ProtocolsEnd-Protocols;
2094}
2095
Steve Narofffb4330f2009-06-17 22:40:22 +00002096/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2097/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002098QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00002099 ObjCProtocolDecl **Protocols,
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002100 unsigned NumProtocols,
2101 unsigned Quals) {
Steve Narofffb4330f2009-06-17 22:40:22 +00002102 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002103 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002104 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Narofffb4330f2009-06-17 22:40:22 +00002105
2106 void *InsertPos = 0;
2107 if (ObjCObjectPointerType *QT =
2108 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002109 return getQualifiedType(QualType(QT, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002110
John McCallfc93cf92009-10-22 22:37:11 +00002111 // Sort the protocol list alphabetically to canonicalize it.
2112 QualType Canonical;
2113 if (!InterfaceT.isCanonical() ||
2114 !areSortedAndUniqued(Protocols, NumProtocols)) {
2115 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
2116 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2117 unsigned UniqueCount = NumProtocols;
2118
2119 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2120 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2121
2122 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2123 &Sorted[0], UniqueCount);
2124 } else {
2125 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2126 Protocols, NumProtocols);
2127 }
2128
2129 // Regenerate InsertPos.
2130 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2131 }
2132
Douglas Gregorf85bee62010-02-08 22:59:26 +00002133 // No match.
2134 unsigned Size = sizeof(ObjCObjectPointerType)
2135 + NumProtocols * sizeof(ObjCProtocolDecl *);
2136 void *Mem = Allocate(Size, TypeAlignment);
2137 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2138 InterfaceT,
2139 Protocols,
2140 NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002141
Steve Narofffb4330f2009-06-17 22:40:22 +00002142 Types.push_back(QType);
2143 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002144 return getQualifiedType(QualType(QType, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002145}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002146
Steve Naroffc277ad12009-07-18 15:33:26 +00002147/// getObjCInterfaceType - Return the unique reference to the type for the
2148/// specified ObjC interface decl. The list of protocols is optional.
2149QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002150 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002151 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00002152 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002153
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002154 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00002155 if (ObjCInterfaceType *QT =
2156 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002157 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002158
John McCallfc93cf92009-10-22 22:37:11 +00002159 // Sort the protocol list alphabetically to canonicalize it.
2160 QualType Canonical;
2161 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2162 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2163 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2164
2165 unsigned UniqueCount = NumProtocols;
2166 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2167
2168 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2169
2170 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2171 }
2172
Douglas Gregorf85bee62010-02-08 22:59:26 +00002173 unsigned Size = sizeof(ObjCInterfaceType)
2174 + NumProtocols * sizeof(ObjCProtocolDecl *);
2175 void *Mem = Allocate(Size, TypeAlignment);
2176 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2177 const_cast<ObjCInterfaceDecl*>(Decl),
2178 Protocols,
2179 NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002180
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002181 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00002182 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002183 return QualType(QType, 0);
2184}
2185
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002186/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2187/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002188/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002189/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002190/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002191QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002192 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002193 if (tofExpr->isTypeDependent()) {
2194 llvm::FoldingSetNodeID ID;
2195 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002196
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002197 void *InsertPos = 0;
2198 DependentTypeOfExprType *Canon
2199 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2200 if (Canon) {
2201 // We already have a "canonical" version of an identical, dependent
2202 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002203 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002204 QualType((TypeOfExprType*)Canon, 0));
2205 }
2206 else {
2207 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002208 Canon
2209 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002210 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2211 toe = Canon;
2212 }
2213 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002214 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002215 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002216 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002217 Types.push_back(toe);
2218 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002219}
2220
Steve Naroffa773cd52007-08-01 18:02:17 +00002221/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2222/// TypeOfType AST's. The only motivation to unique these nodes would be
2223/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002224/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002225/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002226QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002227 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002228 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002229 Types.push_back(tot);
2230 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002231}
2232
Anders Carlssonad6bd352009-06-24 21:24:56 +00002233/// getDecltypeForExpr - Given an expr, will return the decltype for that
2234/// expression, according to the rules in C++0x [dcl.type.simple]p4
2235static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002236 if (e->isTypeDependent())
2237 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002238
Anders Carlssonad6bd352009-06-24 21:24:56 +00002239 // If e is an id expression or a class member access, decltype(e) is defined
2240 // as the type of the entity named by e.
2241 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2242 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2243 return VD->getType();
2244 }
2245 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2246 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2247 return FD->getType();
2248 }
2249 // If e is a function call or an invocation of an overloaded operator,
2250 // (parentheses around e are ignored), decltype(e) is defined as the
2251 // return type of that function.
2252 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2253 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002254
Anders Carlssonad6bd352009-06-24 21:24:56 +00002255 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002256
2257 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002258 // defined as T&, otherwise decltype(e) is defined as T.
2259 if (e->isLvalue(Context) == Expr::LV_Valid)
2260 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002261
Anders Carlssonad6bd352009-06-24 21:24:56 +00002262 return T;
2263}
2264
Anders Carlsson81df7b82009-06-24 19:06:50 +00002265/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2266/// DecltypeType AST's. The only motivation to unique these nodes would be
2267/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002268/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002269/// on canonical type's (which are always unique).
2270QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002271 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002272 if (e->isTypeDependent()) {
2273 llvm::FoldingSetNodeID ID;
2274 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002275
Douglas Gregora21f6c32009-07-30 23:36:40 +00002276 void *InsertPos = 0;
2277 DependentDecltypeType *Canon
2278 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2279 if (Canon) {
2280 // We already have a "canonical" version of an equivalent, dependent
2281 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002282 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002283 QualType((DecltypeType*)Canon, 0));
2284 }
2285 else {
2286 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002287 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002288 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2289 dt = Canon;
2290 }
2291 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002292 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002293 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002294 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002295 Types.push_back(dt);
2296 return QualType(dt, 0);
2297}
2298
Chris Lattnerfb072462007-01-23 05:45:31 +00002299/// getTagDeclType - Return the unique reference to the type for the
2300/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002301QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002302 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002303 // FIXME: What is the design on getTagDeclType when it requires casting
2304 // away const? mutable?
2305 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002306}
2307
Mike Stump11289f42009-09-09 15:08:12 +00002308/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2309/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2310/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002311CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002312 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002313}
Chris Lattnerfb072462007-01-23 05:45:31 +00002314
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002315/// getSignedWCharType - Return the type of "signed wchar_t".
2316/// Used when in C++, as a GCC extension.
2317QualType ASTContext::getSignedWCharType() const {
2318 // FIXME: derive from "Target" ?
2319 return WCharTy;
2320}
2321
2322/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2323/// Used when in C++, as a GCC extension.
2324QualType ASTContext::getUnsignedWCharType() const {
2325 // FIXME: derive from "Target" ?
2326 return UnsignedIntTy;
2327}
2328
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002329/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2330/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2331QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002332 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002333}
2334
Chris Lattnera21ad802008-04-02 05:18:44 +00002335//===----------------------------------------------------------------------===//
2336// Type Operators
2337//===----------------------------------------------------------------------===//
2338
John McCallfc93cf92009-10-22 22:37:11 +00002339CanQualType ASTContext::getCanonicalParamType(QualType T) {
2340 // Push qualifiers into arrays, and then discard any remaining
2341 // qualifiers.
2342 T = getCanonicalType(T);
2343 const Type *Ty = T.getTypePtr();
2344
2345 QualType Result;
2346 if (isa<ArrayType>(Ty)) {
2347 Result = getArrayDecayedType(QualType(Ty,0));
2348 } else if (isa<FunctionType>(Ty)) {
2349 Result = getPointerType(QualType(Ty, 0));
2350 } else {
2351 Result = QualType(Ty, 0);
2352 }
2353
2354 return CanQualType::CreateUnsafe(Result);
2355}
2356
Chris Lattnered0d0792008-04-06 22:41:35 +00002357/// getCanonicalType - Return the canonical (structural) type corresponding to
2358/// the specified potentially non-canonical type. The non-canonical version
2359/// of a type may have many "decorated" versions of types. Decorators can
2360/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2361/// to be free of any of these, allowing two canonical types to be compared
2362/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002363CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002364 QualifierCollector Quals;
2365 const Type *Ptr = Quals.strip(T);
2366 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002367
John McCall8ccfcb52009-09-24 19:53:00 +00002368 // The canonical internal type will be the canonical type *except*
2369 // that we push type qualifiers down through array types.
2370
2371 // If there are no new qualifiers to push down, stop here.
2372 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002373 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002374
John McCall8ccfcb52009-09-24 19:53:00 +00002375 // If the type qualifiers are on an array type, get the canonical
2376 // type of the array with the qualifiers applied to the element
2377 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002378 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2379 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002380 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002381
Chris Lattner7adf0762008-08-04 07:31:14 +00002382 // Get the canonical version of the element with the extra qualifiers on it.
2383 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002384 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002385 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002386
Chris Lattner7adf0762008-08-04 07:31:14 +00002387 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002388 return CanQualType::CreateUnsafe(
2389 getConstantArrayType(NewEltTy, CAT->getSize(),
2390 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002391 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002392 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002393 return CanQualType::CreateUnsafe(
2394 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002395 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002396
Douglas Gregor4619e432008-12-05 23:32:09 +00002397 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002398 return CanQualType::CreateUnsafe(
2399 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002400 DSAT->getSizeExpr() ?
2401 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002402 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002403 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002404 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002405
Chris Lattner7adf0762008-08-04 07:31:14 +00002406 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002407 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002408 VAT->getSizeExpr() ?
2409 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002410 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002411 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002412 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002413}
2414
Chandler Carruth607f38e2009-12-29 07:16:59 +00002415QualType ASTContext::getUnqualifiedArrayType(QualType T,
2416 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002417 Quals = T.getQualifiers();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002418 if (!isa<ArrayType>(T)) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002419 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002420 }
2421
Chandler Carruth607f38e2009-12-29 07:16:59 +00002422 const ArrayType *AT = cast<ArrayType>(T);
2423 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002424 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002425 if (Elt == UnqualElt)
2426 return T;
2427
2428 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2429 return getConstantArrayType(UnqualElt, CAT->getSize(),
2430 CAT->getSizeModifier(), 0);
2431 }
2432
2433 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2434 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2435 }
2436
2437 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2438 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2439 DSAT->getSizeModifier(), 0,
2440 SourceRange());
2441}
2442
John McCall847e2a12009-11-24 18:42:40 +00002443DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2444 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2445 return TD->getDeclName();
2446
2447 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2448 if (DTN->isIdentifier()) {
2449 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2450 } else {
2451 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2452 }
2453 }
2454
John McCalld28ae272009-12-02 08:04:21 +00002455 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2456 assert(Storage);
2457 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002458}
2459
Douglas Gregor6bc50582009-05-07 06:41:52 +00002460TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2461 // If this template name refers to a template, the canonical
2462 // template name merely stores the template itself.
2463 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002464 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002465
John McCalld28ae272009-12-02 08:04:21 +00002466 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002467
Douglas Gregor6bc50582009-05-07 06:41:52 +00002468 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2469 assert(DTN && "Non-dependent template names must refer to template decls.");
2470 return DTN->CanonicalTemplateName;
2471}
2472
Douglas Gregoradee3e32009-11-11 23:06:43 +00002473bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2474 X = getCanonicalTemplateName(X);
2475 Y = getCanonicalTemplateName(Y);
2476 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2477}
2478
Mike Stump11289f42009-09-09 15:08:12 +00002479TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002480ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2481 switch (Arg.getKind()) {
2482 case TemplateArgument::Null:
2483 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002484
Douglas Gregora8e02e72009-07-28 23:00:59 +00002485 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002486 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002487
Douglas Gregora8e02e72009-07-28 23:00:59 +00002488 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002489 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002490
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002491 case TemplateArgument::Template:
2492 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2493
Douglas Gregora8e02e72009-07-28 23:00:59 +00002494 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002495 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002496 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002497
Douglas Gregora8e02e72009-07-28 23:00:59 +00002498 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002499 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002500
Douglas Gregora8e02e72009-07-28 23:00:59 +00002501 case TemplateArgument::Pack: {
2502 // FIXME: Allocate in ASTContext
2503 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2504 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002505 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002506 AEnd = Arg.pack_end();
2507 A != AEnd; (void)++A, ++Idx)
2508 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002509
Douglas Gregora8e02e72009-07-28 23:00:59 +00002510 TemplateArgument Result;
2511 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2512 return Result;
2513 }
2514 }
2515
2516 // Silence GCC warning
2517 assert(false && "Unhandled template argument kind");
2518 return TemplateArgument();
2519}
2520
Douglas Gregor333489b2009-03-27 23:10:48 +00002521NestedNameSpecifier *
2522ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002523 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002524 return 0;
2525
2526 switch (NNS->getKind()) {
2527 case NestedNameSpecifier::Identifier:
2528 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002529 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002530 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2531 NNS->getAsIdentifier());
2532
2533 case NestedNameSpecifier::Namespace:
2534 // A namespace is canonical; build a nested-name-specifier with
2535 // this namespace and no prefix.
2536 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2537
2538 case NestedNameSpecifier::TypeSpec:
2539 case NestedNameSpecifier::TypeSpecWithTemplate: {
2540 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002541 return NestedNameSpecifier::Create(*this, 0,
2542 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002543 T.getTypePtr());
2544 }
2545
2546 case NestedNameSpecifier::Global:
2547 // The global specifier is canonical and unique.
2548 return NNS;
2549 }
2550
2551 // Required to silence a GCC warning
2552 return 0;
2553}
2554
Chris Lattner7adf0762008-08-04 07:31:14 +00002555
2556const ArrayType *ASTContext::getAsArrayType(QualType T) {
2557 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002558 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002559 // Handle the common positive case fast.
2560 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2561 return AT;
2562 }
Mike Stump11289f42009-09-09 15:08:12 +00002563
John McCall8ccfcb52009-09-24 19:53:00 +00002564 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002565 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002566 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002567 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002568
John McCall8ccfcb52009-09-24 19:53:00 +00002569 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002570 // implements C99 6.7.3p8: "If the specification of an array type includes
2571 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002572
Chris Lattner7adf0762008-08-04 07:31:14 +00002573 // If we get here, we either have type qualifiers on the type, or we have
2574 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002575 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002576
John McCall8ccfcb52009-09-24 19:53:00 +00002577 QualifierCollector Qs;
2578 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002579
Chris Lattner7adf0762008-08-04 07:31:14 +00002580 // If we have a simple case, just return now.
2581 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002582 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002583 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002584
Chris Lattner7adf0762008-08-04 07:31:14 +00002585 // Otherwise, we have an array and we have qualifiers on it. Push the
2586 // qualifiers into the array element type and return a new array type.
2587 // Get the canonical version of the element with the extra qualifiers on it.
2588 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002589 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002590
Chris Lattner7adf0762008-08-04 07:31:14 +00002591 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2592 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2593 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002594 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002595 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2596 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2597 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002598 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002599
Mike Stump11289f42009-09-09 15:08:12 +00002600 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002601 = dyn_cast<DependentSizedArrayType>(ATy))
2602 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002603 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002604 DSAT->getSizeExpr() ?
2605 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002606 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002607 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002608 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002609
Chris Lattner7adf0762008-08-04 07:31:14 +00002610 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002611 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002612 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002613 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002614 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002615 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002616 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002617}
2618
2619
Chris Lattnera21ad802008-04-02 05:18:44 +00002620/// getArrayDecayedType - Return the properly qualified result of decaying the
2621/// specified array type to a pointer. This operation is non-trivial when
2622/// handling typedefs etc. The canonical type of "T" must be an array type,
2623/// this returns a pointer to a properly qualified element of the array.
2624///
2625/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2626QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002627 // Get the element type with 'getAsArrayType' so that we don't lose any
2628 // typedefs in the element type of the array. This also handles propagation
2629 // of type qualifiers from the array type into the element type if present
2630 // (C99 6.7.3p8).
2631 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2632 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002633
Chris Lattner7adf0762008-08-04 07:31:14 +00002634 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002635
2636 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002637 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002638}
2639
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002640QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002641 QualifierCollector Qs;
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002642 while (true) {
John McCall8ccfcb52009-09-24 19:53:00 +00002643 const Type *UT = Qs.strip(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002644 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2645 QT = AT->getElementType();
Mike Stumpea086c72009-07-25 23:24:03 +00002646 } else {
John McCall8ccfcb52009-09-24 19:53:00 +00002647 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002648 }
2649 }
2650}
2651
Anders Carlsson4bf82142009-09-25 01:23:32 +00002652QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2653 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002654
Anders Carlsson4bf82142009-09-25 01:23:32 +00002655 if (const ArrayType *AT = getAsArrayType(ElemTy))
2656 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002657
Anders Carlssone0808df2008-12-21 03:44:36 +00002658 return ElemTy;
2659}
2660
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002661/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002662uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002663ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2664 uint64_t ElementCount = 1;
2665 do {
2666 ElementCount *= CA->getSize().getZExtValue();
2667 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2668 } while (CA);
2669 return ElementCount;
2670}
2671
Steve Naroff0af91202007-04-27 21:51:21 +00002672/// getFloatingRank - Return a relative rank for floating point types.
2673/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002674static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002675 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002676 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002677
John McCall9dd450b2009-09-21 23:43:11 +00002678 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2679 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002680 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002681 case BuiltinType::Float: return FloatRank;
2682 case BuiltinType::Double: return DoubleRank;
2683 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002684 }
2685}
2686
Mike Stump11289f42009-09-09 15:08:12 +00002687/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2688/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002689/// 'typeDomain' is a real floating point or complex type.
2690/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002691QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2692 QualType Domain) const {
2693 FloatingRank EltRank = getFloatingRank(Size);
2694 if (Domain->isComplexType()) {
2695 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002696 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002697 case FloatRank: return FloatComplexTy;
2698 case DoubleRank: return DoubleComplexTy;
2699 case LongDoubleRank: return LongDoubleComplexTy;
2700 }
Steve Naroff0af91202007-04-27 21:51:21 +00002701 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002702
2703 assert(Domain->isRealFloatingType() && "Unknown domain!");
2704 switch (EltRank) {
2705 default: assert(0 && "getFloatingRank(): illegal value for rank");
2706 case FloatRank: return FloatTy;
2707 case DoubleRank: return DoubleTy;
2708 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002709 }
Steve Naroffe4718892007-04-27 18:30:00 +00002710}
2711
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002712/// getFloatingTypeOrder - Compare the rank of the two specified floating
2713/// point types, ignoring the domain of the type (i.e. 'double' ==
2714/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002715/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002716int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2717 FloatingRank LHSR = getFloatingRank(LHS);
2718 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002719
Chris Lattnerb90739d2008-04-06 23:38:49 +00002720 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002721 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002722 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002723 return 1;
2724 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002725}
2726
Chris Lattner76a00cf2008-04-06 22:59:24 +00002727/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2728/// routine will assert if passed a built-in type that isn't an integer or enum,
2729/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002730unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002731 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002732 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002733 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002734
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002735 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2736 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2737
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002738 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2739 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2740
2741 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2742 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2743
Chris Lattner76a00cf2008-04-06 22:59:24 +00002744 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002745 default: assert(0 && "getIntegerRank(): not a built-in integer");
2746 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002747 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002748 case BuiltinType::Char_S:
2749 case BuiltinType::Char_U:
2750 case BuiltinType::SChar:
2751 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002752 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002753 case BuiltinType::Short:
2754 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002755 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002756 case BuiltinType::Int:
2757 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002758 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002759 case BuiltinType::Long:
2760 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002761 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002762 case BuiltinType::LongLong:
2763 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002764 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002765 case BuiltinType::Int128:
2766 case BuiltinType::UInt128:
2767 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002768 }
2769}
2770
Eli Friedman629ffb92009-08-20 04:21:42 +00002771/// \brief Whether this is a promotable bitfield reference according
2772/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2773///
2774/// \returns the type this bit-field will promote to, or NULL if no
2775/// promotion occurs.
2776QualType ASTContext::isPromotableBitField(Expr *E) {
2777 FieldDecl *Field = E->getBitField();
2778 if (!Field)
2779 return QualType();
2780
2781 QualType FT = Field->getType();
2782
2783 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2784 uint64_t BitWidth = BitWidthAP.getZExtValue();
2785 uint64_t IntSize = getTypeSize(IntTy);
2786 // GCC extension compatibility: if the bit-field size is less than or equal
2787 // to the size of int, it gets promoted no matter what its type is.
2788 // For instance, unsigned long bf : 4 gets promoted to signed int.
2789 if (BitWidth < IntSize)
2790 return IntTy;
2791
2792 if (BitWidth == IntSize)
2793 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2794
2795 // Types bigger than int are not subject to promotions, and therefore act
2796 // like the base type.
2797 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2798 // is ridiculous.
2799 return QualType();
2800}
2801
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002802/// getPromotedIntegerType - Returns the type that Promotable will
2803/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2804/// integer type.
2805QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2806 assert(!Promotable.isNull());
2807 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002808 if (const EnumType *ET = Promotable->getAs<EnumType>())
2809 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002810 if (Promotable->isSignedIntegerType())
2811 return IntTy;
2812 uint64_t PromotableSize = getTypeSize(Promotable);
2813 uint64_t IntSize = getTypeSize(IntTy);
2814 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2815 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2816}
2817
Mike Stump11289f42009-09-09 15:08:12 +00002818/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002819/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002820/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002821int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002822 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2823 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002824 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002825
Chris Lattner76a00cf2008-04-06 22:59:24 +00002826 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2827 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002828
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002829 unsigned LHSRank = getIntegerRank(LHSC);
2830 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002831
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002832 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2833 if (LHSRank == RHSRank) return 0;
2834 return LHSRank > RHSRank ? 1 : -1;
2835 }
Mike Stump11289f42009-09-09 15:08:12 +00002836
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002837 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2838 if (LHSUnsigned) {
2839 // If the unsigned [LHS] type is larger, return it.
2840 if (LHSRank >= RHSRank)
2841 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002842
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002843 // If the signed type can represent all values of the unsigned type, it
2844 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002845 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002846 return -1;
2847 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002848
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002849 // If the unsigned [RHS] type is larger, return it.
2850 if (RHSRank >= LHSRank)
2851 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002852
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002853 // If the signed type can represent all values of the unsigned type, it
2854 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002855 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002856 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002857}
Anders Carlsson98f07902007-08-17 05:31:46 +00002858
Anders Carlsson6d417272009-11-14 21:45:58 +00002859static RecordDecl *
2860CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2861 SourceLocation L, IdentifierInfo *Id) {
2862 if (Ctx.getLangOptions().CPlusPlus)
2863 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2864 else
2865 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2866}
2867
Mike Stump11289f42009-09-09 15:08:12 +00002868// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002869QualType ASTContext::getCFConstantStringType() {
2870 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002871 CFConstantStringTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002872 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2873 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002874 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002875
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002876 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002877
Anders Carlsson98f07902007-08-17 05:31:46 +00002878 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002879 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002880 // int flags;
2881 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002882 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002883 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002884 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002885 FieldTypes[3] = LongTy;
2886
Anders Carlsson98f07902007-08-17 05:31:46 +00002887 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002888 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002889 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002890 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002891 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002892 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002893 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002894 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002895 }
2896
Douglas Gregord5058122010-02-11 01:19:42 +00002897 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002898 }
Mike Stump11289f42009-09-09 15:08:12 +00002899
Anders Carlsson98f07902007-08-17 05:31:46 +00002900 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002901}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002902
Douglas Gregor512b0772009-04-23 22:29:11 +00002903void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002904 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002905 assert(Rec && "Invalid CFConstantStringType");
2906 CFConstantStringTypeDecl = Rec->getDecl();
2907}
2908
Mike Stump11289f42009-09-09 15:08:12 +00002909QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002910 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002911 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002912 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2913 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00002914 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002915
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002916 QualType FieldTypes[] = {
2917 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002918 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002919 getPointerType(UnsignedLongTy),
2920 getConstantArrayType(UnsignedLongTy,
2921 llvm::APInt(32, 5), ArrayType::Normal, 0)
2922 };
Mike Stump11289f42009-09-09 15:08:12 +00002923
Douglas Gregor91f84212008-12-11 16:49:14 +00002924 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002925 FieldDecl *Field = FieldDecl::Create(*this,
2926 ObjCFastEnumerationStateTypeDecl,
2927 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002928 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002929 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002930 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002931 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002932 }
Mike Stump11289f42009-09-09 15:08:12 +00002933
Douglas Gregord5058122010-02-11 01:19:42 +00002934 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002935 }
Mike Stump11289f42009-09-09 15:08:12 +00002936
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002937 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2938}
2939
Mike Stumpd0153282009-10-20 02:12:22 +00002940QualType ASTContext::getBlockDescriptorType() {
2941 if (BlockDescriptorType)
2942 return getTagDeclType(BlockDescriptorType);
2943
2944 RecordDecl *T;
2945 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002946 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2947 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00002948 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002949
2950 QualType FieldTypes[] = {
2951 UnsignedLongTy,
2952 UnsignedLongTy,
2953 };
2954
2955 const char *FieldNames[] = {
2956 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002957 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002958 };
2959
2960 for (size_t i = 0; i < 2; ++i) {
2961 FieldDecl *Field = FieldDecl::Create(*this,
2962 T,
2963 SourceLocation(),
2964 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00002965 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00002966 /*BitWidth=*/0,
2967 /*Mutable=*/false);
2968 T->addDecl(Field);
2969 }
2970
Douglas Gregord5058122010-02-11 01:19:42 +00002971 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002972
2973 BlockDescriptorType = T;
2974
2975 return getTagDeclType(BlockDescriptorType);
2976}
2977
2978void ASTContext::setBlockDescriptorType(QualType T) {
2979 const RecordType *Rec = T->getAs<RecordType>();
2980 assert(Rec && "Invalid BlockDescriptorType");
2981 BlockDescriptorType = Rec->getDecl();
2982}
2983
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002984QualType ASTContext::getBlockDescriptorExtendedType() {
2985 if (BlockDescriptorExtendedType)
2986 return getTagDeclType(BlockDescriptorExtendedType);
2987
2988 RecordDecl *T;
2989 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002990 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2991 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00002992 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002993
2994 QualType FieldTypes[] = {
2995 UnsignedLongTy,
2996 UnsignedLongTy,
2997 getPointerType(VoidPtrTy),
2998 getPointerType(VoidPtrTy)
2999 };
3000
3001 const char *FieldNames[] = {
3002 "reserved",
3003 "Size",
3004 "CopyFuncPtr",
3005 "DestroyFuncPtr"
3006 };
3007
3008 for (size_t i = 0; i < 4; ++i) {
3009 FieldDecl *Field = FieldDecl::Create(*this,
3010 T,
3011 SourceLocation(),
3012 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003013 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003014 /*BitWidth=*/0,
3015 /*Mutable=*/false);
3016 T->addDecl(Field);
3017 }
3018
Douglas Gregord5058122010-02-11 01:19:42 +00003019 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003020
3021 BlockDescriptorExtendedType = T;
3022
3023 return getTagDeclType(BlockDescriptorExtendedType);
3024}
3025
3026void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3027 const RecordType *Rec = T->getAs<RecordType>();
3028 assert(Rec && "Invalid BlockDescriptorType");
3029 BlockDescriptorExtendedType = Rec->getDecl();
3030}
3031
Mike Stump94967902009-10-21 18:16:27 +00003032bool ASTContext::BlockRequiresCopying(QualType Ty) {
3033 if (Ty->isBlockPointerType())
3034 return true;
3035 if (isObjCNSObjectType(Ty))
3036 return true;
3037 if (Ty->isObjCObjectPointerType())
3038 return true;
3039 return false;
3040}
3041
3042QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3043 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003044 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003045 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003046 // unsigned int __flags;
3047 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003048 // void *__copy_helper; // as needed
3049 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003050 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003051 // } *
3052
Mike Stump94967902009-10-21 18:16:27 +00003053 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3054
3055 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003056 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003057 llvm::SmallString<36> Name;
3058 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3059 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003060 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003061 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3062 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003063 T->startDefinition();
3064 QualType Int32Ty = IntTy;
3065 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3066 QualType FieldTypes[] = {
3067 getPointerType(VoidPtrTy),
3068 getPointerType(getTagDeclType(T)),
3069 Int32Ty,
3070 Int32Ty,
3071 getPointerType(VoidPtrTy),
3072 getPointerType(VoidPtrTy),
3073 Ty
3074 };
3075
3076 const char *FieldNames[] = {
3077 "__isa",
3078 "__forwarding",
3079 "__flags",
3080 "__size",
3081 "__copy_helper",
3082 "__destroy_helper",
3083 DeclName,
3084 };
3085
3086 for (size_t i = 0; i < 7; ++i) {
3087 if (!HasCopyAndDispose && i >=4 && i <= 5)
3088 continue;
3089 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3090 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003091 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003092 /*BitWidth=*/0, /*Mutable=*/false);
3093 T->addDecl(Field);
3094 }
3095
Douglas Gregord5058122010-02-11 01:19:42 +00003096 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003097
3098 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003099}
3100
3101
3102QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003103 bool BlockHasCopyDispose,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003104 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpd0153282009-10-20 02:12:22 +00003105 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003106 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003107 llvm::SmallString<36> Name;
3108 llvm::raw_svector_ostream(Name) << "__block_literal_"
3109 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003110 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003111 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3112 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003113 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003114 QualType FieldTypes[] = {
3115 getPointerType(VoidPtrTy),
3116 IntTy,
3117 IntTy,
3118 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003119 (BlockHasCopyDispose ?
3120 getPointerType(getBlockDescriptorExtendedType()) :
3121 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003122 };
3123
3124 const char *FieldNames[] = {
3125 "__isa",
3126 "__flags",
3127 "__reserved",
3128 "__FuncPtr",
3129 "__descriptor"
3130 };
3131
3132 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003133 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003134 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003135 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003136 /*BitWidth=*/0, /*Mutable=*/false);
3137 T->addDecl(Field);
3138 }
3139
3140 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3141 const Expr *E = BlockDeclRefDecls[i];
3142 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3143 clang::IdentifierInfo *Name = 0;
3144 if (BDRE) {
3145 const ValueDecl *D = BDRE->getDecl();
3146 Name = &Idents.get(D->getName());
3147 }
3148 QualType FieldType = E->getType();
3149
3150 if (BDRE && BDRE->isByRef())
Mike Stump94967902009-10-21 18:16:27 +00003151 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3152 FieldType);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003153
3154 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallbcd03502009-12-07 02:54:59 +00003155 Name, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003156 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpd0153282009-10-20 02:12:22 +00003157 T->addDecl(Field);
3158 }
3159
Douglas Gregord5058122010-02-11 01:19:42 +00003160 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003161
3162 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003163}
3164
Douglas Gregor512b0772009-04-23 22:29:11 +00003165void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003166 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003167 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3168 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3169}
3170
Anders Carlsson18acd442007-10-29 06:33:42 +00003171// This returns true if a type has been typedefed to BOOL:
3172// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003173static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003174 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003175 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3176 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003177
Anders Carlssond8499822007-10-29 05:01:08 +00003178 return false;
3179}
3180
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003181/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003182/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003183CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003184 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003185
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003186 // Make all integer and enum types at least as large as an int
Ken Dyck40775002010-01-11 17:06:35 +00003187 if (sz.isPositive() && type->isIntegralType())
3188 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003189 // Treat arrays as pointers, since that's how they're passed in.
3190 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003191 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003192 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003193}
3194
3195static inline
3196std::string charUnitsToString(const CharUnits &CU) {
3197 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003198}
3199
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003200/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003201/// declaration.
3202void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3203 std::string& S) {
3204 const BlockDecl *Decl = Expr->getBlockDecl();
3205 QualType BlockTy =
3206 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3207 // Encode result type.
3208 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3209 // Compute size of all parameters.
3210 // Start with computing size of a pointer in number of bytes.
3211 // FIXME: There might(should) be a better way of doing this computation!
3212 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003213 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3214 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003215 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003216 E = Decl->param_end(); PI != E; ++PI) {
3217 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003218 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003219 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003220 ParmOffset += sz;
3221 }
3222 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003223 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003224 // Block pointer and offset.
3225 S += "@?0";
3226 ParmOffset = PtrSize;
3227
3228 // Argument types.
3229 ParmOffset = PtrSize;
3230 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3231 Decl->param_end(); PI != E; ++PI) {
3232 ParmVarDecl *PVDecl = *PI;
3233 QualType PType = PVDecl->getOriginalType();
3234 if (const ArrayType *AT =
3235 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3236 // Use array's original type only if it has known number of
3237 // elements.
3238 if (!isa<ConstantArrayType>(AT))
3239 PType = PVDecl->getType();
3240 } else if (PType->isFunctionType())
3241 PType = PVDecl->getType();
3242 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003243 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003244 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003245 }
3246}
3247
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003248/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003249/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003250void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003251 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003252 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003253 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003254 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003255 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003256 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003257 // Compute size of all parameters.
3258 // Start with computing size of a pointer in number of bytes.
3259 // FIXME: There might(should) be a better way of doing this computation!
3260 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003261 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003262 // The first two arguments (self and _cmd) are pointers; account for
3263 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003264 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003265 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003266 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003267 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003268 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003269 assert (sz.isPositive() &&
3270 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003271 ParmOffset += sz;
3272 }
Ken Dyck40775002010-01-11 17:06:35 +00003273 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003274 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003275 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003276
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003277 // Argument types.
3278 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003279 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003280 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003281 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003282 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003283 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003284 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3285 // Use array's original type only if it has known number of
3286 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003287 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003288 PType = PVDecl->getType();
3289 } else if (PType->isFunctionType())
3290 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003291 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003292 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003293 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003294 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003295 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003296 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003297 }
3298}
3299
Daniel Dunbar4932b362008-08-28 04:38:10 +00003300/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003301/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003302/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3303/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003304/// Property attributes are stored as a comma-delimited C string. The simple
3305/// attributes readonly and bycopy are encoded as single characters. The
3306/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3307/// encoded as single characters, followed by an identifier. Property types
3308/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003309/// these attributes are defined by the following enumeration:
3310/// @code
3311/// enum PropertyAttributes {
3312/// kPropertyReadOnly = 'R', // property is read-only.
3313/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3314/// kPropertyByref = '&', // property is a reference to the value last assigned
3315/// kPropertyDynamic = 'D', // property is dynamic
3316/// kPropertyGetter = 'G', // followed by getter selector name
3317/// kPropertySetter = 'S', // followed by setter selector name
3318/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3319/// kPropertyType = 't' // followed by old-style type encoding.
3320/// kPropertyWeak = 'W' // 'weak' property
3321/// kPropertyStrong = 'P' // property GC'able
3322/// kPropertyNonAtomic = 'N' // property non-atomic
3323/// };
3324/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003325void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003326 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003327 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003328 // Collect information from the property implementation decl(s).
3329 bool Dynamic = false;
3330 ObjCPropertyImplDecl *SynthesizePID = 0;
3331
3332 // FIXME: Duplicated code due to poor abstraction.
3333 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003334 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003335 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3336 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003337 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003338 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003339 ObjCPropertyImplDecl *PID = *i;
3340 if (PID->getPropertyDecl() == PD) {
3341 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3342 Dynamic = true;
3343 } else {
3344 SynthesizePID = PID;
3345 }
3346 }
3347 }
3348 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003349 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003350 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003351 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003352 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003353 ObjCPropertyImplDecl *PID = *i;
3354 if (PID->getPropertyDecl() == PD) {
3355 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3356 Dynamic = true;
3357 } else {
3358 SynthesizePID = PID;
3359 }
3360 }
Mike Stump11289f42009-09-09 15:08:12 +00003361 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003362 }
3363 }
3364
3365 // FIXME: This is not very efficient.
3366 S = "T";
3367
3368 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003369 // GCC has some special rules regarding encoding of properties which
3370 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003371 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003372 true /* outermost type */,
3373 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003374
3375 if (PD->isReadOnly()) {
3376 S += ",R";
3377 } else {
3378 switch (PD->getSetterKind()) {
3379 case ObjCPropertyDecl::Assign: break;
3380 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003381 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003382 }
3383 }
3384
3385 // It really isn't clear at all what this means, since properties
3386 // are "dynamic by default".
3387 if (Dynamic)
3388 S += ",D";
3389
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003390 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3391 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003392
Daniel Dunbar4932b362008-08-28 04:38:10 +00003393 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3394 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003395 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003396 }
3397
3398 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3399 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003400 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003401 }
3402
3403 if (SynthesizePID) {
3404 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3405 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003406 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003407 }
3408
3409 // FIXME: OBJCGC: weak & strong
3410}
3411
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003412/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003413/// Another legacy compatibility encoding: 32-bit longs are encoded as
3414/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003415/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3416///
3417void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003418 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003419 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003420 if (BT->getKind() == BuiltinType::ULong &&
3421 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003422 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003423 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003424 if (BT->getKind() == BuiltinType::Long &&
3425 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003426 PointeeTy = IntTy;
3427 }
3428 }
3429}
3430
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003431void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003432 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003433 // We follow the behavior of gcc, expanding structures which are
3434 // directly pointed to, and expanding embedded structures. Note that
3435 // these rules are sufficient to prevent recursive encoding of the
3436 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003437 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003438 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003439}
3440
Mike Stump11289f42009-09-09 15:08:12 +00003441static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003442 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003443 const Expr *E = FD->getBitWidth();
3444 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3445 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00003446 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003447 S += 'b';
3448 S += llvm::utostr(N);
3449}
3450
Daniel Dunbar07d07852009-10-18 21:17:35 +00003451// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003452void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3453 bool ExpandPointedToStructures,
3454 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003455 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003456 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003457 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00003458 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003459 if (FD && FD->isBitField())
3460 return EncodeBitField(this, S, FD);
3461 char encoding;
3462 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003463 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00003464 case BuiltinType::Void: encoding = 'v'; break;
3465 case BuiltinType::Bool: encoding = 'B'; break;
3466 case BuiltinType::Char_U:
3467 case BuiltinType::UChar: encoding = 'C'; break;
3468 case BuiltinType::UShort: encoding = 'S'; break;
3469 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003470 case BuiltinType::ULong:
3471 encoding =
3472 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00003473 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003474 case BuiltinType::UInt128: encoding = 'T'; break;
3475 case BuiltinType::ULongLong: encoding = 'Q'; break;
3476 case BuiltinType::Char_S:
3477 case BuiltinType::SChar: encoding = 'c'; break;
3478 case BuiltinType::Short: encoding = 's'; break;
3479 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003480 case BuiltinType::Long:
3481 encoding =
3482 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003483 break;
3484 case BuiltinType::LongLong: encoding = 'q'; break;
3485 case BuiltinType::Int128: encoding = 't'; break;
3486 case BuiltinType::Float: encoding = 'f'; break;
3487 case BuiltinType::Double: encoding = 'd'; break;
3488 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003489 }
Mike Stump11289f42009-09-09 15:08:12 +00003490
Chris Lattnere7cabb92009-07-13 00:10:46 +00003491 S += encoding;
3492 return;
3493 }
Mike Stump11289f42009-09-09 15:08:12 +00003494
John McCall9dd450b2009-09-21 23:43:11 +00003495 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003496 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003497 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003498 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003499 return;
3500 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003501
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003502 // encoding for pointer or r3eference types.
3503 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003504 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003505 if (PT->isObjCSelType()) {
3506 S += ':';
3507 return;
3508 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003509 PointeeTy = PT->getPointeeType();
3510 }
3511 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3512 PointeeTy = RT->getPointeeType();
3513 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003514 bool isReadOnly = false;
3515 // For historical/compatibility reasons, the read-only qualifier of the
3516 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3517 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003518 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003519 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003520 if (OutermostType && T.isConstQualified()) {
3521 isReadOnly = true;
3522 S += 'r';
3523 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003524 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003525 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003526 while (P->getAs<PointerType>())
3527 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003528 if (P.isConstQualified()) {
3529 isReadOnly = true;
3530 S += 'r';
3531 }
3532 }
3533 if (isReadOnly) {
3534 // Another legacy compatibility encoding. Some ObjC qualifier and type
3535 // combinations need to be rearranged.
3536 // Rewrite "in const" from "nr" to "rn"
3537 const char * s = S.c_str();
3538 int len = S.length();
3539 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3540 std::string replace = "rn";
3541 S.replace(S.end()-2, S.end(), replace);
3542 }
3543 }
Mike Stump11289f42009-09-09 15:08:12 +00003544
Anders Carlssond8499822007-10-29 05:01:08 +00003545 if (PointeeTy->isCharType()) {
3546 // char pointer types should be encoded as '*' unless it is a
3547 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003548 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003549 S += '*';
3550 return;
3551 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003552 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003553 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3554 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3555 S += '#';
3556 return;
3557 }
3558 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3559 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3560 S += '@';
3561 return;
3562 }
3563 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003564 }
Anders Carlssond8499822007-10-29 05:01:08 +00003565 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003566 getLegacyIntegralTypeEncoding(PointeeTy);
3567
Mike Stump11289f42009-09-09 15:08:12 +00003568 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003569 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003570 return;
3571 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003572
Chris Lattnere7cabb92009-07-13 00:10:46 +00003573 if (const ArrayType *AT =
3574 // Ignore type qualifiers etc.
3575 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003576 if (isa<IncompleteArrayType>(AT)) {
3577 // Incomplete arrays are encoded as a pointer to the array element.
3578 S += '^';
3579
Mike Stump11289f42009-09-09 15:08:12 +00003580 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003581 false, ExpandStructures, FD);
3582 } else {
3583 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003584
Anders Carlssond05f44b2009-02-22 01:38:57 +00003585 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3586 S += llvm::utostr(CAT->getSize().getZExtValue());
3587 else {
3588 //Variable length arrays are encoded as a regular array with 0 elements.
3589 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3590 S += '0';
3591 }
Mike Stump11289f42009-09-09 15:08:12 +00003592
3593 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003594 false, ExpandStructures, FD);
3595 S += ']';
3596 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003597 return;
3598 }
Mike Stump11289f42009-09-09 15:08:12 +00003599
John McCall9dd450b2009-09-21 23:43:11 +00003600 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003601 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003602 return;
3603 }
Mike Stump11289f42009-09-09 15:08:12 +00003604
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003605 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003606 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003607 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003608 // Anonymous structures print as '?'
3609 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3610 S += II->getName();
3611 } else {
3612 S += '?';
3613 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003614 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003615 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003616 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3617 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003618 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003619 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003620 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003621 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003622 S += '"';
3623 }
Mike Stump11289f42009-09-09 15:08:12 +00003624
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003625 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003626 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003627 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003628 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003629 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003630 QualType qt = Field->getType();
3631 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003632 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003633 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003634 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003635 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003636 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003637 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003638 return;
3639 }
Mike Stump11289f42009-09-09 15:08:12 +00003640
Chris Lattnere7cabb92009-07-13 00:10:46 +00003641 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003642 if (FD && FD->isBitField())
3643 EncodeBitField(this, S, FD);
3644 else
3645 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003646 return;
3647 }
Mike Stump11289f42009-09-09 15:08:12 +00003648
Chris Lattnere7cabb92009-07-13 00:10:46 +00003649 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003650 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003651 return;
3652 }
Mike Stump11289f42009-09-09 15:08:12 +00003653
John McCall8ccfcb52009-09-24 19:53:00 +00003654 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003655 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003656 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003657 S += '{';
3658 const IdentifierInfo *II = OI->getIdentifier();
3659 S += II->getName();
3660 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003661 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003662 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003663 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003664 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003665 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003666 RecFields[i]);
3667 else
Mike Stump11289f42009-09-09 15:08:12 +00003668 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003669 FD);
3670 }
3671 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003672 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003673 }
Mike Stump11289f42009-09-09 15:08:12 +00003674
John McCall9dd450b2009-09-21 23:43:11 +00003675 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003676 if (OPT->isObjCIdType()) {
3677 S += '@';
3678 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003679 }
Mike Stump11289f42009-09-09 15:08:12 +00003680
Steve Narofff0c86112009-10-28 22:03:49 +00003681 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3682 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3683 // Since this is a binary compatibility issue, need to consult with runtime
3684 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003685 S += '#';
3686 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003687 }
Mike Stump11289f42009-09-09 15:08:12 +00003688
Chris Lattnere7cabb92009-07-13 00:10:46 +00003689 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003690 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003691 ExpandPointedToStructures,
3692 ExpandStructures, FD);
3693 if (FD || EncodingProperty) {
3694 // Note that we do extended encoding of protocol qualifer list
3695 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003696 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003697 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3698 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003699 S += '<';
3700 S += (*I)->getNameAsString();
3701 S += '>';
3702 }
3703 S += '"';
3704 }
3705 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003706 }
Mike Stump11289f42009-09-09 15:08:12 +00003707
Chris Lattnere7cabb92009-07-13 00:10:46 +00003708 QualType PointeeTy = OPT->getPointeeType();
3709 if (!EncodingProperty &&
3710 isa<TypedefType>(PointeeTy.getTypePtr())) {
3711 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003712 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003713 // {...};
3714 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003715 getObjCEncodingForTypeImpl(PointeeTy, S,
3716 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003717 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003718 return;
3719 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003720
3721 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003722 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003723 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003724 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003725 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3726 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003727 S += '<';
3728 S += (*I)->getNameAsString();
3729 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003730 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003731 S += '"';
3732 }
3733 return;
3734 }
Mike Stump11289f42009-09-09 15:08:12 +00003735
Chris Lattnere7cabb92009-07-13 00:10:46 +00003736 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003737}
3738
Mike Stump11289f42009-09-09 15:08:12 +00003739void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003740 std::string& S) const {
3741 if (QT & Decl::OBJC_TQ_In)
3742 S += 'n';
3743 if (QT & Decl::OBJC_TQ_Inout)
3744 S += 'N';
3745 if (QT & Decl::OBJC_TQ_Out)
3746 S += 'o';
3747 if (QT & Decl::OBJC_TQ_Bycopy)
3748 S += 'O';
3749 if (QT & Decl::OBJC_TQ_Byref)
3750 S += 'R';
3751 if (QT & Decl::OBJC_TQ_Oneway)
3752 S += 'V';
3753}
3754
Chris Lattnere7cabb92009-07-13 00:10:46 +00003755void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003756 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003757
Anders Carlsson87c149b2007-10-11 01:00:40 +00003758 BuiltinVaListType = T;
3759}
3760
Chris Lattnere7cabb92009-07-13 00:10:46 +00003761void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003762 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003763}
3764
Chris Lattnere7cabb92009-07-13 00:10:46 +00003765void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003766 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003767}
3768
Chris Lattnere7cabb92009-07-13 00:10:46 +00003769void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003770 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003771}
3772
Chris Lattnere7cabb92009-07-13 00:10:46 +00003773void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003774 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003775}
3776
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003777void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003778 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003779 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003780
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003781 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003782}
3783
John McCalld28ae272009-12-02 08:04:21 +00003784/// \brief Retrieve the template name that corresponds to a non-empty
3785/// lookup.
John McCallad371252010-01-20 00:46:10 +00003786TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3787 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003788 unsigned size = End - Begin;
3789 assert(size > 1 && "set is not overloaded!");
3790
3791 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3792 size * sizeof(FunctionTemplateDecl*));
3793 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3794
3795 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003796 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003797 NamedDecl *D = *I;
3798 assert(isa<FunctionTemplateDecl>(D) ||
3799 (isa<UsingShadowDecl>(D) &&
3800 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3801 *Storage++ = D;
3802 }
3803
3804 return TemplateName(OT);
3805}
3806
Douglas Gregordc572a32009-03-30 22:58:21 +00003807/// \brief Retrieve the template name that represents a qualified
3808/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003809TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003810 bool TemplateKeyword,
3811 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003812 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003813 llvm::FoldingSetNodeID ID;
3814 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3815
3816 void *InsertPos = 0;
3817 QualifiedTemplateName *QTN =
3818 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3819 if (!QTN) {
3820 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3821 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3822 }
3823
3824 return TemplateName(QTN);
3825}
3826
3827/// \brief Retrieve the template name that represents a dependent
3828/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003829TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003830 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003831 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003832 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003833
3834 llvm::FoldingSetNodeID ID;
3835 DependentTemplateName::Profile(ID, NNS, Name);
3836
3837 void *InsertPos = 0;
3838 DependentTemplateName *QTN =
3839 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3840
3841 if (QTN)
3842 return TemplateName(QTN);
3843
3844 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3845 if (CanonNNS == NNS) {
3846 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3847 } else {
3848 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3849 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003850 DependentTemplateName *CheckQTN =
3851 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3852 assert(!CheckQTN && "Dependent type name canonicalization broken");
3853 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00003854 }
3855
3856 DependentTemplateNames.InsertNode(QTN, InsertPos);
3857 return TemplateName(QTN);
3858}
3859
Douglas Gregor71395fa2009-11-04 00:56:37 +00003860/// \brief Retrieve the template name that represents a dependent
3861/// template name such as \c MetaFun::template operator+.
3862TemplateName
3863ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3864 OverloadedOperatorKind Operator) {
3865 assert((!NNS || NNS->isDependent()) &&
3866 "Nested name specifier must be dependent");
3867
3868 llvm::FoldingSetNodeID ID;
3869 DependentTemplateName::Profile(ID, NNS, Operator);
3870
3871 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00003872 DependentTemplateName *QTN
3873 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00003874
3875 if (QTN)
3876 return TemplateName(QTN);
3877
3878 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3879 if (CanonNNS == NNS) {
3880 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3881 } else {
3882 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3883 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003884
3885 DependentTemplateName *CheckQTN
3886 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3887 assert(!CheckQTN && "Dependent template name canonicalization broken");
3888 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00003889 }
3890
3891 DependentTemplateNames.InsertNode(QTN, InsertPos);
3892 return TemplateName(QTN);
3893}
3894
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003895/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003896/// TargetInfo, produce the corresponding type. The unsigned @p Type
3897/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003898CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003899 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003900 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003901 case TargetInfo::SignedShort: return ShortTy;
3902 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3903 case TargetInfo::SignedInt: return IntTy;
3904 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3905 case TargetInfo::SignedLong: return LongTy;
3906 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3907 case TargetInfo::SignedLongLong: return LongLongTy;
3908 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3909 }
3910
3911 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003912 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003913}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003914
3915//===----------------------------------------------------------------------===//
3916// Type Predicates.
3917//===----------------------------------------------------------------------===//
3918
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003919/// isObjCNSObjectType - Return true if this is an NSObject object using
3920/// NSObject attribute on a c-style pointer type.
3921/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003922/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003923///
3924bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3925 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3926 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003927 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003928 return true;
3929 }
Mike Stump11289f42009-09-09 15:08:12 +00003930 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003931}
3932
Fariborz Jahanian96207692009-02-18 21:49:28 +00003933/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3934/// garbage collection attribute.
3935///
John McCall8ccfcb52009-09-24 19:53:00 +00003936Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3937 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003938 if (getLangOptions().ObjC1 &&
3939 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003940 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003941 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003942 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003943 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003944 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003945 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003946 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003947 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003948 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003949 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003950 // Non-pointers have none gc'able attribute regardless of the attribute
3951 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003952 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003953 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003954 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003955 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003956}
3957
Chris Lattner49af6a42008-04-07 06:51:04 +00003958//===----------------------------------------------------------------------===//
3959// Type Compatibility Testing
3960//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00003961
Mike Stump11289f42009-09-09 15:08:12 +00003962/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003963/// compatible.
3964static bool areCompatVectorTypes(const VectorType *LHS,
3965 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00003966 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00003967 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00003968 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00003969}
3970
Steve Naroff8e6aee52009-07-23 01:01:38 +00003971//===----------------------------------------------------------------------===//
3972// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3973//===----------------------------------------------------------------------===//
3974
3975/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3976/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003977bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3978 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003979 if (lProto == rProto)
3980 return true;
3981 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3982 E = rProto->protocol_end(); PI != E; ++PI)
3983 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3984 return true;
3985 return false;
3986}
3987
Steve Naroff8e6aee52009-07-23 01:01:38 +00003988/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3989/// return true if lhs's protocols conform to rhs's protocol; false
3990/// otherwise.
3991bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3992 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3993 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3994 return false;
3995}
3996
3997/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3998/// ObjCQualifiedIDType.
3999bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4000 bool compare) {
4001 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004002 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004003 lhs->isObjCIdType() || lhs->isObjCClassType())
4004 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004005 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004006 rhs->isObjCIdType() || rhs->isObjCClassType())
4007 return true;
4008
4009 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004010 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004011
Steve Naroff8e6aee52009-07-23 01:01:38 +00004012 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004013
Steve Naroff8e6aee52009-07-23 01:01:38 +00004014 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004015 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004016 // make sure we check the class hierarchy.
4017 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4018 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4019 E = lhsQID->qual_end(); I != E; ++I) {
4020 // when comparing an id<P> on lhs with a static type on rhs,
4021 // see if static class implements all of id's protocols, directly or
4022 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004023 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004024 return false;
4025 }
4026 }
4027 // If there are no qualifiers and no interface, we have an 'id'.
4028 return true;
4029 }
Mike Stump11289f42009-09-09 15:08:12 +00004030 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004031 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4032 E = lhsQID->qual_end(); I != E; ++I) {
4033 ObjCProtocolDecl *lhsProto = *I;
4034 bool match = false;
4035
4036 // when comparing an id<P> on lhs with a static type on rhs,
4037 // see if static class implements all of id's protocols, directly or
4038 // through its super class and categories.
4039 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4040 E = rhsOPT->qual_end(); J != E; ++J) {
4041 ObjCProtocolDecl *rhsProto = *J;
4042 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4043 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4044 match = true;
4045 break;
4046 }
4047 }
Mike Stump11289f42009-09-09 15:08:12 +00004048 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004049 // make sure we check the class hierarchy.
4050 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4051 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4052 E = lhsQID->qual_end(); I != E; ++I) {
4053 // when comparing an id<P> on lhs with a static type on rhs,
4054 // see if static class implements all of id's protocols, directly or
4055 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004056 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004057 match = true;
4058 break;
4059 }
4060 }
4061 }
4062 if (!match)
4063 return false;
4064 }
Mike Stump11289f42009-09-09 15:08:12 +00004065
Steve Naroff8e6aee52009-07-23 01:01:38 +00004066 return true;
4067 }
Mike Stump11289f42009-09-09 15:08:12 +00004068
Steve Naroff8e6aee52009-07-23 01:01:38 +00004069 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4070 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4071
Mike Stump11289f42009-09-09 15:08:12 +00004072 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004073 lhs->getAsObjCInterfacePointerType()) {
4074 if (lhsOPT->qual_empty()) {
4075 bool match = false;
4076 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4077 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4078 E = rhsQID->qual_end(); I != E; ++I) {
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.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004082 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004083 match = true;
4084 break;
4085 }
4086 }
4087 if (!match)
4088 return false;
4089 }
4090 return true;
4091 }
Mike Stump11289f42009-09-09 15:08:12 +00004092 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004093 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4094 E = lhsOPT->qual_end(); I != E; ++I) {
4095 ObjCProtocolDecl *lhsProto = *I;
4096 bool match = false;
4097
4098 // when comparing an id<P> on lhs with a static type on rhs,
4099 // see if static class implements all of id's protocols, directly or
4100 // through its super class and categories.
4101 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4102 E = rhsQID->qual_end(); J != E; ++J) {
4103 ObjCProtocolDecl *rhsProto = *J;
4104 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4105 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4106 match = true;
4107 break;
4108 }
4109 }
4110 if (!match)
4111 return false;
4112 }
4113 return true;
4114 }
4115 return false;
4116}
4117
Eli Friedman47f77112008-08-22 00:56:42 +00004118/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004119/// compatible for assignment from RHS to LHS. This handles validation of any
4120/// protocol qualifiers on the LHS or RHS.
4121///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004122bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4123 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004124 // If either type represents the built-in 'id' or 'Class' types, return true.
4125 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004126 return true;
4127
Steve Naroff8e6aee52009-07-23 01:01:38 +00004128 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00004129 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4130 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004131 false);
4132
Steve Naroff7cae42b2009-07-10 23:34:53 +00004133 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4134 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00004135 if (LHS && RHS) // We have 2 user-defined types.
4136 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004137
Steve Naroff8e6aee52009-07-23 01:01:38 +00004138 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004139}
4140
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004141/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4142/// for providing type-safty for objective-c pointers used to pass/return
4143/// arguments in block literals. When passed as arguments, passing 'A*' where
4144/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4145/// not OK. For the return type, the opposite is not OK.
4146bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4147 const ObjCObjectPointerType *LHSOPT,
4148 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004149 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004150 return true;
4151
4152 if (LHSOPT->isObjCBuiltinType()) {
4153 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4154 }
4155
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004156 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004157 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4158 QualType(RHSOPT,0),
4159 false);
4160
4161 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4162 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4163 if (LHS && RHS) { // We have 2 user-defined types.
4164 if (LHS != RHS) {
4165 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4166 return false;
4167 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4168 return true;
4169 }
4170 else
4171 return true;
4172 }
4173 return false;
4174}
4175
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004176/// getIntersectionOfProtocols - This routine finds the intersection of set
4177/// of protocols inherited from two distinct objective-c pointer objects.
4178/// It is used to build composite qualifier list of the composite type of
4179/// the conditional expression involving two objective-c pointer objects.
4180static
4181void getIntersectionOfProtocols(ASTContext &Context,
4182 const ObjCObjectPointerType *LHSOPT,
4183 const ObjCObjectPointerType *RHSOPT,
4184 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4185
4186 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4187 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4188
4189 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4190 unsigned LHSNumProtocols = LHS->getNumProtocols();
4191 if (LHSNumProtocols > 0)
4192 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4193 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004194 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4195 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004196 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4197 LHSInheritedProtocols.end());
4198 }
4199
4200 unsigned RHSNumProtocols = RHS->getNumProtocols();
4201 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004202 ObjCProtocolDecl **RHSProtocols =
4203 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004204 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4205 if (InheritedProtocolSet.count(RHSProtocols[i]))
4206 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4207 }
4208 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004209 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004210 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004211 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4212 RHSInheritedProtocols.begin(),
4213 E = RHSInheritedProtocols.end(); I != E; ++I)
4214 if (InheritedProtocolSet.count((*I)))
4215 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004216 }
4217}
4218
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004219/// areCommonBaseCompatible - Returns common base class of the two classes if
4220/// one found. Note that this is O'2 algorithm. But it will be called as the
4221/// last type comparison in a ?-exp of ObjC pointer types before a
4222/// warning is issued. So, its invokation is extremely rare.
4223QualType ASTContext::areCommonBaseCompatible(
4224 const ObjCObjectPointerType *LHSOPT,
4225 const ObjCObjectPointerType *RHSOPT) {
4226 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4227 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4228 if (!LHS || !RHS)
4229 return QualType();
4230
4231 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4232 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4233 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004234 if (canAssignObjCInterfaces(LHS, RHS)) {
4235 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4236 getIntersectionOfProtocols(*this,
4237 LHSOPT, RHSOPT, IntersectionOfProtocols);
4238 if (IntersectionOfProtocols.empty())
4239 LHSTy = getObjCObjectPointerType(LHSTy);
4240 else
4241 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4242 IntersectionOfProtocols.size());
4243 return LHSTy;
4244 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004245 }
4246
4247 return QualType();
4248}
4249
Eli Friedman47f77112008-08-22 00:56:42 +00004250bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4251 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00004252 // Verify that the base decls are compatible: the RHS must be a subclass of
4253 // the LHS.
4254 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4255 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004256
Chris Lattner49af6a42008-04-07 06:51:04 +00004257 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4258 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004259 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004260 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004261
Chris Lattner49af6a42008-04-07 06:51:04 +00004262 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4263 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004264 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004265 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004266
Steve Naroffc277ad12009-07-18 15:33:26 +00004267 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4268 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004269 LHSPI != LHSPE; LHSPI++) {
4270 bool RHSImplementsProtocol = false;
4271
4272 // If the RHS doesn't implement the protocol on the left, the types
4273 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00004274 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004275 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004276 RHSPI != RHSPE; RHSPI++) {
4277 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004278 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004279 break;
4280 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004281 }
4282 // FIXME: For better diagnostics, consider passing back the protocol name.
4283 if (!RHSImplementsProtocol)
4284 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004285 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004286 // The RHS implements all protocols listed on the LHS.
4287 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004288}
4289
Steve Naroffb7605152009-02-12 17:52:19 +00004290bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4291 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004292 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4293 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004294
Steve Naroff7cae42b2009-07-10 23:34:53 +00004295 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004296 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004297
4298 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4299 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004300}
4301
Mike Stump11289f42009-09-09 15:08:12 +00004302/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004303/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004304/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004305/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004306bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004307 if (getLangOptions().CPlusPlus)
4308 return hasSameType(LHS, RHS);
4309
Eli Friedman47f77112008-08-22 00:56:42 +00004310 return !mergeTypes(LHS, RHS).isNull();
4311}
4312
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004313bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4314 return !mergeTypes(LHS, RHS, true).isNull();
4315}
4316
4317QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4318 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004319 const FunctionType *lbase = lhs->getAs<FunctionType>();
4320 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004321 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4322 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004323 bool allLTypes = true;
4324 bool allRTypes = true;
4325
4326 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004327 QualType retType;
4328 if (OfBlockPointer)
4329 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4330 else
4331 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004332 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004333 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004334 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004335 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004336 allRTypes = false;
Mike Stumpea086c72009-07-25 23:24:03 +00004337 // FIXME: double check this
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004338 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4339 // rbase->getRegParmAttr() != 0 &&
4340 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004341 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4342 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004343 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4344 lbaseInfo.getRegParm();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004345 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004346 if (NoReturn != lbaseInfo.getNoReturn() ||
4347 RegParm != lbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004348 allLTypes = false;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004349 if (NoReturn != rbaseInfo.getNoReturn() ||
4350 RegParm != rbaseInfo.getRegParm())
Mike Stump8c5d7992009-07-25 21:26:53 +00004351 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004352 CallingConv lcc = lbaseInfo.getCC();
4353 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004354 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004355 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004356 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004357
Eli Friedman47f77112008-08-22 00:56:42 +00004358 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004359 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4360 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004361 unsigned lproto_nargs = lproto->getNumArgs();
4362 unsigned rproto_nargs = rproto->getNumArgs();
4363
4364 // Compatible functions must have the same number of arguments
4365 if (lproto_nargs != rproto_nargs)
4366 return QualType();
4367
4368 // Variadic and non-variadic functions aren't compatible
4369 if (lproto->isVariadic() != rproto->isVariadic())
4370 return QualType();
4371
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004372 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4373 return QualType();
4374
Eli Friedman47f77112008-08-22 00:56:42 +00004375 // Check argument compatibility
4376 llvm::SmallVector<QualType, 10> types;
4377 for (unsigned i = 0; i < lproto_nargs; i++) {
4378 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4379 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004380 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004381 if (argtype.isNull()) return QualType();
4382 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004383 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4384 allLTypes = false;
4385 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4386 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004387 }
4388 if (allLTypes) return lhs;
4389 if (allRTypes) return rhs;
4390 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004391 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004392 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004393 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004394 }
4395
4396 if (lproto) allRTypes = false;
4397 if (rproto) allLTypes = false;
4398
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004399 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004400 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004401 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004402 if (proto->isVariadic()) return QualType();
4403 // Check that the types are compatible with the types that
4404 // would result from default argument promotions (C99 6.7.5.3p15).
4405 // The only types actually affected are promotable integer
4406 // types and floats, which would be passed as a different
4407 // type depending on whether the prototype is visible.
4408 unsigned proto_nargs = proto->getNumArgs();
4409 for (unsigned i = 0; i < proto_nargs; ++i) {
4410 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004411
4412 // Look at the promotion type of enum types, since that is the type used
4413 // to pass enum values.
4414 if (const EnumType *Enum = argTy->getAs<EnumType>())
4415 argTy = Enum->getDecl()->getPromotionType();
4416
Eli Friedman47f77112008-08-22 00:56:42 +00004417 if (argTy->isPromotableIntegerType() ||
4418 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4419 return QualType();
4420 }
4421
4422 if (allLTypes) return lhs;
4423 if (allRTypes) return rhs;
4424 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004425 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004426 proto->getTypeQuals(),
4427 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004428 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004429 }
4430
4431 if (allLTypes) return lhs;
4432 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004433 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004434 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004435}
4436
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004437QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4438 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004439 // C++ [expr]: If an expression initially has the type "reference to T", the
4440 // type is adjusted to "T" prior to any further analysis, the expression
4441 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004442 // expression is an lvalue unless the reference is an rvalue reference and
4443 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004444 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4445 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4446
Eli Friedman47f77112008-08-22 00:56:42 +00004447 QualType LHSCan = getCanonicalType(LHS),
4448 RHSCan = getCanonicalType(RHS);
4449
4450 // If two types are identical, they are compatible.
4451 if (LHSCan == RHSCan)
4452 return LHS;
4453
John McCall8ccfcb52009-09-24 19:53:00 +00004454 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004455 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4456 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004457 if (LQuals != RQuals) {
4458 // If any of these qualifiers are different, we have a type
4459 // mismatch.
4460 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4461 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4462 return QualType();
4463
4464 // Exactly one GC qualifier difference is allowed: __strong is
4465 // okay if the other type has no GC qualifier but is an Objective
4466 // C object pointer (i.e. implicitly strong by default). We fix
4467 // this by pretending that the unqualified type was actually
4468 // qualified __strong.
4469 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4470 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4471 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4472
4473 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4474 return QualType();
4475
4476 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4477 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4478 }
4479 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4480 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4481 }
Eli Friedman47f77112008-08-22 00:56:42 +00004482 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004483 }
4484
4485 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004486
Eli Friedmandcca6332009-06-01 01:22:52 +00004487 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4488 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004489
Chris Lattnerfd652912008-01-14 05:45:46 +00004490 // We want to consider the two function types to be the same for these
4491 // comparisons, just force one to the other.
4492 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4493 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004494
4495 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004496 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4497 LHSClass = Type::ConstantArray;
4498 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4499 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004500
Nate Begemance4d7fc2008-04-18 23:10:10 +00004501 // Canonicalize ExtVector -> Vector.
4502 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4503 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004504
Chris Lattner95554662008-04-07 05:43:21 +00004505 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004506 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004507 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004508 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004509 // Compatibility is based on the underlying type, not the promotion
4510 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004511 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004512 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4513 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004514 }
John McCall9dd450b2009-09-21 23:43:11 +00004515 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004516 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4517 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004518 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004519
Eli Friedman47f77112008-08-22 00:56:42 +00004520 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004521 }
Eli Friedman47f77112008-08-22 00:56:42 +00004522
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004523 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004524 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004525#define TYPE(Class, Base)
4526#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004527#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004528#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4529#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4530#include "clang/AST/TypeNodes.def"
4531 assert(false && "Non-canonical and dependent types shouldn't get here");
4532 return QualType();
4533
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004534 case Type::LValueReference:
4535 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004536 case Type::MemberPointer:
4537 assert(false && "C++ should never be in mergeTypes");
4538 return QualType();
4539
4540 case Type::IncompleteArray:
4541 case Type::VariableArray:
4542 case Type::FunctionProto:
4543 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004544 assert(false && "Types are eliminated above");
4545 return QualType();
4546
Chris Lattnerfd652912008-01-14 05:45:46 +00004547 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004548 {
4549 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004550 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4551 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004552 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4553 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004554 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004555 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004556 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004557 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004558 return getPointerType(ResultType);
4559 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004560 case Type::BlockPointer:
4561 {
4562 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004563 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4564 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004565 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004566 if (ResultType.isNull()) return QualType();
4567 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4568 return LHS;
4569 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4570 return RHS;
4571 return getBlockPointerType(ResultType);
4572 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004573 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004574 {
4575 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4576 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4577 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4578 return QualType();
4579
4580 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4581 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4582 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4583 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004584 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4585 return LHS;
4586 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4587 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004588 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4589 ArrayType::ArraySizeModifier(), 0);
4590 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4591 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004592 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4593 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004594 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4595 return LHS;
4596 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4597 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004598 if (LVAT) {
4599 // FIXME: This isn't correct! But tricky to implement because
4600 // the array's size has to be the size of LHS, but the type
4601 // has to be different.
4602 return LHS;
4603 }
4604 if (RVAT) {
4605 // FIXME: This isn't correct! But tricky to implement because
4606 // the array's size has to be the size of RHS, but the type
4607 // has to be different.
4608 return RHS;
4609 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004610 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4611 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004612 return getIncompleteArrayType(ResultType,
4613 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004614 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004615 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004616 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004617 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004618 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004619 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004620 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004621 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004622 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004623 case Type::Complex:
4624 // Distinct complex types are incompatible.
4625 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004626 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004627 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004628 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4629 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004630 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004631 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004632 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00004633 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004634 // FIXME: This should be type compatibility, e.g. whether
4635 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00004636 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4637 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00004638 if (LHSIface && RHSIface &&
4639 canAssignObjCInterfaces(LHSIface, RHSIface))
4640 return LHS;
4641
Eli Friedman47f77112008-08-22 00:56:42 +00004642 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004643 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004644 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004645 if (OfBlockPointer) {
4646 if (canAssignObjCInterfacesInBlockPointer(
4647 LHS->getAs<ObjCObjectPointerType>(),
4648 RHS->getAs<ObjCObjectPointerType>()))
4649 return LHS;
4650 return QualType();
4651 }
John McCall9dd450b2009-09-21 23:43:11 +00004652 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4653 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004654 return LHS;
4655
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004656 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004657 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004658 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004659
4660 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004661}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004662
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004663//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004664// Integer Predicates
4665//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004666
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004667unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004668 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004669 return 1;
John McCall56774992009-12-09 09:09:27 +00004670 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004671 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004672 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004673 return (unsigned)getTypeSize(T);
4674}
4675
4676QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4677 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004678
4679 // Turn <4 x signed int> -> <4 x unsigned int>
4680 if (const VectorType *VTy = T->getAs<VectorType>())
4681 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004682 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004683
4684 // For enums, we return the unsigned version of the base type.
4685 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004686 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004687
4688 const BuiltinType *BTy = T->getAs<BuiltinType>();
4689 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004690 switch (BTy->getKind()) {
4691 case BuiltinType::Char_S:
4692 case BuiltinType::SChar:
4693 return UnsignedCharTy;
4694 case BuiltinType::Short:
4695 return UnsignedShortTy;
4696 case BuiltinType::Int:
4697 return UnsignedIntTy;
4698 case BuiltinType::Long:
4699 return UnsignedLongTy;
4700 case BuiltinType::LongLong:
4701 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004702 case BuiltinType::Int128:
4703 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004704 default:
4705 assert(0 && "Unexpected signed integer type");
4706 return QualType();
4707 }
4708}
4709
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004710ExternalASTSource::~ExternalASTSource() { }
4711
4712void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004713
4714
4715//===----------------------------------------------------------------------===//
4716// Builtin Type Computation
4717//===----------------------------------------------------------------------===//
4718
4719/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4720/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004721static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004722 ASTContext::GetBuiltinTypeError &Error,
4723 bool AllowTypeModifiers = true) {
4724 // Modifiers.
4725 int HowLong = 0;
4726 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004727
Chris Lattnerecd79c62009-06-14 00:45:47 +00004728 // Read the modifiers first.
4729 bool Done = false;
4730 while (!Done) {
4731 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004732 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004733 case 'S':
4734 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4735 assert(!Signed && "Can't use 'S' modifier multiple times!");
4736 Signed = true;
4737 break;
4738 case 'U':
4739 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4740 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4741 Unsigned = true;
4742 break;
4743 case 'L':
4744 assert(HowLong <= 2 && "Can't have LLLL modifier");
4745 ++HowLong;
4746 break;
4747 }
4748 }
4749
4750 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004751
Chris Lattnerecd79c62009-06-14 00:45:47 +00004752 // Read the base type.
4753 switch (*Str++) {
4754 default: assert(0 && "Unknown builtin type letter!");
4755 case 'v':
4756 assert(HowLong == 0 && !Signed && !Unsigned &&
4757 "Bad modifiers used with 'v'!");
4758 Type = Context.VoidTy;
4759 break;
4760 case 'f':
4761 assert(HowLong == 0 && !Signed && !Unsigned &&
4762 "Bad modifiers used with 'f'!");
4763 Type = Context.FloatTy;
4764 break;
4765 case 'd':
4766 assert(HowLong < 2 && !Signed && !Unsigned &&
4767 "Bad modifiers used with 'd'!");
4768 if (HowLong)
4769 Type = Context.LongDoubleTy;
4770 else
4771 Type = Context.DoubleTy;
4772 break;
4773 case 's':
4774 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4775 if (Unsigned)
4776 Type = Context.UnsignedShortTy;
4777 else
4778 Type = Context.ShortTy;
4779 break;
4780 case 'i':
4781 if (HowLong == 3)
4782 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4783 else if (HowLong == 2)
4784 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4785 else if (HowLong == 1)
4786 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4787 else
4788 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4789 break;
4790 case 'c':
4791 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4792 if (Signed)
4793 Type = Context.SignedCharTy;
4794 else if (Unsigned)
4795 Type = Context.UnsignedCharTy;
4796 else
4797 Type = Context.CharTy;
4798 break;
4799 case 'b': // boolean
4800 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4801 Type = Context.BoolTy;
4802 break;
4803 case 'z': // size_t.
4804 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4805 Type = Context.getSizeType();
4806 break;
4807 case 'F':
4808 Type = Context.getCFConstantStringType();
4809 break;
4810 case 'a':
4811 Type = Context.getBuiltinVaListType();
4812 assert(!Type.isNull() && "builtin va list type not initialized!");
4813 break;
4814 case 'A':
4815 // This is a "reference" to a va_list; however, what exactly
4816 // this means depends on how va_list is defined. There are two
4817 // different kinds of va_list: ones passed by value, and ones
4818 // passed by reference. An example of a by-value va_list is
4819 // x86, where va_list is a char*. An example of by-ref va_list
4820 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4821 // we want this argument to be a char*&; for x86-64, we want
4822 // it to be a __va_list_tag*.
4823 Type = Context.getBuiltinVaListType();
4824 assert(!Type.isNull() && "builtin va list type not initialized!");
4825 if (Type->isArrayType()) {
4826 Type = Context.getArrayDecayedType(Type);
4827 } else {
4828 Type = Context.getLValueReferenceType(Type);
4829 }
4830 break;
4831 case 'V': {
4832 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004833 unsigned NumElements = strtoul(Str, &End, 10);
4834 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004835
Chris Lattnerecd79c62009-06-14 00:45:47 +00004836 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004837
Chris Lattnerecd79c62009-06-14 00:45:47 +00004838 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00004839 // FIXME: Don't know what to do about AltiVec.
4840 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004841 break;
4842 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004843 case 'X': {
4844 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4845 Type = Context.getComplexType(ElementType);
4846 break;
4847 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004848 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004849 Type = Context.getFILEType();
4850 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004851 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004852 return QualType();
4853 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004854 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004855 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004856 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004857 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004858 else
4859 Type = Context.getjmp_bufType();
4860
Mike Stump2adb4da2009-07-28 23:47:15 +00004861 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004862 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004863 return QualType();
4864 }
4865 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004866 }
Mike Stump11289f42009-09-09 15:08:12 +00004867
Chris Lattnerecd79c62009-06-14 00:45:47 +00004868 if (!AllowTypeModifiers)
4869 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004870
Chris Lattnerecd79c62009-06-14 00:45:47 +00004871 Done = false;
4872 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00004873 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00004874 default: Done = true; --Str; break;
4875 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00004876 case '&':
John McCallb8b94662010-03-12 04:21:28 +00004877 {
4878 // Both pointers and references can have their pointee types
4879 // qualified with an address space.
4880 char *End;
4881 unsigned AddrSpace = strtoul(Str, &End, 10);
4882 if (End != Str && AddrSpace != 0) {
4883 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4884 Str = End;
4885 }
4886 }
4887 if (c == '*')
4888 Type = Context.getPointerType(Type);
4889 else
4890 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004891 break;
4892 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4893 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004894 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004895 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00004896 case 'D':
4897 Type = Context.getVolatileType(Type);
4898 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004899 }
4900 }
Mike Stump11289f42009-09-09 15:08:12 +00004901
Chris Lattnerecd79c62009-06-14 00:45:47 +00004902 return Type;
4903}
4904
4905/// GetBuiltinType - Return the type for the specified builtin.
4906QualType ASTContext::GetBuiltinType(unsigned id,
4907 GetBuiltinTypeError &Error) {
4908 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004909
Chris Lattnerecd79c62009-06-14 00:45:47 +00004910 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004911
Chris Lattnerecd79c62009-06-14 00:45:47 +00004912 Error = GE_None;
4913 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4914 if (Error != GE_None)
4915 return QualType();
4916 while (TypeStr[0] && TypeStr[0] != '.') {
4917 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4918 if (Error != GE_None)
4919 return QualType();
4920
4921 // Do array -> pointer decay. The builtin should use the decayed type.
4922 if (Ty->isArrayType())
4923 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004924
Chris Lattnerecd79c62009-06-14 00:45:47 +00004925 ArgTypes.push_back(Ty);
4926 }
4927
4928 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4929 "'.' should only occur at end of builtin type list!");
4930
4931 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4932 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4933 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00004934
4935 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00004936 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00004937 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004938 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00004939}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004940
4941QualType
4942ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4943 // Perform the usual unary conversions. We do this early so that
4944 // integral promotions to "int" can allow us to exit early, in the
4945 // lhs == rhs check. Also, for conversion purposes, we ignore any
4946 // qualifiers. For example, "const float" and "float" are
4947 // equivalent.
4948 if (lhs->isPromotableIntegerType())
4949 lhs = getPromotedIntegerType(lhs);
4950 else
4951 lhs = lhs.getUnqualifiedType();
4952 if (rhs->isPromotableIntegerType())
4953 rhs = getPromotedIntegerType(rhs);
4954 else
4955 rhs = rhs.getUnqualifiedType();
4956
4957 // If both types are identical, no conversion is needed.
4958 if (lhs == rhs)
4959 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004960
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004961 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4962 // The caller can deal with this (e.g. pointer + int).
4963 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4964 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004965
4966 // At this point, we have two different arithmetic types.
4967
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004968 // Handle complex types first (C99 6.3.1.8p1).
4969 if (lhs->isComplexType() || rhs->isComplexType()) {
4970 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00004971 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004972 // convert the rhs to the lhs complex type.
4973 return lhs;
4974 }
Mike Stump11289f42009-09-09 15:08:12 +00004975 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004976 // convert the lhs to the rhs complex type.
4977 return rhs;
4978 }
4979 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00004980 // When both operands are complex, the shorter operand is converted to the
4981 // type of the longer, and that is the type of the result. This corresponds
4982 // to what is done when combining two real floating-point operands.
4983 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004984 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00004985 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004986 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00004987 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004988 // "double _Complex" is promoted to "long double _Complex".
4989 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004990
4991 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004992 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004993 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004994 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00004995 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004996 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4997 // domains match. This is a requirement for our implementation, C99
4998 // does not require this promotion.
4999 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5000 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5001 return rhs;
5002 } else { // handle "_Complex double, double".
5003 return lhs;
5004 }
5005 }
5006 return lhs; // The domain/size match exactly.
5007 }
5008 // Now handle "real" floating types (i.e. float, double, long double).
5009 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5010 // if we have an integer operand, the result is the real floating type.
5011 if (rhs->isIntegerType()) {
5012 // convert rhs to the lhs floating point type.
5013 return lhs;
5014 }
5015 if (rhs->isComplexIntegerType()) {
5016 // convert rhs to the complex floating point type.
5017 return getComplexType(lhs);
5018 }
5019 if (lhs->isIntegerType()) {
5020 // convert lhs to the rhs floating point type.
5021 return rhs;
5022 }
Mike Stump11289f42009-09-09 15:08:12 +00005023 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005024 // convert lhs to the complex floating point type.
5025 return getComplexType(rhs);
5026 }
5027 // We have two real floating types, float/complex combos were handled above.
5028 // Convert the smaller operand to the bigger result.
5029 int result = getFloatingTypeOrder(lhs, rhs);
5030 if (result > 0) // convert the rhs
5031 return lhs;
5032 assert(result < 0 && "illegal float comparison");
5033 return rhs; // convert the lhs
5034 }
5035 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5036 // Handle GCC complex int extension.
5037 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5038 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5039
5040 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005041 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005042 rhsComplexInt->getElementType()) >= 0)
5043 return lhs; // convert the rhs
5044 return rhs;
5045 } else if (lhsComplexInt && rhs->isIntegerType()) {
5046 // convert the rhs to the lhs complex type.
5047 return lhs;
5048 } else if (rhsComplexInt && lhs->isIntegerType()) {
5049 // convert the lhs to the rhs complex type.
5050 return rhs;
5051 }
5052 }
5053 // Finally, we have two differing integer types.
5054 // The rules for this case are in C99 6.3.1.8
5055 int compare = getIntegerTypeOrder(lhs, rhs);
5056 bool lhsSigned = lhs->isSignedIntegerType(),
5057 rhsSigned = rhs->isSignedIntegerType();
5058 QualType destType;
5059 if (lhsSigned == rhsSigned) {
5060 // Same signedness; use the higher-ranked type
5061 destType = compare >= 0 ? lhs : rhs;
5062 } else if (compare != (lhsSigned ? 1 : -1)) {
5063 // The unsigned type has greater than or equal rank to the
5064 // signed type, so use the unsigned type
5065 destType = lhsSigned ? rhs : lhs;
5066 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5067 // The two types are different widths; if we are here, that
5068 // means the signed type is larger than the unsigned type, so
5069 // use the signed type.
5070 destType = lhsSigned ? lhs : rhs;
5071 } else {
5072 // The signed type is higher-ranked than the unsigned type,
5073 // but isn't actually any bigger (like unsigned int and long
5074 // on most 32-bit systems). Use the unsigned type corresponding
5075 // to the signed type.
5076 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5077 }
5078 return destType;
5079}