blob: 202e3370b67e5bf1cc8e2a13fc523ffdee29e8f7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Ken Dyckbdc601b2009-12-22 14:23:30 +000015#include "clang/AST/CharUnits.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/ExternalASTSource.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000024#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "clang/Basic/TargetInfo.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000026#include "llvm/ADT/SmallString.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000027#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000028#include "llvm/Support/MathExtras.h"
Benjamin Kramerf5942a42009-10-24 09:57:09 +000029#include "llvm/Support/raw_ostream.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000030#include "RecordLayoutBuilder.h"
31
Reid Spencer5f016e22007-07-11 17:01:13 +000032using namespace clang;
33
34enum FloatingRank {
35 FloatRank, DoubleRank, LongDoubleRank
36};
37
Chris Lattner61710852008-10-05 17:34:18 +000038ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
Daniel Dunbar444be732009-11-13 05:51:54 +000039 const TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000040 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +000041 Builtin::Context &builtins,
Mike Stump1eb44332009-09-09 15:08:12 +000042 bool FreeMem, unsigned size_reserve) :
43 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +000044 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump083c25e2009-10-22 00:49:09 +000045 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
46 SourceMgr(SM), LangOpts(LOpts),
Mike Stump1eb44332009-09-09 15:08:12 +000047 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +000048 Idents(idents), Selectors(sels),
Mike Stump1eb44332009-09-09 15:08:12 +000049 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall0f436562009-08-17 16:35:33 +000050 ObjCIdRedefinitionType = QualType();
51 ObjCClassRedefinitionType = QualType();
Fariborz Jahanian369a3bd2009-11-25 23:07:42 +000052 ObjCSelRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000053 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000054 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000055 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000056}
57
Reid Spencer5f016e22007-07-11 17:01:13 +000058ASTContext::~ASTContext() {
Ted Kremenek3478eb62010-02-11 07:12:28 +000059 // Release the DenseMaps associated with DeclContext objects.
60 // FIXME: Is this the ideal solution?
61 ReleaseDeclContextMaps();
62
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000063 if (FreeMemory) {
64 // Deallocate all the types.
65 while (!Types.empty()) {
66 Types.back()->Destroy(*this);
67 Types.pop_back();
68 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000069
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000070 for (llvm::FoldingSet<ExtQuals>::iterator
71 I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
72 // Increment in loop to prevent using deallocated memory.
John McCall0953e762009-09-24 19:53:00 +000073 Deallocate(&*I++);
Nuno Lopesb74668e2008-12-17 22:30:25 +000074 }
75 }
76
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000077 for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
78 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
79 // Increment in loop to prevent using deallocated memory.
80 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
81 delete R;
82 }
83
84 for (llvm::DenseMap<const ObjCContainerDecl*,
85 const ASTRecordLayout*>::iterator
86 I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
87 // Increment in loop to prevent using deallocated memory.
88 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
89 delete R;
Nuno Lopesb74668e2008-12-17 22:30:25 +000090 }
91
Douglas Gregorab452ba2009-03-26 23:50:42 +000092 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000093 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
94 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000095 NNSEnd = NestedNameSpecifiers.end();
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000096 NNS != NNSEnd; ) {
97 // Increment in loop to prevent using deallocated memory.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000098 (*NNS++).Destroy(*this);
Ted Kremenekbbfd68d2009-12-23 21:13:52 +000099 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000100
101 if (GlobalNestedNameSpecifier)
102 GlobalNestedNameSpecifier->Destroy(*this);
103
Eli Friedmanb26153c2008-05-27 03:08:09 +0000104 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000105}
106
Mike Stump1eb44332009-09-09 15:08:12 +0000107void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
109 ExternalSource.reset(Source.take());
110}
111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112void ASTContext::PrintStats() const {
113 fprintf(stderr, "*** AST Context Stats:\n");
114 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000115
Douglas Gregordbe833d2009-05-26 14:40:08 +0000116 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000117#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000118#define ABSTRACT_TYPE(Name, Parent)
119#include "clang/AST/TypeNodes.def"
120 0 // Extra
121 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000122
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
124 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000125 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 }
127
Douglas Gregordbe833d2009-05-26 14:40:08 +0000128 unsigned Idx = 0;
129 unsigned TotalBytes = 0;
130#define TYPE(Name, Parent) \
131 if (counts[Idx]) \
132 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
133 TotalBytes += counts[Idx] * sizeof(Name##Type); \
134 ++Idx;
135#define ABSTRACT_TYPE(Name, Parent)
136#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Douglas Gregordbe833d2009-05-26 14:40:08 +0000138 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139
140 if (ExternalSource.get()) {
141 fprintf(stderr, "\n");
142 ExternalSource->PrintStats();
143 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000144}
145
146
John McCalle27ec8a2009-10-23 23:03:21 +0000147void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000148 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000149 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000150 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000151}
152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153void ASTContext::InitBuiltinTypes() {
154 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 // C99 6.2.5p19.
157 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // C99 6.2.5p2.
160 InitBuiltinType(BoolTy, BuiltinType::Bool);
161 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000162 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 InitBuiltinType(CharTy, BuiltinType::Char_S);
164 else
165 InitBuiltinType(CharTy, BuiltinType::Char_U);
166 // C99 6.2.5p4.
167 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
168 InitBuiltinType(ShortTy, BuiltinType::Short);
169 InitBuiltinType(IntTy, BuiltinType::Int);
170 InitBuiltinType(LongTy, BuiltinType::Long);
171 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 // C99 6.2.5p6.
174 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
175 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
176 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
177 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
178 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 // C99 6.2.5p10.
181 InitBuiltinType(FloatTy, BuiltinType::Float);
182 InitBuiltinType(DoubleTy, BuiltinType::Double);
183 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000184
Chris Lattner2df9ced2009-04-30 02:43:43 +0000185 // GNU extension, 128-bit integers.
186 InitBuiltinType(Int128Ty, BuiltinType::Int128);
187 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
188
Chris Lattner3a250322009-02-26 23:43:47 +0000189 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
190 InitBuiltinType(WCharTy, BuiltinType::WChar);
191 else // C99
192 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000193
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000194 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
195 InitBuiltinType(Char16Ty, BuiltinType::Char16);
196 else // C99
197 Char16Ty = getFromTargetType(Target.getChar16Type());
198
199 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
200 InitBuiltinType(Char32Ty, BuiltinType::Char32);
201 else // C99
202 Char32Ty = getFromTargetType(Target.getChar32Type());
203
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000204 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000205 InitBuiltinType(OverloadTy, BuiltinType::Overload);
206
207 // Placeholder type for type-dependent expressions whose type is
208 // completely unknown. No code should ever check a type against
209 // DependentTy and users should never see it; however, it is here to
210 // help diagnose failures to properly check for type-dependent
211 // expressions.
212 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000213
Mike Stump1eb44332009-09-09 15:08:12 +0000214 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000215 // not yet been deduced.
216 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 // C99 6.2.5p11.
219 FloatComplexTy = getComplexType(FloatTy);
220 DoubleComplexTy = getComplexType(DoubleTy);
221 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000222
Steve Naroff7e219e42007-10-15 14:41:52 +0000223 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Steve Naroffde2e22d2009-07-15 18:40:39 +0000225 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
226 ObjCIdTypedefType = QualType();
227 ObjCClassTypedefType = QualType();
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000228 ObjCSelTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000230 // Builtin types for 'id', 'Class', and 'SEL'.
Steve Naroffde2e22d2009-07-15 18:40:39 +0000231 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
232 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Fariborz Jahanian13dcd002009-11-21 19:53:08 +0000233 InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
Steve Naroff14108da2009-07-10 23:34:53 +0000234
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000235 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000237 // void * type
238 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000239
240 // nullptr type (C++0x 2.14.7)
241 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000242}
243
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000244MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000245ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000246 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000247 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000248 = InstantiatedFromStaticDataMember.find(Var);
249 if (Pos == InstantiatedFromStaticDataMember.end())
250 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Douglas Gregor7caa6822009-07-24 20:34:43 +0000252 return Pos->second;
253}
254
Mike Stump1eb44332009-09-09 15:08:12 +0000255void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000256ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
257 TemplateSpecializationKind TSK) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000258 assert(Inst->isStaticDataMember() && "Not a static data member");
259 assert(Tmpl->isStaticDataMember() && "Not a static data member");
260 assert(!InstantiatedFromStaticDataMember[Inst] &&
261 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000262 InstantiatedFromStaticDataMember[Inst]
263 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000264}
265
John McCall7ba107a2009-11-18 02:36:19 +0000266NamedDecl *
John McCalled976492009-12-04 22:46:56 +0000267ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
John McCall7ba107a2009-11-18 02:36:19 +0000268 llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
John McCalled976492009-12-04 22:46:56 +0000269 = InstantiatedFromUsingDecl.find(UUD);
270 if (Pos == InstantiatedFromUsingDecl.end())
Anders Carlsson0d8df782009-08-29 19:37:28 +0000271 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlsson0d8df782009-08-29 19:37:28 +0000273 return Pos->second;
274}
275
276void
John McCalled976492009-12-04 22:46:56 +0000277ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
278 assert((isa<UsingDecl>(Pattern) ||
279 isa<UnresolvedUsingValueDecl>(Pattern) ||
280 isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
281 "pattern decl is not a using decl");
282 assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
283 InstantiatedFromUsingDecl[Inst] = Pattern;
284}
285
286UsingShadowDecl *
287ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
288 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
289 = InstantiatedFromUsingShadowDecl.find(Inst);
290 if (Pos == InstantiatedFromUsingShadowDecl.end())
291 return 0;
292
293 return Pos->second;
294}
295
296void
297ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
298 UsingShadowDecl *Pattern) {
299 assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
300 InstantiatedFromUsingShadowDecl[Inst] = Pattern;
Anders Carlsson0d8df782009-08-29 19:37:28 +0000301}
302
Anders Carlssond8b285f2009-09-01 04:26:58 +0000303FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
304 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
305 = InstantiatedFromUnnamedFieldDecl.find(Field);
306 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
307 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Anders Carlssond8b285f2009-09-01 04:26:58 +0000309 return Pos->second;
310}
311
312void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
313 FieldDecl *Tmpl) {
314 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
315 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
316 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
317 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Anders Carlssond8b285f2009-09-01 04:26:58 +0000319 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
320}
321
Douglas Gregor2e222532009-07-02 17:08:52 +0000322namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000323 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000324 : std::binary_function<SourceRange, SourceRange, bool> {
325 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Douglas Gregor2e222532009-07-02 17:08:52 +0000327 public:
328 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Douglas Gregor2e222532009-07-02 17:08:52 +0000330 bool operator()(SourceRange X, SourceRange Y) {
331 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
332 }
333 };
334}
335
336/// \brief Determine whether the given comment is a Doxygen-style comment.
337///
338/// \param Start the start of the comment text.
339///
340/// \param End the end of the comment text.
341///
342/// \param Member whether we want to check whether this is a member comment
343/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
344/// we only return true when we find a non-member comment.
Mike Stump1eb44332009-09-09 15:08:12 +0000345static bool
346isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregor2e222532009-07-02 17:08:52 +0000347 bool Member = false) {
Mike Stump1eb44332009-09-09 15:08:12 +0000348 const char *BufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000349 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
350 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
351 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Douglas Gregor2e222532009-07-02 17:08:52 +0000353 if (End - Start < 4)
354 return false;
355
356 assert(Start[0] == '/' && "Not a comment?");
357 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
358 return false;
359 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
360 return false;
361
362 return (Start[3] == '<') == Member;
363}
364
365/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump1eb44332009-09-09 15:08:12 +0000366/// it has one.
Douglas Gregor2e222532009-07-02 17:08:52 +0000367const char *ASTContext::getCommentForDecl(const Decl *D) {
368 if (!D)
369 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Douglas Gregor2e222532009-07-02 17:08:52 +0000371 // Check whether we have cached a comment string for this declaration
372 // already.
Mike Stump1eb44332009-09-09 15:08:12 +0000373 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregor2e222532009-07-02 17:08:52 +0000374 = DeclComments.find(D);
375 if (Pos != DeclComments.end())
376 return Pos->second.c_str();
377
Mike Stump1eb44332009-09-09 15:08:12 +0000378 // If we have an external AST source and have not yet loaded comments from
Douglas Gregor2e222532009-07-02 17:08:52 +0000379 // that source, do so now.
380 if (ExternalSource && !LoadedExternalComments) {
381 std::vector<SourceRange> LoadedComments;
382 ExternalSource->ReadComments(LoadedComments);
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Douglas Gregor2e222532009-07-02 17:08:52 +0000384 if (!LoadedComments.empty())
385 Comments.insert(Comments.begin(), LoadedComments.begin(),
386 LoadedComments.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Douglas Gregor2e222532009-07-02 17:08:52 +0000388 LoadedExternalComments = true;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
391 // If there are no comments anywhere, we won't find anything.
Douglas Gregor2e222532009-07-02 17:08:52 +0000392 if (Comments.empty())
393 return 0;
394
395 // If the declaration doesn't map directly to a location in a file, we
396 // can't find the comment.
397 SourceLocation DeclStartLoc = D->getLocStart();
398 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
399 return 0;
400
401 // Find the comment that occurs just before this declaration.
402 std::vector<SourceRange>::iterator LastComment
Mike Stump1eb44332009-09-09 15:08:12 +0000403 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregor2e222532009-07-02 17:08:52 +0000404 SourceRange(DeclStartLoc),
405 BeforeInTranslationUnit(&SourceMgr));
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor2e222532009-07-02 17:08:52 +0000407 // Decompose the location for the start of the declaration and find the
408 // beginning of the file buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000409 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregor2e222532009-07-02 17:08:52 +0000410 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000411 const char *FileBufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000412 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Douglas Gregor2e222532009-07-02 17:08:52 +0000414 // First check whether we have a comment for a member.
415 if (LastComment != Comments.end() &&
416 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
417 isDoxygenComment(SourceMgr, *LastComment, true)) {
418 std::pair<FileID, unsigned> LastCommentEndDecomp
419 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
420 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
421 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump1eb44332009-09-09 15:08:12 +0000422 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000423 LastCommentEndDecomp.second)) {
424 // The Doxygen member comment comes after the declaration starts and
425 // is on the same line and in the same file as the declaration. This
426 // is the comment we want.
427 std::string &Result = DeclComments[D];
Mike Stump1eb44332009-09-09 15:08:12 +0000428 Result.append(FileBufferStart +
429 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000430 FileBufferStart + LastCommentEndDecomp.second + 1);
431 return Result.c_str();
432 }
433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Douglas Gregor2e222532009-07-02 17:08:52 +0000435 if (LastComment == Comments.begin())
436 return 0;
437 --LastComment;
438
439 // Decompose the end of the comment.
440 std::pair<FileID, unsigned> LastCommentEndDecomp
441 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Douglas Gregor2e222532009-07-02 17:08:52 +0000443 // If the comment and the declaration aren't in the same file, then they
444 // aren't related.
445 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
446 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Douglas Gregor2e222532009-07-02 17:08:52 +0000448 // Check that we actually have a Doxygen comment.
449 if (!isDoxygenComment(SourceMgr, *LastComment))
450 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Douglas Gregor2e222532009-07-02 17:08:52 +0000452 // Compute the starting line for the declaration and for the end of the
453 // comment (this is expensive).
Mike Stump1eb44332009-09-09 15:08:12 +0000454 unsigned DeclStartLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000455 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
456 unsigned CommentEndLine
Mike Stump1eb44332009-09-09 15:08:12 +0000457 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000458 LastCommentEndDecomp.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Douglas Gregor2e222532009-07-02 17:08:52 +0000460 // If the comment does not end on the line prior to the declaration, then
461 // the comment is not associated with the declaration at all.
462 if (CommentEndLine + 1 != DeclStartLine)
463 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Douglas Gregor2e222532009-07-02 17:08:52 +0000465 // We have a comment, but there may be more comments on the previous lines.
466 // Keep looking so long as the comments are still Doxygen comments and are
467 // still adjacent.
Mike Stump1eb44332009-09-09 15:08:12 +0000468 unsigned ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000469 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
470 std::vector<SourceRange>::iterator FirstComment = LastComment;
471 while (FirstComment != Comments.begin()) {
472 // Look at the previous comment
473 --FirstComment;
474 std::pair<FileID, unsigned> Decomp
475 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Douglas Gregor2e222532009-07-02 17:08:52 +0000477 // If this previous comment is in a different file, we're done.
478 if (Decomp.first != DeclStartDecomp.first) {
479 ++FirstComment;
480 break;
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregor2e222532009-07-02 17:08:52 +0000483 // If this comment is not a Doxygen comment, we're done.
484 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
485 ++FirstComment;
486 break;
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregor2e222532009-07-02 17:08:52 +0000489 // If the line number is not what we expected, we're done.
490 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
491 if (Line != ExpectedLine) {
492 ++FirstComment;
493 break;
494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregor2e222532009-07-02 17:08:52 +0000496 // Set the next expected line number.
Mike Stump1eb44332009-09-09 15:08:12 +0000497 ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000498 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregor2e222532009-07-02 17:08:52 +0000501 // The iterator range [FirstComment, LastComment] contains all of the
502 // BCPL comments that, together, are associated with this declaration.
503 // Form a single comment block string for this declaration that concatenates
504 // all of these comments.
505 std::string &Result = DeclComments[D];
506 while (FirstComment != LastComment) {
507 std::pair<FileID, unsigned> DecompStart
508 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
509 std::pair<FileID, unsigned> DecompEnd
510 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
511 Result.append(FileBufferStart + DecompStart.second,
512 FileBufferStart + DecompEnd.second + 1);
513 ++FirstComment;
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Douglas Gregor2e222532009-07-02 17:08:52 +0000516 // Append the last comment line.
Mike Stump1eb44332009-09-09 15:08:12 +0000517 Result.append(FileBufferStart +
518 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000519 FileBufferStart + LastCommentEndDecomp.second + 1);
520 return Result.c_str();
521}
522
Chris Lattner464175b2007-07-18 17:52:12 +0000523//===----------------------------------------------------------------------===//
524// Type Sizing and Analysis
525//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000526
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000527/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
528/// scalar floating point type.
529const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000530 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000531 assert(BT && "Not a floating point type!");
532 switch (BT->getKind()) {
533 default: assert(0 && "Not a floating point type!");
534 case BuiltinType::Float: return Target.getFloatFormat();
535 case BuiltinType::Double: return Target.getDoubleFormat();
536 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
537 }
538}
539
Ken Dyck8b752f12010-01-27 17:10:57 +0000540/// getDeclAlign - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000541/// specified decl. Note that bitfields do not have a valid alignment, so
542/// this method will assert on them.
Sebastian Redl5d484e82009-11-23 17:18:46 +0000543/// If @p RefAsPointee, references are treated like their underlying type
544/// (for alignof), else they're treated like pointers (for CodeGen).
Ken Dyck8b752f12010-01-27 17:10:57 +0000545CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000546 unsigned Align = Target.getCharWidth();
547
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000548 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Sean Huntbbd37c62009-11-21 08:43:09 +0000549 Align = std::max(Align, AA->getMaxAlignment());
Eli Friedmandcdafb62009-02-22 02:56:25 +0000550
Chris Lattneraf707ab2009-01-24 21:53:27 +0000551 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
552 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000553 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Sebastian Redl5d484e82009-11-23 17:18:46 +0000554 if (RefAsPointee)
555 T = RT->getPointeeType();
556 else
557 T = getPointerType(RT->getPointeeType());
558 }
559 if (!T->isIncompleteType() && !T->isFunctionType()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000560 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000561 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
562 T = cast<ArrayType>(T)->getElementType();
563
564 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
565 }
Charles Davis05f62472010-02-23 04:52:00 +0000566 if (const FieldDecl *FD = dyn_cast<FieldDecl>(VD)) {
567 // In the case of a field in a packed struct, we want the minimum
568 // of the alignment of the field and the alignment of the struct.
569 Align = std::min(Align,
570 getPreferredTypeAlign(FD->getParent()->getTypeForDecl()));
571 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000572 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000573
Ken Dyck8b752f12010-01-27 17:10:57 +0000574 return CharUnits::fromQuantity(Align / Target.getCharWidth());
Chris Lattneraf707ab2009-01-24 21:53:27 +0000575}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000576
Chris Lattnera7674d82007-07-13 22:13:22 +0000577/// getTypeSize - Return the size of the specified type, in bits. This method
578/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000579///
580/// FIXME: Pointers into different addr spaces could have different sizes and
581/// alignment requirements: getPointerInfo should take an AddrSpace, this
582/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000583std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000584ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000585 uint64_t Width=0;
586 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000587 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000588#define TYPE(Class, Base)
589#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000590#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000591#define DEPENDENT_TYPE(Class, Base) case Type::Class:
592#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000593 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000594 break;
595
Chris Lattner692233e2007-07-13 22:27:08 +0000596 case Type::FunctionNoProto:
597 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000598 // GCC extension: alignof(function) = 32 bits
599 Width = 0;
600 Align = 32;
601 break;
602
Douglas Gregor72564e72009-02-26 23:50:07 +0000603 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000604 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000605 Width = 0;
606 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
607 break;
608
Steve Narofffb22d962007-08-30 01:06:46 +0000609 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000610 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Chris Lattner98be4942008-03-05 18:54:05 +0000612 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000613 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000614 Align = EltInfo.second;
615 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000616 }
Nate Begeman213541a2008-04-18 23:10:10 +0000617 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000618 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000619 const VectorType *VT = cast<VectorType>(T);
620 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
621 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000622 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000623 // If the alignment is not a power of 2, round up to the next power of 2.
624 // This happens for non-power-of-2 length vectors.
Chris Lattner9fcfe922009-10-22 05:17:15 +0000625 if (VT->getNumElements() & (VT->getNumElements()-1)) {
626 Align = llvm::NextPowerOf2(Align);
627 Width = llvm::RoundUpToAlignment(Width, Align);
628 }
Chris Lattner030d8842007-07-19 22:06:24 +0000629 break;
630 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000631
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000632 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000633 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000634 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000635 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000636 // GCC extension: alignof(void) = 8 bits.
637 Width = 0;
638 Align = 8;
639 break;
640
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000641 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000642 Width = Target.getBoolWidth();
643 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000644 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000645 case BuiltinType::Char_S:
646 case BuiltinType::Char_U:
647 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000648 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000649 Width = Target.getCharWidth();
650 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000651 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000652 case BuiltinType::WChar:
653 Width = Target.getWCharWidth();
654 Align = Target.getWCharAlign();
655 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000656 case BuiltinType::Char16:
657 Width = Target.getChar16Width();
658 Align = Target.getChar16Align();
659 break;
660 case BuiltinType::Char32:
661 Width = Target.getChar32Width();
662 Align = Target.getChar32Align();
663 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000664 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000665 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000666 Width = Target.getShortWidth();
667 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000668 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000669 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000670 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000671 Width = Target.getIntWidth();
672 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000673 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000674 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000675 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000676 Width = Target.getLongWidth();
677 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000678 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000679 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000680 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000681 Width = Target.getLongLongWidth();
682 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000683 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000684 case BuiltinType::Int128:
685 case BuiltinType::UInt128:
686 Width = 128;
687 Align = 128; // int128_t is 128-bit aligned on all targets.
688 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000689 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000690 Width = Target.getFloatWidth();
691 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000692 break;
693 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000694 Width = Target.getDoubleWidth();
695 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000696 break;
697 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000698 Width = Target.getLongDoubleWidth();
699 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000700 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000701 case BuiltinType::NullPtr:
702 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
703 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000704 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000705 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000706 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000707 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000708 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000709 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000710 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000711 case Type::BlockPointer: {
712 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
713 Width = Target.getPointerWidth(AS);
714 Align = Target.getPointerAlign(AS);
715 break;
716 }
Sebastian Redl5d484e82009-11-23 17:18:46 +0000717 case Type::LValueReference:
718 case Type::RValueReference: {
719 // alignof and sizeof should never enter this code path here, so we go
720 // the pointer route.
721 unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace();
722 Width = Target.getPointerWidth(AS);
723 Align = Target.getPointerAlign(AS);
724 break;
725 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000726 case Type::Pointer: {
727 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000728 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000729 Align = Target.getPointerAlign(AS);
730 break;
731 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000732 case Type::MemberPointer: {
Sebastian Redlf30208a2009-01-24 21:16:55 +0000733 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000734 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000735 getTypeInfo(getPointerDiffType());
736 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000737 if (Pointee->isFunctionType())
738 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000739 Align = PtrDiffInfo.second;
740 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000741 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000742 case Type::Complex: {
743 // Complex types have the same alignment as their elements, but twice the
744 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000745 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000746 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000747 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000748 Align = EltInfo.second;
749 break;
750 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000751 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000752 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000753 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
754 Width = Layout.getSize();
755 Align = Layout.getAlignment();
756 break;
757 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000758 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000759 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000760 const TagType *TT = cast<TagType>(T);
761
762 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000763 Width = 1;
764 Align = 1;
765 break;
766 }
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Daniel Dunbar1d751182008-11-08 05:48:37 +0000768 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000769 return getTypeInfo(ET->getDecl()->getIntegerType());
770
Daniel Dunbar1d751182008-11-08 05:48:37 +0000771 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000772 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
773 Width = Layout.getSize();
774 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000775 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000776 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000777
Chris Lattner9fcfe922009-10-22 05:17:15 +0000778 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000779 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
780 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000781
Chris Lattner9fcfe922009-10-22 05:17:15 +0000782 case Type::Elaborated:
783 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
784 .getTypePtr());
John McCall7da24312009-09-05 00:15:47 +0000785
Douglas Gregor18857642009-04-30 17:32:17 +0000786 case Type::Typedef: {
787 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000788 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000789 Align = std::max(Aligned->getMaxAlignment(),
790 getTypeAlign(Typedef->getUnderlyingType().getTypePtr()));
Douglas Gregor18857642009-04-30 17:32:17 +0000791 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
792 } else
793 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000794 break;
Chris Lattner71763312008-04-06 22:05:18 +0000795 }
Douglas Gregor18857642009-04-30 17:32:17 +0000796
797 case Type::TypeOfExpr:
798 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
799 .getTypePtr());
800
801 case Type::TypeOf:
802 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
803
Anders Carlsson395b4752009-06-24 19:06:50 +0000804 case Type::Decltype:
805 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
806 .getTypePtr());
807
Douglas Gregor18857642009-04-30 17:32:17 +0000808 case Type::QualifiedName:
809 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Douglas Gregor18857642009-04-30 17:32:17 +0000811 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000812 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000813 "Cannot request the size of a dependent type");
814 // FIXME: this is likely to be wrong once we support template
815 // aliases, since a template alias could refer to a typedef that
816 // has an __aligned__ attribute on it.
817 return getTypeInfo(getCanonicalType(T));
818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Chris Lattner464175b2007-07-18 17:52:12 +0000820 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000821 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000822}
823
Ken Dyckbdc601b2009-12-22 14:23:30 +0000824/// getTypeSizeInChars - Return the size of the specified type, in characters.
825/// This method does not work on incomplete types.
826CharUnits ASTContext::getTypeSizeInChars(QualType T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000827 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000828}
829CharUnits ASTContext::getTypeSizeInChars(const Type *T) {
Ken Dyck199c3d62010-01-11 17:06:35 +0000830 return CharUnits::fromQuantity(getTypeSize(T) / getCharWidth());
Ken Dyckbdc601b2009-12-22 14:23:30 +0000831}
832
Ken Dyck16e20cc2010-01-26 17:25:18 +0000833/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
Ken Dyck86fa4312010-01-26 17:22:55 +0000834/// characters. This method does not work on incomplete types.
835CharUnits ASTContext::getTypeAlignInChars(QualType T) {
836 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
837}
838CharUnits ASTContext::getTypeAlignInChars(const Type *T) {
839 return CharUnits::fromQuantity(getTypeAlign(T) / getCharWidth());
840}
841
Chris Lattner34ebde42009-01-27 18:08:34 +0000842/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
843/// type for the current target in bits. This can be different than the ABI
844/// alignment in cases where it is beneficial for performance to overalign
845/// a data type.
846unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
847 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000848
849 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000850 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000851 T = CT->getElementType().getTypePtr();
852 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
853 T->isSpecificBuiltinType(BuiltinType::LongLong))
854 return std::max(ABIAlign, (unsigned)getTypeSize(T));
855
Chris Lattner34ebde42009-01-27 18:08:34 +0000856 return ABIAlign;
857}
858
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000859static void CollectLocalObjCIvars(ASTContext *Ctx,
860 const ObjCInterfaceDecl *OI,
861 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000862 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
863 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000864 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000865 if (!IVDecl->isInvalidDecl())
866 Fields.push_back(cast<FieldDecl>(IVDecl));
867 }
868}
869
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000870void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
871 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
872 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
873 CollectObjCIvars(SuperClass, Fields);
874 CollectLocalObjCIvars(this, OI, Fields);
875}
876
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000877/// ShallowCollectObjCIvars -
878/// Collect all ivars, including those synthesized, in the current class.
879///
880void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000881 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000882 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
883 E = OI->ivar_end(); I != E; ++I) {
884 Ivars.push_back(*I);
885 }
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000886
887 CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000888}
889
Fariborz Jahanian98200742009-05-12 18:14:29 +0000890void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
891 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000892 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
893 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000894 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
895 Ivars.push_back(Ivar);
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Fariborz Jahanian98200742009-05-12 18:14:29 +0000897 // Also look into nested protocols.
898 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
899 E = PD->protocol_end(); P != E; ++P)
900 CollectProtocolSynthesizedIvars(*P, Ivars);
901}
902
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000903/// CollectNonClassIvars -
904/// This routine collects all other ivars which are not declared in the class.
905/// This includes synthesized ivars and those in class's implementation.
Fariborz Jahanian98200742009-05-12 18:14:29 +0000906///
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000907void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
Fariborz Jahanian98200742009-05-12 18:14:29 +0000908 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian0e5ad252010-02-23 01:26:30 +0000909 // Find ivars declared in class extension.
910 if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
911 for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
912 E = CDecl->ivar_end(); I != E; ++I) {
913 Ivars.push_back(*I);
914 }
915 }
916
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000917 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
918 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000919 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
920 Ivars.push_back(Ivar);
921 }
922 // Also look into interface's protocol list for properties declared
923 // in the protocol and whose ivars are synthesized.
924 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
925 PE = OI->protocol_end(); P != PE; ++P) {
926 ObjCProtocolDecl *PD = (*P);
927 CollectProtocolSynthesizedIvars(PD, Ivars);
928 }
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000929
930 // Also add any ivar defined in this class's implementation
931 if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) {
932 for (ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
933 E = ImplDecl->ivar_end(); I != E; ++I)
934 Ivars.push_back(*I);
935 }
Fariborz Jahanian98200742009-05-12 18:14:29 +0000936}
937
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000938/// CollectInheritedProtocols - Collect all protocols in current class and
939/// those inherited by it.
940void ASTContext::CollectInheritedProtocols(const Decl *CDecl,
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000941 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000942 if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
943 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
944 PE = OI->protocol_end(); P != PE; ++P) {
945 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000946 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000947 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
948 PE = Proto->protocol_end(); P != PE; ++P)
949 CollectInheritedProtocols(*P, Protocols);
950 }
951
952 // Categories of this Interface.
953 for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
954 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
955 CollectInheritedProtocols(CDeclChain, Protocols);
956 if (ObjCInterfaceDecl *SD = OI->getSuperClass())
957 while (SD) {
958 CollectInheritedProtocols(SD, Protocols);
959 SD = SD->getSuperClass();
960 }
961 return;
962 }
963 if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
964 for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(),
965 PE = OC->protocol_end(); P != PE; ++P) {
966 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000967 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000968 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
969 PE = Proto->protocol_end(); P != PE; ++P)
970 CollectInheritedProtocols(*P, Protocols);
971 }
972 return;
973 }
974 if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
975 for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
976 PE = OP->protocol_end(); P != PE; ++P) {
977 ObjCProtocolDecl *Proto = (*P);
Fariborz Jahanian432a8892010-02-12 19:27:33 +0000978 Protocols.insert(Proto);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +0000979 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
980 PE = Proto->protocol_end(); P != PE; ++P)
981 CollectInheritedProtocols(*P, Protocols);
982 }
983 return;
984 }
985}
986
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000987unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
988 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000989 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
990 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000991 if ((*I)->getPropertyIvarDecl())
992 ++count;
993
994 // Also look into nested protocols.
995 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
996 E = PD->protocol_end(); P != E; ++P)
997 count += CountProtocolSynthesizedIvars(*P);
998 return count;
999}
1000
Mike Stump1eb44332009-09-09 15:08:12 +00001001unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001002 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001003 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
1004 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001005 if ((*I)->getPropertyIvarDecl())
1006 ++count;
1007 }
1008 // Also look into interface's protocol list for properties declared
1009 // in the protocol and whose ivars are synthesized.
1010 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
1011 PE = OI->protocol_end(); P != PE; ++P) {
1012 ObjCProtocolDecl *PD = (*P);
1013 count += CountProtocolSynthesizedIvars(PD);
1014 }
1015 return count;
1016}
1017
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001018/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
1019ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
1020 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1021 I = ObjCImpls.find(D);
1022 if (I != ObjCImpls.end())
1023 return cast<ObjCImplementationDecl>(I->second);
1024 return 0;
1025}
1026/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1027ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
1028 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
1029 I = ObjCImpls.find(D);
1030 if (I != ObjCImpls.end())
1031 return cast<ObjCCategoryImplDecl>(I->second);
1032 return 0;
1033}
1034
1035/// \brief Set the implementation of ObjCInterfaceDecl.
1036void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1037 ObjCImplementationDecl *ImplD) {
1038 assert(IFaceD && ImplD && "Passed null params");
1039 ObjCImpls[IFaceD] = ImplD;
1040}
1041/// \brief Set the implementation of ObjCCategoryDecl.
1042void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
1043 ObjCCategoryImplDecl *ImplD) {
1044 assert(CatD && ImplD && "Passed null params");
1045 ObjCImpls[CatD] = ImplD;
1046}
1047
John McCalla93c9342009-12-07 02:54:59 +00001048/// \brief Allocate an uninitialized TypeSourceInfo.
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001049///
John McCalla93c9342009-12-07 02:54:59 +00001050/// The caller should initialize the memory held by TypeSourceInfo using
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001051/// the TypeLoc wrappers.
1052///
1053/// \param T the type that will be the basis for type source info. This type
1054/// should refer to how the declarator was written in source code, not to
1055/// what type semantic analysis resolved the declarator to.
John McCalla93c9342009-12-07 02:54:59 +00001056TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
John McCall109de5e2009-10-21 00:23:54 +00001057 unsigned DataSize) {
1058 if (!DataSize)
1059 DataSize = TypeLoc::getFullDataSizeForType(T);
1060 else
1061 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
John McCalla93c9342009-12-07 02:54:59 +00001062 "incorrect data size provided to CreateTypeSourceInfo!");
John McCall109de5e2009-10-21 00:23:54 +00001063
John McCalla93c9342009-12-07 02:54:59 +00001064 TypeSourceInfo *TInfo =
1065 (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1066 new (TInfo) TypeSourceInfo(T);
1067 return TInfo;
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +00001068}
1069
John McCalla93c9342009-12-07 02:54:59 +00001070TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
John McCalla4eb74d2009-10-23 21:14:09 +00001071 SourceLocation L) {
John McCalla93c9342009-12-07 02:54:59 +00001072 TypeSourceInfo *DI = CreateTypeSourceInfo(T);
John McCalla4eb74d2009-10-23 21:14:09 +00001073 DI->getTypeLoc().initialize(L);
1074 return DI;
1075}
1076
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001077/// getInterfaceLayoutImpl - Get or compute information about the
1078/// layout of the given interface.
1079///
1080/// \param Impl - If given, also include the layout of the interface's
1081/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +00001082const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001083ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
1084 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +00001085 assert(!D->isForwardDecl() && "Invalid interface decl!");
1086
Devang Patel44a3dde2008-06-04 21:54:36 +00001087 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +00001088 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +00001089 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
1090 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
1091 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +00001092
Daniel Dunbar453addb2009-05-03 11:16:44 +00001093 // Add in synthesized ivar count if laying out an implementation.
1094 if (Impl) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001095 unsigned SynthCount = CountSynthesizedIvars(D);
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +00001096 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +00001097 // entry. Note we can't cache this because we simply free all
1098 // entries later; however we shouldn't look up implementations
1099 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001100 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +00001101 return getObjCLayout(D, 0);
1102 }
1103
Mike Stump1eb44332009-09-09 15:08:12 +00001104 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001105 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
1106 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Devang Patel44a3dde2008-06-04 21:54:36 +00001108 return *NewEntry;
1109}
1110
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001111const ASTRecordLayout &
1112ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1113 return getObjCLayout(D, 0);
1114}
1115
1116const ASTRecordLayout &
1117ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1118 return getObjCLayout(D->getClassInterface(), D);
1119}
1120
Devang Patel88a981b2007-11-01 19:11:01 +00001121/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +00001122/// specified record (struct/union/class), which indicates its size and field
1123/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +00001124const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Douglas Gregor952b0172010-02-11 01:04:33 +00001125 D = D->getDefinition();
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001126 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +00001127
Chris Lattner464175b2007-07-18 17:52:12 +00001128 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +00001129 // Note that we can't save a reference to the entry because this function
1130 // is recursive.
1131 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +00001132 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001133
Mike Stump1eb44332009-09-09 15:08:12 +00001134 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001135 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +00001136 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Chris Lattner5d2a6302007-07-18 18:26:58 +00001138 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001139}
1140
Anders Carlssonf53df232009-12-07 04:35:11 +00001141const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) {
Douglas Gregor952b0172010-02-11 01:04:33 +00001142 RD = cast<CXXRecordDecl>(RD->getDefinition());
Anders Carlssonf53df232009-12-07 04:35:11 +00001143 assert(RD && "Cannot get key function for forward declarations!");
1144
1145 const CXXMethodDecl *&Entry = KeyFunctions[RD];
1146 if (!Entry)
1147 Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD);
1148 else
1149 assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) &&
1150 "Key function changed!");
1151
1152 return Entry;
1153}
1154
Chris Lattnera7674d82007-07-13 22:13:22 +00001155//===----------------------------------------------------------------------===//
1156// Type creation/memoization methods
1157//===----------------------------------------------------------------------===//
1158
John McCall0953e762009-09-24 19:53:00 +00001159QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1160 unsigned Fast = Quals.getFastQualifiers();
1161 Quals.removeFastQualifiers();
1162
1163 // Check if we've already instantiated this type.
1164 llvm::FoldingSetNodeID ID;
1165 ExtQuals::Profile(ID, TypeNode, Quals);
1166 void *InsertPos = 0;
1167 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1168 assert(EQ->getQualifiers() == Quals);
1169 QualType T = QualType(EQ, Fast);
1170 return T;
1171 }
1172
John McCall6b304a02009-09-24 23:30:46 +00001173 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001174 ExtQualNodes.InsertNode(New, InsertPos);
1175 QualType T = QualType(New, Fast);
1176 return T;
1177}
1178
1179QualType ASTContext::getVolatileType(QualType T) {
1180 QualType CanT = getCanonicalType(T);
1181 if (CanT.isVolatileQualified()) return T;
1182
1183 QualifierCollector Quals;
1184 const Type *TypeNode = Quals.strip(T);
1185 Quals.addVolatile();
1186
1187 return getExtQualType(TypeNode, Quals);
1188}
1189
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001190QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001191 QualType CanT = getCanonicalType(T);
1192 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001193 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001194
John McCall0953e762009-09-24 19:53:00 +00001195 // If we are composing extended qualifiers together, merge together
1196 // into one ExtQuals node.
1197 QualifierCollector Quals;
1198 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
John McCall0953e762009-09-24 19:53:00 +00001200 // If this type already has an address space specified, it cannot get
1201 // another one.
1202 assert(!Quals.hasAddressSpace() &&
1203 "Type cannot be in multiple addr spaces!");
1204 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
John McCall0953e762009-09-24 19:53:00 +00001206 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001207}
1208
Chris Lattnerb7d25532009-02-18 22:53:11 +00001209QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001210 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001211 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001212 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001213 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001215 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001216 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001217 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001218 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1219 return getPointerType(ResultType);
1220 }
1221 }
Mike Stump1eb44332009-09-09 15:08:12 +00001222
John McCall0953e762009-09-24 19:53:00 +00001223 // If we are composing extended qualifiers together, merge together
1224 // into one ExtQuals node.
1225 QualifierCollector Quals;
1226 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001227
John McCall0953e762009-09-24 19:53:00 +00001228 // If this type already has an ObjCGC specified, it cannot get
1229 // another one.
1230 assert(!Quals.hasObjCGCAttr() &&
1231 "Type cannot have multiple ObjCGCs!");
1232 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
John McCall0953e762009-09-24 19:53:00 +00001234 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001235}
Chris Lattnera7674d82007-07-13 22:13:22 +00001236
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001237static QualType getNoReturnCallConvType(ASTContext& Context, QualType T,
1238 bool AddNoReturn,
1239 CallingConv CallConv) {
John McCall0953e762009-09-24 19:53:00 +00001240 QualType ResultType;
Douglas Gregor43c79c22009-12-09 00:47:37 +00001241 if (const PointerType *Pointer = T->getAs<PointerType>()) {
1242 QualType Pointee = Pointer->getPointeeType();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001243 ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn,
1244 CallConv);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001245 if (ResultType == Pointee)
1246 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001247
1248 ResultType = Context.getPointerType(ResultType);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001249 } else if (const BlockPointerType *BlockPointer
1250 = T->getAs<BlockPointerType>()) {
1251 QualType Pointee = BlockPointer->getPointeeType();
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001252 ResultType = getNoReturnCallConvType(Context, Pointee, AddNoReturn,
1253 CallConv);
Douglas Gregor43c79c22009-12-09 00:47:37 +00001254 if (ResultType == Pointee)
1255 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001256
1257 ResultType = Context.getBlockPointerType(ResultType);
1258 } else if (const FunctionType *F = T->getAs<FunctionType>()) {
1259 if (F->getNoReturnAttr() == AddNoReturn && F->getCallConv() == CallConv)
Douglas Gregor43c79c22009-12-09 00:47:37 +00001260 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001261
Douglas Gregor43c79c22009-12-09 00:47:37 +00001262 if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001263 ResultType = Context.getFunctionNoProtoType(FNPT->getResultType(),
1264 AddNoReturn, CallConv);
John McCall0953e762009-09-24 19:53:00 +00001265 } else {
Douglas Gregor43c79c22009-12-09 00:47:37 +00001266 const FunctionProtoType *FPT = cast<FunctionProtoType>(F);
John McCall0953e762009-09-24 19:53:00 +00001267 ResultType
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001268 = Context.getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1269 FPT->getNumArgs(), FPT->isVariadic(),
1270 FPT->getTypeQuals(),
1271 FPT->hasExceptionSpec(),
1272 FPT->hasAnyExceptionSpec(),
1273 FPT->getNumExceptions(),
1274 FPT->exception_begin(),
1275 AddNoReturn, CallConv);
John McCall0953e762009-09-24 19:53:00 +00001276 }
Douglas Gregor43c79c22009-12-09 00:47:37 +00001277 } else
1278 return T;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001279
1280 return Context.getQualifiedType(ResultType, T.getLocalQualifiers());
1281}
1282
1283QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) {
1284 return getNoReturnCallConvType(*this, T, AddNoReturn, T.getCallConv());
1285}
1286
1287QualType ASTContext::getCallConvType(QualType T, CallingConv CallConv) {
1288 return getNoReturnCallConvType(*this, T, T.getNoReturnAttr(), CallConv);
Mike Stump24556362009-07-25 21:26:53 +00001289}
1290
Reid Spencer5f016e22007-07-11 17:01:13 +00001291/// getComplexType - Return the uniqued reference to the type for a complex
1292/// number with the specified element type.
1293QualType ASTContext::getComplexType(QualType T) {
1294 // Unique pointers, to guarantee there is only one pointer of a particular
1295 // structure.
1296 llvm::FoldingSetNodeID ID;
1297 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 void *InsertPos = 0;
1300 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1301 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // If the pointee type isn't canonical, this won't be a canonical type either,
1304 // so fill in the canonical type field.
1305 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001306 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001307 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 // Get the new insert position for the node we care about.
1310 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001311 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 }
John McCall6b304a02009-09-24 23:30:46 +00001313 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 Types.push_back(New);
1315 ComplexTypes.InsertNode(New, InsertPos);
1316 return QualType(New, 0);
1317}
1318
Reid Spencer5f016e22007-07-11 17:01:13 +00001319/// getPointerType - Return the uniqued reference to the type for a pointer to
1320/// the specified type.
1321QualType ASTContext::getPointerType(QualType T) {
1322 // Unique pointers, to guarantee there is only one pointer of a particular
1323 // structure.
1324 llvm::FoldingSetNodeID ID;
1325 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001326
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 void *InsertPos = 0;
1328 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1329 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 // If the pointee type isn't canonical, this won't be a canonical type either,
1332 // so fill in the canonical type field.
1333 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001334 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001335 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 // Get the new insert position for the node we care about.
1338 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001339 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 }
John McCall6b304a02009-09-24 23:30:46 +00001341 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 Types.push_back(New);
1343 PointerTypes.InsertNode(New, InsertPos);
1344 return QualType(New, 0);
1345}
1346
Mike Stump1eb44332009-09-09 15:08:12 +00001347/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001348/// a pointer to the specified block.
1349QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001350 assert(T->isFunctionType() && "block of function types only");
1351 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001352 // structure.
1353 llvm::FoldingSetNodeID ID;
1354 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Steve Naroff5618bd42008-08-27 16:04:49 +00001356 void *InsertPos = 0;
1357 if (BlockPointerType *PT =
1358 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1359 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001360
1361 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001362 // type either so fill in the canonical type field.
1363 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001364 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001365 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Steve Naroff5618bd42008-08-27 16:04:49 +00001367 // Get the new insert position for the node we care about.
1368 BlockPointerType *NewIP =
1369 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001370 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001371 }
John McCall6b304a02009-09-24 23:30:46 +00001372 BlockPointerType *New
1373 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001374 Types.push_back(New);
1375 BlockPointerTypes.InsertNode(New, InsertPos);
1376 return QualType(New, 0);
1377}
1378
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001379/// getLValueReferenceType - Return the uniqued reference to the type for an
1380/// lvalue reference to the specified type.
John McCall54e14c42009-10-22 22:37:11 +00001381QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001382 // Unique pointers, to guarantee there is only one pointer of a particular
1383 // structure.
1384 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001385 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001386
1387 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001388 if (LValueReferenceType *RT =
1389 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001391
John McCall54e14c42009-10-22 22:37:11 +00001392 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1393
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 // If the referencee type isn't canonical, this won't be a canonical type
1395 // either, so fill in the canonical type field.
1396 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001397 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1398 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1399 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001400
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001402 LValueReferenceType *NewIP =
1403 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001404 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001405 }
1406
John McCall6b304a02009-09-24 23:30:46 +00001407 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001408 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1409 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001411 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001412
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001413 return QualType(New, 0);
1414}
1415
1416/// getRValueReferenceType - Return the uniqued reference to the type for an
1417/// rvalue reference to the specified type.
1418QualType ASTContext::getRValueReferenceType(QualType T) {
1419 // Unique pointers, to guarantee there is only one pointer of a particular
1420 // structure.
1421 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001422 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001423
1424 void *InsertPos = 0;
1425 if (RValueReferenceType *RT =
1426 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1427 return QualType(RT, 0);
1428
John McCall54e14c42009-10-22 22:37:11 +00001429 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1430
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001431 // If the referencee type isn't canonical, this won't be a canonical type
1432 // either, so fill in the canonical type field.
1433 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001434 if (InnerRef || !T.isCanonical()) {
1435 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1436 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001437
1438 // Get the new insert position for the node we care about.
1439 RValueReferenceType *NewIP =
1440 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1441 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1442 }
1443
John McCall6b304a02009-09-24 23:30:46 +00001444 RValueReferenceType *New
1445 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001446 Types.push_back(New);
1447 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 return QualType(New, 0);
1449}
1450
Sebastian Redlf30208a2009-01-24 21:16:55 +00001451/// getMemberPointerType - Return the uniqued reference to the type for a
1452/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001453QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001454 // Unique pointers, to guarantee there is only one pointer of a particular
1455 // structure.
1456 llvm::FoldingSetNodeID ID;
1457 MemberPointerType::Profile(ID, T, Cls);
1458
1459 void *InsertPos = 0;
1460 if (MemberPointerType *PT =
1461 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1462 return QualType(PT, 0);
1463
1464 // If the pointee or class type isn't canonical, this won't be a canonical
1465 // type either, so fill in the canonical type field.
1466 QualType Canonical;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001467 if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001468 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1469
1470 // Get the new insert position for the node we care about.
1471 MemberPointerType *NewIP =
1472 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1473 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1474 }
John McCall6b304a02009-09-24 23:30:46 +00001475 MemberPointerType *New
1476 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001477 Types.push_back(New);
1478 MemberPointerTypes.InsertNode(New, InsertPos);
1479 return QualType(New, 0);
1480}
1481
Mike Stump1eb44332009-09-09 15:08:12 +00001482/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001483/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001484QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001485 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001486 ArrayType::ArraySizeModifier ASM,
1487 unsigned EltTypeQuals) {
Sebastian Redl923d56d2009-11-05 15:52:31 +00001488 assert((EltTy->isDependentType() ||
1489 EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
Eli Friedman587cbdf2009-05-29 20:17:55 +00001490 "Constant array of VLAs is illegal!");
1491
Chris Lattner38aeec72009-05-13 04:12:56 +00001492 // Convert the array size into a canonical width matching the pointer size for
1493 // the target.
1494 llvm::APInt ArySize(ArySizeIn);
1495 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001498 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001499
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001501 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001502 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Reid Spencer5f016e22007-07-11 17:01:13 +00001505 // If the element type isn't canonical, this won't be a canonical type either,
1506 // so fill in the canonical type field.
1507 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001508 if (!EltTy.isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001509 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001510 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001512 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001513 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001514 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
John McCall6b304a02009-09-24 23:30:46 +00001517 ConstantArrayType *New = new(*this,TypeAlignment)
1518 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001519 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001520 Types.push_back(New);
1521 return QualType(New, 0);
1522}
1523
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001524/// getVariableArrayType - Returns a non-unique reference to the type for a
1525/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001526QualType ASTContext::getVariableArrayType(QualType EltTy,
1527 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001528 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001529 unsigned EltTypeQuals,
1530 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001531 // Since we don't unique expressions, it isn't possible to unique VLA's
1532 // that have an expression provided for their size.
1533
John McCall6b304a02009-09-24 23:30:46 +00001534 VariableArrayType *New = new(*this, TypeAlignment)
1535 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001536
1537 VariableArrayTypes.push_back(New);
1538 Types.push_back(New);
1539 return QualType(New, 0);
1540}
1541
Douglas Gregor898574e2008-12-05 23:32:09 +00001542/// getDependentSizedArrayType - Returns a non-unique reference to
1543/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001544/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001545QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1546 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001547 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001548 unsigned EltTypeQuals,
1549 SourceRange Brackets) {
Douglas Gregorcb78d882009-11-19 18:03:26 +00001550 assert((!NumElts || NumElts->isTypeDependent() ||
1551 NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001552 "Size must be type- or value-dependent!");
1553
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001554 void *InsertPos = 0;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001555 DependentSizedArrayType *Canon = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00001556 llvm::FoldingSetNodeID ID;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001557
1558 if (NumElts) {
1559 // Dependently-sized array types that do not have a specified
1560 // number of elements will have their sizes deduced from an
1561 // initializer.
Douglas Gregorcb78d882009-11-19 18:03:26 +00001562 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
1563 EltTypeQuals, NumElts);
1564
1565 Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1566 }
1567
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001568 DependentSizedArrayType *New;
1569 if (Canon) {
1570 // We already have a canonical version of this array type; use it as
1571 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001572 New = new (*this, TypeAlignment)
1573 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1574 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001575 } else {
1576 QualType CanonEltTy = getCanonicalType(EltTy);
1577 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001578 New = new (*this, TypeAlignment)
1579 DependentSizedArrayType(*this, EltTy, QualType(),
1580 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorcb78d882009-11-19 18:03:26 +00001581
Douglas Gregor789b1f62010-02-04 18:10:26 +00001582 if (NumElts) {
1583 DependentSizedArrayType *CanonCheck
1584 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1585 assert(!CanonCheck && "Dependent-sized canonical array type broken");
1586 (void)CanonCheck;
Douglas Gregorcb78d882009-11-19 18:03:26 +00001587 DependentSizedArrayTypes.InsertNode(New, InsertPos);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001588 }
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001589 } else {
1590 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1591 ASM, EltTypeQuals,
1592 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001593 New = new (*this, TypeAlignment)
1594 DependentSizedArrayType(*this, EltTy, Canon,
1595 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001596 }
1597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Douglas Gregor898574e2008-12-05 23:32:09 +00001599 Types.push_back(New);
1600 return QualType(New, 0);
1601}
1602
Eli Friedmanc5773c42008-02-15 18:16:39 +00001603QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1604 ArrayType::ArraySizeModifier ASM,
1605 unsigned EltTypeQuals) {
1606 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001607 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001608
1609 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001610 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001611 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1612 return QualType(ATP, 0);
1613
1614 // If the element type isn't canonical, this won't be a canonical type
1615 // either, so fill in the canonical type field.
1616 QualType Canonical;
1617
John McCall467b27b2009-10-22 20:10:53 +00001618 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001619 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001620 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001621
1622 // Get the new insert position for the node we care about.
1623 IncompleteArrayType *NewIP =
1624 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001625 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001626 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001627
John McCall6b304a02009-09-24 23:30:46 +00001628 IncompleteArrayType *New = new (*this, TypeAlignment)
1629 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001630
1631 IncompleteArrayTypes.InsertNode(New, InsertPos);
1632 Types.push_back(New);
1633 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001634}
1635
Steve Naroff73322922007-07-18 18:00:27 +00001636/// getVectorType - Return the unique reference to a vector type of
1637/// the specified element type and size. VectorType must be a built-in type.
John Thompson82287d12010-02-05 00:12:22 +00001638QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
1639 bool IsAltiVec, bool IsPixel) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001640 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Chris Lattnerf52ab252008-04-06 22:59:24 +00001642 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001643 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 // Check if we've already instantiated a vector of this type.
1646 llvm::FoldingSetNodeID ID;
John Thompson82287d12010-02-05 00:12:22 +00001647 VectorType::Profile(ID, vecType, NumElts, Type::Vector,
1648 IsAltiVec, IsPixel);
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 void *InsertPos = 0;
1650 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1651 return QualType(VTP, 0);
1652
1653 // If the element type isn't canonical, this won't be a canonical type either,
1654 // so fill in the canonical type field.
1655 QualType Canonical;
John Thompson82287d12010-02-05 00:12:22 +00001656 if (!vecType.isCanonical() || IsAltiVec || IsPixel) {
1657 Canonical = getVectorType(getCanonicalType(vecType),
1658 NumElts, false, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 // Get the new insert position for the node we care about.
1661 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001662 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 }
John McCall6b304a02009-09-24 23:30:46 +00001664 VectorType *New = new (*this, TypeAlignment)
John Thompson82287d12010-02-05 00:12:22 +00001665 VectorType(vecType, NumElts, Canonical, IsAltiVec, IsPixel);
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 VectorTypes.InsertNode(New, InsertPos);
1667 Types.push_back(New);
1668 return QualType(New, 0);
1669}
1670
Nate Begeman213541a2008-04-18 23:10:10 +00001671/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001672/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001673QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001674 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Chris Lattnerf52ab252008-04-06 22:59:24 +00001676 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001677 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Steve Naroff73322922007-07-18 18:00:27 +00001679 // Check if we've already instantiated a vector of this type.
1680 llvm::FoldingSetNodeID ID;
John Thompson82287d12010-02-05 00:12:22 +00001681 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, false, false);
Steve Naroff73322922007-07-18 18:00:27 +00001682 void *InsertPos = 0;
1683 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1684 return QualType(VTP, 0);
1685
1686 // If the element type isn't canonical, this won't be a canonical type either,
1687 // so fill in the canonical type field.
1688 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001689 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001690 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Steve Naroff73322922007-07-18 18:00:27 +00001692 // Get the new insert position for the node we care about.
1693 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001694 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001695 }
John McCall6b304a02009-09-24 23:30:46 +00001696 ExtVectorType *New = new (*this, TypeAlignment)
1697 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001698 VectorTypes.InsertNode(New, InsertPos);
1699 Types.push_back(New);
1700 return QualType(New, 0);
1701}
1702
Mike Stump1eb44332009-09-09 15:08:12 +00001703QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001704 Expr *SizeExpr,
1705 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001706 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001707 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001708 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001710 void *InsertPos = 0;
1711 DependentSizedExtVectorType *Canon
1712 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1713 DependentSizedExtVectorType *New;
1714 if (Canon) {
1715 // We already have a canonical version of this array type; use it as
1716 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001717 New = new (*this, TypeAlignment)
1718 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1719 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001720 } else {
1721 QualType CanonVecTy = getCanonicalType(vecType);
1722 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001723 New = new (*this, TypeAlignment)
1724 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1725 AttrLoc);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001726
1727 DependentSizedExtVectorType *CanonCheck
1728 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1729 assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1730 (void)CanonCheck;
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001731 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1732 } else {
1733 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1734 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001735 New = new (*this, TypeAlignment)
1736 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001737 }
1738 }
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001740 Types.push_back(New);
1741 return QualType(New, 0);
1742}
1743
Douglas Gregor72564e72009-02-26 23:50:07 +00001744/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001745///
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001746QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn,
1747 CallingConv CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 // Unique functions, to guarantee there is only one function of a particular
1749 // structure.
1750 llvm::FoldingSetNodeID ID;
John McCallf82b4e82010-02-04 05:44:44 +00001751 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn, CallConv);
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001754 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001755 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 QualType Canonical;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001759 if (!ResultTy.isCanonical() ||
John McCall04a67a62010-02-05 21:31:56 +00001760 getCanonicalCallConv(CallConv) != CallConv) {
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001761 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn,
John McCall04a67a62010-02-05 21:31:56 +00001762 getCanonicalCallConv(CallConv));
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001765 FunctionNoProtoType *NewIP =
1766 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001767 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 }
Mike Stump1eb44332009-09-09 15:08:12 +00001769
John McCall6b304a02009-09-24 23:30:46 +00001770 FunctionNoProtoType *New = new (*this, TypeAlignment)
John McCallf82b4e82010-02-04 05:44:44 +00001771 FunctionNoProtoType(ResultTy, Canonical, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001773 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 return QualType(New, 0);
1775}
1776
1777/// getFunctionType - Return a normal function type with a typed argument
1778/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001779QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001780 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001781 unsigned TypeQuals, bool hasExceptionSpec,
1782 bool hasAnyExceptionSpec, unsigned NumExs,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001783 const QualType *ExArray, bool NoReturn,
1784 CallingConv CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 // Unique functions, to guarantee there is only one function of a particular
1786 // structure.
1787 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001788 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001789 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
John McCallf82b4e82010-02-04 05:44:44 +00001790 NumExs, ExArray, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001791
1792 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001793 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001794 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001796
1797 // Determine whether the type being created is already canonical or not.
John McCall54e14c42009-10-22 22:37:11 +00001798 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001800 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 isCanonical = false;
1802
1803 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001804 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001805 QualType Canonical;
John McCall04a67a62010-02-05 21:31:56 +00001806 if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001807 llvm::SmallVector<QualType, 16> CanonicalArgs;
1808 CanonicalArgs.reserve(NumArgs);
1809 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001810 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001811
Chris Lattnerf52ab252008-04-06 22:59:24 +00001812 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001813 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001814 isVariadic, TypeQuals, false,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001815 false, 0, 0, NoReturn,
John McCall04a67a62010-02-05 21:31:56 +00001816 getCanonicalCallConv(CallConv));
Sebastian Redl465226e2009-05-27 22:11:52 +00001817
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001819 FunctionProtoType *NewIP =
1820 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001821 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001822 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001823
Douglas Gregor72564e72009-02-26 23:50:07 +00001824 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001825 // for two variable size arrays (for parameter and exception types) at the
1826 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001827 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001828 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1829 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001830 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001831 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001832 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Douglas Gregorab8bbf42010-01-18 17:14:39 +00001833 ExArray, NumExs, Canonical, NoReturn, CallConv);
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001835 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 return QualType(FTP, 0);
1837}
1838
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001839/// getTypeDeclType - Return the unique reference to the type for the
1840/// specified type declaration.
John McCall19c85762010-02-16 03:57:14 +00001841QualType ASTContext::getTypeDeclType(const TypeDecl *Decl,
1842 const TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001843 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001844 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001845
John McCall19c85762010-02-16 03:57:14 +00001846 if (const TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001847 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001848 else if (isa<TemplateTypeParmDecl>(Decl)) {
1849 assert(false && "Template type parameter types are always available.");
John McCall19c85762010-02-16 03:57:14 +00001850 } else if (const ObjCInterfaceDecl *ObjCInterface
Mike Stump9fdbab32009-07-31 02:02:20 +00001851 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001852 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001853
John McCall19c85762010-02-16 03:57:14 +00001854 if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001855 if (PrevDecl)
1856 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001857 else
John McCall6b304a02009-09-24 23:30:46 +00001858 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
John McCall19c85762010-02-16 03:57:14 +00001859 } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001860 if (PrevDecl)
1861 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001862 else
John McCall6b304a02009-09-24 23:30:46 +00001863 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
John McCall19c85762010-02-16 03:57:14 +00001864 } else if (const UnresolvedUsingTypenameDecl *Using =
John McCalled976492009-12-04 22:46:56 +00001865 dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
1866 Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using);
Mike Stump9fdbab32009-07-31 02:02:20 +00001867 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001868 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001869
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001870 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001871 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001872}
1873
Reid Spencer5f016e22007-07-11 17:01:13 +00001874/// getTypedefType - Return the unique reference to the type for the
1875/// specified typename decl.
John McCall19c85762010-02-16 03:57:14 +00001876QualType ASTContext::getTypedefType(const TypedefDecl *Decl) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Chris Lattnerf52ab252008-04-06 22:59:24 +00001879 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001880 Decl->TypeForDecl = new(*this, TypeAlignment)
1881 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001882 Types.push_back(Decl->TypeForDecl);
1883 return QualType(Decl->TypeForDecl, 0);
1884}
1885
John McCall49a832b2009-10-18 09:09:24 +00001886/// \brief Retrieve a substitution-result type.
1887QualType
1888ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1889 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001890 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001891 && "replacement types must always be canonical");
1892
1893 llvm::FoldingSetNodeID ID;
1894 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1895 void *InsertPos = 0;
1896 SubstTemplateTypeParmType *SubstParm
1897 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1898
1899 if (!SubstParm) {
1900 SubstParm = new (*this, TypeAlignment)
1901 SubstTemplateTypeParmType(Parm, Replacement);
1902 Types.push_back(SubstParm);
1903 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1904 }
1905
1906 return QualType(SubstParm, 0);
1907}
1908
Douglas Gregorfab9d672009-02-05 23:33:38 +00001909/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001910/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001911/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001912QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001913 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001914 IdentifierInfo *Name) {
1915 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001916 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001917 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001918 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001919 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1920
1921 if (TypeParm)
1922 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001924 if (Name) {
1925 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001926 TypeParm = new (*this, TypeAlignment)
1927 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00001928
1929 TemplateTypeParmType *TypeCheck
1930 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1931 assert(!TypeCheck && "Template type parameter canonical type broken");
1932 (void)TypeCheck;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001933 } else
John McCall6b304a02009-09-24 23:30:46 +00001934 TypeParm = new (*this, TypeAlignment)
1935 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001936
1937 Types.push_back(TypeParm);
1938 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1939
1940 return QualType(TypeParm, 0);
1941}
1942
Mike Stump1eb44332009-09-09 15:08:12 +00001943QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001944ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCalld5532b62009-11-23 01:53:49 +00001945 const TemplateArgumentListInfo &Args,
John McCall833ca992009-10-29 08:12:44 +00001946 QualType Canon) {
John McCalld5532b62009-11-23 01:53:49 +00001947 unsigned NumArgs = Args.size();
1948
John McCall833ca992009-10-29 08:12:44 +00001949 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1950 ArgVec.reserve(NumArgs);
1951 for (unsigned i = 0; i != NumArgs; ++i)
1952 ArgVec.push_back(Args[i].getArgument());
1953
1954 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon);
1955}
1956
1957QualType
1958ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001959 const TemplateArgument *Args,
1960 unsigned NumArgs,
1961 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001962 if (!Canon.isNull())
1963 Canon = getCanonicalType(Canon);
1964 else {
1965 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001966 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1967 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1968 CanonArgs.reserve(NumArgs);
1969 for (unsigned I = 0; I != NumArgs; ++I)
1970 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1971
1972 // Determine whether this canonical template specialization type already
1973 // exists.
1974 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001975 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001976 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001977
1978 void *InsertPos = 0;
1979 TemplateSpecializationType *Spec
1980 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor1275ae02009-07-28 23:00:59 +00001982 if (!Spec) {
1983 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001984 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001985 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001986 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001987 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001988 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001989 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001990 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001991 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001992 }
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Douglas Gregorb88e8882009-07-30 17:40:51 +00001994 if (Canon.isNull())
1995 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001996 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001997 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001998 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001999
Douglas Gregor1275ae02009-07-28 23:00:59 +00002000 // Allocate the (non-canonical) template specialization type, but don't
2001 // try to unique it: these types typically have location information that
2002 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00002003 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00002004 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00002005 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00002006 TemplateSpecializationType *Spec
2007 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00002008 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00002009
Douglas Gregor55f6b142009-02-09 18:46:07 +00002010 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00002011 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002012}
2013
Mike Stump1eb44332009-09-09 15:08:12 +00002014QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00002015ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00002016 QualType NamedType) {
2017 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00002018 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002019
2020 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002021 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00002022 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2023 if (T)
2024 return QualType(T, 0);
2025
Douglas Gregor789b1f62010-02-04 18:10:26 +00002026 QualType Canon = NamedType;
2027 if (!Canon.isCanonical()) {
2028 Canon = getCanonicalType(NamedType);
2029 QualifiedNameType *CheckT
2030 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2031 assert(!CheckT && "Qualified name canonical type broken");
2032 (void)CheckT;
2033 }
2034
2035 T = new (*this) QualifiedNameType(NNS, NamedType, Canon);
Douglas Gregore4e5b052009-03-19 00:18:19 +00002036 Types.push_back(T);
2037 QualifiedNameTypes.InsertNode(T, InsertPos);
2038 return QualType(T, 0);
2039}
2040
Mike Stump1eb44332009-09-09 15:08:12 +00002041QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00002042 const IdentifierInfo *Name,
2043 QualType Canon) {
2044 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2045
2046 if (Canon.isNull()) {
2047 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2048 if (CanonNNS != NNS)
2049 Canon = getTypenameType(CanonNNS, Name);
2050 }
2051
2052 llvm::FoldingSetNodeID ID;
2053 TypenameType::Profile(ID, NNS, Name);
2054
2055 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002056 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00002057 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
2058 if (T)
2059 return QualType(T, 0);
2060
2061 T = new (*this) TypenameType(NNS, Name, Canon);
2062 Types.push_back(T);
2063 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002064 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00002065}
2066
Mike Stump1eb44332009-09-09 15:08:12 +00002067QualType
2068ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00002069 const TemplateSpecializationType *TemplateId,
2070 QualType Canon) {
2071 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2072
Douglas Gregor789b1f62010-02-04 18:10:26 +00002073 llvm::FoldingSetNodeID ID;
2074 TypenameType::Profile(ID, NNS, TemplateId);
2075
2076 void *InsertPos = 0;
2077 TypenameType *T
2078 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
2079 if (T)
2080 return QualType(T, 0);
2081
Douglas Gregor17343172009-04-01 00:28:59 +00002082 if (Canon.isNull()) {
2083 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2084 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
2085 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
2086 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00002087 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00002088 assert(CanonTemplateId &&
2089 "Canonical type must also be a template specialization type");
2090 Canon = getTypenameType(CanonNNS, CanonTemplateId);
2091 }
Douglas Gregor789b1f62010-02-04 18:10:26 +00002092
2093 TypenameType *CheckT
2094 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
2095 assert(!CheckT && "Typename canonical type is broken"); (void)CheckT;
Douglas Gregor17343172009-04-01 00:28:59 +00002096 }
2097
Douglas Gregor17343172009-04-01 00:28:59 +00002098 T = new (*this) TypenameType(NNS, TemplateId, Canon);
2099 Types.push_back(T);
2100 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00002101 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00002102}
2103
John McCall7da24312009-09-05 00:15:47 +00002104QualType
2105ASTContext::getElaboratedType(QualType UnderlyingType,
2106 ElaboratedType::TagKind Tag) {
2107 llvm::FoldingSetNodeID ID;
2108 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00002109
John McCall7da24312009-09-05 00:15:47 +00002110 void *InsertPos = 0;
2111 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2112 if (T)
2113 return QualType(T, 0);
2114
Douglas Gregor789b1f62010-02-04 18:10:26 +00002115 QualType Canon = UnderlyingType;
2116 if (!Canon.isCanonical()) {
2117 Canon = getCanonicalType(Canon);
2118 ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2119 assert(!CheckT && "Elaborated canonical type is broken"); (void)CheckT;
2120 }
John McCall7da24312009-09-05 00:15:47 +00002121
2122 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
2123 Types.push_back(T);
2124 ElaboratedTypes.InsertNode(T, InsertPos);
2125 return QualType(T, 0);
2126}
2127
Chris Lattner88cb27a2008-04-07 04:56:42 +00002128/// CmpProtocolNames - Comparison predicate for sorting protocols
2129/// alphabetically.
2130static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
2131 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002132 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00002133}
2134
John McCall54e14c42009-10-22 22:37:11 +00002135static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
2136 unsigned NumProtocols) {
2137 if (NumProtocols == 0) return true;
2138
2139 for (unsigned i = 1; i != NumProtocols; ++i)
2140 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
2141 return false;
2142 return true;
2143}
2144
2145static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00002146 unsigned &NumProtocols) {
2147 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00002148
Chris Lattner88cb27a2008-04-07 04:56:42 +00002149 // Sort protocols, keyed by name.
2150 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
2151
2152 // Remove duplicates.
2153 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
2154 NumProtocols = ProtocolsEnd-Protocols;
2155}
2156
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002157/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2158/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00002159QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00002160 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002161 unsigned NumProtocols) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002162 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00002163 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002164
2165 void *InsertPos = 0;
2166 if (ObjCObjectPointerType *QT =
2167 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2168 return QualType(QT, 0);
2169
John McCall54e14c42009-10-22 22:37:11 +00002170 // Sort the protocol list alphabetically to canonicalize it.
2171 QualType Canonical;
2172 if (!InterfaceT.isCanonical() ||
2173 !areSortedAndUniqued(Protocols, NumProtocols)) {
2174 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
2175 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2176 unsigned UniqueCount = NumProtocols;
2177
2178 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2179 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2180
2181 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2182 &Sorted[0], UniqueCount);
2183 } else {
2184 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
2185 Protocols, NumProtocols);
2186 }
2187
2188 // Regenerate InsertPos.
2189 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2190 }
2191
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002192 // No match.
2193 unsigned Size = sizeof(ObjCObjectPointerType)
2194 + NumProtocols * sizeof(ObjCProtocolDecl *);
2195 void *Mem = Allocate(Size, TypeAlignment);
2196 ObjCObjectPointerType *QType = new (Mem) ObjCObjectPointerType(Canonical,
2197 InterfaceT,
2198 Protocols,
2199 NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002201 Types.push_back(QType);
2202 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
2203 return QualType(QType, 0);
2204}
Chris Lattner88cb27a2008-04-07 04:56:42 +00002205
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002206/// getObjCInterfaceType - Return the unique reference to the type for the
2207/// specified ObjC interface decl. The list of protocols is optional.
2208QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002209 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002210 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002211 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002213 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002214 if (ObjCInterfaceType *QT =
2215 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002216 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002217
John McCall54e14c42009-10-22 22:37:11 +00002218 // Sort the protocol list alphabetically to canonicalize it.
2219 QualType Canonical;
2220 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2221 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2222 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2223
2224 unsigned UniqueCount = NumProtocols;
2225 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2226
2227 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2228
2229 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2230 }
2231
Douglas Gregorfd6a0882010-02-08 22:59:26 +00002232 unsigned Size = sizeof(ObjCInterfaceType)
2233 + NumProtocols * sizeof(ObjCProtocolDecl *);
2234 void *Mem = Allocate(Size, TypeAlignment);
2235 ObjCInterfaceType *QType = new (Mem) ObjCInterfaceType(Canonical,
2236 const_cast<ObjCInterfaceDecl*>(Decl),
2237 Protocols,
2238 NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002239
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002240 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002241 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002242 return QualType(QType, 0);
2243}
2244
Douglas Gregor72564e72009-02-26 23:50:07 +00002245/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2246/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002247/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002248/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002249/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002250QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002251 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002252 if (tofExpr->isTypeDependent()) {
2253 llvm::FoldingSetNodeID ID;
2254 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002255
Douglas Gregorb1975722009-07-30 23:18:24 +00002256 void *InsertPos = 0;
2257 DependentTypeOfExprType *Canon
2258 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2259 if (Canon) {
2260 // We already have a "canonical" version of an identical, dependent
2261 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002262 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002263 QualType((TypeOfExprType*)Canon, 0));
2264 }
2265 else {
2266 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002267 Canon
2268 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002269 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2270 toe = Canon;
2271 }
2272 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002273 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002274 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002275 }
Steve Naroff9752f252007-08-01 18:02:17 +00002276 Types.push_back(toe);
2277 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002278}
2279
Steve Naroff9752f252007-08-01 18:02:17 +00002280/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2281/// TypeOfType AST's. The only motivation to unique these nodes would be
2282/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002283/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002284/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002285QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002286 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002287 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002288 Types.push_back(tot);
2289 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002290}
2291
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002292/// getDecltypeForExpr - Given an expr, will return the decltype for that
2293/// expression, according to the rules in C++0x [dcl.type.simple]p4
2294static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002295 if (e->isTypeDependent())
2296 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002298 // If e is an id expression or a class member access, decltype(e) is defined
2299 // as the type of the entity named by e.
2300 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2301 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2302 return VD->getType();
2303 }
2304 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2305 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2306 return FD->getType();
2307 }
2308 // If e is a function call or an invocation of an overloaded operator,
2309 // (parentheses around e are ignored), decltype(e) is defined as the
2310 // return type of that function.
2311 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2312 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002314 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002315
2316 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002317 // defined as T&, otherwise decltype(e) is defined as T.
2318 if (e->isLvalue(Context) == Expr::LV_Valid)
2319 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002321 return T;
2322}
2323
Anders Carlsson395b4752009-06-24 19:06:50 +00002324/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2325/// DecltypeType AST's. The only motivation to unique these nodes would be
2326/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002327/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002328/// on canonical type's (which are always unique).
2329QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002330 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002331 if (e->isTypeDependent()) {
2332 llvm::FoldingSetNodeID ID;
2333 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002334
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002335 void *InsertPos = 0;
2336 DependentDecltypeType *Canon
2337 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2338 if (Canon) {
2339 // We already have a "canonical" version of an equivalent, dependent
2340 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002341 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002342 QualType((DecltypeType*)Canon, 0));
2343 }
2344 else {
2345 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002346 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002347 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2348 dt = Canon;
2349 }
2350 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002351 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002352 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002353 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002354 Types.push_back(dt);
2355 return QualType(dt, 0);
2356}
2357
Reid Spencer5f016e22007-07-11 17:01:13 +00002358/// getTagDeclType - Return the unique reference to the type for the
2359/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002360QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002361 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002362 // FIXME: What is the design on getTagDeclType when it requires casting
2363 // away const? mutable?
2364 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002365}
2366
Mike Stump1eb44332009-09-09 15:08:12 +00002367/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2368/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2369/// needs to agree with the definition in <stddef.h>.
Anders Carlssona3ccda52009-12-12 00:26:23 +00002370CanQualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002371 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002372}
2373
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002374/// getSignedWCharType - Return the type of "signed wchar_t".
2375/// Used when in C++, as a GCC extension.
2376QualType ASTContext::getSignedWCharType() const {
2377 // FIXME: derive from "Target" ?
2378 return WCharTy;
2379}
2380
2381/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2382/// Used when in C++, as a GCC extension.
2383QualType ASTContext::getUnsignedWCharType() const {
2384 // FIXME: derive from "Target" ?
2385 return UnsignedIntTy;
2386}
2387
Chris Lattner8b9023b2007-07-13 03:05:23 +00002388/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2389/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2390QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002391 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002392}
2393
Chris Lattnere6327742008-04-02 05:18:44 +00002394//===----------------------------------------------------------------------===//
2395// Type Operators
2396//===----------------------------------------------------------------------===//
2397
John McCall54e14c42009-10-22 22:37:11 +00002398CanQualType ASTContext::getCanonicalParamType(QualType T) {
2399 // Push qualifiers into arrays, and then discard any remaining
2400 // qualifiers.
2401 T = getCanonicalType(T);
2402 const Type *Ty = T.getTypePtr();
2403
2404 QualType Result;
2405 if (isa<ArrayType>(Ty)) {
2406 Result = getArrayDecayedType(QualType(Ty,0));
2407 } else if (isa<FunctionType>(Ty)) {
2408 Result = getPointerType(QualType(Ty, 0));
2409 } else {
2410 Result = QualType(Ty, 0);
2411 }
2412
2413 return CanQualType::CreateUnsafe(Result);
2414}
2415
Chris Lattner77c96472008-04-06 22:41:35 +00002416/// getCanonicalType - Return the canonical (structural) type corresponding to
2417/// the specified potentially non-canonical type. The non-canonical version
2418/// of a type may have many "decorated" versions of types. Decorators can
2419/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2420/// to be free of any of these, allowing two canonical types to be compared
2421/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002422CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002423 QualifierCollector Quals;
2424 const Type *Ptr = Quals.strip(T);
2425 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002426
John McCall0953e762009-09-24 19:53:00 +00002427 // The canonical internal type will be the canonical type *except*
2428 // that we push type qualifiers down through array types.
2429
2430 // If there are no new qualifiers to push down, stop here.
2431 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002432 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002433
John McCall0953e762009-09-24 19:53:00 +00002434 // If the type qualifiers are on an array type, get the canonical
2435 // type of the array with the qualifiers applied to the element
2436 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002437 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2438 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002439 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002441 // Get the canonical version of the element with the extra qualifiers on it.
2442 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002443 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002444 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002445
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002446 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002447 return CanQualType::CreateUnsafe(
2448 getConstantArrayType(NewEltTy, CAT->getSize(),
2449 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002450 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002451 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002452 return CanQualType::CreateUnsafe(
2453 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002454 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Douglas Gregor898574e2008-12-05 23:32:09 +00002456 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002457 return CanQualType::CreateUnsafe(
2458 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002459 DSAT->getSizeExpr() ?
2460 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002461 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002462 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor87a924e2009-10-30 22:56:57 +00002463 DSAT->getBracketsRange())->getCanonicalTypeInternal());
Douglas Gregor898574e2008-12-05 23:32:09 +00002464
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002465 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002466 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002467 VAT->getSizeExpr() ?
2468 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002469 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002470 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002471 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002472}
2473
Chandler Carruth28e318c2009-12-29 07:16:59 +00002474QualType ASTContext::getUnqualifiedArrayType(QualType T,
2475 Qualifiers &Quals) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002476 Quals = T.getQualifiers();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002477 if (!isa<ArrayType>(T)) {
Chandler Carruth5535c382010-01-12 20:32:25 +00002478 return T.getUnqualifiedType();
Chandler Carruth28e318c2009-12-29 07:16:59 +00002479 }
2480
Chandler Carruth28e318c2009-12-29 07:16:59 +00002481 const ArrayType *AT = cast<ArrayType>(T);
2482 QualType Elt = AT->getElementType();
Zhongxing Xuc1ae0a82010-01-05 08:15:06 +00002483 QualType UnqualElt = getUnqualifiedArrayType(Elt, Quals);
Chandler Carruth28e318c2009-12-29 07:16:59 +00002484 if (Elt == UnqualElt)
2485 return T;
2486
2487 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
2488 return getConstantArrayType(UnqualElt, CAT->getSize(),
2489 CAT->getSizeModifier(), 0);
2490 }
2491
2492 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
2493 return getIncompleteArrayType(UnqualElt, IAT->getSizeModifier(), 0);
2494 }
2495
2496 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
2497 return getDependentSizedArrayType(UnqualElt, DSAT->getSizeExpr()->Retain(),
2498 DSAT->getSizeModifier(), 0,
2499 SourceRange());
2500}
2501
John McCall80ad16f2009-11-24 18:42:40 +00002502DeclarationName ASTContext::getNameForTemplate(TemplateName Name) {
2503 if (TemplateDecl *TD = Name.getAsTemplateDecl())
2504 return TD->getDeclName();
2505
2506 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
2507 if (DTN->isIdentifier()) {
2508 return DeclarationNames.getIdentifier(DTN->getIdentifier());
2509 } else {
2510 return DeclarationNames.getCXXOperatorName(DTN->getOperator());
2511 }
2512 }
2513
John McCall0bd6feb2009-12-02 08:04:21 +00002514 OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
2515 assert(Storage);
2516 return (*Storage->begin())->getDeclName();
John McCall80ad16f2009-11-24 18:42:40 +00002517}
2518
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002519TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2520 // If this template name refers to a template, the canonical
2521 // template name merely stores the template itself.
2522 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002523 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002524
John McCall0bd6feb2009-12-02 08:04:21 +00002525 assert(!Name.getAsOverloadedTemplate());
Mike Stump1eb44332009-09-09 15:08:12 +00002526
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002527 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2528 assert(DTN && "Non-dependent template names must refer to template decls.");
2529 return DTN->CanonicalTemplateName;
2530}
2531
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002532bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
2533 X = getCanonicalTemplateName(X);
2534 Y = getCanonicalTemplateName(Y);
2535 return X.getAsVoidPointer() == Y.getAsVoidPointer();
2536}
2537
Mike Stump1eb44332009-09-09 15:08:12 +00002538TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002539ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2540 switch (Arg.getKind()) {
2541 case TemplateArgument::Null:
2542 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002543
Douglas Gregor1275ae02009-07-28 23:00:59 +00002544 case TemplateArgument::Expression:
Douglas Gregor1275ae02009-07-28 23:00:59 +00002545 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Douglas Gregor1275ae02009-07-28 23:00:59 +00002547 case TemplateArgument::Declaration:
John McCall833ca992009-10-29 08:12:44 +00002548 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Douglas Gregor788cd062009-11-11 01:00:40 +00002550 case TemplateArgument::Template:
2551 return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
2552
Douglas Gregor1275ae02009-07-28 23:00:59 +00002553 case TemplateArgument::Integral:
John McCall833ca992009-10-29 08:12:44 +00002554 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002555 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002556
Douglas Gregor1275ae02009-07-28 23:00:59 +00002557 case TemplateArgument::Type:
John McCall833ca992009-10-29 08:12:44 +00002558 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Douglas Gregor1275ae02009-07-28 23:00:59 +00002560 case TemplateArgument::Pack: {
2561 // FIXME: Allocate in ASTContext
2562 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2563 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002564 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002565 AEnd = Arg.pack_end();
2566 A != AEnd; (void)++A, ++Idx)
2567 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002568
Douglas Gregor1275ae02009-07-28 23:00:59 +00002569 TemplateArgument Result;
2570 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2571 return Result;
2572 }
2573 }
2574
2575 // Silence GCC warning
2576 assert(false && "Unhandled template argument kind");
2577 return TemplateArgument();
2578}
2579
Douglas Gregord57959a2009-03-27 23:10:48 +00002580NestedNameSpecifier *
2581ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002582 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002583 return 0;
2584
2585 switch (NNS->getKind()) {
2586 case NestedNameSpecifier::Identifier:
2587 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002588 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002589 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2590 NNS->getAsIdentifier());
2591
2592 case NestedNameSpecifier::Namespace:
2593 // A namespace is canonical; build a nested-name-specifier with
2594 // this namespace and no prefix.
2595 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2596
2597 case NestedNameSpecifier::TypeSpec:
2598 case NestedNameSpecifier::TypeSpecWithTemplate: {
2599 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002600 return NestedNameSpecifier::Create(*this, 0,
2601 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002602 T.getTypePtr());
2603 }
2604
2605 case NestedNameSpecifier::Global:
2606 // The global specifier is canonical and unique.
2607 return NNS;
2608 }
2609
2610 // Required to silence a GCC warning
2611 return 0;
2612}
2613
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002614
2615const ArrayType *ASTContext::getAsArrayType(QualType T) {
2616 // Handle the non-qualified case efficiently.
Douglas Gregora4923eb2009-11-16 21:35:15 +00002617 if (!T.hasLocalQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002618 // Handle the common positive case fast.
2619 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2620 return AT;
2621 }
Mike Stump1eb44332009-09-09 15:08:12 +00002622
John McCall0953e762009-09-24 19:53:00 +00002623 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002624 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002625 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002626 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002627
John McCall0953e762009-09-24 19:53:00 +00002628 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002629 // implements C99 6.7.3p8: "If the specification of an array type includes
2630 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002631
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002632 // If we get here, we either have type qualifiers on the type, or we have
2633 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002634 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002635
John McCall0953e762009-09-24 19:53:00 +00002636 QualifierCollector Qs;
2637 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002638
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002639 // If we have a simple case, just return now.
2640 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002641 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002642 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002643
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002644 // Otherwise, we have an array and we have qualifiers on it. Push the
2645 // qualifiers into the array element type and return a new array type.
2646 // Get the canonical version of the element with the extra qualifiers on it.
2647 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002648 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002649
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002650 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2651 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2652 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002653 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002654 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2655 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2656 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002657 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002658
Mike Stump1eb44332009-09-09 15:08:12 +00002659 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002660 = dyn_cast<DependentSizedArrayType>(ATy))
2661 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002662 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002663 DSAT->getSizeExpr() ?
2664 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002665 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002666 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002667 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002668
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002669 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002670 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002671 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002672 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002673 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002674 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002675 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002676}
2677
2678
Chris Lattnere6327742008-04-02 05:18:44 +00002679/// getArrayDecayedType - Return the properly qualified result of decaying the
2680/// specified array type to a pointer. This operation is non-trivial when
2681/// handling typedefs etc. The canonical type of "T" must be an array type,
2682/// this returns a pointer to a properly qualified element of the array.
2683///
2684/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2685QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002686 // Get the element type with 'getAsArrayType' so that we don't lose any
2687 // typedefs in the element type of the array. This also handles propagation
2688 // of type qualifiers from the array type into the element type if present
2689 // (C99 6.7.3p8).
2690 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2691 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002693 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002694
2695 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002696 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002697}
2698
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002699QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002700 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002701 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002702 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002703 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2704 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002705 } else {
John McCall0953e762009-09-24 19:53:00 +00002706 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002707 }
2708 }
2709}
2710
Anders Carlssonfbbce492009-09-25 01:23:32 +00002711QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2712 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Anders Carlssonfbbce492009-09-25 01:23:32 +00002714 if (const ArrayType *AT = getAsArrayType(ElemTy))
2715 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002716
Anders Carlsson6183a992008-12-21 03:44:36 +00002717 return ElemTy;
2718}
2719
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002720/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002721uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002722ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2723 uint64_t ElementCount = 1;
2724 do {
2725 ElementCount *= CA->getSize().getZExtValue();
2726 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2727 } while (CA);
2728 return ElementCount;
2729}
2730
Reid Spencer5f016e22007-07-11 17:01:13 +00002731/// getFloatingRank - Return a relative rank for floating point types.
2732/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002733static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002734 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002735 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002736
John McCall183700f2009-09-21 23:43:11 +00002737 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2738 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002739 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002740 case BuiltinType::Float: return FloatRank;
2741 case BuiltinType::Double: return DoubleRank;
2742 case BuiltinType::LongDouble: return LongDoubleRank;
2743 }
2744}
2745
Mike Stump1eb44332009-09-09 15:08:12 +00002746/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2747/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002748/// 'typeDomain' is a real floating point or complex type.
2749/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002750QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2751 QualType Domain) const {
2752 FloatingRank EltRank = getFloatingRank(Size);
2753 if (Domain->isComplexType()) {
2754 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002755 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002756 case FloatRank: return FloatComplexTy;
2757 case DoubleRank: return DoubleComplexTy;
2758 case LongDoubleRank: return LongDoubleComplexTy;
2759 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002760 }
Chris Lattner1361b112008-04-06 23:58:54 +00002761
2762 assert(Domain->isRealFloatingType() && "Unknown domain!");
2763 switch (EltRank) {
2764 default: assert(0 && "getFloatingRank(): illegal value for rank");
2765 case FloatRank: return FloatTy;
2766 case DoubleRank: return DoubleTy;
2767 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002768 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002769}
2770
Chris Lattner7cfeb082008-04-06 23:55:33 +00002771/// getFloatingTypeOrder - Compare the rank of the two specified floating
2772/// point types, ignoring the domain of the type (i.e. 'double' ==
2773/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002774/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002775int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2776 FloatingRank LHSR = getFloatingRank(LHS);
2777 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002778
Chris Lattnera75cea32008-04-06 23:38:49 +00002779 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002780 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002781 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002782 return 1;
2783 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002784}
2785
Chris Lattnerf52ab252008-04-06 22:59:24 +00002786/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2787/// routine will assert if passed a built-in type that isn't an integer or enum,
2788/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002789unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002790 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002791 if (EnumType* ET = dyn_cast<EnumType>(T))
John McCall842aef82009-12-09 09:09:27 +00002792 T = ET->getDecl()->getPromotionType().getTypePtr();
Eli Friedmanf98aba32009-02-13 02:31:07 +00002793
Eli Friedmana3426752009-07-05 23:44:27 +00002794 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2795 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2796
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002797 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2798 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2799
2800 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2801 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2802
Chris Lattnerf52ab252008-04-06 22:59:24 +00002803 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002804 default: assert(0 && "getIntegerRank(): not a built-in integer");
2805 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002806 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002807 case BuiltinType::Char_S:
2808 case BuiltinType::Char_U:
2809 case BuiltinType::SChar:
2810 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002811 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002812 case BuiltinType::Short:
2813 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002814 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002815 case BuiltinType::Int:
2816 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002817 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002818 case BuiltinType::Long:
2819 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002820 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002821 case BuiltinType::LongLong:
2822 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002823 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002824 case BuiltinType::Int128:
2825 case BuiltinType::UInt128:
2826 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002827 }
2828}
2829
Eli Friedman04e83572009-08-20 04:21:42 +00002830/// \brief Whether this is a promotable bitfield reference according
2831/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2832///
2833/// \returns the type this bit-field will promote to, or NULL if no
2834/// promotion occurs.
2835QualType ASTContext::isPromotableBitField(Expr *E) {
2836 FieldDecl *Field = E->getBitField();
2837 if (!Field)
2838 return QualType();
2839
2840 QualType FT = Field->getType();
2841
2842 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2843 uint64_t BitWidth = BitWidthAP.getZExtValue();
2844 uint64_t IntSize = getTypeSize(IntTy);
2845 // GCC extension compatibility: if the bit-field size is less than or equal
2846 // to the size of int, it gets promoted no matter what its type is.
2847 // For instance, unsigned long bf : 4 gets promoted to signed int.
2848 if (BitWidth < IntSize)
2849 return IntTy;
2850
2851 if (BitWidth == IntSize)
2852 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2853
2854 // Types bigger than int are not subject to promotions, and therefore act
2855 // like the base type.
2856 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2857 // is ridiculous.
2858 return QualType();
2859}
2860
Eli Friedmana95d7572009-08-19 07:44:53 +00002861/// getPromotedIntegerType - Returns the type that Promotable will
2862/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2863/// integer type.
2864QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2865 assert(!Promotable.isNull());
2866 assert(Promotable->isPromotableIntegerType());
John McCall842aef82009-12-09 09:09:27 +00002867 if (const EnumType *ET = Promotable->getAs<EnumType>())
2868 return ET->getDecl()->getPromotionType();
Eli Friedmana95d7572009-08-19 07:44:53 +00002869 if (Promotable->isSignedIntegerType())
2870 return IntTy;
2871 uint64_t PromotableSize = getTypeSize(Promotable);
2872 uint64_t IntSize = getTypeSize(IntTy);
2873 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2874 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2875}
2876
Mike Stump1eb44332009-09-09 15:08:12 +00002877/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002878/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002879/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002880int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002881 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2882 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002883 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002884
Chris Lattnerf52ab252008-04-06 22:59:24 +00002885 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2886 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Chris Lattner7cfeb082008-04-06 23:55:33 +00002888 unsigned LHSRank = getIntegerRank(LHSC);
2889 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002890
Chris Lattner7cfeb082008-04-06 23:55:33 +00002891 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2892 if (LHSRank == RHSRank) return 0;
2893 return LHSRank > RHSRank ? 1 : -1;
2894 }
Mike Stump1eb44332009-09-09 15:08:12 +00002895
Chris Lattner7cfeb082008-04-06 23:55:33 +00002896 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2897 if (LHSUnsigned) {
2898 // If the unsigned [LHS] type is larger, return it.
2899 if (LHSRank >= RHSRank)
2900 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Chris Lattner7cfeb082008-04-06 23:55:33 +00002902 // If the signed type can represent all values of the unsigned type, it
2903 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002904 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002905 return -1;
2906 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002907
Chris Lattner7cfeb082008-04-06 23:55:33 +00002908 // If the unsigned [RHS] type is larger, return it.
2909 if (RHSRank >= LHSRank)
2910 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002911
Chris Lattner7cfeb082008-04-06 23:55:33 +00002912 // If the signed type can represent all values of the unsigned type, it
2913 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002914 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002915 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002916}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002917
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002918static RecordDecl *
2919CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC,
2920 SourceLocation L, IdentifierInfo *Id) {
2921 if (Ctx.getLangOptions().CPlusPlus)
2922 return CXXRecordDecl::Create(Ctx, TK, DC, L, Id);
2923 else
2924 return RecordDecl::Create(Ctx, TK, DC, L, Id);
2925}
2926
Mike Stump1eb44332009-09-09 15:08:12 +00002927// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002928QualType ASTContext::getCFConstantStringType() {
2929 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002930 CFConstantStringTypeDecl =
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002931 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2932 &Idents.get("NSConstantString"));
John McCall5cfa0112010-02-05 01:33:36 +00002933 CFConstantStringTypeDecl->startDefinition();
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002934
Anders Carlssonf06273f2007-11-19 00:25:30 +00002935 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Anders Carlsson71993dd2007-08-17 05:31:46 +00002937 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002938 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002939 // int flags;
2940 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002941 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002942 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002943 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002944 FieldTypes[3] = LongTy;
2945
Anders Carlsson71993dd2007-08-17 05:31:46 +00002946 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002947 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002948 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002949 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00002950 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002951 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002952 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002953 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002954 }
2955
Douglas Gregor838db382010-02-11 01:19:42 +00002956 CFConstantStringTypeDecl->completeDefinition();
Anders Carlsson71993dd2007-08-17 05:31:46 +00002957 }
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Anders Carlsson71993dd2007-08-17 05:31:46 +00002959 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002960}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002961
Douglas Gregor319ac892009-04-23 22:29:11 +00002962void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002963 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002964 assert(Rec && "Invalid CFConstantStringType");
2965 CFConstantStringTypeDecl = Rec->getDecl();
2966}
2967
Mike Stump1eb44332009-09-09 15:08:12 +00002968QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002969 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002970 ObjCFastEnumerationStateTypeDecl =
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00002971 CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2972 &Idents.get("__objcFastEnumerationState"));
John McCall5cfa0112010-02-05 01:33:36 +00002973 ObjCFastEnumerationStateTypeDecl->startDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002974
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002975 QualType FieldTypes[] = {
2976 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002977 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002978 getPointerType(UnsignedLongTy),
2979 getConstantArrayType(UnsignedLongTy,
2980 llvm::APInt(32, 5), ArrayType::Normal, 0)
2981 };
Mike Stump1eb44332009-09-09 15:08:12 +00002982
Douglas Gregor44b43212008-12-11 16:49:14 +00002983 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002984 FieldDecl *Field = FieldDecl::Create(*this,
2985 ObjCFastEnumerationStateTypeDecl,
2986 SourceLocation(), 0,
John McCalla93c9342009-12-07 02:54:59 +00002987 FieldTypes[i], /*TInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002988 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002989 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002990 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002991 }
Mike Stump1eb44332009-09-09 15:08:12 +00002992
Douglas Gregor838db382010-02-11 01:19:42 +00002993 ObjCFastEnumerationStateTypeDecl->completeDefinition();
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002994 }
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002996 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2997}
2998
Mike Stumpadaaad32009-10-20 02:12:22 +00002999QualType ASTContext::getBlockDescriptorType() {
3000 if (BlockDescriptorType)
3001 return getTagDeclType(BlockDescriptorType);
3002
3003 RecordDecl *T;
3004 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003005 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3006 &Idents.get("__block_descriptor"));
John McCall5cfa0112010-02-05 01:33:36 +00003007 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003008
3009 QualType FieldTypes[] = {
3010 UnsignedLongTy,
3011 UnsignedLongTy,
3012 };
3013
3014 const char *FieldNames[] = {
3015 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00003016 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00003017 };
3018
3019 for (size_t i = 0; i < 2; ++i) {
3020 FieldDecl *Field = FieldDecl::Create(*this,
3021 T,
3022 SourceLocation(),
3023 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003024 FieldTypes[i], /*TInfo=*/0,
Mike Stumpadaaad32009-10-20 02:12:22 +00003025 /*BitWidth=*/0,
3026 /*Mutable=*/false);
3027 T->addDecl(Field);
3028 }
3029
Douglas Gregor838db382010-02-11 01:19:42 +00003030 T->completeDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003031
3032 BlockDescriptorType = T;
3033
3034 return getTagDeclType(BlockDescriptorType);
3035}
3036
3037void ASTContext::setBlockDescriptorType(QualType T) {
3038 const RecordType *Rec = T->getAs<RecordType>();
3039 assert(Rec && "Invalid BlockDescriptorType");
3040 BlockDescriptorType = Rec->getDecl();
3041}
3042
Mike Stump083c25e2009-10-22 00:49:09 +00003043QualType ASTContext::getBlockDescriptorExtendedType() {
3044 if (BlockDescriptorExtendedType)
3045 return getTagDeclType(BlockDescriptorExtendedType);
3046
3047 RecordDecl *T;
3048 // FIXME: Needs the FlagAppleBlock bit.
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003049 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3050 &Idents.get("__block_descriptor_withcopydispose"));
John McCall5cfa0112010-02-05 01:33:36 +00003051 T->startDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003052
3053 QualType FieldTypes[] = {
3054 UnsignedLongTy,
3055 UnsignedLongTy,
3056 getPointerType(VoidPtrTy),
3057 getPointerType(VoidPtrTy)
3058 };
3059
3060 const char *FieldNames[] = {
3061 "reserved",
3062 "Size",
3063 "CopyFuncPtr",
3064 "DestroyFuncPtr"
3065 };
3066
3067 for (size_t i = 0; i < 4; ++i) {
3068 FieldDecl *Field = FieldDecl::Create(*this,
3069 T,
3070 SourceLocation(),
3071 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003072 FieldTypes[i], /*TInfo=*/0,
Mike Stump083c25e2009-10-22 00:49:09 +00003073 /*BitWidth=*/0,
3074 /*Mutable=*/false);
3075 T->addDecl(Field);
3076 }
3077
Douglas Gregor838db382010-02-11 01:19:42 +00003078 T->completeDefinition();
Mike Stump083c25e2009-10-22 00:49:09 +00003079
3080 BlockDescriptorExtendedType = T;
3081
3082 return getTagDeclType(BlockDescriptorExtendedType);
3083}
3084
3085void ASTContext::setBlockDescriptorExtendedType(QualType T) {
3086 const RecordType *Rec = T->getAs<RecordType>();
3087 assert(Rec && "Invalid BlockDescriptorType");
3088 BlockDescriptorExtendedType = Rec->getDecl();
3089}
3090
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003091bool ASTContext::BlockRequiresCopying(QualType Ty) {
3092 if (Ty->isBlockPointerType())
3093 return true;
3094 if (isObjCNSObjectType(Ty))
3095 return true;
3096 if (Ty->isObjCObjectPointerType())
3097 return true;
3098 return false;
3099}
3100
3101QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
3102 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00003103 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003104 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00003105 // unsigned int __flags;
3106 // unsigned int __size;
Mike Stump38e16272009-10-21 22:01:24 +00003107 // void *__copy_helper; // as needed
3108 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003109 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00003110 // } *
3111
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003112 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3113
3114 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00003115 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003116 llvm::SmallString<36> Name;
3117 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3118 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003119 RecordDecl *T;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003120 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3121 &Idents.get(Name.str()));
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003122 T->startDefinition();
3123 QualType Int32Ty = IntTy;
3124 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3125 QualType FieldTypes[] = {
3126 getPointerType(VoidPtrTy),
3127 getPointerType(getTagDeclType(T)),
3128 Int32Ty,
3129 Int32Ty,
3130 getPointerType(VoidPtrTy),
3131 getPointerType(VoidPtrTy),
3132 Ty
3133 };
3134
3135 const char *FieldNames[] = {
3136 "__isa",
3137 "__forwarding",
3138 "__flags",
3139 "__size",
3140 "__copy_helper",
3141 "__destroy_helper",
3142 DeclName,
3143 };
3144
3145 for (size_t i = 0; i < 7; ++i) {
3146 if (!HasCopyAndDispose && i >=4 && i <= 5)
3147 continue;
3148 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3149 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003150 FieldTypes[i], /*TInfo=*/0,
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003151 /*BitWidth=*/0, /*Mutable=*/false);
3152 T->addDecl(Field);
3153 }
3154
Douglas Gregor838db382010-02-11 01:19:42 +00003155 T->completeDefinition();
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003156
3157 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00003158}
3159
3160
3161QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00003162 bool BlockHasCopyDispose,
Mike Stumpea26cb52009-10-21 03:49:08 +00003163 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpadaaad32009-10-20 02:12:22 +00003164 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00003165 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramerf5942a42009-10-24 09:57:09 +00003166 llvm::SmallString<36> Name;
3167 llvm::raw_svector_ostream(Name) << "__block_literal_"
3168 << ++UniqueBlockParmTypeID;
Mike Stumpadaaad32009-10-20 02:12:22 +00003169 RecordDecl *T;
Anders Carlsson79cbc7d2009-11-14 21:45:58 +00003170 T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
3171 &Idents.get(Name.str()));
John McCall5cfa0112010-02-05 01:33:36 +00003172 T->startDefinition();
Mike Stumpadaaad32009-10-20 02:12:22 +00003173 QualType FieldTypes[] = {
3174 getPointerType(VoidPtrTy),
3175 IntTy,
3176 IntTy,
3177 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00003178 (BlockHasCopyDispose ?
3179 getPointerType(getBlockDescriptorExtendedType()) :
3180 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00003181 };
3182
3183 const char *FieldNames[] = {
3184 "__isa",
3185 "__flags",
3186 "__reserved",
3187 "__FuncPtr",
3188 "__descriptor"
3189 };
3190
3191 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00003192 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00003193 &Idents.get(FieldNames[i]),
John McCalla93c9342009-12-07 02:54:59 +00003194 FieldTypes[i], /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003195 /*BitWidth=*/0, /*Mutable=*/false);
3196 T->addDecl(Field);
3197 }
3198
3199 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
3200 const Expr *E = BlockDeclRefDecls[i];
3201 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
3202 clang::IdentifierInfo *Name = 0;
3203 if (BDRE) {
3204 const ValueDecl *D = BDRE->getDecl();
3205 Name = &Idents.get(D->getName());
3206 }
3207 QualType FieldType = E->getType();
3208
3209 if (BDRE && BDRE->isByRef())
Mike Stumpaf7b44d2009-10-21 18:16:27 +00003210 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
3211 FieldType);
Mike Stumpea26cb52009-10-21 03:49:08 +00003212
3213 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
John McCalla93c9342009-12-07 02:54:59 +00003214 Name, FieldType, /*TInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00003215 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpadaaad32009-10-20 02:12:22 +00003216 T->addDecl(Field);
3217 }
3218
Douglas Gregor838db382010-02-11 01:19:42 +00003219 T->completeDefinition();
Mike Stumpea26cb52009-10-21 03:49:08 +00003220
3221 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00003222}
3223
Douglas Gregor319ac892009-04-23 22:29:11 +00003224void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00003225 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003226 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3227 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3228}
3229
Anders Carlssone8c49532007-10-29 06:33:42 +00003230// This returns true if a type has been typedefed to BOOL:
3231// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00003232static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00003233 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00003234 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3235 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00003236
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003237 return false;
3238}
3239
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003240/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003241/// purpose.
Ken Dyckaa8741a2010-01-11 19:19:56 +00003242CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) {
Ken Dyck199c3d62010-01-11 17:06:35 +00003243 CharUnits sz = getTypeSizeInChars(type);
Mike Stump1eb44332009-09-09 15:08:12 +00003244
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003245 // Make all integer and enum types at least as large as an int
Ken Dyck199c3d62010-01-11 17:06:35 +00003246 if (sz.isPositive() && type->isIntegralType())
3247 sz = std::max(sz, getTypeSizeInChars(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003248 // Treat arrays as pointers, since that's how they're passed in.
3249 else if (type->isArrayType())
Ken Dyck199c3d62010-01-11 17:06:35 +00003250 sz = getTypeSizeInChars(VoidPtrTy);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003251 return sz;
Ken Dyck199c3d62010-01-11 17:06:35 +00003252}
3253
3254static inline
3255std::string charUnitsToString(const CharUnits &CU) {
3256 return llvm::itostr(CU.getQuantity());
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003257}
3258
David Chisnall5e530af2009-11-17 19:33:30 +00003259/// getObjCEncodingForBlockDecl - Return the encoded type for this method
3260/// declaration.
3261void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr,
3262 std::string& S) {
3263 const BlockDecl *Decl = Expr->getBlockDecl();
3264 QualType BlockTy =
3265 Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
3266 // Encode result type.
3267 getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S);
3268 // Compute size of all parameters.
3269 // Start with computing size of a pointer in number of bytes.
3270 // FIXME: There might(should) be a better way of doing this computation!
3271 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003272 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3273 CharUnits ParmOffset = PtrSize;
David Chisnall5e530af2009-11-17 19:33:30 +00003274 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3275 E = Decl->param_end(); PI != E; ++PI) {
3276 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003277 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003278 assert (sz.isPositive() && "BlockExpr - Incomplete param type");
David Chisnall5e530af2009-11-17 19:33:30 +00003279 ParmOffset += sz;
3280 }
3281 // Size of the argument frame
Ken Dyck199c3d62010-01-11 17:06:35 +00003282 S += charUnitsToString(ParmOffset);
David Chisnall5e530af2009-11-17 19:33:30 +00003283 // Block pointer and offset.
3284 S += "@?0";
3285 ParmOffset = PtrSize;
3286
3287 // Argument types.
3288 ParmOffset = PtrSize;
3289 for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
3290 Decl->param_end(); PI != E; ++PI) {
3291 ParmVarDecl *PVDecl = *PI;
3292 QualType PType = PVDecl->getOriginalType();
3293 if (const ArrayType *AT =
3294 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3295 // Use array's original type only if it has known number of
3296 // elements.
3297 if (!isa<ConstantArrayType>(AT))
3298 PType = PVDecl->getType();
3299 } else if (PType->isFunctionType())
3300 PType = PVDecl->getType();
3301 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003302 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003303 ParmOffset += getObjCEncodingTypeSize(PType);
David Chisnall5e530af2009-11-17 19:33:30 +00003304 }
3305}
3306
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003307/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003308/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003309void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003310 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003311 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003312 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003313 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003314 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003315 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003316 // Compute size of all parameters.
3317 // Start with computing size of a pointer in number of bytes.
3318 // FIXME: There might(should) be a better way of doing this computation!
3319 SourceLocation Loc;
Ken Dyck199c3d62010-01-11 17:06:35 +00003320 CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003321 // The first two arguments (self and _cmd) are pointers; account for
3322 // their size.
Ken Dyck199c3d62010-01-11 17:06:35 +00003323 CharUnits ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003324 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3325 E = Decl->param_end(); PI != E; ++PI) {
3326 QualType PType = (*PI)->getType();
Ken Dyckaa8741a2010-01-11 19:19:56 +00003327 CharUnits sz = getObjCEncodingTypeSize(PType);
Ken Dyck199c3d62010-01-11 17:06:35 +00003328 assert (sz.isPositive() &&
3329 "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003330 ParmOffset += sz;
3331 }
Ken Dyck199c3d62010-01-11 17:06:35 +00003332 S += charUnitsToString(ParmOffset);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003333 S += "@0:";
Ken Dyck199c3d62010-01-11 17:06:35 +00003334 S += charUnitsToString(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003335
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003336 // Argument types.
3337 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003338 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3339 E = Decl->param_end(); PI != E; ++PI) {
3340 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003341 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003342 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003343 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3344 // Use array's original type only if it has known number of
3345 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003346 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003347 PType = PVDecl->getType();
3348 } else if (PType->isFunctionType())
3349 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003350 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003351 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003352 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003353 getObjCEncodingForType(PType, S);
Ken Dyck199c3d62010-01-11 17:06:35 +00003354 S += charUnitsToString(ParmOffset);
Ken Dyckaa8741a2010-01-11 19:19:56 +00003355 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003356 }
3357}
3358
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003359/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003360/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003361/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3362/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003363/// Property attributes are stored as a comma-delimited C string. The simple
3364/// attributes readonly and bycopy are encoded as single characters. The
3365/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3366/// encoded as single characters, followed by an identifier. Property types
3367/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003368/// these attributes are defined by the following enumeration:
3369/// @code
3370/// enum PropertyAttributes {
3371/// kPropertyReadOnly = 'R', // property is read-only.
3372/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3373/// kPropertyByref = '&', // property is a reference to the value last assigned
3374/// kPropertyDynamic = 'D', // property is dynamic
3375/// kPropertyGetter = 'G', // followed by getter selector name
3376/// kPropertySetter = 'S', // followed by setter selector name
3377/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3378/// kPropertyType = 't' // followed by old-style type encoding.
3379/// kPropertyWeak = 'W' // 'weak' property
3380/// kPropertyStrong = 'P' // property GC'able
3381/// kPropertyNonAtomic = 'N' // property non-atomic
3382/// };
3383/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003384void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003385 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003386 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003387 // Collect information from the property implementation decl(s).
3388 bool Dynamic = false;
3389 ObjCPropertyImplDecl *SynthesizePID = 0;
3390
3391 // FIXME: Duplicated code due to poor abstraction.
3392 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003393 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003394 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3395 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003396 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003397 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003398 ObjCPropertyImplDecl *PID = *i;
3399 if (PID->getPropertyDecl() == PD) {
3400 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3401 Dynamic = true;
3402 } else {
3403 SynthesizePID = PID;
3404 }
3405 }
3406 }
3407 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003408 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003409 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003410 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003411 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003412 ObjCPropertyImplDecl *PID = *i;
3413 if (PID->getPropertyDecl() == PD) {
3414 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3415 Dynamic = true;
3416 } else {
3417 SynthesizePID = PID;
3418 }
3419 }
Mike Stump1eb44332009-09-09 15:08:12 +00003420 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003421 }
3422 }
3423
3424 // FIXME: This is not very efficient.
3425 S = "T";
3426
3427 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003428 // GCC has some special rules regarding encoding of properties which
3429 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003430 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003431 true /* outermost type */,
3432 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003433
3434 if (PD->isReadOnly()) {
3435 S += ",R";
3436 } else {
3437 switch (PD->getSetterKind()) {
3438 case ObjCPropertyDecl::Assign: break;
3439 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003440 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003441 }
3442 }
3443
3444 // It really isn't clear at all what this means, since properties
3445 // are "dynamic by default".
3446 if (Dynamic)
3447 S += ",D";
3448
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003449 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3450 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003451
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003452 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3453 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003454 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003455 }
3456
3457 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3458 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003459 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003460 }
3461
3462 if (SynthesizePID) {
3463 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3464 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003465 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003466 }
3467
3468 // FIXME: OBJCGC: weak & strong
3469}
3470
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003471/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003472/// Another legacy compatibility encoding: 32-bit longs are encoded as
3473/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003474/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3475///
3476void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003477 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003478 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003479 if (BT->getKind() == BuiltinType::ULong &&
3480 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003481 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003482 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003483 if (BT->getKind() == BuiltinType::Long &&
3484 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003485 PointeeTy = IntTy;
3486 }
3487 }
3488}
3489
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003490void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003491 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003492 // We follow the behavior of gcc, expanding structures which are
3493 // directly pointed to, and expanding embedded structures. Note that
3494 // these rules are sufficient to prevent recursive encoding of the
3495 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003496 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003497 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003498}
3499
Mike Stump1eb44332009-09-09 15:08:12 +00003500static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003501 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003502 const Expr *E = FD->getBitWidth();
3503 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3504 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00003505 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003506 S += 'b';
3507 S += llvm::utostr(N);
3508}
3509
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003510// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003511void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3512 bool ExpandPointedToStructures,
3513 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003514 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003515 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003516 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00003517 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003518 if (FD && FD->isBitField())
3519 return EncodeBitField(this, S, FD);
3520 char encoding;
3521 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003522 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003523 case BuiltinType::Void: encoding = 'v'; break;
3524 case BuiltinType::Bool: encoding = 'B'; break;
3525 case BuiltinType::Char_U:
3526 case BuiltinType::UChar: encoding = 'C'; break;
3527 case BuiltinType::UShort: encoding = 'S'; break;
3528 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003529 case BuiltinType::ULong:
3530 encoding =
3531 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00003532 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003533 case BuiltinType::UInt128: encoding = 'T'; break;
3534 case BuiltinType::ULongLong: encoding = 'Q'; break;
3535 case BuiltinType::Char_S:
3536 case BuiltinType::SChar: encoding = 'c'; break;
3537 case BuiltinType::Short: encoding = 's'; break;
3538 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003539 case BuiltinType::Long:
3540 encoding =
3541 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003542 break;
3543 case BuiltinType::LongLong: encoding = 'q'; break;
3544 case BuiltinType::Int128: encoding = 't'; break;
3545 case BuiltinType::Float: encoding = 'f'; break;
3546 case BuiltinType::Double: encoding = 'd'; break;
3547 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003548 }
Mike Stump1eb44332009-09-09 15:08:12 +00003549
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003550 S += encoding;
3551 return;
3552 }
Mike Stump1eb44332009-09-09 15:08:12 +00003553
John McCall183700f2009-09-21 23:43:11 +00003554 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003555 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003556 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003557 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003558 return;
3559 }
Fariborz Jahanian60bce3e2009-11-23 20:40:50 +00003560
Ted Kremenek6217b802009-07-29 21:53:49 +00003561 if (const PointerType *PT = T->getAs<PointerType>()) {
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003562 if (PT->isObjCSelType()) {
3563 S += ':';
3564 return;
3565 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003566 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanian8d2c0a92009-11-30 18:43:52 +00003567
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003568 bool isReadOnly = false;
3569 // For historical/compatibility reasons, the read-only qualifier of the
3570 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3571 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003572 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003573 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003574 if (OutermostType && T.isConstQualified()) {
3575 isReadOnly = true;
3576 S += 'r';
3577 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003578 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003579 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003580 while (P->getAs<PointerType>())
3581 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003582 if (P.isConstQualified()) {
3583 isReadOnly = true;
3584 S += 'r';
3585 }
3586 }
3587 if (isReadOnly) {
3588 // Another legacy compatibility encoding. Some ObjC qualifier and type
3589 // combinations need to be rearranged.
3590 // Rewrite "in const" from "nr" to "rn"
3591 const char * s = S.c_str();
3592 int len = S.length();
3593 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3594 std::string replace = "rn";
3595 S.replace(S.end()-2, S.end(), replace);
3596 }
3597 }
Mike Stump1eb44332009-09-09 15:08:12 +00003598
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003599 if (PointeeTy->isCharType()) {
3600 // char pointer types should be encoded as '*' unless it is a
3601 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003602 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003603 S += '*';
3604 return;
3605 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003606 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003607 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3608 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3609 S += '#';
3610 return;
3611 }
3612 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3613 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3614 S += '@';
3615 return;
3616 }
3617 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003618 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003619 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003620 getLegacyIntegralTypeEncoding(PointeeTy);
3621
Mike Stump1eb44332009-09-09 15:08:12 +00003622 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003623 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003624 return;
3625 }
Mike Stump1eb44332009-09-09 15:08:12 +00003626
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003627 if (const ArrayType *AT =
3628 // Ignore type qualifiers etc.
3629 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003630 if (isa<IncompleteArrayType>(AT)) {
3631 // Incomplete arrays are encoded as a pointer to the array element.
3632 S += '^';
3633
Mike Stump1eb44332009-09-09 15:08:12 +00003634 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003635 false, ExpandStructures, FD);
3636 } else {
3637 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003638
Anders Carlsson559a8332009-02-22 01:38:57 +00003639 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3640 S += llvm::utostr(CAT->getSize().getZExtValue());
3641 else {
3642 //Variable length arrays are encoded as a regular array with 0 elements.
3643 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3644 S += '0';
3645 }
Mike Stump1eb44332009-09-09 15:08:12 +00003646
3647 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003648 false, ExpandStructures, FD);
3649 S += ']';
3650 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003651 return;
3652 }
Mike Stump1eb44332009-09-09 15:08:12 +00003653
John McCall183700f2009-09-21 23:43:11 +00003654 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003655 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003656 return;
3657 }
Mike Stump1eb44332009-09-09 15:08:12 +00003658
Ted Kremenek6217b802009-07-29 21:53:49 +00003659 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003660 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003661 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003662 // Anonymous structures print as '?'
3663 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3664 S += II->getName();
3665 } else {
3666 S += '?';
3667 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003668 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003669 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003670 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3671 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003672 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003673 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003674 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003675 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003676 S += '"';
3677 }
Mike Stump1eb44332009-09-09 15:08:12 +00003678
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003679 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003680 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003681 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003682 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003683 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003684 QualType qt = Field->getType();
3685 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003686 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003687 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003688 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003689 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003690 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003691 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003692 return;
3693 }
Mike Stump1eb44332009-09-09 15:08:12 +00003694
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003695 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003696 if (FD && FD->isBitField())
3697 EncodeBitField(this, S, FD);
3698 else
3699 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003700 return;
3701 }
Mike Stump1eb44332009-09-09 15:08:12 +00003702
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003703 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003704 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003705 return;
3706 }
Mike Stump1eb44332009-09-09 15:08:12 +00003707
John McCall0953e762009-09-24 19:53:00 +00003708 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003709 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003710 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003711 S += '{';
3712 const IdentifierInfo *II = OI->getIdentifier();
3713 S += II->getName();
3714 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003715 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003716 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003717 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003718 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003719 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003720 RecFields[i]);
3721 else
Mike Stump1eb44332009-09-09 15:08:12 +00003722 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003723 FD);
3724 }
3725 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003726 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003727 }
Mike Stump1eb44332009-09-09 15:08:12 +00003728
John McCall183700f2009-09-21 23:43:11 +00003729 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003730 if (OPT->isObjCIdType()) {
3731 S += '@';
3732 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003733 }
Mike Stump1eb44332009-09-09 15:08:12 +00003734
Steve Naroff27d20a22009-10-28 22:03:49 +00003735 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3736 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3737 // Since this is a binary compatibility issue, need to consult with runtime
3738 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff14108da2009-07-10 23:34:53 +00003739 S += '#';
3740 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003741 }
Mike Stump1eb44332009-09-09 15:08:12 +00003742
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003743 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003744 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003745 ExpandPointedToStructures,
3746 ExpandStructures, FD);
3747 if (FD || EncodingProperty) {
3748 // Note that we do extended encoding of protocol qualifer list
3749 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003750 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003751 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3752 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003753 S += '<';
3754 S += (*I)->getNameAsString();
3755 S += '>';
3756 }
3757 S += '"';
3758 }
3759 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003760 }
Mike Stump1eb44332009-09-09 15:08:12 +00003761
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003762 QualType PointeeTy = OPT->getPointeeType();
3763 if (!EncodingProperty &&
3764 isa<TypedefType>(PointeeTy.getTypePtr())) {
3765 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003766 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003767 // {...};
3768 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003769 getObjCEncodingForTypeImpl(PointeeTy, S,
3770 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003771 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003772 return;
3773 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003774
3775 S += '@';
Steve Naroff27d20a22009-10-28 22:03:49 +00003776 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003777 S += '"';
Steve Naroff27d20a22009-10-28 22:03:49 +00003778 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003779 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3780 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003781 S += '<';
3782 S += (*I)->getNameAsString();
3783 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003784 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003785 S += '"';
3786 }
3787 return;
3788 }
Mike Stump1eb44332009-09-09 15:08:12 +00003789
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003790 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003791}
3792
Mike Stump1eb44332009-09-09 15:08:12 +00003793void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003794 std::string& S) const {
3795 if (QT & Decl::OBJC_TQ_In)
3796 S += 'n';
3797 if (QT & Decl::OBJC_TQ_Inout)
3798 S += 'N';
3799 if (QT & Decl::OBJC_TQ_Out)
3800 S += 'o';
3801 if (QT & Decl::OBJC_TQ_Bycopy)
3802 S += 'O';
3803 if (QT & Decl::OBJC_TQ_Byref)
3804 S += 'R';
3805 if (QT & Decl::OBJC_TQ_Oneway)
3806 S += 'V';
3807}
3808
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003809void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003810 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003811
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003812 BuiltinVaListType = T;
3813}
3814
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003815void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003816 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003817}
3818
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003819void ASTContext::setObjCSelType(QualType T) {
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00003820 ObjCSelTypedefType = T;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003821}
3822
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003823void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003824 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003825}
3826
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003827void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003828 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003829}
3830
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003831void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003832 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003833 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003834
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003835 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003836}
3837
John McCall0bd6feb2009-12-02 08:04:21 +00003838/// \brief Retrieve the template name that corresponds to a non-empty
3839/// lookup.
John McCalleec51cf2010-01-20 00:46:10 +00003840TemplateName ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
3841 UnresolvedSetIterator End) {
John McCall0bd6feb2009-12-02 08:04:21 +00003842 unsigned size = End - Begin;
3843 assert(size > 1 && "set is not overloaded!");
3844
3845 void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
3846 size * sizeof(FunctionTemplateDecl*));
3847 OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
3848
3849 NamedDecl **Storage = OT->getStorage();
John McCalleec51cf2010-01-20 00:46:10 +00003850 for (UnresolvedSetIterator I = Begin; I != End; ++I) {
John McCall0bd6feb2009-12-02 08:04:21 +00003851 NamedDecl *D = *I;
3852 assert(isa<FunctionTemplateDecl>(D) ||
3853 (isa<UsingShadowDecl>(D) &&
3854 isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
3855 *Storage++ = D;
3856 }
3857
3858 return TemplateName(OT);
3859}
3860
Douglas Gregor7532dc62009-03-30 22:58:21 +00003861/// \brief Retrieve the template name that represents a qualified
3862/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003863TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003864 bool TemplateKeyword,
3865 TemplateDecl *Template) {
Douglas Gregor789b1f62010-02-04 18:10:26 +00003866 // FIXME: Canonicalization?
Douglas Gregor7532dc62009-03-30 22:58:21 +00003867 llvm::FoldingSetNodeID ID;
3868 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3869
3870 void *InsertPos = 0;
3871 QualifiedTemplateName *QTN =
3872 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3873 if (!QTN) {
3874 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3875 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3876 }
3877
3878 return TemplateName(QTN);
3879}
3880
3881/// \brief Retrieve the template name that represents a dependent
3882/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003883TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003884 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003885 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003886 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003887
3888 llvm::FoldingSetNodeID ID;
3889 DependentTemplateName::Profile(ID, NNS, Name);
3890
3891 void *InsertPos = 0;
3892 DependentTemplateName *QTN =
3893 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3894
3895 if (QTN)
3896 return TemplateName(QTN);
3897
3898 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3899 if (CanonNNS == NNS) {
3900 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3901 } else {
3902 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3903 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00003904 DependentTemplateName *CheckQTN =
3905 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3906 assert(!CheckQTN && "Dependent type name canonicalization broken");
3907 (void)CheckQTN;
Douglas Gregor7532dc62009-03-30 22:58:21 +00003908 }
3909
3910 DependentTemplateNames.InsertNode(QTN, InsertPos);
3911 return TemplateName(QTN);
3912}
3913
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003914/// \brief Retrieve the template name that represents a dependent
3915/// template name such as \c MetaFun::template operator+.
3916TemplateName
3917ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3918 OverloadedOperatorKind Operator) {
3919 assert((!NNS || NNS->isDependent()) &&
3920 "Nested name specifier must be dependent");
3921
3922 llvm::FoldingSetNodeID ID;
3923 DependentTemplateName::Profile(ID, NNS, Operator);
3924
3925 void *InsertPos = 0;
Douglas Gregor789b1f62010-02-04 18:10:26 +00003926 DependentTemplateName *QTN
3927 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003928
3929 if (QTN)
3930 return TemplateName(QTN);
3931
3932 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3933 if (CanonNNS == NNS) {
3934 QTN = new (*this,4) DependentTemplateName(NNS, Operator);
3935 } else {
3936 TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
3937 QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
Douglas Gregor789b1f62010-02-04 18:10:26 +00003938
3939 DependentTemplateName *CheckQTN
3940 = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3941 assert(!CheckQTN && "Dependent template name canonicalization broken");
3942 (void)CheckQTN;
Douglas Gregorca1bdd72009-11-04 00:56:37 +00003943 }
3944
3945 DependentTemplateNames.InsertNode(QTN, InsertPos);
3946 return TemplateName(QTN);
3947}
3948
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003949/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003950/// TargetInfo, produce the corresponding type. The unsigned @p Type
3951/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00003952CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003953 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00003954 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003955 case TargetInfo::SignedShort: return ShortTy;
3956 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3957 case TargetInfo::SignedInt: return IntTy;
3958 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3959 case TargetInfo::SignedLong: return LongTy;
3960 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3961 case TargetInfo::SignedLongLong: return LongLongTy;
3962 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3963 }
3964
3965 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00003966 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003967}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003968
3969//===----------------------------------------------------------------------===//
3970// Type Predicates.
3971//===----------------------------------------------------------------------===//
3972
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003973/// isObjCNSObjectType - Return true if this is an NSObject object using
3974/// NSObject attribute on a c-style pointer type.
3975/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003976/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003977///
3978bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3979 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3980 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003981 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003982 return true;
3983 }
Mike Stump1eb44332009-09-09 15:08:12 +00003984 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003985}
3986
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003987/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3988/// garbage collection attribute.
3989///
John McCall0953e762009-09-24 19:53:00 +00003990Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3991 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003992 if (getLangOptions().ObjC1 &&
3993 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003994 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003995 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003996 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003997 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003998 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003999 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004000 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004001 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00004002 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00004003 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00004004 // Non-pointers have none gc'able attribute regardless of the attribute
4005 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00004006 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00004007 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004008 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00004009 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00004010}
4011
Chris Lattner6ac46a42008-04-07 06:51:04 +00004012//===----------------------------------------------------------------------===//
4013// Type Compatibility Testing
4014//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00004015
Mike Stump1eb44332009-09-09 15:08:12 +00004016/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004017/// compatible.
4018static bool areCompatVectorTypes(const VectorType *LHS,
4019 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00004020 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00004021 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00004022 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00004023}
4024
Steve Naroff4084c302009-07-23 01:01:38 +00004025//===----------------------------------------------------------------------===//
4026// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
4027//===----------------------------------------------------------------------===//
4028
4029/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
4030/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004031bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
4032 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00004033 if (lProto == rProto)
4034 return true;
4035 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
4036 E = rProto->protocol_end(); PI != E; ++PI)
4037 if (ProtocolCompatibleWithProtocol(lProto, *PI))
4038 return true;
4039 return false;
4040}
4041
Steve Naroff4084c302009-07-23 01:01:38 +00004042/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
4043/// return true if lhs's protocols conform to rhs's protocol; false
4044/// otherwise.
4045bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
4046 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
4047 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
4048 return false;
4049}
4050
4051/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
4052/// ObjCQualifiedIDType.
4053bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
4054 bool compare) {
4055 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00004056 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004057 lhs->isObjCIdType() || lhs->isObjCClassType())
4058 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004059 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00004060 rhs->isObjCIdType() || rhs->isObjCClassType())
4061 return true;
4062
4063 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00004064 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004065
Steve Naroff4084c302009-07-23 01:01:38 +00004066 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004067
Steve Naroff4084c302009-07-23 01:01:38 +00004068 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004069 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00004070 // make sure we check the class hierarchy.
4071 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4072 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4073 E = lhsQID->qual_end(); I != E; ++I) {
4074 // when comparing an id<P> on lhs with a static type on rhs,
4075 // see if static class implements all of id's protocols, directly or
4076 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004077 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00004078 return false;
4079 }
4080 }
4081 // If there are no qualifiers and no interface, we have an 'id'.
4082 return true;
4083 }
Mike Stump1eb44332009-09-09 15:08:12 +00004084 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004085 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4086 E = lhsQID->qual_end(); I != E; ++I) {
4087 ObjCProtocolDecl *lhsProto = *I;
4088 bool match = false;
4089
4090 // when comparing an id<P> on lhs with a static type on rhs,
4091 // see if static class implements all of id's protocols, directly or
4092 // through its super class and categories.
4093 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4094 E = rhsOPT->qual_end(); J != E; ++J) {
4095 ObjCProtocolDecl *rhsProto = *J;
4096 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4097 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4098 match = true;
4099 break;
4100 }
4101 }
Mike Stump1eb44332009-09-09 15:08:12 +00004102 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00004103 // make sure we check the class hierarchy.
4104 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
4105 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4106 E = lhsQID->qual_end(); I != E; ++I) {
4107 // when comparing an id<P> on lhs with a static type on rhs,
4108 // see if static class implements all of id's protocols, directly or
4109 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004110 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004111 match = true;
4112 break;
4113 }
4114 }
4115 }
4116 if (!match)
4117 return false;
4118 }
Mike Stump1eb44332009-09-09 15:08:12 +00004119
Steve Naroff4084c302009-07-23 01:01:38 +00004120 return true;
4121 }
Mike Stump1eb44332009-09-09 15:08:12 +00004122
Steve Naroff4084c302009-07-23 01:01:38 +00004123 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
4124 assert(rhsQID && "One of the LHS/RHS should be id<x>");
4125
Mike Stump1eb44332009-09-09 15:08:12 +00004126 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00004127 lhs->getAsObjCInterfacePointerType()) {
4128 if (lhsOPT->qual_empty()) {
4129 bool match = false;
4130 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
4131 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
4132 E = rhsQID->qual_end(); I != E; ++I) {
4133 // when comparing an id<P> on lhs with a static type on rhs,
4134 // see if static class implements all of id's protocols, directly or
4135 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00004136 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00004137 match = true;
4138 break;
4139 }
4140 }
4141 if (!match)
4142 return false;
4143 }
4144 return true;
4145 }
Mike Stump1eb44332009-09-09 15:08:12 +00004146 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00004147 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
4148 E = lhsOPT->qual_end(); I != E; ++I) {
4149 ObjCProtocolDecl *lhsProto = *I;
4150 bool match = false;
4151
4152 // when comparing an id<P> on lhs with a static type on rhs,
4153 // see if static class implements all of id's protocols, directly or
4154 // through its super class and categories.
4155 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
4156 E = rhsQID->qual_end(); J != E; ++J) {
4157 ObjCProtocolDecl *rhsProto = *J;
4158 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
4159 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
4160 match = true;
4161 break;
4162 }
4163 }
4164 if (!match)
4165 return false;
4166 }
4167 return true;
4168 }
4169 return false;
4170}
4171
Eli Friedman3d815e72008-08-22 00:56:42 +00004172/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00004173/// compatible for assignment from RHS to LHS. This handles validation of any
4174/// protocol qualifiers on the LHS or RHS.
4175///
Steve Naroff14108da2009-07-10 23:34:53 +00004176bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
4177 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00004178 // If either type represents the built-in 'id' or 'Class' types, return true.
4179 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00004180 return true;
4181
Steve Naroff4084c302009-07-23 01:01:38 +00004182 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00004183 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
4184 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00004185 false);
4186
Steve Naroff14108da2009-07-10 23:34:53 +00004187 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4188 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00004189 if (LHS && RHS) // We have 2 user-defined types.
4190 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00004191
Steve Naroff4084c302009-07-23 01:01:38 +00004192 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004193}
4194
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004195/// getIntersectionOfProtocols - This routine finds the intersection of set
4196/// of protocols inherited from two distinct objective-c pointer objects.
4197/// It is used to build composite qualifier list of the composite type of
4198/// the conditional expression involving two objective-c pointer objects.
4199static
4200void getIntersectionOfProtocols(ASTContext &Context,
4201 const ObjCObjectPointerType *LHSOPT,
4202 const ObjCObjectPointerType *RHSOPT,
4203 llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
4204
4205 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4206 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4207
4208 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
4209 unsigned LHSNumProtocols = LHS->getNumProtocols();
4210 if (LHSNumProtocols > 0)
4211 InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
4212 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004213 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
4214 Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols);
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004215 InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
4216 LHSInheritedProtocols.end());
4217 }
4218
4219 unsigned RHSNumProtocols = RHS->getNumProtocols();
4220 if (RHSNumProtocols > 0) {
4221 ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin();
4222 for (unsigned i = 0; i < RHSNumProtocols; ++i)
4223 if (InheritedProtocolSet.count(RHSProtocols[i]))
4224 IntersectionOfProtocols.push_back(RHSProtocols[i]);
4225 }
4226 else {
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004227 llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004228 Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols);
Fariborz Jahanian432a8892010-02-12 19:27:33 +00004229 for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
4230 RHSInheritedProtocols.begin(),
4231 E = RHSInheritedProtocols.end(); I != E; ++I)
4232 if (InheritedProtocolSet.count((*I)))
4233 IntersectionOfProtocols.push_back((*I));
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004234 }
4235}
4236
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004237/// areCommonBaseCompatible - Returns common base class of the two classes if
4238/// one found. Note that this is O'2 algorithm. But it will be called as the
4239/// last type comparison in a ?-exp of ObjC pointer types before a
4240/// warning is issued. So, its invokation is extremely rare.
4241QualType ASTContext::areCommonBaseCompatible(
4242 const ObjCObjectPointerType *LHSOPT,
4243 const ObjCObjectPointerType *RHSOPT) {
4244 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
4245 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
4246 if (!LHS || !RHS)
4247 return QualType();
4248
4249 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
4250 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
4251 LHS = LHSTy->getAs<ObjCInterfaceType>();
Fariborz Jahaniane23fa2d2009-10-30 01:13:23 +00004252 if (canAssignObjCInterfaces(LHS, RHS)) {
4253 llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols;
4254 getIntersectionOfProtocols(*this,
4255 LHSOPT, RHSOPT, IntersectionOfProtocols);
4256 if (IntersectionOfProtocols.empty())
4257 LHSTy = getObjCObjectPointerType(LHSTy);
4258 else
4259 LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0],
4260 IntersectionOfProtocols.size());
4261 return LHSTy;
4262 }
Fariborz Jahaniandb07b3f2009-10-27 23:02:38 +00004263 }
4264
4265 return QualType();
4266}
4267
Eli Friedman3d815e72008-08-22 00:56:42 +00004268bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
4269 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00004270 // Verify that the base decls are compatible: the RHS must be a subclass of
4271 // the LHS.
4272 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
4273 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004274
Chris Lattner6ac46a42008-04-07 06:51:04 +00004275 // RHS must have a superset of the protocols in the LHS. If the LHS is not
4276 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004277 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004278 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004279
Chris Lattner6ac46a42008-04-07 06:51:04 +00004280 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
4281 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004282 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00004283 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00004284
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004285 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
4286 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004287 LHSPI != LHSPE; LHSPI++) {
4288 bool RHSImplementsProtocol = false;
4289
4290 // If the RHS doesn't implement the protocol on the left, the types
4291 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00004292 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00004293 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00004294 RHSPI != RHSPE; RHSPI++) {
4295 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004296 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00004297 break;
4298 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004299 }
4300 // FIXME: For better diagnostics, consider passing back the protocol name.
4301 if (!RHSImplementsProtocol)
4302 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004303 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00004304 // The RHS implements all protocols listed on the LHS.
4305 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00004306}
4307
Steve Naroff389bf462009-02-12 17:52:19 +00004308bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
4309 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00004310 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
4311 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00004312
Steve Naroff14108da2009-07-10 23:34:53 +00004313 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00004314 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00004315
4316 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
4317 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00004318}
4319
Mike Stump1eb44332009-09-09 15:08:12 +00004320/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00004321/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00004322/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00004323/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00004324bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004325 if (getLangOptions().CPlusPlus)
4326 return hasSameType(LHS, RHS);
4327
Eli Friedman3d815e72008-08-22 00:56:42 +00004328 return !mergeTypes(LHS, RHS).isNull();
4329}
4330
4331QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00004332 const FunctionType *lbase = lhs->getAs<FunctionType>();
4333 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00004334 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
4335 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00004336 bool allLTypes = true;
4337 bool allRTypes = true;
4338
4339 // Check return type
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004340 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
Eli Friedman3d815e72008-08-22 00:56:42 +00004341 if (retType.isNull()) return QualType();
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004342 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
Chris Lattner61710852008-10-05 17:34:18 +00004343 allLTypes = false;
Fariborz Jahanian6de8b622010-02-10 00:32:12 +00004344 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
Chris Lattner61710852008-10-05 17:34:18 +00004345 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00004346 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00004347 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
4348 if (NoReturn != lbase->getNoReturnAttr())
4349 allLTypes = false;
4350 if (NoReturn != rbase->getNoReturnAttr())
4351 allRTypes = false;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004352 CallingConv lcc = lbase->getCallConv();
4353 CallingConv rcc = rbase->getCallConv();
4354 // Compatible functions must have compatible calling conventions
John McCall04a67a62010-02-05 21:31:56 +00004355 if (!isSameCallConv(lcc, rcc))
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004356 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00004357
Eli Friedman3d815e72008-08-22 00:56:42 +00004358 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00004359 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
4360 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004361 unsigned lproto_nargs = lproto->getNumArgs();
4362 unsigned rproto_nargs = rproto->getNumArgs();
4363
4364 // Compatible functions must have the same number of arguments
4365 if (lproto_nargs != rproto_nargs)
4366 return QualType();
4367
4368 // Variadic and non-variadic functions aren't compatible
4369 if (lproto->isVariadic() != rproto->isVariadic())
4370 return QualType();
4371
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00004372 if (lproto->getTypeQuals() != rproto->getTypeQuals())
4373 return QualType();
4374
Eli Friedman3d815e72008-08-22 00:56:42 +00004375 // Check argument compatibility
4376 llvm::SmallVector<QualType, 10> types;
4377 for (unsigned i = 0; i < lproto_nargs; i++) {
4378 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
4379 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
4380 QualType argtype = mergeTypes(largtype, rargtype);
4381 if (argtype.isNull()) return QualType();
4382 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00004383 if (getCanonicalType(argtype) != getCanonicalType(largtype))
4384 allLTypes = false;
4385 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
4386 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00004387 }
4388 if (allLTypes) return lhs;
4389 if (allRTypes) return rhs;
4390 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00004391 lproto->isVariadic(), lproto->getTypeQuals(),
Douglas Gregor1bf40242010-02-12 17:17:28 +00004392 false, false, 0, 0, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004393 }
4394
4395 if (lproto) allRTypes = false;
4396 if (rproto) allLTypes = false;
4397
Douglas Gregor72564e72009-02-26 23:50:07 +00004398 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00004399 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00004400 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00004401 if (proto->isVariadic()) return QualType();
4402 // Check that the types are compatible with the types that
4403 // would result from default argument promotions (C99 6.7.5.3p15).
4404 // The only types actually affected are promotable integer
4405 // types and floats, which would be passed as a different
4406 // type depending on whether the prototype is visible.
4407 unsigned proto_nargs = proto->getNumArgs();
4408 for (unsigned i = 0; i < proto_nargs; ++i) {
4409 QualType argTy = proto->getArgType(i);
Douglas Gregorb0f8eac2010-02-03 19:27:29 +00004410
4411 // Look at the promotion type of enum types, since that is the type used
4412 // to pass enum values.
4413 if (const EnumType *Enum = argTy->getAs<EnumType>())
4414 argTy = Enum->getDecl()->getPromotionType();
4415
Eli Friedman3d815e72008-08-22 00:56:42 +00004416 if (argTy->isPromotableIntegerType() ||
4417 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4418 return QualType();
4419 }
4420
4421 if (allLTypes) return lhs;
4422 if (allRTypes) return rhs;
4423 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00004424 proto->getNumArgs(), proto->isVariadic(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00004425 proto->getTypeQuals(),
4426 false, false, 0, 0, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004427 }
4428
4429 if (allLTypes) return lhs;
4430 if (allRTypes) return rhs;
Douglas Gregorab8bbf42010-01-18 17:14:39 +00004431 return getFunctionNoProtoType(retType, NoReturn, lcc);
Eli Friedman3d815e72008-08-22 00:56:42 +00004432}
4433
4434QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00004435 // C++ [expr]: If an expression initially has the type "reference to T", the
4436 // type is adjusted to "T" prior to any further analysis, the expression
4437 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004438 // expression is an lvalue unless the reference is an rvalue reference and
4439 // the expression is a function call (possibly inside parentheses).
Douglas Gregor0e709ab2010-02-03 21:02:30 +00004440 assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
4441 assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
4442
Eli Friedman3d815e72008-08-22 00:56:42 +00004443 QualType LHSCan = getCanonicalType(LHS),
4444 RHSCan = getCanonicalType(RHS);
4445
4446 // If two types are identical, they are compatible.
4447 if (LHSCan == RHSCan)
4448 return LHS;
4449
John McCall0953e762009-09-24 19:53:00 +00004450 // If the qualifiers are different, the types aren't compatible... mostly.
Douglas Gregora4923eb2009-11-16 21:35:15 +00004451 Qualifiers LQuals = LHSCan.getLocalQualifiers();
4452 Qualifiers RQuals = RHSCan.getLocalQualifiers();
John McCall0953e762009-09-24 19:53:00 +00004453 if (LQuals != RQuals) {
4454 // If any of these qualifiers are different, we have a type
4455 // mismatch.
4456 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4457 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4458 return QualType();
4459
4460 // Exactly one GC qualifier difference is allowed: __strong is
4461 // okay if the other type has no GC qualifier but is an Objective
4462 // C object pointer (i.e. implicitly strong by default). We fix
4463 // this by pretending that the unqualified type was actually
4464 // qualified __strong.
4465 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4466 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4467 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4468
4469 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4470 return QualType();
4471
4472 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4473 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4474 }
4475 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4476 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4477 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004478 return QualType();
John McCall0953e762009-09-24 19:53:00 +00004479 }
4480
4481 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00004482
Eli Friedman852d63b2009-06-01 01:22:52 +00004483 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4484 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00004485
Chris Lattner1adb8832008-01-14 05:45:46 +00004486 // We want to consider the two function types to be the same for these
4487 // comparisons, just force one to the other.
4488 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4489 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00004490
4491 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00004492 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4493 LHSClass = Type::ConstantArray;
4494 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4495 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00004496
Nate Begeman213541a2008-04-18 23:10:10 +00004497 // Canonicalize ExtVector -> Vector.
4498 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4499 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00004500
Chris Lattnera36a61f2008-04-07 05:43:21 +00004501 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004502 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00004503 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00004504 // a signed integer type, or an unsigned integer type.
John McCall842aef82009-12-09 09:09:27 +00004505 // Compatibility is based on the underlying type, not the promotion
4506 // type.
John McCall183700f2009-09-21 23:43:11 +00004507 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004508 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4509 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004510 }
John McCall183700f2009-09-21 23:43:11 +00004511 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004512 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4513 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004514 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004515
Eli Friedman3d815e72008-08-22 00:56:42 +00004516 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004517 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004518
Steve Naroff4a746782008-01-09 22:43:08 +00004519 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004520 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00004521#define TYPE(Class, Base)
4522#define ABSTRACT_TYPE(Class, Base)
4523#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4524#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4525#include "clang/AST/TypeNodes.def"
4526 assert(false && "Non-canonical and dependent types shouldn't get here");
4527 return QualType();
4528
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004529 case Type::LValueReference:
4530 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00004531 case Type::MemberPointer:
4532 assert(false && "C++ should never be in mergeTypes");
4533 return QualType();
4534
4535 case Type::IncompleteArray:
4536 case Type::VariableArray:
4537 case Type::FunctionProto:
4538 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00004539 assert(false && "Types are eliminated above");
4540 return QualType();
4541
Chris Lattner1adb8832008-01-14 05:45:46 +00004542 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00004543 {
4544 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004545 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4546 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00004547 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4548 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00004549 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004550 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00004551 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004552 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004553 return getPointerType(ResultType);
4554 }
Steve Naroffc0febd52008-12-10 17:49:55 +00004555 case Type::BlockPointer:
4556 {
4557 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004558 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4559 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00004560 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4561 if (ResultType.isNull()) return QualType();
4562 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4563 return LHS;
4564 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4565 return RHS;
4566 return getBlockPointerType(ResultType);
4567 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004568 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00004569 {
4570 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4571 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4572 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4573 return QualType();
4574
4575 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4576 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4577 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4578 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00004579 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4580 return LHS;
4581 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4582 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00004583 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4584 ArrayType::ArraySizeModifier(), 0);
4585 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4586 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004587 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4588 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00004589 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4590 return LHS;
4591 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4592 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004593 if (LVAT) {
4594 // FIXME: This isn't correct! But tricky to implement because
4595 // the array's size has to be the size of LHS, but the type
4596 // has to be different.
4597 return LHS;
4598 }
4599 if (RVAT) {
4600 // FIXME: This isn't correct! But tricky to implement because
4601 // the array's size has to be the size of RHS, but the type
4602 // has to be different.
4603 return RHS;
4604 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00004605 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4606 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004607 return getIncompleteArrayType(ResultType,
4608 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004609 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004610 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00004611 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00004612 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00004613 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00004614 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00004615 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004616 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00004617 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00004618 case Type::Complex:
4619 // Distinct complex types are incompatible.
4620 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004621 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004622 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00004623 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00004624 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00004625 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004626 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00004627 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004628 // FIXME: This should be type compatibility, e.g. whether
4629 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00004630 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4631 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00004632 if (LHSIface && RHSIface &&
4633 canAssignObjCInterfaces(LHSIface, RHSIface))
4634 return LHS;
4635
Eli Friedman3d815e72008-08-22 00:56:42 +00004636 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004637 }
Steve Naroff14108da2009-07-10 23:34:53 +00004638 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00004639 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4640 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00004641 return LHS;
4642
Steve Naroffbc76dd02008-12-10 22:14:21 +00004643 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00004644 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00004645 case Type::TemplateSpecialization:
4646 assert(false && "Dependent types have no size");
4647 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00004648 }
Douglas Gregor72564e72009-02-26 23:50:07 +00004649
4650 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004651}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00004652
Chris Lattner5426bf62008-04-07 07:01:58 +00004653//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00004654// Integer Predicates
4655//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00004656
Eli Friedmanad74a752008-06-28 06:23:08 +00004657unsigned ASTContext::getIntWidth(QualType T) {
Sebastian Redl632d7722009-11-05 21:10:57 +00004658 if (T->isBooleanType())
Eli Friedmanad74a752008-06-28 06:23:08 +00004659 return 1;
John McCall842aef82009-12-09 09:09:27 +00004660 if (EnumType *ET = dyn_cast<EnumType>(T))
Eli Friedman29a7f332009-12-10 22:29:29 +00004661 T = ET->getDecl()->getIntegerType();
Eli Friedmanf98aba32009-02-13 02:31:07 +00004662 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00004663 return (unsigned)getTypeSize(T);
4664}
4665
4666QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4667 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00004668
4669 // Turn <4 x signed int> -> <4 x unsigned int>
4670 if (const VectorType *VTy = T->getAs<VectorType>())
4671 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
John Thompson82287d12010-02-05 00:12:22 +00004672 VTy->getNumElements(), VTy->isAltiVec(), VTy->isPixel());
Chris Lattner6a2b9262009-10-17 20:33:28 +00004673
4674 // For enums, we return the unsigned version of the base type.
4675 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00004676 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00004677
4678 const BuiltinType *BTy = T->getAs<BuiltinType>();
4679 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00004680 switch (BTy->getKind()) {
4681 case BuiltinType::Char_S:
4682 case BuiltinType::SChar:
4683 return UnsignedCharTy;
4684 case BuiltinType::Short:
4685 return UnsignedShortTy;
4686 case BuiltinType::Int:
4687 return UnsignedIntTy;
4688 case BuiltinType::Long:
4689 return UnsignedLongTy;
4690 case BuiltinType::LongLong:
4691 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004692 case BuiltinType::Int128:
4693 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004694 default:
4695 assert(0 && "Unexpected signed integer type");
4696 return QualType();
4697 }
4698}
4699
Douglas Gregor2cf26342009-04-09 22:27:44 +00004700ExternalASTSource::~ExternalASTSource() { }
4701
4702void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004703
4704
4705//===----------------------------------------------------------------------===//
4706// Builtin Type Computation
4707//===----------------------------------------------------------------------===//
4708
4709/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4710/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004711static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004712 ASTContext::GetBuiltinTypeError &Error,
4713 bool AllowTypeModifiers = true) {
4714 // Modifiers.
4715 int HowLong = 0;
4716 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004717
Chris Lattner86df27b2009-06-14 00:45:47 +00004718 // Read the modifiers first.
4719 bool Done = false;
4720 while (!Done) {
4721 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004722 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004723 case 'S':
4724 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4725 assert(!Signed && "Can't use 'S' modifier multiple times!");
4726 Signed = true;
4727 break;
4728 case 'U':
4729 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4730 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4731 Unsigned = true;
4732 break;
4733 case 'L':
4734 assert(HowLong <= 2 && "Can't have LLLL modifier");
4735 ++HowLong;
4736 break;
4737 }
4738 }
4739
4740 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004741
Chris Lattner86df27b2009-06-14 00:45:47 +00004742 // Read the base type.
4743 switch (*Str++) {
4744 default: assert(0 && "Unknown builtin type letter!");
4745 case 'v':
4746 assert(HowLong == 0 && !Signed && !Unsigned &&
4747 "Bad modifiers used with 'v'!");
4748 Type = Context.VoidTy;
4749 break;
4750 case 'f':
4751 assert(HowLong == 0 && !Signed && !Unsigned &&
4752 "Bad modifiers used with 'f'!");
4753 Type = Context.FloatTy;
4754 break;
4755 case 'd':
4756 assert(HowLong < 2 && !Signed && !Unsigned &&
4757 "Bad modifiers used with 'd'!");
4758 if (HowLong)
4759 Type = Context.LongDoubleTy;
4760 else
4761 Type = Context.DoubleTy;
4762 break;
4763 case 's':
4764 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4765 if (Unsigned)
4766 Type = Context.UnsignedShortTy;
4767 else
4768 Type = Context.ShortTy;
4769 break;
4770 case 'i':
4771 if (HowLong == 3)
4772 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4773 else if (HowLong == 2)
4774 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4775 else if (HowLong == 1)
4776 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4777 else
4778 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4779 break;
4780 case 'c':
4781 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4782 if (Signed)
4783 Type = Context.SignedCharTy;
4784 else if (Unsigned)
4785 Type = Context.UnsignedCharTy;
4786 else
4787 Type = Context.CharTy;
4788 break;
4789 case 'b': // boolean
4790 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4791 Type = Context.BoolTy;
4792 break;
4793 case 'z': // size_t.
4794 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4795 Type = Context.getSizeType();
4796 break;
4797 case 'F':
4798 Type = Context.getCFConstantStringType();
4799 break;
4800 case 'a':
4801 Type = Context.getBuiltinVaListType();
4802 assert(!Type.isNull() && "builtin va list type not initialized!");
4803 break;
4804 case 'A':
4805 // This is a "reference" to a va_list; however, what exactly
4806 // this means depends on how va_list is defined. There are two
4807 // different kinds of va_list: ones passed by value, and ones
4808 // passed by reference. An example of a by-value va_list is
4809 // x86, where va_list is a char*. An example of by-ref va_list
4810 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4811 // we want this argument to be a char*&; for x86-64, we want
4812 // it to be a __va_list_tag*.
4813 Type = Context.getBuiltinVaListType();
4814 assert(!Type.isNull() && "builtin va list type not initialized!");
4815 if (Type->isArrayType()) {
4816 Type = Context.getArrayDecayedType(Type);
4817 } else {
4818 Type = Context.getLValueReferenceType(Type);
4819 }
4820 break;
4821 case 'V': {
4822 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004823 unsigned NumElements = strtoul(Str, &End, 10);
4824 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004825
Chris Lattner86df27b2009-06-14 00:45:47 +00004826 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004827
Chris Lattner86df27b2009-06-14 00:45:47 +00004828 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
John Thompson82287d12010-02-05 00:12:22 +00004829 // FIXME: Don't know what to do about AltiVec.
4830 Type = Context.getVectorType(ElementType, NumElements, false, false);
Chris Lattner86df27b2009-06-14 00:45:47 +00004831 break;
4832 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004833 case 'X': {
4834 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4835 Type = Context.getComplexType(ElementType);
4836 break;
4837 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004838 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004839 Type = Context.getFILEType();
4840 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004841 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004842 return QualType();
4843 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004844 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004845 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004846 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004847 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004848 else
4849 Type = Context.getjmp_bufType();
4850
Mike Stumpfd612db2009-07-28 23:47:15 +00004851 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004852 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004853 return QualType();
4854 }
4855 break;
Mike Stump782fa302009-07-28 02:25:19 +00004856 }
Mike Stump1eb44332009-09-09 15:08:12 +00004857
Chris Lattner86df27b2009-06-14 00:45:47 +00004858 if (!AllowTypeModifiers)
4859 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004860
Chris Lattner86df27b2009-06-14 00:45:47 +00004861 Done = false;
4862 while (!Done) {
4863 switch (*Str++) {
4864 default: Done = true; --Str; break;
4865 case '*':
4866 Type = Context.getPointerType(Type);
4867 break;
4868 case '&':
4869 Type = Context.getLValueReferenceType(Type);
4870 break;
4871 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4872 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004873 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004874 break;
Fariborz Jahanian013af392010-01-26 22:48:42 +00004875 case 'D':
4876 Type = Context.getVolatileType(Type);
4877 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004878 }
4879 }
Mike Stump1eb44332009-09-09 15:08:12 +00004880
Chris Lattner86df27b2009-06-14 00:45:47 +00004881 return Type;
4882}
4883
4884/// GetBuiltinType - Return the type for the specified builtin.
4885QualType ASTContext::GetBuiltinType(unsigned id,
4886 GetBuiltinTypeError &Error) {
4887 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004888
Chris Lattner86df27b2009-06-14 00:45:47 +00004889 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004890
Chris Lattner86df27b2009-06-14 00:45:47 +00004891 Error = GE_None;
4892 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4893 if (Error != GE_None)
4894 return QualType();
4895 while (TypeStr[0] && TypeStr[0] != '.') {
4896 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4897 if (Error != GE_None)
4898 return QualType();
4899
4900 // Do array -> pointer decay. The builtin should use the decayed type.
4901 if (Ty->isArrayType())
4902 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004903
Chris Lattner86df27b2009-06-14 00:45:47 +00004904 ArgTypes.push_back(Ty);
4905 }
4906
4907 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4908 "'.' should only occur at end of builtin type list!");
4909
4910 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4911 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4912 return getFunctionNoProtoType(ResType);
Douglas Gregorce056bc2010-02-21 22:15:06 +00004913
4914 // FIXME: Should we create noreturn types?
Chris Lattner86df27b2009-06-14 00:45:47 +00004915 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00004916 TypeStr[0] == '.', 0, false, false, 0, 0,
4917 false, CC_Default);
Chris Lattner86df27b2009-06-14 00:45:47 +00004918}
Eli Friedmana95d7572009-08-19 07:44:53 +00004919
4920QualType
4921ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4922 // Perform the usual unary conversions. We do this early so that
4923 // integral promotions to "int" can allow us to exit early, in the
4924 // lhs == rhs check. Also, for conversion purposes, we ignore any
4925 // qualifiers. For example, "const float" and "float" are
4926 // equivalent.
4927 if (lhs->isPromotableIntegerType())
4928 lhs = getPromotedIntegerType(lhs);
4929 else
4930 lhs = lhs.getUnqualifiedType();
4931 if (rhs->isPromotableIntegerType())
4932 rhs = getPromotedIntegerType(rhs);
4933 else
4934 rhs = rhs.getUnqualifiedType();
4935
4936 // If both types are identical, no conversion is needed.
4937 if (lhs == rhs)
4938 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004939
Eli Friedmana95d7572009-08-19 07:44:53 +00004940 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4941 // The caller can deal with this (e.g. pointer + int).
4942 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4943 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004944
4945 // At this point, we have two different arithmetic types.
4946
Eli Friedmana95d7572009-08-19 07:44:53 +00004947 // Handle complex types first (C99 6.3.1.8p1).
4948 if (lhs->isComplexType() || rhs->isComplexType()) {
4949 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004950 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004951 // convert the rhs to the lhs complex type.
4952 return lhs;
4953 }
Mike Stump1eb44332009-09-09 15:08:12 +00004954 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004955 // convert the lhs to the rhs complex type.
4956 return rhs;
4957 }
4958 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004959 // When both operands are complex, the shorter operand is converted to the
4960 // type of the longer, and that is the type of the result. This corresponds
4961 // to what is done when combining two real floating-point operands.
4962 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004963 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004964 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004965 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004966 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004967 // "double _Complex" is promoted to "long double _Complex".
4968 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004969
4970 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004971 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004972 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004973 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004974 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004975 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4976 // domains match. This is a requirement for our implementation, C99
4977 // does not require this promotion.
4978 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4979 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4980 return rhs;
4981 } else { // handle "_Complex double, double".
4982 return lhs;
4983 }
4984 }
4985 return lhs; // The domain/size match exactly.
4986 }
4987 // Now handle "real" floating types (i.e. float, double, long double).
4988 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4989 // if we have an integer operand, the result is the real floating type.
4990 if (rhs->isIntegerType()) {
4991 // convert rhs to the lhs floating point type.
4992 return lhs;
4993 }
4994 if (rhs->isComplexIntegerType()) {
4995 // convert rhs to the complex floating point type.
4996 return getComplexType(lhs);
4997 }
4998 if (lhs->isIntegerType()) {
4999 // convert lhs to the rhs floating point type.
5000 return rhs;
5001 }
Mike Stump1eb44332009-09-09 15:08:12 +00005002 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00005003 // convert lhs to the complex floating point type.
5004 return getComplexType(rhs);
5005 }
5006 // We have two real floating types, float/complex combos were handled above.
5007 // Convert the smaller operand to the bigger result.
5008 int result = getFloatingTypeOrder(lhs, rhs);
5009 if (result > 0) // convert the rhs
5010 return lhs;
5011 assert(result < 0 && "illegal float comparison");
5012 return rhs; // convert the lhs
5013 }
5014 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
5015 // Handle GCC complex int extension.
5016 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
5017 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
5018
5019 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00005020 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00005021 rhsComplexInt->getElementType()) >= 0)
5022 return lhs; // convert the rhs
5023 return rhs;
5024 } else if (lhsComplexInt && rhs->isIntegerType()) {
5025 // convert the rhs to the lhs complex type.
5026 return lhs;
5027 } else if (rhsComplexInt && lhs->isIntegerType()) {
5028 // convert the lhs to the rhs complex type.
5029 return rhs;
5030 }
5031 }
5032 // Finally, we have two differing integer types.
5033 // The rules for this case are in C99 6.3.1.8
5034 int compare = getIntegerTypeOrder(lhs, rhs);
5035 bool lhsSigned = lhs->isSignedIntegerType(),
5036 rhsSigned = rhs->isSignedIntegerType();
5037 QualType destType;
5038 if (lhsSigned == rhsSigned) {
5039 // Same signedness; use the higher-ranked type
5040 destType = compare >= 0 ? lhs : rhs;
5041 } else if (compare != (lhsSigned ? 1 : -1)) {
5042 // The unsigned type has greater than or equal rank to the
5043 // signed type, so use the unsigned type
5044 destType = lhsSigned ? rhs : lhs;
5045 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
5046 // The two types are different widths; if we are here, that
5047 // means the signed type is larger than the unsigned type, so
5048 // use the signed type.
5049 destType = lhsSigned ? lhs : rhs;
5050 } else {
5051 // The signed type is higher-ranked than the unsigned type,
5052 // but isn't actually any bigger (like unsigned int and long
5053 // on most 32-bit systems). Use the unsigned type corresponding
5054 // to the signed type.
5055 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
5056 }
5057 return destType;
5058}