blob: d60a8204c7d5e82511e4ca2f7a473cec927dad6b [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerddc135e2006-11-10 06:34:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff67391b82007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000018#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000019#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000023#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000024#include "clang/Basic/TargetInfo.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000025#include "llvm/ADT/SmallString.h"
Anders Carlssond8499822007-10-29 05:01:08 +000026#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000027#include "llvm/Support/MathExtras.h"
Benjamin Kramer1402ce32009-10-24 09:57:09 +000028#include "llvm/Support/raw_ostream.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000029#include "RecordLayoutBuilder.h"
30
Chris Lattnerddc135e2006-11-10 06:34:16 +000031using namespace clang;
32
Steve Naroff0af91202007-04-27 21:51:21 +000033enum FloatingRank {
34 FloatRank, DoubleRank, LongDoubleRank
35};
36
Chris Lattner465fa322008-10-05 17:34:18 +000037ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
38 TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +000039 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +000040 Builtin::Context &builtins,
Mike Stump11289f42009-09-09 15:08:12 +000041 bool FreeMem, unsigned size_reserve) :
42 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +000043 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stumpe1b19ba2009-10-22 00:49:09 +000044 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
45 SourceMgr(SM), LangOpts(LOpts),
Mike Stump11289f42009-09-09 15:08:12 +000046 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000047 Idents(idents), Selectors(sels),
Mike Stump11289f42009-09-09 15:08:12 +000048 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall9f57c292009-08-17 16:35:33 +000049 ObjCIdRedefinitionType = QualType();
50 ObjCClassRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +000051 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +000052 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +000053 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +000054}
55
Chris Lattnerd5973eb2006-11-12 00:53:46 +000056ASTContext::~ASTContext() {
57 // Deallocate all the types.
58 while (!Types.empty()) {
Ted Kremeneka2157712008-05-21 16:38:54 +000059 Types.back()->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +000060 Types.pop_back();
61 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +000062
Nuno Lopese013c7f2008-12-17 22:30:25 +000063 {
John McCall8ccfcb52009-09-24 19:53:00 +000064 llvm::FoldingSet<ExtQuals>::iterator
65 I = ExtQualNodes.begin(), E = ExtQualNodes.end();
66 while (I != E)
67 Deallocate(&*I++);
68 }
69
70 {
Nuno Lopese013c7f2008-12-17 22:30:25 +000071 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
72 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
73 while (I != E) {
74 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
75 delete R;
76 }
77 }
78
79 {
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +000080 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
81 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopese013c7f2008-12-17 22:30:25 +000082 while (I != E) {
83 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
84 delete R;
85 }
86 }
87
Douglas Gregorf21eb492009-03-26 23:50:42 +000088 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +000089 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
90 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +000091 NNSEnd = NestedNameSpecifiers.end();
92 NNS != NNSEnd;
Douglas Gregorc741fb12009-03-27 23:54:10 +000093 /* Increment in loop */)
94 (*NNS++).Destroy(*this);
Douglas Gregorf21eb492009-03-26 23:50:42 +000095
96 if (GlobalNestedNameSpecifier)
97 GlobalNestedNameSpecifier->Destroy(*this);
98
Eli Friedmane2bbfe22008-05-27 03:08:09 +000099 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000100}
101
Mike Stump11289f42009-09-09 15:08:12 +0000102void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
104 ExternalSource.reset(Source.take());
105}
106
Chris Lattner4eb445d2007-01-26 01:27:23 +0000107void ASTContext::PrintStats() const {
108 fprintf(stderr, "*** AST Context Stats:\n");
109 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000110
Douglas Gregora30d0462009-05-26 14:40:08 +0000111 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000112#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000113#define ABSTRACT_TYPE(Name, Parent)
114#include "clang/AST/TypeNodes.def"
115 0 // Extra
116 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000117
Chris Lattner4eb445d2007-01-26 01:27:23 +0000118 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
119 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000120 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000121 }
122
Douglas Gregora30d0462009-05-26 14:40:08 +0000123 unsigned Idx = 0;
124 unsigned TotalBytes = 0;
125#define TYPE(Name, Parent) \
126 if (counts[Idx]) \
127 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
128 TotalBytes += counts[Idx] * sizeof(Name##Type); \
129 ++Idx;
130#define ABSTRACT_TYPE(Name, Parent)
131#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregora30d0462009-05-26 14:40:08 +0000133 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134
135 if (ExternalSource.get()) {
136 fprintf(stderr, "\n");
137 ExternalSource->PrintStats();
138 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000139}
140
141
John McCall48f2d582009-10-23 23:03:21 +0000142void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000143 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCall48f2d582009-10-23 23:03:21 +0000144 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall90d1c2d2009-09-24 23:30:46 +0000145 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000146}
147
Chris Lattner970e54e2006-11-12 00:37:36 +0000148void ASTContext::InitBuiltinTypes() {
149 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattner970e54e2006-11-12 00:37:36 +0000151 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000152 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattner970e54e2006-11-12 00:37:36 +0000154 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000155 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000156 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000157 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000158 InitBuiltinType(CharTy, BuiltinType::Char_S);
159 else
160 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000161 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000162 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
163 InitBuiltinType(ShortTy, BuiltinType::Short);
164 InitBuiltinType(IntTy, BuiltinType::Int);
165 InitBuiltinType(LongTy, BuiltinType::Long);
166 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattner970e54e2006-11-12 00:37:36 +0000168 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000169 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
170 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
171 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
172 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
173 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000174
Chris Lattner970e54e2006-11-12 00:37:36 +0000175 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000176 InitBuiltinType(FloatTy, BuiltinType::Float);
177 InitBuiltinType(DoubleTy, BuiltinType::Double);
178 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000179
Chris Lattnerf122cef2009-04-30 02:43:43 +0000180 // GNU extension, 128-bit integers.
181 InitBuiltinType(Int128Ty, BuiltinType::Int128);
182 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
183
Chris Lattner007cb022009-02-26 23:43:47 +0000184 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
185 InitBuiltinType(WCharTy, BuiltinType::WChar);
186 else // C99
187 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000188
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000189 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
190 InitBuiltinType(Char16Ty, BuiltinType::Char16);
191 else // C99
192 Char16Ty = getFromTargetType(Target.getChar16Type());
193
194 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
195 InitBuiltinType(Char32Ty, BuiltinType::Char32);
196 else // C99
197 Char32Ty = getFromTargetType(Target.getChar32Type());
198
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000199 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000200 InitBuiltinType(OverloadTy, BuiltinType::Overload);
201
202 // Placeholder type for type-dependent expressions whose type is
203 // completely unknown. No code should ever check a type against
204 // DependentTy and users should never see it; however, it is here to
205 // help diagnose failures to properly check for type-dependent
206 // expressions.
207 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000208
Mike Stump11289f42009-09-09 15:08:12 +0000209 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000210 // not yet been deduced.
211 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000212
Chris Lattner970e54e2006-11-12 00:37:36 +0000213 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000214 FloatComplexTy = getComplexType(FloatTy);
215 DoubleComplexTy = getComplexType(DoubleTy);
216 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000217
Steve Naroff66e9f332007-10-15 14:41:52 +0000218 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000219
Steve Naroff1329fa02009-07-15 18:40:39 +0000220 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
221 ObjCIdTypedefType = QualType();
222 ObjCClassTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000223
Steve Naroff1329fa02009-07-15 18:40:39 +0000224 // Builtin types for 'id' and 'Class'.
225 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
226 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000227
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000228 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000229
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000230 // void * type
231 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000232
233 // nullptr type (C++0x 2.14.7)
234 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000235}
236
Douglas Gregor86d142a2009-10-08 07:24:58 +0000237MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000238ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000239 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000240 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000241 = InstantiatedFromStaticDataMember.find(Var);
242 if (Pos == InstantiatedFromStaticDataMember.end())
243 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000244
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000245 return Pos->second;
246}
247
Mike Stump11289f42009-09-09 15:08:12 +0000248void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000249ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
250 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000251 assert(Inst->isStaticDataMember() && "Not a static data member");
252 assert(Tmpl->isStaticDataMember() && "Not a static data member");
253 assert(!InstantiatedFromStaticDataMember[Inst] &&
254 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000255 InstantiatedFromStaticDataMember[Inst]
256 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000257}
258
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000259UnresolvedUsingDecl *
260ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump11289f42009-09-09 15:08:12 +0000261 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000262 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
263 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
264 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000265
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000266 return Pos->second;
267}
268
269void
270ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
271 UnresolvedUsingDecl *UUD) {
272 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
273 "Already noted what using decl what instantiated from");
274 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
275}
276
Anders Carlsson5da84842009-09-01 04:26:58 +0000277FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
278 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
279 = InstantiatedFromUnnamedFieldDecl.find(Field);
280 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
281 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000282
Anders Carlsson5da84842009-09-01 04:26:58 +0000283 return Pos->second;
284}
285
286void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
287 FieldDecl *Tmpl) {
288 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
289 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
290 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
291 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000292
Anders Carlsson5da84842009-09-01 04:26:58 +0000293 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
294}
295
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000296namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000297 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000298 : std::binary_function<SourceRange, SourceRange, bool> {
299 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000301 public:
302 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000303
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000304 bool operator()(SourceRange X, SourceRange Y) {
305 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
306 }
307 };
308}
309
310/// \brief Determine whether the given comment is a Doxygen-style comment.
311///
312/// \param Start the start of the comment text.
313///
314/// \param End the end of the comment text.
315///
316/// \param Member whether we want to check whether this is a member comment
317/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
318/// we only return true when we find a non-member comment.
Mike Stump11289f42009-09-09 15:08:12 +0000319static bool
320isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000321 bool Member = false) {
Mike Stump11289f42009-09-09 15:08:12 +0000322 const char *BufferStart
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000323 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
324 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
325 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000326
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000327 if (End - Start < 4)
328 return false;
329
330 assert(Start[0] == '/' && "Not a comment?");
331 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
332 return false;
333 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
334 return false;
335
336 return (Start[3] == '<') == Member;
337}
338
339/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump11289f42009-09-09 15:08:12 +0000340/// it has one.
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000341const char *ASTContext::getCommentForDecl(const Decl *D) {
342 if (!D)
343 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000345 // Check whether we have cached a comment string for this declaration
346 // already.
Mike Stump11289f42009-09-09 15:08:12 +0000347 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000348 = DeclComments.find(D);
349 if (Pos != DeclComments.end())
350 return Pos->second.c_str();
351
Mike Stump11289f42009-09-09 15:08:12 +0000352 // If we have an external AST source and have not yet loaded comments from
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000353 // that source, do so now.
354 if (ExternalSource && !LoadedExternalComments) {
355 std::vector<SourceRange> LoadedComments;
356 ExternalSource->ReadComments(LoadedComments);
Mike Stump11289f42009-09-09 15:08:12 +0000357
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000358 if (!LoadedComments.empty())
359 Comments.insert(Comments.begin(), LoadedComments.begin(),
360 LoadedComments.end());
Mike Stump11289f42009-09-09 15:08:12 +0000361
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000362 LoadedExternalComments = true;
363 }
Mike Stump11289f42009-09-09 15:08:12 +0000364
365 // If there are no comments anywhere, we won't find anything.
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000366 if (Comments.empty())
367 return 0;
368
369 // If the declaration doesn't map directly to a location in a file, we
370 // can't find the comment.
371 SourceLocation DeclStartLoc = D->getLocStart();
372 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
373 return 0;
374
375 // Find the comment that occurs just before this declaration.
376 std::vector<SourceRange>::iterator LastComment
Mike Stump11289f42009-09-09 15:08:12 +0000377 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000378 SourceRange(DeclStartLoc),
379 BeforeInTranslationUnit(&SourceMgr));
Mike Stump11289f42009-09-09 15:08:12 +0000380
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000381 // Decompose the location for the start of the declaration and find the
382 // beginning of the file buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000383 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000384 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000385 const char *FileBufferStart
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000386 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump11289f42009-09-09 15:08:12 +0000387
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000388 // First check whether we have a comment for a member.
389 if (LastComment != Comments.end() &&
390 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
391 isDoxygenComment(SourceMgr, *LastComment, true)) {
392 std::pair<FileID, unsigned> LastCommentEndDecomp
393 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
394 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
395 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump11289f42009-09-09 15:08:12 +0000396 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000397 LastCommentEndDecomp.second)) {
398 // The Doxygen member comment comes after the declaration starts and
399 // is on the same line and in the same file as the declaration. This
400 // is the comment we want.
401 std::string &Result = DeclComments[D];
Mike Stump11289f42009-09-09 15:08:12 +0000402 Result.append(FileBufferStart +
403 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000404 FileBufferStart + LastCommentEndDecomp.second + 1);
405 return Result.c_str();
406 }
407 }
Mike Stump11289f42009-09-09 15:08:12 +0000408
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000409 if (LastComment == Comments.begin())
410 return 0;
411 --LastComment;
412
413 // Decompose the end of the comment.
414 std::pair<FileID, unsigned> LastCommentEndDecomp
415 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000416
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000417 // If the comment and the declaration aren't in the same file, then they
418 // aren't related.
419 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
420 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000421
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000422 // Check that we actually have a Doxygen comment.
423 if (!isDoxygenComment(SourceMgr, *LastComment))
424 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000425
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000426 // Compute the starting line for the declaration and for the end of the
427 // comment (this is expensive).
Mike Stump11289f42009-09-09 15:08:12 +0000428 unsigned DeclStartLine
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000429 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
430 unsigned CommentEndLine
Mike Stump11289f42009-09-09 15:08:12 +0000431 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000432 LastCommentEndDecomp.second);
Mike Stump11289f42009-09-09 15:08:12 +0000433
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000434 // If the comment does not end on the line prior to the declaration, then
435 // the comment is not associated with the declaration at all.
436 if (CommentEndLine + 1 != DeclStartLine)
437 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000438
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000439 // We have a comment, but there may be more comments on the previous lines.
440 // Keep looking so long as the comments are still Doxygen comments and are
441 // still adjacent.
Mike Stump11289f42009-09-09 15:08:12 +0000442 unsigned ExpectedLine
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000443 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
444 std::vector<SourceRange>::iterator FirstComment = LastComment;
445 while (FirstComment != Comments.begin()) {
446 // Look at the previous comment
447 --FirstComment;
448 std::pair<FileID, unsigned> Decomp
449 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000450
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000451 // If this previous comment is in a different file, we're done.
452 if (Decomp.first != DeclStartDecomp.first) {
453 ++FirstComment;
454 break;
455 }
Mike Stump11289f42009-09-09 15:08:12 +0000456
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000457 // If this comment is not a Doxygen comment, we're done.
458 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
459 ++FirstComment;
460 break;
461 }
Mike Stump11289f42009-09-09 15:08:12 +0000462
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000463 // If the line number is not what we expected, we're done.
464 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
465 if (Line != ExpectedLine) {
466 ++FirstComment;
467 break;
468 }
Mike Stump11289f42009-09-09 15:08:12 +0000469
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000470 // Set the next expected line number.
Mike Stump11289f42009-09-09 15:08:12 +0000471 ExpectedLine
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000472 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
473 }
Mike Stump11289f42009-09-09 15:08:12 +0000474
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000475 // The iterator range [FirstComment, LastComment] contains all of the
476 // BCPL comments that, together, are associated with this declaration.
477 // Form a single comment block string for this declaration that concatenates
478 // all of these comments.
479 std::string &Result = DeclComments[D];
480 while (FirstComment != LastComment) {
481 std::pair<FileID, unsigned> DecompStart
482 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
483 std::pair<FileID, unsigned> DecompEnd
484 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
485 Result.append(FileBufferStart + DecompStart.second,
486 FileBufferStart + DecompEnd.second + 1);
487 ++FirstComment;
488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000490 // Append the last comment line.
Mike Stump11289f42009-09-09 15:08:12 +0000491 Result.append(FileBufferStart +
492 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000493 FileBufferStart + LastCommentEndDecomp.second + 1);
494 return Result.c_str();
495}
496
Chris Lattner53cfe802007-07-18 17:52:12 +0000497//===----------------------------------------------------------------------===//
498// Type Sizing and Analysis
499//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000500
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000501/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
502/// scalar floating point type.
503const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000504 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000505 assert(BT && "Not a floating point type!");
506 switch (BT->getKind()) {
507 default: assert(0 && "Not a floating point type!");
508 case BuiltinType::Float: return Target.getFloatFormat();
509 case BuiltinType::Double: return Target.getDoubleFormat();
510 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
511 }
512}
513
Mike Stump8ce0ea12009-09-22 02:43:44 +0000514/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000515/// specified decl. Note that bitfields do not have a valid alignment, so
516/// this method will assert on them.
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +0000517unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000518 unsigned Align = Target.getCharWidth();
519
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000520 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedman19a546c2009-02-22 02:56:25 +0000521 Align = std::max(Align, AA->getAlignment());
522
Chris Lattner68061312009-01-24 21:53:27 +0000523 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
524 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000525 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000526 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlsson0e6d2b32009-04-10 04:52:36 +0000527 Align = Target.getPointerAlign(AS);
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000528 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
529 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000530 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
531 T = cast<ArrayType>(T)->getElementType();
532
533 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
534 }
Chris Lattner68061312009-01-24 21:53:27 +0000535 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000536
537 return Align / Target.getCharWidth();
Chris Lattner68061312009-01-24 21:53:27 +0000538}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000539
Chris Lattner983a8bb2007-07-13 22:13:22 +0000540/// getTypeSize - Return the size of the specified type, in bits. This method
541/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000542///
543/// FIXME: Pointers into different addr spaces could have different sizes and
544/// alignment requirements: getPointerInfo should take an AddrSpace, this
545/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000546std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000547ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000548 uint64_t Width=0;
549 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000550 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000551#define TYPE(Class, Base)
552#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000553#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000554#define DEPENDENT_TYPE(Class, Base) case Type::Class:
555#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000556 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000557 break;
558
Chris Lattner355332d2007-07-13 22:27:08 +0000559 case Type::FunctionNoProto:
560 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000561 // GCC extension: alignof(function) = 32 bits
562 Width = 0;
563 Align = 32;
564 break;
565
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000566 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000567 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000568 Width = 0;
569 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
570 break;
571
Steve Naroff5c131802007-08-30 01:06:46 +0000572 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000573 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000574
Chris Lattner37e05872008-03-05 18:54:05 +0000575 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000576 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000577 Align = EltInfo.second;
578 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000579 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000580 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000581 case Type::Vector: {
Chris Lattner63d2b362009-10-22 05:17:15 +0000582 const VectorType *VT = cast<VectorType>(T);
583 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
584 Width = EltInfo.first*VT->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000585 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000586 // If the alignment is not a power of 2, round up to the next power of 2.
587 // This happens for non-power-of-2 length vectors.
Chris Lattner63d2b362009-10-22 05:17:15 +0000588 if (VT->getNumElements() & (VT->getNumElements()-1)) {
589 Align = llvm::NextPowerOf2(Align);
590 Width = llvm::RoundUpToAlignment(Width, Align);
591 }
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000592 break;
593 }
Chris Lattner647fb222007-07-18 18:26:58 +0000594
Chris Lattner7570e9c2008-03-08 08:52:55 +0000595 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000596 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000597 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000598 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000599 // GCC extension: alignof(void) = 8 bits.
600 Width = 0;
601 Align = 8;
602 break;
603
Chris Lattner6a4f7452007-12-19 19:23:28 +0000604 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000605 Width = Target.getBoolWidth();
606 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000607 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000608 case BuiltinType::Char_S:
609 case BuiltinType::Char_U:
610 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000611 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000612 Width = Target.getCharWidth();
613 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000614 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000615 case BuiltinType::WChar:
616 Width = Target.getWCharWidth();
617 Align = Target.getWCharAlign();
618 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000619 case BuiltinType::Char16:
620 Width = Target.getChar16Width();
621 Align = Target.getChar16Align();
622 break;
623 case BuiltinType::Char32:
624 Width = Target.getChar32Width();
625 Align = Target.getChar32Align();
626 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000627 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000628 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000629 Width = Target.getShortWidth();
630 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000631 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000632 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000633 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000634 Width = Target.getIntWidth();
635 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000636 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000637 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000638 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000639 Width = Target.getLongWidth();
640 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000641 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000642 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000643 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000644 Width = Target.getLongLongWidth();
645 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000646 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000647 case BuiltinType::Int128:
648 case BuiltinType::UInt128:
649 Width = 128;
650 Align = 128; // int128_t is 128-bit aligned on all targets.
651 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000652 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000653 Width = Target.getFloatWidth();
654 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000655 break;
656 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000657 Width = Target.getDoubleWidth();
658 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000659 break;
660 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000661 Width = Target.getLongDoubleWidth();
662 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000663 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000664 case BuiltinType::NullPtr:
665 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
666 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000667 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000668 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000669 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +0000670 case Type::FixedWidthInt:
671 // FIXME: This isn't precisely correct; the width/alignment should depend
672 // on the available types for the target
673 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner641f1ea2009-02-15 21:20:13 +0000674 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedman1efaaea2009-02-13 02:31:07 +0000675 Align = Width;
676 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000677 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000678 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000679 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000680 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000681 case Type::BlockPointer: {
682 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
683 Width = Target.getPointerWidth(AS);
684 Align = Target.getPointerAlign(AS);
685 break;
686 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000687 case Type::Pointer: {
688 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000689 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000690 Align = Target.getPointerAlign(AS);
691 break;
692 }
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000693 case Type::LValueReference:
694 case Type::RValueReference:
Chris Lattner0523dc92007-07-13 22:16:13 +0000695 // "When applied to a reference or a reference type, the result is the size
Chris Lattner647fb222007-07-18 18:26:58 +0000696 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6a4f7452007-12-19 19:23:28 +0000697 // FIXME: This is wrong for struct layout: a reference in a struct has
698 // pointer size.
Chris Lattnerdb5f1fa2008-04-02 17:35:06 +0000699 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000700 case Type::MemberPointer: {
Anders Carlsson32440a02009-05-17 02:06:04 +0000701 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
702 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
703 // If we ever want to support other ABIs this needs to be abstracted.
704
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000705 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000706 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000707 getTypeInfo(getPointerDiffType());
708 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000709 if (Pointee->isFunctionType())
710 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000711 Align = PtrDiffInfo.second;
712 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000713 }
Chris Lattner647fb222007-07-18 18:26:58 +0000714 case Type::Complex: {
715 // Complex types have the same alignment as their elements, but twice the
716 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000717 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000718 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000719 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000720 Align = EltInfo.second;
721 break;
722 }
Devang Pateldbb72632008-06-04 21:54:36 +0000723 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000724 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000725 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
726 Width = Layout.getSize();
727 Align = Layout.getAlignment();
728 break;
729 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000730 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000731 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000732 const TagType *TT = cast<TagType>(T);
733
734 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000735 Width = 1;
736 Align = 1;
737 break;
738 }
Mike Stump11289f42009-09-09 15:08:12 +0000739
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000740 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000741 return getTypeInfo(ET->getDecl()->getIntegerType());
742
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000743 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000744 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
745 Width = Layout.getSize();
746 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000747 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000748 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000749
Chris Lattner63d2b362009-10-22 05:17:15 +0000750 case Type::SubstTemplateTypeParm:
John McCallcebee162009-10-18 09:09:24 +0000751 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
752 getReplacementType().getTypePtr());
John McCallcebee162009-10-18 09:09:24 +0000753
Chris Lattner63d2b362009-10-22 05:17:15 +0000754 case Type::Elaborated:
755 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
756 .getTypePtr());
John McCallfcc33b02009-09-05 00:15:47 +0000757
Douglas Gregoref462e62009-04-30 17:32:17 +0000758 case Type::Typedef: {
759 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000760 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregoref462e62009-04-30 17:32:17 +0000761 Align = Aligned->getAlignment();
762 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
763 } else
764 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000765 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000766 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000767
768 case Type::TypeOfExpr:
769 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
770 .getTypePtr());
771
772 case Type::TypeOf:
773 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
774
Anders Carlsson81df7b82009-06-24 19:06:50 +0000775 case Type::Decltype:
776 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
777 .getTypePtr());
778
Douglas Gregoref462e62009-04-30 17:32:17 +0000779 case Type::QualifiedName:
780 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000781
Douglas Gregoref462e62009-04-30 17:32:17 +0000782 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000783 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000784 "Cannot request the size of a dependent type");
785 // FIXME: this is likely to be wrong once we support template
786 // aliases, since a template alias could refer to a typedef that
787 // has an __aligned__ attribute on it.
788 return getTypeInfo(getCanonicalType(T));
789 }
Mike Stump11289f42009-09-09 15:08:12 +0000790
Chris Lattner53cfe802007-07-18 17:52:12 +0000791 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000792 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000793}
794
Chris Lattnera3402cd2009-01-27 18:08:34 +0000795/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
796/// type for the current target in bits. This can be different than the ABI
797/// alignment in cases where it is beneficial for performance to overalign
798/// a data type.
799unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
800 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000801
802 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000803 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000804 T = CT->getElementType().getTypePtr();
805 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
806 T->isSpecificBuiltinType(BuiltinType::LongLong))
807 return std::max(ABIAlign, (unsigned)getTypeSize(T));
808
Chris Lattnera3402cd2009-01-27 18:08:34 +0000809 return ABIAlign;
810}
811
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000812static void CollectLocalObjCIvars(ASTContext *Ctx,
813 const ObjCInterfaceDecl *OI,
814 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000815 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
816 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000817 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000818 if (!IVDecl->isInvalidDecl())
819 Fields.push_back(cast<FieldDecl>(IVDecl));
820 }
821}
822
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000823void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
824 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
825 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
826 CollectObjCIvars(SuperClass, Fields);
827 CollectLocalObjCIvars(this, OI, Fields);
828}
829
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000830/// ShallowCollectObjCIvars -
831/// Collect all ivars, including those synthesized, in the current class.
832///
833void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
834 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
835 bool CollectSynthesized) {
836 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
837 E = OI->ivar_end(); I != E; ++I) {
838 Ivars.push_back(*I);
839 }
840 if (CollectSynthesized)
841 CollectSynthesizedIvars(OI, Ivars);
842}
843
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000844void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
845 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000846 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
847 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000848 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
849 Ivars.push_back(Ivar);
Mike Stump11289f42009-09-09 15:08:12 +0000850
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000851 // Also look into nested protocols.
852 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
853 E = PD->protocol_end(); P != E; ++P)
854 CollectProtocolSynthesizedIvars(*P, Ivars);
855}
856
857/// CollectSynthesizedIvars -
858/// This routine collect synthesized ivars for the designated class.
859///
860void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
861 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000862 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
863 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000864 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
865 Ivars.push_back(Ivar);
866 }
867 // Also look into interface's protocol list for properties declared
868 // in the protocol and whose ivars are synthesized.
869 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
870 PE = OI->protocol_end(); P != PE; ++P) {
871 ObjCProtocolDecl *PD = (*P);
872 CollectProtocolSynthesizedIvars(PD, Ivars);
873 }
874}
875
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000876unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
877 unsigned count = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000878 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
879 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000880 if ((*I)->getPropertyIvarDecl())
881 ++count;
882
883 // Also look into nested protocols.
884 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
885 E = PD->protocol_end(); P != E; ++P)
886 count += CountProtocolSynthesizedIvars(*P);
887 return count;
888}
889
Mike Stump11289f42009-09-09 15:08:12 +0000890unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000891 unsigned count = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000892 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
893 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000894 if ((*I)->getPropertyIvarDecl())
895 ++count;
896 }
897 // Also look into interface's protocol list for properties declared
898 // in the protocol and whose ivars are synthesized.
899 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
900 PE = OI->protocol_end(); P != PE; ++P) {
901 ObjCProtocolDecl *PD = (*P);
902 count += CountProtocolSynthesizedIvars(PD);
903 }
904 return count;
905}
906
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000907/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
908ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
909 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
910 I = ObjCImpls.find(D);
911 if (I != ObjCImpls.end())
912 return cast<ObjCImplementationDecl>(I->second);
913 return 0;
914}
915/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
916ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
917 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
918 I = ObjCImpls.find(D);
919 if (I != ObjCImpls.end())
920 return cast<ObjCCategoryImplDecl>(I->second);
921 return 0;
922}
923
924/// \brief Set the implementation of ObjCInterfaceDecl.
925void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
926 ObjCImplementationDecl *ImplD) {
927 assert(IFaceD && ImplD && "Passed null params");
928 ObjCImpls[IFaceD] = ImplD;
929}
930/// \brief Set the implementation of ObjCCategoryDecl.
931void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
932 ObjCCategoryImplDecl *ImplD) {
933 assert(CatD && ImplD && "Passed null params");
934 ObjCImpls[CatD] = ImplD;
935}
936
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000937/// \brief Allocate an uninitialized DeclaratorInfo.
938///
939/// The caller should initialize the memory held by DeclaratorInfo using
940/// the TypeLoc wrappers.
941///
942/// \param T the type that will be the basis for type source info. This type
943/// should refer to how the declarator was written in source code, not to
944/// what type semantic analysis resolved the declarator to.
John McCall26fe7e02009-10-21 00:23:54 +0000945DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T,
946 unsigned DataSize) {
947 if (!DataSize)
948 DataSize = TypeLoc::getFullDataSizeForType(T);
949 else
950 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
951 "incorrect data size provided to CreateDeclaratorInfo!");
952
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000953 DeclaratorInfo *DInfo =
954 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
955 new (DInfo) DeclaratorInfo(T);
956 return DInfo;
957}
958
John McCall3665e002009-10-23 21:14:09 +0000959DeclaratorInfo *ASTContext::getTrivialDeclaratorInfo(QualType T,
960 SourceLocation L) {
961 DeclaratorInfo *DI = CreateDeclaratorInfo(T);
962 DI->getTypeLoc().initialize(L);
963 return DI;
964}
965
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000966/// getInterfaceLayoutImpl - Get or compute information about the
967/// layout of the given interface.
968///
969/// \param Impl - If given, also include the layout of the interface's
970/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000971const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000972ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
973 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000974 assert(!D->isForwardDecl() && "Invalid interface decl!");
975
Devang Pateldbb72632008-06-04 21:54:36 +0000976 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000977 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000978 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
979 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
980 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000981
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000982 // Add in synthesized ivar count if laying out an implementation.
983 if (Impl) {
Anders Carlssona4267a62009-07-18 21:19:52 +0000984 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000985 unsigned SynthCount = CountSynthesizedIvars(D);
986 FieldCount += SynthCount;
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000987 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000988 // entry. Note we can't cache this because we simply free all
989 // entries later; however we shouldn't look up implementations
990 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000991 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000992 return getObjCLayout(D, 0);
993 }
994
Mike Stump11289f42009-09-09 15:08:12 +0000995 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000996 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
997 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000998
Devang Pateldbb72632008-06-04 21:54:36 +0000999 return *NewEntry;
1000}
1001
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +00001002const ASTRecordLayout &
1003ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1004 return getObjCLayout(D, 0);
1005}
1006
1007const ASTRecordLayout &
1008ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1009 return getObjCLayout(D->getClassInterface(), D);
1010}
1011
Devang Patele11664a2007-11-01 19:11:01 +00001012/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +00001013/// specified record (struct/union/class), which indicates its size and field
1014/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +00001015const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek21475702008-09-05 17:16:31 +00001016 D = D->getDefinition(*this);
1017 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +00001018
Chris Lattner53cfe802007-07-18 17:52:12 +00001019 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +00001020 // Note that we can't save a reference to the entry because this function
1021 // is recursive.
1022 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +00001023 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +00001024
Mike Stump11289f42009-09-09 15:08:12 +00001025 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +00001026 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +00001027 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +00001028
Chris Lattner647fb222007-07-18 18:26:58 +00001029 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +00001030}
1031
Chris Lattner983a8bb2007-07-13 22:13:22 +00001032//===----------------------------------------------------------------------===//
1033// Type creation/memoization methods
1034//===----------------------------------------------------------------------===//
1035
John McCall8ccfcb52009-09-24 19:53:00 +00001036QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1037 unsigned Fast = Quals.getFastQualifiers();
1038 Quals.removeFastQualifiers();
1039
1040 // Check if we've already instantiated this type.
1041 llvm::FoldingSetNodeID ID;
1042 ExtQuals::Profile(ID, TypeNode, Quals);
1043 void *InsertPos = 0;
1044 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1045 assert(EQ->getQualifiers() == Quals);
1046 QualType T = QualType(EQ, Fast);
1047 return T;
1048 }
1049
John McCall90d1c2d2009-09-24 23:30:46 +00001050 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001051 ExtQualNodes.InsertNode(New, InsertPos);
1052 QualType T = QualType(New, Fast);
1053 return T;
1054}
1055
1056QualType ASTContext::getVolatileType(QualType T) {
1057 QualType CanT = getCanonicalType(T);
1058 if (CanT.isVolatileQualified()) return T;
1059
1060 QualifierCollector Quals;
1061 const Type *TypeNode = Quals.strip(T);
1062 Quals.addVolatile();
1063
1064 return getExtQualType(TypeNode, Quals);
1065}
1066
Fariborz Jahanianece85822009-02-17 18:27:45 +00001067QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001068 QualType CanT = getCanonicalType(T);
1069 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001070 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001071
John McCall8ccfcb52009-09-24 19:53:00 +00001072 // If we are composing extended qualifiers together, merge together
1073 // into one ExtQuals node.
1074 QualifierCollector Quals;
1075 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001076
John McCall8ccfcb52009-09-24 19:53:00 +00001077 // If this type already has an address space specified, it cannot get
1078 // another one.
1079 assert(!Quals.hasAddressSpace() &&
1080 "Type cannot be in multiple addr spaces!");
1081 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001082
John McCall8ccfcb52009-09-24 19:53:00 +00001083 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001084}
1085
Chris Lattnerd60183d2009-02-18 22:53:11 +00001086QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001087 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001088 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001089 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001090 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001091
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001092 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001093 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001094 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001095 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1096 return getPointerType(ResultType);
1097 }
1098 }
Mike Stump11289f42009-09-09 15:08:12 +00001099
John McCall8ccfcb52009-09-24 19:53:00 +00001100 // If we are composing extended qualifiers together, merge together
1101 // into one ExtQuals node.
1102 QualifierCollector Quals;
1103 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001104
John McCall8ccfcb52009-09-24 19:53:00 +00001105 // If this type already has an ObjCGC specified, it cannot get
1106 // another one.
1107 assert(!Quals.hasObjCGCAttr() &&
1108 "Type cannot have multiple ObjCGCs!");
1109 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001110
John McCall8ccfcb52009-09-24 19:53:00 +00001111 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001112}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001113
Mike Stump8c5d7992009-07-25 21:26:53 +00001114QualType ASTContext::getNoReturnType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00001115 QualType ResultType;
Mike Stump8c5d7992009-07-25 21:26:53 +00001116 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001117 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001118 ResultType = getNoReturnType(Pointee);
Mike Stumpea086c72009-07-25 23:24:03 +00001119 ResultType = getPointerType(ResultType);
John McCall8ccfcb52009-09-24 19:53:00 +00001120 } else if (T->isBlockPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001121 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001122 ResultType = getNoReturnType(Pointee);
Mike Stumpea086c72009-07-25 23:24:03 +00001123 ResultType = getBlockPointerType(ResultType);
John McCall8ccfcb52009-09-24 19:53:00 +00001124 } else {
1125 assert (T->isFunctionType()
1126 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump11289f42009-09-09 15:08:12 +00001127
Benjamin Kramer07fec3b2009-09-25 11:47:22 +00001128 if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) {
1129 ResultType = getFunctionNoProtoType(FNPT->getResultType(), true);
John McCall8ccfcb52009-09-24 19:53:00 +00001130 } else {
1131 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1132 ResultType
1133 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1134 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1135 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1136 F->getNumExceptions(), F->exception_begin(), true);
1137 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001138 }
John McCall8ccfcb52009-09-24 19:53:00 +00001139
1140 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump8c5d7992009-07-25 21:26:53 +00001141}
1142
Chris Lattnerc6395932007-06-22 20:56:16 +00001143/// getComplexType - Return the uniqued reference to the type for a complex
1144/// number with the specified element type.
1145QualType ASTContext::getComplexType(QualType T) {
1146 // Unique pointers, to guarantee there is only one pointer of a particular
1147 // structure.
1148 llvm::FoldingSetNodeID ID;
1149 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001150
Chris Lattnerc6395932007-06-22 20:56:16 +00001151 void *InsertPos = 0;
1152 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1153 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001154
Chris Lattnerc6395932007-06-22 20:56:16 +00001155 // If the pointee type isn't canonical, this won't be a canonical type either,
1156 // so fill in the canonical type field.
1157 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001158 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001159 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001160
Chris Lattnerc6395932007-06-22 20:56:16 +00001161 // Get the new insert position for the node we care about.
1162 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001163 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001164 }
John McCall90d1c2d2009-09-24 23:30:46 +00001165 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001166 Types.push_back(New);
1167 ComplexTypes.InsertNode(New, InsertPos);
1168 return QualType(New, 0);
1169}
1170
Eli Friedman1efaaea2009-02-13 02:31:07 +00001171QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1172 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1173 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1174 FixedWidthIntType *&Entry = Map[Width];
1175 if (!Entry)
1176 Entry = new FixedWidthIntType(Width, Signed);
1177 return QualType(Entry, 0);
1178}
Chris Lattnerc6395932007-06-22 20:56:16 +00001179
Chris Lattner970e54e2006-11-12 00:37:36 +00001180/// getPointerType - Return the uniqued reference to the type for a pointer to
1181/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001182QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001183 // Unique pointers, to guarantee there is only one pointer of a particular
1184 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001185 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001186 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001187
Chris Lattner67521df2007-01-27 01:29:36 +00001188 void *InsertPos = 0;
1189 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001190 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001191
Chris Lattner7ccecb92006-11-12 08:50:50 +00001192 // If the pointee type isn't canonical, this won't be a canonical type either,
1193 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001194 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001195 if (!T.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001196 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001197
Chris Lattner67521df2007-01-27 01:29:36 +00001198 // Get the new insert position for the node we care about.
1199 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001200 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001201 }
John McCall90d1c2d2009-09-24 23:30:46 +00001202 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001203 Types.push_back(New);
1204 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001205 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001206}
1207
Mike Stump11289f42009-09-09 15:08:12 +00001208/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001209/// a pointer to the specified block.
1210QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001211 assert(T->isFunctionType() && "block of function types only");
1212 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001213 // structure.
1214 llvm::FoldingSetNodeID ID;
1215 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001216
Steve Naroffec33ed92008-08-27 16:04:49 +00001217 void *InsertPos = 0;
1218 if (BlockPointerType *PT =
1219 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1220 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001221
1222 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001223 // type either so fill in the canonical type field.
1224 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001225 if (!T.isCanonical()) {
Steve Naroffec33ed92008-08-27 16:04:49 +00001226 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001227
Steve Naroffec33ed92008-08-27 16:04:49 +00001228 // Get the new insert position for the node we care about.
1229 BlockPointerType *NewIP =
1230 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001231 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001232 }
John McCall90d1c2d2009-09-24 23:30:46 +00001233 BlockPointerType *New
1234 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001235 Types.push_back(New);
1236 BlockPointerTypes.InsertNode(New, InsertPos);
1237 return QualType(New, 0);
1238}
1239
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001240/// getLValueReferenceType - Return the uniqued reference to the type for an
1241/// lvalue reference to the specified type.
John McCallfc93cf92009-10-22 22:37:11 +00001242QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Bill Wendling3708c182007-05-27 10:15:43 +00001243 // Unique pointers, to guarantee there is only one pointer of a particular
1244 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001245 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001246 ReferenceType::Profile(ID, T, SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001247
1248 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001249 if (LValueReferenceType *RT =
1250 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001251 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001252
John McCallfc93cf92009-10-22 22:37:11 +00001253 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1254
Bill Wendling3708c182007-05-27 10:15:43 +00001255 // If the referencee type isn't canonical, this won't be a canonical type
1256 // either, so fill in the canonical type field.
1257 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001258 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1259 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1260 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001261
Bill Wendling3708c182007-05-27 10:15:43 +00001262 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001263 LValueReferenceType *NewIP =
1264 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001265 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001266 }
1267
John McCall90d1c2d2009-09-24 23:30:46 +00001268 LValueReferenceType *New
John McCallfc93cf92009-10-22 22:37:11 +00001269 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1270 SpelledAsLValue);
Bill Wendling3708c182007-05-27 10:15:43 +00001271 Types.push_back(New);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001272 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCallfc93cf92009-10-22 22:37:11 +00001273
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001274 return QualType(New, 0);
1275}
1276
1277/// getRValueReferenceType - Return the uniqued reference to the type for an
1278/// rvalue reference to the specified type.
1279QualType ASTContext::getRValueReferenceType(QualType T) {
1280 // Unique pointers, to guarantee there is only one pointer of a particular
1281 // structure.
1282 llvm::FoldingSetNodeID ID;
John McCallfc93cf92009-10-22 22:37:11 +00001283 ReferenceType::Profile(ID, T, false);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001284
1285 void *InsertPos = 0;
1286 if (RValueReferenceType *RT =
1287 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1288 return QualType(RT, 0);
1289
John McCallfc93cf92009-10-22 22:37:11 +00001290 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1291
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001292 // If the referencee type isn't canonical, this won't be a canonical type
1293 // either, so fill in the canonical type field.
1294 QualType Canonical;
John McCallfc93cf92009-10-22 22:37:11 +00001295 if (InnerRef || !T.isCanonical()) {
1296 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1297 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001298
1299 // Get the new insert position for the node we care about.
1300 RValueReferenceType *NewIP =
1301 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1302 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1303 }
1304
John McCall90d1c2d2009-09-24 23:30:46 +00001305 RValueReferenceType *New
1306 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001307 Types.push_back(New);
1308 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001309 return QualType(New, 0);
1310}
1311
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001312/// getMemberPointerType - Return the uniqued reference to the type for a
1313/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001314QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001315 // Unique pointers, to guarantee there is only one pointer of a particular
1316 // structure.
1317 llvm::FoldingSetNodeID ID;
1318 MemberPointerType::Profile(ID, T, Cls);
1319
1320 void *InsertPos = 0;
1321 if (MemberPointerType *PT =
1322 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1323 return QualType(PT, 0);
1324
1325 // If the pointee or class type isn't canonical, this won't be a canonical
1326 // type either, so fill in the canonical type field.
1327 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001328 if (!T.isCanonical()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001329 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1330
1331 // Get the new insert position for the node we care about.
1332 MemberPointerType *NewIP =
1333 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1334 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1335 }
John McCall90d1c2d2009-09-24 23:30:46 +00001336 MemberPointerType *New
1337 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001338 Types.push_back(New);
1339 MemberPointerTypes.InsertNode(New, InsertPos);
1340 return QualType(New, 0);
1341}
1342
Mike Stump11289f42009-09-09 15:08:12 +00001343/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001344/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001345QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001346 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001347 ArrayType::ArraySizeModifier ASM,
1348 unsigned EltTypeQuals) {
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001349 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1350 "Constant array of VLAs is illegal!");
1351
Chris Lattnere2df3f92009-05-13 04:12:56 +00001352 // Convert the array size into a canonical width matching the pointer size for
1353 // the target.
1354 llvm::APInt ArySize(ArySizeIn);
1355 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001356
Chris Lattner23b7eb62007-06-15 23:05:46 +00001357 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001358 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001359
Chris Lattner36f8e652007-01-27 08:31:04 +00001360 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001361 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001362 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001363 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001364
Chris Lattner7ccecb92006-11-12 08:50:50 +00001365 // If the element type isn't canonical, this won't be a canonical type either,
1366 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001367 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001368 if (!EltTy.isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001369 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001370 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001371 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001372 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001373 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001374 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001375 }
Mike Stump11289f42009-09-09 15:08:12 +00001376
John McCall90d1c2d2009-09-24 23:30:46 +00001377 ConstantArrayType *New = new(*this,TypeAlignment)
1378 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001379 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001380 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001381 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001382}
1383
Steve Naroffcadebd02007-08-30 18:14:25 +00001384/// getVariableArrayType - Returns a non-unique reference to the type for a
1385/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001386QualType ASTContext::getVariableArrayType(QualType EltTy,
1387 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001388 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001389 unsigned EltTypeQuals,
1390 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001391 // Since we don't unique expressions, it isn't possible to unique VLA's
1392 // that have an expression provided for their size.
1393
John McCall90d1c2d2009-09-24 23:30:46 +00001394 VariableArrayType *New = new(*this, TypeAlignment)
1395 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001396
1397 VariableArrayTypes.push_back(New);
1398 Types.push_back(New);
1399 return QualType(New, 0);
1400}
1401
Douglas Gregor4619e432008-12-05 23:32:09 +00001402/// getDependentSizedArrayType - Returns a non-unique reference to
1403/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001404/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001405QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1406 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001407 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001408 unsigned EltTypeQuals,
1409 SourceRange Brackets) {
Mike Stump11289f42009-09-09 15:08:12 +00001410 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001411 "Size must be type- or value-dependent!");
1412
Douglas Gregorf3f95522009-07-31 00:23:35 +00001413 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001414 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregorf3f95522009-07-31 00:23:35 +00001415 EltTypeQuals, NumElts);
Douglas Gregor4619e432008-12-05 23:32:09 +00001416
Douglas Gregorf3f95522009-07-31 00:23:35 +00001417 void *InsertPos = 0;
1418 DependentSizedArrayType *Canon
1419 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1420 DependentSizedArrayType *New;
1421 if (Canon) {
1422 // We already have a canonical version of this array type; use it as
1423 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001424 New = new (*this, TypeAlignment)
1425 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1426 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001427 } else {
1428 QualType CanonEltTy = getCanonicalType(EltTy);
1429 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001430 New = new (*this, TypeAlignment)
1431 DependentSizedArrayType(*this, EltTy, QualType(),
1432 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001433 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1434 } else {
1435 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1436 ASM, EltTypeQuals,
1437 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001438 New = new (*this, TypeAlignment)
1439 DependentSizedArrayType(*this, EltTy, Canon,
1440 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001441 }
1442 }
Mike Stump11289f42009-09-09 15:08:12 +00001443
Douglas Gregor4619e432008-12-05 23:32:09 +00001444 Types.push_back(New);
1445 return QualType(New, 0);
1446}
1447
Eli Friedmanbd258282008-02-15 18:16:39 +00001448QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1449 ArrayType::ArraySizeModifier ASM,
1450 unsigned EltTypeQuals) {
1451 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001452 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001453
1454 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001455 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001456 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1457 return QualType(ATP, 0);
1458
1459 // If the element type isn't canonical, this won't be a canonical type
1460 // either, so fill in the canonical type field.
1461 QualType Canonical;
1462
John McCallb692a092009-10-22 20:10:53 +00001463 if (!EltTy.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001464 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001465 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001466
1467 // Get the new insert position for the node we care about.
1468 IncompleteArrayType *NewIP =
1469 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001470 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001471 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001472
John McCall90d1c2d2009-09-24 23:30:46 +00001473 IncompleteArrayType *New = new (*this, TypeAlignment)
1474 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001475
1476 IncompleteArrayTypes.InsertNode(New, InsertPos);
1477 Types.push_back(New);
1478 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001479}
1480
Steve Naroff91fcddb2007-07-18 18:00:27 +00001481/// getVectorType - Return the unique reference to a vector type of
1482/// the specified element type and size. VectorType must be a built-in type.
1483QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001484 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001485
Chris Lattner76a00cf2008-04-06 22:59:24 +00001486 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001487 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001488
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001489 // Check if we've already instantiated a vector of this type.
1490 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001491 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001492 void *InsertPos = 0;
1493 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1494 return QualType(VTP, 0);
1495
1496 // If the element type isn't canonical, this won't be a canonical type either,
1497 // so fill in the canonical type field.
1498 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001499 if (!vecType.isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001500 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001501
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001502 // Get the new insert position for the node we care about.
1503 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001504 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001505 }
John McCall90d1c2d2009-09-24 23:30:46 +00001506 VectorType *New = new (*this, TypeAlignment)
1507 VectorType(vecType, NumElts, Canonical);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001508 VectorTypes.InsertNode(New, InsertPos);
1509 Types.push_back(New);
1510 return QualType(New, 0);
1511}
1512
Nate Begemance4d7fc2008-04-18 23:10:10 +00001513/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001514/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001515QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001516 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001517
Chris Lattner76a00cf2008-04-06 22:59:24 +00001518 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001519 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001520
Steve Naroff91fcddb2007-07-18 18:00:27 +00001521 // Check if we've already instantiated a vector of this type.
1522 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001523 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001524 void *InsertPos = 0;
1525 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1526 return QualType(VTP, 0);
1527
1528 // If the element type isn't canonical, this won't be a canonical type either,
1529 // so fill in the canonical type field.
1530 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001531 if (!vecType.isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001532 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001533
Steve Naroff91fcddb2007-07-18 18:00:27 +00001534 // Get the new insert position for the node we care about.
1535 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001536 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001537 }
John McCall90d1c2d2009-09-24 23:30:46 +00001538 ExtVectorType *New = new (*this, TypeAlignment)
1539 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001540 VectorTypes.InsertNode(New, InsertPos);
1541 Types.push_back(New);
1542 return QualType(New, 0);
1543}
1544
Mike Stump11289f42009-09-09 15:08:12 +00001545QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001546 Expr *SizeExpr,
1547 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001548 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001549 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001550 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001551
Douglas Gregor352169a2009-07-31 03:54:25 +00001552 void *InsertPos = 0;
1553 DependentSizedExtVectorType *Canon
1554 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1555 DependentSizedExtVectorType *New;
1556 if (Canon) {
1557 // We already have a canonical version of this array type; use it as
1558 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001559 New = new (*this, TypeAlignment)
1560 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1561 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001562 } else {
1563 QualType CanonVecTy = getCanonicalType(vecType);
1564 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001565 New = new (*this, TypeAlignment)
1566 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1567 AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001568 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1569 } else {
1570 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1571 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001572 New = new (*this, TypeAlignment)
1573 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001574 }
1575 }
Mike Stump11289f42009-09-09 15:08:12 +00001576
Douglas Gregor758a8692009-06-17 21:51:59 +00001577 Types.push_back(New);
1578 return QualType(New, 0);
1579}
1580
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001581/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001582///
Mike Stump8c5d7992009-07-25 21:26:53 +00001583QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001584 // Unique functions, to guarantee there is only one function of a particular
1585 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001586 llvm::FoldingSetNodeID ID;
Mike Stump8c5d7992009-07-25 21:26:53 +00001587 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump11289f42009-09-09 15:08:12 +00001588
Chris Lattner47955de2007-01-27 08:37:20 +00001589 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001590 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001591 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001592 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001593
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001594 QualType Canonical;
John McCallb692a092009-10-22 20:10:53 +00001595 if (!ResultTy.isCanonical()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001596 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump11289f42009-09-09 15:08:12 +00001597
Chris Lattner47955de2007-01-27 08:37:20 +00001598 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001599 FunctionNoProtoType *NewIP =
1600 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001601 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001602 }
Mike Stump11289f42009-09-09 15:08:12 +00001603
John McCall90d1c2d2009-09-24 23:30:46 +00001604 FunctionNoProtoType *New = new (*this, TypeAlignment)
1605 FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Chris Lattner47955de2007-01-27 08:37:20 +00001606 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001607 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001608 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001609}
1610
1611/// getFunctionType - Return a normal function type with a typed argument
1612/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001613QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001614 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001615 unsigned TypeQuals, bool hasExceptionSpec,
1616 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump8c5d7992009-07-25 21:26:53 +00001617 const QualType *ExArray, bool NoReturn) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001618 // Unique functions, to guarantee there is only one function of a particular
1619 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001620 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001621 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001622 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump8c5d7992009-07-25 21:26:53 +00001623 NumExs, ExArray, NoReturn);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001624
1625 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001626 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001627 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001628 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001629
1630 // Determine whether the type being created is already canonical or not.
John McCallfc93cf92009-10-22 22:37:11 +00001631 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001632 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001633 if (!ArgArray[i].isCanonicalAsParam())
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001634 isCanonical = false;
1635
1636 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001637 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001638 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001639 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001640 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001641 CanonicalArgs.reserve(NumArgs);
1642 for (unsigned i = 0; i != NumArgs; ++i)
John McCallfc93cf92009-10-22 22:37:11 +00001643 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001644
Chris Lattner76a00cf2008-04-06 22:59:24 +00001645 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001646 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001647 isVariadic, TypeQuals, false,
1648 false, 0, 0, NoReturn);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001649
Chris Lattnerfd4de792007-01-27 01:15:32 +00001650 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001651 FunctionProtoType *NewIP =
1652 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001653 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001654 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001655
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001656 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001657 // for two variable size arrays (for parameter and exception types) at the
1658 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001659 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001660 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1661 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001662 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001663 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001664 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump8c5d7992009-07-25 21:26:53 +00001665 ExArray, NumExs, Canonical, NoReturn);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001666 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001667 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001668 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001669}
Chris Lattneref51c202006-11-10 07:17:23 +00001670
Douglas Gregor83a586e2008-04-13 21:07:44 +00001671/// getTypeDeclType - Return the unique reference to the type for the
1672/// specified type declaration.
Ted Kremenek21475702008-09-05 17:16:31 +00001673QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001674 assert(Decl && "Passed null for Decl param");
Douglas Gregor83a586e2008-04-13 21:07:44 +00001675 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001676
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001677 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001678 return getTypedefType(Typedef);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001679 else if (isa<TemplateTypeParmDecl>(Decl)) {
1680 assert(false && "Template type parameter types are always available.");
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001681 } else if (ObjCInterfaceDecl *ObjCInterface
1682 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001683 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001684
Douglas Gregor89ee6822009-02-28 01:32:25 +00001685 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek721e2392009-01-19 21:31:22 +00001686 if (PrevDecl)
1687 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff096bab72009-01-27 22:08:43 +00001688 else
John McCall90d1c2d2009-09-24 23:30:46 +00001689 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001690 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek721e2392009-01-19 21:31:22 +00001691 if (PrevDecl)
1692 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff096bab72009-01-27 22:08:43 +00001693 else
John McCall90d1c2d2009-09-24 23:30:46 +00001694 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001695 } else
Douglas Gregor83a586e2008-04-13 21:07:44 +00001696 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001697
Ted Kremenek21475702008-09-05 17:16:31 +00001698 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001699 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001700}
1701
Chris Lattner32d920b2007-01-26 02:01:53 +00001702/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001703/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001704QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1705 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001706
Chris Lattner76a00cf2008-04-06 22:59:24 +00001707 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001708 Decl->TypeForDecl = new(*this, TypeAlignment)
1709 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001710 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001711 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001712}
1713
John McCallcebee162009-10-18 09:09:24 +00001714/// \brief Retrieve a substitution-result type.
1715QualType
1716ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1717 QualType Replacement) {
John McCallb692a092009-10-22 20:10:53 +00001718 assert(Replacement.isCanonical()
John McCallcebee162009-10-18 09:09:24 +00001719 && "replacement types must always be canonical");
1720
1721 llvm::FoldingSetNodeID ID;
1722 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1723 void *InsertPos = 0;
1724 SubstTemplateTypeParmType *SubstParm
1725 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1726
1727 if (!SubstParm) {
1728 SubstParm = new (*this, TypeAlignment)
1729 SubstTemplateTypeParmType(Parm, Replacement);
1730 Types.push_back(SubstParm);
1731 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1732 }
1733
1734 return QualType(SubstParm, 0);
1735}
1736
Douglas Gregoreff93e02009-02-05 23:33:38 +00001737/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001738/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001739/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001740QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001741 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001742 IdentifierInfo *Name) {
1743 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001744 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001745 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001746 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001747 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1748
1749 if (TypeParm)
1750 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001751
Anders Carlsson90036dc2009-06-16 00:30:48 +00001752 if (Name) {
1753 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001754 TypeParm = new (*this, TypeAlignment)
1755 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Anders Carlsson90036dc2009-06-16 00:30:48 +00001756 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001757 TypeParm = new (*this, TypeAlignment)
1758 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001759
1760 Types.push_back(TypeParm);
1761 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1762
1763 return QualType(TypeParm, 0);
1764}
1765
Mike Stump11289f42009-09-09 15:08:12 +00001766QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001767ASTContext::getTemplateSpecializationType(TemplateName Template,
John McCall0ad16662009-10-29 08:12:44 +00001768 const TemplateArgumentLoc *Args,
1769 unsigned NumArgs,
1770 QualType Canon) {
1771 llvm::SmallVector<TemplateArgument, 4> ArgVec;
1772 ArgVec.reserve(NumArgs);
1773 for (unsigned i = 0; i != NumArgs; ++i)
1774 ArgVec.push_back(Args[i].getArgument());
1775
1776 return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon);
1777}
1778
1779QualType
1780ASTContext::getTemplateSpecializationType(TemplateName Template,
Douglas Gregordc572a32009-03-30 22:58:21 +00001781 const TemplateArgument *Args,
1782 unsigned NumArgs,
1783 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001784 if (!Canon.isNull())
1785 Canon = getCanonicalType(Canon);
1786 else {
1787 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001788 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1789 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1790 CanonArgs.reserve(NumArgs);
1791 for (unsigned I = 0; I != NumArgs; ++I)
1792 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1793
1794 // Determine whether this canonical template specialization type already
1795 // exists.
1796 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001797 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor00044172009-07-29 16:09:57 +00001798 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001799
1800 void *InsertPos = 0;
1801 TemplateSpecializationType *Spec
1802 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001803
Douglas Gregora8e02e72009-07-28 23:00:59 +00001804 if (!Spec) {
1805 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001806 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001807 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001808 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001809 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001810 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001811 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001812 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001813 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001814 }
Mike Stump11289f42009-09-09 15:08:12 +00001815
Douglas Gregor15301382009-07-30 17:40:51 +00001816 if (Canon.isNull())
1817 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001818 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001819 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001820 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001821
Douglas Gregora8e02e72009-07-28 23:00:59 +00001822 // Allocate the (non-canonical) template specialization type, but don't
1823 // try to unique it: these types typically have location information that
1824 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001825 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001826 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001827 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001828 TemplateSpecializationType *Spec
1829 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001830 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001831
Douglas Gregor8bf42052009-02-09 18:46:07 +00001832 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001833 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001834}
1835
Mike Stump11289f42009-09-09 15:08:12 +00001836QualType
Douglas Gregorf21eb492009-03-26 23:50:42 +00001837ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor52537682009-03-19 00:18:19 +00001838 QualType NamedType) {
1839 llvm::FoldingSetNodeID ID;
Douglas Gregorf21eb492009-03-26 23:50:42 +00001840 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001841
1842 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001843 QualifiedNameType *T
Douglas Gregor52537682009-03-19 00:18:19 +00001844 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1845 if (T)
1846 return QualType(T, 0);
1847
Mike Stump11289f42009-09-09 15:08:12 +00001848 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorf21eb492009-03-26 23:50:42 +00001849 getCanonicalType(NamedType));
Douglas Gregor52537682009-03-19 00:18:19 +00001850 Types.push_back(T);
1851 QualifiedNameTypes.InsertNode(T, InsertPos);
1852 return QualType(T, 0);
1853}
1854
Mike Stump11289f42009-09-09 15:08:12 +00001855QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor333489b2009-03-27 23:10:48 +00001856 const IdentifierInfo *Name,
1857 QualType Canon) {
1858 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1859
1860 if (Canon.isNull()) {
1861 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1862 if (CanonNNS != NNS)
1863 Canon = getTypenameType(CanonNNS, Name);
1864 }
1865
1866 llvm::FoldingSetNodeID ID;
1867 TypenameType::Profile(ID, NNS, Name);
1868
1869 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001870 TypenameType *T
Douglas Gregor333489b2009-03-27 23:10:48 +00001871 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1872 if (T)
1873 return QualType(T, 0);
1874
1875 T = new (*this) TypenameType(NNS, Name, Canon);
1876 Types.push_back(T);
1877 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001878 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001879}
1880
Mike Stump11289f42009-09-09 15:08:12 +00001881QualType
1882ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregordce2b622009-04-01 00:28:59 +00001883 const TemplateSpecializationType *TemplateId,
1884 QualType Canon) {
1885 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1886
1887 if (Canon.isNull()) {
1888 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1889 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1890 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1891 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00001892 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00001893 assert(CanonTemplateId &&
1894 "Canonical type must also be a template specialization type");
1895 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1896 }
1897 }
1898
1899 llvm::FoldingSetNodeID ID;
1900 TypenameType::Profile(ID, NNS, TemplateId);
1901
1902 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001903 TypenameType *T
Douglas Gregordce2b622009-04-01 00:28:59 +00001904 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1905 if (T)
1906 return QualType(T, 0);
1907
1908 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1909 Types.push_back(T);
1910 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001911 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00001912}
1913
John McCallfcc33b02009-09-05 00:15:47 +00001914QualType
1915ASTContext::getElaboratedType(QualType UnderlyingType,
1916 ElaboratedType::TagKind Tag) {
1917 llvm::FoldingSetNodeID ID;
1918 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump11289f42009-09-09 15:08:12 +00001919
John McCallfcc33b02009-09-05 00:15:47 +00001920 void *InsertPos = 0;
1921 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1922 if (T)
1923 return QualType(T, 0);
1924
1925 QualType Canon = getCanonicalType(UnderlyingType);
1926
1927 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1928 Types.push_back(T);
1929 ElaboratedTypes.InsertNode(T, InsertPos);
1930 return QualType(T, 0);
1931}
1932
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001933/// CmpProtocolNames - Comparison predicate for sorting protocols
1934/// alphabetically.
1935static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1936 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00001937 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001938}
1939
John McCallfc93cf92009-10-22 22:37:11 +00001940static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
1941 unsigned NumProtocols) {
1942 if (NumProtocols == 0) return true;
1943
1944 for (unsigned i = 1; i != NumProtocols; ++i)
1945 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
1946 return false;
1947 return true;
1948}
1949
1950static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001951 unsigned &NumProtocols) {
1952 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00001953
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001954 // Sort protocols, keyed by name.
1955 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1956
1957 // Remove duplicates.
1958 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1959 NumProtocols = ProtocolsEnd-Protocols;
1960}
1961
Steve Narofffb4330f2009-06-17 22:40:22 +00001962/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1963/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00001964QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00001965 ObjCProtocolDecl **Protocols,
Steve Narofffb4330f2009-06-17 22:40:22 +00001966 unsigned NumProtocols) {
Steve Narofffb4330f2009-06-17 22:40:22 +00001967 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00001968 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00001969
1970 void *InsertPos = 0;
1971 if (ObjCObjectPointerType *QT =
1972 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1973 return QualType(QT, 0);
1974
John McCallfc93cf92009-10-22 22:37:11 +00001975 // Sort the protocol list alphabetically to canonicalize it.
1976 QualType Canonical;
1977 if (!InterfaceT.isCanonical() ||
1978 !areSortedAndUniqued(Protocols, NumProtocols)) {
1979 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
1980 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
1981 unsigned UniqueCount = NumProtocols;
1982
1983 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
1984 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
1985
1986 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
1987 &Sorted[0], UniqueCount);
1988 } else {
1989 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
1990 Protocols, NumProtocols);
1991 }
1992
1993 // Regenerate InsertPos.
1994 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1995 }
1996
Steve Narofffb4330f2009-06-17 22:40:22 +00001997 // No Match;
John McCall90d1c2d2009-09-24 23:30:46 +00001998 ObjCObjectPointerType *QType = new (*this, TypeAlignment)
John McCallfc93cf92009-10-22 22:37:11 +00001999 ObjCObjectPointerType(Canonical, InterfaceT, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002000
Steve Narofffb4330f2009-06-17 22:40:22 +00002001 Types.push_back(QType);
2002 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
2003 return QualType(QType, 0);
2004}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00002005
Steve Naroffc277ad12009-07-18 15:33:26 +00002006/// getObjCInterfaceType - Return the unique reference to the type for the
2007/// specified ObjC interface decl. The list of protocols is optional.
2008QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002009 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002010 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00002011 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00002012
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002013 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00002014 if (ObjCInterfaceType *QT =
2015 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002016 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00002017
John McCallfc93cf92009-10-22 22:37:11 +00002018 // Sort the protocol list alphabetically to canonicalize it.
2019 QualType Canonical;
2020 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2021 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2022 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2023
2024 unsigned UniqueCount = NumProtocols;
2025 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2026
2027 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2028
2029 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2030 }
2031
John McCall90d1c2d2009-09-24 23:30:46 +00002032 ObjCInterfaceType *QType = new (*this, TypeAlignment)
John McCallfc93cf92009-10-22 22:37:11 +00002033 ObjCInterfaceType(Canonical, const_cast<ObjCInterfaceDecl*>(Decl),
John McCall90d1c2d2009-09-24 23:30:46 +00002034 Protocols, NumProtocols);
John McCallfc93cf92009-10-22 22:37:11 +00002035
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002036 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00002037 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002038 return QualType(QType, 0);
2039}
2040
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002041/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2042/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00002043/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002044/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002045/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002046QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002047 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002048 if (tofExpr->isTypeDependent()) {
2049 llvm::FoldingSetNodeID ID;
2050 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002051
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002052 void *InsertPos = 0;
2053 DependentTypeOfExprType *Canon
2054 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2055 if (Canon) {
2056 // We already have a "canonical" version of an identical, dependent
2057 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002058 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002059 QualType((TypeOfExprType*)Canon, 0));
2060 }
2061 else {
2062 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002063 Canon
2064 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002065 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2066 toe = Canon;
2067 }
2068 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002069 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002070 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002071 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002072 Types.push_back(toe);
2073 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002074}
2075
Steve Naroffa773cd52007-08-01 18:02:17 +00002076/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2077/// TypeOfType AST's. The only motivation to unique these nodes would be
2078/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002079/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002080/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002081QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002082 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002083 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002084 Types.push_back(tot);
2085 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002086}
2087
Anders Carlssonad6bd352009-06-24 21:24:56 +00002088/// getDecltypeForExpr - Given an expr, will return the decltype for that
2089/// expression, according to the rules in C++0x [dcl.type.simple]p4
2090static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002091 if (e->isTypeDependent())
2092 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002093
Anders Carlssonad6bd352009-06-24 21:24:56 +00002094 // If e is an id expression or a class member access, decltype(e) is defined
2095 // as the type of the entity named by e.
2096 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2097 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2098 return VD->getType();
2099 }
2100 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2101 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2102 return FD->getType();
2103 }
2104 // If e is a function call or an invocation of an overloaded operator,
2105 // (parentheses around e are ignored), decltype(e) is defined as the
2106 // return type of that function.
2107 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2108 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002109
Anders Carlssonad6bd352009-06-24 21:24:56 +00002110 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002111
2112 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002113 // defined as T&, otherwise decltype(e) is defined as T.
2114 if (e->isLvalue(Context) == Expr::LV_Valid)
2115 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002116
Anders Carlssonad6bd352009-06-24 21:24:56 +00002117 return T;
2118}
2119
Anders Carlsson81df7b82009-06-24 19:06:50 +00002120/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2121/// DecltypeType AST's. The only motivation to unique these nodes would be
2122/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002123/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002124/// on canonical type's (which are always unique).
2125QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002126 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002127 if (e->isTypeDependent()) {
2128 llvm::FoldingSetNodeID ID;
2129 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002130
Douglas Gregora21f6c32009-07-30 23:36:40 +00002131 void *InsertPos = 0;
2132 DependentDecltypeType *Canon
2133 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2134 if (Canon) {
2135 // We already have a "canonical" version of an equivalent, dependent
2136 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002137 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002138 QualType((DecltypeType*)Canon, 0));
2139 }
2140 else {
2141 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002142 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002143 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2144 dt = Canon;
2145 }
2146 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002147 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002148 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002149 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002150 Types.push_back(dt);
2151 return QualType(dt, 0);
2152}
2153
Chris Lattnerfb072462007-01-23 05:45:31 +00002154/// getTagDeclType - Return the unique reference to the type for the
2155/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002156QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002157 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002158 // FIXME: What is the design on getTagDeclType when it requires casting
2159 // away const? mutable?
2160 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002161}
2162
Mike Stump11289f42009-09-09 15:08:12 +00002163/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2164/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2165/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00002166QualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002167 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002168}
Chris Lattnerfb072462007-01-23 05:45:31 +00002169
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002170/// getSignedWCharType - Return the type of "signed wchar_t".
2171/// Used when in C++, as a GCC extension.
2172QualType ASTContext::getSignedWCharType() const {
2173 // FIXME: derive from "Target" ?
2174 return WCharTy;
2175}
2176
2177/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2178/// Used when in C++, as a GCC extension.
2179QualType ASTContext::getUnsignedWCharType() const {
2180 // FIXME: derive from "Target" ?
2181 return UnsignedIntTy;
2182}
2183
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002184/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2185/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2186QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002187 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002188}
2189
Chris Lattnera21ad802008-04-02 05:18:44 +00002190//===----------------------------------------------------------------------===//
2191// Type Operators
2192//===----------------------------------------------------------------------===//
2193
John McCallfc93cf92009-10-22 22:37:11 +00002194CanQualType ASTContext::getCanonicalParamType(QualType T) {
2195 // Push qualifiers into arrays, and then discard any remaining
2196 // qualifiers.
2197 T = getCanonicalType(T);
2198 const Type *Ty = T.getTypePtr();
2199
2200 QualType Result;
2201 if (isa<ArrayType>(Ty)) {
2202 Result = getArrayDecayedType(QualType(Ty,0));
2203 } else if (isa<FunctionType>(Ty)) {
2204 Result = getPointerType(QualType(Ty, 0));
2205 } else {
2206 Result = QualType(Ty, 0);
2207 }
2208
2209 return CanQualType::CreateUnsafe(Result);
2210}
2211
Chris Lattnered0d0792008-04-06 22:41:35 +00002212/// getCanonicalType - Return the canonical (structural) type corresponding to
2213/// the specified potentially non-canonical type. The non-canonical version
2214/// of a type may have many "decorated" versions of types. Decorators can
2215/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2216/// to be free of any of these, allowing two canonical types to be compared
2217/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002218CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002219 QualifierCollector Quals;
2220 const Type *Ptr = Quals.strip(T);
2221 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002222
John McCall8ccfcb52009-09-24 19:53:00 +00002223 // The canonical internal type will be the canonical type *except*
2224 // that we push type qualifiers down through array types.
2225
2226 // If there are no new qualifiers to push down, stop here.
2227 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002228 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002229
John McCall8ccfcb52009-09-24 19:53:00 +00002230 // If the type qualifiers are on an array type, get the canonical
2231 // type of the array with the qualifiers applied to the element
2232 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002233 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2234 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002235 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002236
Chris Lattner7adf0762008-08-04 07:31:14 +00002237 // Get the canonical version of the element with the extra qualifiers on it.
2238 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002239 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002240 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002241
Chris Lattner7adf0762008-08-04 07:31:14 +00002242 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002243 return CanQualType::CreateUnsafe(
2244 getConstantArrayType(NewEltTy, CAT->getSize(),
2245 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002246 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002247 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002248 return CanQualType::CreateUnsafe(
2249 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002250 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002251
Douglas Gregor4619e432008-12-05 23:32:09 +00002252 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002253 return CanQualType::CreateUnsafe(
2254 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002255 DSAT->getSizeExpr() ?
2256 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002257 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002258 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002259 DSAT->getBracketsRange()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002260
Chris Lattner7adf0762008-08-04 07:31:14 +00002261 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002262 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002263 VAT->getSizeExpr() ?
2264 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002265 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002266 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002267 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002268}
2269
Douglas Gregor6bc50582009-05-07 06:41:52 +00002270TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2271 // If this template name refers to a template, the canonical
2272 // template name merely stores the template itself.
2273 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002274 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002275
Mike Stump11289f42009-09-09 15:08:12 +00002276 // If this template name refers to a set of overloaded function templates,
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00002277 /// the canonical template name merely stores the set of function templates.
Douglas Gregor802a0302009-07-31 05:24:01 +00002278 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2279 OverloadedFunctionDecl *CanonOvl = 0;
2280 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2281 FEnd = Ovl->function_end();
2282 F != FEnd; ++F) {
2283 Decl *Canon = F->get()->getCanonicalDecl();
2284 if (CanonOvl || Canon != F->get()) {
2285 if (!CanonOvl)
Mike Stump11289f42009-09-09 15:08:12 +00002286 CanonOvl = OverloadedFunctionDecl::Create(*this,
2287 Ovl->getDeclContext(),
Douglas Gregor802a0302009-07-31 05:24:01 +00002288 Ovl->getDeclName());
Mike Stump11289f42009-09-09 15:08:12 +00002289
Douglas Gregor802a0302009-07-31 05:24:01 +00002290 CanonOvl->addOverload(
2291 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2292 }
2293 }
Mike Stump11289f42009-09-09 15:08:12 +00002294
Douglas Gregor802a0302009-07-31 05:24:01 +00002295 return TemplateName(CanonOvl? CanonOvl : Ovl);
2296 }
Mike Stump11289f42009-09-09 15:08:12 +00002297
Douglas Gregor6bc50582009-05-07 06:41:52 +00002298 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2299 assert(DTN && "Non-dependent template names must refer to template decls.");
2300 return DTN->CanonicalTemplateName;
2301}
2302
Mike Stump11289f42009-09-09 15:08:12 +00002303TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002304ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2305 switch (Arg.getKind()) {
2306 case TemplateArgument::Null:
2307 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002308
Douglas Gregora8e02e72009-07-28 23:00:59 +00002309 case TemplateArgument::Expression:
2310 // FIXME: Build canonical expression?
2311 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002312
Douglas Gregora8e02e72009-07-28 23:00:59 +00002313 case TemplateArgument::Declaration:
John McCall0ad16662009-10-29 08:12:44 +00002314 return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002315
Douglas Gregora8e02e72009-07-28 23:00:59 +00002316 case TemplateArgument::Integral:
John McCall0ad16662009-10-29 08:12:44 +00002317 return TemplateArgument(*Arg.getAsIntegral(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002318 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002319
Douglas Gregora8e02e72009-07-28 23:00:59 +00002320 case TemplateArgument::Type:
John McCall0ad16662009-10-29 08:12:44 +00002321 return TemplateArgument(getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002322
Douglas Gregora8e02e72009-07-28 23:00:59 +00002323 case TemplateArgument::Pack: {
2324 // FIXME: Allocate in ASTContext
2325 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2326 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002327 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002328 AEnd = Arg.pack_end();
2329 A != AEnd; (void)++A, ++Idx)
2330 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002331
Douglas Gregora8e02e72009-07-28 23:00:59 +00002332 TemplateArgument Result;
2333 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2334 return Result;
2335 }
2336 }
2337
2338 // Silence GCC warning
2339 assert(false && "Unhandled template argument kind");
2340 return TemplateArgument();
2341}
2342
Douglas Gregor333489b2009-03-27 23:10:48 +00002343NestedNameSpecifier *
2344ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002345 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002346 return 0;
2347
2348 switch (NNS->getKind()) {
2349 case NestedNameSpecifier::Identifier:
2350 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002351 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002352 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2353 NNS->getAsIdentifier());
2354
2355 case NestedNameSpecifier::Namespace:
2356 // A namespace is canonical; build a nested-name-specifier with
2357 // this namespace and no prefix.
2358 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2359
2360 case NestedNameSpecifier::TypeSpec:
2361 case NestedNameSpecifier::TypeSpecWithTemplate: {
2362 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002363 return NestedNameSpecifier::Create(*this, 0,
2364 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002365 T.getTypePtr());
2366 }
2367
2368 case NestedNameSpecifier::Global:
2369 // The global specifier is canonical and unique.
2370 return NNS;
2371 }
2372
2373 // Required to silence a GCC warning
2374 return 0;
2375}
2376
Chris Lattner7adf0762008-08-04 07:31:14 +00002377
2378const ArrayType *ASTContext::getAsArrayType(QualType T) {
2379 // Handle the non-qualified case efficiently.
John McCall8ccfcb52009-09-24 19:53:00 +00002380 if (!T.hasQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002381 // Handle the common positive case fast.
2382 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2383 return AT;
2384 }
Mike Stump11289f42009-09-09 15:08:12 +00002385
John McCall8ccfcb52009-09-24 19:53:00 +00002386 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002387 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002388 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002389 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002390
John McCall8ccfcb52009-09-24 19:53:00 +00002391 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002392 // implements C99 6.7.3p8: "If the specification of an array type includes
2393 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002394
Chris Lattner7adf0762008-08-04 07:31:14 +00002395 // If we get here, we either have type qualifiers on the type, or we have
2396 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002397 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002398
John McCall8ccfcb52009-09-24 19:53:00 +00002399 QualifierCollector Qs;
2400 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002401
Chris Lattner7adf0762008-08-04 07:31:14 +00002402 // If we have a simple case, just return now.
2403 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002404 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002405 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002406
Chris Lattner7adf0762008-08-04 07:31:14 +00002407 // Otherwise, we have an array and we have qualifiers on it. Push the
2408 // qualifiers into the array element type and return a new array type.
2409 // Get the canonical version of the element with the extra qualifiers on it.
2410 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002411 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002412
Chris Lattner7adf0762008-08-04 07:31:14 +00002413 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2414 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2415 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002416 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002417 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2418 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2419 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002420 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002421
Mike Stump11289f42009-09-09 15:08:12 +00002422 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002423 = dyn_cast<DependentSizedArrayType>(ATy))
2424 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002425 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002426 DSAT->getSizeExpr() ?
2427 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002428 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002429 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002430 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002431
Chris Lattner7adf0762008-08-04 07:31:14 +00002432 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002433 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002434 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002435 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002436 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002437 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002438 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002439}
2440
2441
Chris Lattnera21ad802008-04-02 05:18:44 +00002442/// getArrayDecayedType - Return the properly qualified result of decaying the
2443/// specified array type to a pointer. This operation is non-trivial when
2444/// handling typedefs etc. The canonical type of "T" must be an array type,
2445/// this returns a pointer to a properly qualified element of the array.
2446///
2447/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2448QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002449 // Get the element type with 'getAsArrayType' so that we don't lose any
2450 // typedefs in the element type of the array. This also handles propagation
2451 // of type qualifiers from the array type into the element type if present
2452 // (C99 6.7.3p8).
2453 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2454 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002455
Chris Lattner7adf0762008-08-04 07:31:14 +00002456 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002457
2458 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002459 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002460}
2461
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002462QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002463 QualifierCollector Qs;
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002464 while (true) {
John McCall8ccfcb52009-09-24 19:53:00 +00002465 const Type *UT = Qs.strip(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002466 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2467 QT = AT->getElementType();
Mike Stumpea086c72009-07-25 23:24:03 +00002468 } else {
John McCall8ccfcb52009-09-24 19:53:00 +00002469 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002470 }
2471 }
2472}
2473
Anders Carlsson4bf82142009-09-25 01:23:32 +00002474QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2475 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002476
Anders Carlsson4bf82142009-09-25 01:23:32 +00002477 if (const ArrayType *AT = getAsArrayType(ElemTy))
2478 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002479
Anders Carlssone0808df2008-12-21 03:44:36 +00002480 return ElemTy;
2481}
2482
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002483/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002484uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002485ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2486 uint64_t ElementCount = 1;
2487 do {
2488 ElementCount *= CA->getSize().getZExtValue();
2489 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2490 } while (CA);
2491 return ElementCount;
2492}
2493
Steve Naroff0af91202007-04-27 21:51:21 +00002494/// getFloatingRank - Return a relative rank for floating point types.
2495/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002496static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002497 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002498 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002499
John McCall9dd450b2009-09-21 23:43:11 +00002500 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2501 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002502 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002503 case BuiltinType::Float: return FloatRank;
2504 case BuiltinType::Double: return DoubleRank;
2505 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002506 }
2507}
2508
Mike Stump11289f42009-09-09 15:08:12 +00002509/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2510/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002511/// 'typeDomain' is a real floating point or complex type.
2512/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002513QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2514 QualType Domain) const {
2515 FloatingRank EltRank = getFloatingRank(Size);
2516 if (Domain->isComplexType()) {
2517 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002518 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002519 case FloatRank: return FloatComplexTy;
2520 case DoubleRank: return DoubleComplexTy;
2521 case LongDoubleRank: return LongDoubleComplexTy;
2522 }
Steve Naroff0af91202007-04-27 21:51:21 +00002523 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002524
2525 assert(Domain->isRealFloatingType() && "Unknown domain!");
2526 switch (EltRank) {
2527 default: assert(0 && "getFloatingRank(): illegal value for rank");
2528 case FloatRank: return FloatTy;
2529 case DoubleRank: return DoubleTy;
2530 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002531 }
Steve Naroffe4718892007-04-27 18:30:00 +00002532}
2533
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002534/// getFloatingTypeOrder - Compare the rank of the two specified floating
2535/// point types, ignoring the domain of the type (i.e. 'double' ==
2536/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002537/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002538int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2539 FloatingRank LHSR = getFloatingRank(LHS);
2540 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002541
Chris Lattnerb90739d2008-04-06 23:38:49 +00002542 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002543 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002544 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002545 return 1;
2546 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002547}
2548
Chris Lattner76a00cf2008-04-06 22:59:24 +00002549/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2550/// routine will assert if passed a built-in type that isn't an integer or enum,
2551/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002552unsigned ASTContext::getIntegerRank(Type *T) {
John McCallb692a092009-10-22 20:10:53 +00002553 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002554 if (EnumType* ET = dyn_cast<EnumType>(T))
2555 T = ET->getDecl()->getIntegerType().getTypePtr();
2556
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002557 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2558 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2559
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002560 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2561 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2562
2563 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2564 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2565
Eli Friedman1efaaea2009-02-13 02:31:07 +00002566 // There are two things which impact the integer rank: the width, and
2567 // the ordering of builtins. The builtin ordering is encoded in the
2568 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner15ba9492009-06-14 01:54:56 +00002569 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedman1efaaea2009-02-13 02:31:07 +00002570 return FWIT->getWidth() << 3;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002571
Chris Lattner76a00cf2008-04-06 22:59:24 +00002572 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002573 default: assert(0 && "getIntegerRank(): not a built-in integer");
2574 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002575 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002576 case BuiltinType::Char_S:
2577 case BuiltinType::Char_U:
2578 case BuiltinType::SChar:
2579 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002580 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002581 case BuiltinType::Short:
2582 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002583 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002584 case BuiltinType::Int:
2585 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002586 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002587 case BuiltinType::Long:
2588 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002589 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002590 case BuiltinType::LongLong:
2591 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002592 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002593 case BuiltinType::Int128:
2594 case BuiltinType::UInt128:
2595 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002596 }
2597}
2598
Eli Friedman629ffb92009-08-20 04:21:42 +00002599/// \brief Whether this is a promotable bitfield reference according
2600/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2601///
2602/// \returns the type this bit-field will promote to, or NULL if no
2603/// promotion occurs.
2604QualType ASTContext::isPromotableBitField(Expr *E) {
2605 FieldDecl *Field = E->getBitField();
2606 if (!Field)
2607 return QualType();
2608
2609 QualType FT = Field->getType();
2610
2611 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2612 uint64_t BitWidth = BitWidthAP.getZExtValue();
2613 uint64_t IntSize = getTypeSize(IntTy);
2614 // GCC extension compatibility: if the bit-field size is less than or equal
2615 // to the size of int, it gets promoted no matter what its type is.
2616 // For instance, unsigned long bf : 4 gets promoted to signed int.
2617 if (BitWidth < IntSize)
2618 return IntTy;
2619
2620 if (BitWidth == IntSize)
2621 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2622
2623 // Types bigger than int are not subject to promotions, and therefore act
2624 // like the base type.
2625 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2626 // is ridiculous.
2627 return QualType();
2628}
2629
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002630/// getPromotedIntegerType - Returns the type that Promotable will
2631/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2632/// integer type.
2633QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2634 assert(!Promotable.isNull());
2635 assert(Promotable->isPromotableIntegerType());
2636 if (Promotable->isSignedIntegerType())
2637 return IntTy;
2638 uint64_t PromotableSize = getTypeSize(Promotable);
2639 uint64_t IntSize = getTypeSize(IntTy);
2640 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2641 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2642}
2643
Mike Stump11289f42009-09-09 15:08:12 +00002644/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002645/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002646/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002647int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002648 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2649 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002650 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002651
Chris Lattner76a00cf2008-04-06 22:59:24 +00002652 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2653 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002654
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002655 unsigned LHSRank = getIntegerRank(LHSC);
2656 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002657
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002658 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2659 if (LHSRank == RHSRank) return 0;
2660 return LHSRank > RHSRank ? 1 : -1;
2661 }
Mike Stump11289f42009-09-09 15:08:12 +00002662
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002663 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2664 if (LHSUnsigned) {
2665 // If the unsigned [LHS] type is larger, return it.
2666 if (LHSRank >= RHSRank)
2667 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002668
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002669 // If the signed type can represent all values of the unsigned type, it
2670 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002671 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002672 return -1;
2673 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002674
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002675 // If the unsigned [RHS] type is larger, return it.
2676 if (RHSRank >= LHSRank)
2677 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002678
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002679 // If the signed type can represent all values of the unsigned type, it
2680 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002681 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002682 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002683}
Anders Carlsson98f07902007-08-17 05:31:46 +00002684
Mike Stump11289f42009-09-09 15:08:12 +00002685// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002686QualType ASTContext::getCFConstantStringType() {
2687 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002688 CFConstantStringTypeDecl =
2689 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek47923c72008-09-05 01:34:33 +00002690 &Idents.get("NSConstantString"));
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002691 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002692
Anders Carlsson98f07902007-08-17 05:31:46 +00002693 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002694 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002695 // int flags;
2696 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002697 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002698 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002699 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002700 FieldTypes[3] = LongTy;
2701
Anders Carlsson98f07902007-08-17 05:31:46 +00002702 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002703 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002704 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002705 SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002706 FieldTypes[i], /*DInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002707 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002708 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002709 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002710 }
2711
2712 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson98f07902007-08-17 05:31:46 +00002713 }
Mike Stump11289f42009-09-09 15:08:12 +00002714
Anders Carlsson98f07902007-08-17 05:31:46 +00002715 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002716}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002717
Douglas Gregor512b0772009-04-23 22:29:11 +00002718void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002719 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002720 assert(Rec && "Invalid CFConstantStringType");
2721 CFConstantStringTypeDecl = Rec->getDecl();
2722}
2723
Mike Stump11289f42009-09-09 15:08:12 +00002724QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002725 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002726 ObjCFastEnumerationStateTypeDecl =
2727 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2728 &Idents.get("__objcFastEnumerationState"));
Mike Stump11289f42009-09-09 15:08:12 +00002729
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002730 QualType FieldTypes[] = {
2731 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002732 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002733 getPointerType(UnsignedLongTy),
2734 getConstantArrayType(UnsignedLongTy,
2735 llvm::APInt(32, 5), ArrayType::Normal, 0)
2736 };
Mike Stump11289f42009-09-09 15:08:12 +00002737
Douglas Gregor91f84212008-12-11 16:49:14 +00002738 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002739 FieldDecl *Field = FieldDecl::Create(*this,
2740 ObjCFastEnumerationStateTypeDecl,
2741 SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002742 FieldTypes[i], /*DInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002743 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002744 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002745 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002746 }
Mike Stump11289f42009-09-09 15:08:12 +00002747
Douglas Gregor91f84212008-12-11 16:49:14 +00002748 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002749 }
Mike Stump11289f42009-09-09 15:08:12 +00002750
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002751 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2752}
2753
Mike Stumpd0153282009-10-20 02:12:22 +00002754QualType ASTContext::getBlockDescriptorType() {
2755 if (BlockDescriptorType)
2756 return getTagDeclType(BlockDescriptorType);
2757
2758 RecordDecl *T;
2759 // FIXME: Needs the FlagAppleBlock bit.
2760 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2761 &Idents.get("__block_descriptor"));
2762
2763 QualType FieldTypes[] = {
2764 UnsignedLongTy,
2765 UnsignedLongTy,
2766 };
2767
2768 const char *FieldNames[] = {
2769 "reserved",
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002770 "Size"
Mike Stumpd0153282009-10-20 02:12:22 +00002771 };
2772
2773 for (size_t i = 0; i < 2; ++i) {
2774 FieldDecl *Field = FieldDecl::Create(*this,
2775 T,
2776 SourceLocation(),
2777 &Idents.get(FieldNames[i]),
2778 FieldTypes[i], /*DInfo=*/0,
2779 /*BitWidth=*/0,
2780 /*Mutable=*/false);
2781 T->addDecl(Field);
2782 }
2783
2784 T->completeDefinition(*this);
2785
2786 BlockDescriptorType = T;
2787
2788 return getTagDeclType(BlockDescriptorType);
2789}
2790
2791void ASTContext::setBlockDescriptorType(QualType T) {
2792 const RecordType *Rec = T->getAs<RecordType>();
2793 assert(Rec && "Invalid BlockDescriptorType");
2794 BlockDescriptorType = Rec->getDecl();
2795}
2796
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002797QualType ASTContext::getBlockDescriptorExtendedType() {
2798 if (BlockDescriptorExtendedType)
2799 return getTagDeclType(BlockDescriptorExtendedType);
2800
2801 RecordDecl *T;
2802 // FIXME: Needs the FlagAppleBlock bit.
2803 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2804 &Idents.get("__block_descriptor_withcopydispose"));
2805
2806 QualType FieldTypes[] = {
2807 UnsignedLongTy,
2808 UnsignedLongTy,
2809 getPointerType(VoidPtrTy),
2810 getPointerType(VoidPtrTy)
2811 };
2812
2813 const char *FieldNames[] = {
2814 "reserved",
2815 "Size",
2816 "CopyFuncPtr",
2817 "DestroyFuncPtr"
2818 };
2819
2820 for (size_t i = 0; i < 4; ++i) {
2821 FieldDecl *Field = FieldDecl::Create(*this,
2822 T,
2823 SourceLocation(),
2824 &Idents.get(FieldNames[i]),
2825 FieldTypes[i], /*DInfo=*/0,
2826 /*BitWidth=*/0,
2827 /*Mutable=*/false);
2828 T->addDecl(Field);
2829 }
2830
2831 T->completeDefinition(*this);
2832
2833 BlockDescriptorExtendedType = T;
2834
2835 return getTagDeclType(BlockDescriptorExtendedType);
2836}
2837
2838void ASTContext::setBlockDescriptorExtendedType(QualType T) {
2839 const RecordType *Rec = T->getAs<RecordType>();
2840 assert(Rec && "Invalid BlockDescriptorType");
2841 BlockDescriptorExtendedType = Rec->getDecl();
2842}
2843
Mike Stump94967902009-10-21 18:16:27 +00002844bool ASTContext::BlockRequiresCopying(QualType Ty) {
2845 if (Ty->isBlockPointerType())
2846 return true;
2847 if (isObjCNSObjectType(Ty))
2848 return true;
2849 if (Ty->isObjCObjectPointerType())
2850 return true;
2851 return false;
2852}
2853
2854QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
2855 // type = struct __Block_byref_1_X {
Mike Stump7fe9cc12009-10-21 03:49:08 +00002856 // void *__isa;
Mike Stump94967902009-10-21 18:16:27 +00002857 // struct __Block_byref_1_X *__forwarding;
Mike Stump7fe9cc12009-10-21 03:49:08 +00002858 // unsigned int __flags;
2859 // unsigned int __size;
Mike Stump066b6162009-10-21 22:01:24 +00002860 // void *__copy_helper; // as needed
2861 // void *__destroy_help // as needed
Mike Stump94967902009-10-21 18:16:27 +00002862 // int X;
Mike Stump7fe9cc12009-10-21 03:49:08 +00002863 // } *
2864
Mike Stump94967902009-10-21 18:16:27 +00002865 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
2866
2867 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00002868 static unsigned int UniqueBlockByRefTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00002869 llvm::SmallString<36> Name;
2870 llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
2871 ++UniqueBlockByRefTypeID << '_' << DeclName;
Mike Stump94967902009-10-21 18:16:27 +00002872 RecordDecl *T;
2873 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Benjamin Kramer1402ce32009-10-24 09:57:09 +00002874 &Idents.get(Name.str()));
Mike Stump94967902009-10-21 18:16:27 +00002875 T->startDefinition();
2876 QualType Int32Ty = IntTy;
2877 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
2878 QualType FieldTypes[] = {
2879 getPointerType(VoidPtrTy),
2880 getPointerType(getTagDeclType(T)),
2881 Int32Ty,
2882 Int32Ty,
2883 getPointerType(VoidPtrTy),
2884 getPointerType(VoidPtrTy),
2885 Ty
2886 };
2887
2888 const char *FieldNames[] = {
2889 "__isa",
2890 "__forwarding",
2891 "__flags",
2892 "__size",
2893 "__copy_helper",
2894 "__destroy_helper",
2895 DeclName,
2896 };
2897
2898 for (size_t i = 0; i < 7; ++i) {
2899 if (!HasCopyAndDispose && i >=4 && i <= 5)
2900 continue;
2901 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
2902 &Idents.get(FieldNames[i]),
2903 FieldTypes[i], /*DInfo=*/0,
2904 /*BitWidth=*/0, /*Mutable=*/false);
2905 T->addDecl(Field);
2906 }
2907
2908 T->completeDefinition(*this);
2909
2910 return getPointerType(getTagDeclType(T));
Mike Stump7fe9cc12009-10-21 03:49:08 +00002911}
2912
2913
2914QualType ASTContext::getBlockParmType(
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002915 bool BlockHasCopyDispose,
Mike Stump7fe9cc12009-10-21 03:49:08 +00002916 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpd0153282009-10-20 02:12:22 +00002917 // FIXME: Move up
Fariborz Jahanianaa9894c52009-10-24 00:16:42 +00002918 static unsigned int UniqueBlockParmTypeID = 0;
Benjamin Kramer1402ce32009-10-24 09:57:09 +00002919 llvm::SmallString<36> Name;
2920 llvm::raw_svector_ostream(Name) << "__block_literal_"
2921 << ++UniqueBlockParmTypeID;
Mike Stumpd0153282009-10-20 02:12:22 +00002922 RecordDecl *T;
2923 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Benjamin Kramer1402ce32009-10-24 09:57:09 +00002924 &Idents.get(Name.str()));
Mike Stumpd0153282009-10-20 02:12:22 +00002925 QualType FieldTypes[] = {
2926 getPointerType(VoidPtrTy),
2927 IntTy,
2928 IntTy,
2929 getPointerType(VoidPtrTy),
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002930 (BlockHasCopyDispose ?
2931 getPointerType(getBlockDescriptorExtendedType()) :
2932 getPointerType(getBlockDescriptorType()))
Mike Stumpd0153282009-10-20 02:12:22 +00002933 };
2934
2935 const char *FieldNames[] = {
2936 "__isa",
2937 "__flags",
2938 "__reserved",
2939 "__FuncPtr",
2940 "__descriptor"
2941 };
2942
2943 for (size_t i = 0; i < 5; ++i) {
Mike Stump7fe9cc12009-10-21 03:49:08 +00002944 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpd0153282009-10-20 02:12:22 +00002945 &Idents.get(FieldNames[i]),
2946 FieldTypes[i], /*DInfo=*/0,
Mike Stump7fe9cc12009-10-21 03:49:08 +00002947 /*BitWidth=*/0, /*Mutable=*/false);
2948 T->addDecl(Field);
2949 }
2950
2951 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
2952 const Expr *E = BlockDeclRefDecls[i];
2953 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
2954 clang::IdentifierInfo *Name = 0;
2955 if (BDRE) {
2956 const ValueDecl *D = BDRE->getDecl();
2957 Name = &Idents.get(D->getName());
2958 }
2959 QualType FieldType = E->getType();
2960
2961 if (BDRE && BDRE->isByRef())
Mike Stump94967902009-10-21 18:16:27 +00002962 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
2963 FieldType);
Mike Stump7fe9cc12009-10-21 03:49:08 +00002964
2965 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
2966 Name, FieldType, /*DInfo=*/0,
2967 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpd0153282009-10-20 02:12:22 +00002968 T->addDecl(Field);
2969 }
2970
2971 T->completeDefinition(*this);
Mike Stump7fe9cc12009-10-21 03:49:08 +00002972
2973 return getPointerType(getTagDeclType(T));
Mike Stumpd0153282009-10-20 02:12:22 +00002974}
2975
Douglas Gregor512b0772009-04-23 22:29:11 +00002976void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002977 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002978 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2979 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2980}
2981
Anders Carlsson18acd442007-10-29 06:33:42 +00002982// This returns true if a type has been typedefed to BOOL:
2983// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00002984static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00002985 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00002986 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2987 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00002988
Anders Carlssond8499822007-10-29 05:01:08 +00002989 return false;
2990}
2991
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002992/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002993/// purpose.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002994int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner37e05872008-03-05 18:54:05 +00002995 uint64_t sz = getTypeSize(type);
Mike Stump11289f42009-09-09 15:08:12 +00002996
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002997 // Make all integer and enum types at least as large as an int
2998 if (sz > 0 && type->isIntegralType())
Chris Lattner37e05872008-03-05 18:54:05 +00002999 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003000 // Treat arrays as pointers, since that's how they're passed in.
3001 else if (type->isArrayType())
Chris Lattner37e05872008-03-05 18:54:05 +00003002 sz = getTypeSize(VoidPtrTy);
3003 return sz / getTypeSize(CharTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003004}
3005
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003006/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003007/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003008void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003009 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003010 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003011 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003012 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003013 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003014 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003015 // Compute size of all parameters.
3016 // Start with computing size of a pointer in number of bytes.
3017 // FIXME: There might(should) be a better way of doing this computation!
3018 SourceLocation Loc;
Chris Lattner37e05872008-03-05 18:54:05 +00003019 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003020 // The first two arguments (self and _cmd) are pointers; account for
3021 // their size.
3022 int ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003023 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3024 E = Decl->param_end(); PI != E; ++PI) {
3025 QualType PType = (*PI)->getType();
3026 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003027 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003028 ParmOffset += sz;
3029 }
3030 S += llvm::utostr(ParmOffset);
3031 S += "@0:";
3032 S += llvm::utostr(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00003033
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003034 // Argument types.
3035 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00003036 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3037 E = Decl->param_end(); PI != E; ++PI) {
3038 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00003039 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003040 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00003041 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3042 // Use array's original type only if it has known number of
3043 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00003044 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00003045 PType = PVDecl->getType();
3046 } else if (PType->isFunctionType())
3047 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003048 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003049 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00003050 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003051 getObjCEncodingForType(PType, S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003052 S += llvm::utostr(ParmOffset);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003053 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00003054 }
3055}
3056
Daniel Dunbar4932b362008-08-28 04:38:10 +00003057/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003058/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00003059/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3060/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00003061/// Property attributes are stored as a comma-delimited C string. The simple
3062/// attributes readonly and bycopy are encoded as single characters. The
3063/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3064/// encoded as single characters, followed by an identifier. Property types
3065/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00003066/// these attributes are defined by the following enumeration:
3067/// @code
3068/// enum PropertyAttributes {
3069/// kPropertyReadOnly = 'R', // property is read-only.
3070/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3071/// kPropertyByref = '&', // property is a reference to the value last assigned
3072/// kPropertyDynamic = 'D', // property is dynamic
3073/// kPropertyGetter = 'G', // followed by getter selector name
3074/// kPropertySetter = 'S', // followed by setter selector name
3075/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3076/// kPropertyType = 't' // followed by old-style type encoding.
3077/// kPropertyWeak = 'W' // 'weak' property
3078/// kPropertyStrong = 'P' // property GC'able
3079/// kPropertyNonAtomic = 'N' // property non-atomic
3080/// };
3081/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00003082void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00003083 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00003084 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003085 // Collect information from the property implementation decl(s).
3086 bool Dynamic = false;
3087 ObjCPropertyImplDecl *SynthesizePID = 0;
3088
3089 // FIXME: Duplicated code due to poor abstraction.
3090 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00003091 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00003092 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3093 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003094 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003095 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003096 ObjCPropertyImplDecl *PID = *i;
3097 if (PID->getPropertyDecl() == PD) {
3098 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3099 Dynamic = true;
3100 } else {
3101 SynthesizePID = PID;
3102 }
3103 }
3104 }
3105 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00003106 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003107 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003108 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00003109 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003110 ObjCPropertyImplDecl *PID = *i;
3111 if (PID->getPropertyDecl() == PD) {
3112 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3113 Dynamic = true;
3114 } else {
3115 SynthesizePID = PID;
3116 }
3117 }
Mike Stump11289f42009-09-09 15:08:12 +00003118 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00003119 }
3120 }
3121
3122 // FIXME: This is not very efficient.
3123 S = "T";
3124
3125 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003126 // GCC has some special rules regarding encoding of properties which
3127 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00003128 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003129 true /* outermost type */,
3130 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00003131
3132 if (PD->isReadOnly()) {
3133 S += ",R";
3134 } else {
3135 switch (PD->getSetterKind()) {
3136 case ObjCPropertyDecl::Assign: break;
3137 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00003138 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00003139 }
3140 }
3141
3142 // It really isn't clear at all what this means, since properties
3143 // are "dynamic by default".
3144 if (Dynamic)
3145 S += ",D";
3146
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003147 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3148 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00003149
Daniel Dunbar4932b362008-08-28 04:38:10 +00003150 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3151 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00003152 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003153 }
3154
3155 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3156 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00003157 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003158 }
3159
3160 if (SynthesizePID) {
3161 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3162 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00003163 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00003164 }
3165
3166 // FIXME: OBJCGC: weak & strong
3167}
3168
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003169/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00003170/// Another legacy compatibility encoding: 32-bit longs are encoded as
3171/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003172/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3173///
3174void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00003175 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00003176 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003177 if (BT->getKind() == BuiltinType::ULong &&
3178 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003179 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00003180 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00003181 if (BT->getKind() == BuiltinType::Long &&
3182 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003183 PointeeTy = IntTy;
3184 }
3185 }
3186}
3187
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003188void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003189 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003190 // We follow the behavior of gcc, expanding structures which are
3191 // directly pointed to, and expanding embedded structures. Note that
3192 // these rules are sufficient to prevent recursive encoding of the
3193 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00003194 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00003195 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003196}
3197
Mike Stump11289f42009-09-09 15:08:12 +00003198static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003199 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003200 const Expr *E = FD->getBitWidth();
3201 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3202 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00003203 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003204 S += 'b';
3205 S += llvm::utostr(N);
3206}
3207
Daniel Dunbar07d07852009-10-18 21:17:35 +00003208// FIXME: Use SmallString for accumulating string.
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003209void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3210 bool ExpandPointedToStructures,
3211 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00003212 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00003213 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00003214 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00003215 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003216 if (FD && FD->isBitField())
3217 return EncodeBitField(this, S, FD);
3218 char encoding;
3219 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00003220 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00003221 case BuiltinType::Void: encoding = 'v'; break;
3222 case BuiltinType::Bool: encoding = 'B'; break;
3223 case BuiltinType::Char_U:
3224 case BuiltinType::UChar: encoding = 'C'; break;
3225 case BuiltinType::UShort: encoding = 'S'; break;
3226 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003227 case BuiltinType::ULong:
3228 encoding =
3229 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00003230 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003231 case BuiltinType::UInt128: encoding = 'T'; break;
3232 case BuiltinType::ULongLong: encoding = 'Q'; break;
3233 case BuiltinType::Char_S:
3234 case BuiltinType::SChar: encoding = 'c'; break;
3235 case BuiltinType::Short: encoding = 's'; break;
3236 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00003237 case BuiltinType::Long:
3238 encoding =
3239 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003240 break;
3241 case BuiltinType::LongLong: encoding = 'q'; break;
3242 case BuiltinType::Int128: encoding = 't'; break;
3243 case BuiltinType::Float: encoding = 'f'; break;
3244 case BuiltinType::Double: encoding = 'd'; break;
3245 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003246 }
Mike Stump11289f42009-09-09 15:08:12 +00003247
Chris Lattnere7cabb92009-07-13 00:10:46 +00003248 S += encoding;
3249 return;
3250 }
Mike Stump11289f42009-09-09 15:08:12 +00003251
John McCall9dd450b2009-09-21 23:43:11 +00003252 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00003253 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00003254 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00003255 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003256 return;
3257 }
Mike Stump11289f42009-09-09 15:08:12 +00003258
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003259 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlssond8499822007-10-29 05:01:08 +00003260 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003261 bool isReadOnly = false;
3262 // For historical/compatibility reasons, the read-only qualifier of the
3263 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3264 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00003265 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00003266 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003267 if (OutermostType && T.isConstQualified()) {
3268 isReadOnly = true;
3269 S += 'r';
3270 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00003271 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003272 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003273 while (P->getAs<PointerType>())
3274 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003275 if (P.isConstQualified()) {
3276 isReadOnly = true;
3277 S += 'r';
3278 }
3279 }
3280 if (isReadOnly) {
3281 // Another legacy compatibility encoding. Some ObjC qualifier and type
3282 // combinations need to be rearranged.
3283 // Rewrite "in const" from "nr" to "rn"
3284 const char * s = S.c_str();
3285 int len = S.length();
3286 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3287 std::string replace = "rn";
3288 S.replace(S.end()-2, S.end(), replace);
3289 }
3290 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00003291 if (isObjCSelType(PointeeTy)) {
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003292 S += ':';
3293 return;
Fariborz Jahanian509d8d62007-10-30 17:06:23 +00003294 }
Mike Stump11289f42009-09-09 15:08:12 +00003295
Anders Carlssond8499822007-10-29 05:01:08 +00003296 if (PointeeTy->isCharType()) {
3297 // char pointer types should be encoded as '*' unless it is a
3298 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003299 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003300 S += '*';
3301 return;
3302 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003303 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003304 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3305 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3306 S += '#';
3307 return;
3308 }
3309 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3310 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3311 S += '@';
3312 return;
3313 }
3314 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003315 }
Anders Carlssond8499822007-10-29 05:01:08 +00003316 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003317 getLegacyIntegralTypeEncoding(PointeeTy);
3318
Mike Stump11289f42009-09-09 15:08:12 +00003319 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003320 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003321 return;
3322 }
Mike Stump11289f42009-09-09 15:08:12 +00003323
Chris Lattnere7cabb92009-07-13 00:10:46 +00003324 if (const ArrayType *AT =
3325 // Ignore type qualifiers etc.
3326 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003327 if (isa<IncompleteArrayType>(AT)) {
3328 // Incomplete arrays are encoded as a pointer to the array element.
3329 S += '^';
3330
Mike Stump11289f42009-09-09 15:08:12 +00003331 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003332 false, ExpandStructures, FD);
3333 } else {
3334 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003335
Anders Carlssond05f44b2009-02-22 01:38:57 +00003336 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3337 S += llvm::utostr(CAT->getSize().getZExtValue());
3338 else {
3339 //Variable length arrays are encoded as a regular array with 0 elements.
3340 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3341 S += '0';
3342 }
Mike Stump11289f42009-09-09 15:08:12 +00003343
3344 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003345 false, ExpandStructures, FD);
3346 S += ']';
3347 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003348 return;
3349 }
Mike Stump11289f42009-09-09 15:08:12 +00003350
John McCall9dd450b2009-09-21 23:43:11 +00003351 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003352 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003353 return;
3354 }
Mike Stump11289f42009-09-09 15:08:12 +00003355
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003356 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003357 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003358 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003359 // Anonymous structures print as '?'
3360 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3361 S += II->getName();
3362 } else {
3363 S += '?';
3364 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003365 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003366 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003367 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3368 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003369 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003370 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003371 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003372 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003373 S += '"';
3374 }
Mike Stump11289f42009-09-09 15:08:12 +00003375
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003376 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003377 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003378 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003379 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003380 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003381 QualType qt = Field->getType();
3382 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003383 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003384 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003385 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003386 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003387 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003388 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003389 return;
3390 }
Mike Stump11289f42009-09-09 15:08:12 +00003391
Chris Lattnere7cabb92009-07-13 00:10:46 +00003392 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003393 if (FD && FD->isBitField())
3394 EncodeBitField(this, S, FD);
3395 else
3396 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003397 return;
3398 }
Mike Stump11289f42009-09-09 15:08:12 +00003399
Chris Lattnere7cabb92009-07-13 00:10:46 +00003400 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003401 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003402 return;
3403 }
Mike Stump11289f42009-09-09 15:08:12 +00003404
John McCall8ccfcb52009-09-24 19:53:00 +00003405 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003406 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003407 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003408 S += '{';
3409 const IdentifierInfo *II = OI->getIdentifier();
3410 S += II->getName();
3411 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003412 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003413 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003414 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003415 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003416 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003417 RecFields[i]);
3418 else
Mike Stump11289f42009-09-09 15:08:12 +00003419 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003420 FD);
3421 }
3422 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003423 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003424 }
Mike Stump11289f42009-09-09 15:08:12 +00003425
John McCall9dd450b2009-09-21 23:43:11 +00003426 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003427 if (OPT->isObjCIdType()) {
3428 S += '@';
3429 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003430 }
Mike Stump11289f42009-09-09 15:08:12 +00003431
Steve Narofff0c86112009-10-28 22:03:49 +00003432 if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
3433 // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
3434 // Since this is a binary compatibility issue, need to consult with runtime
3435 // folks. Fortunately, this is a *very* obsure construct.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003436 S += '#';
3437 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003438 }
Mike Stump11289f42009-09-09 15:08:12 +00003439
Chris Lattnere7cabb92009-07-13 00:10:46 +00003440 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003441 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003442 ExpandPointedToStructures,
3443 ExpandStructures, FD);
3444 if (FD || EncodingProperty) {
3445 // Note that we do extended encoding of protocol qualifer list
3446 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003447 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003448 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3449 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003450 S += '<';
3451 S += (*I)->getNameAsString();
3452 S += '>';
3453 }
3454 S += '"';
3455 }
3456 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003457 }
Mike Stump11289f42009-09-09 15:08:12 +00003458
Chris Lattnere7cabb92009-07-13 00:10:46 +00003459 QualType PointeeTy = OPT->getPointeeType();
3460 if (!EncodingProperty &&
3461 isa<TypedefType>(PointeeTy.getTypePtr())) {
3462 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003463 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003464 // {...};
3465 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003466 getObjCEncodingForTypeImpl(PointeeTy, S,
3467 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003468 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003469 return;
3470 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003471
3472 S += '@';
Steve Narofff0c86112009-10-28 22:03:49 +00003473 if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003474 S += '"';
Steve Narofff0c86112009-10-28 22:03:49 +00003475 S += OPT->getInterfaceDecl()->getIdentifier()->getName();
Steve Naroffaccc4882009-07-20 17:56:53 +00003476 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3477 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003478 S += '<';
3479 S += (*I)->getNameAsString();
3480 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003481 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003482 S += '"';
3483 }
3484 return;
3485 }
Mike Stump11289f42009-09-09 15:08:12 +00003486
Chris Lattnere7cabb92009-07-13 00:10:46 +00003487 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003488}
3489
Mike Stump11289f42009-09-09 15:08:12 +00003490void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003491 std::string& S) const {
3492 if (QT & Decl::OBJC_TQ_In)
3493 S += 'n';
3494 if (QT & Decl::OBJC_TQ_Inout)
3495 S += 'N';
3496 if (QT & Decl::OBJC_TQ_Out)
3497 S += 'o';
3498 if (QT & Decl::OBJC_TQ_Bycopy)
3499 S += 'O';
3500 if (QT & Decl::OBJC_TQ_Byref)
3501 S += 'R';
3502 if (QT & Decl::OBJC_TQ_Oneway)
3503 S += 'V';
3504}
3505
Chris Lattnere7cabb92009-07-13 00:10:46 +00003506void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003507 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003508
Anders Carlsson87c149b2007-10-11 01:00:40 +00003509 BuiltinVaListType = T;
3510}
3511
Chris Lattnere7cabb92009-07-13 00:10:46 +00003512void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003513 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003514}
3515
Chris Lattnere7cabb92009-07-13 00:10:46 +00003516void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor512b0772009-04-23 22:29:11 +00003517 ObjCSelType = T;
3518
John McCall9dd450b2009-09-21 23:43:11 +00003519 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003520 if (!TT)
3521 return;
3522 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003523
3524 // typedef struct objc_selector *SEL;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003525 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanian1778f4b2009-01-16 19:58:32 +00003526 if (!ptr)
3527 return;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003528 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanian1778f4b2009-01-16 19:58:32 +00003529 if (!rec)
3530 return;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003531 SelStructType = rec;
3532}
3533
Chris Lattnere7cabb92009-07-13 00:10:46 +00003534void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003535 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003536}
3537
Chris Lattnere7cabb92009-07-13 00:10:46 +00003538void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003539 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003540}
3541
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003542void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003543 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003544 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003545
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003546 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003547}
3548
Douglas Gregordc572a32009-03-30 22:58:21 +00003549/// \brief Retrieve the template name that represents a qualified
3550/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003551TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003552 bool TemplateKeyword,
3553 TemplateDecl *Template) {
3554 llvm::FoldingSetNodeID ID;
3555 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3556
3557 void *InsertPos = 0;
3558 QualifiedTemplateName *QTN =
3559 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3560 if (!QTN) {
3561 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3562 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3563 }
3564
3565 return TemplateName(QTN);
3566}
3567
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00003568/// \brief Retrieve the template name that represents a qualified
3569/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003570TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00003571 bool TemplateKeyword,
3572 OverloadedFunctionDecl *Template) {
3573 llvm::FoldingSetNodeID ID;
3574 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump11289f42009-09-09 15:08:12 +00003575
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00003576 void *InsertPos = 0;
3577 QualifiedTemplateName *QTN =
3578 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3579 if (!QTN) {
3580 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3581 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3582 }
Mike Stump11289f42009-09-09 15:08:12 +00003583
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00003584 return TemplateName(QTN);
3585}
3586
Douglas Gregordc572a32009-03-30 22:58:21 +00003587/// \brief Retrieve the template name that represents a dependent
3588/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003589TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003590 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003591 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003592 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003593
3594 llvm::FoldingSetNodeID ID;
3595 DependentTemplateName::Profile(ID, NNS, Name);
3596
3597 void *InsertPos = 0;
3598 DependentTemplateName *QTN =
3599 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3600
3601 if (QTN)
3602 return TemplateName(QTN);
3603
3604 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3605 if (CanonNNS == NNS) {
3606 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3607 } else {
3608 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3609 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3610 }
3611
3612 DependentTemplateNames.InsertNode(QTN, InsertPos);
3613 return TemplateName(QTN);
3614}
3615
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003616/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003617/// TargetInfo, produce the corresponding type. The unsigned @p Type
3618/// is actually a value of type @c TargetInfo::IntType.
John McCall48f2d582009-10-23 23:03:21 +00003619CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003620 switch (Type) {
John McCall48f2d582009-10-23 23:03:21 +00003621 case TargetInfo::NoInt: return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003622 case TargetInfo::SignedShort: return ShortTy;
3623 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3624 case TargetInfo::SignedInt: return IntTy;
3625 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3626 case TargetInfo::SignedLong: return LongTy;
3627 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3628 case TargetInfo::SignedLongLong: return LongLongTy;
3629 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3630 }
3631
3632 assert(false && "Unhandled TargetInfo::IntType value");
John McCall48f2d582009-10-23 23:03:21 +00003633 return CanQualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003634}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003635
3636//===----------------------------------------------------------------------===//
3637// Type Predicates.
3638//===----------------------------------------------------------------------===//
3639
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003640/// isObjCNSObjectType - Return true if this is an NSObject object using
3641/// NSObject attribute on a c-style pointer type.
3642/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003643/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003644///
3645bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3646 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3647 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003648 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003649 return true;
3650 }
Mike Stump11289f42009-09-09 15:08:12 +00003651 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003652}
3653
Fariborz Jahanian96207692009-02-18 21:49:28 +00003654/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3655/// garbage collection attribute.
3656///
John McCall8ccfcb52009-09-24 19:53:00 +00003657Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3658 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003659 if (getLangOptions().ObjC1 &&
3660 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003661 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003662 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003663 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003664 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003665 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003666 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003667 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003668 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003669 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003670 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003671 // Non-pointers have none gc'able attribute regardless of the attribute
3672 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003673 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003674 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003675 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003676 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003677}
3678
Chris Lattner49af6a42008-04-07 06:51:04 +00003679//===----------------------------------------------------------------------===//
3680// Type Compatibility Testing
3681//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00003682
Mike Stump11289f42009-09-09 15:08:12 +00003683/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003684/// compatible.
3685static bool areCompatVectorTypes(const VectorType *LHS,
3686 const VectorType *RHS) {
John McCallb692a092009-10-22 20:10:53 +00003687 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner49af6a42008-04-07 06:51:04 +00003688 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00003689 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00003690}
3691
Steve Naroff8e6aee52009-07-23 01:01:38 +00003692//===----------------------------------------------------------------------===//
3693// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3694//===----------------------------------------------------------------------===//
3695
3696/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3697/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003698bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3699 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003700 if (lProto == rProto)
3701 return true;
3702 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3703 E = rProto->protocol_end(); PI != E; ++PI)
3704 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3705 return true;
3706 return false;
3707}
3708
Steve Naroff8e6aee52009-07-23 01:01:38 +00003709/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3710/// return true if lhs's protocols conform to rhs's protocol; false
3711/// otherwise.
3712bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3713 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3714 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3715 return false;
3716}
3717
3718/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3719/// ObjCQualifiedIDType.
3720bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3721 bool compare) {
3722 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00003723 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003724 lhs->isObjCIdType() || lhs->isObjCClassType())
3725 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003726 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003727 rhs->isObjCIdType() || rhs->isObjCClassType())
3728 return true;
3729
3730 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00003731 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00003732
Steve Naroff8e6aee52009-07-23 01:01:38 +00003733 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00003734
Steve Naroff8e6aee52009-07-23 01:01:38 +00003735 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003736 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00003737 // make sure we check the class hierarchy.
3738 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3739 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3740 E = lhsQID->qual_end(); I != E; ++I) {
3741 // when comparing an id<P> on lhs with a static type on rhs,
3742 // see if static class implements all of id's protocols, directly or
3743 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003744 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00003745 return false;
3746 }
3747 }
3748 // If there are no qualifiers and no interface, we have an 'id'.
3749 return true;
3750 }
Mike Stump11289f42009-09-09 15:08:12 +00003751 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00003752 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3753 E = lhsQID->qual_end(); I != E; ++I) {
3754 ObjCProtocolDecl *lhsProto = *I;
3755 bool match = false;
3756
3757 // when comparing an id<P> on lhs with a static type on rhs,
3758 // see if static class implements all of id's protocols, directly or
3759 // through its super class and categories.
3760 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3761 E = rhsOPT->qual_end(); J != E; ++J) {
3762 ObjCProtocolDecl *rhsProto = *J;
3763 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3764 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3765 match = true;
3766 break;
3767 }
3768 }
Mike Stump11289f42009-09-09 15:08:12 +00003769 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00003770 // make sure we check the class hierarchy.
3771 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3772 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3773 E = lhsQID->qual_end(); I != E; ++I) {
3774 // when comparing an id<P> on lhs with a static type on rhs,
3775 // see if static class implements all of id's protocols, directly or
3776 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003777 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003778 match = true;
3779 break;
3780 }
3781 }
3782 }
3783 if (!match)
3784 return false;
3785 }
Mike Stump11289f42009-09-09 15:08:12 +00003786
Steve Naroff8e6aee52009-07-23 01:01:38 +00003787 return true;
3788 }
Mike Stump11289f42009-09-09 15:08:12 +00003789
Steve Naroff8e6aee52009-07-23 01:01:38 +00003790 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3791 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3792
Mike Stump11289f42009-09-09 15:08:12 +00003793 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00003794 lhs->getAsObjCInterfacePointerType()) {
3795 if (lhsOPT->qual_empty()) {
3796 bool match = false;
3797 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3798 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3799 E = rhsQID->qual_end(); I != E; ++I) {
3800 // when comparing an id<P> on lhs with a static type on rhs,
3801 // see if static class implements all of id's protocols, directly or
3802 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003803 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003804 match = true;
3805 break;
3806 }
3807 }
3808 if (!match)
3809 return false;
3810 }
3811 return true;
3812 }
Mike Stump11289f42009-09-09 15:08:12 +00003813 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00003814 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3815 E = lhsOPT->qual_end(); I != E; ++I) {
3816 ObjCProtocolDecl *lhsProto = *I;
3817 bool match = false;
3818
3819 // when comparing an id<P> on lhs with a static type on rhs,
3820 // see if static class implements all of id's protocols, directly or
3821 // through its super class and categories.
3822 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3823 E = rhsQID->qual_end(); J != E; ++J) {
3824 ObjCProtocolDecl *rhsProto = *J;
3825 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3826 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3827 match = true;
3828 break;
3829 }
3830 }
3831 if (!match)
3832 return false;
3833 }
3834 return true;
3835 }
3836 return false;
3837}
3838
Eli Friedman47f77112008-08-22 00:56:42 +00003839/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003840/// compatible for assignment from RHS to LHS. This handles validation of any
3841/// protocol qualifiers on the LHS or RHS.
3842///
Steve Naroff7cae42b2009-07-10 23:34:53 +00003843bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3844 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003845 // If either type represents the built-in 'id' or 'Class' types, return true.
3846 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00003847 return true;
3848
Steve Naroff8e6aee52009-07-23 01:01:38 +00003849 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00003850 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3851 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00003852 false);
3853
Steve Naroff7cae42b2009-07-10 23:34:53 +00003854 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3855 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00003856 if (LHS && RHS) // We have 2 user-defined types.
3857 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00003858
Steve Naroff8e6aee52009-07-23 01:01:38 +00003859 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00003860}
3861
Fariborz Jahanianef8b8ce2009-10-27 23:02:38 +00003862/// areCommonBaseCompatible - Returns common base class of the two classes if
3863/// one found. Note that this is O'2 algorithm. But it will be called as the
3864/// last type comparison in a ?-exp of ObjC pointer types before a
3865/// warning is issued. So, its invokation is extremely rare.
3866QualType ASTContext::areCommonBaseCompatible(
3867 const ObjCObjectPointerType *LHSOPT,
3868 const ObjCObjectPointerType *RHSOPT) {
3869 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3870 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
3871 if (!LHS || !RHS)
3872 return QualType();
3873
3874 while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) {
3875 QualType LHSTy = getObjCInterfaceType(LHSIDecl);
3876 LHS = LHSTy->getAs<ObjCInterfaceType>();
3877 if (canAssignObjCInterfaces(LHS, RHS))
3878 return getObjCObjectPointerType(LHSTy);
3879 }
3880
3881 return QualType();
3882}
3883
Eli Friedman47f77112008-08-22 00:56:42 +00003884bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3885 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00003886 // Verify that the base decls are compatible: the RHS must be a subclass of
3887 // the LHS.
3888 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3889 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003890
Chris Lattner49af6a42008-04-07 06:51:04 +00003891 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3892 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00003893 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00003894 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003895
Chris Lattner49af6a42008-04-07 06:51:04 +00003896 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3897 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00003898 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00003899 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00003900
Steve Naroffc277ad12009-07-18 15:33:26 +00003901 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3902 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00003903 LHSPI != LHSPE; LHSPI++) {
3904 bool RHSImplementsProtocol = false;
3905
3906 // If the RHS doesn't implement the protocol on the left, the types
3907 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00003908 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00003909 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00003910 RHSPI != RHSPE; RHSPI++) {
3911 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00003912 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00003913 break;
3914 }
Steve Naroff114aecb2009-03-01 16:12:44 +00003915 }
3916 // FIXME: For better diagnostics, consider passing back the protocol name.
3917 if (!RHSImplementsProtocol)
3918 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00003919 }
Steve Naroff114aecb2009-03-01 16:12:44 +00003920 // The RHS implements all protocols listed on the LHS.
3921 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00003922}
3923
Steve Naroffb7605152009-02-12 17:52:19 +00003924bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3925 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00003926 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3927 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00003928
Steve Naroff7cae42b2009-07-10 23:34:53 +00003929 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00003930 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00003931
3932 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3933 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00003934}
3935
Mike Stump11289f42009-09-09 15:08:12 +00003936/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00003937/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00003938/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00003939/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00003940bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3941 return !mergeTypes(LHS, RHS).isNull();
3942}
3943
3944QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall9dd450b2009-09-21 23:43:11 +00003945 const FunctionType *lbase = lhs->getAs<FunctionType>();
3946 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003947 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3948 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00003949 bool allLTypes = true;
3950 bool allRTypes = true;
3951
3952 // Check return type
3953 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3954 if (retType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00003955 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3956 allLTypes = false;
3957 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3958 allRTypes = false;
Mike Stumpea086c72009-07-25 23:24:03 +00003959 // FIXME: double check this
Mike Stump8c5d7992009-07-25 21:26:53 +00003960 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3961 if (NoReturn != lbase->getNoReturnAttr())
3962 allLTypes = false;
3963 if (NoReturn != rbase->getNoReturnAttr())
3964 allRTypes = false;
Mike Stump11289f42009-09-09 15:08:12 +00003965
Eli Friedman47f77112008-08-22 00:56:42 +00003966 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00003967 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3968 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00003969 unsigned lproto_nargs = lproto->getNumArgs();
3970 unsigned rproto_nargs = rproto->getNumArgs();
3971
3972 // Compatible functions must have the same number of arguments
3973 if (lproto_nargs != rproto_nargs)
3974 return QualType();
3975
3976 // Variadic and non-variadic functions aren't compatible
3977 if (lproto->isVariadic() != rproto->isVariadic())
3978 return QualType();
3979
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00003980 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3981 return QualType();
3982
Eli Friedman47f77112008-08-22 00:56:42 +00003983 // Check argument compatibility
3984 llvm::SmallVector<QualType, 10> types;
3985 for (unsigned i = 0; i < lproto_nargs; i++) {
3986 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3987 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3988 QualType argtype = mergeTypes(largtype, rargtype);
3989 if (argtype.isNull()) return QualType();
3990 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00003991 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3992 allLTypes = false;
3993 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3994 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00003995 }
3996 if (allLTypes) return lhs;
3997 if (allRTypes) return rhs;
3998 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00003999 lproto->isVariadic(), lproto->getTypeQuals(),
4000 NoReturn);
Eli Friedman47f77112008-08-22 00:56:42 +00004001 }
4002
4003 if (lproto) allRTypes = false;
4004 if (rproto) allLTypes = false;
4005
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004006 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00004007 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00004008 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00004009 if (proto->isVariadic()) return QualType();
4010 // Check that the types are compatible with the types that
4011 // would result from default argument promotions (C99 6.7.5.3p15).
4012 // The only types actually affected are promotable integer
4013 // types and floats, which would be passed as a different
4014 // type depending on whether the prototype is visible.
4015 unsigned proto_nargs = proto->getNumArgs();
4016 for (unsigned i = 0; i < proto_nargs; ++i) {
4017 QualType argTy = proto->getArgType(i);
4018 if (argTy->isPromotableIntegerType() ||
4019 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
4020 return QualType();
4021 }
4022
4023 if (allLTypes) return lhs;
4024 if (allRTypes) return rhs;
4025 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00004026 proto->getNumArgs(), proto->isVariadic(),
4027 proto->getTypeQuals(), NoReturn);
Eli Friedman47f77112008-08-22 00:56:42 +00004028 }
4029
4030 if (allLTypes) return lhs;
4031 if (allRTypes) return rhs;
Mike Stump8c5d7992009-07-25 21:26:53 +00004032 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman47f77112008-08-22 00:56:42 +00004033}
4034
4035QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00004036 // C++ [expr]: If an expression initially has the type "reference to T", the
4037 // type is adjusted to "T" prior to any further analysis, the expression
4038 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004039 // expression is an lvalue unless the reference is an rvalue reference and
4040 // the expression is a function call (possibly inside parentheses).
Eli Friedman47f77112008-08-22 00:56:42 +00004041 // FIXME: C++ shouldn't be going through here! The rules are different
4042 // enough that they should be handled separately.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004043 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
4044 // shouldn't be going through here!
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004045 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerddfdaf92008-04-07 04:07:56 +00004046 LHS = RT->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004047 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerddfdaf92008-04-07 04:07:56 +00004048 RHS = RT->getPointeeType();
Chris Lattneraee96c32008-04-07 05:37:56 +00004049
Eli Friedman47f77112008-08-22 00:56:42 +00004050 QualType LHSCan = getCanonicalType(LHS),
4051 RHSCan = getCanonicalType(RHS);
4052
4053 // If two types are identical, they are compatible.
4054 if (LHSCan == RHSCan)
4055 return LHS;
4056
John McCall8ccfcb52009-09-24 19:53:00 +00004057 // If the qualifiers are different, the types aren't compatible... mostly.
4058 Qualifiers LQuals = LHSCan.getQualifiers();
4059 Qualifiers RQuals = RHSCan.getQualifiers();
4060 if (LQuals != RQuals) {
4061 // If any of these qualifiers are different, we have a type
4062 // mismatch.
4063 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4064 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4065 return QualType();
4066
4067 // Exactly one GC qualifier difference is allowed: __strong is
4068 // okay if the other type has no GC qualifier but is an Objective
4069 // C object pointer (i.e. implicitly strong by default). We fix
4070 // this by pretending that the unqualified type was actually
4071 // qualified __strong.
4072 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4073 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4074 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4075
4076 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4077 return QualType();
4078
4079 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4080 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4081 }
4082 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4083 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4084 }
Eli Friedman47f77112008-08-22 00:56:42 +00004085 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00004086 }
4087
4088 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00004089
Eli Friedmandcca6332009-06-01 01:22:52 +00004090 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4091 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00004092
Chris Lattnerfd652912008-01-14 05:45:46 +00004093 // We want to consider the two function types to be the same for these
4094 // comparisons, just force one to the other.
4095 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4096 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00004097
4098 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00004099 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4100 LHSClass = Type::ConstantArray;
4101 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4102 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00004103
Nate Begemance4d7fc2008-04-18 23:10:10 +00004104 // Canonicalize ExtVector -> Vector.
4105 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4106 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00004107
Chris Lattner95554662008-04-07 05:43:21 +00004108 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004109 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00004110 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00004111 // a signed integer type, or an unsigned integer type.
John McCall9dd450b2009-09-21 23:43:11 +00004112 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004113 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4114 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004115 }
John McCall9dd450b2009-09-21 23:43:11 +00004116 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00004117 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4118 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00004119 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004120
Eli Friedman47f77112008-08-22 00:56:42 +00004121 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004122 }
Eli Friedman47f77112008-08-22 00:56:42 +00004123
Steve Naroffc6edcbd2008-01-09 22:43:08 +00004124 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00004125 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004126#define TYPE(Class, Base)
4127#define ABSTRACT_TYPE(Class, Base)
4128#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4129#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4130#include "clang/AST/TypeNodes.def"
4131 assert(false && "Non-canonical and dependent types shouldn't get here");
4132 return QualType();
4133
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00004134 case Type::LValueReference:
4135 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004136 case Type::MemberPointer:
4137 assert(false && "C++ should never be in mergeTypes");
4138 return QualType();
4139
4140 case Type::IncompleteArray:
4141 case Type::VariableArray:
4142 case Type::FunctionProto:
4143 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004144 assert(false && "Types are eliminated above");
4145 return QualType();
4146
Chris Lattnerfd652912008-01-14 05:45:46 +00004147 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00004148 {
4149 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004150 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4151 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00004152 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4153 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00004154 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004155 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00004156 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00004157 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004158 return getPointerType(ResultType);
4159 }
Steve Naroff68e167d2008-12-10 17:49:55 +00004160 case Type::BlockPointer:
4161 {
4162 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00004163 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4164 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroff68e167d2008-12-10 17:49:55 +00004165 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4166 if (ResultType.isNull()) return QualType();
4167 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4168 return LHS;
4169 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4170 return RHS;
4171 return getBlockPointerType(ResultType);
4172 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004173 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00004174 {
4175 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4176 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4177 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4178 return QualType();
4179
4180 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4181 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4182 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4183 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00004184 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4185 return LHS;
4186 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4187 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00004188 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4189 ArrayType::ArraySizeModifier(), 0);
4190 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4191 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004192 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4193 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00004194 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4195 return LHS;
4196 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4197 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00004198 if (LVAT) {
4199 // FIXME: This isn't correct! But tricky to implement because
4200 // the array's size has to be the size of LHS, but the type
4201 // has to be different.
4202 return LHS;
4203 }
4204 if (RVAT) {
4205 // FIXME: This isn't correct! But tricky to implement because
4206 // the array's size has to be the size of RHS, but the type
4207 // has to be different.
4208 return RHS;
4209 }
Eli Friedman3e62c212008-08-22 01:48:21 +00004210 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4211 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00004212 return getIncompleteArrayType(ResultType,
4213 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00004214 }
Chris Lattnerfd652912008-01-14 05:45:46 +00004215 case Type::FunctionNoProto:
Eli Friedman47f77112008-08-22 00:56:42 +00004216 return mergeFunctionTypes(LHS, RHS);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004217 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004218 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00004219 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00004220 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004221 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00004222 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00004223 case Type::Complex:
4224 // Distinct complex types are incompatible.
4225 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00004226 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00004227 // FIXME: The merged type should be an ExtVector!
John McCall9dd450b2009-09-21 23:43:11 +00004228 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00004229 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00004230 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004231 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00004232 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00004233 // FIXME: This should be type compatibility, e.g. whether
4234 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00004235 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4236 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00004237 if (LHSIface && RHSIface &&
4238 canAssignObjCInterfaces(LHSIface, RHSIface))
4239 return LHS;
4240
Eli Friedman47f77112008-08-22 00:56:42 +00004241 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00004242 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00004243 case Type::ObjCObjectPointer: {
John McCall9dd450b2009-09-21 23:43:11 +00004244 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4245 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00004246 return LHS;
4247
Steve Naroffc68cfcf2008-12-10 22:14:21 +00004248 return QualType();
Steve Naroff7cae42b2009-07-10 23:34:53 +00004249 }
Eli Friedmancad96382009-02-27 23:04:43 +00004250 case Type::FixedWidthInt:
4251 // Distinct fixed-width integers are not compatible.
4252 return QualType();
Douglas Gregordc572a32009-03-30 22:58:21 +00004253 case Type::TemplateSpecialization:
4254 assert(false && "Dependent types have no size");
4255 break;
Steve Naroff32e44c02007-10-15 20:41:53 +00004256 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00004257
4258 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00004259}
Ted Kremenekfc581a92007-10-31 17:10:13 +00004260
Chris Lattner4ba0cef2008-04-07 07:01:58 +00004261//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004262// Integer Predicates
4263//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00004264
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004265unsigned ASTContext::getIntWidth(QualType T) {
4266 if (T == BoolTy)
4267 return 1;
Chris Lattnerec3a1562009-10-17 20:33:28 +00004268 if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) {
Eli Friedman1efaaea2009-02-13 02:31:07 +00004269 return FWIT->getWidth();
4270 }
4271 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004272 return (unsigned)getTypeSize(T);
4273}
4274
4275QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4276 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00004277
4278 // Turn <4 x signed int> -> <4 x unsigned int>
4279 if (const VectorType *VTy = T->getAs<VectorType>())
4280 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
4281 VTy->getNumElements());
4282
4283 // For enums, we return the unsigned version of the base type.
4284 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004285 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00004286
4287 const BuiltinType *BTy = T->getAs<BuiltinType>();
4288 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004289 switch (BTy->getKind()) {
4290 case BuiltinType::Char_S:
4291 case BuiltinType::SChar:
4292 return UnsignedCharTy;
4293 case BuiltinType::Short:
4294 return UnsignedShortTy;
4295 case BuiltinType::Int:
4296 return UnsignedIntTy;
4297 case BuiltinType::Long:
4298 return UnsignedLongTy;
4299 case BuiltinType::LongLong:
4300 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00004301 case BuiltinType::Int128:
4302 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00004303 default:
4304 assert(0 && "Unexpected signed integer type");
4305 return QualType();
4306 }
4307}
4308
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004309ExternalASTSource::~ExternalASTSource() { }
4310
4311void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004312
4313
4314//===----------------------------------------------------------------------===//
4315// Builtin Type Computation
4316//===----------------------------------------------------------------------===//
4317
4318/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4319/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004320static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004321 ASTContext::GetBuiltinTypeError &Error,
4322 bool AllowTypeModifiers = true) {
4323 // Modifiers.
4324 int HowLong = 0;
4325 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004326
Chris Lattnerecd79c62009-06-14 00:45:47 +00004327 // Read the modifiers first.
4328 bool Done = false;
4329 while (!Done) {
4330 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004331 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004332 case 'S':
4333 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4334 assert(!Signed && "Can't use 'S' modifier multiple times!");
4335 Signed = true;
4336 break;
4337 case 'U':
4338 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4339 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4340 Unsigned = true;
4341 break;
4342 case 'L':
4343 assert(HowLong <= 2 && "Can't have LLLL modifier");
4344 ++HowLong;
4345 break;
4346 }
4347 }
4348
4349 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004350
Chris Lattnerecd79c62009-06-14 00:45:47 +00004351 // Read the base type.
4352 switch (*Str++) {
4353 default: assert(0 && "Unknown builtin type letter!");
4354 case 'v':
4355 assert(HowLong == 0 && !Signed && !Unsigned &&
4356 "Bad modifiers used with 'v'!");
4357 Type = Context.VoidTy;
4358 break;
4359 case 'f':
4360 assert(HowLong == 0 && !Signed && !Unsigned &&
4361 "Bad modifiers used with 'f'!");
4362 Type = Context.FloatTy;
4363 break;
4364 case 'd':
4365 assert(HowLong < 2 && !Signed && !Unsigned &&
4366 "Bad modifiers used with 'd'!");
4367 if (HowLong)
4368 Type = Context.LongDoubleTy;
4369 else
4370 Type = Context.DoubleTy;
4371 break;
4372 case 's':
4373 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4374 if (Unsigned)
4375 Type = Context.UnsignedShortTy;
4376 else
4377 Type = Context.ShortTy;
4378 break;
4379 case 'i':
4380 if (HowLong == 3)
4381 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4382 else if (HowLong == 2)
4383 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4384 else if (HowLong == 1)
4385 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4386 else
4387 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4388 break;
4389 case 'c':
4390 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4391 if (Signed)
4392 Type = Context.SignedCharTy;
4393 else if (Unsigned)
4394 Type = Context.UnsignedCharTy;
4395 else
4396 Type = Context.CharTy;
4397 break;
4398 case 'b': // boolean
4399 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4400 Type = Context.BoolTy;
4401 break;
4402 case 'z': // size_t.
4403 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4404 Type = Context.getSizeType();
4405 break;
4406 case 'F':
4407 Type = Context.getCFConstantStringType();
4408 break;
4409 case 'a':
4410 Type = Context.getBuiltinVaListType();
4411 assert(!Type.isNull() && "builtin va list type not initialized!");
4412 break;
4413 case 'A':
4414 // This is a "reference" to a va_list; however, what exactly
4415 // this means depends on how va_list is defined. There are two
4416 // different kinds of va_list: ones passed by value, and ones
4417 // passed by reference. An example of a by-value va_list is
4418 // x86, where va_list is a char*. An example of by-ref va_list
4419 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4420 // we want this argument to be a char*&; for x86-64, we want
4421 // it to be a __va_list_tag*.
4422 Type = Context.getBuiltinVaListType();
4423 assert(!Type.isNull() && "builtin va list type not initialized!");
4424 if (Type->isArrayType()) {
4425 Type = Context.getArrayDecayedType(Type);
4426 } else {
4427 Type = Context.getLValueReferenceType(Type);
4428 }
4429 break;
4430 case 'V': {
4431 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004432 unsigned NumElements = strtoul(Str, &End, 10);
4433 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004434
Chris Lattnerecd79c62009-06-14 00:45:47 +00004435 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004436
Chris Lattnerecd79c62009-06-14 00:45:47 +00004437 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4438 Type = Context.getVectorType(ElementType, NumElements);
4439 break;
4440 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004441 case 'X': {
4442 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4443 Type = Context.getComplexType(ElementType);
4444 break;
4445 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004446 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004447 Type = Context.getFILEType();
4448 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004449 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004450 return QualType();
4451 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004452 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004453 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004454 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004455 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004456 else
4457 Type = Context.getjmp_bufType();
4458
Mike Stump2adb4da2009-07-28 23:47:15 +00004459 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004460 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004461 return QualType();
4462 }
4463 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004464 }
Mike Stump11289f42009-09-09 15:08:12 +00004465
Chris Lattnerecd79c62009-06-14 00:45:47 +00004466 if (!AllowTypeModifiers)
4467 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004468
Chris Lattnerecd79c62009-06-14 00:45:47 +00004469 Done = false;
4470 while (!Done) {
4471 switch (*Str++) {
4472 default: Done = true; --Str; break;
4473 case '*':
4474 Type = Context.getPointerType(Type);
4475 break;
4476 case '&':
4477 Type = Context.getLValueReferenceType(Type);
4478 break;
4479 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4480 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004481 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004482 break;
4483 }
4484 }
Mike Stump11289f42009-09-09 15:08:12 +00004485
Chris Lattnerecd79c62009-06-14 00:45:47 +00004486 return Type;
4487}
4488
4489/// GetBuiltinType - Return the type for the specified builtin.
4490QualType ASTContext::GetBuiltinType(unsigned id,
4491 GetBuiltinTypeError &Error) {
4492 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004493
Chris Lattnerecd79c62009-06-14 00:45:47 +00004494 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004495
Chris Lattnerecd79c62009-06-14 00:45:47 +00004496 Error = GE_None;
4497 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4498 if (Error != GE_None)
4499 return QualType();
4500 while (TypeStr[0] && TypeStr[0] != '.') {
4501 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4502 if (Error != GE_None)
4503 return QualType();
4504
4505 // Do array -> pointer decay. The builtin should use the decayed type.
4506 if (Ty->isArrayType())
4507 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004508
Chris Lattnerecd79c62009-06-14 00:45:47 +00004509 ArgTypes.push_back(Ty);
4510 }
4511
4512 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4513 "'.' should only occur at end of builtin type list!");
4514
4515 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4516 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4517 return getFunctionNoProtoType(ResType);
4518 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4519 TypeStr[0] == '.', 0);
4520}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004521
4522QualType
4523ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4524 // Perform the usual unary conversions. We do this early so that
4525 // integral promotions to "int" can allow us to exit early, in the
4526 // lhs == rhs check. Also, for conversion purposes, we ignore any
4527 // qualifiers. For example, "const float" and "float" are
4528 // equivalent.
4529 if (lhs->isPromotableIntegerType())
4530 lhs = getPromotedIntegerType(lhs);
4531 else
4532 lhs = lhs.getUnqualifiedType();
4533 if (rhs->isPromotableIntegerType())
4534 rhs = getPromotedIntegerType(rhs);
4535 else
4536 rhs = rhs.getUnqualifiedType();
4537
4538 // If both types are identical, no conversion is needed.
4539 if (lhs == rhs)
4540 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004541
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004542 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4543 // The caller can deal with this (e.g. pointer + int).
4544 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4545 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004546
4547 // At this point, we have two different arithmetic types.
4548
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004549 // Handle complex types first (C99 6.3.1.8p1).
4550 if (lhs->isComplexType() || rhs->isComplexType()) {
4551 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00004552 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004553 // convert the rhs to the lhs complex type.
4554 return lhs;
4555 }
Mike Stump11289f42009-09-09 15:08:12 +00004556 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004557 // convert the lhs to the rhs complex type.
4558 return rhs;
4559 }
4560 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00004561 // When both operands are complex, the shorter operand is converted to the
4562 // type of the longer, and that is the type of the result. This corresponds
4563 // to what is done when combining two real floating-point operands.
4564 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004565 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00004566 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004567 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00004568 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004569 // "double _Complex" is promoted to "long double _Complex".
4570 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004571
4572 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004573 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004574 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004575 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00004576 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004577 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4578 // domains match. This is a requirement for our implementation, C99
4579 // does not require this promotion.
4580 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4581 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4582 return rhs;
4583 } else { // handle "_Complex double, double".
4584 return lhs;
4585 }
4586 }
4587 return lhs; // The domain/size match exactly.
4588 }
4589 // Now handle "real" floating types (i.e. float, double, long double).
4590 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4591 // if we have an integer operand, the result is the real floating type.
4592 if (rhs->isIntegerType()) {
4593 // convert rhs to the lhs floating point type.
4594 return lhs;
4595 }
4596 if (rhs->isComplexIntegerType()) {
4597 // convert rhs to the complex floating point type.
4598 return getComplexType(lhs);
4599 }
4600 if (lhs->isIntegerType()) {
4601 // convert lhs to the rhs floating point type.
4602 return rhs;
4603 }
Mike Stump11289f42009-09-09 15:08:12 +00004604 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004605 // convert lhs to the complex floating point type.
4606 return getComplexType(rhs);
4607 }
4608 // We have two real floating types, float/complex combos were handled above.
4609 // Convert the smaller operand to the bigger result.
4610 int result = getFloatingTypeOrder(lhs, rhs);
4611 if (result > 0) // convert the rhs
4612 return lhs;
4613 assert(result < 0 && "illegal float comparison");
4614 return rhs; // convert the lhs
4615 }
4616 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4617 // Handle GCC complex int extension.
4618 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4619 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4620
4621 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00004622 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004623 rhsComplexInt->getElementType()) >= 0)
4624 return lhs; // convert the rhs
4625 return rhs;
4626 } else if (lhsComplexInt && rhs->isIntegerType()) {
4627 // convert the rhs to the lhs complex type.
4628 return lhs;
4629 } else if (rhsComplexInt && lhs->isIntegerType()) {
4630 // convert the lhs to the rhs complex type.
4631 return rhs;
4632 }
4633 }
4634 // Finally, we have two differing integer types.
4635 // The rules for this case are in C99 6.3.1.8
4636 int compare = getIntegerTypeOrder(lhs, rhs);
4637 bool lhsSigned = lhs->isSignedIntegerType(),
4638 rhsSigned = rhs->isSignedIntegerType();
4639 QualType destType;
4640 if (lhsSigned == rhsSigned) {
4641 // Same signedness; use the higher-ranked type
4642 destType = compare >= 0 ? lhs : rhs;
4643 } else if (compare != (lhsSigned ? 1 : -1)) {
4644 // The unsigned type has greater than or equal rank to the
4645 // signed type, so use the unsigned type
4646 destType = lhsSigned ? rhs : lhs;
4647 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4648 // The two types are different widths; if we are here, that
4649 // means the signed type is larger than the unsigned type, so
4650 // use the signed type.
4651 destType = lhsSigned ? lhs : rhs;
4652 } else {
4653 // The signed type is higher-ranked than the unsigned type,
4654 // but isn't actually any bigger (like unsigned int and long
4655 // on most 32-bit systems). Use the unsigned type corresponding
4656 // to the signed type.
4657 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4658 }
4659 return destType;
4660}