blob: 3a8084833e40b0ba5b08632635a2d30673796d6d [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerddc135e2006-11-10 06:34:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyck8c89d592009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff67391b82007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000021#include "clang/AST/ExternalASTSource.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000024#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000026#include "llvm/ADT/SmallString.h"
Anders Carlssond8499822007-10-29 05:01:08 +000027#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000028#include "llvm/Support/MathExtras.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000029#include "llvm/Support/raw_ostream.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000030#include "RecordLayoutBuilder.h"
31
Chris Lattnerddc135e2006-11-10 06:34:16 +000032using namespace clang;
33
Steve Naroff0af91202007-04-27 21:51:21 +000034enum FloatingRank {
35 FloatRank, DoubleRank, LongDoubleRank
36};
37
Chris Lattner465fa322008-10-05 17:34:18 +000038ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar1b444192009-11-13 05:51:54 +000039 const TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +000040 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +000041 Builtin::Context &builtins,
Mike Stump11289f42009-09-09 15:08:12 +000042 bool FreeMem, unsigned size_reserve) :
43 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Fariborz Jahaniane804c282010-04-23 17:41:07 +000044 NSConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +000045 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +000046 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
Douglas Gregor9507d462010-03-19 22:13:20 +000047 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000048 Idents(idents), Selectors(sels),
Ted Kremenek6aead3a2010-05-10 20:40:08 +000049 BuiltinInfo(builtins),
50 DeclarationNames(*this),
51 ExternalSource(0), PrintingPolicy(LOpts),
John McCallc62bb642010-03-24 05:22:00 +000052 LastSDM(0, 0) {
David Chisnall9f57c292009-08-17 16:35:33 +000053 ObjCIdRedefinitionType = QualType();
54 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian04b258c2009-11-25 23:07:42 +000055 ObjCSelRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +000056 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +000057 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +000058 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +000059}
60
Chris Lattnerd5973eb2006-11-12 00:53:46 +000061ASTContext::~ASTContext() {
Ted Kremenekda4e0d32010-02-11 07:12:28 +000062 // Release the DenseMaps associated with DeclContext objects.
63 // FIXME: Is this the ideal solution?
64 ReleaseDeclContextMaps();
Douglas Gregor832940b2010-03-02 23:58:15 +000065
66 // Release all of the memory associated with overridden C++ methods.
67 for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
68 OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
69 OM != OMEnd; ++OM)
70 OM->second.Destroy();
Ted Kremenekda4e0d32010-02-11 07:12:28 +000071
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000072 if (FreeMemory) {
73 // Deallocate all the types.
74 while (!Types.empty()) {
75 Types.back()->Destroy(*this);
76 Types.pop_back();
77 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +000078
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000079 for (llvm::FoldingSet<ExtQuals>::iterator
80 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
81 // Increment in loop to prevent using deallocated memory.
John McCall8ccfcb52009-09-24 19:53:00 +000082 Deallocate(&*I++);
Nuno Lopese013c7f2008-12-17 22:30:25 +000083 }
Nuno Lopese013c7f2008-12-17 22:30:25 +000084
Ted Kremenekc3015a92010-03-08 20:56:29 +000085 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
86 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
87 // Increment in loop to prevent using deallocated memory.
88 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
89 R->Destroy(*this);
90 }
Ted Kremenek40ee0cc2009-12-23 21:13:52 +000091
Ted Kremenekc3015a92010-03-08 20:56:29 +000092 for (llvm::DenseMap<const ObjCContainerDecl*,
93 const ASTRecordLayout*>::iterator
94 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
95 // Increment in loop to prevent using deallocated memory.
96 if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
97 R->Destroy(*this);
98 }
Nuno Lopese013c7f2008-12-17 22:30:25 +000099 }
100
Douglas Gregorf21eb492009-03-26 23:50:42 +0000101 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000102 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
103 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000104 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000105 NNS != NNSEnd; ) {
106 // Increment in loop to prevent using deallocated memory.
Douglas Gregorc741fb12009-03-27 23:54:10 +0000107 (*NNS++).Destroy(*this);
Ted Kremenek40ee0cc2009-12-23 21:13:52 +0000108 }
Douglas Gregorf21eb492009-03-26 23:50:42 +0000109
110 if (GlobalNestedNameSpecifier)
111 GlobalNestedNameSpecifier->Destroy(*this);
112
Ted Kremenek6aead3a2010-05-10 20:40:08 +0000113 // Deallocate the memory associated with the DeclarationNameTable.
114 DeclarationNames.DoDestroy(*this);
115
Eli Friedmane2bbfe22008-05-27 03:08:09 +0000116 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000117}
118
Mike Stump11289f42009-09-09 15:08:12 +0000119void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
121 ExternalSource.reset(Source.take());
122}
123
Chris Lattner4eb445d2007-01-26 01:27:23 +0000124void ASTContext::PrintStats() const {
125 fprintf(stderr, "*** AST Context Stats:\n");
126 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000127
Douglas Gregora30d0462009-05-26 14:40:08 +0000128 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000129#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000130#define ABSTRACT_TYPE(Name, Parent)
131#include "clang/AST/TypeNodes.def"
132 0 // Extra
133 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000134
Chris Lattner4eb445d2007-01-26 01:27:23 +0000135 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
136 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000137 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000138 }
139
Douglas Gregora30d0462009-05-26 14:40:08 +0000140 unsigned Idx = 0;
141 unsigned TotalBytes = 0;
142#define TYPE(Name, Parent) \
143 if (counts[Idx]) \
144 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
145 TotalBytes += counts[Idx] * sizeof(Name##Type); \
146 ++Idx;
147#define ABSTRACT_TYPE(Name, Parent)
148#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000149
Douglas Gregora30d0462009-05-26 14:40:08 +0000150 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151
152 if (ExternalSource.get()) {
153 fprintf(stderr, "\n");
154 ExternalSource->PrintStats();
155 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000156}
157
158
John McCall48f2d582009-10-23 23:03:21 +0000159void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000160 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000161 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000162 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000163}
164
Chris Lattner970e54e2006-11-12 00:37:36 +0000165void ASTContext::InitBuiltinTypes() {
166 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattner970e54e2006-11-12 00:37:36 +0000168 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000169 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000170
Chris Lattner970e54e2006-11-12 00:37:36 +0000171 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000172 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000173 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000174 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000175 InitBuiltinType(CharTy, BuiltinType::Char_S);
176 else
177 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000178 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000179 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
180 InitBuiltinType(ShortTy, BuiltinType::Short);
181 InitBuiltinType(IntTy, BuiltinType::Int);
182 InitBuiltinType(LongTy, BuiltinType::Long);
183 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattner970e54e2006-11-12 00:37:36 +0000185 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000186 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
187 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
188 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
189 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
190 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000191
Chris Lattner970e54e2006-11-12 00:37:36 +0000192 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000193 InitBuiltinType(FloatTy, BuiltinType::Float);
194 InitBuiltinType(DoubleTy, BuiltinType::Double);
195 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000196
Chris Lattnerf122cef2009-04-30 02:43:43 +0000197 // GNU extension, 128-bit integers.
198 InitBuiltinType(Int128Ty, BuiltinType::Int128);
199 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
200
Chris Lattner007cb022009-02-26 23:43:47 +0000201 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
202 InitBuiltinType(WCharTy, BuiltinType::WChar);
203 else // C99
204 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000205
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000206 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
207 InitBuiltinType(Char16Ty, BuiltinType::Char16);
208 else // C99
209 Char16Ty = getFromTargetType(Target.getChar16Type());
210
211 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
212 InitBuiltinType(Char32Ty, BuiltinType::Char32);
213 else // C99
214 Char32Ty = getFromTargetType(Target.getChar32Type());
215
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000216 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000217 InitBuiltinType(OverloadTy, BuiltinType::Overload);
218
219 // Placeholder type for type-dependent expressions whose type is
220 // completely unknown. No code should ever check a type against
221 // DependentTy and users should never see it; however, it is here to
222 // help diagnose failures to properly check for type-dependent
223 // expressions.
224 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000225
Mike Stump11289f42009-09-09 15:08:12 +0000226 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000227 // not yet been deduced.
228 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000229
Chris Lattner970e54e2006-11-12 00:37:36 +0000230 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000231 FloatComplexTy = getComplexType(FloatTy);
232 DoubleComplexTy = getComplexType(DoubleTy);
233 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000234
Steve Naroff66e9f332007-10-15 14:41:52 +0000235 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000236
Steve Naroff1329fa02009-07-15 18:40:39 +0000237 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
238 ObjCIdTypedefType = QualType();
239 ObjCClassTypedefType = QualType();
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000240 ObjCSelTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000241
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000242 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroff1329fa02009-07-15 18:40:39 +0000243 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
244 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +0000245 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000246
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000247 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000248
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000249 // void * type
250 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000251
252 // nullptr type (C++0x 2.14.7)
253 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000254}
255
Douglas Gregor86d142a2009-10-08 07:24:58 +0000256MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000257ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000258 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000259 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000260 = InstantiatedFromStaticDataMember.find(Var);
261 if (Pos == InstantiatedFromStaticDataMember.end())
262 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000264 return Pos->second;
265}
266
Mike Stump11289f42009-09-09 15:08:12 +0000267void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000268ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
269 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000270 assert(Inst->isStaticDataMember() && "Not a static data member");
271 assert(Tmpl->isStaticDataMember() && "Not a static data member");
272 assert(!InstantiatedFromStaticDataMember[Inst] &&
273 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000274 InstantiatedFromStaticDataMember[Inst]
275 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000276}
277
John McCalle61f2ba2009-11-18 02:36:19 +0000278NamedDecl *
John McCallb96ec562009-12-04 22:46:56 +0000279ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCalle61f2ba2009-11-18 02:36:19 +0000280 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCallb96ec562009-12-04 22:46:56 +0000281 = InstantiatedFromUsingDecl.find(UUD);
282 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000283 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000284
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000285 return Pos->second;
286}
287
288void
John McCallb96ec562009-12-04 22:46:56 +0000289ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
290 assert((isa<UsingDecl>(Pattern) ||
291 isa<UnresolvedUsingValueDecl>(Pattern) ||
292 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
293 "pattern decl is not a using decl");
294 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
295 InstantiatedFromUsingDecl[Inst] = Pattern;
296}
297
298UsingShadowDecl *
299ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
300 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
301 = InstantiatedFromUsingShadowDecl.find(Inst);
302 if (Pos == InstantiatedFromUsingShadowDecl.end())
303 return 0;
304
305 return Pos->second;
306}
307
308void
309ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
310 UsingShadowDecl *Pattern) {
311 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
312 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000313}
314
Anders Carlsson5da84842009-09-01 04:26:58 +0000315FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
316 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
317 = InstantiatedFromUnnamedFieldDecl.find(Field);
318 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
319 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000320
Anders Carlsson5da84842009-09-01 04:26:58 +0000321 return Pos->second;
322}
323
324void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
325 FieldDecl *Tmpl) {
326 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
327 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
328 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
329 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000330
Anders Carlsson5da84842009-09-01 04:26:58 +0000331 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
332}
333
Douglas Gregor832940b2010-03-02 23:58:15 +0000334ASTContext::overridden_cxx_method_iterator
335ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
336 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
337 = OverriddenMethods.find(Method);
338 if (Pos == OverriddenMethods.end())
339 return 0;
340
341 return Pos->second.begin();
342}
343
344ASTContext::overridden_cxx_method_iterator
345ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
346 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
347 = OverriddenMethods.find(Method);
348 if (Pos == OverriddenMethods.end())
349 return 0;
350
351 return Pos->second.end();
352}
353
354void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
355 const CXXMethodDecl *Overridden) {
356 OverriddenMethods[Method].push_back(Overridden);
357}
358
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000359namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000360 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000361 : std::binary_function<SourceRange, SourceRange, bool> {
362 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000364 public:
365 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000366
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000367 bool operator()(SourceRange X, SourceRange Y) {
368 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
369 }
370 };
371}
372
Chris Lattner53cfe802007-07-18 17:52:12 +0000373//===----------------------------------------------------------------------===//
374// Type Sizing and Analysis
375//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000376
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000377/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
378/// scalar floating point type.
379const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000380 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000381 assert(BT && "Not a floating point type!");
382 switch (BT->getKind()) {
383 default: assert(0 && "Not a floating point type!");
384 case BuiltinType::Float: return Target.getFloatFormat();
385 case BuiltinType::Double: return Target.getDoubleFormat();
386 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
387 }
388}
389
Ken Dyck160146e2010-01-27 17:10:57 +0000390/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000391/// specified decl. Note that bitfields do not have a valid alignment, so
392/// this method will assert on them.
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000393/// If @p RefAsPointee, references are treated like their underlying type
394/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck160146e2010-01-27 17:10:57 +0000395CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000396 unsigned Align = Target.getCharWidth();
397
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000398 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Alexis Hunt96d5c762009-11-21 08:43:09 +0000399 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedman19a546c2009-02-22 02:56:25 +0000400
Chris Lattner68061312009-01-24 21:53:27 +0000401 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
402 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000403 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000404 if (RefAsPointee)
405 T = RT->getPointeeType();
406 else
407 T = getPointerType(RT->getPointeeType());
408 }
409 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000410 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000411 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
412 T = cast<ArrayType>(T)->getElementType();
413
414 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
415 }
Charles Davis3fc51072010-02-23 04:52:00 +0000416 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
417 // In the case of a field in a packed struct, we want the minimum
418 // of the alignment of the field and the alignment of the struct.
419 Align = std::min(Align,
420 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
421 }
Chris Lattner68061312009-01-24 21:53:27 +0000422 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000423
Ken Dyck160146e2010-01-27 17:10:57 +0000424 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattner68061312009-01-24 21:53:27 +0000425}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000426
Chris Lattner983a8bb2007-07-13 22:13:22 +0000427/// getTypeSize - Return the size of the specified type, in bits. This method
428/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000429///
430/// FIXME: Pointers into different addr spaces could have different sizes and
431/// alignment requirements: getPointerInfo should take an AddrSpace, this
432/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000433std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000434ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000435 uint64_t Width=0;
436 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000437 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000438#define TYPE(Class, Base)
439#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000440#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000441#define DEPENDENT_TYPE(Class, Base) case Type::Class:
442#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000443 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000444 break;
445
Chris Lattner355332d2007-07-13 22:27:08 +0000446 case Type::FunctionNoProto:
447 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000448 // GCC extension: alignof(function) = 32 bits
449 Width = 0;
450 Align = 32;
451 break;
452
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000453 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000454 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000455 Width = 0;
456 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
457 break;
458
Steve Naroff5c131802007-08-30 01:06:46 +0000459 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000460 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattner37e05872008-03-05 18:54:05 +0000462 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000463 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000464 Align = EltInfo.second;
465 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000466 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000467 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000468 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000469 const VectorType *VT = cast<VectorType>(T);
470 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
471 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000472 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000473 // If the alignment is not a power of 2, round up to the next power of 2.
474 // This happens for non-power-of-2 length vectors.
Dan Gohmanef78c8e2010-04-21 23:32:43 +0000475 if (Align & (Align-1)) {
Chris Lattner63d2b362009-10-22 05:17:15 +0000476 Align = llvm::NextPowerOf2(Align);
477 Width = llvm::RoundUpToAlignment(Width, Align);
478 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000479 break;
480 }
Chris Lattner647fb222007-07-18 18:26:58 +0000481
Chris Lattner7570e9c2008-03-08 08:52:55 +0000482 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000483 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000484 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000485 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000486 // GCC extension: alignof(void) = 8 bits.
487 Width = 0;
488 Align = 8;
489 break;
490
Chris Lattner6a4f7452007-12-19 19:23:28 +0000491 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000492 Width = Target.getBoolWidth();
493 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000494 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000495 case BuiltinType::Char_S:
496 case BuiltinType::Char_U:
497 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000498 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000499 Width = Target.getCharWidth();
500 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000501 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000502 case BuiltinType::WChar:
503 Width = Target.getWCharWidth();
504 Align = Target.getWCharAlign();
505 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000506 case BuiltinType::Char16:
507 Width = Target.getChar16Width();
508 Align = Target.getChar16Align();
509 break;
510 case BuiltinType::Char32:
511 Width = Target.getChar32Width();
512 Align = Target.getChar32Align();
513 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000514 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000515 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000516 Width = Target.getShortWidth();
517 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000518 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000519 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000520 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000521 Width = Target.getIntWidth();
522 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000523 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000524 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000525 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000526 Width = Target.getLongWidth();
527 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000528 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000529 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000530 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000531 Width = Target.getLongLongWidth();
532 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000533 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000534 case BuiltinType::Int128:
535 case BuiltinType::UInt128:
536 Width = 128;
537 Align = 128; // int128_t is 128-bit aligned on all targets.
538 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000539 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000540 Width = Target.getFloatWidth();
541 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000542 break;
543 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000544 Width = Target.getDoubleWidth();
545 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000546 break;
547 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000548 Width = Target.getLongDoubleWidth();
549 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000550 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000551 case BuiltinType::NullPtr:
552 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
553 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000554 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000555 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000556 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000557 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000558 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000559 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000560 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000561 case Type::BlockPointer: {
562 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
563 Width = Target.getPointerWidth(AS);
564 Align = Target.getPointerAlign(AS);
565 break;
566 }
Sebastian Redl22e2e5c2009-11-23 17:18:46 +0000567 case Type::LValueReference:
568 case Type::RValueReference: {
569 // alignof and sizeof should never enter this code path here, so we go
570 // the pointer route.
571 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
572 Width = Target.getPointerWidth(AS);
573 Align = Target.getPointerAlign(AS);
574 break;
575 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000576 case Type::Pointer: {
577 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000578 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000579 Align = Target.getPointerAlign(AS);
580 break;
581 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000582 case Type::MemberPointer: {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000583 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000584 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000585 getTypeInfo(getPointerDiffType());
586 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000587 if (Pointee->isFunctionType())
588 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000589 Align = PtrDiffInfo.second;
590 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000591 }
Chris Lattner647fb222007-07-18 18:26:58 +0000592 case Type::Complex: {
593 // Complex types have the same alignment as their elements, but twice the
594 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000595 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000596 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000597 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000598 Align = EltInfo.second;
599 break;
600 }
Devang Pateldbb72632008-06-04 21:54:36 +0000601 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000602 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000603 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
604 Width = Layout.getSize();
605 Align = Layout.getAlignment();
606 break;
607 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000608 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000609 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000610 const TagType *TT = cast<TagType>(T);
611
612 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000613 Width = 1;
614 Align = 1;
615 break;
616 }
Mike Stump11289f42009-09-09 15:08:12 +0000617
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000618 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000619 return getTypeInfo(ET->getDecl()->getIntegerType());
620
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000621 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000622 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
623 Width = Layout.getSize();
624 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000625 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000626 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000627
Chris Lattner63d2b362009-10-22 05:17:15 +0000628 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000629 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
630 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000631
Chris Lattner63d2b362009-10-22 05:17:15 +0000632 case Type::Elaborated:
633 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
634 .getTypePtr());
John McCallfcc33b02009-09-05 00:15:47 +0000635
Douglas Gregoref462e62009-04-30 17:32:17 +0000636 case Type::Typedef: {
637 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000638 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000639 Align = std::max(Aligned->getMaxAlignment(),
640 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregoref462e62009-04-30 17:32:17 +0000641 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
642 } else
643 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000644 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000645 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000646
647 case Type::TypeOfExpr:
648 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
649 .getTypePtr());
650
651 case Type::TypeOf:
652 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
653
Anders Carlsson81df7b82009-06-24 19:06:50 +0000654 case Type::Decltype:
655 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
656 .getTypePtr());
657
Douglas Gregoref462e62009-04-30 17:32:17 +0000658 case Type::QualifiedName:
659 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000660
Douglas Gregoref462e62009-04-30 17:32:17 +0000661 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000662 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000663 "Cannot request the size of a dependent type");
664 // FIXME: this is likely to be wrong once we support template
665 // aliases, since a template alias could refer to a typedef that
666 // has an __aligned__ attribute on it.
667 return getTypeInfo(getCanonicalType(T));
668 }
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattner53cfe802007-07-18 17:52:12 +0000670 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000671 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000672}
673
Ken Dyck8c89d592009-12-22 14:23:30 +0000674/// getTypeSizeInChars - Return the size of the specified type, in characters.
675/// This method does not work on incomplete types.
676CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck40775002010-01-11 17:06:35 +0000677 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000678}
679CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck40775002010-01-11 17:06:35 +0000680 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyck8c89d592009-12-22 14:23:30 +0000681}
682
Ken Dycka6046ab2010-01-26 17:25:18 +0000683/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck24d28d62010-01-26 17:22:55 +0000684/// characters. This method does not work on incomplete types.
685CharUnits ASTContext::getTypeAlignInChars(QualType T) {
686 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
687}
688CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
689 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
690}
691
Chris Lattnera3402cd2009-01-27 18:08:34 +0000692/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
693/// type for the current target in bits. This can be different than the ABI
694/// alignment in cases where it is beneficial for performance to overalign
695/// a data type.
696unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
697 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000698
699 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000700 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000701 T = CT->getElementType().getTypePtr();
702 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
703 T->isSpecificBuiltinType(BuiltinType::LongLong))
704 return std::max(ABIAlign, (unsigned)getTypeSize(T));
705
Chris Lattnera3402cd2009-01-27 18:08:34 +0000706 return ABIAlign;
707}
708
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000709static void CollectLocalObjCIvars(ASTContext *Ctx,
710 const ObjCInterfaceDecl *OI,
711 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000712 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
713 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000714 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000715 if (!IVDecl->isInvalidDecl())
716 Fields.push_back(cast<FieldDecl>(IVDecl));
717 }
718}
719
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000720void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
721 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
722 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
723 CollectObjCIvars(SuperClass, Fields);
724 CollectLocalObjCIvars(this, OI, Fields);
725}
726
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000727/// ShallowCollectObjCIvars -
728/// Collect all ivars, including those synthesized, in the current class.
729///
730void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000731 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000732 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
733 E = OI->ivar_end(); I != E; ++I) {
734 Ivars.push_back(*I);
735 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000736
737 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000738}
739
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000740/// CollectNonClassIvars -
741/// This routine collects all other ivars which are not declared in the class.
Ted Kremenek86838aa2010-03-11 19:44:54 +0000742/// This includes synthesized ivars (via @synthesize) and those in
743// class's @implementation.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000744///
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000745void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000746 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanianafe13862010-02-23 01:26:30 +0000747 // Find ivars declared in class extension.
748 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
749 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
750 E = CDecl->ivar_end(); I != E; ++I) {
751 Ivars.push_back(*I);
752 }
753 }
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000754
Ted Kremenek86838aa2010-03-11 19:44:54 +0000755 // Also add any ivar defined in this class's implementation. This
756 // includes synthesized ivars.
Fariborz Jahanianaef66222010-02-19 00:31:17 +0000757 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
758 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
759 E = ImplDecl->ivar_end(); I != E; ++I)
760 Ivars.push_back(*I);
761 }
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000762}
763
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000764/// CollectInheritedProtocols - Collect all protocols in current class and
765/// those inherited by it.
766void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000767 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000768 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
769 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
770 PE = OI->protocol_end(); P != PE; ++P) {
771 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000772 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000773 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000774 PE = Proto->protocol_end(); P != PE; ++P) {
775 Protocols.insert(*P);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000776 CollectInheritedProtocols(*P, Protocols);
777 }
Fariborz Jahanian8e3b9db2010-02-25 18:24:33 +0000778 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000779
780 // Categories of this Interface.
781 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
782 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
783 CollectInheritedProtocols(CDeclChain, Protocols);
784 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
785 while (SD) {
786 CollectInheritedProtocols(SD, Protocols);
787 SD = SD->getSuperClass();
788 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000789 } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000790 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
791 PE = OC->protocol_end(); P != PE; ++P) {
792 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000793 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000794 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
795 PE = Proto->protocol_end(); P != PE; ++P)
796 CollectInheritedProtocols(*P, Protocols);
797 }
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000798 } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000799 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
800 PE = OP->protocol_end(); P != PE; ++P) {
801 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +0000802 Protocols.insert(Proto);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000803 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
804 PE = Proto->protocol_end(); P != PE; ++P)
805 CollectInheritedProtocols(*P, Protocols);
806 }
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +0000807 }
808}
809
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000810unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) {
811 unsigned count = 0;
812 // Count ivars declared in class extension.
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000813 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension())
814 count += CDecl->ivar_size();
815
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000816 // Count ivar defined in this class's implementation. This
817 // includes synthesized ivars.
818 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
Benjamin Kramer0a3fe042010-04-27 17:47:25 +0000819 count += ImplDecl->ivar_size();
820
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000821 return count;
822}
823
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000824/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
825ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
826 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
827 I = ObjCImpls.find(D);
828 if (I != ObjCImpls.end())
829 return cast<ObjCImplementationDecl>(I->second);
830 return 0;
831}
832/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
833ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
834 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
835 I = ObjCImpls.find(D);
836 if (I != ObjCImpls.end())
837 return cast<ObjCCategoryImplDecl>(I->second);
838 return 0;
839}
840
841/// \brief Set the implementation of ObjCInterfaceDecl.
842void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
843 ObjCImplementationDecl *ImplD) {
844 assert(IFaceD && ImplD && "Passed null params");
845 ObjCImpls[IFaceD] = ImplD;
846}
847/// \brief Set the implementation of ObjCCategoryDecl.
848void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
849 ObjCCategoryImplDecl *ImplD) {
850 assert(CatD && ImplD && "Passed null params");
851 ObjCImpls[CatD] = ImplD;
852}
853
John McCallbcd03502009-12-07 02:54:59 +0000854/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000855///
John McCallbcd03502009-12-07 02:54:59 +0000856/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000857/// the TypeLoc wrappers.
858///
859/// \param T the type that will be the basis for type source info. This type
860/// should refer to how the declarator was written in source code, not to
861/// what type semantic analysis resolved the declarator to.
John McCallbcd03502009-12-07 02:54:59 +0000862TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall26fe7e02009-10-21 00:23:54 +0000863 unsigned DataSize) {
864 if (!DataSize)
865 DataSize = TypeLoc::getFullDataSizeForType(T);
866 else
867 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCallbcd03502009-12-07 02:54:59 +0000868 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall26fe7e02009-10-21 00:23:54 +0000869
John McCallbcd03502009-12-07 02:54:59 +0000870 TypeSourceInfo *TInfo =
871 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
872 new (TInfo) TypeSourceInfo(T);
873 return TInfo;
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000874}
875
John McCallbcd03502009-12-07 02:54:59 +0000876TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCall3665e002009-10-23 21:14:09 +0000877 SourceLocation L) {
John McCallbcd03502009-12-07 02:54:59 +0000878 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCall3665e002009-10-23 21:14:09 +0000879 DI->getTypeLoc().initialize(L);
880 return DI;
881}
882
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000883/// getInterfaceLayoutImpl - Get or compute information about the
884/// layout of the given interface.
885///
886/// \param Impl - If given, also include the layout of the interface's
887/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000888const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000889ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
890 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000891 assert(!D->isForwardDecl() && "Invalid interface decl!");
892
Devang Pateldbb72632008-06-04 21:54:36 +0000893 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000894 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000895 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
896 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
897 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000898
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000899 // Add in synthesized ivar count if laying out an implementation.
900 if (Impl) {
Fariborz Jahaniand2ae2d02010-03-22 18:25:57 +0000901 unsigned SynthCount = CountNonClassIvars(D);
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000902 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000903 // entry. Note we can't cache this because we simply free all
904 // entries later; however we shouldn't look up implementations
905 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000906 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000907 return getObjCLayout(D, 0);
908 }
909
Mike Stump11289f42009-09-09 15:08:12 +0000910 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000911 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
912 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000913
Devang Pateldbb72632008-06-04 21:54:36 +0000914 return *NewEntry;
915}
916
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000917const ASTRecordLayout &
918ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
919 return getObjCLayout(D, 0);
920}
921
922const ASTRecordLayout &
923ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
924 return getObjCLayout(D->getClassInterface(), D);
925}
926
Devang Patele11664a2007-11-01 19:11:01 +0000927/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +0000928/// specified record (struct/union/class), which indicates its size and field
929/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +0000930const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000931 D = D->getDefinition();
Ted Kremenek21475702008-09-05 17:16:31 +0000932 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +0000933
Chris Lattner53cfe802007-07-18 17:52:12 +0000934 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +0000935 // Note that we can't save a reference to the entry because this function
936 // is recursive.
937 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +0000938 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +0000939
Mike Stump11289f42009-09-09 15:08:12 +0000940 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000941 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +0000942 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000943
Daniel Dunbarccabe482010-04-19 20:44:53 +0000944 if (getLangOptions().DumpRecordLayouts) {
945 llvm::errs() << "\n*** Dumping AST Record Layout\n";
946 DumpRecordLayout(D, llvm::errs());
947 }
948
Chris Lattner647fb222007-07-18 18:26:58 +0000949 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +0000950}
951
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000952const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor0a5a2212010-02-11 01:04:33 +0000953 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlsson5ebf8b42009-12-07 04:35:11 +0000954 assert(RD && "Cannot get key function for forward declarations!");
955
956 const CXXMethodDecl *&Entry = KeyFunctions[RD];
957 if (!Entry)
958 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
959 else
960 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
961 "Key function changed!");
962
963 return Entry;
964}
965
Chris Lattner983a8bb2007-07-13 22:13:22 +0000966//===----------------------------------------------------------------------===//
967// Type creation/memoization methods
968//===----------------------------------------------------------------------===//
969
John McCall8ccfcb52009-09-24 19:53:00 +0000970QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
971 unsigned Fast = Quals.getFastQualifiers();
972 Quals.removeFastQualifiers();
973
974 // Check if we've already instantiated this type.
975 llvm::FoldingSetNodeID ID;
976 ExtQuals::Profile(ID, TypeNode, Quals);
977 void *InsertPos = 0;
978 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
979 assert(EQ->getQualifiers() == Quals);
980 QualType T = QualType(EQ, Fast);
981 return T;
982 }
983
John McCall90d1c2d2009-09-24 23:30:46 +0000984 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000985 ExtQualNodes.InsertNode(New, InsertPos);
986 QualType T = QualType(New, Fast);
987 return T;
988}
989
990QualType ASTContext::getVolatileType(QualType T) {
991 QualType CanT = getCanonicalType(T);
992 if (CanT.isVolatileQualified()) return T;
993
994 QualifierCollector Quals;
995 const Type *TypeNode = Quals.strip(T);
996 Quals.addVolatile();
997
998 return getExtQualType(TypeNode, Quals);
999}
1000
Fariborz Jahanianece85822009-02-17 18:27:45 +00001001QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001002 QualType CanT = getCanonicalType(T);
1003 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001004 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001005
John McCall8ccfcb52009-09-24 19:53:00 +00001006 // If we are composing extended qualifiers together, merge together
1007 // into one ExtQuals node.
1008 QualifierCollector Quals;
1009 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001010
John McCall8ccfcb52009-09-24 19:53:00 +00001011 // If this type already has an address space specified, it cannot get
1012 // another one.
1013 assert(!Quals.hasAddressSpace() &&
1014 "Type cannot be in multiple addr spaces!");
1015 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001016
John McCall8ccfcb52009-09-24 19:53:00 +00001017 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001018}
1019
Chris Lattnerd60183d2009-02-18 22:53:11 +00001020QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001021 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001022 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001023 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001024 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001025
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001026 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001027 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001028 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001029 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1030 return getPointerType(ResultType);
1031 }
1032 }
Mike Stump11289f42009-09-09 15:08:12 +00001033
John McCall8ccfcb52009-09-24 19:53:00 +00001034 // If we are composing extended qualifiers together, merge together
1035 // into one ExtQuals node.
1036 QualifierCollector Quals;
1037 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001038
John McCall8ccfcb52009-09-24 19:53:00 +00001039 // If this type already has an ObjCGC specified, it cannot get
1040 // another one.
1041 assert(!Quals.hasObjCGCAttr() &&
1042 "Type cannot have multiple ObjCGCs!");
1043 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001044
John McCall8ccfcb52009-09-24 19:53:00 +00001045 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001046}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001047
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001048static QualType getExtFunctionType(ASTContext& Context, QualType T,
1049 const FunctionType::ExtInfo &Info) {
John McCall8ccfcb52009-09-24 19:53:00 +00001050 QualType ResultType;
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001051 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1052 QualType Pointee = Pointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001053 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001054 if (ResultType == Pointee)
1055 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001056
1057 ResultType = Context.getPointerType(ResultType);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001058 } else if (const BlockPointerType *BlockPointer
1059 = T->getAs<BlockPointerType>()) {
1060 QualType Pointee = BlockPointer->getPointeeType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001061 ResultType = getExtFunctionType(Context, Pointee, Info);
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001062 if (ResultType == Pointee)
1063 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001064
1065 ResultType = Context.getBlockPointerType(ResultType);
1066 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001067 if (F->getExtInfo() == Info)
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001068 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001069
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001070 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregor8c940862010-01-18 17:14:39 +00001071 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001072 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001073 } else {
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001074 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall8ccfcb52009-09-24 19:53:00 +00001075 ResultType
Douglas Gregor8c940862010-01-18 17:14:39 +00001076 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1077 FPT->getNumArgs(), FPT->isVariadic(),
1078 FPT->getTypeQuals(),
1079 FPT->hasExceptionSpec(),
1080 FPT->hasAnyExceptionSpec(),
1081 FPT->getNumExceptions(),
1082 FPT->exception_begin(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001083 Info);
John McCall8ccfcb52009-09-24 19:53:00 +00001084 }
Douglas Gregor40cb9ad2009-12-09 00:47:37 +00001085 } else
1086 return T;
Douglas Gregor8c940862010-01-18 17:14:39 +00001087
1088 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1089}
1090
1091QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001092 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001093 return getExtFunctionType(*this, T,
1094 Info.withNoReturn(AddNoReturn));
Douglas Gregor8c940862010-01-18 17:14:39 +00001095}
1096
1097QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001098 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001099 return getExtFunctionType(*this, T,
1100 Info.withCallingConv(CallConv));
Mike Stump8c5d7992009-07-25 21:26:53 +00001101}
1102
Rafael Espindola49b85ab2010-03-30 22:15:11 +00001103QualType ASTContext::getRegParmType(QualType T, unsigned RegParm) {
1104 FunctionType::ExtInfo Info = getFunctionExtInfo(T);
1105 return getExtFunctionType(*this, T,
1106 Info.withRegParm(RegParm));
1107}
1108
Chris Lattnerc6395932007-06-22 20:56:16 +00001109/// getComplexType - Return the uniqued reference to the type for a complex
1110/// number with the specified element type.
1111QualType ASTContext::getComplexType(QualType T) {
1112 // Unique pointers, to guarantee there is only one pointer of a particular
1113 // structure.
1114 llvm::FoldingSetNodeID ID;
1115 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001116
Chris Lattnerc6395932007-06-22 20:56:16 +00001117 void *InsertPos = 0;
1118 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1119 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001120
Chris Lattnerc6395932007-06-22 20:56:16 +00001121 // If the pointee type isn't canonical, this won't be a canonical type either,
1122 // so fill in the canonical type field.
1123 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001124 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001125 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattnerc6395932007-06-22 20:56:16 +00001127 // Get the new insert position for the node we care about.
1128 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001129 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001130 }
John McCall90d1c2d2009-09-24 23:30:46 +00001131 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001132 Types.push_back(New);
1133 ComplexTypes.InsertNode(New, InsertPos);
1134 return QualType(New, 0);
1135}
1136
Chris Lattner970e54e2006-11-12 00:37:36 +00001137/// getPointerType - Return the uniqued reference to the type for a pointer to
1138/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001139QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001140 // Unique pointers, to guarantee there is only one pointer of a particular
1141 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001142 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001143 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001144
Chris Lattner67521df2007-01-27 01:29:36 +00001145 void *InsertPos = 0;
1146 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001147 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001148
Chris Lattner7ccecb92006-11-12 08:50:50 +00001149 // If the pointee type isn't canonical, this won't be a canonical type either,
1150 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001151 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001152 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001153 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001154
Chris Lattner67521df2007-01-27 01:29:36 +00001155 // Get the new insert position for the node we care about.
1156 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001157 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001158 }
John McCall90d1c2d2009-09-24 23:30:46 +00001159 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001160 Types.push_back(New);
1161 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001162 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001163}
1164
Mike Stump11289f42009-09-09 15:08:12 +00001165/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001166/// a pointer to the specified block.
1167QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001168 assert(T->isFunctionType() && "block of function types only");
1169 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001170 // structure.
1171 llvm::FoldingSetNodeID ID;
1172 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001173
Steve Naroffec33ed92008-08-27 16:04:49 +00001174 void *InsertPos = 0;
1175 if (BlockPointerType *PT =
1176 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1177 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001178
1179 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001180 // type either so fill in the canonical type field.
1181 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001182 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001183 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001184
Steve Naroffec33ed92008-08-27 16:04:49 +00001185 // Get the new insert position for the node we care about.
1186 BlockPointerType *NewIP =
1187 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001188 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001189 }
John McCall90d1c2d2009-09-24 23:30:46 +00001190 BlockPointerType *New
1191 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001192 Types.push_back(New);
1193 BlockPointerTypes.InsertNode(New, InsertPos);
1194 return QualType(New, 0);
1195}
1196
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001197/// getLValueReferenceType - Return the uniqued reference to the type for an
1198/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001199QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001200 // Unique pointers, to guarantee there is only one pointer of a particular
1201 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001202 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001203 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001204
1205 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001206 if (LValueReferenceType *RT =
1207 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001208 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001209
John McCallfc93cf92009-10-22 22:37:11 +00001210 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1211
Bill Wendling3708c182007-05-27 10:15:43 +00001212 // If the referencee type isn't canonical, this won't be a canonical type
1213 // either, so fill in the canonical type field.
1214 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001215 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1216 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1217 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001218
Bill Wendling3708c182007-05-27 10:15:43 +00001219 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001220 LValueReferenceType *NewIP =
1221 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001222 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001223 }
1224
John McCall90d1c2d2009-09-24 23:30:46 +00001225 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001226 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1227 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001228 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001229 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001230
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001231 return QualType(New, 0);
1232}
1233
1234/// getRValueReferenceType - Return the uniqued reference to the type for an
1235/// rvalue reference to the specified type.
1236QualType ASTContext::getRValueReferenceType(QualType T) {
1237 // Unique pointers, to guarantee there is only one pointer of a particular
1238 // structure.
1239 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001240 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001241
1242 void *InsertPos = 0;
1243 if (RValueReferenceType *RT =
1244 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1245 return QualType(RT, 0);
1246
John McCallfc93cf92009-10-22 22:37:11 +00001247 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1248
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001249 // If the referencee type isn't canonical, this won't be a canonical type
1250 // either, so fill in the canonical type field.
1251 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001252 if (InnerRef || !T.isCanonical()) {
1253 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1254 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001255
1256 // Get the new insert position for the node we care about.
1257 RValueReferenceType *NewIP =
1258 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1259 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1260 }
1261
John McCall90d1c2d2009-09-24 23:30:46 +00001262 RValueReferenceType *New
1263 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001264 Types.push_back(New);
1265 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001266 return QualType(New, 0);
1267}
1268
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001269/// getMemberPointerType - Return the uniqued reference to the type for a
1270/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001271QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001272 // Unique pointers, to guarantee there is only one pointer of a particular
1273 // structure.
1274 llvm::FoldingSetNodeID ID;
1275 MemberPointerType::Profile(ID, T, Cls);
1276
1277 void *InsertPos = 0;
1278 if (MemberPointerType *PT =
1279 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1280 return QualType(PT, 0);
1281
1282 // If the pointee or class type isn't canonical, this won't be a canonical
1283 // type either, so fill in the canonical type field.
1284 QualType Canonical;
Douglas Gregor615ac672009-11-04 16:49:01 +00001285 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001286 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1287
1288 // Get the new insert position for the node we care about.
1289 MemberPointerType *NewIP =
1290 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1291 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1292 }
John McCall90d1c2d2009-09-24 23:30:46 +00001293 MemberPointerType *New
1294 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001295 Types.push_back(New);
1296 MemberPointerTypes.InsertNode(New, InsertPos);
1297 return QualType(New, 0);
1298}
1299
Mike Stump11289f42009-09-09 15:08:12 +00001300/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001301/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001302QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001303 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001304 ArrayType::ArraySizeModifier ASM,
1305 unsigned EltTypeQuals) {
Sebastian Redl2dfdb822009-11-05 15:52:31 +00001306 assert((EltTy->isDependentType() ||
1307 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001308 "Constant array of VLAs is illegal!");
1309
Chris Lattnere2df3f92009-05-13 04:12:56 +00001310 // Convert the array size into a canonical width matching the pointer size for
1311 // the target.
1312 llvm::APInt ArySize(ArySizeIn);
1313 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001314
Chris Lattner23b7eb62007-06-15 23:05:46 +00001315 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001316 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001317
Chris Lattner36f8e652007-01-27 08:31:04 +00001318 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001319 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001320 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001321 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001322
Chris Lattner7ccecb92006-11-12 08:50:50 +00001323 // If the element type isn't canonical, this won't be a canonical type either,
1324 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001325 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001326 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001327 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001328 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001329 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001330 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001331 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001332 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001333 }
Mike Stump11289f42009-09-09 15:08:12 +00001334
John McCall90d1c2d2009-09-24 23:30:46 +00001335 ConstantArrayType *New = new(*this,TypeAlignment)
1336 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001337 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001338 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001339 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001340}
1341
Steve Naroffcadebd02007-08-30 18:14:25 +00001342/// getVariableArrayType - Returns a non-unique reference to the type for a
1343/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001344QualType ASTContext::getVariableArrayType(QualType EltTy,
1345 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001346 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001347 unsigned EltTypeQuals,
1348 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001349 // Since we don't unique expressions, it isn't possible to unique VLA's
1350 // that have an expression provided for their size.
1351
John McCall90d1c2d2009-09-24 23:30:46 +00001352 VariableArrayType *New = new(*this, TypeAlignment)
1353 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001354
1355 VariableArrayTypes.push_back(New);
1356 Types.push_back(New);
1357 return QualType(New, 0);
1358}
1359
Douglas Gregor4619e432008-12-05 23:32:09 +00001360/// getDependentSizedArrayType - Returns a non-unique reference to
1361/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001362/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001363QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1364 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001365 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001366 unsigned EltTypeQuals,
1367 SourceRange Brackets) {
Douglas Gregorad2956c2009-11-19 18:03:26 +00001368 assert((!NumElts || NumElts->isTypeDependent() ||
1369 NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001370 "Size must be type- or value-dependent!");
1371
Douglas Gregorf3f95522009-07-31 00:23:35 +00001372 void *InsertPos = 0;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001373 DependentSizedArrayType *Canon = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00001374 llvm::FoldingSetNodeID ID;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001375
1376 if (NumElts) {
1377 // Dependently-sized array types that do not have a specified
1378 // number of elements will have their sizes deduced from an
1379 // initializer.
Douglas Gregorad2956c2009-11-19 18:03:26 +00001380 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1381 EltTypeQuals, NumElts);
1382
1383 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1384 }
1385
Douglas Gregorf3f95522009-07-31 00:23:35 +00001386 DependentSizedArrayType *New;
1387 if (Canon) {
1388 // We already have a canonical version of this array type; use it as
1389 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001390 New = new (*this, TypeAlignment)
1391 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1392 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001393 } else {
1394 QualType CanonEltTy = getCanonicalType(EltTy);
1395 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001396 New = new (*this, TypeAlignment)
1397 DependentSizedArrayType(*this, EltTy, QualType(),
1398 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorad2956c2009-11-19 18:03:26 +00001399
Douglas Gregorc42075a2010-02-04 18:10:26 +00001400 if (NumElts) {
1401 DependentSizedArrayType *CanonCheck
1402 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1403 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1404 (void)CanonCheck;
Douglas Gregorad2956c2009-11-19 18:03:26 +00001405 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001406 }
Douglas Gregorf3f95522009-07-31 00:23:35 +00001407 } else {
1408 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1409 ASM, EltTypeQuals,
1410 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001411 New = new (*this, TypeAlignment)
1412 DependentSizedArrayType(*this, EltTy, Canon,
1413 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001414 }
1415 }
Mike Stump11289f42009-09-09 15:08:12 +00001416
Douglas Gregor4619e432008-12-05 23:32:09 +00001417 Types.push_back(New);
1418 return QualType(New, 0);
1419}
1420
Eli Friedmanbd258282008-02-15 18:16:39 +00001421QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1422 ArrayType::ArraySizeModifier ASM,
1423 unsigned EltTypeQuals) {
1424 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001425 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001426
1427 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001428 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001429 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1430 return QualType(ATP, 0);
1431
1432 // If the element type isn't canonical, this won't be a canonical type
1433 // either, so fill in the canonical type field.
1434 QualType Canonical;
1435
John McCallb692a092009-10-22 20:10:53 +00001436 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001437 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001438 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001439
1440 // Get the new insert position for the node we care about.
1441 IncompleteArrayType *NewIP =
1442 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001443 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001444 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001445
John McCall90d1c2d2009-09-24 23:30:46 +00001446 IncompleteArrayType *New = new (*this, TypeAlignment)
1447 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001448
1449 IncompleteArrayTypes.InsertNode(New, InsertPos);
1450 Types.push_back(New);
1451 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001452}
1453
Steve Naroff91fcddb2007-07-18 18:00:27 +00001454/// getVectorType - Return the unique reference to a vector type of
1455/// the specified element type and size. VectorType must be a built-in type.
John Thompson22334602010-02-05 00:12:22 +00001456QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1457 bool IsAltiVec, bool IsPixel) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001458 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001459
Chris Lattner76a00cf2008-04-06 22:59:24 +00001460 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001461 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001462
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001463 // Check if we've already instantiated a vector of this type.
1464 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001465 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1466 IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001467 void *InsertPos = 0;
1468 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1469 return QualType(VTP, 0);
1470
1471 // If the element type isn't canonical, this won't be a canonical type either,
1472 // so fill in the canonical type field.
1473 QualType Canonical;
John Thompson22334602010-02-05 00:12:22 +00001474 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1475 Canonical = getVectorType(getCanonicalType(vecType),
1476 NumElts, false, false);
Mike Stump11289f42009-09-09 15:08:12 +00001477
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001478 // Get the new insert position for the node we care about.
1479 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001480 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001481 }
John McCall90d1c2d2009-09-24 23:30:46 +00001482 VectorType *New = new (*this, TypeAlignment)
John Thompson22334602010-02-05 00:12:22 +00001483 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001484 VectorTypes.InsertNode(New, InsertPos);
1485 Types.push_back(New);
1486 return QualType(New, 0);
1487}
1488
Nate Begemance4d7fc2008-04-18 23:10:10 +00001489/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001490/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001491QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001492 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001493
Chris Lattner76a00cf2008-04-06 22:59:24 +00001494 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001495 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001496
Steve Naroff91fcddb2007-07-18 18:00:27 +00001497 // Check if we've already instantiated a vector of this type.
1498 llvm::FoldingSetNodeID ID;
John Thompson22334602010-02-05 00:12:22 +00001499 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001500 void *InsertPos = 0;
1501 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1502 return QualType(VTP, 0);
1503
1504 // If the element type isn't canonical, this won't be a canonical type either,
1505 // so fill in the canonical type field.
1506 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001507 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001508 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001509
Steve Naroff91fcddb2007-07-18 18:00:27 +00001510 // Get the new insert position for the node we care about.
1511 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001512 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001513 }
John McCall90d1c2d2009-09-24 23:30:46 +00001514 ExtVectorType *New = new (*this, TypeAlignment)
1515 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001516 VectorTypes.InsertNode(New, InsertPos);
1517 Types.push_back(New);
1518 return QualType(New, 0);
1519}
1520
Mike Stump11289f42009-09-09 15:08:12 +00001521QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001522 Expr *SizeExpr,
1523 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001524 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001525 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001526 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001527
Douglas Gregor352169a2009-07-31 03:54:25 +00001528 void *InsertPos = 0;
1529 DependentSizedExtVectorType *Canon
1530 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1531 DependentSizedExtVectorType *New;
1532 if (Canon) {
1533 // We already have a canonical version of this array type; use it as
1534 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001535 New = new (*this, TypeAlignment)
1536 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1537 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001538 } else {
1539 QualType CanonVecTy = getCanonicalType(vecType);
1540 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001541 New = new (*this, TypeAlignment)
1542 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1543 AttrLoc);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001544
1545 DependentSizedExtVectorType *CanonCheck
1546 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1547 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1548 (void)CanonCheck;
Douglas Gregor352169a2009-07-31 03:54:25 +00001549 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1550 } else {
1551 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1552 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001553 New = new (*this, TypeAlignment)
1554 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001555 }
1556 }
Mike Stump11289f42009-09-09 15:08:12 +00001557
Douglas Gregor758a8692009-06-17 21:51:59 +00001558 Types.push_back(New);
1559 return QualType(New, 0);
1560}
1561
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001562/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001563///
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001564QualType ASTContext::getFunctionNoProtoType(QualType ResultTy,
1565 const FunctionType::ExtInfo &Info) {
1566 const CallingConv CallConv = Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001567 // Unique functions, to guarantee there is only one function of a particular
1568 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001569 llvm::FoldingSetNodeID ID;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001570 FunctionNoProtoType::Profile(ID, ResultTy, Info);
Mike Stump11289f42009-09-09 15:08:12 +00001571
Chris Lattner47955de2007-01-27 08:37:20 +00001572 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001573 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001574 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001575 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001576
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001577 QualType Canonical;
Douglas Gregor8c940862010-01-18 17:14:39 +00001578 if (!ResultTy.isCanonical() ||
John McCallab26cfa2010-02-05 21:31:56 +00001579 getCanonicalCallConv(CallConv) != CallConv) {
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001580 Canonical =
1581 getFunctionNoProtoType(getCanonicalType(ResultTy),
1582 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Mike Stump11289f42009-09-09 15:08:12 +00001583
Chris Lattner47955de2007-01-27 08:37:20 +00001584 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001585 FunctionNoProtoType *NewIP =
1586 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001587 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001588 }
Mike Stump11289f42009-09-09 15:08:12 +00001589
John McCall90d1c2d2009-09-24 23:30:46 +00001590 FunctionNoProtoType *New = new (*this, TypeAlignment)
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001591 FunctionNoProtoType(ResultTy, Canonical, Info);
Chris Lattner47955de2007-01-27 08:37:20 +00001592 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001593 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001594 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001595}
1596
1597/// getFunctionType - Return a normal function type with a typed argument
1598/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001599QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001600 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001601 unsigned TypeQuals, bool hasExceptionSpec,
1602 bool hasAnyExceptionSpec, unsigned NumExs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001603 const QualType *ExArray,
1604 const FunctionType::ExtInfo &Info) {
1605 const CallingConv CallConv= Info.getCC();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001606 // Unique functions, to guarantee there is only one function of a particular
1607 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001608 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001609 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001610 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001611 NumExs, ExArray, Info);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001612
1613 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001614 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001615 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001616 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001617
1618 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001619 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001620 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001621 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001622 isCanonical = false;
1623
1624 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001625 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001626 QualType Canonical;
John McCallab26cfa2010-02-05 21:31:56 +00001627 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001628 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001629 CanonicalArgs.reserve(NumArgs);
1630 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001631 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001632
Chris Lattner76a00cf2008-04-06 22:59:24 +00001633 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001634 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001635 isVariadic, TypeQuals, false,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001636 false, 0, 0,
1637 Info.withCallingConv(getCanonicalCallConv(CallConv)));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001638
Chris Lattnerfd4de792007-01-27 01:15:32 +00001639 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001640 FunctionProtoType *NewIP =
1641 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001642 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001643 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001644
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001645 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001646 // for two variable size arrays (for parameter and exception types) at the
1647 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001648 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001649 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1650 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001651 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001652 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001653 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001654 ExArray, NumExs, Canonical, Info);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001655 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001656 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001657 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001658}
Chris Lattneref51c202006-11-10 07:17:23 +00001659
John McCalle78aac42010-03-10 03:28:59 +00001660#ifndef NDEBUG
1661static bool NeedsInjectedClassNameType(const RecordDecl *D) {
1662 if (!isa<CXXRecordDecl>(D)) return false;
1663 const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1664 if (isa<ClassTemplatePartialSpecializationDecl>(RD))
1665 return true;
1666 if (RD->getDescribedClassTemplate() &&
1667 !isa<ClassTemplateSpecializationDecl>(RD))
1668 return true;
1669 return false;
1670}
1671#endif
1672
1673/// getInjectedClassNameType - Return the unique reference to the
1674/// injected class name type for the specified templated declaration.
1675QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
1676 QualType TST) {
1677 assert(NeedsInjectedClassNameType(Decl));
1678 if (Decl->TypeForDecl) {
1679 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1680 } else if (CXXRecordDecl *PrevDecl
1681 = cast_or_null<CXXRecordDecl>(Decl->getPreviousDeclaration())) {
1682 assert(PrevDecl->TypeForDecl && "previous declaration has no type");
1683 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1684 assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
1685 } else {
John McCall2408e322010-04-27 00:57:59 +00001686 Decl->TypeForDecl =
1687 new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
John McCalle78aac42010-03-10 03:28:59 +00001688 Types.push_back(Decl->TypeForDecl);
1689 }
1690 return QualType(Decl->TypeForDecl, 0);
1691}
1692
Douglas Gregor83a586e2008-04-13 21:07:44 +00001693/// getTypeDeclType - Return the unique reference to the type for the
1694/// specified type declaration.
John McCall96f0b5f2010-03-10 06:48:02 +00001695QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001696 assert(Decl && "Passed null for Decl param");
John McCall96f0b5f2010-03-10 06:48:02 +00001697 assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
Mike Stump11289f42009-09-09 15:08:12 +00001698
John McCall81e38502010-02-16 03:57:14 +00001699 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001700 return getTypedefType(Typedef);
John McCall96f0b5f2010-03-10 06:48:02 +00001701
1702 if (const ObjCInterfaceDecl *ObjCInterface
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001703 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001704 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001705
John McCall96f0b5f2010-03-10 06:48:02 +00001706 assert(!isa<TemplateTypeParmDecl>(Decl) &&
1707 "Template type parameter types are always available.");
1708
John McCall81e38502010-02-16 03:57:14 +00001709 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001710 assert(!Record->getPreviousDeclaration() &&
1711 "struct/union has previous declaration");
1712 assert(!NeedsInjectedClassNameType(Record));
1713 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall81e38502010-02-16 03:57:14 +00001714 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
John McCall96f0b5f2010-03-10 06:48:02 +00001715 assert(!Enum->getPreviousDeclaration() &&
1716 "enum has previous declaration");
1717 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall81e38502010-02-16 03:57:14 +00001718 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCallb96ec562009-12-04 22:46:56 +00001719 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1720 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001721 } else
John McCall96f0b5f2010-03-10 06:48:02 +00001722 llvm_unreachable("TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001723
John McCall96f0b5f2010-03-10 06:48:02 +00001724 Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001725 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001726}
1727
Chris Lattner32d920b2007-01-26 02:01:53 +00001728/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001729/// specified typename decl.
John McCall81e38502010-02-16 03:57:14 +00001730QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001731 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001732
Chris Lattner76a00cf2008-04-06 22:59:24 +00001733 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001734 Decl->TypeForDecl = new(*this, TypeAlignment)
1735 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001736 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001737 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001738}
1739
John McCallcebee162009-10-18 09:09:24 +00001740/// \brief Retrieve a substitution-result type.
1741QualType
1742ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1743 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001744 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001745 && "replacement types must always be canonical");
1746
1747 llvm::FoldingSetNodeID ID;
1748 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1749 void *InsertPos = 0;
1750 SubstTemplateTypeParmType *SubstParm
1751 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1752
1753 if (!SubstParm) {
1754 SubstParm = new (*this, TypeAlignment)
1755 SubstTemplateTypeParmType(Parm, Replacement);
1756 Types.push_back(SubstParm);
1757 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1758 }
1759
1760 return QualType(SubstParm, 0);
1761}
1762
Douglas Gregoreff93e02009-02-05 23:33:38 +00001763/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001764/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001765/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001766QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001767 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001768 IdentifierInfo *Name) {
1769 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001770 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001771 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001772 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001773 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1774
1775 if (TypeParm)
1776 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001777
Anders Carlsson90036dc2009-06-16 00:30:48 +00001778 if (Name) {
1779 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001780 TypeParm = new (*this, TypeAlignment)
1781 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001782
1783 TemplateTypeParmType *TypeCheck
1784 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1785 assert(!TypeCheck && "Template type parameter canonical type broken");
1786 (void)TypeCheck;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001787 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001788 TypeParm = new (*this, TypeAlignment)
1789 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001790
1791 Types.push_back(TypeParm);
1792 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1793
1794 return QualType(TypeParm, 0);
1795}
1796
John McCalle78aac42010-03-10 03:28:59 +00001797TypeSourceInfo *
1798ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
1799 SourceLocation NameLoc,
1800 const TemplateArgumentListInfo &Args,
1801 QualType CanonType) {
1802 QualType TST = getTemplateSpecializationType(Name, Args, CanonType);
1803
1804 TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
1805 TemplateSpecializationTypeLoc TL
1806 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1807 TL.setTemplateNameLoc(NameLoc);
1808 TL.setLAngleLoc(Args.getLAngleLoc());
1809 TL.setRAngleLoc(Args.getRAngleLoc());
1810 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1811 TL.setArgLocInfo(i, Args[i].getLocInfo());
1812 return DI;
1813}
1814
Mike Stump11289f42009-09-09 15:08:12 +00001815QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001816ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall6b51f282009-11-23 01:53:49 +00001817 const TemplateArgumentListInfo &Args,
John McCall2408e322010-04-27 00:57:59 +00001818 QualType Canon,
1819 bool IsCurrentInstantiation) {
John McCall6b51f282009-11-23 01:53:49 +00001820 unsigned NumArgs = Args.size();
1821
John McCall0ad16662009-10-29 08:12:44 +00001822 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1823 ArgVec.reserve(NumArgs);
1824 for (unsigned i = 0; i != NumArgs; ++i)
1825 ArgVec.push_back(Args[i].getArgument());
1826
John McCall2408e322010-04-27 00:57:59 +00001827 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
1828 Canon, IsCurrentInstantiation);
John McCall0ad16662009-10-29 08:12:44 +00001829}
1830
1831QualType
1832ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001833 const TemplateArgument *Args,
1834 unsigned NumArgs,
John McCall2408e322010-04-27 00:57:59 +00001835 QualType Canon,
1836 bool IsCurrentInstantiation) {
Douglas Gregor15301382009-07-30 17:40:51 +00001837 if (!Canon.isNull())
1838 Canon = getCanonicalType(Canon);
1839 else {
John McCall2408e322010-04-27 00:57:59 +00001840 assert(!IsCurrentInstantiation &&
1841 "current-instantiation specializations should always "
1842 "have a canonical type");
1843
Douglas Gregor15301382009-07-30 17:40:51 +00001844 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001845 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1846 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1847 CanonArgs.reserve(NumArgs);
1848 for (unsigned I = 0; I != NumArgs; ++I)
1849 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1850
1851 // Determine whether this canonical template specialization type already
1852 // exists.
1853 llvm::FoldingSetNodeID ID;
John McCall2408e322010-04-27 00:57:59 +00001854 TemplateSpecializationType::Profile(ID, CanonTemplate, false,
Douglas Gregor00044172009-07-29 16:09:57 +00001855 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001856
1857 void *InsertPos = 0;
1858 TemplateSpecializationType *Spec
1859 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001860
Douglas Gregora8e02e72009-07-28 23:00:59 +00001861 if (!Spec) {
1862 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001863 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001864 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001865 TypeAlignment);
John McCall2408e322010-04-27 00:57:59 +00001866 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, false,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001867 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001868 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001869 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001870 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001871 }
Mike Stump11289f42009-09-09 15:08:12 +00001872
Douglas Gregor15301382009-07-30 17:40:51 +00001873 if (Canon.isNull())
1874 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001875 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001876 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001877 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001878
Douglas Gregora8e02e72009-07-28 23:00:59 +00001879 // Allocate the (non-canonical) template specialization type, but don't
1880 // try to unique it: these types typically have location information that
1881 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001882 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001883 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001884 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001885 TemplateSpecializationType *Spec
John McCall2408e322010-04-27 00:57:59 +00001886 = new (Mem) TemplateSpecializationType(*this, Template,
1887 IsCurrentInstantiation,
1888 Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001889 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001890
Douglas Gregor8bf42052009-02-09 18:46:07 +00001891 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001892 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001893}
1894
Mike Stump11289f42009-09-09 15:08:12 +00001895QualType
Douglas Gregorf21eb492009-03-26 23:50:42 +00001896ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor52537682009-03-19 00:18:19 +00001897 QualType NamedType) {
1898 llvm::FoldingSetNodeID ID;
Douglas Gregorf21eb492009-03-26 23:50:42 +00001899 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001900
1901 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001902 QualifiedNameType *T
Douglas Gregor52537682009-03-19 00:18:19 +00001903 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1904 if (T)
1905 return QualType(T, 0);
1906
Douglas Gregorc42075a2010-02-04 18:10:26 +00001907 QualType Canon = NamedType;
1908 if (!Canon.isCanonical()) {
1909 Canon = getCanonicalType(NamedType);
1910 QualifiedNameType *CheckT
1911 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1912 assert(!CheckT && "Qualified name canonical type broken");
1913 (void)CheckT;
1914 }
1915
1916 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregor52537682009-03-19 00:18:19 +00001917 Types.push_back(T);
1918 QualifiedNameTypes.InsertNode(T, InsertPos);
1919 return QualType(T, 0);
1920}
1921
Douglas Gregor02085352010-03-31 20:19:30 +00001922QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1923 NestedNameSpecifier *NNS,
1924 const IdentifierInfo *Name,
1925 QualType Canon) {
Douglas Gregor333489b2009-03-27 23:10:48 +00001926 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1927
1928 if (Canon.isNull()) {
1929 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
Douglas Gregor02085352010-03-31 20:19:30 +00001930 ElaboratedTypeKeyword CanonKeyword = Keyword;
1931 if (Keyword == ETK_None)
1932 CanonKeyword = ETK_Typename;
1933
1934 if (CanonNNS != NNS || CanonKeyword != Keyword)
1935 Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001936 }
1937
1938 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001939 DependentNameType::Profile(ID, Keyword, NNS, Name);
Douglas Gregor333489b2009-03-27 23:10:48 +00001940
1941 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001942 DependentNameType *T
1943 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor333489b2009-03-27 23:10:48 +00001944 if (T)
1945 return QualType(T, 0);
1946
Douglas Gregor02085352010-03-31 20:19:30 +00001947 T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
Douglas Gregor333489b2009-03-27 23:10:48 +00001948 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001949 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001950 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001951}
1952
Mike Stump11289f42009-09-09 15:08:12 +00001953QualType
Douglas Gregor02085352010-03-31 20:19:30 +00001954ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
1955 NestedNameSpecifier *NNS,
1956 const TemplateSpecializationType *TemplateId,
1957 QualType Canon) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001958 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1959
Douglas Gregorc42075a2010-02-04 18:10:26 +00001960 llvm::FoldingSetNodeID ID;
Douglas Gregor02085352010-03-31 20:19:30 +00001961 DependentNameType::Profile(ID, Keyword, NNS, TemplateId);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001962
1963 void *InsertPos = 0;
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001964 DependentNameType *T
1965 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001966 if (T)
1967 return QualType(T, 0);
1968
Douglas Gregordce2b622009-04-01 00:28:59 +00001969 if (Canon.isNull()) {
1970 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1971 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
Douglas Gregor02085352010-03-31 20:19:30 +00001972 ElaboratedTypeKeyword CanonKeyword = Keyword;
1973 if (Keyword == ETK_None)
1974 CanonKeyword = ETK_Typename;
1975 if (CanonNNS != NNS || CanonKeyword != Keyword ||
1976 CanonType != QualType(TemplateId, 0)) {
Douglas Gregordce2b622009-04-01 00:28:59 +00001977 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00001978 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00001979 assert(CanonTemplateId &&
1980 "Canonical type must also be a template specialization type");
Douglas Gregor02085352010-03-31 20:19:30 +00001981 Canon = getDependentNameType(CanonKeyword, CanonNNS, CanonTemplateId);
Douglas Gregordce2b622009-04-01 00:28:59 +00001982 }
Douglas Gregorc42075a2010-02-04 18:10:26 +00001983
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001984 DependentNameType *CheckT
1985 = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorc42075a2010-02-04 18:10:26 +00001986 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregordce2b622009-04-01 00:28:59 +00001987 }
1988
Douglas Gregor02085352010-03-31 20:19:30 +00001989 T = new (*this) DependentNameType(Keyword, NNS, TemplateId, Canon);
Douglas Gregordce2b622009-04-01 00:28:59 +00001990 Types.push_back(T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001991 DependentNameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001992 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00001993}
1994
John McCallfcc33b02009-09-05 00:15:47 +00001995QualType
1996ASTContext::getElaboratedType(QualType UnderlyingType,
1997 ElaboratedType::TagKind Tag) {
1998 llvm::FoldingSetNodeID ID;
1999 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump11289f42009-09-09 15:08:12 +00002000
John McCallfcc33b02009-09-05 00:15:47 +00002001 void *InsertPos = 0;
2002 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2003 if (T)
2004 return QualType(T, 0);
2005
Douglas Gregorc42075a2010-02-04 18:10:26 +00002006 QualType Canon = UnderlyingType;
2007 if (!Canon.isCanonical()) {
2008 Canon = getCanonicalType(Canon);
2009 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2010 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2011 }
John McCallfcc33b02009-09-05 00:15:47 +00002012
2013 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2014 Types.push_back(T);
2015 ElaboratedTypes.InsertNode(T, InsertPos);
2016 return QualType(T, 0);
2017}
2018
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002019/// CmpProtocolNames - Comparison predicate for sorting protocols
2020/// alphabetically.
2021static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2022 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00002023 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002024}
2025
John McCallfc93cf92009-10-22 22:37:11 +00002026static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2027 unsigned NumProtocols) {
2028 if (NumProtocols == 0) return true;
2029
2030 for (unsigned i = 1; i != NumProtocols; ++i)
2031 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2032 return false;
2033 return true;
2034}
2035
2036static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002037 unsigned &NumProtocols) {
2038 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00002039
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002040 // Sort protocols, keyed by name.
2041 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2042
2043 // Remove duplicates.
2044 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2045 NumProtocols = ProtocolsEnd-Protocols;
2046}
2047
Steve Narofffb4330f2009-06-17 22:40:22 +00002048/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2049/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002050QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00002051 ObjCProtocolDecl **Protocols,
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002052 unsigned NumProtocols,
2053 unsigned Quals) {
Steve Narofffb4330f2009-06-17 22:40:22 +00002054 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002055 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002056 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
Steve Narofffb4330f2009-06-17 22:40:22 +00002057
2058 void *InsertPos = 0;
2059 if (ObjCObjectPointerType *QT =
2060 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002061 return getQualifiedType(QualType(QT, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002062
John McCallfc93cf92009-10-22 22:37:11 +00002063 // Sort the protocol list alphabetically to canonicalize it.
2064 QualType Canonical;
2065 if (!InterfaceT.isCanonical() ||
2066 !areSortedAndUniqued(Protocols, NumProtocols)) {
2067 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002068 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2069 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002070 unsigned UniqueCount = NumProtocols;
2071
John McCallfc93cf92009-10-22 22:37:11 +00002072 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2073
2074 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2075 &Sorted[0], UniqueCount);
2076 } else {
2077 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2078 Protocols, NumProtocols);
2079 }
2080
2081 // Regenerate InsertPos.
2082 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2083 }
2084
Douglas Gregorf85bee62010-02-08 22:59:26 +00002085 // No match.
2086 unsigned Size = sizeof(ObjCObjectPointerType)
2087 + NumProtocols * sizeof(ObjCProtocolDecl *);
2088 void *Mem = Allocate(Size, TypeAlignment);
2089 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2090 InterfaceT,
2091 Protocols,
2092 NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002093
Steve Narofffb4330f2009-06-17 22:40:22 +00002094 Types.push_back(QType);
2095 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniand2bccaf2010-03-05 22:42:55 +00002096 return getQualifiedType(QualType(QType, 0), Qs);
Steve Narofffb4330f2009-06-17 22:40:22 +00002097}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002098
Steve Naroffc277ad12009-07-18 15:33:26 +00002099/// getObjCInterfaceType - Return the unique reference to the type for the
2100/// specified ObjC interface decl. The list of protocols is optional.
2101QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002102 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002103 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00002104 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002105
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002106 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00002107 if (ObjCInterfaceType *QT =
2108 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002109 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002110
John McCallfc93cf92009-10-22 22:37:11 +00002111 // Sort the protocol list alphabetically to canonicalize it.
2112 QualType Canonical;
2113 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002114 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
2115 Protocols + NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002116
2117 unsigned UniqueCount = NumProtocols;
2118 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2119
2120 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2121
2122 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2123 }
2124
Douglas Gregorf85bee62010-02-08 22:59:26 +00002125 unsigned Size = sizeof(ObjCInterfaceType)
2126 + NumProtocols * sizeof(ObjCProtocolDecl *);
2127 void *Mem = Allocate(Size, TypeAlignment);
2128 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2129 const_cast<ObjCInterfaceDecl*>(Decl),
2130 Protocols,
2131 NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002132
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002133 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00002134 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002135 return QualType(QType, 0);
2136}
2137
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002138/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2139/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002140/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002141/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002142/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002143QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002144 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002145 if (tofExpr->isTypeDependent()) {
2146 llvm::FoldingSetNodeID ID;
2147 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002148
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002149 void *InsertPos = 0;
2150 DependentTypeOfExprType *Canon
2151 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2152 if (Canon) {
2153 // We already have a "canonical" version of an identical, dependent
2154 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002155 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002156 QualType((TypeOfExprType*)Canon, 0));
2157 }
2158 else {
2159 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002160 Canon
2161 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002162 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2163 toe = Canon;
2164 }
2165 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002166 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002167 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002168 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002169 Types.push_back(toe);
2170 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002171}
2172
Steve Naroffa773cd52007-08-01 18:02:17 +00002173/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2174/// TypeOfType AST's. The only motivation to unique these nodes would be
2175/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002176/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002177/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002178QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002179 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002180 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002181 Types.push_back(tot);
2182 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002183}
2184
Anders Carlssonad6bd352009-06-24 21:24:56 +00002185/// getDecltypeForExpr - Given an expr, will return the decltype for that
2186/// expression, according to the rules in C++0x [dcl.type.simple]p4
2187static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002188 if (e->isTypeDependent())
2189 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002190
Anders Carlssonad6bd352009-06-24 21:24:56 +00002191 // If e is an id expression or a class member access, decltype(e) is defined
2192 // as the type of the entity named by e.
2193 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2194 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2195 return VD->getType();
2196 }
2197 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2198 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2199 return FD->getType();
2200 }
2201 // If e is a function call or an invocation of an overloaded operator,
2202 // (parentheses around e are ignored), decltype(e) is defined as the
2203 // return type of that function.
2204 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2205 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002206
Anders Carlssonad6bd352009-06-24 21:24:56 +00002207 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002208
2209 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002210 // defined as T&, otherwise decltype(e) is defined as T.
2211 if (e->isLvalue(Context) == Expr::LV_Valid)
2212 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002213
Anders Carlssonad6bd352009-06-24 21:24:56 +00002214 return T;
2215}
2216
Anders Carlsson81df7b82009-06-24 19:06:50 +00002217/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2218/// DecltypeType AST's. The only motivation to unique these nodes would be
2219/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002220/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002221/// on canonical type's (which are always unique).
2222QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002223 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002224 if (e->isTypeDependent()) {
2225 llvm::FoldingSetNodeID ID;
2226 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002227
Douglas Gregora21f6c32009-07-30 23:36:40 +00002228 void *InsertPos = 0;
2229 DependentDecltypeType *Canon
2230 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2231 if (Canon) {
2232 // We already have a "canonical" version of an equivalent, dependent
2233 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002234 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002235 QualType((DecltypeType*)Canon, 0));
2236 }
2237 else {
2238 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002239 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002240 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2241 dt = Canon;
2242 }
2243 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002244 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002245 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002246 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002247 Types.push_back(dt);
2248 return QualType(dt, 0);
2249}
2250
Chris Lattnerfb072462007-01-23 05:45:31 +00002251/// getTagDeclType - Return the unique reference to the type for the
2252/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002253QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002254 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002255 // FIXME: What is the design on getTagDeclType when it requires casting
2256 // away const? mutable?
2257 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002258}
2259
Mike Stump11289f42009-09-09 15:08:12 +00002260/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2261/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2262/// needs to agree with the definition in <stddef.h>.
Anders Carlsson22f443f2009-12-12 00:26:23 +00002263CanQualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002264 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002265}
Chris Lattnerfb072462007-01-23 05:45:31 +00002266
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002267/// getSignedWCharType - Return the type of "signed wchar_t".
2268/// Used when in C++, as a GCC extension.
2269QualType ASTContext::getSignedWCharType() const {
2270 // FIXME: derive from "Target" ?
2271 return WCharTy;
2272}
2273
2274/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2275/// Used when in C++, as a GCC extension.
2276QualType ASTContext::getUnsignedWCharType() const {
2277 // FIXME: derive from "Target" ?
2278 return UnsignedIntTy;
2279}
2280
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002281/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2282/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2283QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002284 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002285}
2286
Chris Lattnera21ad802008-04-02 05:18:44 +00002287//===----------------------------------------------------------------------===//
2288// Type Operators
2289//===----------------------------------------------------------------------===//
2290
John McCallfc93cf92009-10-22 22:37:11 +00002291CanQualType ASTContext::getCanonicalParamType(QualType T) {
2292 // Push qualifiers into arrays, and then discard any remaining
2293 // qualifiers.
2294 T = getCanonicalType(T);
2295 const Type *Ty = T.getTypePtr();
2296
2297 QualType Result;
2298 if (isa<ArrayType>(Ty)) {
2299 Result = getArrayDecayedType(QualType(Ty,0));
2300 } else if (isa<FunctionType>(Ty)) {
2301 Result = getPointerType(QualType(Ty, 0));
2302 } else {
2303 Result = QualType(Ty, 0);
2304 }
2305
2306 return CanQualType::CreateUnsafe(Result);
2307}
2308
Chris Lattnered0d0792008-04-06 22:41:35 +00002309/// getCanonicalType - Return the canonical (structural) type corresponding to
2310/// the specified potentially non-canonical type. The non-canonical version
2311/// of a type may have many "decorated" versions of types. Decorators can
2312/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2313/// to be free of any of these, allowing two canonical types to be compared
2314/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002315CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002316 QualifierCollector Quals;
2317 const Type *Ptr = Quals.strip(T);
2318 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002319
John McCall8ccfcb52009-09-24 19:53:00 +00002320 // The canonical internal type will be the canonical type *except*
2321 // that we push type qualifiers down through array types.
2322
2323 // If there are no new qualifiers to push down, stop here.
2324 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002325 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002326
John McCall8ccfcb52009-09-24 19:53:00 +00002327 // If the type qualifiers are on an array type, get the canonical
2328 // type of the array with the qualifiers applied to the element
2329 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002330 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2331 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002332 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002333
Chris Lattner7adf0762008-08-04 07:31:14 +00002334 // Get the canonical version of the element with the extra qualifiers on it.
2335 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002336 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002337 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002338
Chris Lattner7adf0762008-08-04 07:31:14 +00002339 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002340 return CanQualType::CreateUnsafe(
2341 getConstantArrayType(NewEltTy, CAT->getSize(),
2342 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002343 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002344 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002345 return CanQualType::CreateUnsafe(
2346 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002347 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002348
Douglas Gregor4619e432008-12-05 23:32:09 +00002349 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002350 return CanQualType::CreateUnsafe(
2351 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002352 DSAT->getSizeExpr() ?
2353 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002354 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002355 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor326b2fa2009-10-30 22:56:57 +00002356 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor4619e432008-12-05 23:32:09 +00002357
Chris Lattner7adf0762008-08-04 07:31:14 +00002358 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002359 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002360 VAT->getSizeExpr() ?
2361 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002362 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002363 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002364 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002365}
2366
Chandler Carruth607f38e2009-12-29 07:16:59 +00002367QualType ASTContext::getUnqualifiedArrayType(QualType T,
2368 Qualifiers &Quals) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002369 Quals = T.getQualifiers();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002370 if (!isa<ArrayType>(T)) {
Chandler Carruth04bdce62010-01-12 20:32:25 +00002371 return T.getUnqualifiedType();
Chandler Carruth607f38e2009-12-29 07:16:59 +00002372 }
2373
Chandler Carruth607f38e2009-12-29 07:16:59 +00002374 const ArrayType *AT = cast<ArrayType>(T);
2375 QualType Elt = AT->getElementType();
Zhongxing Xucd321a32010-01-05 08:15:06 +00002376 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth607f38e2009-12-29 07:16:59 +00002377 if (Elt == UnqualElt)
2378 return T;
2379
2380 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2381 return getConstantArrayType(UnqualElt, CAT->getSize(),
2382 CAT->getSizeModifier(), 0);
2383 }
2384
2385 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2386 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2387 }
2388
2389 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2390 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2391 DSAT->getSizeModifier(), 0,
2392 SourceRange());
2393}
2394
John McCall847e2a12009-11-24 18:42:40 +00002395DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2396 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2397 return TD->getDeclName();
2398
2399 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2400 if (DTN->isIdentifier()) {
2401 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2402 } else {
2403 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2404 }
2405 }
2406
John McCalld28ae272009-12-02 08:04:21 +00002407 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2408 assert(Storage);
2409 return (*Storage->begin())->getDeclName();
John McCall847e2a12009-11-24 18:42:40 +00002410}
2411
Douglas Gregor6bc50582009-05-07 06:41:52 +00002412TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2413 // If this template name refers to a template, the canonical
2414 // template name merely stores the template itself.
2415 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002416 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002417
John McCalld28ae272009-12-02 08:04:21 +00002418 assert(!Name.getAsOverloadedTemplate());
Mike Stump11289f42009-09-09 15:08:12 +00002419
Douglas Gregor6bc50582009-05-07 06:41:52 +00002420 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2421 assert(DTN && "Non-dependent template names must refer to template decls.");
2422 return DTN->CanonicalTemplateName;
2423}
2424
Douglas Gregoradee3e32009-11-11 23:06:43 +00002425bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2426 X = getCanonicalTemplateName(X);
2427 Y = getCanonicalTemplateName(Y);
2428 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2429}
2430
Mike Stump11289f42009-09-09 15:08:12 +00002431TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002432ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2433 switch (Arg.getKind()) {
2434 case TemplateArgument::Null:
2435 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002436
Douglas Gregora8e02e72009-07-28 23:00:59 +00002437 case TemplateArgument::Expression:
Douglas Gregora8e02e72009-07-28 23:00:59 +00002438 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002439
Douglas Gregora8e02e72009-07-28 23:00:59 +00002440 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002441 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002442
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002443 case TemplateArgument::Template:
2444 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2445
Douglas Gregora8e02e72009-07-28 23:00:59 +00002446 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002447 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002448 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002449
Douglas Gregora8e02e72009-07-28 23:00:59 +00002450 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002451 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002452
Douglas Gregora8e02e72009-07-28 23:00:59 +00002453 case TemplateArgument::Pack: {
2454 // FIXME: Allocate in ASTContext
2455 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2456 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002457 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002458 AEnd = Arg.pack_end();
2459 A != AEnd; (void)++A, ++Idx)
2460 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002461
Douglas Gregora8e02e72009-07-28 23:00:59 +00002462 TemplateArgument Result;
2463 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2464 return Result;
2465 }
2466 }
2467
2468 // Silence GCC warning
2469 assert(false && "Unhandled template argument kind");
2470 return TemplateArgument();
2471}
2472
Douglas Gregor333489b2009-03-27 23:10:48 +00002473NestedNameSpecifier *
2474ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002475 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002476 return 0;
2477
2478 switch (NNS->getKind()) {
2479 case NestedNameSpecifier::Identifier:
2480 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002481 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002482 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2483 NNS->getAsIdentifier());
2484
2485 case NestedNameSpecifier::Namespace:
2486 // A namespace is canonical; build a nested-name-specifier with
2487 // this namespace and no prefix.
2488 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2489
2490 case NestedNameSpecifier::TypeSpec:
2491 case NestedNameSpecifier::TypeSpecWithTemplate: {
2492 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002493 return NestedNameSpecifier::Create(*this, 0,
2494 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002495 T.getTypePtr());
2496 }
2497
2498 case NestedNameSpecifier::Global:
2499 // The global specifier is canonical and unique.
2500 return NNS;
2501 }
2502
2503 // Required to silence a GCC warning
2504 return 0;
2505}
2506
Chris Lattner7adf0762008-08-04 07:31:14 +00002507
2508const ArrayType *ASTContext::getAsArrayType(QualType T) {
2509 // Handle the non-qualified case efficiently.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002510 if (!T.hasLocalQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002511 // Handle the common positive case fast.
2512 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2513 return AT;
2514 }
Mike Stump11289f42009-09-09 15:08:12 +00002515
John McCall8ccfcb52009-09-24 19:53:00 +00002516 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002517 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002518 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002519 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002520
John McCall8ccfcb52009-09-24 19:53:00 +00002521 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002522 // implements C99 6.7.3p8: "If the specification of an array type includes
2523 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002524
Chris Lattner7adf0762008-08-04 07:31:14 +00002525 // If we get here, we either have type qualifiers on the type, or we have
2526 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002527 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002528
John McCall8ccfcb52009-09-24 19:53:00 +00002529 QualifierCollector Qs;
2530 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002531
Chris Lattner7adf0762008-08-04 07:31:14 +00002532 // If we have a simple case, just return now.
2533 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002534 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002535 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002536
Chris Lattner7adf0762008-08-04 07:31:14 +00002537 // Otherwise, we have an array and we have qualifiers on it. Push the
2538 // qualifiers into the array element type and return a new array type.
2539 // Get the canonical version of the element with the extra qualifiers on it.
2540 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002541 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002542
Chris Lattner7adf0762008-08-04 07:31:14 +00002543 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2544 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2545 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002546 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002547 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2548 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2549 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002550 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002551
Mike Stump11289f42009-09-09 15:08:12 +00002552 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002553 = dyn_cast<DependentSizedArrayType>(ATy))
2554 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002555 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002556 DSAT->getSizeExpr() ?
2557 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002558 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002559 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002560 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002561
Chris Lattner7adf0762008-08-04 07:31:14 +00002562 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002563 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002564 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002565 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002566 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002567 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002568 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002569}
2570
2571
Chris Lattnera21ad802008-04-02 05:18:44 +00002572/// getArrayDecayedType - Return the properly qualified result of decaying the
2573/// specified array type to a pointer. This operation is non-trivial when
2574/// handling typedefs etc. The canonical type of "T" must be an array type,
2575/// this returns a pointer to a properly qualified element of the array.
2576///
2577/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2578QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002579 // Get the element type with 'getAsArrayType' so that we don't lose any
2580 // typedefs in the element type of the array. This also handles propagation
2581 // of type qualifiers from the array type into the element type if present
2582 // (C99 6.7.3p8).
2583 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2584 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002585
Chris Lattner7adf0762008-08-04 07:31:14 +00002586 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002587
2588 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002589 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002590}
2591
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002592QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002593 QualifierCollector Qs;
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00002594 while (const ArrayType *AT = getAsArrayType(QualType(Qs.strip(QT), 0)))
2595 QT = AT->getElementType();
2596 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002597}
2598
Anders Carlsson4bf82142009-09-25 01:23:32 +00002599QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2600 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002601
Anders Carlsson4bf82142009-09-25 01:23:32 +00002602 if (const ArrayType *AT = getAsArrayType(ElemTy))
2603 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002604
Anders Carlssone0808df2008-12-21 03:44:36 +00002605 return ElemTy;
2606}
2607
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002608/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002609uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002610ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2611 uint64_t ElementCount = 1;
2612 do {
2613 ElementCount *= CA->getSize().getZExtValue();
2614 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2615 } while (CA);
2616 return ElementCount;
2617}
2618
Steve Naroff0af91202007-04-27 21:51:21 +00002619/// getFloatingRank - Return a relative rank for floating point types.
2620/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002621static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002622 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002623 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002624
John McCall9dd450b2009-09-21 23:43:11 +00002625 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2626 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002627 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002628 case BuiltinType::Float: return FloatRank;
2629 case BuiltinType::Double: return DoubleRank;
2630 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002631 }
2632}
2633
Mike Stump11289f42009-09-09 15:08:12 +00002634/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2635/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002636/// 'typeDomain' is a real floating point or complex type.
2637/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002638QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2639 QualType Domain) const {
2640 FloatingRank EltRank = getFloatingRank(Size);
2641 if (Domain->isComplexType()) {
2642 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002643 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002644 case FloatRank: return FloatComplexTy;
2645 case DoubleRank: return DoubleComplexTy;
2646 case LongDoubleRank: return LongDoubleComplexTy;
2647 }
Steve Naroff0af91202007-04-27 21:51:21 +00002648 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002649
2650 assert(Domain->isRealFloatingType() && "Unknown domain!");
2651 switch (EltRank) {
2652 default: assert(0 && "getFloatingRank(): illegal value for rank");
2653 case FloatRank: return FloatTy;
2654 case DoubleRank: return DoubleTy;
2655 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002656 }
Steve Naroffe4718892007-04-27 18:30:00 +00002657}
2658
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002659/// getFloatingTypeOrder - Compare the rank of the two specified floating
2660/// point types, ignoring the domain of the type (i.e. 'double' ==
2661/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002662/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002663int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2664 FloatingRank LHSR = getFloatingRank(LHS);
2665 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002666
Chris Lattnerb90739d2008-04-06 23:38:49 +00002667 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002668 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002669 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002670 return 1;
2671 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002672}
2673
Chris Lattner76a00cf2008-04-06 22:59:24 +00002674/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2675/// routine will assert if passed a built-in type that isn't an integer or enum,
2676/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002677unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002678 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002679 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall56774992009-12-09 09:09:27 +00002680 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedman1efaaea2009-02-13 02:31:07 +00002681
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002682 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2683 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2684
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002685 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2686 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2687
2688 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2689 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2690
Chris Lattner76a00cf2008-04-06 22:59:24 +00002691 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002692 default: assert(0 && "getIntegerRank(): not a built-in integer");
2693 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002694 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002695 case BuiltinType::Char_S:
2696 case BuiltinType::Char_U:
2697 case BuiltinType::SChar:
2698 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002699 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002700 case BuiltinType::Short:
2701 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002702 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002703 case BuiltinType::Int:
2704 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002705 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002706 case BuiltinType::Long:
2707 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002708 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002709 case BuiltinType::LongLong:
2710 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002711 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002712 case BuiltinType::Int128:
2713 case BuiltinType::UInt128:
2714 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002715 }
2716}
2717
Eli Friedman629ffb92009-08-20 04:21:42 +00002718/// \brief Whether this is a promotable bitfield reference according
2719/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2720///
2721/// \returns the type this bit-field will promote to, or NULL if no
2722/// promotion occurs.
2723QualType ASTContext::isPromotableBitField(Expr *E) {
2724 FieldDecl *Field = E->getBitField();
2725 if (!Field)
2726 return QualType();
2727
2728 QualType FT = Field->getType();
2729
2730 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2731 uint64_t BitWidth = BitWidthAP.getZExtValue();
2732 uint64_t IntSize = getTypeSize(IntTy);
2733 // GCC extension compatibility: if the bit-field size is less than or equal
2734 // to the size of int, it gets promoted no matter what its type is.
2735 // For instance, unsigned long bf : 4 gets promoted to signed int.
2736 if (BitWidth < IntSize)
2737 return IntTy;
2738
2739 if (BitWidth == IntSize)
2740 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2741
2742 // Types bigger than int are not subject to promotions, and therefore act
2743 // like the base type.
2744 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2745 // is ridiculous.
2746 return QualType();
2747}
2748
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002749/// getPromotedIntegerType - Returns the type that Promotable will
2750/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2751/// integer type.
2752QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2753 assert(!Promotable.isNull());
2754 assert(Promotable->isPromotableIntegerType());
John McCall56774992009-12-09 09:09:27 +00002755 if (const EnumType *ET = Promotable->getAs<EnumType>())
2756 return ET->getDecl()->getPromotionType();
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002757 if (Promotable->isSignedIntegerType())
2758 return IntTy;
2759 uint64_t PromotableSize = getTypeSize(Promotable);
2760 uint64_t IntSize = getTypeSize(IntTy);
2761 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2762 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2763}
2764
Mike Stump11289f42009-09-09 15:08:12 +00002765/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002766/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002767/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002768int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002769 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2770 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002771 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002772
Chris Lattner76a00cf2008-04-06 22:59:24 +00002773 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2774 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002775
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002776 unsigned LHSRank = getIntegerRank(LHSC);
2777 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002778
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002779 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2780 if (LHSRank == RHSRank) return 0;
2781 return LHSRank > RHSRank ? 1 : -1;
2782 }
Mike Stump11289f42009-09-09 15:08:12 +00002783
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002784 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2785 if (LHSUnsigned) {
2786 // If the unsigned [LHS] type is larger, return it.
2787 if (LHSRank >= RHSRank)
2788 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002789
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002790 // If the signed type can represent all values of the unsigned type, it
2791 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002792 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002793 return -1;
2794 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002795
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002796 // If the unsigned [RHS] type is larger, return it.
2797 if (RHSRank >= LHSRank)
2798 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002799
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002800 // If the signed type can represent all values of the unsigned type, it
2801 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002802 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002803 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002804}
Anders Carlsson98f07902007-08-17 05:31:46 +00002805
Anders Carlsson6d417272009-11-14 21:45:58 +00002806static RecordDecl *
2807CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2808 SourceLocation L, IdentifierInfo *Id) {
2809 if (Ctx.getLangOptions().CPlusPlus)
2810 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2811 else
2812 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2813}
2814
Mike Stump11289f42009-09-09 15:08:12 +00002815// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002816QualType ASTContext::getCFConstantStringType() {
2817 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002818 CFConstantStringTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002819 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2820 &Idents.get("NSConstantString"));
John McCallae580fe2010-02-05 01:33:36 +00002821 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson6d417272009-11-14 21:45:58 +00002822
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002823 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002824
Anders Carlsson98f07902007-08-17 05:31:46 +00002825 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002826 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002827 // int flags;
2828 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002829 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002830 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002831 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002832 FieldTypes[3] = LongTy;
2833
Anders Carlsson98f07902007-08-17 05:31:46 +00002834 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002835 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002836 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002837 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002838 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002839 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002840 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002841 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002842 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002843 }
2844
Douglas Gregord5058122010-02-11 01:19:42 +00002845 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson98f07902007-08-17 05:31:46 +00002846 }
Mike Stump11289f42009-09-09 15:08:12 +00002847
Anders Carlsson98f07902007-08-17 05:31:46 +00002848 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002849}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002850
Douglas Gregor512b0772009-04-23 22:29:11 +00002851void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002852 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002853 assert(Rec && "Invalid CFConstantStringType");
2854 CFConstantStringTypeDecl = Rec->getDecl();
2855}
2856
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002857// getNSConstantStringType - Return the type used for constant NSStrings.
2858QualType ASTContext::getNSConstantStringType() {
2859 if (!NSConstantStringTypeDecl) {
2860 NSConstantStringTypeDecl =
2861 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2862 &Idents.get("__builtin_NSString"));
2863 NSConstantStringTypeDecl->startDefinition();
2864
2865 QualType FieldTypes[3];
2866
2867 // const int *isa;
2868 FieldTypes[0] = getPointerType(IntTy.withConst());
2869 // const char *str;
2870 FieldTypes[1] = getPointerType(CharTy.withConst());
2871 // unsigned int length;
2872 FieldTypes[2] = UnsignedIntTy;
2873
2874 // Create fields
2875 for (unsigned i = 0; i < 3; ++i) {
2876 FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
2877 SourceLocation(), 0,
2878 FieldTypes[i], /*TInfo=*/0,
2879 /*BitWidth=*/0,
2880 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002881 Field->setAccess(AS_public);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002882 NSConstantStringTypeDecl->addDecl(Field);
2883 }
2884
2885 NSConstantStringTypeDecl->completeDefinition();
2886 }
2887
2888 return getTagDeclType(NSConstantStringTypeDecl);
2889}
2890
2891void ASTContext::setNSConstantStringType(QualType T) {
2892 const RecordType *Rec = T->getAs<RecordType>();
2893 assert(Rec && "Invalid NSConstantStringType");
2894 NSConstantStringTypeDecl = Rec->getDecl();
2895}
2896
Mike Stump11289f42009-09-09 15:08:12 +00002897QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002898 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002899 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson6d417272009-11-14 21:45:58 +00002900 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2901 &Idents.get("__objcFastEnumerationState"));
John McCallae580fe2010-02-05 01:33:36 +00002902 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002903
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002904 QualType FieldTypes[] = {
2905 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002906 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002907 getPointerType(UnsignedLongTy),
2908 getConstantArrayType(UnsignedLongTy,
2909 llvm::APInt(32, 5), ArrayType::Normal, 0)
2910 };
Mike Stump11289f42009-09-09 15:08:12 +00002911
Douglas Gregor91f84212008-12-11 16:49:14 +00002912 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002913 FieldDecl *Field = FieldDecl::Create(*this,
2914 ObjCFastEnumerationStateTypeDecl,
2915 SourceLocation(), 0,
John McCallbcd03502009-12-07 02:54:59 +00002916 FieldTypes[i], /*TInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002917 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002918 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002919 Field->setAccess(AS_public);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002920 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002921 }
Mike Stump11289f42009-09-09 15:08:12 +00002922
Douglas Gregord5058122010-02-11 01:19:42 +00002923 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002924 }
Mike Stump11289f42009-09-09 15:08:12 +00002925
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002926 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2927}
2928
Mike Stumpd0153282009-10-20 02:12:22 +00002929QualType ASTContext::getBlockDescriptorType() {
2930 if (BlockDescriptorType)
2931 return getTagDeclType(BlockDescriptorType);
2932
2933 RecordDecl *T;
2934 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002935 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2936 &Idents.get("__block_descriptor"));
John McCallae580fe2010-02-05 01:33:36 +00002937 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002938
2939 QualType FieldTypes[] = {
2940 UnsignedLongTy,
2941 UnsignedLongTy,
2942 };
2943
2944 const char *FieldNames[] = {
2945 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002946 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002947 };
2948
2949 for (size_t i = 0; i < 2; ++i) {
2950 FieldDecl *Field = FieldDecl::Create(*this,
2951 T,
2952 SourceLocation(),
2953 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00002954 FieldTypes[i], /*TInfo=*/0,
Mike Stumpd0153282009-10-20 02:12:22 +00002955 /*BitWidth=*/0,
2956 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00002957 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00002958 T->addDecl(Field);
2959 }
2960
Douglas Gregord5058122010-02-11 01:19:42 +00002961 T->completeDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00002962
2963 BlockDescriptorType = T;
2964
2965 return getTagDeclType(BlockDescriptorType);
2966}
2967
2968void ASTContext::setBlockDescriptorType(QualType T) {
2969 const RecordType *Rec = T->getAs<RecordType>();
2970 assert(Rec && "Invalid BlockDescriptorType");
2971 BlockDescriptorType = Rec->getDecl();
2972}
2973
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002974QualType ASTContext::getBlockDescriptorExtendedType() {
2975 if (BlockDescriptorExtendedType)
2976 return getTagDeclType(BlockDescriptorExtendedType);
2977
2978 RecordDecl *T;
2979 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson6d417272009-11-14 21:45:58 +00002980 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2981 &Idents.get("__block_descriptor_withcopydispose"));
John McCallae580fe2010-02-05 01:33:36 +00002982 T->startDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002983
2984 QualType FieldTypes[] = {
2985 UnsignedLongTy,
2986 UnsignedLongTy,
2987 getPointerType(VoidPtrTy),
2988 getPointerType(VoidPtrTy)
2989 };
2990
2991 const char *FieldNames[] = {
2992 "reserved",
2993 "Size",
2994 "CopyFuncPtr",
2995 "DestroyFuncPtr"
2996 };
2997
2998 for (size_t i = 0; i < 4; ++i) {
2999 FieldDecl *Field = FieldDecl::Create(*this,
3000 T,
3001 SourceLocation(),
3002 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003003 FieldTypes[i], /*TInfo=*/0,
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003004 /*BitWidth=*/0,
3005 /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003006 Field->setAccess(AS_public);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003007 T->addDecl(Field);
3008 }
3009
Douglas Gregord5058122010-02-11 01:19:42 +00003010 T->completeDefinition();
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003011
3012 BlockDescriptorExtendedType = T;
3013
3014 return getTagDeclType(BlockDescriptorExtendedType);
3015}
3016
3017void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3018 const RecordType *Rec = T->getAs<RecordType>();
3019 assert(Rec && "Invalid BlockDescriptorType");
3020 BlockDescriptorExtendedType = Rec->getDecl();
3021}
3022
Mike Stump94967902009-10-21 18:16:27 +00003023bool ASTContext::BlockRequiresCopying(QualType Ty) {
3024 if (Ty->isBlockPointerType())
3025 return true;
3026 if (isObjCNSObjectType(Ty))
3027 return true;
3028 if (Ty->isObjCObjectPointerType())
3029 return true;
3030 return false;
3031}
3032
3033QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3034 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003035 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00003036 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003037 // unsigned int __flags;
3038 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00003039 // void *__copy_helper; // as needed
3040 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00003041 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00003042 // } *
3043
Mike Stump94967902009-10-21 18:16:27 +00003044 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3045
3046 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003047 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003048 llvm::SmallString<36> Name;
3049 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3050 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00003051 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003052 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3053 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00003054 T->startDefinition();
3055 QualType Int32Ty = IntTy;
3056 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3057 QualType FieldTypes[] = {
3058 getPointerType(VoidPtrTy),
3059 getPointerType(getTagDeclType(T)),
3060 Int32Ty,
3061 Int32Ty,
3062 getPointerType(VoidPtrTy),
3063 getPointerType(VoidPtrTy),
3064 Ty
3065 };
3066
3067 const char *FieldNames[] = {
3068 "__isa",
3069 "__forwarding",
3070 "__flags",
3071 "__size",
3072 "__copy_helper",
3073 "__destroy_helper",
3074 DeclName,
3075 };
3076
3077 for (size_t i = 0; i < 7; ++i) {
3078 if (!HasCopyAndDispose && i >=4 && i <= 5)
3079 continue;
3080 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3081 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003082 FieldTypes[i], /*TInfo=*/0,
Mike Stump94967902009-10-21 18:16:27 +00003083 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003084 Field->setAccess(AS_public);
Mike Stump94967902009-10-21 18:16:27 +00003085 T->addDecl(Field);
3086 }
3087
Douglas Gregord5058122010-02-11 01:19:42 +00003088 T->completeDefinition();
Mike Stump94967902009-10-21 18:16:27 +00003089
3090 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00003091}
3092
3093
3094QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003095 bool BlockHasCopyDispose,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003096 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpd0153282009-10-20 02:12:22 +00003097 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00003098 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00003099 llvm::SmallString<36> Name;
3100 llvm::raw_svector_ostream(Name) << "__block_literal_"
3101 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00003102 RecordDecl *T;
Anders Carlsson6d417272009-11-14 21:45:58 +00003103 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3104 &Idents.get(Name.str()));
John McCallae580fe2010-02-05 01:33:36 +00003105 T->startDefinition();
Mike Stumpd0153282009-10-20 02:12:22 +00003106 QualType FieldTypes[] = {
3107 getPointerType(VoidPtrTy),
3108 IntTy,
3109 IntTy,
3110 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00003111 (BlockHasCopyDispose ?
3112 getPointerType(getBlockDescriptorExtendedType()) :
3113 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00003114 };
3115
3116 const char *FieldNames[] = {
3117 "__isa",
3118 "__flags",
3119 "__reserved",
3120 "__FuncPtr",
3121 "__descriptor"
3122 };
3123
3124 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00003125 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00003126 &Idents.get(FieldNames[i]),
John McCallbcd03502009-12-07 02:54:59 +00003127 FieldTypes[i], /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003128 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003129 Field->setAccess(AS_public);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003130 T->addDecl(Field);
3131 }
3132
3133 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3134 const Expr *E = BlockDeclRefDecls[i];
3135 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3136 clang::IdentifierInfo *Name = 0;
3137 if (BDRE) {
3138 const ValueDecl *D = BDRE->getDecl();
3139 Name = &Idents.get(D->getName());
3140 }
3141 QualType FieldType = E->getType();
3142
3143 if (BDRE && BDRE->isByRef())
Mike Stump94967902009-10-21 18:16:27 +00003144 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3145 FieldType);
Mike Stump7fe9cc12009-10-21 03:49:08 +00003146
3147 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCallbcd03502009-12-07 02:54:59 +00003148 Name, FieldType, /*TInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00003149 /*BitWidth=*/0, /*Mutable=*/false);
John McCall4d4dcc82010-04-30 21:35:41 +00003150 Field->setAccess(AS_public);
Mike Stumpd0153282009-10-20 02:12:22 +00003151 T->addDecl(Field);
3152 }
3153
Douglas Gregord5058122010-02-11 01:19:42 +00003154 T->completeDefinition();
Mike Stump7fe9cc12009-10-21 03:49:08 +00003155
3156 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00003157}
3158
Douglas Gregor512b0772009-04-23 22:29:11 +00003159void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003160 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003161 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3162 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3163}
3164
Anders Carlsson18acd442007-10-29 06:33:42 +00003165// This returns true if a type has been typedefed to BOOL:
3166// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00003167static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00003168 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00003169 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3170 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00003171
Anders Carlssond8499822007-10-29 05:01:08 +00003172 return false;
3173}
3174
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003175/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003176/// purpose.
Ken Dyckde37a672010-01-11 19:19:56 +00003177CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck40775002010-01-11 17:06:35 +00003178 CharUnits sz = getTypeSizeInChars(type);
Mike Stump11289f42009-09-09 15:08:12 +00003179
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003180 // Make all integer and enum types at least as large as an int
Ken Dyck40775002010-01-11 17:06:35 +00003181 if (sz.isPositive() && type->isIntegralType())
3182 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003183 // Treat arrays as pointers, since that's how they're passed in.
3184 else if (type->isArrayType())
Ken Dyck40775002010-01-11 17:06:35 +00003185 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckde37a672010-01-11 19:19:56 +00003186 return sz;
Ken Dyck40775002010-01-11 17:06:35 +00003187}
3188
3189static inline
3190std::string charUnitsToString(const CharUnits &CU) {
3191 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003192}
3193
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003194/// getObjCEncodingForBlockDecl - Return the encoded type for this block
David Chisnall950a9512009-11-17 19:33:30 +00003195/// declaration.
3196void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3197 std::string& S) {
3198 const BlockDecl *Decl = Expr->getBlockDecl();
3199 QualType BlockTy =
3200 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3201 // Encode result type.
3202 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3203 // Compute size of all parameters.
3204 // Start with computing size of a pointer in number of bytes.
3205 // FIXME: There might(should) be a better way of doing this computation!
3206 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003207 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3208 CharUnits ParmOffset = PtrSize;
Fariborz Jahanian590c3522010-04-08 18:06:22 +00003209 for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
David Chisnall950a9512009-11-17 19:33:30 +00003210 E = Decl->param_end(); PI != E; ++PI) {
3211 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003212 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003213 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall950a9512009-11-17 19:33:30 +00003214 ParmOffset += sz;
3215 }
3216 // Size of the argument frame
Ken Dyck40775002010-01-11 17:06:35 +00003217 S += charUnitsToString(ParmOffset);
David Chisnall950a9512009-11-17 19:33:30 +00003218 // Block pointer and offset.
3219 S += "@?0";
3220 ParmOffset = PtrSize;
3221
3222 // Argument types.
3223 ParmOffset = PtrSize;
3224 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3225 Decl->param_end(); PI != E; ++PI) {
3226 ParmVarDecl *PVDecl = *PI;
3227 QualType PType = PVDecl->getOriginalType();
3228 if (const ArrayType *AT =
3229 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3230 // Use array's original type only if it has known number of
3231 // elements.
3232 if (!isa<ConstantArrayType>(AT))
3233 PType = PVDecl->getType();
3234 } else if (PType->isFunctionType())
3235 PType = PVDecl->getType();
3236 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003237 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003238 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall950a9512009-11-17 19:33:30 +00003239 }
3240}
3241
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003242/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003243/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003244void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003245 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003246 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003247 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003248 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003249 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003250 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003251 // Compute size of all parameters.
3252 // Start with computing size of a pointer in number of bytes.
3253 // FIXME: There might(should) be a better way of doing this computation!
3254 SourceLocation Loc;
Ken Dyck40775002010-01-11 17:06:35 +00003255 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003256 // The first two arguments (self and _cmd) are pointers; account for
3257 // their size.
Ken Dyck40775002010-01-11 17:06:35 +00003258 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003259 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003260 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003261 QualType PType = (*PI)->getType();
Ken Dyckde37a672010-01-11 19:19:56 +00003262 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck40775002010-01-11 17:06:35 +00003263 assert (sz.isPositive() &&
3264 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003265 ParmOffset += sz;
3266 }
Ken Dyck40775002010-01-11 17:06:35 +00003267 S += charUnitsToString(ParmOffset);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003268 S += "@0:";
Ken Dyck40775002010-01-11 17:06:35 +00003269 S += charUnitsToString(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003270
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003271 // Argument types.
3272 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003273 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
Fariborz Jahaniand9235db2010-04-08 21:29:11 +00003274 E = Decl->sel_param_end(); PI != E; ++PI) {
Chris Lattnera4997152009-02-20 18:43:26 +00003275 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003276 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003277 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003278 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3279 // Use array's original type only if it has known number of
3280 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003281 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003282 PType = PVDecl->getType();
3283 } else if (PType->isFunctionType())
3284 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003285 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003286 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003287 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003288 getObjCEncodingForType(PType, S);
Ken Dyck40775002010-01-11 17:06:35 +00003289 S += charUnitsToString(ParmOffset);
Ken Dyckde37a672010-01-11 19:19:56 +00003290 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003291 }
3292}
3293
Daniel Dunbar4932b362008-08-28 04:38:10 +00003294/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003295/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003296/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3297/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003298/// Property attributes are stored as a comma-delimited C string. The simple
3299/// attributes readonly and bycopy are encoded as single characters. The
3300/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3301/// encoded as single characters, followed by an identifier. Property types
3302/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003303/// these attributes are defined by the following enumeration:
3304/// @code
3305/// enum PropertyAttributes {
3306/// kPropertyReadOnly = 'R', // property is read-only.
3307/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3308/// kPropertyByref = '&', // property is a reference to the value last assigned
3309/// kPropertyDynamic = 'D', // property is dynamic
3310/// kPropertyGetter = 'G', // followed by getter selector name
3311/// kPropertySetter = 'S', // followed by setter selector name
3312/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3313/// kPropertyType = 't' // followed by old-style type encoding.
3314/// kPropertyWeak = 'W' // 'weak' property
3315/// kPropertyStrong = 'P' // property GC'able
3316/// kPropertyNonAtomic = 'N' // property non-atomic
3317/// };
3318/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003319void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003320 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003321 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003322 // Collect information from the property implementation decl(s).
3323 bool Dynamic = false;
3324 ObjCPropertyImplDecl *SynthesizePID = 0;
3325
3326 // FIXME: Duplicated code due to poor abstraction.
3327 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003328 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003329 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3330 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003331 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003332 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003333 ObjCPropertyImplDecl *PID = *i;
3334 if (PID->getPropertyDecl() == PD) {
3335 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3336 Dynamic = true;
3337 } else {
3338 SynthesizePID = PID;
3339 }
3340 }
3341 }
3342 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003343 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003344 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003345 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003346 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003347 ObjCPropertyImplDecl *PID = *i;
3348 if (PID->getPropertyDecl() == PD) {
3349 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3350 Dynamic = true;
3351 } else {
3352 SynthesizePID = PID;
3353 }
3354 }
Mike Stump11289f42009-09-09 15:08:12 +00003355 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003356 }
3357 }
3358
3359 // FIXME: This is not very efficient.
3360 S = "T";
3361
3362 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003363 // GCC has some special rules regarding encoding of properties which
3364 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003365 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003366 true /* outermost type */,
3367 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003368
3369 if (PD->isReadOnly()) {
3370 S += ",R";
3371 } else {
3372 switch (PD->getSetterKind()) {
3373 case ObjCPropertyDecl::Assign: break;
3374 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003375 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003376 }
3377 }
3378
3379 // It really isn't clear at all what this means, since properties
3380 // are "dynamic by default".
3381 if (Dynamic)
3382 S += ",D";
3383
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003384 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3385 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003386
Daniel Dunbar4932b362008-08-28 04:38:10 +00003387 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3388 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003389 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003390 }
3391
3392 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3393 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003394 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003395 }
3396
3397 if (SynthesizePID) {
3398 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3399 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003400 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003401 }
3402
3403 // FIXME: OBJCGC: weak & strong
3404}
3405
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003406/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003407/// Another legacy compatibility encoding: 32-bit longs are encoded as
3408/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003409/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3410///
3411void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003412 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003413 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003414 if (BT->getKind() == BuiltinType::ULong &&
3415 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003416 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003417 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003418 if (BT->getKind() == BuiltinType::Long &&
3419 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003420 PointeeTy = IntTy;
3421 }
3422 }
3423}
3424
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003425void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003426 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003427 // We follow the behavior of gcc, expanding structures which are
3428 // directly pointed to, and expanding embedded structures. Note that
3429 // these rules are sufficient to prevent recursive encoding of the
3430 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003431 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003432 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003433}
3434
Mike Stump11289f42009-09-09 15:08:12 +00003435static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003436 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003437 const Expr *E = FD->getBitWidth();
3438 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3439 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00003440 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003441 S += 'b';
3442 S += llvm::utostr(N);
3443}
3444
Daniel Dunbar07d07852009-10-18 21:17:35 +00003445// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003446void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3447 bool ExpandPointedToStructures,
3448 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003449 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003450 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003451 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00003452 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003453 if (FD && FD->isBitField())
3454 return EncodeBitField(this, S, FD);
3455 char encoding;
3456 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003457 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00003458 case BuiltinType::Void: encoding = 'v'; break;
3459 case BuiltinType::Bool: encoding = 'B'; break;
3460 case BuiltinType::Char_U:
3461 case BuiltinType::UChar: encoding = 'C'; break;
3462 case BuiltinType::UShort: encoding = 'S'; break;
3463 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003464 case BuiltinType::ULong:
3465 encoding =
3466 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00003467 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003468 case BuiltinType::UInt128: encoding = 'T'; break;
3469 case BuiltinType::ULongLong: encoding = 'Q'; break;
3470 case BuiltinType::Char_S:
3471 case BuiltinType::SChar: encoding = 'c'; break;
3472 case BuiltinType::Short: encoding = 's'; break;
3473 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003474 case BuiltinType::Long:
3475 encoding =
3476 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003477 break;
3478 case BuiltinType::LongLong: encoding = 'q'; break;
3479 case BuiltinType::Int128: encoding = 't'; break;
3480 case BuiltinType::Float: encoding = 'f'; break;
3481 case BuiltinType::Double: encoding = 'd'; break;
3482 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003483 }
Mike Stump11289f42009-09-09 15:08:12 +00003484
Chris Lattnere7cabb92009-07-13 00:10:46 +00003485 S += encoding;
3486 return;
3487 }
Mike Stump11289f42009-09-09 15:08:12 +00003488
John McCall9dd450b2009-09-21 23:43:11 +00003489 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003490 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003491 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003492 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003493 return;
3494 }
Fariborz Jahaniand25c2192009-11-23 20:40:50 +00003495
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003496 // encoding for pointer or r3eference types.
3497 QualType PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003498 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian89b660c2009-11-30 18:43:52 +00003499 if (PT->isObjCSelType()) {
3500 S += ':';
3501 return;
3502 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003503 PointeeTy = PT->getPointeeType();
3504 }
3505 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
3506 PointeeTy = RT->getPointeeType();
3507 if (!PointeeTy.isNull()) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003508 bool isReadOnly = false;
3509 // For historical/compatibility reasons, the read-only qualifier of the
3510 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3511 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003512 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003513 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003514 if (OutermostType && T.isConstQualified()) {
3515 isReadOnly = true;
3516 S += 'r';
3517 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003518 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003519 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003520 while (P->getAs<PointerType>())
3521 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003522 if (P.isConstQualified()) {
3523 isReadOnly = true;
3524 S += 'r';
3525 }
3526 }
3527 if (isReadOnly) {
3528 // Another legacy compatibility encoding. Some ObjC qualifier and type
3529 // combinations need to be rearranged.
3530 // Rewrite "in const" from "nr" to "rn"
Benjamin Kramer2e3197e2010-04-27 17:12:11 +00003531 if (llvm::StringRef(S).endswith("nr"))
3532 S.replace(S.end()-2, S.end(), "rn");
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003533 }
Mike Stump11289f42009-09-09 15:08:12 +00003534
Anders Carlssond8499822007-10-29 05:01:08 +00003535 if (PointeeTy->isCharType()) {
3536 // char pointer types should be encoded as '*' unless it is a
3537 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003538 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003539 S += '*';
3540 return;
3541 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003542 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003543 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3544 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3545 S += '#';
3546 return;
3547 }
3548 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3549 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3550 S += '@';
3551 return;
3552 }
3553 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003554 }
Anders Carlssond8499822007-10-29 05:01:08 +00003555 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003556 getLegacyIntegralTypeEncoding(PointeeTy);
3557
Mike Stump11289f42009-09-09 15:08:12 +00003558 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003559 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003560 return;
3561 }
Fariborz Jahanian9ffd7062010-04-13 23:45:47 +00003562
Chris Lattnere7cabb92009-07-13 00:10:46 +00003563 if (const ArrayType *AT =
3564 // Ignore type qualifiers etc.
3565 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003566 if (isa<IncompleteArrayType>(AT)) {
3567 // Incomplete arrays are encoded as a pointer to the array element.
3568 S += '^';
3569
Mike Stump11289f42009-09-09 15:08:12 +00003570 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003571 false, ExpandStructures, FD);
3572 } else {
3573 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003574
Anders Carlssond05f44b2009-02-22 01:38:57 +00003575 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3576 S += llvm::utostr(CAT->getSize().getZExtValue());
3577 else {
3578 //Variable length arrays are encoded as a regular array with 0 elements.
3579 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3580 S += '0';
3581 }
Mike Stump11289f42009-09-09 15:08:12 +00003582
3583 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003584 false, ExpandStructures, FD);
3585 S += ']';
3586 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003587 return;
3588 }
Mike Stump11289f42009-09-09 15:08:12 +00003589
John McCall9dd450b2009-09-21 23:43:11 +00003590 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003591 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003592 return;
3593 }
Mike Stump11289f42009-09-09 15:08:12 +00003594
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003595 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003596 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003597 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003598 // Anonymous structures print as '?'
3599 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3600 S += II->getName();
Fariborz Jahanianc5158202010-05-07 00:28:49 +00003601 if (ClassTemplateSpecializationDecl *Spec
3602 = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
3603 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
3604 std::string TemplateArgsStr
3605 = TemplateSpecializationType::PrintTemplateArgumentList(
3606 TemplateArgs.getFlatArgumentList(),
3607 TemplateArgs.flat_size(),
3608 (*this).PrintingPolicy);
3609
3610 S += TemplateArgsStr;
3611 }
Daniel Dunbar40cac772008-10-17 06:22:57 +00003612 } else {
3613 S += '?';
3614 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003615 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003616 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003617 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3618 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003619 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003620 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003621 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003622 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003623 S += '"';
3624 }
Mike Stump11289f42009-09-09 15:08:12 +00003625
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003626 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003627 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003628 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003629 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003630 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003631 QualType qt = Field->getType();
3632 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003633 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003634 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003635 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003636 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003637 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003638 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003639 return;
3640 }
Mike Stump11289f42009-09-09 15:08:12 +00003641
Chris Lattnere7cabb92009-07-13 00:10:46 +00003642 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003643 if (FD && FD->isBitField())
3644 EncodeBitField(this, S, FD);
3645 else
3646 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003647 return;
3648 }
Mike Stump11289f42009-09-09 15:08:12 +00003649
Chris Lattnere7cabb92009-07-13 00:10:46 +00003650 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003651 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003652 return;
3653 }
Mike Stump11289f42009-09-09 15:08:12 +00003654
John McCall8ccfcb52009-09-24 19:53:00 +00003655 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003656 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003657 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003658 S += '{';
3659 const IdentifierInfo *II = OI->getIdentifier();
3660 S += II->getName();
3661 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003662 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003663 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003664 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003665 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003666 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003667 RecFields[i]);
3668 else
Mike Stump11289f42009-09-09 15:08:12 +00003669 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003670 FD);
3671 }
3672 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003673 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003674 }
Mike Stump11289f42009-09-09 15:08:12 +00003675
John McCall9dd450b2009-09-21 23:43:11 +00003676 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003677 if (OPT->isObjCIdType()) {
3678 S += '@';
3679 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003680 }
Mike Stump11289f42009-09-09 15:08:12 +00003681
Steve Narofff0c86112009-10-28 22:03:49 +00003682 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3683 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3684 // Since this is a binary compatibility issue, need to consult with runtime
3685 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003686 S += '#';
3687 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003688 }
Mike Stump11289f42009-09-09 15:08:12 +00003689
Chris Lattnere7cabb92009-07-13 00:10:46 +00003690 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003691 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003692 ExpandPointedToStructures,
3693 ExpandStructures, FD);
3694 if (FD || EncodingProperty) {
3695 // Note that we do extended encoding of protocol qualifer list
3696 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003697 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003698 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3699 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003700 S += '<';
3701 S += (*I)->getNameAsString();
3702 S += '>';
3703 }
3704 S += '"';
3705 }
3706 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003707 }
Mike Stump11289f42009-09-09 15:08:12 +00003708
Chris Lattnere7cabb92009-07-13 00:10:46 +00003709 QualType PointeeTy = OPT->getPointeeType();
3710 if (!EncodingProperty &&
3711 isa<TypedefType>(PointeeTy.getTypePtr())) {
3712 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003713 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003714 // {...};
3715 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003716 getObjCEncodingForTypeImpl(PointeeTy, S,
3717 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003718 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003719 return;
3720 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003721
3722 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003723 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003724 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003725 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003726 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3727 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003728 S += '<';
3729 S += (*I)->getNameAsString();
3730 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003731 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003732 S += '"';
3733 }
3734 return;
3735 }
Mike Stump11289f42009-09-09 15:08:12 +00003736
Chris Lattnere7cabb92009-07-13 00:10:46 +00003737 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003738}
3739
Mike Stump11289f42009-09-09 15:08:12 +00003740void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003741 std::string& S) const {
3742 if (QT & Decl::OBJC_TQ_In)
3743 S += 'n';
3744 if (QT & Decl::OBJC_TQ_Inout)
3745 S += 'N';
3746 if (QT & Decl::OBJC_TQ_Out)
3747 S += 'o';
3748 if (QT & Decl::OBJC_TQ_Bycopy)
3749 S += 'O';
3750 if (QT & Decl::OBJC_TQ_Byref)
3751 S += 'R';
3752 if (QT & Decl::OBJC_TQ_Oneway)
3753 S += 'V';
3754}
3755
Chris Lattnere7cabb92009-07-13 00:10:46 +00003756void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003757 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003758
Anders Carlsson87c149b2007-10-11 01:00:40 +00003759 BuiltinVaListType = T;
3760}
3761
Chris Lattnere7cabb92009-07-13 00:10:46 +00003762void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003763 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003764}
3765
Chris Lattnere7cabb92009-07-13 00:10:46 +00003766void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian252ba5f2009-11-21 19:53:08 +00003767 ObjCSelTypedefType = T;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003768}
3769
Chris Lattnere7cabb92009-07-13 00:10:46 +00003770void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003771 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003772}
3773
Chris Lattnere7cabb92009-07-13 00:10:46 +00003774void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003775 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003776}
3777
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003778void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003779 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003780 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003781
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003782 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003783}
3784
John McCalld28ae272009-12-02 08:04:21 +00003785/// \brief Retrieve the template name that corresponds to a non-empty
3786/// lookup.
John McCallad371252010-01-20 00:46:10 +00003787TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3788 UnresolvedSetIterator End) {
John McCalld28ae272009-12-02 08:04:21 +00003789 unsigned size = End - Begin;
3790 assert(size > 1 && "set is not overloaded!");
3791
3792 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3793 size * sizeof(FunctionTemplateDecl*));
3794 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3795
3796 NamedDecl **Storage = OT->getStorage();
John McCallad371252010-01-20 00:46:10 +00003797 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCalld28ae272009-12-02 08:04:21 +00003798 NamedDecl *D = *I;
3799 assert(isa<FunctionTemplateDecl>(D) ||
3800 (isa<UsingShadowDecl>(D) &&
3801 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3802 *Storage++ = D;
3803 }
3804
3805 return TemplateName(OT);
3806}
3807
Douglas Gregordc572a32009-03-30 22:58:21 +00003808/// \brief Retrieve the template name that represents a qualified
3809/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003810TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003811 bool TemplateKeyword,
3812 TemplateDecl *Template) {
Douglas Gregorc42075a2010-02-04 18:10:26 +00003813 // FIXME: Canonicalization?
Douglas Gregordc572a32009-03-30 22:58:21 +00003814 llvm::FoldingSetNodeID ID;
3815 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3816
3817 void *InsertPos = 0;
3818 QualifiedTemplateName *QTN =
3819 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3820 if (!QTN) {
3821 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3822 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3823 }
3824
3825 return TemplateName(QTN);
3826}
3827
3828/// \brief Retrieve the template name that represents a dependent
3829/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003830TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003831 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003832 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003833 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003834
3835 llvm::FoldingSetNodeID ID;
3836 DependentTemplateName::Profile(ID, NNS, Name);
3837
3838 void *InsertPos = 0;
3839 DependentTemplateName *QTN =
3840 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3841
3842 if (QTN)
3843 return TemplateName(QTN);
3844
3845 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3846 if (CanonNNS == NNS) {
3847 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3848 } else {
3849 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3850 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003851 DependentTemplateName *CheckQTN =
3852 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3853 assert(!CheckQTN && "Dependent type name canonicalization broken");
3854 (void)CheckQTN;
Douglas Gregordc572a32009-03-30 22:58:21 +00003855 }
3856
3857 DependentTemplateNames.InsertNode(QTN, InsertPos);
3858 return TemplateName(QTN);
3859}
3860
Douglas Gregor71395fa2009-11-04 00:56:37 +00003861/// \brief Retrieve the template name that represents a dependent
3862/// template name such as \c MetaFun::template operator+.
3863TemplateName
3864ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3865 OverloadedOperatorKind Operator) {
3866 assert((!NNS || NNS->isDependent()) &&
3867 "Nested name specifier must be dependent");
3868
3869 llvm::FoldingSetNodeID ID;
3870 DependentTemplateName::Profile(ID, NNS, Operator);
3871
3872 void *InsertPos = 0;
Douglas Gregorc42075a2010-02-04 18:10:26 +00003873 DependentTemplateName *QTN
3874 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor71395fa2009-11-04 00:56:37 +00003875
3876 if (QTN)
3877 return TemplateName(QTN);
3878
3879 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3880 if (CanonNNS == NNS) {
3881 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3882 } else {
3883 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3884 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregorc42075a2010-02-04 18:10:26 +00003885
3886 DependentTemplateName *CheckQTN
3887 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3888 assert(!CheckQTN && "Dependent template name canonicalization broken");
3889 (void)CheckQTN;
Douglas Gregor71395fa2009-11-04 00:56:37 +00003890 }
3891
3892 DependentTemplateNames.InsertNode(QTN, InsertPos);
3893 return TemplateName(QTN);
3894}
3895
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003896/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003897/// TargetInfo, produce the corresponding type. The unsigned @p Type
3898/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003899CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003900 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003901 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003902 case TargetInfo::SignedShort: return ShortTy;
3903 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3904 case TargetInfo::SignedInt: return IntTy;
3905 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3906 case TargetInfo::SignedLong: return LongTy;
3907 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3908 case TargetInfo::SignedLongLong: return LongLongTy;
3909 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3910 }
3911
3912 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003913 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003914}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003915
3916//===----------------------------------------------------------------------===//
3917// Type Predicates.
3918//===----------------------------------------------------------------------===//
3919
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003920/// isObjCNSObjectType - Return true if this is an NSObject object using
3921/// NSObject attribute on a c-style pointer type.
3922/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003923/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003924///
3925bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3926 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3927 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003928 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003929 return true;
3930 }
Mike Stump11289f42009-09-09 15:08:12 +00003931 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003932}
3933
Fariborz Jahanian96207692009-02-18 21:49:28 +00003934/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3935/// garbage collection attribute.
3936///
John McCall8ccfcb52009-09-24 19:53:00 +00003937Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3938 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003939 if (getLangOptions().ObjC1 &&
3940 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003941 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003942 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003943 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003944 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003945 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003946 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003947 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003948 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003949 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003950 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003951 // Non-pointers have none gc'able attribute regardless of the attribute
3952 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003953 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003954 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003955 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003956 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003957}
3958
Chris Lattner49af6a42008-04-07 06:51:04 +00003959//===----------------------------------------------------------------------===//
3960// Type Compatibility Testing
3961//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00003962
Mike Stump11289f42009-09-09 15:08:12 +00003963/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003964/// compatible.
3965static bool areCompatVectorTypes(const VectorType *LHS,
3966 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00003967 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00003968 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00003969 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00003970}
3971
Steve Naroff8e6aee52009-07-23 01:01:38 +00003972//===----------------------------------------------------------------------===//
3973// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3974//===----------------------------------------------------------------------===//
3975
3976/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3977/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003978bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3979 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003980 if (lProto == rProto)
3981 return true;
3982 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3983 E = rProto->protocol_end(); PI != E; ++PI)
3984 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3985 return true;
3986 return false;
3987}
3988
Steve Naroff8e6aee52009-07-23 01:01:38 +00003989/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3990/// return true if lhs's protocols conform to rhs's protocol; false
3991/// otherwise.
3992bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3993 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3994 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3995 return false;
3996}
3997
3998/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3999/// ObjCQualifiedIDType.
4000bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4001 bool compare) {
4002 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00004003 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004004 lhs->isObjCIdType() || lhs->isObjCClassType())
4005 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004006 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00004007 rhs->isObjCIdType() || rhs->isObjCClassType())
4008 return true;
4009
4010 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00004011 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004012
Steve Naroff8e6aee52009-07-23 01:01:38 +00004013 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00004014
Steve Naroff8e6aee52009-07-23 01:01:38 +00004015 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004016 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004017 // make sure we check the class hierarchy.
4018 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4019 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4020 E = lhsQID->qual_end(); I != E; ++I) {
4021 // when comparing an id<P> on lhs with a static type on rhs,
4022 // see if static class implements all of id's protocols, directly or
4023 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004024 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00004025 return false;
4026 }
4027 }
4028 // If there are no qualifiers and no interface, we have an 'id'.
4029 return true;
4030 }
Mike Stump11289f42009-09-09 15:08:12 +00004031 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004032 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4033 E = lhsQID->qual_end(); I != E; ++I) {
4034 ObjCProtocolDecl *lhsProto = *I;
4035 bool match = false;
4036
4037 // when comparing an id<P> on lhs with a static type on rhs,
4038 // see if static class implements all of id's protocols, directly or
4039 // through its super class and categories.
4040 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4041 E = rhsOPT->qual_end(); J != E; ++J) {
4042 ObjCProtocolDecl *rhsProto = *J;
4043 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4044 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4045 match = true;
4046 break;
4047 }
4048 }
Mike Stump11289f42009-09-09 15:08:12 +00004049 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00004050 // make sure we check the class hierarchy.
4051 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4052 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4053 E = lhsQID->qual_end(); I != E; ++I) {
4054 // when comparing an id<P> on lhs with a static type on rhs,
4055 // see if static class implements all of id's protocols, directly or
4056 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004057 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004058 match = true;
4059 break;
4060 }
4061 }
4062 }
4063 if (!match)
4064 return false;
4065 }
Mike Stump11289f42009-09-09 15:08:12 +00004066
Steve Naroff8e6aee52009-07-23 01:01:38 +00004067 return true;
4068 }
Mike Stump11289f42009-09-09 15:08:12 +00004069
Steve Naroff8e6aee52009-07-23 01:01:38 +00004070 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4071 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4072
Mike Stump11289f42009-09-09 15:08:12 +00004073 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00004074 lhs->getAsObjCInterfacePointerType()) {
4075 if (lhsOPT->qual_empty()) {
4076 bool match = false;
4077 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4078 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4079 E = rhsQID->qual_end(); I != E; ++I) {
4080 // when comparing an id<P> on lhs with a static type on rhs,
4081 // see if static class implements all of id's protocols, directly or
4082 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00004083 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00004084 match = true;
4085 break;
4086 }
4087 }
4088 if (!match)
4089 return false;
4090 }
4091 return true;
4092 }
Mike Stump11289f42009-09-09 15:08:12 +00004093 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00004094 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4095 E = lhsOPT->qual_end(); I != E; ++I) {
4096 ObjCProtocolDecl *lhsProto = *I;
4097 bool match = false;
4098
4099 // when comparing an id<P> on lhs with a static type on rhs,
4100 // see if static class implements all of id's protocols, directly or
4101 // through its super class and categories.
4102 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4103 E = rhsQID->qual_end(); J != E; ++J) {
4104 ObjCProtocolDecl *rhsProto = *J;
4105 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4106 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4107 match = true;
4108 break;
4109 }
4110 }
4111 if (!match)
4112 return false;
4113 }
4114 return true;
4115 }
4116 return false;
4117}
4118
Eli Friedman47f77112008-08-22 00:56:42 +00004119/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00004120/// compatible for assignment from RHS to LHS. This handles validation of any
4121/// protocol qualifiers on the LHS or RHS.
4122///
Steve Naroff7cae42b2009-07-10 23:34:53 +00004123bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4124 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00004125 // If either type represents the built-in 'id' or 'Class' types, return true.
4126 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00004127 return true;
4128
Steve Naroff8e6aee52009-07-23 01:01:38 +00004129 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00004130 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4131 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004132 false);
4133
Steve Naroff7cae42b2009-07-10 23:34:53 +00004134 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4135 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00004136 if (LHS && RHS) // We have 2 user-defined types.
4137 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00004138
Steve Naroff8e6aee52009-07-23 01:01:38 +00004139 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004140}
4141
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004142/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
4143/// for providing type-safty for objective-c pointers used to pass/return
4144/// arguments in block literals. When passed as arguments, passing 'A*' where
4145/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
4146/// not OK. For the return type, the opposite is not OK.
4147bool ASTContext::canAssignObjCInterfacesInBlockPointer(
4148 const ObjCObjectPointerType *LHSOPT,
4149 const ObjCObjectPointerType *RHSOPT) {
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004150 if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004151 return true;
4152
4153 if (LHSOPT->isObjCBuiltinType()) {
4154 return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
4155 }
4156
Fariborz Jahanian440a6832010-04-06 17:23:39 +00004157 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004158 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4159 QualType(RHSOPT,0),
4160 false);
4161
4162 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4163 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4164 if (LHS && RHS) { // We have 2 user-defined types.
4165 if (LHS != RHS) {
4166 if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4167 return false;
4168 if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
4169 return true;
4170 }
4171 else
4172 return true;
4173 }
4174 return false;
4175}
4176
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004177/// getIntersectionOfProtocols - This routine finds the intersection of set
4178/// of protocols inherited from two distinct objective-c pointer objects.
4179/// It is used to build composite qualifier list of the composite type of
4180/// the conditional expression involving two objective-c pointer objects.
4181static
4182void getIntersectionOfProtocols(ASTContext &Context,
4183 const ObjCObjectPointerType *LHSOPT,
4184 const ObjCObjectPointerType *RHSOPT,
4185 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4186
4187 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4188 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4189
4190 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4191 unsigned LHSNumProtocols = LHS->getNumProtocols();
4192 if (LHSNumProtocols > 0)
4193 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4194 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004195 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4196 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004197 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4198 LHSInheritedProtocols.end());
4199 }
4200
4201 unsigned RHSNumProtocols = RHS->getNumProtocols();
4202 if (RHSNumProtocols > 0) {
Dan Gohman145f3f12010-04-19 16:39:44 +00004203 ObjCProtocolDecl **RHSProtocols =
4204 const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004205 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4206 if (InheritedProtocolSet.count(RHSProtocols[i]))
4207 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4208 }
4209 else {
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004210 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004211 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahaniandc68f952010-02-12 19:27:33 +00004212 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4213 RHSInheritedProtocols.begin(),
4214 E = RHSInheritedProtocols.end(); I != E; ++I)
4215 if (InheritedProtocolSet.count((*I)))
4216 IntersectionOfProtocols.push_back((*I));
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004217 }
4218}
4219
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004220/// areCommonBaseCompatible - Returns common base class of the two classes if
4221/// one found. Note that this is O'2 algorithm. But it will be called as the
4222/// last type comparison in a ?-exp of ObjC pointer types before a
4223/// warning is issued. So, its invokation is extremely rare.
4224QualType ASTContext::areCommonBaseCompatible(
4225 const ObjCObjectPointerType *LHSOPT,
4226 const ObjCObjectPointerType *RHSOPT) {
4227 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4228 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4229 if (!LHS || !RHS)
4230 return QualType();
4231
4232 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4233 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4234 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahanian6c5a8e22009-10-30 01:13:23 +00004235 if (canAssignObjCInterfaces(LHS, RHS)) {
4236 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4237 getIntersectionOfProtocols(*this,
4238 LHSOPT, RHSOPT, IntersectionOfProtocols);
4239 if (IntersectionOfProtocols.empty())
4240 LHSTy = getObjCObjectPointerType(LHSTy);
4241 else
4242 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4243 IntersectionOfProtocols.size());
4244 return LHSTy;
4245 }
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00004246 }
4247
4248 return QualType();
4249}
4250
Eli Friedman47f77112008-08-22 00:56:42 +00004251bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4252 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00004253 // Verify that the base decls are compatible: the RHS must be a subclass of
4254 // the LHS.
4255 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4256 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004257
Chris Lattner49af6a42008-04-07 06:51:04 +00004258 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4259 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00004260 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004261 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004262
Chris Lattner49af6a42008-04-07 06:51:04 +00004263 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4264 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00004265 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00004266 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00004267
Steve Naroffc277ad12009-07-18 15:33:26 +00004268 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4269 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00004270 LHSPI != LHSPE; LHSPI++) {
4271 bool RHSImplementsProtocol = false;
4272
4273 // If the RHS doesn't implement the protocol on the left, the types
4274 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00004275 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00004276 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00004277 RHSPI != RHSPE; RHSPI++) {
4278 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00004279 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00004280 break;
4281 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004282 }
4283 // FIXME: For better diagnostics, consider passing back the protocol name.
4284 if (!RHSImplementsProtocol)
4285 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00004286 }
Steve Naroff114aecb2009-03-01 16:12:44 +00004287 // The RHS implements all protocols listed on the LHS.
4288 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00004289}
4290
Steve Naroffb7605152009-02-12 17:52:19 +00004291bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4292 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00004293 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4294 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00004295
Steve Naroff7cae42b2009-07-10 23:34:53 +00004296 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00004297 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00004298
4299 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4300 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00004301}
4302
Mike Stump11289f42009-09-09 15:08:12 +00004303/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00004304/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00004305/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00004306/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00004307bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor21e771e2010-02-03 21:02:30 +00004308 if (getLangOptions().CPlusPlus)
4309 return hasSameType(LHS, RHS);
4310
Eli Friedman47f77112008-08-22 00:56:42 +00004311 return !mergeTypes(LHS, RHS).isNull();
4312}
4313
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004314bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
4315 return !mergeTypes(LHS, RHS, true).isNull();
4316}
4317
4318QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
4319 bool OfBlockPointer) {
John McCall9dd450b2009-09-21 23:43:11 +00004320 const FunctionType *lbase = lhs->getAs<FunctionType>();
4321 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004322 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4323 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00004324 bool allLTypes = true;
4325 bool allRTypes = true;
4326
4327 // Check return type
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004328 QualType retType;
4329 if (OfBlockPointer)
4330 retType = mergeTypes(rbase->getResultType(), lbase->getResultType(), true);
4331 else
4332 retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman47f77112008-08-22 00:56:42 +00004333 if (retType.isNull()) return QualType();
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004334 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004335 allLTypes = false;
Fariborz Jahanian113b8ad2010-02-10 00:32:12 +00004336 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner465fa322008-10-05 17:34:18 +00004337 allRTypes = false;
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004338 // FIXME: double check this
4339 // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
4340 // rbase->getRegParmAttr() != 0 &&
4341 // lbase->getRegParmAttr() != rbase->getRegParmAttr()?
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004342 FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
4343 FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
Daniel Dunbaredd5bae2010-04-28 16:20:58 +00004344 unsigned RegParm = lbaseInfo.getRegParm() == 0 ? rbaseInfo.getRegParm() :
4345 lbaseInfo.getRegParm();
4346 bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
4347 if (NoReturn != lbaseInfo.getNoReturn() ||
4348 RegParm != lbaseInfo.getRegParm())
4349 allLTypes = false;
4350 if (NoReturn != rbaseInfo.getNoReturn() ||
4351 RegParm != rbaseInfo.getRegParm())
4352 allRTypes = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004353 CallingConv lcc = lbaseInfo.getCC();
4354 CallingConv rcc = rbaseInfo.getCC();
Douglas Gregor8c940862010-01-18 17:14:39 +00004355 // Compatible functions must have compatible calling conventions
John McCallab26cfa2010-02-05 21:31:56 +00004356 if (!isSameCallConv(lcc, rcc))
Douglas Gregor8c940862010-01-18 17:14:39 +00004357 return QualType();
Mike Stump11289f42009-09-09 15:08:12 +00004358
Eli Friedman47f77112008-08-22 00:56:42 +00004359 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004360 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4361 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004362 unsigned lproto_nargs = lproto->getNumArgs();
4363 unsigned rproto_nargs = rproto->getNumArgs();
4364
4365 // Compatible functions must have the same number of arguments
4366 if (lproto_nargs != rproto_nargs)
4367 return QualType();
4368
4369 // Variadic and non-variadic functions aren't compatible
4370 if (lproto->isVariadic() != rproto->isVariadic())
4371 return QualType();
4372
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00004373 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4374 return QualType();
4375
Eli Friedman47f77112008-08-22 00:56:42 +00004376 // Check argument compatibility
4377 llvm::SmallVector<QualType, 10> types;
4378 for (unsigned i = 0; i < lproto_nargs; i++) {
4379 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4380 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004381 QualType argtype = mergeTypes(largtype, rargtype, OfBlockPointer);
Eli Friedman47f77112008-08-22 00:56:42 +00004382 if (argtype.isNull()) return QualType();
4383 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00004384 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4385 allLTypes = false;
4386 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4387 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00004388 }
4389 if (allLTypes) return lhs;
4390 if (allRTypes) return rhs;
4391 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00004392 lproto->isVariadic(), lproto->getTypeQuals(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004393 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004394 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004395 }
4396
4397 if (lproto) allRTypes = false;
4398 if (rproto) allLTypes = false;
4399
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004400 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004401 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004402 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004403 if (proto->isVariadic()) return QualType();
4404 // Check that the types are compatible with the types that
4405 // would result from default argument promotions (C99 6.7.5.3p15).
4406 // The only types actually affected are promotable integer
4407 // types and floats, which would be passed as a different
4408 // type depending on whether the prototype is visible.
4409 unsigned proto_nargs = proto->getNumArgs();
4410 for (unsigned i = 0; i < proto_nargs; ++i) {
4411 QualType argTy = proto->getArgType(i);
Douglas Gregor2973d402010-02-03 19:27:29 +00004412
4413 // Look at the promotion type of enum types, since that is the type used
4414 // to pass enum values.
4415 if (const EnumType *Enum = argTy->getAs<EnumType>())
4416 argTy = Enum->getDecl()->getPromotionType();
4417
Eli Friedman47f77112008-08-22 00:56:42 +00004418 if (argTy->isPromotableIntegerType() ||
4419 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4420 return QualType();
4421 }
4422
4423 if (allLTypes) return lhs;
4424 if (allRTypes) return rhs;
4425 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004426 proto->getNumArgs(), proto->isVariadic(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004427 proto->getTypeQuals(),
4428 false, false, 0, 0,
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004429 FunctionType::ExtInfo(NoReturn, RegParm, lcc));
Eli Friedman47f77112008-08-22 00:56:42 +00004430 }
4431
4432 if (allLTypes) return lhs;
4433 if (allRTypes) return rhs;
Rafael Espindola49b85ab2010-03-30 22:15:11 +00004434 FunctionType::ExtInfo Info(NoReturn, RegParm, lcc);
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004435 return getFunctionNoProtoType(retType, Info);
Eli Friedman47f77112008-08-22 00:56:42 +00004436}
4437
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004438QualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
4439 bool OfBlockPointer) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004440 // C++ [expr]: If an expression initially has the type "reference to T", the
4441 // type is adjusted to "T" prior to any further analysis, the expression
4442 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004443 // expression is an lvalue unless the reference is an rvalue reference and
4444 // the expression is a function call (possibly inside parentheses).
Douglas Gregor21e771e2010-02-03 21:02:30 +00004445 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4446 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4447
Eli Friedman47f77112008-08-22 00:56:42 +00004448 QualType LHSCan = getCanonicalType(LHS),
4449 RHSCan = getCanonicalType(RHS);
4450
4451 // If two types are identical, they are compatible.
4452 if (LHSCan == RHSCan)
4453 return LHS;
4454
John McCall8ccfcb52009-09-24 19:53:00 +00004455 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00004456 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4457 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall8ccfcb52009-09-24 19:53:00 +00004458 if (LQuals != RQuals) {
4459 // If any of these qualifiers are different, we have a type
4460 // mismatch.
4461 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4462 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4463 return QualType();
4464
4465 // Exactly one GC qualifier difference is allowed: __strong is
4466 // okay if the other type has no GC qualifier but is an Objective
4467 // C object pointer (i.e. implicitly strong by default). We fix
4468 // this by pretending that the unqualified type was actually
4469 // qualified __strong.
4470 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4471 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4472 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4473
4474 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4475 return QualType();
4476
4477 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4478 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4479 }
4480 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4481 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4482 }
Eli Friedman47f77112008-08-22 00:56:42 +00004483 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004484 }
4485
4486 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004487
Eli Friedmandcca6332009-06-01 01:22:52 +00004488 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4489 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004490
Chris Lattnerfd652912008-01-14 05:45:46 +00004491 // We want to consider the two function types to be the same for these
4492 // comparisons, just force one to the other.
4493 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4494 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004495
4496 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004497 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4498 LHSClass = Type::ConstantArray;
4499 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4500 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004501
Nate Begemance4d7fc2008-04-18 23:10:10 +00004502 // Canonicalize ExtVector -> Vector.
4503 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4504 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004505
Chris Lattner95554662008-04-07 05:43:21 +00004506 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004507 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004508 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004509 // a signed integer type, or an unsigned integer type.
John McCall56774992009-12-09 09:09:27 +00004510 // Compatibility is based on the underlying type, not the promotion
4511 // type.
John McCall9dd450b2009-09-21 23:43:11 +00004512 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004513 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4514 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004515 }
John McCall9dd450b2009-09-21 23:43:11 +00004516 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004517 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4518 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004519 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004520
Eli Friedman47f77112008-08-22 00:56:42 +00004521 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004522 }
Eli Friedman47f77112008-08-22 00:56:42 +00004523
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004524 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004525 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004526#define TYPE(Class, Base)
4527#define ABSTRACT_TYPE(Class, Base)
John McCallbd8d9bd2010-03-01 23:49:17 +00004528#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004529#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4530#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4531#include "clang/AST/TypeNodes.def"
4532 assert(false && "Non-canonical and dependent types shouldn't get here");
4533 return QualType();
4534
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004535 case Type::LValueReference:
4536 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004537 case Type::MemberPointer:
4538 assert(false && "C++ should never be in mergeTypes");
4539 return QualType();
4540
4541 case Type::IncompleteArray:
4542 case Type::VariableArray:
4543 case Type::FunctionProto:
4544 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004545 assert(false && "Types are eliminated above");
4546 return QualType();
4547
Chris Lattnerfd652912008-01-14 05:45:46 +00004548 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004549 {
4550 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004551 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4552 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004553 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4554 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004555 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004556 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004557 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004558 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004559 return getPointerType(ResultType);
4560 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004561 case Type::BlockPointer:
4562 {
4563 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004564 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4565 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004566 QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer);
Steve Naroff68e167d2008-12-10 17:49:55 +00004567 if (ResultType.isNull()) return QualType();
4568 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4569 return LHS;
4570 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4571 return RHS;
4572 return getBlockPointerType(ResultType);
4573 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004574 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004575 {
4576 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4577 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4578 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4579 return QualType();
4580
4581 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4582 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4583 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4584 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004585 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4586 return LHS;
4587 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4588 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004589 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4590 ArrayType::ArraySizeModifier(), 0);
4591 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4592 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004593 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4594 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004595 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4596 return LHS;
4597 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4598 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004599 if (LVAT) {
4600 // FIXME: This isn't correct! But tricky to implement because
4601 // the array's size has to be the size of LHS, but the type
4602 // has to be different.
4603 return LHS;
4604 }
4605 if (RVAT) {
4606 // FIXME: This isn't correct! But tricky to implement because
4607 // the array's size has to be the size of RHS, but the type
4608 // has to be different.
4609 return RHS;
4610 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004611 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4612 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004613 return getIncompleteArrayType(ResultType,
4614 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004615 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004616 case Type::FunctionNoProto:
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004617 return mergeFunctionTypes(LHS, RHS, OfBlockPointer);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004618 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004619 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004620 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004621 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004622 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004623 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004624 case Type::Complex:
4625 // Distinct complex types are incompatible.
4626 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004627 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004628 // FIXME: The merged type should be an ExtVector!
John McCall44c064b2010-03-12 23:14:13 +00004629 if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
4630 RHSCan->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004631 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004632 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004633 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00004634 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004635 // FIXME: This should be type compatibility, e.g. whether
4636 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00004637 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4638 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00004639 if (LHSIface && RHSIface &&
4640 canAssignObjCInterfaces(LHSIface, RHSIface))
4641 return LHS;
4642
Eli Friedman47f77112008-08-22 00:56:42 +00004643 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004644 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004645 case Type::ObjCObjectPointer: {
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004646 if (OfBlockPointer) {
4647 if (canAssignObjCInterfacesInBlockPointer(
4648 LHS->getAs<ObjCObjectPointerType>(),
4649 RHS->getAs<ObjCObjectPointerType>()))
4650 return LHS;
4651 return QualType();
4652 }
John McCall9dd450b2009-09-21 23:43:11 +00004653 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4654 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004655 return LHS;
4656
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004657 return QualType();
Fariborz Jahanianb8b0ea32010-03-17 00:20:01 +00004658 }
Steve Naroff32e44c02007-10-15 20:41:53 +00004659 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004660
4661 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004662}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004663
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004664//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004665// Integer Predicates
4666//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004667
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004668unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl87869bc2009-11-05 21:10:57 +00004669 if (T->isBooleanType())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004670 return 1;
John McCall56774992009-12-09 09:09:27 +00004671 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedmanee275c82009-12-10 22:29:29 +00004672 T = ET->getDecl()->getIntegerType();
Eli Friedman1efaaea2009-02-13 02:31:07 +00004673 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004674 return (unsigned)getTypeSize(T);
4675}
4676
4677QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4678 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004679
4680 // Turn <4 x signed int> -> <4 x unsigned int>
4681 if (const VectorType *VTy = T->getAs<VectorType>())
4682 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson22334602010-02-05 00:12:22 +00004683 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattnerec3a1562009-10-17 20:33:28 +00004684
4685 // For enums, we return the unsigned version of the base type.
4686 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004687 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004688
4689 const BuiltinType *BTy = T->getAs<BuiltinType>();
4690 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004691 switch (BTy->getKind()) {
4692 case BuiltinType::Char_S:
4693 case BuiltinType::SChar:
4694 return UnsignedCharTy;
4695 case BuiltinType::Short:
4696 return UnsignedShortTy;
4697 case BuiltinType::Int:
4698 return UnsignedIntTy;
4699 case BuiltinType::Long:
4700 return UnsignedLongTy;
4701 case BuiltinType::LongLong:
4702 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004703 case BuiltinType::Int128:
4704 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004705 default:
4706 assert(0 && "Unexpected signed integer type");
4707 return QualType();
4708 }
4709}
4710
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004711ExternalASTSource::~ExternalASTSource() { }
4712
4713void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004714
4715
4716//===----------------------------------------------------------------------===//
4717// Builtin Type Computation
4718//===----------------------------------------------------------------------===//
4719
4720/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4721/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004722static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004723 ASTContext::GetBuiltinTypeError &Error,
4724 bool AllowTypeModifiers = true) {
4725 // Modifiers.
4726 int HowLong = 0;
4727 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004728
Chris Lattnerecd79c62009-06-14 00:45:47 +00004729 // Read the modifiers first.
4730 bool Done = false;
4731 while (!Done) {
4732 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004733 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004734 case 'S':
4735 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4736 assert(!Signed && "Can't use 'S' modifier multiple times!");
4737 Signed = true;
4738 break;
4739 case 'U':
4740 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4741 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4742 Unsigned = true;
4743 break;
4744 case 'L':
4745 assert(HowLong <= 2 && "Can't have LLLL modifier");
4746 ++HowLong;
4747 break;
4748 }
4749 }
4750
4751 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004752
Chris Lattnerecd79c62009-06-14 00:45:47 +00004753 // Read the base type.
4754 switch (*Str++) {
4755 default: assert(0 && "Unknown builtin type letter!");
4756 case 'v':
4757 assert(HowLong == 0 && !Signed && !Unsigned &&
4758 "Bad modifiers used with 'v'!");
4759 Type = Context.VoidTy;
4760 break;
4761 case 'f':
4762 assert(HowLong == 0 && !Signed && !Unsigned &&
4763 "Bad modifiers used with 'f'!");
4764 Type = Context.FloatTy;
4765 break;
4766 case 'd':
4767 assert(HowLong < 2 && !Signed && !Unsigned &&
4768 "Bad modifiers used with 'd'!");
4769 if (HowLong)
4770 Type = Context.LongDoubleTy;
4771 else
4772 Type = Context.DoubleTy;
4773 break;
4774 case 's':
4775 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4776 if (Unsigned)
4777 Type = Context.UnsignedShortTy;
4778 else
4779 Type = Context.ShortTy;
4780 break;
4781 case 'i':
4782 if (HowLong == 3)
4783 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4784 else if (HowLong == 2)
4785 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4786 else if (HowLong == 1)
4787 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4788 else
4789 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4790 break;
4791 case 'c':
4792 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4793 if (Signed)
4794 Type = Context.SignedCharTy;
4795 else if (Unsigned)
4796 Type = Context.UnsignedCharTy;
4797 else
4798 Type = Context.CharTy;
4799 break;
4800 case 'b': // boolean
4801 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4802 Type = Context.BoolTy;
4803 break;
4804 case 'z': // size_t.
4805 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4806 Type = Context.getSizeType();
4807 break;
4808 case 'F':
4809 Type = Context.getCFConstantStringType();
4810 break;
4811 case 'a':
4812 Type = Context.getBuiltinVaListType();
4813 assert(!Type.isNull() && "builtin va list type not initialized!");
4814 break;
4815 case 'A':
4816 // This is a "reference" to a va_list; however, what exactly
4817 // this means depends on how va_list is defined. There are two
4818 // different kinds of va_list: ones passed by value, and ones
4819 // passed by reference. An example of a by-value va_list is
4820 // x86, where va_list is a char*. An example of by-ref va_list
4821 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4822 // we want this argument to be a char*&; for x86-64, we want
4823 // it to be a __va_list_tag*.
4824 Type = Context.getBuiltinVaListType();
4825 assert(!Type.isNull() && "builtin va list type not initialized!");
4826 if (Type->isArrayType()) {
4827 Type = Context.getArrayDecayedType(Type);
4828 } else {
4829 Type = Context.getLValueReferenceType(Type);
4830 }
4831 break;
4832 case 'V': {
4833 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004834 unsigned NumElements = strtoul(Str, &End, 10);
4835 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004836
Chris Lattnerecd79c62009-06-14 00:45:47 +00004837 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004838
Chris Lattnerecd79c62009-06-14 00:45:47 +00004839 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson22334602010-02-05 00:12:22 +00004840 // FIXME: Don't know what to do about AltiVec.
4841 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004842 break;
4843 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004844 case 'X': {
4845 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4846 Type = Context.getComplexType(ElementType);
4847 break;
4848 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004849 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004850 Type = Context.getFILEType();
4851 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004852 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004853 return QualType();
4854 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004855 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004856 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004857 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004858 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004859 else
4860 Type = Context.getjmp_bufType();
4861
Mike Stump2adb4da2009-07-28 23:47:15 +00004862 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004863 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004864 return QualType();
4865 }
4866 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004867 }
Mike Stump11289f42009-09-09 15:08:12 +00004868
Chris Lattnerecd79c62009-06-14 00:45:47 +00004869 if (!AllowTypeModifiers)
4870 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004871
Chris Lattnerecd79c62009-06-14 00:45:47 +00004872 Done = false;
4873 while (!Done) {
John McCallb8b94662010-03-12 04:21:28 +00004874 switch (char c = *Str++) {
Chris Lattnerecd79c62009-06-14 00:45:47 +00004875 default: Done = true; --Str; break;
4876 case '*':
Chris Lattnerecd79c62009-06-14 00:45:47 +00004877 case '&':
John McCallb8b94662010-03-12 04:21:28 +00004878 {
4879 // Both pointers and references can have their pointee types
4880 // qualified with an address space.
4881 char *End;
4882 unsigned AddrSpace = strtoul(Str, &End, 10);
4883 if (End != Str && AddrSpace != 0) {
4884 Type = Context.getAddrSpaceQualType(Type, AddrSpace);
4885 Str = End;
4886 }
4887 }
4888 if (c == '*')
4889 Type = Context.getPointerType(Type);
4890 else
4891 Type = Context.getLValueReferenceType(Type);
Chris Lattnerecd79c62009-06-14 00:45:47 +00004892 break;
4893 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4894 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004895 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004896 break;
Fariborz Jahaniand59baba2010-01-26 22:48:42 +00004897 case 'D':
4898 Type = Context.getVolatileType(Type);
4899 break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004900 }
4901 }
Mike Stump11289f42009-09-09 15:08:12 +00004902
Chris Lattnerecd79c62009-06-14 00:45:47 +00004903 return Type;
4904}
4905
4906/// GetBuiltinType - Return the type for the specified builtin.
4907QualType ASTContext::GetBuiltinType(unsigned id,
4908 GetBuiltinTypeError &Error) {
4909 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004910
Chris Lattnerecd79c62009-06-14 00:45:47 +00004911 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004912
Chris Lattnerecd79c62009-06-14 00:45:47 +00004913 Error = GE_None;
4914 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4915 if (Error != GE_None)
4916 return QualType();
4917 while (TypeStr[0] && TypeStr[0] != '.') {
4918 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4919 if (Error != GE_None)
4920 return QualType();
4921
4922 // Do array -> pointer decay. The builtin should use the decayed type.
4923 if (Ty->isArrayType())
4924 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004925
Chris Lattnerecd79c62009-06-14 00:45:47 +00004926 ArgTypes.push_back(Ty);
4927 }
4928
4929 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4930 "'.' should only occur at end of builtin type list!");
4931
4932 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4933 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4934 return getFunctionNoProtoType(ResType);
Douglas Gregor36c569f2010-02-21 22:15:06 +00004935
4936 // FIXME: Should we create noreturn types?
Chris Lattnerecd79c62009-06-14 00:45:47 +00004937 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregor36c569f2010-02-21 22:15:06 +00004938 TypeStr[0] == '.', 0, false, false, 0, 0,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00004939 FunctionType::ExtInfo());
Chris Lattnerecd79c62009-06-14 00:45:47 +00004940}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004941
4942QualType
4943ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4944 // Perform the usual unary conversions. We do this early so that
4945 // integral promotions to "int" can allow us to exit early, in the
4946 // lhs == rhs check. Also, for conversion purposes, we ignore any
4947 // qualifiers. For example, "const float" and "float" are
4948 // equivalent.
4949 if (lhs->isPromotableIntegerType())
4950 lhs = getPromotedIntegerType(lhs);
4951 else
4952 lhs = lhs.getUnqualifiedType();
4953 if (rhs->isPromotableIntegerType())
4954 rhs = getPromotedIntegerType(rhs);
4955 else
4956 rhs = rhs.getUnqualifiedType();
4957
4958 // If both types are identical, no conversion is needed.
4959 if (lhs == rhs)
4960 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004961
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004962 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4963 // The caller can deal with this (e.g. pointer + int).
4964 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4965 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004966
4967 // At this point, we have two different arithmetic types.
4968
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004969 // Handle complex types first (C99 6.3.1.8p1).
4970 if (lhs->isComplexType() || rhs->isComplexType()) {
4971 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00004972 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004973 // convert the rhs to the lhs complex type.
4974 return lhs;
4975 }
Mike Stump11289f42009-09-09 15:08:12 +00004976 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004977 // convert the lhs to the rhs complex type.
4978 return rhs;
4979 }
4980 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00004981 // When both operands are complex, the shorter operand is converted to the
4982 // type of the longer, and that is the type of the result. This corresponds
4983 // to what is done when combining two real floating-point operands.
4984 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004985 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00004986 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004987 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00004988 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004989 // "double _Complex" is promoted to "long double _Complex".
4990 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004991
4992 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004993 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004994 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004995 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00004996 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004997 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4998 // domains match. This is a requirement for our implementation, C99
4999 // does not require this promotion.
5000 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
5001 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
5002 return rhs;
5003 } else { // handle "_Complex double, double".
5004 return lhs;
5005 }
5006 }
5007 return lhs; // The domain/size match exactly.
5008 }
5009 // Now handle "real" floating types (i.e. float, double, long double).
5010 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
5011 // if we have an integer operand, the result is the real floating type.
5012 if (rhs->isIntegerType()) {
5013 // convert rhs to the lhs floating point type.
5014 return lhs;
5015 }
5016 if (rhs->isComplexIntegerType()) {
5017 // convert rhs to the complex floating point type.
5018 return getComplexType(lhs);
5019 }
5020 if (lhs->isIntegerType()) {
5021 // convert lhs to the rhs floating point type.
5022 return rhs;
5023 }
Mike Stump11289f42009-09-09 15:08:12 +00005024 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005025 // convert lhs to the complex floating point type.
5026 return getComplexType(rhs);
5027 }
5028 // We have two real floating types, float/complex combos were handled above.
5029 // Convert the smaller operand to the bigger result.
5030 int result = getFloatingTypeOrder(lhs, rhs);
5031 if (result > 0) // convert the rhs
5032 return lhs;
5033 assert(result < 0 && "illegal float comparison");
5034 return rhs; // convert the lhs
5035 }
5036 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5037 // Handle GCC complex int extension.
5038 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5039 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5040
5041 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00005042 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00005043 rhsComplexInt->getElementType()) >= 0)
5044 return lhs; // convert the rhs
5045 return rhs;
5046 } else if (lhsComplexInt && rhs->isIntegerType()) {
5047 // convert the rhs to the lhs complex type.
5048 return lhs;
5049 } else if (rhsComplexInt && lhs->isIntegerType()) {
5050 // convert the lhs to the rhs complex type.
5051 return rhs;
5052 }
5053 }
5054 // Finally, we have two differing integer types.
5055 // The rules for this case are in C99 6.3.1.8
5056 int compare = getIntegerTypeOrder(lhs, rhs);
5057 bool lhsSigned = lhs->isSignedIntegerType(),
5058 rhsSigned = rhs->isSignedIntegerType();
5059 QualType destType;
5060 if (lhsSigned == rhsSigned) {
5061 // Same signedness; use the higher-ranked type
5062 destType = compare >= 0 ? lhs : rhs;
5063 } else if (compare != (lhsSigned ? 1 : -1)) {
5064 // The unsigned type has greater than or equal rank to the
5065 // signed type, so use the unsigned type
5066 destType = lhsSigned ? rhs : lhs;
5067 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5068 // The two types are different widths; if we are here, that
5069 // means the signed type is larger than the unsigned type, so
5070 // use the signed type.
5071 destType = lhsSigned ? lhs : rhs;
5072 } else {
5073 // The signed type is higher-ranked than the unsigned type,
5074 // but isn't actually any bigger (like unsigned int and long
5075 // on most 32-bit systems). Use the unsigned type corresponding
5076 // to the signed type.
5077 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5078 }
5079 return destType;
5080}