blob: eb09d9ecce05ccaa692020bf75a51ba906de8ff5 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000018#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000023#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Anders Carlsson29445a02009-07-18 21:19:52 +000028#include "RecordLayoutBuilder.h"
29
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
32enum FloatingRank {
33 FloatRank, DoubleRank, LongDoubleRank
34};
35
Chris Lattner61710852008-10-05 17:34:18 +000036ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
37 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000038 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +000039 Builtin::Context &builtins,
Mike Stump1eb44332009-09-09 15:08:12 +000040 bool FreeMem, unsigned size_reserve) :
41 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stump782fa302009-07-28 02:25:19 +000042 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump083c25e2009-10-22 00:49:09 +000043 sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
44 SourceMgr(SM), LangOpts(LOpts),
Mike Stump1eb44332009-09-09 15:08:12 +000045 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +000046 Idents(idents), Selectors(sels),
Mike Stump1eb44332009-09-09 15:08:12 +000047 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall0f436562009-08-17 16:35:33 +000048 ObjCIdRedefinitionType = QualType();
49 ObjCClassRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000050 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000051 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000052 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000053}
54
Reid Spencer5f016e22007-07-11 17:01:13 +000055ASTContext::~ASTContext() {
56 // Deallocate all the types.
57 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000058 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000059 Types.pop_back();
60 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000061
Nuno Lopesb74668e2008-12-17 22:30:25 +000062 {
John McCall0953e762009-09-24 19:53:00 +000063 llvm::FoldingSet<ExtQuals>::iterator
64 I = ExtQualNodes.begin(), E = ExtQualNodes.end();
65 while (I != E)
66 Deallocate(&*I++);
67 }
68
69 {
Nuno Lopesb74668e2008-12-17 22:30:25 +000070 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
71 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
72 while (I != E) {
73 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
74 delete R;
75 }
76 }
77
78 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000079 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
80 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000081 while (I != E) {
82 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
83 delete R;
84 }
85 }
86
Douglas Gregorab452ba2009-03-26 23:50:42 +000087 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000088 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
89 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000090 NNSEnd = NestedNameSpecifiers.end();
91 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000092 /* Increment in loop */)
93 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000094
95 if (GlobalNestedNameSpecifier)
96 GlobalNestedNameSpecifier->Destroy(*this);
97
Eli Friedmanb26153c2008-05-27 03:08:09 +000098 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000099}
100
Mike Stump1eb44332009-09-09 15:08:12 +0000101void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
103 ExternalSource.reset(Source.take());
104}
105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106void ASTContext::PrintStats() const {
107 fprintf(stderr, "*** AST Context Stats:\n");
108 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000109
Douglas Gregordbe833d2009-05-26 14:40:08 +0000110 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000111#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000112#define ABSTRACT_TYPE(Name, Parent)
113#include "clang/AST/TypeNodes.def"
114 0 // Extra
115 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
118 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000119 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121
Douglas Gregordbe833d2009-05-26 14:40:08 +0000122 unsigned Idx = 0;
123 unsigned TotalBytes = 0;
124#define TYPE(Name, Parent) \
125 if (counts[Idx]) \
126 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
127 TotalBytes += counts[Idx] * sizeof(Name##Type); \
128 ++Idx;
129#define ABSTRACT_TYPE(Name, Parent)
130#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Douglas Gregordbe833d2009-05-26 14:40:08 +0000132 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133
134 if (ExternalSource.get()) {
135 fprintf(stderr, "\n");
136 ExternalSource->PrintStats();
137 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
140
John McCalle27ec8a2009-10-23 23:03:21 +0000141void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
John McCall6b304a02009-09-24 23:30:46 +0000142 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
John McCalle27ec8a2009-10-23 23:03:21 +0000143 R = CanQualType::CreateUnsafe(QualType(Ty, 0));
John McCall6b304a02009-09-24 23:30:46 +0000144 Types.push_back(Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000145}
146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147void ASTContext::InitBuiltinTypes() {
148 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 // C99 6.2.5p19.
151 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 // C99 6.2.5p2.
154 InitBuiltinType(BoolTy, BuiltinType::Bool);
155 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000156 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 InitBuiltinType(CharTy, BuiltinType::Char_S);
158 else
159 InitBuiltinType(CharTy, BuiltinType::Char_U);
160 // C99 6.2.5p4.
161 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
162 InitBuiltinType(ShortTy, BuiltinType::Short);
163 InitBuiltinType(IntTy, BuiltinType::Int);
164 InitBuiltinType(LongTy, BuiltinType::Long);
165 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 // C99 6.2.5p6.
168 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
169 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
170 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
171 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
172 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 // C99 6.2.5p10.
175 InitBuiltinType(FloatTy, BuiltinType::Float);
176 InitBuiltinType(DoubleTy, BuiltinType::Double);
177 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000178
Chris Lattner2df9ced2009-04-30 02:43:43 +0000179 // GNU extension, 128-bit integers.
180 InitBuiltinType(Int128Ty, BuiltinType::Int128);
181 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
182
Chris Lattner3a250322009-02-26 23:43:47 +0000183 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
184 InitBuiltinType(WCharTy, BuiltinType::WChar);
185 else // C99
186 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000187
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000188 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
189 InitBuiltinType(Char16Ty, BuiltinType::Char16);
190 else // C99
191 Char16Ty = getFromTargetType(Target.getChar16Type());
192
193 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
194 InitBuiltinType(Char32Ty, BuiltinType::Char32);
195 else // C99
196 Char32Ty = getFromTargetType(Target.getChar32Type());
197
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000198 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000199 InitBuiltinType(OverloadTy, BuiltinType::Overload);
200
201 // Placeholder type for type-dependent expressions whose type is
202 // completely unknown. No code should ever check a type against
203 // DependentTy and users should never see it; however, it is here to
204 // help diagnose failures to properly check for type-dependent
205 // expressions.
206 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000207
Mike Stump1eb44332009-09-09 15:08:12 +0000208 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000209 // not yet been deduced.
210 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 // C99 6.2.5p11.
213 FloatComplexTy = getComplexType(FloatTy);
214 DoubleComplexTy = getComplexType(DoubleTy);
215 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000216
Steve Naroff7e219e42007-10-15 14:41:52 +0000217 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Steve Naroffde2e22d2009-07-15 18:40:39 +0000219 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
220 ObjCIdTypedefType = QualType();
221 ObjCClassTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Steve Naroffde2e22d2009-07-15 18:40:39 +0000223 // Builtin types for 'id' and 'Class'.
224 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
225 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff14108da2009-07-10 23:34:53 +0000226
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000227 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000229 // void * type
230 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000231
232 // nullptr type (C++0x 2.14.7)
233 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234}
235
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000236MemberSpecializationInfo *
Douglas Gregor663b5a02009-10-14 20:14:33 +0000237ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000238 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor663b5a02009-10-14 20:14:33 +0000239 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregor7caa6822009-07-24 20:34:43 +0000240 = InstantiatedFromStaticDataMember.find(Var);
241 if (Pos == InstantiatedFromStaticDataMember.end())
242 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Douglas Gregor7caa6822009-07-24 20:34:43 +0000244 return Pos->second;
245}
246
Mike Stump1eb44332009-09-09 15:08:12 +0000247void
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000248ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
249 TemplateSpecializationKind TSK) {
Douglas Gregor7caa6822009-07-24 20:34:43 +0000250 assert(Inst->isStaticDataMember() && "Not a static data member");
251 assert(Tmpl->isStaticDataMember() && "Not a static data member");
252 assert(!InstantiatedFromStaticDataMember[Inst] &&
253 "Already noted what static data member was instantiated from");
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000254 InstantiatedFromStaticDataMember[Inst]
255 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregor7caa6822009-07-24 20:34:43 +0000256}
257
Anders Carlsson0d8df782009-08-29 19:37:28 +0000258UnresolvedUsingDecl *
259ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000260 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson0d8df782009-08-29 19:37:28 +0000261 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
262 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
263 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Anders Carlsson0d8df782009-08-29 19:37:28 +0000265 return Pos->second;
266}
267
268void
269ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
270 UnresolvedUsingDecl *UUD) {
271 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
272 "Already noted what using decl what instantiated from");
273 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
274}
275
Anders Carlssond8b285f2009-09-01 04:26:58 +0000276FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
277 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
278 = InstantiatedFromUnnamedFieldDecl.find(Field);
279 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
280 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Anders Carlssond8b285f2009-09-01 04:26:58 +0000282 return Pos->second;
283}
284
285void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
286 FieldDecl *Tmpl) {
287 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
288 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
289 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
290 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Anders Carlssond8b285f2009-09-01 04:26:58 +0000292 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
293}
294
Douglas Gregor2e222532009-07-02 17:08:52 +0000295namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000296 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000297 : std::binary_function<SourceRange, SourceRange, bool> {
298 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Douglas Gregor2e222532009-07-02 17:08:52 +0000300 public:
301 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregor2e222532009-07-02 17:08:52 +0000303 bool operator()(SourceRange X, SourceRange Y) {
304 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
305 }
306 };
307}
308
309/// \brief Determine whether the given comment is a Doxygen-style comment.
310///
311/// \param Start the start of the comment text.
312///
313/// \param End the end of the comment text.
314///
315/// \param Member whether we want to check whether this is a member comment
316/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
317/// we only return true when we find a non-member comment.
Mike Stump1eb44332009-09-09 15:08:12 +0000318static bool
319isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregor2e222532009-07-02 17:08:52 +0000320 bool Member = false) {
Mike Stump1eb44332009-09-09 15:08:12 +0000321 const char *BufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000322 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
323 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
324 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Douglas Gregor2e222532009-07-02 17:08:52 +0000326 if (End - Start < 4)
327 return false;
328
329 assert(Start[0] == '/' && "Not a comment?");
330 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
331 return false;
332 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
333 return false;
334
335 return (Start[3] == '<') == Member;
336}
337
338/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump1eb44332009-09-09 15:08:12 +0000339/// it has one.
Douglas Gregor2e222532009-07-02 17:08:52 +0000340const char *ASTContext::getCommentForDecl(const Decl *D) {
341 if (!D)
342 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Douglas Gregor2e222532009-07-02 17:08:52 +0000344 // Check whether we have cached a comment string for this declaration
345 // already.
Mike Stump1eb44332009-09-09 15:08:12 +0000346 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregor2e222532009-07-02 17:08:52 +0000347 = DeclComments.find(D);
348 if (Pos != DeclComments.end())
349 return Pos->second.c_str();
350
Mike Stump1eb44332009-09-09 15:08:12 +0000351 // If we have an external AST source and have not yet loaded comments from
Douglas Gregor2e222532009-07-02 17:08:52 +0000352 // that source, do so now.
353 if (ExternalSource && !LoadedExternalComments) {
354 std::vector<SourceRange> LoadedComments;
355 ExternalSource->ReadComments(LoadedComments);
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Douglas Gregor2e222532009-07-02 17:08:52 +0000357 if (!LoadedComments.empty())
358 Comments.insert(Comments.begin(), LoadedComments.begin(),
359 LoadedComments.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Douglas Gregor2e222532009-07-02 17:08:52 +0000361 LoadedExternalComments = true;
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
364 // If there are no comments anywhere, we won't find anything.
Douglas Gregor2e222532009-07-02 17:08:52 +0000365 if (Comments.empty())
366 return 0;
367
368 // If the declaration doesn't map directly to a location in a file, we
369 // can't find the comment.
370 SourceLocation DeclStartLoc = D->getLocStart();
371 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
372 return 0;
373
374 // Find the comment that occurs just before this declaration.
375 std::vector<SourceRange>::iterator LastComment
Mike Stump1eb44332009-09-09 15:08:12 +0000376 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregor2e222532009-07-02 17:08:52 +0000377 SourceRange(DeclStartLoc),
378 BeforeInTranslationUnit(&SourceMgr));
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Douglas Gregor2e222532009-07-02 17:08:52 +0000380 // Decompose the location for the start of the declaration and find the
381 // beginning of the file buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000382 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregor2e222532009-07-02 17:08:52 +0000383 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000384 const char *FileBufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000385 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregor2e222532009-07-02 17:08:52 +0000387 // First check whether we have a comment for a member.
388 if (LastComment != Comments.end() &&
389 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
390 isDoxygenComment(SourceMgr, *LastComment, true)) {
391 std::pair<FileID, unsigned> LastCommentEndDecomp
392 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
393 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
394 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump1eb44332009-09-09 15:08:12 +0000395 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000396 LastCommentEndDecomp.second)) {
397 // The Doxygen member comment comes after the declaration starts and
398 // is on the same line and in the same file as the declaration. This
399 // is the comment we want.
400 std::string &Result = DeclComments[D];
Mike Stump1eb44332009-09-09 15:08:12 +0000401 Result.append(FileBufferStart +
402 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000403 FileBufferStart + LastCommentEndDecomp.second + 1);
404 return Result.c_str();
405 }
406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Douglas Gregor2e222532009-07-02 17:08:52 +0000408 if (LastComment == Comments.begin())
409 return 0;
410 --LastComment;
411
412 // Decompose the end of the comment.
413 std::pair<FileID, unsigned> LastCommentEndDecomp
414 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Douglas Gregor2e222532009-07-02 17:08:52 +0000416 // If the comment and the declaration aren't in the same file, then they
417 // aren't related.
418 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
419 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Douglas Gregor2e222532009-07-02 17:08:52 +0000421 // Check that we actually have a Doxygen comment.
422 if (!isDoxygenComment(SourceMgr, *LastComment))
423 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Douglas Gregor2e222532009-07-02 17:08:52 +0000425 // Compute the starting line for the declaration and for the end of the
426 // comment (this is expensive).
Mike Stump1eb44332009-09-09 15:08:12 +0000427 unsigned DeclStartLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000428 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
429 unsigned CommentEndLine
Mike Stump1eb44332009-09-09 15:08:12 +0000430 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000431 LastCommentEndDecomp.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregor2e222532009-07-02 17:08:52 +0000433 // If the comment does not end on the line prior to the declaration, then
434 // the comment is not associated with the declaration at all.
435 if (CommentEndLine + 1 != DeclStartLine)
436 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Douglas Gregor2e222532009-07-02 17:08:52 +0000438 // We have a comment, but there may be more comments on the previous lines.
439 // Keep looking so long as the comments are still Doxygen comments and are
440 // still adjacent.
Mike Stump1eb44332009-09-09 15:08:12 +0000441 unsigned ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000442 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
443 std::vector<SourceRange>::iterator FirstComment = LastComment;
444 while (FirstComment != Comments.begin()) {
445 // Look at the previous comment
446 --FirstComment;
447 std::pair<FileID, unsigned> Decomp
448 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Douglas Gregor2e222532009-07-02 17:08:52 +0000450 // If this previous comment is in a different file, we're done.
451 if (Decomp.first != DeclStartDecomp.first) {
452 ++FirstComment;
453 break;
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Douglas Gregor2e222532009-07-02 17:08:52 +0000456 // If this comment is not a Doxygen comment, we're done.
457 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
458 ++FirstComment;
459 break;
460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Douglas Gregor2e222532009-07-02 17:08:52 +0000462 // If the line number is not what we expected, we're done.
463 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
464 if (Line != ExpectedLine) {
465 ++FirstComment;
466 break;
467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Douglas Gregor2e222532009-07-02 17:08:52 +0000469 // Set the next expected line number.
Mike Stump1eb44332009-09-09 15:08:12 +0000470 ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000471 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
472 }
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregor2e222532009-07-02 17:08:52 +0000474 // The iterator range [FirstComment, LastComment] contains all of the
475 // BCPL comments that, together, are associated with this declaration.
476 // Form a single comment block string for this declaration that concatenates
477 // all of these comments.
478 std::string &Result = DeclComments[D];
479 while (FirstComment != LastComment) {
480 std::pair<FileID, unsigned> DecompStart
481 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
482 std::pair<FileID, unsigned> DecompEnd
483 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
484 Result.append(FileBufferStart + DecompStart.second,
485 FileBufferStart + DecompEnd.second + 1);
486 ++FirstComment;
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregor2e222532009-07-02 17:08:52 +0000489 // Append the last comment line.
Mike Stump1eb44332009-09-09 15:08:12 +0000490 Result.append(FileBufferStart +
491 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000492 FileBufferStart + LastCommentEndDecomp.second + 1);
493 return Result.c_str();
494}
495
Chris Lattner464175b2007-07-18 17:52:12 +0000496//===----------------------------------------------------------------------===//
497// Type Sizing and Analysis
498//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000499
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000500/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
501/// scalar floating point type.
502const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000503 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000504 assert(BT && "Not a floating point type!");
505 switch (BT->getKind()) {
506 default: assert(0 && "Not a floating point type!");
507 case BuiltinType::Float: return Target.getFloatFormat();
508 case BuiltinType::Double: return Target.getDoubleFormat();
509 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
510 }
511}
512
Mike Stump196efbf2009-09-22 02:43:44 +0000513/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000514/// specified decl. Note that bitfields do not have a valid alignment, so
515/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000516unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000517 unsigned Align = Target.getCharWidth();
518
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000519 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedmandcdafb62009-02-22 02:56:25 +0000520 Align = std::max(Align, AA->getAlignment());
521
Chris Lattneraf707ab2009-01-24 21:53:27 +0000522 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
523 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000524 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000525 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000526 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000527 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
528 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000529 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
530 T = cast<ArrayType>(T)->getElementType();
531
532 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
533 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000534 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000535
536 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000537}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000538
Chris Lattnera7674d82007-07-13 22:13:22 +0000539/// getTypeSize - Return the size of the specified type, in bits. This method
540/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000541///
542/// FIXME: Pointers into different addr spaces could have different sizes and
543/// alignment requirements: getPointerInfo should take an AddrSpace, this
544/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000545std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000546ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000547 uint64_t Width=0;
548 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000549 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000550#define TYPE(Class, Base)
551#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000552#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000553#define DEPENDENT_TYPE(Class, Base) case Type::Class:
554#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000555 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000556 break;
557
Chris Lattner692233e2007-07-13 22:27:08 +0000558 case Type::FunctionNoProto:
559 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000560 // GCC extension: alignof(function) = 32 bits
561 Width = 0;
562 Align = 32;
563 break;
564
Douglas Gregor72564e72009-02-26 23:50:07 +0000565 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000566 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000567 Width = 0;
568 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
569 break;
570
Steve Narofffb22d962007-08-30 01:06:46 +0000571 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000572 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattner98be4942008-03-05 18:54:05 +0000574 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000575 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000576 Align = EltInfo.second;
577 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000578 }
Nate Begeman213541a2008-04-18 23:10:10 +0000579 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000580 case Type::Vector: {
Chris Lattner9fcfe922009-10-22 05:17:15 +0000581 const VectorType *VT = cast<VectorType>(T);
582 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
583 Width = EltInfo.first*VT->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000584 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000585 // If the alignment is not a power of 2, round up to the next power of 2.
586 // This happens for non-power-of-2 length vectors.
Chris Lattner9fcfe922009-10-22 05:17:15 +0000587 if (VT->getNumElements() & (VT->getNumElements()-1)) {
588 Align = llvm::NextPowerOf2(Align);
589 Width = llvm::RoundUpToAlignment(Width, Align);
590 }
Chris Lattner030d8842007-07-19 22:06:24 +0000591 break;
592 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000593
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000594 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000595 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000596 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000597 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000598 // GCC extension: alignof(void) = 8 bits.
599 Width = 0;
600 Align = 8;
601 break;
602
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000603 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000604 Width = Target.getBoolWidth();
605 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000606 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000607 case BuiltinType::Char_S:
608 case BuiltinType::Char_U:
609 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000610 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000611 Width = Target.getCharWidth();
612 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000613 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000614 case BuiltinType::WChar:
615 Width = Target.getWCharWidth();
616 Align = Target.getWCharAlign();
617 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000618 case BuiltinType::Char16:
619 Width = Target.getChar16Width();
620 Align = Target.getChar16Align();
621 break;
622 case BuiltinType::Char32:
623 Width = Target.getChar32Width();
624 Align = Target.getChar32Align();
625 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000626 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000627 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000628 Width = Target.getShortWidth();
629 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000630 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000631 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000632 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000633 Width = Target.getIntWidth();
634 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000635 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000636 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000637 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000638 Width = Target.getLongWidth();
639 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000640 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000641 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000642 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000643 Width = Target.getLongLongWidth();
644 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000645 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000646 case BuiltinType::Int128:
647 case BuiltinType::UInt128:
648 Width = 128;
649 Align = 128; // int128_t is 128-bit aligned on all targets.
650 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000651 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000652 Width = Target.getFloatWidth();
653 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000654 break;
655 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000656 Width = Target.getDoubleWidth();
657 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000658 break;
659 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000660 Width = Target.getLongDoubleWidth();
661 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000662 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000663 case BuiltinType::NullPtr:
664 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
665 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000666 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000667 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000668 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000669 case Type::FixedWidthInt:
670 // FIXME: This isn't precisely correct; the width/alignment should depend
671 // on the available types for the target
672 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000673 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000674 Align = Width;
675 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000676 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000677 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000678 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000679 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000680 case Type::BlockPointer: {
681 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
682 Width = Target.getPointerWidth(AS);
683 Align = Target.getPointerAlign(AS);
684 break;
685 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000686 case Type::Pointer: {
687 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000688 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000689 Align = Target.getPointerAlign(AS);
690 break;
691 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000692 case Type::LValueReference:
693 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000694 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000695 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000696 // FIXME: This is wrong for struct layout: a reference in a struct has
697 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000698 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000699 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000700 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
701 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
702 // If we ever want to support other ABIs this needs to be abstracted.
703
Sebastian Redlf30208a2009-01-24 21:16:55 +0000704 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000705 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000706 getTypeInfo(getPointerDiffType());
707 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000708 if (Pointee->isFunctionType())
709 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000710 Align = PtrDiffInfo.second;
711 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000712 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000713 case Type::Complex: {
714 // Complex types have the same alignment as their elements, but twice the
715 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000716 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000717 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000718 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000719 Align = EltInfo.second;
720 break;
721 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000722 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000723 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000724 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
725 Width = Layout.getSize();
726 Align = Layout.getAlignment();
727 break;
728 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000729 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000730 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000731 const TagType *TT = cast<TagType>(T);
732
733 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000734 Width = 1;
735 Align = 1;
736 break;
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Daniel Dunbar1d751182008-11-08 05:48:37 +0000739 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000740 return getTypeInfo(ET->getDecl()->getIntegerType());
741
Daniel Dunbar1d751182008-11-08 05:48:37 +0000742 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000743 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
744 Width = Layout.getSize();
745 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000746 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000747 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000748
Chris Lattner9fcfe922009-10-22 05:17:15 +0000749 case Type::SubstTemplateTypeParm:
John McCall49a832b2009-10-18 09:09:24 +0000750 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
751 getReplacementType().getTypePtr());
John McCall49a832b2009-10-18 09:09:24 +0000752
Chris Lattner9fcfe922009-10-22 05:17:15 +0000753 case Type::Elaborated:
754 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType()
755 .getTypePtr());
John McCall7da24312009-09-05 00:15:47 +0000756
Douglas Gregor18857642009-04-30 17:32:17 +0000757 case Type::Typedef: {
758 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000759 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregor18857642009-04-30 17:32:17 +0000760 Align = Aligned->getAlignment();
761 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
762 } else
763 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000764 break;
Chris Lattner71763312008-04-06 22:05:18 +0000765 }
Douglas Gregor18857642009-04-30 17:32:17 +0000766
767 case Type::TypeOfExpr:
768 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
769 .getTypePtr());
770
771 case Type::TypeOf:
772 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
773
Anders Carlsson395b4752009-06-24 19:06:50 +0000774 case Type::Decltype:
775 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
776 .getTypePtr());
777
Douglas Gregor18857642009-04-30 17:32:17 +0000778 case Type::QualifiedName:
779 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Douglas Gregor18857642009-04-30 17:32:17 +0000781 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000782 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000783 "Cannot request the size of a dependent type");
784 // FIXME: this is likely to be wrong once we support template
785 // aliases, since a template alias could refer to a typedef that
786 // has an __aligned__ attribute on it.
787 return getTypeInfo(getCanonicalType(T));
788 }
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattner464175b2007-07-18 17:52:12 +0000790 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000791 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000792}
793
Chris Lattner34ebde42009-01-27 18:08:34 +0000794/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
795/// type for the current target in bits. This can be different than the ABI
796/// alignment in cases where it is beneficial for performance to overalign
797/// a data type.
798unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
799 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000800
801 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000802 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000803 T = CT->getElementType().getTypePtr();
804 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
805 T->isSpecificBuiltinType(BuiltinType::LongLong))
806 return std::max(ABIAlign, (unsigned)getTypeSize(T));
807
Chris Lattner34ebde42009-01-27 18:08:34 +0000808 return ABIAlign;
809}
810
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000811static void CollectLocalObjCIvars(ASTContext *Ctx,
812 const ObjCInterfaceDecl *OI,
813 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000814 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
815 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000816 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000817 if (!IVDecl->isInvalidDecl())
818 Fields.push_back(cast<FieldDecl>(IVDecl));
819 }
820}
821
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000822void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
823 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
824 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
825 CollectObjCIvars(SuperClass, Fields);
826 CollectLocalObjCIvars(this, OI, Fields);
827}
828
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000829/// ShallowCollectObjCIvars -
830/// Collect all ivars, including those synthesized, in the current class.
831///
832void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
833 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
834 bool CollectSynthesized) {
835 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
836 E = OI->ivar_end(); I != E; ++I) {
837 Ivars.push_back(*I);
838 }
839 if (CollectSynthesized)
840 CollectSynthesizedIvars(OI, Ivars);
841}
842
Fariborz Jahanian98200742009-05-12 18:14:29 +0000843void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
844 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000845 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
846 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000847 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
848 Ivars.push_back(Ivar);
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Fariborz Jahanian98200742009-05-12 18:14:29 +0000850 // Also look into nested protocols.
851 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
852 E = PD->protocol_end(); P != E; ++P)
853 CollectProtocolSynthesizedIvars(*P, Ivars);
854}
855
856/// CollectSynthesizedIvars -
857/// This routine collect synthesized ivars for the designated class.
858///
859void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
860 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000861 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
862 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000863 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
864 Ivars.push_back(Ivar);
865 }
866 // Also look into interface's protocol list for properties declared
867 // in the protocol and whose ivars are synthesized.
868 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
869 PE = OI->protocol_end(); P != PE; ++P) {
870 ObjCProtocolDecl *PD = (*P);
871 CollectProtocolSynthesizedIvars(PD, Ivars);
872 }
873}
874
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000875unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
876 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000877 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
878 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000879 if ((*I)->getPropertyIvarDecl())
880 ++count;
881
882 // Also look into nested protocols.
883 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
884 E = PD->protocol_end(); P != E; ++P)
885 count += CountProtocolSynthesizedIvars(*P);
886 return count;
887}
888
Mike Stump1eb44332009-09-09 15:08:12 +0000889unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000890 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000891 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
892 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000893 if ((*I)->getPropertyIvarDecl())
894 ++count;
895 }
896 // Also look into interface's protocol list for properties declared
897 // in the protocol and whose ivars are synthesized.
898 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
899 PE = OI->protocol_end(); P != PE; ++P) {
900 ObjCProtocolDecl *PD = (*P);
901 count += CountProtocolSynthesizedIvars(PD);
902 }
903 return count;
904}
905
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000906/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
907ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
908 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
909 I = ObjCImpls.find(D);
910 if (I != ObjCImpls.end())
911 return cast<ObjCImplementationDecl>(I->second);
912 return 0;
913}
914/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
915ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
916 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
917 I = ObjCImpls.find(D);
918 if (I != ObjCImpls.end())
919 return cast<ObjCCategoryImplDecl>(I->second);
920 return 0;
921}
922
923/// \brief Set the implementation of ObjCInterfaceDecl.
924void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
925 ObjCImplementationDecl *ImplD) {
926 assert(IFaceD && ImplD && "Passed null params");
927 ObjCImpls[IFaceD] = ImplD;
928}
929/// \brief Set the implementation of ObjCCategoryDecl.
930void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
931 ObjCCategoryImplDecl *ImplD) {
932 assert(CatD && ImplD && "Passed null params");
933 ObjCImpls[CatD] = ImplD;
934}
935
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000936/// \brief Allocate an uninitialized DeclaratorInfo.
937///
938/// The caller should initialize the memory held by DeclaratorInfo using
939/// the TypeLoc wrappers.
940///
941/// \param T the type that will be the basis for type source info. This type
942/// should refer to how the declarator was written in source code, not to
943/// what type semantic analysis resolved the declarator to.
John McCall109de5e2009-10-21 00:23:54 +0000944DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T,
945 unsigned DataSize) {
946 if (!DataSize)
947 DataSize = TypeLoc::getFullDataSizeForType(T);
948 else
949 assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
950 "incorrect data size provided to CreateDeclaratorInfo!");
951
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000952 DeclaratorInfo *DInfo =
953 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
954 new (DInfo) DeclaratorInfo(T);
955 return DInfo;
956}
957
John McCalla4eb74d2009-10-23 21:14:09 +0000958DeclaratorInfo *ASTContext::getTrivialDeclaratorInfo(QualType T,
959 SourceLocation L) {
960 DeclaratorInfo *DI = CreateDeclaratorInfo(T);
961 DI->getTypeLoc().initialize(L);
962 return DI;
963}
964
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000965/// getInterfaceLayoutImpl - Get or compute information about the
966/// layout of the given interface.
967///
968/// \param Impl - If given, also include the layout of the interface's
969/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000970const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000971ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
972 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000973 assert(!D->isForwardDecl() && "Invalid interface decl!");
974
Devang Patel44a3dde2008-06-04 21:54:36 +0000975 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000976 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000977 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
978 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
979 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000980
Daniel Dunbar453addb2009-05-03 11:16:44 +0000981 // Add in synthesized ivar count if laying out an implementation.
982 if (Impl) {
Anders Carlsson29445a02009-07-18 21:19:52 +0000983 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000984 unsigned SynthCount = CountSynthesizedIvars(D);
985 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000986 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000987 // entry. Note we can't cache this because we simply free all
988 // entries later; however we shouldn't look up implementations
989 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000990 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000991 return getObjCLayout(D, 0);
992 }
993
Mike Stump1eb44332009-09-09 15:08:12 +0000994 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000995 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
996 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Devang Patel44a3dde2008-06-04 21:54:36 +0000998 return *NewEntry;
999}
1000
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +00001001const ASTRecordLayout &
1002ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
1003 return getObjCLayout(D, 0);
1004}
1005
1006const ASTRecordLayout &
1007ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
1008 return getObjCLayout(D->getClassInterface(), D);
1009}
1010
Devang Patel88a981b2007-11-01 19:11:01 +00001011/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +00001012/// specified record (struct/union/class), which indicates its size and field
1013/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +00001014const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001015 D = D->getDefinition(*this);
1016 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +00001017
Chris Lattner464175b2007-07-18 17:52:12 +00001018 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +00001019 // Note that we can't save a reference to the entry because this function
1020 // is recursive.
1021 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +00001022 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001023
Mike Stump1eb44332009-09-09 15:08:12 +00001024 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001025 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +00001026 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattner5d2a6302007-07-18 18:26:58 +00001028 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001029}
1030
Chris Lattnera7674d82007-07-13 22:13:22 +00001031//===----------------------------------------------------------------------===//
1032// Type creation/memoization methods
1033//===----------------------------------------------------------------------===//
1034
John McCall0953e762009-09-24 19:53:00 +00001035QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1036 unsigned Fast = Quals.getFastQualifiers();
1037 Quals.removeFastQualifiers();
1038
1039 // Check if we've already instantiated this type.
1040 llvm::FoldingSetNodeID ID;
1041 ExtQuals::Profile(ID, TypeNode, Quals);
1042 void *InsertPos = 0;
1043 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1044 assert(EQ->getQualifiers() == Quals);
1045 QualType T = QualType(EQ, Fast);
1046 return T;
1047 }
1048
John McCall6b304a02009-09-24 23:30:46 +00001049 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall0953e762009-09-24 19:53:00 +00001050 ExtQualNodes.InsertNode(New, InsertPos);
1051 QualType T = QualType(New, Fast);
1052 return T;
1053}
1054
1055QualType ASTContext::getVolatileType(QualType T) {
1056 QualType CanT = getCanonicalType(T);
1057 if (CanT.isVolatileQualified()) return T;
1058
1059 QualifierCollector Quals;
1060 const Type *TypeNode = Quals.strip(T);
1061 Quals.addVolatile();
1062
1063 return getExtQualType(TypeNode, Quals);
1064}
1065
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001066QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001067 QualType CanT = getCanonicalType(T);
1068 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001069 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001070
John McCall0953e762009-09-24 19:53:00 +00001071 // If we are composing extended qualifiers together, merge together
1072 // into one ExtQuals node.
1073 QualifierCollector Quals;
1074 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001075
John McCall0953e762009-09-24 19:53:00 +00001076 // If this type already has an address space specified, it cannot get
1077 // another one.
1078 assert(!Quals.hasAddressSpace() &&
1079 "Type cannot be in multiple addr spaces!");
1080 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001081
John McCall0953e762009-09-24 19:53:00 +00001082 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001083}
1084
Chris Lattnerb7d25532009-02-18 22:53:11 +00001085QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001086 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001087 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001088 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001089 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001091 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001092 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001093 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001094 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1095 return getPointerType(ResultType);
1096 }
1097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
John McCall0953e762009-09-24 19:53:00 +00001099 // If we are composing extended qualifiers together, merge together
1100 // into one ExtQuals node.
1101 QualifierCollector Quals;
1102 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001103
John McCall0953e762009-09-24 19:53:00 +00001104 // If this type already has an ObjCGC specified, it cannot get
1105 // another one.
1106 assert(!Quals.hasObjCGCAttr() &&
1107 "Type cannot have multiple ObjCGCs!");
1108 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001109
John McCall0953e762009-09-24 19:53:00 +00001110 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001111}
Chris Lattnera7674d82007-07-13 22:13:22 +00001112
Mike Stump24556362009-07-25 21:26:53 +00001113QualType ASTContext::getNoReturnType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00001114 QualType ResultType;
Mike Stump24556362009-07-25 21:26:53 +00001115 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001116 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001117 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001118 ResultType = getPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001119 } else if (T->isBlockPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001120 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001121 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001122 ResultType = getBlockPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001123 } else {
1124 assert (T->isFunctionType()
1125 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Benjamin Kramerbdbeeb52009-09-25 11:47:22 +00001127 if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) {
1128 ResultType = getFunctionNoProtoType(FNPT->getResultType(), true);
John McCall0953e762009-09-24 19:53:00 +00001129 } else {
1130 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1131 ResultType
1132 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1133 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1134 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1135 F->getNumExceptions(), F->exception_begin(), true);
1136 }
Mike Stump24556362009-07-25 21:26:53 +00001137 }
John McCall0953e762009-09-24 19:53:00 +00001138
1139 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump24556362009-07-25 21:26:53 +00001140}
1141
Reid Spencer5f016e22007-07-11 17:01:13 +00001142/// getComplexType - Return the uniqued reference to the type for a complex
1143/// number with the specified element type.
1144QualType ASTContext::getComplexType(QualType T) {
1145 // Unique pointers, to guarantee there is only one pointer of a particular
1146 // structure.
1147 llvm::FoldingSetNodeID ID;
1148 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 void *InsertPos = 0;
1151 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1152 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 // If the pointee type isn't canonical, this won't be a canonical type either,
1155 // so fill in the canonical type field.
1156 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001157 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001158 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 // Get the new insert position for the node we care about.
1161 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001162 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 }
John McCall6b304a02009-09-24 23:30:46 +00001164 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 Types.push_back(New);
1166 ComplexTypes.InsertNode(New, InsertPos);
1167 return QualType(New, 0);
1168}
1169
Eli Friedmanf98aba32009-02-13 02:31:07 +00001170QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1171 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1172 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1173 FixedWidthIntType *&Entry = Map[Width];
1174 if (!Entry)
1175 Entry = new FixedWidthIntType(Width, Signed);
1176 return QualType(Entry, 0);
1177}
Reid Spencer5f016e22007-07-11 17:01:13 +00001178
1179/// getPointerType - Return the uniqued reference to the type for a pointer to
1180/// the specified type.
1181QualType ASTContext::getPointerType(QualType T) {
1182 // Unique pointers, to guarantee there is only one pointer of a particular
1183 // structure.
1184 llvm::FoldingSetNodeID ID;
1185 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 void *InsertPos = 0;
1188 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1189 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // If the pointee type isn't canonical, this won't be a canonical type either,
1192 // so fill in the canonical type field.
1193 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001194 if (!T.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001195 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 // Get the new insert position for the node we care about.
1198 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001199 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 }
John McCall6b304a02009-09-24 23:30:46 +00001201 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 Types.push_back(New);
1203 PointerTypes.InsertNode(New, InsertPos);
1204 return QualType(New, 0);
1205}
1206
Mike Stump1eb44332009-09-09 15:08:12 +00001207/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001208/// a pointer to the specified block.
1209QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001210 assert(T->isFunctionType() && "block of function types only");
1211 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001212 // structure.
1213 llvm::FoldingSetNodeID ID;
1214 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Steve Naroff5618bd42008-08-27 16:04:49 +00001216 void *InsertPos = 0;
1217 if (BlockPointerType *PT =
1218 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1219 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
1221 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001222 // type either so fill in the canonical type field.
1223 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001224 if (!T.isCanonical()) {
Steve Naroff5618bd42008-08-27 16:04:49 +00001225 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Steve Naroff5618bd42008-08-27 16:04:49 +00001227 // Get the new insert position for the node we care about.
1228 BlockPointerType *NewIP =
1229 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001230 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001231 }
John McCall6b304a02009-09-24 23:30:46 +00001232 BlockPointerType *New
1233 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001234 Types.push_back(New);
1235 BlockPointerTypes.InsertNode(New, InsertPos);
1236 return QualType(New, 0);
1237}
1238
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001239/// getLValueReferenceType - Return the uniqued reference to the type for an
1240/// lvalue reference to the specified type.
John McCall54e14c42009-10-22 22:37:11 +00001241QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 // Unique pointers, to guarantee there is only one pointer of a particular
1243 // structure.
1244 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001245 ReferenceType::Profile(ID, T, SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001246
1247 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001248 if (LValueReferenceType *RT =
1249 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001251
John McCall54e14c42009-10-22 22:37:11 +00001252 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1253
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 // If the referencee type isn't canonical, this won't be a canonical type
1255 // either, so fill in the canonical type field.
1256 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001257 if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
1258 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1259 Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001260
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001262 LValueReferenceType *NewIP =
1263 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001264 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 }
1266
John McCall6b304a02009-09-24 23:30:46 +00001267 LValueReferenceType *New
John McCall54e14c42009-10-22 22:37:11 +00001268 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
1269 SpelledAsLValue);
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001271 LValueReferenceTypes.InsertNode(New, InsertPos);
John McCall54e14c42009-10-22 22:37:11 +00001272
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001273 return QualType(New, 0);
1274}
1275
1276/// getRValueReferenceType - Return the uniqued reference to the type for an
1277/// rvalue reference to the specified type.
1278QualType ASTContext::getRValueReferenceType(QualType T) {
1279 // Unique pointers, to guarantee there is only one pointer of a particular
1280 // structure.
1281 llvm::FoldingSetNodeID ID;
John McCall54e14c42009-10-22 22:37:11 +00001282 ReferenceType::Profile(ID, T, false);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001283
1284 void *InsertPos = 0;
1285 if (RValueReferenceType *RT =
1286 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1287 return QualType(RT, 0);
1288
John McCall54e14c42009-10-22 22:37:11 +00001289 const ReferenceType *InnerRef = T->getAs<ReferenceType>();
1290
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001291 // If the referencee type isn't canonical, this won't be a canonical type
1292 // either, so fill in the canonical type field.
1293 QualType Canonical;
John McCall54e14c42009-10-22 22:37:11 +00001294 if (InnerRef || !T.isCanonical()) {
1295 QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
1296 Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001297
1298 // Get the new insert position for the node we care about.
1299 RValueReferenceType *NewIP =
1300 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1301 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1302 }
1303
John McCall6b304a02009-09-24 23:30:46 +00001304 RValueReferenceType *New
1305 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001306 Types.push_back(New);
1307 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 return QualType(New, 0);
1309}
1310
Sebastian Redlf30208a2009-01-24 21:16:55 +00001311/// getMemberPointerType - Return the uniqued reference to the type for a
1312/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001313QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001314 // Unique pointers, to guarantee there is only one pointer of a particular
1315 // structure.
1316 llvm::FoldingSetNodeID ID;
1317 MemberPointerType::Profile(ID, T, Cls);
1318
1319 void *InsertPos = 0;
1320 if (MemberPointerType *PT =
1321 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1322 return QualType(PT, 0);
1323
1324 // If the pointee or class type isn't canonical, this won't be a canonical
1325 // type either, so fill in the canonical type field.
1326 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001327 if (!T.isCanonical()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001328 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1329
1330 // Get the new insert position for the node we care about.
1331 MemberPointerType *NewIP =
1332 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1333 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1334 }
John McCall6b304a02009-09-24 23:30:46 +00001335 MemberPointerType *New
1336 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001337 Types.push_back(New);
1338 MemberPointerTypes.InsertNode(New, InsertPos);
1339 return QualType(New, 0);
1340}
1341
Mike Stump1eb44332009-09-09 15:08:12 +00001342/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001343/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001344QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001345 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001346 ArrayType::ArraySizeModifier ASM,
1347 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001348 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1349 "Constant array of VLAs is illegal!");
1350
Chris Lattner38aeec72009-05-13 04:12:56 +00001351 // Convert the array size into a canonical width matching the pointer size for
1352 // the target.
1353 llvm::APInt ArySize(ArySizeIn);
1354 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Reid Spencer5f016e22007-07-11 17:01:13 +00001356 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001357 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001360 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001361 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 // If the element type isn't canonical, this won't be a canonical type either,
1365 // so fill in the canonical type field.
1366 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001367 if (!EltTy.isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001368 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001369 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001371 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001372 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001373 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 }
Mike Stump1eb44332009-09-09 15:08:12 +00001375
John McCall6b304a02009-09-24 23:30:46 +00001376 ConstantArrayType *New = new(*this,TypeAlignment)
1377 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001378 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 Types.push_back(New);
1380 return QualType(New, 0);
1381}
1382
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001383/// getVariableArrayType - Returns a non-unique reference to the type for a
1384/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001385QualType ASTContext::getVariableArrayType(QualType EltTy,
1386 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001387 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001388 unsigned EltTypeQuals,
1389 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001390 // Since we don't unique expressions, it isn't possible to unique VLA's
1391 // that have an expression provided for their size.
1392
John McCall6b304a02009-09-24 23:30:46 +00001393 VariableArrayType *New = new(*this, TypeAlignment)
1394 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001395
1396 VariableArrayTypes.push_back(New);
1397 Types.push_back(New);
1398 return QualType(New, 0);
1399}
1400
Douglas Gregor898574e2008-12-05 23:32:09 +00001401/// getDependentSizedArrayType - Returns a non-unique reference to
1402/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001403/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001404QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1405 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001406 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001407 unsigned EltTypeQuals,
1408 SourceRange Brackets) {
Mike Stump1eb44332009-09-09 15:08:12 +00001409 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001410 "Size must be type- or value-dependent!");
1411
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001412 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001413 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001414 EltTypeQuals, NumElts);
Douglas Gregor898574e2008-12-05 23:32:09 +00001415
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001416 void *InsertPos = 0;
1417 DependentSizedArrayType *Canon
1418 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1419 DependentSizedArrayType *New;
1420 if (Canon) {
1421 // We already have a canonical version of this array type; use it as
1422 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001423 New = new (*this, TypeAlignment)
1424 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1425 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001426 } else {
1427 QualType CanonEltTy = getCanonicalType(EltTy);
1428 if (CanonEltTy == EltTy) {
John McCall6b304a02009-09-24 23:30:46 +00001429 New = new (*this, TypeAlignment)
1430 DependentSizedArrayType(*this, EltTy, QualType(),
1431 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001432 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1433 } else {
1434 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1435 ASM, EltTypeQuals,
1436 SourceRange());
John McCall6b304a02009-09-24 23:30:46 +00001437 New = new (*this, TypeAlignment)
1438 DependentSizedArrayType(*this, EltTy, Canon,
1439 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001440 }
1441 }
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Douglas Gregor898574e2008-12-05 23:32:09 +00001443 Types.push_back(New);
1444 return QualType(New, 0);
1445}
1446
Eli Friedmanc5773c42008-02-15 18:16:39 +00001447QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1448 ArrayType::ArraySizeModifier ASM,
1449 unsigned EltTypeQuals) {
1450 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001451 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001452
1453 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001454 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001455 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1456 return QualType(ATP, 0);
1457
1458 // If the element type isn't canonical, this won't be a canonical type
1459 // either, so fill in the canonical type field.
1460 QualType Canonical;
1461
John McCall467b27b2009-10-22 20:10:53 +00001462 if (!EltTy.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001463 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001464 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001465
1466 // Get the new insert position for the node we care about.
1467 IncompleteArrayType *NewIP =
1468 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001469 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001470 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001471
John McCall6b304a02009-09-24 23:30:46 +00001472 IncompleteArrayType *New = new (*this, TypeAlignment)
1473 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001474
1475 IncompleteArrayTypes.InsertNode(New, InsertPos);
1476 Types.push_back(New);
1477 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001478}
1479
Steve Naroff73322922007-07-18 18:00:27 +00001480/// getVectorType - Return the unique reference to a vector type of
1481/// the specified element type and size. VectorType must be a built-in type.
1482QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Chris Lattnerf52ab252008-04-06 22:59:24 +00001485 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001486 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 // Check if we've already instantiated a vector of this type.
1489 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001490 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 void *InsertPos = 0;
1492 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1493 return QualType(VTP, 0);
1494
1495 // If the element type isn't canonical, this won't be a canonical type either,
1496 // so fill in the canonical type field.
1497 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001498 if (!vecType.isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001499 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 // Get the new insert position for the node we care about.
1502 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001503 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 }
John McCall6b304a02009-09-24 23:30:46 +00001505 VectorType *New = new (*this, TypeAlignment)
1506 VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001507 VectorTypes.InsertNode(New, InsertPos);
1508 Types.push_back(New);
1509 return QualType(New, 0);
1510}
1511
Nate Begeman213541a2008-04-18 23:10:10 +00001512/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001513/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001514QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001515 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Chris Lattnerf52ab252008-04-06 22:59:24 +00001517 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001518 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Steve Naroff73322922007-07-18 18:00:27 +00001520 // Check if we've already instantiated a vector of this type.
1521 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001522 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001523 void *InsertPos = 0;
1524 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1525 return QualType(VTP, 0);
1526
1527 // If the element type isn't canonical, this won't be a canonical type either,
1528 // so fill in the canonical type field.
1529 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001530 if (!vecType.isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001531 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Steve Naroff73322922007-07-18 18:00:27 +00001533 // Get the new insert position for the node we care about.
1534 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001535 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001536 }
John McCall6b304a02009-09-24 23:30:46 +00001537 ExtVectorType *New = new (*this, TypeAlignment)
1538 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001539 VectorTypes.InsertNode(New, InsertPos);
1540 Types.push_back(New);
1541 return QualType(New, 0);
1542}
1543
Mike Stump1eb44332009-09-09 15:08:12 +00001544QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001545 Expr *SizeExpr,
1546 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001547 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001548 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001549 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001551 void *InsertPos = 0;
1552 DependentSizedExtVectorType *Canon
1553 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1554 DependentSizedExtVectorType *New;
1555 if (Canon) {
1556 // We already have a canonical version of this array type; use it as
1557 // the canonical type for a newly-built type.
John McCall6b304a02009-09-24 23:30:46 +00001558 New = new (*this, TypeAlignment)
1559 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1560 SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001561 } else {
1562 QualType CanonVecTy = getCanonicalType(vecType);
1563 if (CanonVecTy == vecType) {
John McCall6b304a02009-09-24 23:30:46 +00001564 New = new (*this, TypeAlignment)
1565 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1566 AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001567 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1568 } else {
1569 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1570 SourceLocation());
John McCall6b304a02009-09-24 23:30:46 +00001571 New = new (*this, TypeAlignment)
1572 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001573 }
1574 }
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001576 Types.push_back(New);
1577 return QualType(New, 0);
1578}
1579
Douglas Gregor72564e72009-02-26 23:50:07 +00001580/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001581///
Mike Stump24556362009-07-25 21:26:53 +00001582QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001583 // Unique functions, to guarantee there is only one function of a particular
1584 // structure.
1585 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001586 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001589 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001590 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 QualType Canonical;
John McCall467b27b2009-10-22 20:10:53 +00001594 if (!ResultTy.isCanonical()) {
Mike Stump24556362009-07-25 21:26:53 +00001595 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001598 FunctionNoProtoType *NewIP =
1599 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001600 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 }
Mike Stump1eb44332009-09-09 15:08:12 +00001602
John McCall6b304a02009-09-24 23:30:46 +00001603 FunctionNoProtoType *New = new (*this, TypeAlignment)
1604 FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001606 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 return QualType(New, 0);
1608}
1609
1610/// getFunctionType - Return a normal function type with a typed argument
1611/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001612QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001613 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001614 unsigned TypeQuals, bool hasExceptionSpec,
1615 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump24556362009-07-25 21:26:53 +00001616 const QualType *ExArray, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001617 // Unique functions, to guarantee there is only one function of a particular
1618 // structure.
1619 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001620 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001621 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001622 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001623
1624 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001625 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001626 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001628
1629 // Determine whether the type being created is already canonical or not.
John McCall54e14c42009-10-22 22:37:11 +00001630 bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical();
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001632 if (!ArgArray[i].isCanonicalAsParam())
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 isCanonical = false;
1634
1635 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001636 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 QualType Canonical;
1638 if (!isCanonical) {
1639 llvm::SmallVector<QualType, 16> CanonicalArgs;
1640 CanonicalArgs.reserve(NumArgs);
1641 for (unsigned i = 0; i != NumArgs; ++i)
John McCall54e14c42009-10-22 22:37:11 +00001642 CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001643
Chris Lattnerf52ab252008-04-06 22:59:24 +00001644 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001645 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001646 isVariadic, TypeQuals, false,
1647 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001648
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001650 FunctionProtoType *NewIP =
1651 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001652 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001653 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001654
Douglas Gregor72564e72009-02-26 23:50:07 +00001655 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001656 // for two variable size arrays (for parameter and exception types) at the
1657 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001658 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001659 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1660 NumArgs*sizeof(QualType) +
John McCall6b304a02009-09-24 23:30:46 +00001661 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregor72564e72009-02-26 23:50:07 +00001662 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001663 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001664 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001666 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 return QualType(FTP, 0);
1668}
1669
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001670/// getTypeDeclType - Return the unique reference to the type for the
1671/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001672QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001673 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001674 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001676 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001677 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001678 else if (isa<TemplateTypeParmDecl>(Decl)) {
1679 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001680 } else if (ObjCInterfaceDecl *ObjCInterface
1681 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001682 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001683
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001684 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001685 if (PrevDecl)
1686 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001687 else
John McCall6b304a02009-09-24 23:30:46 +00001688 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001689 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001690 if (PrevDecl)
1691 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001692 else
John McCall6b304a02009-09-24 23:30:46 +00001693 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001694 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001695 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001696
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001697 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001698 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001699}
1700
Reid Spencer5f016e22007-07-11 17:01:13 +00001701/// getTypedefType - Return the unique reference to the type for the
1702/// specified typename decl.
1703QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1704 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Chris Lattnerf52ab252008-04-06 22:59:24 +00001706 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall6b304a02009-09-24 23:30:46 +00001707 Decl->TypeForDecl = new(*this, TypeAlignment)
1708 TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 Types.push_back(Decl->TypeForDecl);
1710 return QualType(Decl->TypeForDecl, 0);
1711}
1712
John McCall49a832b2009-10-18 09:09:24 +00001713/// \brief Retrieve a substitution-result type.
1714QualType
1715ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1716 QualType Replacement) {
John McCall467b27b2009-10-22 20:10:53 +00001717 assert(Replacement.isCanonical()
John McCall49a832b2009-10-18 09:09:24 +00001718 && "replacement types must always be canonical");
1719
1720 llvm::FoldingSetNodeID ID;
1721 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1722 void *InsertPos = 0;
1723 SubstTemplateTypeParmType *SubstParm
1724 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1725
1726 if (!SubstParm) {
1727 SubstParm = new (*this, TypeAlignment)
1728 SubstTemplateTypeParmType(Parm, Replacement);
1729 Types.push_back(SubstParm);
1730 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1731 }
1732
1733 return QualType(SubstParm, 0);
1734}
1735
Douglas Gregorfab9d672009-02-05 23:33:38 +00001736/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001737/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001738/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001739QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001740 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001741 IdentifierInfo *Name) {
1742 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001743 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001744 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001745 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001746 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1747
1748 if (TypeParm)
1749 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001751 if (Name) {
1752 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall6b304a02009-09-24 23:30:46 +00001753 TypeParm = new (*this, TypeAlignment)
1754 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001755 } else
John McCall6b304a02009-09-24 23:30:46 +00001756 TypeParm = new (*this, TypeAlignment)
1757 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001758
1759 Types.push_back(TypeParm);
1760 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1761
1762 return QualType(TypeParm, 0);
1763}
1764
Mike Stump1eb44332009-09-09 15:08:12 +00001765QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001766ASTContext::getTemplateSpecializationType(TemplateName Template,
1767 const TemplateArgument *Args,
1768 unsigned NumArgs,
1769 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001770 if (!Canon.isNull())
1771 Canon = getCanonicalType(Canon);
1772 else {
1773 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001774 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1775 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1776 CanonArgs.reserve(NumArgs);
1777 for (unsigned I = 0; I != NumArgs; ++I)
1778 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1779
1780 // Determine whether this canonical template specialization type already
1781 // exists.
1782 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001783 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001784 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001785
1786 void *InsertPos = 0;
1787 TemplateSpecializationType *Spec
1788 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Douglas Gregor1275ae02009-07-28 23:00:59 +00001790 if (!Spec) {
1791 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001792 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001793 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001794 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001795 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001796 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001797 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001798 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001799 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001800 }
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Douglas Gregorb88e8882009-07-30 17:40:51 +00001802 if (Canon.isNull())
1803 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001804 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001805 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001806 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001807
Douglas Gregor1275ae02009-07-28 23:00:59 +00001808 // Allocate the (non-canonical) template specialization type, but don't
1809 // try to unique it: these types typically have location information that
1810 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001811 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001812 sizeof(TemplateArgument) * NumArgs),
John McCall6b304a02009-09-24 23:30:46 +00001813 TypeAlignment);
Mike Stump1eb44332009-09-09 15:08:12 +00001814 TemplateSpecializationType *Spec
1815 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001816 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Douglas Gregor55f6b142009-02-09 18:46:07 +00001818 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001819 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001820}
1821
Mike Stump1eb44332009-09-09 15:08:12 +00001822QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001823ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001824 QualType NamedType) {
1825 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001826 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001827
1828 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001829 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001830 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1831 if (T)
1832 return QualType(T, 0);
1833
Mike Stump1eb44332009-09-09 15:08:12 +00001834 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001835 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001836 Types.push_back(T);
1837 QualifiedNameTypes.InsertNode(T, InsertPos);
1838 return QualType(T, 0);
1839}
1840
Mike Stump1eb44332009-09-09 15:08:12 +00001841QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001842 const IdentifierInfo *Name,
1843 QualType Canon) {
1844 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1845
1846 if (Canon.isNull()) {
1847 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1848 if (CanonNNS != NNS)
1849 Canon = getTypenameType(CanonNNS, Name);
1850 }
1851
1852 llvm::FoldingSetNodeID ID;
1853 TypenameType::Profile(ID, NNS, Name);
1854
1855 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001856 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001857 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1858 if (T)
1859 return QualType(T, 0);
1860
1861 T = new (*this) TypenameType(NNS, Name, Canon);
1862 Types.push_back(T);
1863 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001864 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001865}
1866
Mike Stump1eb44332009-09-09 15:08:12 +00001867QualType
1868ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001869 const TemplateSpecializationType *TemplateId,
1870 QualType Canon) {
1871 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1872
1873 if (Canon.isNull()) {
1874 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1875 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1876 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1877 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00001878 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00001879 assert(CanonTemplateId &&
1880 "Canonical type must also be a template specialization type");
1881 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1882 }
1883 }
1884
1885 llvm::FoldingSetNodeID ID;
1886 TypenameType::Profile(ID, NNS, TemplateId);
1887
1888 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001889 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001890 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1891 if (T)
1892 return QualType(T, 0);
1893
1894 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1895 Types.push_back(T);
1896 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001897 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001898}
1899
John McCall7da24312009-09-05 00:15:47 +00001900QualType
1901ASTContext::getElaboratedType(QualType UnderlyingType,
1902 ElaboratedType::TagKind Tag) {
1903 llvm::FoldingSetNodeID ID;
1904 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001905
John McCall7da24312009-09-05 00:15:47 +00001906 void *InsertPos = 0;
1907 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1908 if (T)
1909 return QualType(T, 0);
1910
1911 QualType Canon = getCanonicalType(UnderlyingType);
1912
1913 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1914 Types.push_back(T);
1915 ElaboratedTypes.InsertNode(T, InsertPos);
1916 return QualType(T, 0);
1917}
1918
Chris Lattner88cb27a2008-04-07 04:56:42 +00001919/// CmpProtocolNames - Comparison predicate for sorting protocols
1920/// alphabetically.
1921static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1922 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001923 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001924}
1925
John McCall54e14c42009-10-22 22:37:11 +00001926static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols,
1927 unsigned NumProtocols) {
1928 if (NumProtocols == 0) return true;
1929
1930 for (unsigned i = 1; i != NumProtocols; ++i)
1931 if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
1932 return false;
1933 return true;
1934}
1935
1936static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
Chris Lattner88cb27a2008-04-07 04:56:42 +00001937 unsigned &NumProtocols) {
1938 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Chris Lattner88cb27a2008-04-07 04:56:42 +00001940 // Sort protocols, keyed by name.
1941 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1942
1943 // Remove duplicates.
1944 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1945 NumProtocols = ProtocolsEnd-Protocols;
1946}
1947
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001948/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1949/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001950QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001951 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001952 unsigned NumProtocols) {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001953 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001954 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001955
1956 void *InsertPos = 0;
1957 if (ObjCObjectPointerType *QT =
1958 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1959 return QualType(QT, 0);
1960
John McCall54e14c42009-10-22 22:37:11 +00001961 // Sort the protocol list alphabetically to canonicalize it.
1962 QualType Canonical;
1963 if (!InterfaceT.isCanonical() ||
1964 !areSortedAndUniqued(Protocols, NumProtocols)) {
1965 if (!areSortedAndUniqued(Protocols, NumProtocols)) {
1966 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
1967 unsigned UniqueCount = NumProtocols;
1968
1969 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
1970 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
1971
1972 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
1973 &Sorted[0], UniqueCount);
1974 } else {
1975 Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT),
1976 Protocols, NumProtocols);
1977 }
1978
1979 // Regenerate InsertPos.
1980 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1981 }
1982
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001983 // No Match;
John McCall6b304a02009-09-24 23:30:46 +00001984 ObjCObjectPointerType *QType = new (*this, TypeAlignment)
John McCall54e14c42009-10-22 22:37:11 +00001985 ObjCObjectPointerType(Canonical, InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001987 Types.push_back(QType);
1988 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1989 return QualType(QType, 0);
1990}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001991
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001992/// getObjCInterfaceType - Return the unique reference to the type for the
1993/// specified ObjC interface decl. The list of protocols is optional.
1994QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001995 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001996 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001997 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001999 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002000 if (ObjCInterfaceType *QT =
2001 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002002 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002003
John McCall54e14c42009-10-22 22:37:11 +00002004 // Sort the protocol list alphabetically to canonicalize it.
2005 QualType Canonical;
2006 if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) {
2007 llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols);
2008 std::copy(Protocols, Protocols + NumProtocols, Sorted.begin());
2009
2010 unsigned UniqueCount = NumProtocols;
2011 SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2012
2013 Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount);
2014
2015 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos);
2016 }
2017
John McCall6b304a02009-09-24 23:30:46 +00002018 ObjCInterfaceType *QType = new (*this, TypeAlignment)
John McCall54e14c42009-10-22 22:37:11 +00002019 ObjCInterfaceType(Canonical, const_cast<ObjCInterfaceDecl*>(Decl),
John McCall6b304a02009-09-24 23:30:46 +00002020 Protocols, NumProtocols);
John McCall54e14c42009-10-22 22:37:11 +00002021
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002022 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002023 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00002024 return QualType(QType, 0);
2025}
2026
Douglas Gregor72564e72009-02-26 23:50:07 +00002027/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2028/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002029/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002030/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002031/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002032QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002033 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002034 if (tofExpr->isTypeDependent()) {
2035 llvm::FoldingSetNodeID ID;
2036 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002037
Douglas Gregorb1975722009-07-30 23:18:24 +00002038 void *InsertPos = 0;
2039 DependentTypeOfExprType *Canon
2040 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2041 if (Canon) {
2042 // We already have a "canonical" version of an identical, dependent
2043 // typeof(expr) type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002044 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002045 QualType((TypeOfExprType*)Canon, 0));
2046 }
2047 else {
2048 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002049 Canon
2050 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregorb1975722009-07-30 23:18:24 +00002051 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2052 toe = Canon;
2053 }
2054 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002055 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall6b304a02009-09-24 23:30:46 +00002056 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregordd0257c2009-07-08 00:03:05 +00002057 }
Steve Naroff9752f252007-08-01 18:02:17 +00002058 Types.push_back(toe);
2059 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002060}
2061
Steve Naroff9752f252007-08-01 18:02:17 +00002062/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2063/// TypeOfType AST's. The only motivation to unique these nodes would be
2064/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002065/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002066/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002067QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002068 QualType Canonical = getCanonicalType(tofType);
John McCall6b304a02009-09-24 23:30:46 +00002069 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002070 Types.push_back(tot);
2071 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002072}
2073
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002074/// getDecltypeForExpr - Given an expr, will return the decltype for that
2075/// expression, according to the rules in C++0x [dcl.type.simple]p4
2076static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002077 if (e->isTypeDependent())
2078 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002079
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002080 // If e is an id expression or a class member access, decltype(e) is defined
2081 // as the type of the entity named by e.
2082 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2083 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2084 return VD->getType();
2085 }
2086 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2087 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2088 return FD->getType();
2089 }
2090 // If e is a function call or an invocation of an overloaded operator,
2091 // (parentheses around e are ignored), decltype(e) is defined as the
2092 // return type of that function.
2093 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2094 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002095
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002096 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002097
2098 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002099 // defined as T&, otherwise decltype(e) is defined as T.
2100 if (e->isLvalue(Context) == Expr::LV_Valid)
2101 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002103 return T;
2104}
2105
Anders Carlsson395b4752009-06-24 19:06:50 +00002106/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2107/// DecltypeType AST's. The only motivation to unique these nodes would be
2108/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002109/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002110/// on canonical type's (which are always unique).
2111QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002112 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002113 if (e->isTypeDependent()) {
2114 llvm::FoldingSetNodeID ID;
2115 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002116
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002117 void *InsertPos = 0;
2118 DependentDecltypeType *Canon
2119 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2120 if (Canon) {
2121 // We already have a "canonical" version of an equivalent, dependent
2122 // decltype type. Use that as our canonical type.
John McCall6b304a02009-09-24 23:30:46 +00002123 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002124 QualType((DecltypeType*)Canon, 0));
2125 }
2126 else {
2127 // Build a new, canonical typeof(expr) type.
John McCall6b304a02009-09-24 23:30:46 +00002128 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002129 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2130 dt = Canon;
2131 }
2132 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002133 QualType T = getDecltypeForExpr(e, *this);
John McCall6b304a02009-09-24 23:30:46 +00002134 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002135 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002136 Types.push_back(dt);
2137 return QualType(dt, 0);
2138}
2139
Reid Spencer5f016e22007-07-11 17:01:13 +00002140/// getTagDeclType - Return the unique reference to the type for the
2141/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002142QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002143 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002144 // FIXME: What is the design on getTagDeclType when it requires casting
2145 // away const? mutable?
2146 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002147}
2148
Mike Stump1eb44332009-09-09 15:08:12 +00002149/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2150/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2151/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002152QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002153 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002154}
2155
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002156/// getSignedWCharType - Return the type of "signed wchar_t".
2157/// Used when in C++, as a GCC extension.
2158QualType ASTContext::getSignedWCharType() const {
2159 // FIXME: derive from "Target" ?
2160 return WCharTy;
2161}
2162
2163/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2164/// Used when in C++, as a GCC extension.
2165QualType ASTContext::getUnsignedWCharType() const {
2166 // FIXME: derive from "Target" ?
2167 return UnsignedIntTy;
2168}
2169
Chris Lattner8b9023b2007-07-13 03:05:23 +00002170/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2171/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2172QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002173 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002174}
2175
Chris Lattnere6327742008-04-02 05:18:44 +00002176//===----------------------------------------------------------------------===//
2177// Type Operators
2178//===----------------------------------------------------------------------===//
2179
John McCall54e14c42009-10-22 22:37:11 +00002180CanQualType ASTContext::getCanonicalParamType(QualType T) {
2181 // Push qualifiers into arrays, and then discard any remaining
2182 // qualifiers.
2183 T = getCanonicalType(T);
2184 const Type *Ty = T.getTypePtr();
2185
2186 QualType Result;
2187 if (isa<ArrayType>(Ty)) {
2188 Result = getArrayDecayedType(QualType(Ty,0));
2189 } else if (isa<FunctionType>(Ty)) {
2190 Result = getPointerType(QualType(Ty, 0));
2191 } else {
2192 Result = QualType(Ty, 0);
2193 }
2194
2195 return CanQualType::CreateUnsafe(Result);
2196}
2197
Chris Lattner77c96472008-04-06 22:41:35 +00002198/// getCanonicalType - Return the canonical (structural) type corresponding to
2199/// the specified potentially non-canonical type. The non-canonical version
2200/// of a type may have many "decorated" versions of types. Decorators can
2201/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2202/// to be free of any of these, allowing two canonical types to be compared
2203/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002204CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002205 QualifierCollector Quals;
2206 const Type *Ptr = Quals.strip(T);
2207 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002208
John McCall0953e762009-09-24 19:53:00 +00002209 // The canonical internal type will be the canonical type *except*
2210 // that we push type qualifiers down through array types.
2211
2212 // If there are no new qualifiers to push down, stop here.
2213 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002214 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002215
John McCall0953e762009-09-24 19:53:00 +00002216 // If the type qualifiers are on an array type, get the canonical
2217 // type of the array with the qualifiers applied to the element
2218 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002219 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2220 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002221 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002222
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002223 // Get the canonical version of the element with the extra qualifiers on it.
2224 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002225 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002226 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002228 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002229 return CanQualType::CreateUnsafe(
2230 getConstantArrayType(NewEltTy, CAT->getSize(),
2231 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002232 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002233 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002234 return CanQualType::CreateUnsafe(
2235 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002236 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Douglas Gregor898574e2008-12-05 23:32:09 +00002238 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002239 return CanQualType::CreateUnsafe(
2240 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002241 DSAT->getSizeExpr() ?
2242 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002243 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002244 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002245 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002246
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002247 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002248 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002249 VAT->getSizeExpr() ?
2250 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002251 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002252 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002253 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002254}
2255
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002256TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2257 // If this template name refers to a template, the canonical
2258 // template name merely stores the template itself.
2259 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002260 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002261
Mike Stump1eb44332009-09-09 15:08:12 +00002262 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002263 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002264 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2265 OverloadedFunctionDecl *CanonOvl = 0;
2266 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2267 FEnd = Ovl->function_end();
2268 F != FEnd; ++F) {
2269 Decl *Canon = F->get()->getCanonicalDecl();
2270 if (CanonOvl || Canon != F->get()) {
2271 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002272 CanonOvl = OverloadedFunctionDecl::Create(*this,
2273 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002274 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002275
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002276 CanonOvl->addOverload(
2277 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2278 }
2279 }
Mike Stump1eb44332009-09-09 15:08:12 +00002280
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002281 return TemplateName(CanonOvl? CanonOvl : Ovl);
2282 }
Mike Stump1eb44332009-09-09 15:08:12 +00002283
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002284 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2285 assert(DTN && "Non-dependent template names must refer to template decls.");
2286 return DTN->CanonicalTemplateName;
2287}
2288
Mike Stump1eb44332009-09-09 15:08:12 +00002289TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002290ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2291 switch (Arg.getKind()) {
2292 case TemplateArgument::Null:
2293 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002294
Douglas Gregor1275ae02009-07-28 23:00:59 +00002295 case TemplateArgument::Expression:
2296 // FIXME: Build canonical expression?
2297 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002298
Douglas Gregor1275ae02009-07-28 23:00:59 +00002299 case TemplateArgument::Declaration:
2300 return TemplateArgument(SourceLocation(),
2301 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Douglas Gregor1275ae02009-07-28 23:00:59 +00002303 case TemplateArgument::Integral:
2304 return TemplateArgument(SourceLocation(),
2305 *Arg.getAsIntegral(),
2306 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Douglas Gregor1275ae02009-07-28 23:00:59 +00002308 case TemplateArgument::Type:
2309 return TemplateArgument(SourceLocation(),
2310 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Douglas Gregor1275ae02009-07-28 23:00:59 +00002312 case TemplateArgument::Pack: {
2313 // FIXME: Allocate in ASTContext
2314 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2315 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002316 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002317 AEnd = Arg.pack_end();
2318 A != AEnd; (void)++A, ++Idx)
2319 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Douglas Gregor1275ae02009-07-28 23:00:59 +00002321 TemplateArgument Result;
2322 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2323 return Result;
2324 }
2325 }
2326
2327 // Silence GCC warning
2328 assert(false && "Unhandled template argument kind");
2329 return TemplateArgument();
2330}
2331
Douglas Gregord57959a2009-03-27 23:10:48 +00002332NestedNameSpecifier *
2333ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002334 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002335 return 0;
2336
2337 switch (NNS->getKind()) {
2338 case NestedNameSpecifier::Identifier:
2339 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002340 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002341 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2342 NNS->getAsIdentifier());
2343
2344 case NestedNameSpecifier::Namespace:
2345 // A namespace is canonical; build a nested-name-specifier with
2346 // this namespace and no prefix.
2347 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2348
2349 case NestedNameSpecifier::TypeSpec:
2350 case NestedNameSpecifier::TypeSpecWithTemplate: {
2351 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002352 return NestedNameSpecifier::Create(*this, 0,
2353 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002354 T.getTypePtr());
2355 }
2356
2357 case NestedNameSpecifier::Global:
2358 // The global specifier is canonical and unique.
2359 return NNS;
2360 }
2361
2362 // Required to silence a GCC warning
2363 return 0;
2364}
2365
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002366
2367const ArrayType *ASTContext::getAsArrayType(QualType T) {
2368 // Handle the non-qualified case efficiently.
John McCall0953e762009-09-24 19:53:00 +00002369 if (!T.hasQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002370 // Handle the common positive case fast.
2371 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2372 return AT;
2373 }
Mike Stump1eb44332009-09-09 15:08:12 +00002374
John McCall0953e762009-09-24 19:53:00 +00002375 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002376 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002377 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002378 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002379
John McCall0953e762009-09-24 19:53:00 +00002380 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002381 // implements C99 6.7.3p8: "If the specification of an array type includes
2382 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002384 // If we get here, we either have type qualifiers on the type, or we have
2385 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002386 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002387
John McCall0953e762009-09-24 19:53:00 +00002388 QualifierCollector Qs;
2389 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002391 // If we have a simple case, just return now.
2392 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002393 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002394 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002395
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002396 // Otherwise, we have an array and we have qualifiers on it. Push the
2397 // qualifiers into the array element type and return a new array type.
2398 // Get the canonical version of the element with the extra qualifiers on it.
2399 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002400 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002401
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002402 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2403 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2404 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002405 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002406 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2407 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2408 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002409 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002410
Mike Stump1eb44332009-09-09 15:08:12 +00002411 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002412 = dyn_cast<DependentSizedArrayType>(ATy))
2413 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002414 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002415 DSAT->getSizeExpr() ?
2416 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002417 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002418 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002419 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002420
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002421 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002422 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002423 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002424 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002425 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002426 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002427 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002428}
2429
2430
Chris Lattnere6327742008-04-02 05:18:44 +00002431/// getArrayDecayedType - Return the properly qualified result of decaying the
2432/// specified array type to a pointer. This operation is non-trivial when
2433/// handling typedefs etc. The canonical type of "T" must be an array type,
2434/// this returns a pointer to a properly qualified element of the array.
2435///
2436/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2437QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002438 // Get the element type with 'getAsArrayType' so that we don't lose any
2439 // typedefs in the element type of the array. This also handles propagation
2440 // of type qualifiers from the array type into the element type if present
2441 // (C99 6.7.3p8).
2442 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2443 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002444
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002445 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002446
2447 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002448 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002449}
2450
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002451QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002452 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002453 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002454 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002455 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2456 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002457 } else {
John McCall0953e762009-09-24 19:53:00 +00002458 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002459 }
2460 }
2461}
2462
Anders Carlssonfbbce492009-09-25 01:23:32 +00002463QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2464 QualType ElemTy = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002465
Anders Carlssonfbbce492009-09-25 01:23:32 +00002466 if (const ArrayType *AT = getAsArrayType(ElemTy))
2467 return getBaseElementType(AT);
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Anders Carlsson6183a992008-12-21 03:44:36 +00002469 return ElemTy;
2470}
2471
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002472/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002473uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002474ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2475 uint64_t ElementCount = 1;
2476 do {
2477 ElementCount *= CA->getSize().getZExtValue();
2478 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2479 } while (CA);
2480 return ElementCount;
2481}
2482
Reid Spencer5f016e22007-07-11 17:01:13 +00002483/// getFloatingRank - Return a relative rank for floating point types.
2484/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002485static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002486 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002487 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002488
John McCall183700f2009-09-21 23:43:11 +00002489 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2490 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002491 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002492 case BuiltinType::Float: return FloatRank;
2493 case BuiltinType::Double: return DoubleRank;
2494 case BuiltinType::LongDouble: return LongDoubleRank;
2495 }
2496}
2497
Mike Stump1eb44332009-09-09 15:08:12 +00002498/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2499/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002500/// 'typeDomain' is a real floating point or complex type.
2501/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002502QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2503 QualType Domain) const {
2504 FloatingRank EltRank = getFloatingRank(Size);
2505 if (Domain->isComplexType()) {
2506 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002507 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002508 case FloatRank: return FloatComplexTy;
2509 case DoubleRank: return DoubleComplexTy;
2510 case LongDoubleRank: return LongDoubleComplexTy;
2511 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002512 }
Chris Lattner1361b112008-04-06 23:58:54 +00002513
2514 assert(Domain->isRealFloatingType() && "Unknown domain!");
2515 switch (EltRank) {
2516 default: assert(0 && "getFloatingRank(): illegal value for rank");
2517 case FloatRank: return FloatTy;
2518 case DoubleRank: return DoubleTy;
2519 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002520 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002521}
2522
Chris Lattner7cfeb082008-04-06 23:55:33 +00002523/// getFloatingTypeOrder - Compare the rank of the two specified floating
2524/// point types, ignoring the domain of the type (i.e. 'double' ==
2525/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002526/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002527int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2528 FloatingRank LHSR = getFloatingRank(LHS);
2529 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002530
Chris Lattnera75cea32008-04-06 23:38:49 +00002531 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002532 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002533 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002534 return 1;
2535 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002536}
2537
Chris Lattnerf52ab252008-04-06 22:59:24 +00002538/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2539/// routine will assert if passed a built-in type that isn't an integer or enum,
2540/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002541unsigned ASTContext::getIntegerRank(Type *T) {
John McCall467b27b2009-10-22 20:10:53 +00002542 assert(T->isCanonicalUnqualified() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002543 if (EnumType* ET = dyn_cast<EnumType>(T))
2544 T = ET->getDecl()->getIntegerType().getTypePtr();
2545
Eli Friedmana3426752009-07-05 23:44:27 +00002546 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2547 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2548
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002549 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2550 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2551
2552 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2553 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2554
Eli Friedmanf98aba32009-02-13 02:31:07 +00002555 // There are two things which impact the integer rank: the width, and
2556 // the ordering of builtins. The builtin ordering is encoded in the
2557 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002558 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002559 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002560
Chris Lattnerf52ab252008-04-06 22:59:24 +00002561 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002562 default: assert(0 && "getIntegerRank(): not a built-in integer");
2563 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002564 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002565 case BuiltinType::Char_S:
2566 case BuiltinType::Char_U:
2567 case BuiltinType::SChar:
2568 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002569 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002570 case BuiltinType::Short:
2571 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002572 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002573 case BuiltinType::Int:
2574 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002575 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002576 case BuiltinType::Long:
2577 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002578 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002579 case BuiltinType::LongLong:
2580 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002581 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002582 case BuiltinType::Int128:
2583 case BuiltinType::UInt128:
2584 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002585 }
2586}
2587
Eli Friedman04e83572009-08-20 04:21:42 +00002588/// \brief Whether this is a promotable bitfield reference according
2589/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2590///
2591/// \returns the type this bit-field will promote to, or NULL if no
2592/// promotion occurs.
2593QualType ASTContext::isPromotableBitField(Expr *E) {
2594 FieldDecl *Field = E->getBitField();
2595 if (!Field)
2596 return QualType();
2597
2598 QualType FT = Field->getType();
2599
2600 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2601 uint64_t BitWidth = BitWidthAP.getZExtValue();
2602 uint64_t IntSize = getTypeSize(IntTy);
2603 // GCC extension compatibility: if the bit-field size is less than or equal
2604 // to the size of int, it gets promoted no matter what its type is.
2605 // For instance, unsigned long bf : 4 gets promoted to signed int.
2606 if (BitWidth < IntSize)
2607 return IntTy;
2608
2609 if (BitWidth == IntSize)
2610 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2611
2612 // Types bigger than int are not subject to promotions, and therefore act
2613 // like the base type.
2614 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2615 // is ridiculous.
2616 return QualType();
2617}
2618
Eli Friedmana95d7572009-08-19 07:44:53 +00002619/// getPromotedIntegerType - Returns the type that Promotable will
2620/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2621/// integer type.
2622QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2623 assert(!Promotable.isNull());
2624 assert(Promotable->isPromotableIntegerType());
2625 if (Promotable->isSignedIntegerType())
2626 return IntTy;
2627 uint64_t PromotableSize = getTypeSize(Promotable);
2628 uint64_t IntSize = getTypeSize(IntTy);
2629 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2630 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2631}
2632
Mike Stump1eb44332009-09-09 15:08:12 +00002633/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002634/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002635/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002636int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002637 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2638 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002639 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002640
Chris Lattnerf52ab252008-04-06 22:59:24 +00002641 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2642 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002643
Chris Lattner7cfeb082008-04-06 23:55:33 +00002644 unsigned LHSRank = getIntegerRank(LHSC);
2645 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002646
Chris Lattner7cfeb082008-04-06 23:55:33 +00002647 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2648 if (LHSRank == RHSRank) return 0;
2649 return LHSRank > RHSRank ? 1 : -1;
2650 }
Mike Stump1eb44332009-09-09 15:08:12 +00002651
Chris Lattner7cfeb082008-04-06 23:55:33 +00002652 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2653 if (LHSUnsigned) {
2654 // If the unsigned [LHS] type is larger, return it.
2655 if (LHSRank >= RHSRank)
2656 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002657
Chris Lattner7cfeb082008-04-06 23:55:33 +00002658 // If the signed type can represent all values of the unsigned type, it
2659 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002660 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002661 return -1;
2662 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002663
Chris Lattner7cfeb082008-04-06 23:55:33 +00002664 // If the unsigned [RHS] type is larger, return it.
2665 if (RHSRank >= LHSRank)
2666 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002667
Chris Lattner7cfeb082008-04-06 23:55:33 +00002668 // If the signed type can represent all values of the unsigned type, it
2669 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002670 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002671 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002672}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002673
Mike Stump1eb44332009-09-09 15:08:12 +00002674// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002675QualType ASTContext::getCFConstantStringType() {
2676 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002677 CFConstantStringTypeDecl =
2678 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002679 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002680 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002681
Anders Carlsson71993dd2007-08-17 05:31:46 +00002682 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002683 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002684 // int flags;
2685 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002686 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002687 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002688 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002689 FieldTypes[3] = LongTy;
2690
Anders Carlsson71993dd2007-08-17 05:31:46 +00002691 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002692 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002693 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002694 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002695 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002696 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002697 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002698 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002699 }
2700
2701 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002702 }
Mike Stump1eb44332009-09-09 15:08:12 +00002703
Anders Carlsson71993dd2007-08-17 05:31:46 +00002704 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002705}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002706
Douglas Gregor319ac892009-04-23 22:29:11 +00002707void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002708 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002709 assert(Rec && "Invalid CFConstantStringType");
2710 CFConstantStringTypeDecl = Rec->getDecl();
2711}
2712
Mike Stump1eb44332009-09-09 15:08:12 +00002713QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002714 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002715 ObjCFastEnumerationStateTypeDecl =
2716 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2717 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002719 QualType FieldTypes[] = {
2720 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002721 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002722 getPointerType(UnsignedLongTy),
2723 getConstantArrayType(UnsignedLongTy,
2724 llvm::APInt(32, 5), ArrayType::Normal, 0)
2725 };
Mike Stump1eb44332009-09-09 15:08:12 +00002726
Douglas Gregor44b43212008-12-11 16:49:14 +00002727 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002728 FieldDecl *Field = FieldDecl::Create(*this,
2729 ObjCFastEnumerationStateTypeDecl,
2730 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002731 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002732 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002733 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002734 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002735 }
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Douglas Gregor44b43212008-12-11 16:49:14 +00002737 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002738 }
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002740 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2741}
2742
Mike Stumpadaaad32009-10-20 02:12:22 +00002743QualType ASTContext::getBlockDescriptorType() {
2744 if (BlockDescriptorType)
2745 return getTagDeclType(BlockDescriptorType);
2746
2747 RecordDecl *T;
2748 // FIXME: Needs the FlagAppleBlock bit.
2749 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2750 &Idents.get("__block_descriptor"));
2751
2752 QualType FieldTypes[] = {
2753 UnsignedLongTy,
2754 UnsignedLongTy,
2755 };
2756
2757 const char *FieldNames[] = {
2758 "reserved",
Mike Stump083c25e2009-10-22 00:49:09 +00002759 "Size"
Mike Stumpadaaad32009-10-20 02:12:22 +00002760 };
2761
2762 for (size_t i = 0; i < 2; ++i) {
2763 FieldDecl *Field = FieldDecl::Create(*this,
2764 T,
2765 SourceLocation(),
2766 &Idents.get(FieldNames[i]),
2767 FieldTypes[i], /*DInfo=*/0,
2768 /*BitWidth=*/0,
2769 /*Mutable=*/false);
2770 T->addDecl(Field);
2771 }
2772
2773 T->completeDefinition(*this);
2774
2775 BlockDescriptorType = T;
2776
2777 return getTagDeclType(BlockDescriptorType);
2778}
2779
2780void ASTContext::setBlockDescriptorType(QualType T) {
2781 const RecordType *Rec = T->getAs<RecordType>();
2782 assert(Rec && "Invalid BlockDescriptorType");
2783 BlockDescriptorType = Rec->getDecl();
2784}
2785
Mike Stump083c25e2009-10-22 00:49:09 +00002786QualType ASTContext::getBlockDescriptorExtendedType() {
2787 if (BlockDescriptorExtendedType)
2788 return getTagDeclType(BlockDescriptorExtendedType);
2789
2790 RecordDecl *T;
2791 // FIXME: Needs the FlagAppleBlock bit.
2792 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2793 &Idents.get("__block_descriptor_withcopydispose"));
2794
2795 QualType FieldTypes[] = {
2796 UnsignedLongTy,
2797 UnsignedLongTy,
2798 getPointerType(VoidPtrTy),
2799 getPointerType(VoidPtrTy)
2800 };
2801
2802 const char *FieldNames[] = {
2803 "reserved",
2804 "Size",
2805 "CopyFuncPtr",
2806 "DestroyFuncPtr"
2807 };
2808
2809 for (size_t i = 0; i < 4; ++i) {
2810 FieldDecl *Field = FieldDecl::Create(*this,
2811 T,
2812 SourceLocation(),
2813 &Idents.get(FieldNames[i]),
2814 FieldTypes[i], /*DInfo=*/0,
2815 /*BitWidth=*/0,
2816 /*Mutable=*/false);
2817 T->addDecl(Field);
2818 }
2819
2820 T->completeDefinition(*this);
2821
2822 BlockDescriptorExtendedType = T;
2823
2824 return getTagDeclType(BlockDescriptorExtendedType);
2825}
2826
2827void ASTContext::setBlockDescriptorExtendedType(QualType T) {
2828 const RecordType *Rec = T->getAs<RecordType>();
2829 assert(Rec && "Invalid BlockDescriptorType");
2830 BlockDescriptorExtendedType = Rec->getDecl();
2831}
2832
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002833bool ASTContext::BlockRequiresCopying(QualType Ty) {
2834 if (Ty->isBlockPointerType())
2835 return true;
2836 if (isObjCNSObjectType(Ty))
2837 return true;
2838 if (Ty->isObjCObjectPointerType())
2839 return true;
2840 return false;
2841}
2842
2843QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) {
2844 // type = struct __Block_byref_1_X {
Mike Stumpea26cb52009-10-21 03:49:08 +00002845 // void *__isa;
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002846 // struct __Block_byref_1_X *__forwarding;
Mike Stumpea26cb52009-10-21 03:49:08 +00002847 // unsigned int __flags;
2848 // unsigned int __size;
Mike Stump38e16272009-10-21 22:01:24 +00002849 // void *__copy_helper; // as needed
2850 // void *__destroy_help // as needed
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002851 // int X;
Mike Stumpea26cb52009-10-21 03:49:08 +00002852 // } *
2853
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002854 bool HasCopyAndDispose = BlockRequiresCopying(Ty);
2855
2856 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00002857 static unsigned int UniqueBlockByRefTypeID = 0;
2858 char * Name =
2859 (char*)alloca(strlen("__Block_byref_") + 10 + 1 + strlen(DeclName) + 1);
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002860 sprintf(Name, "__Block_byref_%d_%s", ++UniqueBlockByRefTypeID, DeclName);
2861 RecordDecl *T;
2862 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2863 &Idents.get(Name));
2864 T->startDefinition();
2865 QualType Int32Ty = IntTy;
2866 assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
2867 QualType FieldTypes[] = {
2868 getPointerType(VoidPtrTy),
2869 getPointerType(getTagDeclType(T)),
2870 Int32Ty,
2871 Int32Ty,
2872 getPointerType(VoidPtrTy),
2873 getPointerType(VoidPtrTy),
2874 Ty
2875 };
2876
2877 const char *FieldNames[] = {
2878 "__isa",
2879 "__forwarding",
2880 "__flags",
2881 "__size",
2882 "__copy_helper",
2883 "__destroy_helper",
2884 DeclName,
2885 };
2886
2887 for (size_t i = 0; i < 7; ++i) {
2888 if (!HasCopyAndDispose && i >=4 && i <= 5)
2889 continue;
2890 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
2891 &Idents.get(FieldNames[i]),
2892 FieldTypes[i], /*DInfo=*/0,
2893 /*BitWidth=*/0, /*Mutable=*/false);
2894 T->addDecl(Field);
2895 }
2896
2897 T->completeDefinition(*this);
2898
2899 return getPointerType(getTagDeclType(T));
Mike Stumpea26cb52009-10-21 03:49:08 +00002900}
2901
2902
2903QualType ASTContext::getBlockParmType(
Mike Stump083c25e2009-10-22 00:49:09 +00002904 bool BlockHasCopyDispose,
Mike Stumpea26cb52009-10-21 03:49:08 +00002905 llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
Mike Stumpadaaad32009-10-20 02:12:22 +00002906 // FIXME: Move up
Fariborz Jahanian4d0d85c2009-10-24 00:16:42 +00002907 static unsigned int UniqueBlockParmTypeID = 0;
2908 char * Name =
2909 (char*)alloca(strlen("__block_literal_") + 10 + 1);
Mike Stumpadaaad32009-10-20 02:12:22 +00002910 sprintf(Name, "__block_literal_%u", ++UniqueBlockParmTypeID);
2911 RecordDecl *T;
2912 T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2913 &Idents.get(Name));
Mike Stumpadaaad32009-10-20 02:12:22 +00002914 QualType FieldTypes[] = {
2915 getPointerType(VoidPtrTy),
2916 IntTy,
2917 IntTy,
2918 getPointerType(VoidPtrTy),
Mike Stump083c25e2009-10-22 00:49:09 +00002919 (BlockHasCopyDispose ?
2920 getPointerType(getBlockDescriptorExtendedType()) :
2921 getPointerType(getBlockDescriptorType()))
Mike Stumpadaaad32009-10-20 02:12:22 +00002922 };
2923
2924 const char *FieldNames[] = {
2925 "__isa",
2926 "__flags",
2927 "__reserved",
2928 "__FuncPtr",
2929 "__descriptor"
2930 };
2931
2932 for (size_t i = 0; i < 5; ++i) {
Mike Stumpea26cb52009-10-21 03:49:08 +00002933 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
Mike Stumpadaaad32009-10-20 02:12:22 +00002934 &Idents.get(FieldNames[i]),
2935 FieldTypes[i], /*DInfo=*/0,
Mike Stumpea26cb52009-10-21 03:49:08 +00002936 /*BitWidth=*/0, /*Mutable=*/false);
2937 T->addDecl(Field);
2938 }
2939
2940 for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) {
2941 const Expr *E = BlockDeclRefDecls[i];
2942 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
2943 clang::IdentifierInfo *Name = 0;
2944 if (BDRE) {
2945 const ValueDecl *D = BDRE->getDecl();
2946 Name = &Idents.get(D->getName());
2947 }
2948 QualType FieldType = E->getType();
2949
2950 if (BDRE && BDRE->isByRef())
Mike Stumpaf7b44d2009-10-21 18:16:27 +00002951 FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(),
2952 FieldType);
Mike Stumpea26cb52009-10-21 03:49:08 +00002953
2954 FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
2955 Name, FieldType, /*DInfo=*/0,
2956 /*BitWidth=*/0, /*Mutable=*/false);
Mike Stumpadaaad32009-10-20 02:12:22 +00002957 T->addDecl(Field);
2958 }
2959
2960 T->completeDefinition(*this);
Mike Stumpea26cb52009-10-21 03:49:08 +00002961
2962 return getPointerType(getTagDeclType(T));
Mike Stumpadaaad32009-10-20 02:12:22 +00002963}
2964
Douglas Gregor319ac892009-04-23 22:29:11 +00002965void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002966 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002967 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2968 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2969}
2970
Anders Carlssone8c49532007-10-29 06:33:42 +00002971// This returns true if a type has been typedefed to BOOL:
2972// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002973static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002974 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002975 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2976 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002977
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002978 return false;
2979}
2980
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002981/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002982/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002983int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002984 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002985
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002986 // Make all integer and enum types at least as large as an int
2987 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002988 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002989 // Treat arrays as pointers, since that's how they're passed in.
2990 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002991 sz = getTypeSize(VoidPtrTy);
2992 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002993}
2994
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002995/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002996/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002997void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002998 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002999 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003000 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003001 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003002 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003003 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003004 // Compute size of all parameters.
3005 // Start with computing size of a pointer in number of bytes.
3006 // FIXME: There might(should) be a better way of doing this computation!
3007 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00003008 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003009 // The first two arguments (self and _cmd) are pointers; account for
3010 // their size.
3011 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003012 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3013 E = Decl->param_end(); PI != E; ++PI) {
3014 QualType PType = (*PI)->getType();
3015 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003016 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003017 ParmOffset += sz;
3018 }
3019 S += llvm::utostr(ParmOffset);
3020 S += "@0:";
3021 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003023 // Argument types.
3024 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00003025 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
3026 E = Decl->param_end(); PI != E; ++PI) {
3027 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00003028 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003029 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00003030 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3031 // Use array's original type only if it has known number of
3032 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00003033 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00003034 PType = PVDecl->getType();
3035 } else if (PType->isFunctionType())
3036 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003037 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003038 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00003039 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003040 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003041 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003042 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00003043 }
3044}
3045
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003046/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003047/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003048/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3049/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00003050/// Property attributes are stored as a comma-delimited C string. The simple
3051/// attributes readonly and bycopy are encoded as single characters. The
3052/// parametrized attributes, getter=name, setter=name, and ivar=name, are
3053/// encoded as single characters, followed by an identifier. Property types
3054/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00003055/// these attributes are defined by the following enumeration:
3056/// @code
3057/// enum PropertyAttributes {
3058/// kPropertyReadOnly = 'R', // property is read-only.
3059/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
3060/// kPropertyByref = '&', // property is a reference to the value last assigned
3061/// kPropertyDynamic = 'D', // property is dynamic
3062/// kPropertyGetter = 'G', // followed by getter selector name
3063/// kPropertySetter = 'S', // followed by setter selector name
3064/// kPropertyInstanceVariable = 'V' // followed by instance variable name
3065/// kPropertyType = 't' // followed by old-style type encoding.
3066/// kPropertyWeak = 'W' // 'weak' property
3067/// kPropertyStrong = 'P' // property GC'able
3068/// kPropertyNonAtomic = 'N' // property non-atomic
3069/// };
3070/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00003071void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003072 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00003073 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003074 // Collect information from the property implementation decl(s).
3075 bool Dynamic = false;
3076 ObjCPropertyImplDecl *SynthesizePID = 0;
3077
3078 // FIXME: Duplicated code due to poor abstraction.
3079 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00003080 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003081 dyn_cast<ObjCCategoryImplDecl>(Container)) {
3082 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003083 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003084 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003085 ObjCPropertyImplDecl *PID = *i;
3086 if (PID->getPropertyDecl() == PD) {
3087 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3088 Dynamic = true;
3089 } else {
3090 SynthesizePID = PID;
3091 }
3092 }
3093 }
3094 } else {
Chris Lattner61710852008-10-05 17:34:18 +00003095 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003096 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003097 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00003098 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003099 ObjCPropertyImplDecl *PID = *i;
3100 if (PID->getPropertyDecl() == PD) {
3101 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
3102 Dynamic = true;
3103 } else {
3104 SynthesizePID = PID;
3105 }
3106 }
Mike Stump1eb44332009-09-09 15:08:12 +00003107 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003108 }
3109 }
3110
3111 // FIXME: This is not very efficient.
3112 S = "T";
3113
3114 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003115 // GCC has some special rules regarding encoding of properties which
3116 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00003117 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003118 true /* outermost type */,
3119 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003120
3121 if (PD->isReadOnly()) {
3122 S += ",R";
3123 } else {
3124 switch (PD->getSetterKind()) {
3125 case ObjCPropertyDecl::Assign: break;
3126 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003127 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003128 }
3129 }
3130
3131 // It really isn't clear at all what this means, since properties
3132 // are "dynamic by default".
3133 if (Dynamic)
3134 S += ",D";
3135
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003136 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
3137 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00003138
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003139 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
3140 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003141 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003142 }
3143
3144 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
3145 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00003146 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003147 }
3148
3149 if (SynthesizePID) {
3150 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
3151 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00003152 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003153 }
3154
3155 // FIXME: OBJCGC: weak & strong
3156}
3157
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003158/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00003159/// Another legacy compatibility encoding: 32-bit longs are encoded as
3160/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003161/// 'i' or 'I' instead if encoding a struct field, or a pointer!
3162///
3163void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00003164 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00003165 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003166 if (BT->getKind() == BuiltinType::ULong &&
3167 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003168 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00003169 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00003170 if (BT->getKind() == BuiltinType::Long &&
3171 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003172 PointeeTy = IntTy;
3173 }
3174 }
3175}
3176
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003177void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003178 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003179 // We follow the behavior of gcc, expanding structures which are
3180 // directly pointed to, and expanding embedded structures. Note that
3181 // these rules are sufficient to prevent recursive encoding of the
3182 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00003183 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00003184 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003185}
3186
Mike Stump1eb44332009-09-09 15:08:12 +00003187static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003188 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003189 const Expr *E = FD->getBitWidth();
3190 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
3191 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00003192 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003193 S += 'b';
3194 S += llvm::utostr(N);
3195}
3196
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003197// FIXME: Use SmallString for accumulating string.
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003198void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
3199 bool ExpandPointedToStructures,
3200 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00003201 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00003202 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003203 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00003204 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003205 if (FD && FD->isBitField())
3206 return EncodeBitField(this, S, FD);
3207 char encoding;
3208 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003209 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003210 case BuiltinType::Void: encoding = 'v'; break;
3211 case BuiltinType::Bool: encoding = 'B'; break;
3212 case BuiltinType::Char_U:
3213 case BuiltinType::UChar: encoding = 'C'; break;
3214 case BuiltinType::UShort: encoding = 'S'; break;
3215 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003216 case BuiltinType::ULong:
3217 encoding =
3218 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00003219 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003220 case BuiltinType::UInt128: encoding = 'T'; break;
3221 case BuiltinType::ULongLong: encoding = 'Q'; break;
3222 case BuiltinType::Char_S:
3223 case BuiltinType::SChar: encoding = 'c'; break;
3224 case BuiltinType::Short: encoding = 's'; break;
3225 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00003226 case BuiltinType::Long:
3227 encoding =
3228 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003229 break;
3230 case BuiltinType::LongLong: encoding = 'q'; break;
3231 case BuiltinType::Int128: encoding = 't'; break;
3232 case BuiltinType::Float: encoding = 'f'; break;
3233 case BuiltinType::Double: encoding = 'd'; break;
3234 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003235 }
Mike Stump1eb44332009-09-09 15:08:12 +00003236
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003237 S += encoding;
3238 return;
3239 }
Mike Stump1eb44332009-09-09 15:08:12 +00003240
John McCall183700f2009-09-21 23:43:11 +00003241 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003242 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00003243 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00003244 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003245 return;
3246 }
Mike Stump1eb44332009-09-09 15:08:12 +00003247
Ted Kremenek6217b802009-07-29 21:53:49 +00003248 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003249 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003250 bool isReadOnly = false;
3251 // For historical/compatibility reasons, the read-only qualifier of the
3252 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3253 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003254 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003255 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003256 if (OutermostType && T.isConstQualified()) {
3257 isReadOnly = true;
3258 S += 'r';
3259 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003260 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003261 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003262 while (P->getAs<PointerType>())
3263 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003264 if (P.isConstQualified()) {
3265 isReadOnly = true;
3266 S += 'r';
3267 }
3268 }
3269 if (isReadOnly) {
3270 // Another legacy compatibility encoding. Some ObjC qualifier and type
3271 // combinations need to be rearranged.
3272 // Rewrite "in const" from "nr" to "rn"
3273 const char * s = S.c_str();
3274 int len = S.length();
3275 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3276 std::string replace = "rn";
3277 S.replace(S.end()-2, S.end(), replace);
3278 }
3279 }
Steve Naroff14108da2009-07-10 23:34:53 +00003280 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00003281 S += ':';
3282 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00003283 }
Mike Stump1eb44332009-09-09 15:08:12 +00003284
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003285 if (PointeeTy->isCharType()) {
3286 // char pointer types should be encoded as '*' unless it is a
3287 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003288 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003289 S += '*';
3290 return;
3291 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003292 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003293 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3294 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3295 S += '#';
3296 return;
3297 }
3298 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3299 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3300 S += '@';
3301 return;
3302 }
3303 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003304 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003305 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003306 getLegacyIntegralTypeEncoding(PointeeTy);
3307
Mike Stump1eb44332009-09-09 15:08:12 +00003308 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003309 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003310 return;
3311 }
Mike Stump1eb44332009-09-09 15:08:12 +00003312
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003313 if (const ArrayType *AT =
3314 // Ignore type qualifiers etc.
3315 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003316 if (isa<IncompleteArrayType>(AT)) {
3317 // Incomplete arrays are encoded as a pointer to the array element.
3318 S += '^';
3319
Mike Stump1eb44332009-09-09 15:08:12 +00003320 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003321 false, ExpandStructures, FD);
3322 } else {
3323 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003324
Anders Carlsson559a8332009-02-22 01:38:57 +00003325 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3326 S += llvm::utostr(CAT->getSize().getZExtValue());
3327 else {
3328 //Variable length arrays are encoded as a regular array with 0 elements.
3329 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3330 S += '0';
3331 }
Mike Stump1eb44332009-09-09 15:08:12 +00003332
3333 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003334 false, ExpandStructures, FD);
3335 S += ']';
3336 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003337 return;
3338 }
Mike Stump1eb44332009-09-09 15:08:12 +00003339
John McCall183700f2009-09-21 23:43:11 +00003340 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003341 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003342 return;
3343 }
Mike Stump1eb44332009-09-09 15:08:12 +00003344
Ted Kremenek6217b802009-07-29 21:53:49 +00003345 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003346 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003347 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003348 // Anonymous structures print as '?'
3349 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3350 S += II->getName();
3351 } else {
3352 S += '?';
3353 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003354 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003355 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003356 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3357 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003358 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003359 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003360 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003361 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003362 S += '"';
3363 }
Mike Stump1eb44332009-09-09 15:08:12 +00003364
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003365 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003366 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003367 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003368 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003369 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003370 QualType qt = Field->getType();
3371 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003372 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003373 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003374 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003375 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003376 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003377 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003378 return;
3379 }
Mike Stump1eb44332009-09-09 15:08:12 +00003380
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003381 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003382 if (FD && FD->isBitField())
3383 EncodeBitField(this, S, FD);
3384 else
3385 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003386 return;
3387 }
Mike Stump1eb44332009-09-09 15:08:12 +00003388
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003389 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003390 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003391 return;
3392 }
Mike Stump1eb44332009-09-09 15:08:12 +00003393
John McCall0953e762009-09-24 19:53:00 +00003394 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003395 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003396 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003397 S += '{';
3398 const IdentifierInfo *II = OI->getIdentifier();
3399 S += II->getName();
3400 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003401 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003402 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003403 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003404 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003405 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003406 RecFields[i]);
3407 else
Mike Stump1eb44332009-09-09 15:08:12 +00003408 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003409 FD);
3410 }
3411 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003412 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003413 }
Mike Stump1eb44332009-09-09 15:08:12 +00003414
John McCall183700f2009-09-21 23:43:11 +00003415 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003416 if (OPT->isObjCIdType()) {
3417 S += '@';
3418 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003419 }
Mike Stump1eb44332009-09-09 15:08:12 +00003420
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003421 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003422 S += '#';
3423 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003424 }
Mike Stump1eb44332009-09-09 15:08:12 +00003425
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003426 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003427 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003428 ExpandPointedToStructures,
3429 ExpandStructures, FD);
3430 if (FD || EncodingProperty) {
3431 // Note that we do extended encoding of protocol qualifer list
3432 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003433 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003434 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3435 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003436 S += '<';
3437 S += (*I)->getNameAsString();
3438 S += '>';
3439 }
3440 S += '"';
3441 }
3442 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003443 }
Mike Stump1eb44332009-09-09 15:08:12 +00003444
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003445 QualType PointeeTy = OPT->getPointeeType();
3446 if (!EncodingProperty &&
3447 isa<TypedefType>(PointeeTy.getTypePtr())) {
3448 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003449 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003450 // {...};
3451 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003452 getObjCEncodingForTypeImpl(PointeeTy, S,
3453 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003454 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003455 return;
3456 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003457
3458 S += '@';
3459 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003460 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003461 S += OPT->getInterfaceDecl()->getNameAsCString();
3462 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3463 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003464 S += '<';
3465 S += (*I)->getNameAsString();
3466 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003467 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003468 S += '"';
3469 }
3470 return;
3471 }
Mike Stump1eb44332009-09-09 15:08:12 +00003472
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003473 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003474}
3475
Mike Stump1eb44332009-09-09 15:08:12 +00003476void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003477 std::string& S) const {
3478 if (QT & Decl::OBJC_TQ_In)
3479 S += 'n';
3480 if (QT & Decl::OBJC_TQ_Inout)
3481 S += 'N';
3482 if (QT & Decl::OBJC_TQ_Out)
3483 S += 'o';
3484 if (QT & Decl::OBJC_TQ_Bycopy)
3485 S += 'O';
3486 if (QT & Decl::OBJC_TQ_Byref)
3487 S += 'R';
3488 if (QT & Decl::OBJC_TQ_Oneway)
3489 S += 'V';
3490}
3491
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003492void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003493 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003494
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003495 BuiltinVaListType = T;
3496}
3497
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003498void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003499 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003500}
3501
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003502void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003503 ObjCSelType = T;
3504
John McCall183700f2009-09-21 23:43:11 +00003505 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003506 if (!TT)
3507 return;
3508 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003509
3510 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003511 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003512 if (!ptr)
3513 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003514 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003515 if (!rec)
3516 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003517 SelStructType = rec;
3518}
3519
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003520void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003521 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003522}
3523
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003524void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003525 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003526}
3527
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003528void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003529 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003530 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003531
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003532 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003533}
3534
Douglas Gregor7532dc62009-03-30 22:58:21 +00003535/// \brief Retrieve the template name that represents a qualified
3536/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003537TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003538 bool TemplateKeyword,
3539 TemplateDecl *Template) {
3540 llvm::FoldingSetNodeID ID;
3541 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3542
3543 void *InsertPos = 0;
3544 QualifiedTemplateName *QTN =
3545 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3546 if (!QTN) {
3547 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3548 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3549 }
3550
3551 return TemplateName(QTN);
3552}
3553
Douglas Gregord99cbe62009-07-29 18:26:50 +00003554/// \brief Retrieve the template name that represents a qualified
3555/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003556TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003557 bool TemplateKeyword,
3558 OverloadedFunctionDecl *Template) {
3559 llvm::FoldingSetNodeID ID;
3560 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003561
Douglas Gregord99cbe62009-07-29 18:26:50 +00003562 void *InsertPos = 0;
3563 QualifiedTemplateName *QTN =
3564 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3565 if (!QTN) {
3566 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3567 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3568 }
Mike Stump1eb44332009-09-09 15:08:12 +00003569
Douglas Gregord99cbe62009-07-29 18:26:50 +00003570 return TemplateName(QTN);
3571}
3572
Douglas Gregor7532dc62009-03-30 22:58:21 +00003573/// \brief Retrieve the template name that represents a dependent
3574/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003575TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003576 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003577 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003578 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003579
3580 llvm::FoldingSetNodeID ID;
3581 DependentTemplateName::Profile(ID, NNS, Name);
3582
3583 void *InsertPos = 0;
3584 DependentTemplateName *QTN =
3585 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3586
3587 if (QTN)
3588 return TemplateName(QTN);
3589
3590 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3591 if (CanonNNS == NNS) {
3592 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3593 } else {
3594 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3595 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3596 }
3597
3598 DependentTemplateNames.InsertNode(QTN, InsertPos);
3599 return TemplateName(QTN);
3600}
3601
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003602/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003603/// TargetInfo, produce the corresponding type. The unsigned @p Type
3604/// is actually a value of type @c TargetInfo::IntType.
John McCalle27ec8a2009-10-23 23:03:21 +00003605CanQualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003606 switch (Type) {
John McCalle27ec8a2009-10-23 23:03:21 +00003607 case TargetInfo::NoInt: return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003608 case TargetInfo::SignedShort: return ShortTy;
3609 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3610 case TargetInfo::SignedInt: return IntTy;
3611 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3612 case TargetInfo::SignedLong: return LongTy;
3613 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3614 case TargetInfo::SignedLongLong: return LongLongTy;
3615 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3616 }
3617
3618 assert(false && "Unhandled TargetInfo::IntType value");
John McCalle27ec8a2009-10-23 23:03:21 +00003619 return CanQualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003620}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003621
3622//===----------------------------------------------------------------------===//
3623// Type Predicates.
3624//===----------------------------------------------------------------------===//
3625
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003626/// isObjCNSObjectType - Return true if this is an NSObject object using
3627/// NSObject attribute on a c-style pointer type.
3628/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003629/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003630///
3631bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3632 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3633 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003634 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003635 return true;
3636 }
Mike Stump1eb44332009-09-09 15:08:12 +00003637 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003638}
3639
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003640/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3641/// garbage collection attribute.
3642///
John McCall0953e762009-09-24 19:53:00 +00003643Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3644 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003645 if (getLangOptions().ObjC1 &&
3646 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003647 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003648 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003649 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003650 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003651 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003652 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003653 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003654 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003655 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003656 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003657 // Non-pointers have none gc'able attribute regardless of the attribute
3658 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003659 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003660 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003661 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003662 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003663}
3664
Chris Lattner6ac46a42008-04-07 06:51:04 +00003665//===----------------------------------------------------------------------===//
3666// Type Compatibility Testing
3667//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003668
Mike Stump1eb44332009-09-09 15:08:12 +00003669/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003670/// compatible.
3671static bool areCompatVectorTypes(const VectorType *LHS,
3672 const VectorType *RHS) {
John McCall467b27b2009-10-22 20:10:53 +00003673 assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
Chris Lattner6ac46a42008-04-07 06:51:04 +00003674 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003675 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003676}
3677
Steve Naroff4084c302009-07-23 01:01:38 +00003678//===----------------------------------------------------------------------===//
3679// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3680//===----------------------------------------------------------------------===//
3681
3682/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3683/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003684bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3685 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003686 if (lProto == rProto)
3687 return true;
3688 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3689 E = rProto->protocol_end(); PI != E; ++PI)
3690 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3691 return true;
3692 return false;
3693}
3694
Steve Naroff4084c302009-07-23 01:01:38 +00003695/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3696/// return true if lhs's protocols conform to rhs's protocol; false
3697/// otherwise.
3698bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3699 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3700 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3701 return false;
3702}
3703
3704/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3705/// ObjCQualifiedIDType.
3706bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3707 bool compare) {
3708 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003709 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003710 lhs->isObjCIdType() || lhs->isObjCClassType())
3711 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003712 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003713 rhs->isObjCIdType() || rhs->isObjCClassType())
3714 return true;
3715
3716 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003717 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003718
Steve Naroff4084c302009-07-23 01:01:38 +00003719 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003720
Steve Naroff4084c302009-07-23 01:01:38 +00003721 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003722 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003723 // make sure we check the class hierarchy.
3724 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3725 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3726 E = lhsQID->qual_end(); I != E; ++I) {
3727 // when comparing an id<P> on lhs with a static type on rhs,
3728 // see if static class implements all of id's protocols, directly or
3729 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003730 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003731 return false;
3732 }
3733 }
3734 // If there are no qualifiers and no interface, we have an 'id'.
3735 return true;
3736 }
Mike Stump1eb44332009-09-09 15:08:12 +00003737 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003738 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3739 E = lhsQID->qual_end(); I != E; ++I) {
3740 ObjCProtocolDecl *lhsProto = *I;
3741 bool match = false;
3742
3743 // when comparing an id<P> on lhs with a static type on rhs,
3744 // see if static class implements all of id's protocols, directly or
3745 // through its super class and categories.
3746 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3747 E = rhsOPT->qual_end(); J != E; ++J) {
3748 ObjCProtocolDecl *rhsProto = *J;
3749 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3750 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3751 match = true;
3752 break;
3753 }
3754 }
Mike Stump1eb44332009-09-09 15:08:12 +00003755 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003756 // make sure we check the class hierarchy.
3757 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3758 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3759 E = lhsQID->qual_end(); I != E; ++I) {
3760 // when comparing an id<P> on lhs with a static type on rhs,
3761 // see if static class implements all of id's protocols, directly or
3762 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003763 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003764 match = true;
3765 break;
3766 }
3767 }
3768 }
3769 if (!match)
3770 return false;
3771 }
Mike Stump1eb44332009-09-09 15:08:12 +00003772
Steve Naroff4084c302009-07-23 01:01:38 +00003773 return true;
3774 }
Mike Stump1eb44332009-09-09 15:08:12 +00003775
Steve Naroff4084c302009-07-23 01:01:38 +00003776 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3777 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3778
Mike Stump1eb44332009-09-09 15:08:12 +00003779 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003780 lhs->getAsObjCInterfacePointerType()) {
3781 if (lhsOPT->qual_empty()) {
3782 bool match = false;
3783 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3784 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3785 E = rhsQID->qual_end(); I != E; ++I) {
3786 // when comparing an id<P> on lhs with a static type on rhs,
3787 // see if static class implements all of id's protocols, directly or
3788 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003789 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003790 match = true;
3791 break;
3792 }
3793 }
3794 if (!match)
3795 return false;
3796 }
3797 return true;
3798 }
Mike Stump1eb44332009-09-09 15:08:12 +00003799 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003800 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3801 E = lhsOPT->qual_end(); I != E; ++I) {
3802 ObjCProtocolDecl *lhsProto = *I;
3803 bool match = false;
3804
3805 // when comparing an id<P> on lhs with a static type on rhs,
3806 // see if static class implements all of id's protocols, directly or
3807 // through its super class and categories.
3808 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3809 E = rhsQID->qual_end(); J != E; ++J) {
3810 ObjCProtocolDecl *rhsProto = *J;
3811 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3812 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3813 match = true;
3814 break;
3815 }
3816 }
3817 if (!match)
3818 return false;
3819 }
3820 return true;
3821 }
3822 return false;
3823}
3824
Eli Friedman3d815e72008-08-22 00:56:42 +00003825/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003826/// compatible for assignment from RHS to LHS. This handles validation of any
3827/// protocol qualifiers on the LHS or RHS.
3828///
Steve Naroff14108da2009-07-10 23:34:53 +00003829bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3830 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003831 // If either type represents the built-in 'id' or 'Class' types, return true.
3832 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003833 return true;
3834
Steve Naroff4084c302009-07-23 01:01:38 +00003835 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003836 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3837 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003838 false);
3839
Steve Naroff14108da2009-07-10 23:34:53 +00003840 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3841 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003842 if (LHS && RHS) // We have 2 user-defined types.
3843 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003844
Steve Naroff4084c302009-07-23 01:01:38 +00003845 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003846}
3847
Eli Friedman3d815e72008-08-22 00:56:42 +00003848bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3849 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003850 // Verify that the base decls are compatible: the RHS must be a subclass of
3851 // the LHS.
3852 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3853 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003854
Chris Lattner6ac46a42008-04-07 06:51:04 +00003855 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3856 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003857 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003858 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003859
Chris Lattner6ac46a42008-04-07 06:51:04 +00003860 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3861 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003862 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003863 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003864
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003865 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3866 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003867 LHSPI != LHSPE; LHSPI++) {
3868 bool RHSImplementsProtocol = false;
3869
3870 // If the RHS doesn't implement the protocol on the left, the types
3871 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003872 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003873 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003874 RHSPI != RHSPE; RHSPI++) {
3875 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003876 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003877 break;
3878 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003879 }
3880 // FIXME: For better diagnostics, consider passing back the protocol name.
3881 if (!RHSImplementsProtocol)
3882 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003883 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003884 // The RHS implements all protocols listed on the LHS.
3885 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003886}
3887
Steve Naroff389bf462009-02-12 17:52:19 +00003888bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3889 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00003890 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3891 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003892
Steve Naroff14108da2009-07-10 23:34:53 +00003893 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003894 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003895
3896 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3897 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003898}
3899
Mike Stump1eb44332009-09-09 15:08:12 +00003900/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003901/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003902/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003903/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003904bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3905 return !mergeTypes(LHS, RHS).isNull();
3906}
3907
3908QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00003909 const FunctionType *lbase = lhs->getAs<FunctionType>();
3910 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00003911 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3912 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003913 bool allLTypes = true;
3914 bool allRTypes = true;
3915
3916 // Check return type
3917 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3918 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003919 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3920 allLTypes = false;
3921 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3922 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003923 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003924 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3925 if (NoReturn != lbase->getNoReturnAttr())
3926 allLTypes = false;
3927 if (NoReturn != rbase->getNoReturnAttr())
3928 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003929
Eli Friedman3d815e72008-08-22 00:56:42 +00003930 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003931 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3932 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003933 unsigned lproto_nargs = lproto->getNumArgs();
3934 unsigned rproto_nargs = rproto->getNumArgs();
3935
3936 // Compatible functions must have the same number of arguments
3937 if (lproto_nargs != rproto_nargs)
3938 return QualType();
3939
3940 // Variadic and non-variadic functions aren't compatible
3941 if (lproto->isVariadic() != rproto->isVariadic())
3942 return QualType();
3943
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003944 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3945 return QualType();
3946
Eli Friedman3d815e72008-08-22 00:56:42 +00003947 // Check argument compatibility
3948 llvm::SmallVector<QualType, 10> types;
3949 for (unsigned i = 0; i < lproto_nargs; i++) {
3950 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3951 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3952 QualType argtype = mergeTypes(largtype, rargtype);
3953 if (argtype.isNull()) return QualType();
3954 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003955 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3956 allLTypes = false;
3957 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3958 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003959 }
3960 if (allLTypes) return lhs;
3961 if (allRTypes) return rhs;
3962 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003963 lproto->isVariadic(), lproto->getTypeQuals(),
3964 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003965 }
3966
3967 if (lproto) allRTypes = false;
3968 if (rproto) allLTypes = false;
3969
Douglas Gregor72564e72009-02-26 23:50:07 +00003970 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003971 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003972 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003973 if (proto->isVariadic()) return QualType();
3974 // Check that the types are compatible with the types that
3975 // would result from default argument promotions (C99 6.7.5.3p15).
3976 // The only types actually affected are promotable integer
3977 // types and floats, which would be passed as a different
3978 // type depending on whether the prototype is visible.
3979 unsigned proto_nargs = proto->getNumArgs();
3980 for (unsigned i = 0; i < proto_nargs; ++i) {
3981 QualType argTy = proto->getArgType(i);
3982 if (argTy->isPromotableIntegerType() ||
3983 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3984 return QualType();
3985 }
3986
3987 if (allLTypes) return lhs;
3988 if (allRTypes) return rhs;
3989 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003990 proto->getNumArgs(), proto->isVariadic(),
3991 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003992 }
3993
3994 if (allLTypes) return lhs;
3995 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003996 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003997}
3998
3999QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00004000 // C++ [expr]: If an expression initially has the type "reference to T", the
4001 // type is adjusted to "T" prior to any further analysis, the expression
4002 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004003 // expression is an lvalue unless the reference is an rvalue reference and
4004 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00004005 // FIXME: C++ shouldn't be going through here! The rules are different
4006 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004007 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
4008 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00004009 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00004010 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00004011 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00004012 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00004013
Eli Friedman3d815e72008-08-22 00:56:42 +00004014 QualType LHSCan = getCanonicalType(LHS),
4015 RHSCan = getCanonicalType(RHS);
4016
4017 // If two types are identical, they are compatible.
4018 if (LHSCan == RHSCan)
4019 return LHS;
4020
John McCall0953e762009-09-24 19:53:00 +00004021 // If the qualifiers are different, the types aren't compatible... mostly.
4022 Qualifiers LQuals = LHSCan.getQualifiers();
4023 Qualifiers RQuals = RHSCan.getQualifiers();
4024 if (LQuals != RQuals) {
4025 // If any of these qualifiers are different, we have a type
4026 // mismatch.
4027 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
4028 LQuals.getAddressSpace() != RQuals.getAddressSpace())
4029 return QualType();
4030
4031 // Exactly one GC qualifier difference is allowed: __strong is
4032 // okay if the other type has no GC qualifier but is an Objective
4033 // C object pointer (i.e. implicitly strong by default). We fix
4034 // this by pretending that the unqualified type was actually
4035 // qualified __strong.
4036 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
4037 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
4038 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
4039
4040 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
4041 return QualType();
4042
4043 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
4044 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
4045 }
4046 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
4047 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
4048 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004049 return QualType();
John McCall0953e762009-09-24 19:53:00 +00004050 }
4051
4052 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00004053
Eli Friedman852d63b2009-06-01 01:22:52 +00004054 Type::TypeClass LHSClass = LHSCan->getTypeClass();
4055 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00004056
Chris Lattner1adb8832008-01-14 05:45:46 +00004057 // We want to consider the two function types to be the same for these
4058 // comparisons, just force one to the other.
4059 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
4060 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00004061
4062 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00004063 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
4064 LHSClass = Type::ConstantArray;
4065 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
4066 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00004067
Nate Begeman213541a2008-04-18 23:10:10 +00004068 // Canonicalize ExtVector -> Vector.
4069 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
4070 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00004071
Chris Lattnera36a61f2008-04-07 05:43:21 +00004072 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004073 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00004074 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00004075 // a signed integer type, or an unsigned integer type.
John McCall183700f2009-09-21 23:43:11 +00004076 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004077 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
4078 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004079 }
John McCall183700f2009-09-21 23:43:11 +00004080 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00004081 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
4082 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00004083 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004084
Eli Friedman3d815e72008-08-22 00:56:42 +00004085 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004086 }
Eli Friedman3d815e72008-08-22 00:56:42 +00004087
Steve Naroff4a746782008-01-09 22:43:08 +00004088 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00004089 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00004090#define TYPE(Class, Base)
4091#define ABSTRACT_TYPE(Class, Base)
4092#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4093#define DEPENDENT_TYPE(Class, Base) case Type::Class:
4094#include "clang/AST/TypeNodes.def"
4095 assert(false && "Non-canonical and dependent types shouldn't get here");
4096 return QualType();
4097
Sebastian Redl7c80bd62009-03-16 23:22:08 +00004098 case Type::LValueReference:
4099 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00004100 case Type::MemberPointer:
4101 assert(false && "C++ should never be in mergeTypes");
4102 return QualType();
4103
4104 case Type::IncompleteArray:
4105 case Type::VariableArray:
4106 case Type::FunctionProto:
4107 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00004108 assert(false && "Types are eliminated above");
4109 return QualType();
4110
Chris Lattner1adb8832008-01-14 05:45:46 +00004111 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00004112 {
4113 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004114 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
4115 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00004116 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4117 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00004118 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004119 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00004120 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00004121 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004122 return getPointerType(ResultType);
4123 }
Steve Naroffc0febd52008-12-10 17:49:55 +00004124 case Type::BlockPointer:
4125 {
4126 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00004127 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
4128 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00004129 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
4130 if (ResultType.isNull()) return QualType();
4131 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
4132 return LHS;
4133 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
4134 return RHS;
4135 return getBlockPointerType(ResultType);
4136 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004137 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00004138 {
4139 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
4140 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
4141 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
4142 return QualType();
4143
4144 QualType LHSElem = getAsArrayType(LHS)->getElementType();
4145 QualType RHSElem = getAsArrayType(RHS)->getElementType();
4146 QualType ResultType = mergeTypes(LHSElem, RHSElem);
4147 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00004148 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4149 return LHS;
4150 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4151 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00004152 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
4153 ArrayType::ArraySizeModifier(), 0);
4154 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
4155 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004156 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
4157 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00004158 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
4159 return LHS;
4160 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
4161 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00004162 if (LVAT) {
4163 // FIXME: This isn't correct! But tricky to implement because
4164 // the array's size has to be the size of LHS, but the type
4165 // has to be different.
4166 return LHS;
4167 }
4168 if (RVAT) {
4169 // FIXME: This isn't correct! But tricky to implement because
4170 // the array's size has to be the size of RHS, but the type
4171 // has to be different.
4172 return RHS;
4173 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00004174 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
4175 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004176 return getIncompleteArrayType(ResultType,
4177 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00004178 }
Chris Lattner1adb8832008-01-14 05:45:46 +00004179 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00004180 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00004181 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00004182 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00004183 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00004184 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004185 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00004186 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00004187 case Type::Complex:
4188 // Distinct complex types are incompatible.
4189 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00004190 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004191 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00004192 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00004193 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00004194 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004195 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00004196 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004197 // FIXME: This should be type compatibility, e.g. whether
4198 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00004199 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
4200 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00004201 if (LHSIface && RHSIface &&
4202 canAssignObjCInterfaces(LHSIface, RHSIface))
4203 return LHS;
4204
Eli Friedman3d815e72008-08-22 00:56:42 +00004205 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00004206 }
Steve Naroff14108da2009-07-10 23:34:53 +00004207 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00004208 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
4209 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00004210 return LHS;
4211
Steve Naroffbc76dd02008-12-10 22:14:21 +00004212 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00004213 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00004214 case Type::FixedWidthInt:
4215 // Distinct fixed-width integers are not compatible.
4216 return QualType();
Douglas Gregor7532dc62009-03-30 22:58:21 +00004217 case Type::TemplateSpecialization:
4218 assert(false && "Dependent types have no size");
4219 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00004220 }
Douglas Gregor72564e72009-02-26 23:50:07 +00004221
4222 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004223}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00004224
Chris Lattner5426bf62008-04-07 07:01:58 +00004225//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00004226// Integer Predicates
4227//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00004228
Eli Friedmanad74a752008-06-28 06:23:08 +00004229unsigned ASTContext::getIntWidth(QualType T) {
4230 if (T == BoolTy)
4231 return 1;
Chris Lattner6a2b9262009-10-17 20:33:28 +00004232 if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) {
Eli Friedmanf98aba32009-02-13 02:31:07 +00004233 return FWIT->getWidth();
4234 }
4235 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00004236 return (unsigned)getTypeSize(T);
4237}
4238
4239QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4240 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattner6a2b9262009-10-17 20:33:28 +00004241
4242 // Turn <4 x signed int> -> <4 x unsigned int>
4243 if (const VectorType *VTy = T->getAs<VectorType>())
4244 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
4245 VTy->getNumElements());
4246
4247 // For enums, we return the unsigned version of the base type.
4248 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00004249 T = ETy->getDecl()->getIntegerType();
Chris Lattner6a2b9262009-10-17 20:33:28 +00004250
4251 const BuiltinType *BTy = T->getAs<BuiltinType>();
4252 assert(BTy && "Unexpected signed integer type");
Eli Friedmanad74a752008-06-28 06:23:08 +00004253 switch (BTy->getKind()) {
4254 case BuiltinType::Char_S:
4255 case BuiltinType::SChar:
4256 return UnsignedCharTy;
4257 case BuiltinType::Short:
4258 return UnsignedShortTy;
4259 case BuiltinType::Int:
4260 return UnsignedIntTy;
4261 case BuiltinType::Long:
4262 return UnsignedLongTy;
4263 case BuiltinType::LongLong:
4264 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004265 case BuiltinType::Int128:
4266 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004267 default:
4268 assert(0 && "Unexpected signed integer type");
4269 return QualType();
4270 }
4271}
4272
Douglas Gregor2cf26342009-04-09 22:27:44 +00004273ExternalASTSource::~ExternalASTSource() { }
4274
4275void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004276
4277
4278//===----------------------------------------------------------------------===//
4279// Builtin Type Computation
4280//===----------------------------------------------------------------------===//
4281
4282/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4283/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004284static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004285 ASTContext::GetBuiltinTypeError &Error,
4286 bool AllowTypeModifiers = true) {
4287 // Modifiers.
4288 int HowLong = 0;
4289 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004290
Chris Lattner86df27b2009-06-14 00:45:47 +00004291 // Read the modifiers first.
4292 bool Done = false;
4293 while (!Done) {
4294 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004295 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004296 case 'S':
4297 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4298 assert(!Signed && "Can't use 'S' modifier multiple times!");
4299 Signed = true;
4300 break;
4301 case 'U':
4302 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4303 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4304 Unsigned = true;
4305 break;
4306 case 'L':
4307 assert(HowLong <= 2 && "Can't have LLLL modifier");
4308 ++HowLong;
4309 break;
4310 }
4311 }
4312
4313 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004314
Chris Lattner86df27b2009-06-14 00:45:47 +00004315 // Read the base type.
4316 switch (*Str++) {
4317 default: assert(0 && "Unknown builtin type letter!");
4318 case 'v':
4319 assert(HowLong == 0 && !Signed && !Unsigned &&
4320 "Bad modifiers used with 'v'!");
4321 Type = Context.VoidTy;
4322 break;
4323 case 'f':
4324 assert(HowLong == 0 && !Signed && !Unsigned &&
4325 "Bad modifiers used with 'f'!");
4326 Type = Context.FloatTy;
4327 break;
4328 case 'd':
4329 assert(HowLong < 2 && !Signed && !Unsigned &&
4330 "Bad modifiers used with 'd'!");
4331 if (HowLong)
4332 Type = Context.LongDoubleTy;
4333 else
4334 Type = Context.DoubleTy;
4335 break;
4336 case 's':
4337 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4338 if (Unsigned)
4339 Type = Context.UnsignedShortTy;
4340 else
4341 Type = Context.ShortTy;
4342 break;
4343 case 'i':
4344 if (HowLong == 3)
4345 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4346 else if (HowLong == 2)
4347 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4348 else if (HowLong == 1)
4349 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4350 else
4351 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4352 break;
4353 case 'c':
4354 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4355 if (Signed)
4356 Type = Context.SignedCharTy;
4357 else if (Unsigned)
4358 Type = Context.UnsignedCharTy;
4359 else
4360 Type = Context.CharTy;
4361 break;
4362 case 'b': // boolean
4363 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4364 Type = Context.BoolTy;
4365 break;
4366 case 'z': // size_t.
4367 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4368 Type = Context.getSizeType();
4369 break;
4370 case 'F':
4371 Type = Context.getCFConstantStringType();
4372 break;
4373 case 'a':
4374 Type = Context.getBuiltinVaListType();
4375 assert(!Type.isNull() && "builtin va list type not initialized!");
4376 break;
4377 case 'A':
4378 // This is a "reference" to a va_list; however, what exactly
4379 // this means depends on how va_list is defined. There are two
4380 // different kinds of va_list: ones passed by value, and ones
4381 // passed by reference. An example of a by-value va_list is
4382 // x86, where va_list is a char*. An example of by-ref va_list
4383 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4384 // we want this argument to be a char*&; for x86-64, we want
4385 // it to be a __va_list_tag*.
4386 Type = Context.getBuiltinVaListType();
4387 assert(!Type.isNull() && "builtin va list type not initialized!");
4388 if (Type->isArrayType()) {
4389 Type = Context.getArrayDecayedType(Type);
4390 } else {
4391 Type = Context.getLValueReferenceType(Type);
4392 }
4393 break;
4394 case 'V': {
4395 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004396 unsigned NumElements = strtoul(Str, &End, 10);
4397 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004398
Chris Lattner86df27b2009-06-14 00:45:47 +00004399 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004400
Chris Lattner86df27b2009-06-14 00:45:47 +00004401 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4402 Type = Context.getVectorType(ElementType, NumElements);
4403 break;
4404 }
Douglas Gregord3a23b22009-09-28 21:45:01 +00004405 case 'X': {
4406 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4407 Type = Context.getComplexType(ElementType);
4408 break;
4409 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004410 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004411 Type = Context.getFILEType();
4412 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004413 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004414 return QualType();
4415 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004416 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004417 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004418 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004419 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004420 else
4421 Type = Context.getjmp_bufType();
4422
Mike Stumpfd612db2009-07-28 23:47:15 +00004423 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004424 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004425 return QualType();
4426 }
4427 break;
Mike Stump782fa302009-07-28 02:25:19 +00004428 }
Mike Stump1eb44332009-09-09 15:08:12 +00004429
Chris Lattner86df27b2009-06-14 00:45:47 +00004430 if (!AllowTypeModifiers)
4431 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004432
Chris Lattner86df27b2009-06-14 00:45:47 +00004433 Done = false;
4434 while (!Done) {
4435 switch (*Str++) {
4436 default: Done = true; --Str; break;
4437 case '*':
4438 Type = Context.getPointerType(Type);
4439 break;
4440 case '&':
4441 Type = Context.getLValueReferenceType(Type);
4442 break;
4443 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4444 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004445 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004446 break;
4447 }
4448 }
Mike Stump1eb44332009-09-09 15:08:12 +00004449
Chris Lattner86df27b2009-06-14 00:45:47 +00004450 return Type;
4451}
4452
4453/// GetBuiltinType - Return the type for the specified builtin.
4454QualType ASTContext::GetBuiltinType(unsigned id,
4455 GetBuiltinTypeError &Error) {
4456 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004457
Chris Lattner86df27b2009-06-14 00:45:47 +00004458 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004459
Chris Lattner86df27b2009-06-14 00:45:47 +00004460 Error = GE_None;
4461 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4462 if (Error != GE_None)
4463 return QualType();
4464 while (TypeStr[0] && TypeStr[0] != '.') {
4465 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4466 if (Error != GE_None)
4467 return QualType();
4468
4469 // Do array -> pointer decay. The builtin should use the decayed type.
4470 if (Ty->isArrayType())
4471 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004472
Chris Lattner86df27b2009-06-14 00:45:47 +00004473 ArgTypes.push_back(Ty);
4474 }
4475
4476 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4477 "'.' should only occur at end of builtin type list!");
4478
4479 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4480 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4481 return getFunctionNoProtoType(ResType);
4482 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4483 TypeStr[0] == '.', 0);
4484}
Eli Friedmana95d7572009-08-19 07:44:53 +00004485
4486QualType
4487ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4488 // Perform the usual unary conversions. We do this early so that
4489 // integral promotions to "int" can allow us to exit early, in the
4490 // lhs == rhs check. Also, for conversion purposes, we ignore any
4491 // qualifiers. For example, "const float" and "float" are
4492 // equivalent.
4493 if (lhs->isPromotableIntegerType())
4494 lhs = getPromotedIntegerType(lhs);
4495 else
4496 lhs = lhs.getUnqualifiedType();
4497 if (rhs->isPromotableIntegerType())
4498 rhs = getPromotedIntegerType(rhs);
4499 else
4500 rhs = rhs.getUnqualifiedType();
4501
4502 // If both types are identical, no conversion is needed.
4503 if (lhs == rhs)
4504 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004505
Eli Friedmana95d7572009-08-19 07:44:53 +00004506 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4507 // The caller can deal with this (e.g. pointer + int).
4508 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4509 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004510
4511 // At this point, we have two different arithmetic types.
4512
Eli Friedmana95d7572009-08-19 07:44:53 +00004513 // Handle complex types first (C99 6.3.1.8p1).
4514 if (lhs->isComplexType() || rhs->isComplexType()) {
4515 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004516 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004517 // convert the rhs to the lhs complex type.
4518 return lhs;
4519 }
Mike Stump1eb44332009-09-09 15:08:12 +00004520 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004521 // convert the lhs to the rhs complex type.
4522 return rhs;
4523 }
4524 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004525 // When both operands are complex, the shorter operand is converted to the
4526 // type of the longer, and that is the type of the result. This corresponds
4527 // to what is done when combining two real floating-point operands.
4528 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004529 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004530 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004531 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004532 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004533 // "double _Complex" is promoted to "long double _Complex".
4534 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004535
4536 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004537 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004538 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004539 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004540 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004541 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4542 // domains match. This is a requirement for our implementation, C99
4543 // does not require this promotion.
4544 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4545 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4546 return rhs;
4547 } else { // handle "_Complex double, double".
4548 return lhs;
4549 }
4550 }
4551 return lhs; // The domain/size match exactly.
4552 }
4553 // Now handle "real" floating types (i.e. float, double, long double).
4554 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4555 // if we have an integer operand, the result is the real floating type.
4556 if (rhs->isIntegerType()) {
4557 // convert rhs to the lhs floating point type.
4558 return lhs;
4559 }
4560 if (rhs->isComplexIntegerType()) {
4561 // convert rhs to the complex floating point type.
4562 return getComplexType(lhs);
4563 }
4564 if (lhs->isIntegerType()) {
4565 // convert lhs to the rhs floating point type.
4566 return rhs;
4567 }
Mike Stump1eb44332009-09-09 15:08:12 +00004568 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004569 // convert lhs to the complex floating point type.
4570 return getComplexType(rhs);
4571 }
4572 // We have two real floating types, float/complex combos were handled above.
4573 // Convert the smaller operand to the bigger result.
4574 int result = getFloatingTypeOrder(lhs, rhs);
4575 if (result > 0) // convert the rhs
4576 return lhs;
4577 assert(result < 0 && "illegal float comparison");
4578 return rhs; // convert the lhs
4579 }
4580 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4581 // Handle GCC complex int extension.
4582 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4583 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4584
4585 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004586 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004587 rhsComplexInt->getElementType()) >= 0)
4588 return lhs; // convert the rhs
4589 return rhs;
4590 } else if (lhsComplexInt && rhs->isIntegerType()) {
4591 // convert the rhs to the lhs complex type.
4592 return lhs;
4593 } else if (rhsComplexInt && lhs->isIntegerType()) {
4594 // convert the lhs to the rhs complex type.
4595 return rhs;
4596 }
4597 }
4598 // Finally, we have two differing integer types.
4599 // The rules for this case are in C99 6.3.1.8
4600 int compare = getIntegerTypeOrder(lhs, rhs);
4601 bool lhsSigned = lhs->isSignedIntegerType(),
4602 rhsSigned = rhs->isSignedIntegerType();
4603 QualType destType;
4604 if (lhsSigned == rhsSigned) {
4605 // Same signedness; use the higher-ranked type
4606 destType = compare >= 0 ? lhs : rhs;
4607 } else if (compare != (lhsSigned ? 1 : -1)) {
4608 // The unsigned type has greater than or equal rank to the
4609 // signed type, so use the unsigned type
4610 destType = lhsSigned ? rhs : lhs;
4611 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4612 // The two types are different widths; if we are here, that
4613 // means the signed type is larger than the unsigned type, so
4614 // use the signed type.
4615 destType = lhsSigned ? lhs : rhs;
4616 } else {
4617 // The signed type is higher-ranked than the unsigned type,
4618 // but isn't actually any bigger (like unsigned int and long
4619 // on most 32-bit systems). Use the unsigned type corresponding
4620 // to the signed type.
4621 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4622 }
4623 return destType;
4624}