blob: a2a88af44f8d10509048598746b741b1964ad811 [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 Stump1eb44332009-09-09 15:08:12 +000043 sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts),
44 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e222532009-07-02 17:08:52 +000045 Idents(idents), Selectors(sels),
Mike Stump1eb44332009-09-09 15:08:12 +000046 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall0f436562009-08-17 16:35:33 +000047 ObjCIdRedefinitionType = QualType();
48 ObjCClassRedefinitionType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +000049 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000050 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000051 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000052}
53
Reid Spencer5f016e22007-07-11 17:01:13 +000054ASTContext::~ASTContext() {
55 // Deallocate all the types.
56 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000057 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 Types.pop_back();
59 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000060
Nuno Lopesb74668e2008-12-17 22:30:25 +000061 {
John McCall0953e762009-09-24 19:53:00 +000062 llvm::FoldingSet<ExtQuals>::iterator
63 I = ExtQualNodes.begin(), E = ExtQualNodes.end();
64 while (I != E)
65 Deallocate(&*I++);
66 }
67
68 {
Nuno Lopesb74668e2008-12-17 22:30:25 +000069 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
70 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
71 while (I != E) {
72 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
73 delete R;
74 }
75 }
76
77 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000078 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
79 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000080 while (I != E) {
81 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
82 delete R;
83 }
84 }
85
Douglas Gregorab452ba2009-03-26 23:50:42 +000086 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000087 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
88 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000089 NNSEnd = NestedNameSpecifiers.end();
90 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000091 /* Increment in loop */)
92 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000093
94 if (GlobalNestedNameSpecifier)
95 GlobalNestedNameSpecifier->Destroy(*this);
96
Eli Friedmanb26153c2008-05-27 03:08:09 +000097 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000098}
99
Mike Stump1eb44332009-09-09 15:08:12 +0000100void
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
102 ExternalSource.reset(Source.take());
103}
104
Reid Spencer5f016e22007-07-11 17:01:13 +0000105void ASTContext::PrintStats() const {
106 fprintf(stderr, "*** AST Context Stats:\n");
107 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000108
Douglas Gregordbe833d2009-05-26 14:40:08 +0000109 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000110#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000111#define ABSTRACT_TYPE(Name, Parent)
112#include "clang/AST/TypeNodes.def"
113 0 // Extra
114 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
117 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000118 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120
Douglas Gregordbe833d2009-05-26 14:40:08 +0000121 unsigned Idx = 0;
122 unsigned TotalBytes = 0;
123#define TYPE(Name, Parent) \
124 if (counts[Idx]) \
125 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
126 TotalBytes += counts[Idx] * sizeof(Name##Type); \
127 ++Idx;
128#define ABSTRACT_TYPE(Name, Parent)
129#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregordbe833d2009-05-26 14:40:08 +0000131 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132
133 if (ExternalSource.get()) {
134 fprintf(stderr, "\n");
135 ExternalSource->PrintStats();
136 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
139
140void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000141 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000142}
143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144void ASTContext::InitBuiltinTypes() {
145 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 // C99 6.2.5p19.
148 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 // C99 6.2.5p2.
151 InitBuiltinType(BoolTy, BuiltinType::Bool);
152 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000153 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 InitBuiltinType(CharTy, BuiltinType::Char_S);
155 else
156 InitBuiltinType(CharTy, BuiltinType::Char_U);
157 // C99 6.2.5p4.
158 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
159 InitBuiltinType(ShortTy, BuiltinType::Short);
160 InitBuiltinType(IntTy, BuiltinType::Int);
161 InitBuiltinType(LongTy, BuiltinType::Long);
162 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // C99 6.2.5p6.
165 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
166 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
167 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
168 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
169 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 // C99 6.2.5p10.
172 InitBuiltinType(FloatTy, BuiltinType::Float);
173 InitBuiltinType(DoubleTy, BuiltinType::Double);
174 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000175
Chris Lattner2df9ced2009-04-30 02:43:43 +0000176 // GNU extension, 128-bit integers.
177 InitBuiltinType(Int128Ty, BuiltinType::Int128);
178 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
179
Chris Lattner3a250322009-02-26 23:43:47 +0000180 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
181 InitBuiltinType(WCharTy, BuiltinType::WChar);
182 else // C99
183 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000184
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000185 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
186 InitBuiltinType(Char16Ty, BuiltinType::Char16);
187 else // C99
188 Char16Ty = getFromTargetType(Target.getChar16Type());
189
190 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
191 InitBuiltinType(Char32Ty, BuiltinType::Char32);
192 else // C99
193 Char32Ty = getFromTargetType(Target.getChar32Type());
194
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000195 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000196 InitBuiltinType(OverloadTy, BuiltinType::Overload);
197
198 // Placeholder type for type-dependent expressions whose type is
199 // completely unknown. No code should ever check a type against
200 // DependentTy and users should never see it; however, it is here to
201 // help diagnose failures to properly check for type-dependent
202 // expressions.
203 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000204
Mike Stump1eb44332009-09-09 15:08:12 +0000205 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000206 // not yet been deduced.
207 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 // C99 6.2.5p11.
210 FloatComplexTy = getComplexType(FloatTy);
211 DoubleComplexTy = getComplexType(DoubleTy);
212 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000213
Steve Naroff7e219e42007-10-15 14:41:52 +0000214 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Steve Naroffde2e22d2009-07-15 18:40:39 +0000216 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
217 ObjCIdTypedefType = QualType();
218 ObjCClassTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Steve Naroffde2e22d2009-07-15 18:40:39 +0000220 // Builtin types for 'id' and 'Class'.
221 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
222 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff14108da2009-07-10 23:34:53 +0000223
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000224 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000226 // void * type
227 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000228
229 // nullptr type (C++0x 2.14.7)
230 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000231}
232
Douglas Gregor7caa6822009-07-24 20:34:43 +0000233VarDecl *ASTContext::getInstantiatedFromStaticDataMember(VarDecl *Var) {
234 assert(Var->isStaticDataMember() && "Not a static data member");
235 llvm::DenseMap<VarDecl *, VarDecl *>::iterator Pos
236 = InstantiatedFromStaticDataMember.find(Var);
237 if (Pos == InstantiatedFromStaticDataMember.end())
238 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Douglas Gregor7caa6822009-07-24 20:34:43 +0000240 return Pos->second;
241}
242
Mike Stump1eb44332009-09-09 15:08:12 +0000243void
Douglas Gregor7caa6822009-07-24 20:34:43 +0000244ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl) {
245 assert(Inst->isStaticDataMember() && "Not a static data member");
246 assert(Tmpl->isStaticDataMember() && "Not a static data member");
247 assert(!InstantiatedFromStaticDataMember[Inst] &&
248 "Already noted what static data member was instantiated from");
249 InstantiatedFromStaticDataMember[Inst] = Tmpl;
250}
251
Anders Carlsson0d8df782009-08-29 19:37:28 +0000252UnresolvedUsingDecl *
253ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000254 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson0d8df782009-08-29 19:37:28 +0000255 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
256 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
257 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Anders Carlsson0d8df782009-08-29 19:37:28 +0000259 return Pos->second;
260}
261
262void
263ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
264 UnresolvedUsingDecl *UUD) {
265 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
266 "Already noted what using decl what instantiated from");
267 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
268}
269
Anders Carlssond8b285f2009-09-01 04:26:58 +0000270FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
271 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
272 = InstantiatedFromUnnamedFieldDecl.find(Field);
273 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
274 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Anders Carlssond8b285f2009-09-01 04:26:58 +0000276 return Pos->second;
277}
278
279void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
280 FieldDecl *Tmpl) {
281 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
282 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
283 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
284 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Anders Carlssond8b285f2009-09-01 04:26:58 +0000286 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
287}
288
Douglas Gregor2e222532009-07-02 17:08:52 +0000289namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000290 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000291 : std::binary_function<SourceRange, SourceRange, bool> {
292 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Douglas Gregor2e222532009-07-02 17:08:52 +0000294 public:
295 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Douglas Gregor2e222532009-07-02 17:08:52 +0000297 bool operator()(SourceRange X, SourceRange Y) {
298 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
299 }
300 };
301}
302
303/// \brief Determine whether the given comment is a Doxygen-style comment.
304///
305/// \param Start the start of the comment text.
306///
307/// \param End the end of the comment text.
308///
309/// \param Member whether we want to check whether this is a member comment
310/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
311/// we only return true when we find a non-member comment.
Mike Stump1eb44332009-09-09 15:08:12 +0000312static bool
313isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregor2e222532009-07-02 17:08:52 +0000314 bool Member = false) {
Mike Stump1eb44332009-09-09 15:08:12 +0000315 const char *BufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000316 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
317 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
318 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Douglas Gregor2e222532009-07-02 17:08:52 +0000320 if (End - Start < 4)
321 return false;
322
323 assert(Start[0] == '/' && "Not a comment?");
324 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
325 return false;
326 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
327 return false;
328
329 return (Start[3] == '<') == Member;
330}
331
332/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump1eb44332009-09-09 15:08:12 +0000333/// it has one.
Douglas Gregor2e222532009-07-02 17:08:52 +0000334const char *ASTContext::getCommentForDecl(const Decl *D) {
335 if (!D)
336 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Douglas Gregor2e222532009-07-02 17:08:52 +0000338 // Check whether we have cached a comment string for this declaration
339 // already.
Mike Stump1eb44332009-09-09 15:08:12 +0000340 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregor2e222532009-07-02 17:08:52 +0000341 = DeclComments.find(D);
342 if (Pos != DeclComments.end())
343 return Pos->second.c_str();
344
Mike Stump1eb44332009-09-09 15:08:12 +0000345 // If we have an external AST source and have not yet loaded comments from
Douglas Gregor2e222532009-07-02 17:08:52 +0000346 // that source, do so now.
347 if (ExternalSource && !LoadedExternalComments) {
348 std::vector<SourceRange> LoadedComments;
349 ExternalSource->ReadComments(LoadedComments);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Douglas Gregor2e222532009-07-02 17:08:52 +0000351 if (!LoadedComments.empty())
352 Comments.insert(Comments.begin(), LoadedComments.begin(),
353 LoadedComments.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Douglas Gregor2e222532009-07-02 17:08:52 +0000355 LoadedExternalComments = true;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
358 // If there are no comments anywhere, we won't find anything.
Douglas Gregor2e222532009-07-02 17:08:52 +0000359 if (Comments.empty())
360 return 0;
361
362 // If the declaration doesn't map directly to a location in a file, we
363 // can't find the comment.
364 SourceLocation DeclStartLoc = D->getLocStart();
365 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
366 return 0;
367
368 // Find the comment that occurs just before this declaration.
369 std::vector<SourceRange>::iterator LastComment
Mike Stump1eb44332009-09-09 15:08:12 +0000370 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregor2e222532009-07-02 17:08:52 +0000371 SourceRange(DeclStartLoc),
372 BeforeInTranslationUnit(&SourceMgr));
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Douglas Gregor2e222532009-07-02 17:08:52 +0000374 // Decompose the location for the start of the declaration and find the
375 // beginning of the file buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000376 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregor2e222532009-07-02 17:08:52 +0000377 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000378 const char *FileBufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000379 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Douglas Gregor2e222532009-07-02 17:08:52 +0000381 // First check whether we have a comment for a member.
382 if (LastComment != Comments.end() &&
383 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
384 isDoxygenComment(SourceMgr, *LastComment, true)) {
385 std::pair<FileID, unsigned> LastCommentEndDecomp
386 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
387 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
388 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump1eb44332009-09-09 15:08:12 +0000389 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000390 LastCommentEndDecomp.second)) {
391 // The Doxygen member comment comes after the declaration starts and
392 // is on the same line and in the same file as the declaration. This
393 // is the comment we want.
394 std::string &Result = DeclComments[D];
Mike Stump1eb44332009-09-09 15:08:12 +0000395 Result.append(FileBufferStart +
396 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000397 FileBufferStart + LastCommentEndDecomp.second + 1);
398 return Result.c_str();
399 }
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Douglas Gregor2e222532009-07-02 17:08:52 +0000402 if (LastComment == Comments.begin())
403 return 0;
404 --LastComment;
405
406 // Decompose the end of the comment.
407 std::pair<FileID, unsigned> LastCommentEndDecomp
408 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregor2e222532009-07-02 17:08:52 +0000410 // If the comment and the declaration aren't in the same file, then they
411 // aren't related.
412 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
413 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Douglas Gregor2e222532009-07-02 17:08:52 +0000415 // Check that we actually have a Doxygen comment.
416 if (!isDoxygenComment(SourceMgr, *LastComment))
417 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor2e222532009-07-02 17:08:52 +0000419 // Compute the starting line for the declaration and for the end of the
420 // comment (this is expensive).
Mike Stump1eb44332009-09-09 15:08:12 +0000421 unsigned DeclStartLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000422 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
423 unsigned CommentEndLine
Mike Stump1eb44332009-09-09 15:08:12 +0000424 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000425 LastCommentEndDecomp.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Douglas Gregor2e222532009-07-02 17:08:52 +0000427 // If the comment does not end on the line prior to the declaration, then
428 // the comment is not associated with the declaration at all.
429 if (CommentEndLine + 1 != DeclStartLine)
430 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregor2e222532009-07-02 17:08:52 +0000432 // We have a comment, but there may be more comments on the previous lines.
433 // Keep looking so long as the comments are still Doxygen comments and are
434 // still adjacent.
Mike Stump1eb44332009-09-09 15:08:12 +0000435 unsigned ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000436 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
437 std::vector<SourceRange>::iterator FirstComment = LastComment;
438 while (FirstComment != Comments.begin()) {
439 // Look at the previous comment
440 --FirstComment;
441 std::pair<FileID, unsigned> Decomp
442 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Douglas Gregor2e222532009-07-02 17:08:52 +0000444 // If this previous comment is in a different file, we're done.
445 if (Decomp.first != DeclStartDecomp.first) {
446 ++FirstComment;
447 break;
448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Douglas Gregor2e222532009-07-02 17:08:52 +0000450 // If this comment is not a Doxygen comment, we're done.
451 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
452 ++FirstComment;
453 break;
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Douglas Gregor2e222532009-07-02 17:08:52 +0000456 // If the line number is not what we expected, we're done.
457 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
458 if (Line != ExpectedLine) {
459 ++FirstComment;
460 break;
461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Douglas Gregor2e222532009-07-02 17:08:52 +0000463 // Set the next expected line number.
Mike Stump1eb44332009-09-09 15:08:12 +0000464 ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000465 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor2e222532009-07-02 17:08:52 +0000468 // The iterator range [FirstComment, LastComment] contains all of the
469 // BCPL comments that, together, are associated with this declaration.
470 // Form a single comment block string for this declaration that concatenates
471 // all of these comments.
472 std::string &Result = DeclComments[D];
473 while (FirstComment != LastComment) {
474 std::pair<FileID, unsigned> DecompStart
475 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
476 std::pair<FileID, unsigned> DecompEnd
477 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
478 Result.append(FileBufferStart + DecompStart.second,
479 FileBufferStart + DecompEnd.second + 1);
480 ++FirstComment;
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregor2e222532009-07-02 17:08:52 +0000483 // Append the last comment line.
Mike Stump1eb44332009-09-09 15:08:12 +0000484 Result.append(FileBufferStart +
485 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000486 FileBufferStart + LastCommentEndDecomp.second + 1);
487 return Result.c_str();
488}
489
Chris Lattner464175b2007-07-18 17:52:12 +0000490//===----------------------------------------------------------------------===//
491// Type Sizing and Analysis
492//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000493
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000494/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
495/// scalar floating point type.
496const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000497 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000498 assert(BT && "Not a floating point type!");
499 switch (BT->getKind()) {
500 default: assert(0 && "Not a floating point type!");
501 case BuiltinType::Float: return Target.getFloatFormat();
502 case BuiltinType::Double: return Target.getDoubleFormat();
503 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
504 }
505}
506
Mike Stump196efbf2009-09-22 02:43:44 +0000507/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
Chris Lattneraf707ab2009-01-24 21:53:27 +0000508/// specified decl. Note that bitfields do not have a valid alignment, so
509/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000510unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000511 unsigned Align = Target.getCharWidth();
512
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000513 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedmandcdafb62009-02-22 02:56:25 +0000514 Align = std::max(Align, AA->getAlignment());
515
Chris Lattneraf707ab2009-01-24 21:53:27 +0000516 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
517 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000518 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000519 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000520 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000521 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
522 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000523 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
524 T = cast<ArrayType>(T)->getElementType();
525
526 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
527 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000528 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000529
530 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000531}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000532
Chris Lattnera7674d82007-07-13 22:13:22 +0000533/// getTypeSize - Return the size of the specified type, in bits. This method
534/// does not work on incomplete types.
John McCall0953e762009-09-24 19:53:00 +0000535///
536/// FIXME: Pointers into different addr spaces could have different sizes and
537/// alignment requirements: getPointerInfo should take an AddrSpace, this
538/// should take a QualType, &c.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000539std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000540ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000541 uint64_t Width=0;
542 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000543 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000544#define TYPE(Class, Base)
545#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000546#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000547#define DEPENDENT_TYPE(Class, Base) case Type::Class:
548#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000549 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000550 break;
551
Chris Lattner692233e2007-07-13 22:27:08 +0000552 case Type::FunctionNoProto:
553 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000554 // GCC extension: alignof(function) = 32 bits
555 Width = 0;
556 Align = 32;
557 break;
558
Douglas Gregor72564e72009-02-26 23:50:07 +0000559 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000560 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000561 Width = 0;
562 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
563 break;
564
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000565 case Type::ConstantArrayWithExpr:
566 case Type::ConstantArrayWithoutExpr:
Steve Narofffb22d962007-08-30 01:06:46 +0000567 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000568 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner98be4942008-03-05 18:54:05 +0000570 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000571 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000572 Align = EltInfo.second;
573 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000574 }
Nate Begeman213541a2008-04-18 23:10:10 +0000575 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000576 case Type::Vector: {
Mike Stump1eb44332009-09-09 15:08:12 +0000577 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000578 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000579 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000580 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000581 // If the alignment is not a power of 2, round up to the next power of 2.
582 // This happens for non-power-of-2 length vectors.
583 // FIXME: this should probably be a target property.
584 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000585 break;
586 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000587
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000588 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000589 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000590 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000591 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000592 // GCC extension: alignof(void) = 8 bits.
593 Width = 0;
594 Align = 8;
595 break;
596
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000597 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000598 Width = Target.getBoolWidth();
599 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000600 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000601 case BuiltinType::Char_S:
602 case BuiltinType::Char_U:
603 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000604 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000605 Width = Target.getCharWidth();
606 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000607 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000608 case BuiltinType::WChar:
609 Width = Target.getWCharWidth();
610 Align = Target.getWCharAlign();
611 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000612 case BuiltinType::Char16:
613 Width = Target.getChar16Width();
614 Align = Target.getChar16Align();
615 break;
616 case BuiltinType::Char32:
617 Width = Target.getChar32Width();
618 Align = Target.getChar32Align();
619 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000620 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000621 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000622 Width = Target.getShortWidth();
623 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000624 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000625 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000626 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000627 Width = Target.getIntWidth();
628 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000629 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000630 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000631 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000632 Width = Target.getLongWidth();
633 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000634 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000635 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000636 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000637 Width = Target.getLongLongWidth();
638 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000639 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000640 case BuiltinType::Int128:
641 case BuiltinType::UInt128:
642 Width = 128;
643 Align = 128; // int128_t is 128-bit aligned on all targets.
644 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000645 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000646 Width = Target.getFloatWidth();
647 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000648 break;
649 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000650 Width = Target.getDoubleWidth();
651 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000652 break;
653 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000654 Width = Target.getLongDoubleWidth();
655 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000656 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000657 case BuiltinType::NullPtr:
658 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
659 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000660 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000661 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000662 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000663 case Type::FixedWidthInt:
664 // FIXME: This isn't precisely correct; the width/alignment should depend
665 // on the available types for the target
666 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000667 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000668 Align = Width;
669 break;
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000670 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000671 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000672 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000673 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000674 case Type::BlockPointer: {
675 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
676 Width = Target.getPointerWidth(AS);
677 Align = Target.getPointerAlign(AS);
678 break;
679 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000680 case Type::Pointer: {
681 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000682 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000683 Align = Target.getPointerAlign(AS);
684 break;
685 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000686 case Type::LValueReference:
687 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000688 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000689 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000690 // FIXME: This is wrong for struct layout: a reference in a struct has
691 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000692 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000693 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000694 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
695 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
696 // If we ever want to support other ABIs this needs to be abstracted.
697
Sebastian Redlf30208a2009-01-24 21:16:55 +0000698 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000699 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000700 getTypeInfo(getPointerDiffType());
701 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000702 if (Pointee->isFunctionType())
703 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000704 Align = PtrDiffInfo.second;
705 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000706 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000707 case Type::Complex: {
708 // Complex types have the same alignment as their elements, but twice the
709 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000710 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000711 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000712 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000713 Align = EltInfo.second;
714 break;
715 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000716 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000717 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000718 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
719 Width = Layout.getSize();
720 Align = Layout.getAlignment();
721 break;
722 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000723 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000724 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000725 const TagType *TT = cast<TagType>(T);
726
727 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000728 Width = 1;
729 Align = 1;
730 break;
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Daniel Dunbar1d751182008-11-08 05:48:37 +0000733 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000734 return getTypeInfo(ET->getDecl()->getIntegerType());
735
Daniel Dunbar1d751182008-11-08 05:48:37 +0000736 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000737 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
738 Width = Layout.getSize();
739 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000740 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000741 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000742
John McCall7da24312009-09-05 00:15:47 +0000743 case Type::Elaborated: {
744 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
745 }
746
Douglas Gregor18857642009-04-30 17:32:17 +0000747 case Type::Typedef: {
748 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000749 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregor18857642009-04-30 17:32:17 +0000750 Align = Aligned->getAlignment();
751 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
752 } else
753 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000754 break;
Chris Lattner71763312008-04-06 22:05:18 +0000755 }
Douglas Gregor18857642009-04-30 17:32:17 +0000756
757 case Type::TypeOfExpr:
758 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
759 .getTypePtr());
760
761 case Type::TypeOf:
762 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
763
Anders Carlsson395b4752009-06-24 19:06:50 +0000764 case Type::Decltype:
765 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
766 .getTypePtr());
767
Douglas Gregor18857642009-04-30 17:32:17 +0000768 case Type::QualifiedName:
769 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Douglas Gregor18857642009-04-30 17:32:17 +0000771 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000772 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000773 "Cannot request the size of a dependent type");
774 // FIXME: this is likely to be wrong once we support template
775 // aliases, since a template alias could refer to a typedef that
776 // has an __aligned__ attribute on it.
777 return getTypeInfo(getCanonicalType(T));
778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Chris Lattner464175b2007-07-18 17:52:12 +0000780 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000781 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000782}
783
Chris Lattner34ebde42009-01-27 18:08:34 +0000784/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
785/// type for the current target in bits. This can be different than the ABI
786/// alignment in cases where it is beneficial for performance to overalign
787/// a data type.
788unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
789 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000790
791 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000792 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000793 T = CT->getElementType().getTypePtr();
794 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
795 T->isSpecificBuiltinType(BuiltinType::LongLong))
796 return std::max(ABIAlign, (unsigned)getTypeSize(T));
797
Chris Lattner34ebde42009-01-27 18:08:34 +0000798 return ABIAlign;
799}
800
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000801static void CollectLocalObjCIvars(ASTContext *Ctx,
802 const ObjCInterfaceDecl *OI,
803 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000804 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
805 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000806 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000807 if (!IVDecl->isInvalidDecl())
808 Fields.push_back(cast<FieldDecl>(IVDecl));
809 }
810}
811
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000812void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
813 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
814 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
815 CollectObjCIvars(SuperClass, Fields);
816 CollectLocalObjCIvars(this, OI, Fields);
817}
818
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000819/// ShallowCollectObjCIvars -
820/// Collect all ivars, including those synthesized, in the current class.
821///
822void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
823 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
824 bool CollectSynthesized) {
825 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
826 E = OI->ivar_end(); I != E; ++I) {
827 Ivars.push_back(*I);
828 }
829 if (CollectSynthesized)
830 CollectSynthesizedIvars(OI, Ivars);
831}
832
Fariborz Jahanian98200742009-05-12 18:14:29 +0000833void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
834 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000835 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
836 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000837 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
838 Ivars.push_back(Ivar);
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Fariborz Jahanian98200742009-05-12 18:14:29 +0000840 // Also look into nested protocols.
841 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
842 E = PD->protocol_end(); P != E; ++P)
843 CollectProtocolSynthesizedIvars(*P, Ivars);
844}
845
846/// CollectSynthesizedIvars -
847/// This routine collect synthesized ivars for the designated class.
848///
849void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
850 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000851 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
852 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000853 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
854 Ivars.push_back(Ivar);
855 }
856 // Also look into interface's protocol list for properties declared
857 // in the protocol and whose ivars are synthesized.
858 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
859 PE = OI->protocol_end(); P != PE; ++P) {
860 ObjCProtocolDecl *PD = (*P);
861 CollectProtocolSynthesizedIvars(PD, Ivars);
862 }
863}
864
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000865unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
866 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000867 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
868 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000869 if ((*I)->getPropertyIvarDecl())
870 ++count;
871
872 // Also look into nested protocols.
873 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
874 E = PD->protocol_end(); P != E; ++P)
875 count += CountProtocolSynthesizedIvars(*P);
876 return count;
877}
878
Mike Stump1eb44332009-09-09 15:08:12 +0000879unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000880 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000881 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
882 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000883 if ((*I)->getPropertyIvarDecl())
884 ++count;
885 }
886 // Also look into interface's protocol list for properties declared
887 // in the protocol and whose ivars are synthesized.
888 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
889 PE = OI->protocol_end(); P != PE; ++P) {
890 ObjCProtocolDecl *PD = (*P);
891 count += CountProtocolSynthesizedIvars(PD);
892 }
893 return count;
894}
895
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000896/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
897ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
898 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
899 I = ObjCImpls.find(D);
900 if (I != ObjCImpls.end())
901 return cast<ObjCImplementationDecl>(I->second);
902 return 0;
903}
904/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
905ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
906 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
907 I = ObjCImpls.find(D);
908 if (I != ObjCImpls.end())
909 return cast<ObjCCategoryImplDecl>(I->second);
910 return 0;
911}
912
913/// \brief Set the implementation of ObjCInterfaceDecl.
914void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
915 ObjCImplementationDecl *ImplD) {
916 assert(IFaceD && ImplD && "Passed null params");
917 ObjCImpls[IFaceD] = ImplD;
918}
919/// \brief Set the implementation of ObjCCategoryDecl.
920void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
921 ObjCCategoryImplDecl *ImplD) {
922 assert(CatD && ImplD && "Passed null params");
923 ObjCImpls[CatD] = ImplD;
924}
925
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000926/// \brief Allocate an uninitialized DeclaratorInfo.
927///
928/// The caller should initialize the memory held by DeclaratorInfo using
929/// the TypeLoc wrappers.
930///
931/// \param T the type that will be the basis for type source info. This type
932/// should refer to how the declarator was written in source code, not to
933/// what type semantic analysis resolved the declarator to.
934DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
935 unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
936 DeclaratorInfo *DInfo =
937 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
938 new (DInfo) DeclaratorInfo(T);
939 return DInfo;
940}
941
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000942/// getInterfaceLayoutImpl - Get or compute information about the
943/// layout of the given interface.
944///
945/// \param Impl - If given, also include the layout of the interface's
946/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000947const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000948ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
949 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000950 assert(!D->isForwardDecl() && "Invalid interface decl!");
951
Devang Patel44a3dde2008-06-04 21:54:36 +0000952 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000953 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000954 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
955 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
956 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000957
Daniel Dunbar453addb2009-05-03 11:16:44 +0000958 // Add in synthesized ivar count if laying out an implementation.
959 if (Impl) {
Anders Carlsson29445a02009-07-18 21:19:52 +0000960 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000961 unsigned SynthCount = CountSynthesizedIvars(D);
962 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000963 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000964 // entry. Note we can't cache this because we simply free all
965 // entries later; however we shouldn't look up implementations
966 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000967 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000968 return getObjCLayout(D, 0);
969 }
970
Mike Stump1eb44332009-09-09 15:08:12 +0000971 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000972 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
973 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Devang Patel44a3dde2008-06-04 21:54:36 +0000975 return *NewEntry;
976}
977
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000978const ASTRecordLayout &
979ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
980 return getObjCLayout(D, 0);
981}
982
983const ASTRecordLayout &
984ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
985 return getObjCLayout(D->getClassInterface(), D);
986}
987
Devang Patel88a981b2007-11-01 19:11:01 +0000988/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000989/// specified record (struct/union/class), which indicates its size and field
990/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000991const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000992 D = D->getDefinition(*this);
993 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000994
Chris Lattner464175b2007-07-18 17:52:12 +0000995 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +0000996 // Note that we can't save a reference to the entry because this function
997 // is recursive.
998 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000999 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001000
Mike Stump1eb44332009-09-09 15:08:12 +00001001 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +00001002 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +00001003 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Chris Lattner5d2a6302007-07-18 18:26:58 +00001005 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001006}
1007
Chris Lattnera7674d82007-07-13 22:13:22 +00001008//===----------------------------------------------------------------------===//
1009// Type creation/memoization methods
1010//===----------------------------------------------------------------------===//
1011
John McCall0953e762009-09-24 19:53:00 +00001012QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1013 unsigned Fast = Quals.getFastQualifiers();
1014 Quals.removeFastQualifiers();
1015
1016 // Check if we've already instantiated this type.
1017 llvm::FoldingSetNodeID ID;
1018 ExtQuals::Profile(ID, TypeNode, Quals);
1019 void *InsertPos = 0;
1020 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1021 assert(EQ->getQualifiers() == Quals);
1022 QualType T = QualType(EQ, Fast);
1023 return T;
1024 }
1025
1026 ExtQuals *New = new (*this, 8) ExtQuals(*this, TypeNode, Quals);
1027 ExtQualNodes.InsertNode(New, InsertPos);
1028 QualType T = QualType(New, Fast);
1029 return T;
1030}
1031
1032QualType ASTContext::getVolatileType(QualType T) {
1033 QualType CanT = getCanonicalType(T);
1034 if (CanT.isVolatileQualified()) return T;
1035
1036 QualifierCollector Quals;
1037 const Type *TypeNode = Quals.strip(T);
1038 Quals.addVolatile();
1039
1040 return getExtQualType(TypeNode, Quals);
1041}
1042
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001043QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001044 QualType CanT = getCanonicalType(T);
1045 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001046 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001047
John McCall0953e762009-09-24 19:53:00 +00001048 // If we are composing extended qualifiers together, merge together
1049 // into one ExtQuals node.
1050 QualifierCollector Quals;
1051 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001052
John McCall0953e762009-09-24 19:53:00 +00001053 // If this type already has an address space specified, it cannot get
1054 // another one.
1055 assert(!Quals.hasAddressSpace() &&
1056 "Type cannot be in multiple addr spaces!");
1057 Quals.addAddressSpace(AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001058
John McCall0953e762009-09-24 19:53:00 +00001059 return getExtQualType(TypeNode, Quals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001060}
1061
Chris Lattnerb7d25532009-02-18 22:53:11 +00001062QualType ASTContext::getObjCGCQualType(QualType T,
John McCall0953e762009-09-24 19:53:00 +00001063 Qualifiers::GC GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001064 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001065 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001066 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001068 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001069 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001070 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001071 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1072 return getPointerType(ResultType);
1073 }
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
John McCall0953e762009-09-24 19:53:00 +00001076 // If we are composing extended qualifiers together, merge together
1077 // into one ExtQuals node.
1078 QualifierCollector Quals;
1079 const Type *TypeNode = Quals.strip(T);
Mike Stump1eb44332009-09-09 15:08:12 +00001080
John McCall0953e762009-09-24 19:53:00 +00001081 // If this type already has an ObjCGC specified, it cannot get
1082 // another one.
1083 assert(!Quals.hasObjCGCAttr() &&
1084 "Type cannot have multiple ObjCGCs!");
1085 Quals.addObjCGCAttr(GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001086
John McCall0953e762009-09-24 19:53:00 +00001087 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001088}
Chris Lattnera7674d82007-07-13 22:13:22 +00001089
Mike Stump24556362009-07-25 21:26:53 +00001090QualType ASTContext::getNoReturnType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00001091 QualType ResultType;
Mike Stump24556362009-07-25 21:26:53 +00001092 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001093 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001094 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001095 ResultType = getPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001096 } else if (T->isBlockPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001097 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001098 ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001099 ResultType = getBlockPointerType(ResultType);
John McCall0953e762009-09-24 19:53:00 +00001100 } else {
1101 assert (T->isFunctionType()
1102 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump1eb44332009-09-09 15:08:12 +00001103
John McCall0953e762009-09-24 19:53:00 +00001104 if (const FunctionNoProtoType *F = T->getAs<FunctionNoProtoType>()) {
1105 ResultType = getFunctionNoProtoType(F->getResultType(), true);
1106 } else {
1107 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1108 ResultType
1109 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1110 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1111 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1112 F->getNumExceptions(), F->exception_begin(), true);
1113 }
Mike Stump24556362009-07-25 21:26:53 +00001114 }
John McCall0953e762009-09-24 19:53:00 +00001115
1116 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump24556362009-07-25 21:26:53 +00001117}
1118
Reid Spencer5f016e22007-07-11 17:01:13 +00001119/// getComplexType - Return the uniqued reference to the type for a complex
1120/// number with the specified element type.
1121QualType ASTContext::getComplexType(QualType T) {
1122 // Unique pointers, to guarantee there is only one pointer of a particular
1123 // structure.
1124 llvm::FoldingSetNodeID ID;
1125 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 void *InsertPos = 0;
1128 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1129 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 // If the pointee type isn't canonical, this won't be a canonical type either,
1132 // so fill in the canonical type field.
1133 QualType Canonical;
1134 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001135 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 // Get the new insert position for the node we care about.
1138 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001139 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 }
Steve Narofff83820b2009-01-27 22:08:43 +00001141 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 Types.push_back(New);
1143 ComplexTypes.InsertNode(New, InsertPos);
1144 return QualType(New, 0);
1145}
1146
Eli Friedmanf98aba32009-02-13 02:31:07 +00001147QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1148 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1149 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1150 FixedWidthIntType *&Entry = Map[Width];
1151 if (!Entry)
1152 Entry = new FixedWidthIntType(Width, Signed);
1153 return QualType(Entry, 0);
1154}
Reid Spencer5f016e22007-07-11 17:01:13 +00001155
1156/// getPointerType - Return the uniqued reference to the type for a pointer to
1157/// the specified type.
1158QualType ASTContext::getPointerType(QualType T) {
1159 // Unique pointers, to guarantee there is only one pointer of a particular
1160 // structure.
1161 llvm::FoldingSetNodeID ID;
1162 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 void *InsertPos = 0;
1165 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1166 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 // If the pointee type isn't canonical, this won't be a canonical type either,
1169 // so fill in the canonical type field.
1170 QualType Canonical;
1171 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001172 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 // Get the new insert position for the node we care about.
1175 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001176 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 }
Steve Narofff83820b2009-01-27 22:08:43 +00001178 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 Types.push_back(New);
1180 PointerTypes.InsertNode(New, InsertPos);
1181 return QualType(New, 0);
1182}
1183
Mike Stump1eb44332009-09-09 15:08:12 +00001184/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001185/// a pointer to the specified block.
1186QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001187 assert(T->isFunctionType() && "block of function types only");
1188 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001189 // structure.
1190 llvm::FoldingSetNodeID ID;
1191 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Steve Naroff5618bd42008-08-27 16:04:49 +00001193 void *InsertPos = 0;
1194 if (BlockPointerType *PT =
1195 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1196 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
1198 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001199 // type either so fill in the canonical type field.
1200 QualType Canonical;
1201 if (!T->isCanonical()) {
1202 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Steve Naroff5618bd42008-08-27 16:04:49 +00001204 // Get the new insert position for the node we care about.
1205 BlockPointerType *NewIP =
1206 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001207 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001208 }
Steve Narofff83820b2009-01-27 22:08:43 +00001209 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001210 Types.push_back(New);
1211 BlockPointerTypes.InsertNode(New, InsertPos);
1212 return QualType(New, 0);
1213}
1214
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001215/// getLValueReferenceType - Return the uniqued reference to the type for an
1216/// lvalue reference to the specified type.
1217QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 // Unique pointers, to guarantee there is only one pointer of a particular
1219 // structure.
1220 llvm::FoldingSetNodeID ID;
1221 ReferenceType::Profile(ID, T);
1222
1223 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001224 if (LValueReferenceType *RT =
1225 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001227
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 // If the referencee type isn't canonical, this won't be a canonical type
1229 // either, so fill in the canonical type field.
1230 QualType Canonical;
1231 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001232 Canonical = getLValueReferenceType(getCanonicalType(T));
1233
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001235 LValueReferenceType *NewIP =
1236 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001237 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 }
1239
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001240 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001242 LValueReferenceTypes.InsertNode(New, InsertPos);
1243 return QualType(New, 0);
1244}
1245
1246/// getRValueReferenceType - Return the uniqued reference to the type for an
1247/// rvalue reference to the specified type.
1248QualType ASTContext::getRValueReferenceType(QualType T) {
1249 // Unique pointers, to guarantee there is only one pointer of a particular
1250 // structure.
1251 llvm::FoldingSetNodeID ID;
1252 ReferenceType::Profile(ID, T);
1253
1254 void *InsertPos = 0;
1255 if (RValueReferenceType *RT =
1256 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1257 return QualType(RT, 0);
1258
1259 // If the referencee type isn't canonical, this won't be a canonical type
1260 // either, so fill in the canonical type field.
1261 QualType Canonical;
1262 if (!T->isCanonical()) {
1263 Canonical = getRValueReferenceType(getCanonicalType(T));
1264
1265 // Get the new insert position for the node we care about.
1266 RValueReferenceType *NewIP =
1267 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1268 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1269 }
1270
1271 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1272 Types.push_back(New);
1273 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 return QualType(New, 0);
1275}
1276
Sebastian Redlf30208a2009-01-24 21:16:55 +00001277/// getMemberPointerType - Return the uniqued reference to the type for a
1278/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001279QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001280 // Unique pointers, to guarantee there is only one pointer of a particular
1281 // structure.
1282 llvm::FoldingSetNodeID ID;
1283 MemberPointerType::Profile(ID, T, Cls);
1284
1285 void *InsertPos = 0;
1286 if (MemberPointerType *PT =
1287 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1288 return QualType(PT, 0);
1289
1290 // If the pointee or class type isn't canonical, this won't be a canonical
1291 // type either, so fill in the canonical type field.
1292 QualType Canonical;
1293 if (!T->isCanonical()) {
1294 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1295
1296 // Get the new insert position for the node we care about.
1297 MemberPointerType *NewIP =
1298 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1299 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1300 }
Steve Narofff83820b2009-01-27 22:08:43 +00001301 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001302 Types.push_back(New);
1303 MemberPointerTypes.InsertNode(New, InsertPos);
1304 return QualType(New, 0);
1305}
1306
Mike Stump1eb44332009-09-09 15:08:12 +00001307/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001308/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001309QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001310 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001311 ArrayType::ArraySizeModifier ASM,
1312 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001313 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1314 "Constant array of VLAs is illegal!");
1315
Chris Lattner38aeec72009-05-13 04:12:56 +00001316 // Convert the array size into a canonical width matching the pointer size for
1317 // the target.
1318 llvm::APInt ArySize(ArySizeIn);
1319 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001322 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001325 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001326 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001327 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 // If the element type isn't canonical, this won't be a canonical type either,
1330 // so fill in the canonical type field.
1331 QualType Canonical;
1332 if (!EltTy->isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001333 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001334 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001336 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001337 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001338 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001339 }
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001341 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001342 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001343 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 Types.push_back(New);
1345 return QualType(New, 0);
1346}
1347
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001348/// getConstantArrayWithExprType - Return a reference to the type for
1349/// an array of the specified element type.
1350QualType
1351ASTContext::getConstantArrayWithExprType(QualType EltTy,
1352 const llvm::APInt &ArySizeIn,
1353 Expr *ArySizeExpr,
1354 ArrayType::ArraySizeModifier ASM,
1355 unsigned EltTypeQuals,
1356 SourceRange Brackets) {
1357 // Convert the array size into a canonical width matching the pointer
1358 // size for the target.
1359 llvm::APInt ArySize(ArySizeIn);
1360 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1361
1362 // Compute the canonical ConstantArrayType.
1363 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1364 ArySize, ASM, EltTypeQuals);
1365 // Since we don't unique expressions, it isn't possible to unique VLA's
1366 // that have an expression provided for their size.
1367 ConstantArrayWithExprType *New =
1368 new(*this,8)ConstantArrayWithExprType(EltTy, Canonical,
1369 ArySize, ArySizeExpr,
1370 ASM, EltTypeQuals, Brackets);
1371 Types.push_back(New);
1372 return QualType(New, 0);
1373}
1374
1375/// getConstantArrayWithoutExprType - Return a reference to the type for
1376/// an array of the specified element type.
1377QualType
1378ASTContext::getConstantArrayWithoutExprType(QualType EltTy,
1379 const llvm::APInt &ArySizeIn,
1380 ArrayType::ArraySizeModifier ASM,
1381 unsigned EltTypeQuals) {
1382 // Convert the array size into a canonical width matching the pointer
1383 // size for the target.
1384 llvm::APInt ArySize(ArySizeIn);
1385 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1386
1387 // Compute the canonical ConstantArrayType.
1388 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1389 ArySize, ASM, EltTypeQuals);
1390 ConstantArrayWithoutExprType *New =
1391 new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical,
1392 ArySize, ASM, EltTypeQuals);
1393 Types.push_back(New);
1394 return QualType(New, 0);
1395}
1396
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001397/// getVariableArrayType - Returns a non-unique reference to the type for a
1398/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001399QualType ASTContext::getVariableArrayType(QualType EltTy,
1400 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001401 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001402 unsigned EltTypeQuals,
1403 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001404 // Since we don't unique expressions, it isn't possible to unique VLA's
1405 // that have an expression provided for their size.
1406
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001407 VariableArrayType *New =
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001408 new(*this,8)VariableArrayType(EltTy, QualType(),
1409 NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001410
1411 VariableArrayTypes.push_back(New);
1412 Types.push_back(New);
1413 return QualType(New, 0);
1414}
1415
Douglas Gregor898574e2008-12-05 23:32:09 +00001416/// getDependentSizedArrayType - Returns a non-unique reference to
1417/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001418/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001419QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1420 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001421 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001422 unsigned EltTypeQuals,
1423 SourceRange Brackets) {
Mike Stump1eb44332009-09-09 15:08:12 +00001424 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001425 "Size must be type- or value-dependent!");
1426
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001427 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001428 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001429 EltTypeQuals, NumElts);
Douglas Gregor898574e2008-12-05 23:32:09 +00001430
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001431 void *InsertPos = 0;
1432 DependentSizedArrayType *Canon
1433 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1434 DependentSizedArrayType *New;
1435 if (Canon) {
1436 // We already have a canonical version of this array type; use it as
1437 // the canonical type for a newly-built type.
Mike Stump1eb44332009-09-09 15:08:12 +00001438 New = new (*this,8) DependentSizedArrayType(*this, EltTy,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001439 QualType(Canon, 0),
1440 NumElts, ASM, EltTypeQuals,
1441 Brackets);
1442 } else {
1443 QualType CanonEltTy = getCanonicalType(EltTy);
1444 if (CanonEltTy == EltTy) {
1445 New = new (*this,8) DependentSizedArrayType(*this, EltTy, QualType(),
1446 NumElts, ASM, EltTypeQuals,
1447 Brackets);
1448 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1449 } else {
1450 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1451 ASM, EltTypeQuals,
1452 SourceRange());
1453 New = new (*this,8) DependentSizedArrayType(*this, EltTy, Canon,
1454 NumElts, ASM, EltTypeQuals,
Mike Stump1eb44332009-09-09 15:08:12 +00001455 Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001456 }
1457 }
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Douglas Gregor898574e2008-12-05 23:32:09 +00001459 Types.push_back(New);
1460 return QualType(New, 0);
1461}
1462
Eli Friedmanc5773c42008-02-15 18:16:39 +00001463QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1464 ArrayType::ArraySizeModifier ASM,
1465 unsigned EltTypeQuals) {
1466 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001467 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001468
1469 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001470 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001471 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1472 return QualType(ATP, 0);
1473
1474 // If the element type isn't canonical, this won't be a canonical type
1475 // either, so fill in the canonical type field.
1476 QualType Canonical;
1477
1478 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001479 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001480 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001481
1482 // Get the new insert position for the node we care about.
1483 IncompleteArrayType *NewIP =
1484 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001485 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001486 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001487
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001488 IncompleteArrayType *New
1489 = new (*this,8) IncompleteArrayType(EltTy, Canonical,
1490 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001491
1492 IncompleteArrayTypes.InsertNode(New, InsertPos);
1493 Types.push_back(New);
1494 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001495}
1496
Steve Naroff73322922007-07-18 18:00:27 +00001497/// getVectorType - Return the unique reference to a vector type of
1498/// the specified element type and size. VectorType must be a built-in type.
1499QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Chris Lattnerf52ab252008-04-06 22:59:24 +00001502 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001503 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Reid Spencer5f016e22007-07-11 17:01:13 +00001505 // Check if we've already instantiated a vector of this type.
1506 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001507 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 void *InsertPos = 0;
1509 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1510 return QualType(VTP, 0);
1511
1512 // If the element type isn't canonical, this won't be a canonical type either,
1513 // so fill in the canonical type field.
1514 QualType Canonical;
1515 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001516 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Reid Spencer5f016e22007-07-11 17:01:13 +00001518 // Get the new insert position for the node we care about.
1519 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001520 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 }
Steve Narofff83820b2009-01-27 22:08:43 +00001522 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 VectorTypes.InsertNode(New, InsertPos);
1524 Types.push_back(New);
1525 return QualType(New, 0);
1526}
1527
Nate Begeman213541a2008-04-18 23:10:10 +00001528/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001529/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001530QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001531 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Chris Lattnerf52ab252008-04-06 22:59:24 +00001533 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001534 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Steve Naroff73322922007-07-18 18:00:27 +00001536 // Check if we've already instantiated a vector of this type.
1537 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001538 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001539 void *InsertPos = 0;
1540 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1541 return QualType(VTP, 0);
1542
1543 // If the element type isn't canonical, this won't be a canonical type either,
1544 // so fill in the canonical type field.
1545 QualType Canonical;
1546 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001547 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Steve Naroff73322922007-07-18 18:00:27 +00001549 // Get the new insert position for the node we care about.
1550 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001551 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001552 }
Steve Narofff83820b2009-01-27 22:08:43 +00001553 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001554 VectorTypes.InsertNode(New, InsertPos);
1555 Types.push_back(New);
1556 return QualType(New, 0);
1557}
1558
Mike Stump1eb44332009-09-09 15:08:12 +00001559QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001560 Expr *SizeExpr,
1561 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001562 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001563 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001564 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001566 void *InsertPos = 0;
1567 DependentSizedExtVectorType *Canon
1568 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1569 DependentSizedExtVectorType *New;
1570 if (Canon) {
1571 // We already have a canonical version of this array type; use it as
1572 // the canonical type for a newly-built type.
1573 New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
1574 QualType(Canon, 0),
1575 SizeExpr, AttrLoc);
1576 } else {
1577 QualType CanonVecTy = getCanonicalType(vecType);
1578 if (CanonVecTy == vecType) {
Mike Stump1eb44332009-09-09 15:08:12 +00001579 New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
1580 QualType(), SizeExpr,
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001581 AttrLoc);
1582 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1583 } else {
1584 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1585 SourceLocation());
1586 New = new (*this,8) DependentSizedExtVectorType(*this, vecType, Canon,
1587 SizeExpr, AttrLoc);
1588 }
1589 }
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001591 Types.push_back(New);
1592 return QualType(New, 0);
1593}
1594
Douglas Gregor72564e72009-02-26 23:50:07 +00001595/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001596///
Mike Stump24556362009-07-25 21:26:53 +00001597QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 // Unique functions, to guarantee there is only one function of a particular
1599 // structure.
1600 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001601 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001604 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001605 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 QualType Canonical;
1609 if (!ResultTy->isCanonical()) {
Mike Stump24556362009-07-25 21:26:53 +00001610 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001613 FunctionNoProtoType *NewIP =
1614 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001615 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Mike Stump24556362009-07-25 21:26:53 +00001618 FunctionNoProtoType *New
1619 = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001621 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 return QualType(New, 0);
1623}
1624
1625/// getFunctionType - Return a normal function type with a typed argument
1626/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001627QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001628 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001629 unsigned TypeQuals, bool hasExceptionSpec,
1630 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump24556362009-07-25 21:26:53 +00001631 const QualType *ExArray, bool NoReturn) {
Anders Carlsson83913e32009-09-16 23:47:08 +00001632 if (LangOpts.CPlusPlus) {
1633 for (unsigned i = 0; i != NumArgs; ++i)
John McCall0953e762009-09-24 19:53:00 +00001634 assert(!ArgArray[i].hasQualifiers() &&
1635 "C++ arguments can't have toplevel qualifiers!");
Anders Carlsson83913e32009-09-16 23:47:08 +00001636 }
1637
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 // Unique functions, to guarantee there is only one function of a particular
1639 // structure.
1640 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001641 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001642 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001643 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001644
1645 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001646 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001647 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001649
1650 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001652 if (hasExceptionSpec)
1653 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1655 if (!ArgArray[i]->isCanonical())
1656 isCanonical = false;
1657
1658 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001659 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 QualType Canonical;
1661 if (!isCanonical) {
1662 llvm::SmallVector<QualType, 16> CanonicalArgs;
1663 CanonicalArgs.reserve(NumArgs);
1664 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001665 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001666
Chris Lattnerf52ab252008-04-06 22:59:24 +00001667 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001668 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001669 isVariadic, TypeQuals, false,
1670 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001671
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001673 FunctionProtoType *NewIP =
1674 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001675 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001677
Douglas Gregor72564e72009-02-26 23:50:07 +00001678 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001679 // for two variable size arrays (for parameter and exception types) at the
1680 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001681 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001682 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1683 NumArgs*sizeof(QualType) +
1684 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001685 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001686 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001687 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001689 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001690 return QualType(FTP, 0);
1691}
1692
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001693/// getTypeDeclType - Return the unique reference to the type for the
1694/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001695QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001696 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001697 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001699 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001700 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001701 else if (isa<TemplateTypeParmDecl>(Decl)) {
1702 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001703 } else if (ObjCInterfaceDecl *ObjCInterface
1704 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001705 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001706
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001707 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001708 if (PrevDecl)
1709 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001710 else
1711 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001712 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001713 if (PrevDecl)
1714 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001715 else
1716 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001717 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001718 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001719
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001720 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001721 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001722}
1723
Reid Spencer5f016e22007-07-11 17:01:13 +00001724/// getTypedefType - Return the unique reference to the type for the
1725/// specified typename decl.
1726QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1727 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Chris Lattnerf52ab252008-04-06 22:59:24 +00001729 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001730 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 Types.push_back(Decl->TypeForDecl);
1732 return QualType(Decl->TypeForDecl, 0);
1733}
1734
Douglas Gregorfab9d672009-02-05 23:33:38 +00001735/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001736/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001737/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001738QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001739 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001740 IdentifierInfo *Name) {
1741 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001742 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001743 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001744 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001745 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1746
1747 if (TypeParm)
1748 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001750 if (Name) {
1751 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
1752 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
1753 Name, Canon);
1754 } else
1755 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001756
1757 Types.push_back(TypeParm);
1758 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1759
1760 return QualType(TypeParm, 0);
1761}
1762
Mike Stump1eb44332009-09-09 15:08:12 +00001763QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001764ASTContext::getTemplateSpecializationType(TemplateName Template,
1765 const TemplateArgument *Args,
1766 unsigned NumArgs,
1767 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001768 if (!Canon.isNull())
1769 Canon = getCanonicalType(Canon);
1770 else {
1771 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001772 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1773 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1774 CanonArgs.reserve(NumArgs);
1775 for (unsigned I = 0; I != NumArgs; ++I)
1776 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1777
1778 // Determine whether this canonical template specialization type already
1779 // exists.
1780 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001781 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001782 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001783
1784 void *InsertPos = 0;
1785 TemplateSpecializationType *Spec
1786 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Douglas Gregor1275ae02009-07-28 23:00:59 +00001788 if (!Spec) {
1789 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001790 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001791 sizeof(TemplateArgument) * NumArgs),
1792 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001793 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001794 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001795 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001796 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001797 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001798 }
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregorb88e8882009-07-30 17:40:51 +00001800 if (Canon.isNull())
1801 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001802 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001803 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001804 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001805
Douglas Gregor1275ae02009-07-28 23:00:59 +00001806 // Allocate the (non-canonical) template specialization type, but don't
1807 // try to unique it: these types typically have location information that
1808 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001809 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001810 sizeof(TemplateArgument) * NumArgs),
1811 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001812 TemplateSpecializationType *Spec
1813 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001814 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Douglas Gregor55f6b142009-02-09 18:46:07 +00001816 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001817 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001818}
1819
Mike Stump1eb44332009-09-09 15:08:12 +00001820QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001821ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001822 QualType NamedType) {
1823 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001824 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001825
1826 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001827 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001828 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1829 if (T)
1830 return QualType(T, 0);
1831
Mike Stump1eb44332009-09-09 15:08:12 +00001832 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001833 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001834 Types.push_back(T);
1835 QualifiedNameTypes.InsertNode(T, InsertPos);
1836 return QualType(T, 0);
1837}
1838
Mike Stump1eb44332009-09-09 15:08:12 +00001839QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001840 const IdentifierInfo *Name,
1841 QualType Canon) {
1842 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1843
1844 if (Canon.isNull()) {
1845 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1846 if (CanonNNS != NNS)
1847 Canon = getTypenameType(CanonNNS, Name);
1848 }
1849
1850 llvm::FoldingSetNodeID ID;
1851 TypenameType::Profile(ID, NNS, Name);
1852
1853 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001854 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001855 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1856 if (T)
1857 return QualType(T, 0);
1858
1859 T = new (*this) TypenameType(NNS, Name, Canon);
1860 Types.push_back(T);
1861 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001862 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001863}
1864
Mike Stump1eb44332009-09-09 15:08:12 +00001865QualType
1866ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001867 const TemplateSpecializationType *TemplateId,
1868 QualType Canon) {
1869 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1870
1871 if (Canon.isNull()) {
1872 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1873 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1874 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1875 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00001876 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00001877 assert(CanonTemplateId &&
1878 "Canonical type must also be a template specialization type");
1879 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1880 }
1881 }
1882
1883 llvm::FoldingSetNodeID ID;
1884 TypenameType::Profile(ID, NNS, TemplateId);
1885
1886 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001887 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001888 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1889 if (T)
1890 return QualType(T, 0);
1891
1892 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1893 Types.push_back(T);
1894 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001895 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001896}
1897
John McCall7da24312009-09-05 00:15:47 +00001898QualType
1899ASTContext::getElaboratedType(QualType UnderlyingType,
1900 ElaboratedType::TagKind Tag) {
1901 llvm::FoldingSetNodeID ID;
1902 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001903
John McCall7da24312009-09-05 00:15:47 +00001904 void *InsertPos = 0;
1905 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1906 if (T)
1907 return QualType(T, 0);
1908
1909 QualType Canon = getCanonicalType(UnderlyingType);
1910
1911 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1912 Types.push_back(T);
1913 ElaboratedTypes.InsertNode(T, InsertPos);
1914 return QualType(T, 0);
1915}
1916
Chris Lattner88cb27a2008-04-07 04:56:42 +00001917/// CmpProtocolNames - Comparison predicate for sorting protocols
1918/// alphabetically.
1919static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1920 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001921 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001922}
1923
1924static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1925 unsigned &NumProtocols) {
1926 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Chris Lattner88cb27a2008-04-07 04:56:42 +00001928 // Sort protocols, keyed by name.
1929 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1930
1931 // Remove duplicates.
1932 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1933 NumProtocols = ProtocolsEnd-Protocols;
1934}
1935
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001936/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1937/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001938QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001939 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001940 unsigned NumProtocols) {
1941 // Sort the protocol list alphabetically to canonicalize it.
1942 if (NumProtocols)
1943 SortAndUniqueProtocols(Protocols, NumProtocols);
1944
1945 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001946 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001947
1948 void *InsertPos = 0;
1949 if (ObjCObjectPointerType *QT =
1950 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1951 return QualType(QT, 0);
1952
1953 // No Match;
1954 ObjCObjectPointerType *QType =
Steve Naroff14108da2009-07-10 23:34:53 +00001955 new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001957 Types.push_back(QType);
1958 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1959 return QualType(QType, 0);
1960}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001961
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001962/// getObjCInterfaceType - Return the unique reference to the type for the
1963/// specified ObjC interface decl. The list of protocols is optional.
1964QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001965 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump1eb44332009-09-09 15:08:12 +00001966 if (NumProtocols)
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001967 // Sort the protocol list alphabetically to canonicalize it.
1968 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001970 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001971 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001973 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001974 if (ObjCInterfaceType *QT =
1975 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001976 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001978 // No Match;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001979 ObjCInterfaceType *QType =
Mike Stump1eb44332009-09-09 15:08:12 +00001980 new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001981 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001982 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001983 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001984 return QualType(QType, 0);
1985}
1986
Douglas Gregor72564e72009-02-26 23:50:07 +00001987/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1988/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001989/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00001990/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00001991/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001992QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00001993 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00001994 if (tofExpr->isTypeDependent()) {
1995 llvm::FoldingSetNodeID ID;
1996 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Douglas Gregorb1975722009-07-30 23:18:24 +00001998 void *InsertPos = 0;
1999 DependentTypeOfExprType *Canon
2000 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2001 if (Canon) {
2002 // We already have a "canonical" version of an identical, dependent
2003 // typeof(expr) type. Use that as our canonical type.
Mike Stump1eb44332009-09-09 15:08:12 +00002004 toe = new (*this, 8) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002005 QualType((TypeOfExprType*)Canon, 0));
2006 }
2007 else {
2008 // Build a new, canonical typeof(expr) type.
2009 Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr);
2010 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2011 toe = Canon;
2012 }
2013 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002014 QualType Canonical = getCanonicalType(tofExpr->getType());
2015 toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
2016 }
Steve Naroff9752f252007-08-01 18:02:17 +00002017 Types.push_back(toe);
2018 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002019}
2020
Steve Naroff9752f252007-08-01 18:02:17 +00002021/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2022/// TypeOfType AST's. The only motivation to unique these nodes would be
2023/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002024/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002025/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002026QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002027 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00002028 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002029 Types.push_back(tot);
2030 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002031}
2032
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002033/// getDecltypeForExpr - Given an expr, will return the decltype for that
2034/// expression, according to the rules in C++0x [dcl.type.simple]p4
2035static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002036 if (e->isTypeDependent())
2037 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002039 // If e is an id expression or a class member access, decltype(e) is defined
2040 // as the type of the entity named by e.
2041 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2042 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2043 return VD->getType();
2044 }
2045 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2046 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2047 return FD->getType();
2048 }
2049 // If e is a function call or an invocation of an overloaded operator,
2050 // (parentheses around e are ignored), decltype(e) is defined as the
2051 // return type of that function.
2052 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2053 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002055 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002056
2057 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002058 // defined as T&, otherwise decltype(e) is defined as T.
2059 if (e->isLvalue(Context) == Expr::LV_Valid)
2060 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002061
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002062 return T;
2063}
2064
Anders Carlsson395b4752009-06-24 19:06:50 +00002065/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2066/// DecltypeType AST's. The only motivation to unique these nodes would be
2067/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002068/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002069/// on canonical type's (which are always unique).
2070QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002071 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002072 if (e->isTypeDependent()) {
2073 llvm::FoldingSetNodeID ID;
2074 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002075
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002076 void *InsertPos = 0;
2077 DependentDecltypeType *Canon
2078 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2079 if (Canon) {
2080 // We already have a "canonical" version of an equivalent, dependent
2081 // decltype type. Use that as our canonical type.
2082 dt = new (*this, 8) DecltypeType(e, DependentTy,
2083 QualType((DecltypeType*)Canon, 0));
2084 }
2085 else {
2086 // Build a new, canonical typeof(expr) type.
2087 Canon = new (*this, 8) DependentDecltypeType(*this, e);
2088 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2089 dt = Canon;
2090 }
2091 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002092 QualType T = getDecltypeForExpr(e, *this);
Mike Stump1eb44332009-09-09 15:08:12 +00002093 dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002094 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002095 Types.push_back(dt);
2096 return QualType(dt, 0);
2097}
2098
Reid Spencer5f016e22007-07-11 17:01:13 +00002099/// getTagDeclType - Return the unique reference to the type for the
2100/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002101QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002102 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002103 // FIXME: What is the design on getTagDeclType when it requires casting
2104 // away const? mutable?
2105 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002106}
2107
Mike Stump1eb44332009-09-09 15:08:12 +00002108/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2109/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2110/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002111QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002112 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002113}
2114
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002115/// getSignedWCharType - Return the type of "signed wchar_t".
2116/// Used when in C++, as a GCC extension.
2117QualType ASTContext::getSignedWCharType() const {
2118 // FIXME: derive from "Target" ?
2119 return WCharTy;
2120}
2121
2122/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2123/// Used when in C++, as a GCC extension.
2124QualType ASTContext::getUnsignedWCharType() const {
2125 // FIXME: derive from "Target" ?
2126 return UnsignedIntTy;
2127}
2128
Chris Lattner8b9023b2007-07-13 03:05:23 +00002129/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2130/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2131QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002132 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002133}
2134
Chris Lattnere6327742008-04-02 05:18:44 +00002135//===----------------------------------------------------------------------===//
2136// Type Operators
2137//===----------------------------------------------------------------------===//
2138
Chris Lattner77c96472008-04-06 22:41:35 +00002139/// getCanonicalType - Return the canonical (structural) type corresponding to
2140/// the specified potentially non-canonical type. The non-canonical version
2141/// of a type may have many "decorated" versions of types. Decorators can
2142/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2143/// to be free of any of these, allowing two canonical types to be compared
2144/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002145CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall0953e762009-09-24 19:53:00 +00002146 QualifierCollector Quals;
2147 const Type *Ptr = Quals.strip(T);
2148 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002149
John McCall0953e762009-09-24 19:53:00 +00002150 // The canonical internal type will be the canonical type *except*
2151 // that we push type qualifiers down through array types.
2152
2153 // If there are no new qualifiers to push down, stop here.
2154 if (!Quals.hasQualifiers())
Douglas Gregor50d62d12009-08-05 05:36:45 +00002155 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002156
John McCall0953e762009-09-24 19:53:00 +00002157 // If the type qualifiers are on an array type, get the canonical
2158 // type of the array with the qualifiers applied to the element
2159 // type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002160 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2161 if (!AT)
John McCall0953e762009-09-24 19:53:00 +00002162 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump1eb44332009-09-09 15:08:12 +00002163
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002164 // Get the canonical version of the element with the extra qualifiers on it.
2165 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002166 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002167 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002169 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002170 return CanQualType::CreateUnsafe(
2171 getConstantArrayType(NewEltTy, CAT->getSize(),
2172 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002173 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002174 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002175 return CanQualType::CreateUnsafe(
2176 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002177 IAT->getIndexTypeCVRQualifiers()));
Mike Stump1eb44332009-09-09 15:08:12 +00002178
Douglas Gregor898574e2008-12-05 23:32:09 +00002179 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002180 return CanQualType::CreateUnsafe(
2181 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002182 DSAT->getSizeExpr() ?
2183 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002184 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002185 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002186 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002187
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002188 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002189 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002190 VAT->getSizeExpr() ?
2191 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002192 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002193 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor50d62d12009-08-05 05:36:45 +00002194 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002195}
2196
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002197TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2198 // If this template name refers to a template, the canonical
2199 // template name merely stores the template itself.
2200 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002201 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002202
Mike Stump1eb44332009-09-09 15:08:12 +00002203 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002204 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002205 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2206 OverloadedFunctionDecl *CanonOvl = 0;
2207 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2208 FEnd = Ovl->function_end();
2209 F != FEnd; ++F) {
2210 Decl *Canon = F->get()->getCanonicalDecl();
2211 if (CanonOvl || Canon != F->get()) {
2212 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002213 CanonOvl = OverloadedFunctionDecl::Create(*this,
2214 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002215 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002217 CanonOvl->addOverload(
2218 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2219 }
2220 }
Mike Stump1eb44332009-09-09 15:08:12 +00002221
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002222 return TemplateName(CanonOvl? CanonOvl : Ovl);
2223 }
Mike Stump1eb44332009-09-09 15:08:12 +00002224
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002225 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2226 assert(DTN && "Non-dependent template names must refer to template decls.");
2227 return DTN->CanonicalTemplateName;
2228}
2229
Mike Stump1eb44332009-09-09 15:08:12 +00002230TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002231ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2232 switch (Arg.getKind()) {
2233 case TemplateArgument::Null:
2234 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Douglas Gregor1275ae02009-07-28 23:00:59 +00002236 case TemplateArgument::Expression:
2237 // FIXME: Build canonical expression?
2238 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002239
Douglas Gregor1275ae02009-07-28 23:00:59 +00002240 case TemplateArgument::Declaration:
2241 return TemplateArgument(SourceLocation(),
2242 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Douglas Gregor1275ae02009-07-28 23:00:59 +00002244 case TemplateArgument::Integral:
2245 return TemplateArgument(SourceLocation(),
2246 *Arg.getAsIntegral(),
2247 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Douglas Gregor1275ae02009-07-28 23:00:59 +00002249 case TemplateArgument::Type:
2250 return TemplateArgument(SourceLocation(),
2251 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Douglas Gregor1275ae02009-07-28 23:00:59 +00002253 case TemplateArgument::Pack: {
2254 // FIXME: Allocate in ASTContext
2255 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2256 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002257 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002258 AEnd = Arg.pack_end();
2259 A != AEnd; (void)++A, ++Idx)
2260 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Douglas Gregor1275ae02009-07-28 23:00:59 +00002262 TemplateArgument Result;
2263 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2264 return Result;
2265 }
2266 }
2267
2268 // Silence GCC warning
2269 assert(false && "Unhandled template argument kind");
2270 return TemplateArgument();
2271}
2272
Douglas Gregord57959a2009-03-27 23:10:48 +00002273NestedNameSpecifier *
2274ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002275 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002276 return 0;
2277
2278 switch (NNS->getKind()) {
2279 case NestedNameSpecifier::Identifier:
2280 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002281 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002282 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2283 NNS->getAsIdentifier());
2284
2285 case NestedNameSpecifier::Namespace:
2286 // A namespace is canonical; build a nested-name-specifier with
2287 // this namespace and no prefix.
2288 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2289
2290 case NestedNameSpecifier::TypeSpec:
2291 case NestedNameSpecifier::TypeSpecWithTemplate: {
2292 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002293 return NestedNameSpecifier::Create(*this, 0,
2294 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002295 T.getTypePtr());
2296 }
2297
2298 case NestedNameSpecifier::Global:
2299 // The global specifier is canonical and unique.
2300 return NNS;
2301 }
2302
2303 // Required to silence a GCC warning
2304 return 0;
2305}
2306
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002307
2308const ArrayType *ASTContext::getAsArrayType(QualType T) {
2309 // Handle the non-qualified case efficiently.
John McCall0953e762009-09-24 19:53:00 +00002310 if (!T.hasQualifiers()) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002311 // Handle the common positive case fast.
2312 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2313 return AT;
2314 }
Mike Stump1eb44332009-09-09 15:08:12 +00002315
John McCall0953e762009-09-24 19:53:00 +00002316 // Handle the common negative case fast.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002317 QualType CType = T->getCanonicalTypeInternal();
John McCall0953e762009-09-24 19:53:00 +00002318 if (!isa<ArrayType>(CType))
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002319 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002320
John McCall0953e762009-09-24 19:53:00 +00002321 // Apply any qualifiers from the array type to the element type. This
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002322 // implements C99 6.7.3p8: "If the specification of an array type includes
2323 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002324
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002325 // If we get here, we either have type qualifiers on the type, or we have
2326 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002327 // we must propagate them down into the element type.
Mike Stump1eb44332009-09-09 15:08:12 +00002328
John McCall0953e762009-09-24 19:53:00 +00002329 QualifierCollector Qs;
2330 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002332 // If we have a simple case, just return now.
2333 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall0953e762009-09-24 19:53:00 +00002334 if (ATy == 0 || Qs.empty())
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002335 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002337 // Otherwise, we have an array and we have qualifiers on it. Push the
2338 // qualifiers into the array element type and return a new array type.
2339 // Get the canonical version of the element with the extra qualifiers on it.
2340 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall0953e762009-09-24 19:53:00 +00002341 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002343 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2344 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2345 CAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002346 CAT->getIndexTypeCVRQualifiers()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002347 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2348 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2349 IAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002350 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002351
Mike Stump1eb44332009-09-09 15:08:12 +00002352 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002353 = dyn_cast<DependentSizedArrayType>(ATy))
2354 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002355 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002356 DSAT->getSizeExpr() ?
2357 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002358 DSAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002359 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002360 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002362 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002363 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002364 VAT->getSizeExpr() ?
John McCall0953e762009-09-24 19:53:00 +00002365 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002366 VAT->getSizeModifier(),
John McCall0953e762009-09-24 19:53:00 +00002367 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002368 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002369}
2370
2371
Chris Lattnere6327742008-04-02 05:18:44 +00002372/// getArrayDecayedType - Return the properly qualified result of decaying the
2373/// specified array type to a pointer. This operation is non-trivial when
2374/// handling typedefs etc. The canonical type of "T" must be an array type,
2375/// this returns a pointer to a properly qualified element of the array.
2376///
2377/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2378QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002379 // Get the element type with 'getAsArrayType' so that we don't lose any
2380 // typedefs in the element type of the array. This also handles propagation
2381 // of type qualifiers from the array type into the element type if present
2382 // (C99 6.7.3p8).
2383 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2384 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002386 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002387
2388 // int x[restrict 4] -> int *restrict
John McCall0953e762009-09-24 19:53:00 +00002389 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnere6327742008-04-02 05:18:44 +00002390}
2391
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002392QualType ASTContext::getBaseElementType(QualType QT) {
John McCall0953e762009-09-24 19:53:00 +00002393 QualifierCollector Qs;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002394 while (true) {
John McCall0953e762009-09-24 19:53:00 +00002395 const Type *UT = Qs.strip(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002396 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2397 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002398 } else {
John McCall0953e762009-09-24 19:53:00 +00002399 return Qs.apply(QT);
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002400 }
2401 }
2402}
2403
Daniel Dunbard786f6a2009-01-05 22:14:37 +00002404QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00002405 QualType ElemTy = VAT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002406
Anders Carlsson6183a992008-12-21 03:44:36 +00002407 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
2408 return getBaseElementType(VAT);
Mike Stump1eb44332009-09-09 15:08:12 +00002409
Anders Carlsson6183a992008-12-21 03:44:36 +00002410 return ElemTy;
2411}
2412
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002413/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002414uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002415ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2416 uint64_t ElementCount = 1;
2417 do {
2418 ElementCount *= CA->getSize().getZExtValue();
2419 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2420 } while (CA);
2421 return ElementCount;
2422}
2423
Reid Spencer5f016e22007-07-11 17:01:13 +00002424/// getFloatingRank - Return a relative rank for floating point types.
2425/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002426static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002427 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002428 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002429
John McCall183700f2009-09-21 23:43:11 +00002430 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2431 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002432 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002433 case BuiltinType::Float: return FloatRank;
2434 case BuiltinType::Double: return DoubleRank;
2435 case BuiltinType::LongDouble: return LongDoubleRank;
2436 }
2437}
2438
Mike Stump1eb44332009-09-09 15:08:12 +00002439/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2440/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002441/// 'typeDomain' is a real floating point or complex type.
2442/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002443QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2444 QualType Domain) const {
2445 FloatingRank EltRank = getFloatingRank(Size);
2446 if (Domain->isComplexType()) {
2447 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002448 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002449 case FloatRank: return FloatComplexTy;
2450 case DoubleRank: return DoubleComplexTy;
2451 case LongDoubleRank: return LongDoubleComplexTy;
2452 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002453 }
Chris Lattner1361b112008-04-06 23:58:54 +00002454
2455 assert(Domain->isRealFloatingType() && "Unknown domain!");
2456 switch (EltRank) {
2457 default: assert(0 && "getFloatingRank(): illegal value for rank");
2458 case FloatRank: return FloatTy;
2459 case DoubleRank: return DoubleTy;
2460 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002461 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002462}
2463
Chris Lattner7cfeb082008-04-06 23:55:33 +00002464/// getFloatingTypeOrder - Compare the rank of the two specified floating
2465/// point types, ignoring the domain of the type (i.e. 'double' ==
2466/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002467/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002468int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2469 FloatingRank LHSR = getFloatingRank(LHS);
2470 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002471
Chris Lattnera75cea32008-04-06 23:38:49 +00002472 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002473 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002474 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002475 return 1;
2476 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002477}
2478
Chris Lattnerf52ab252008-04-06 22:59:24 +00002479/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2480/// routine will assert if passed a built-in type that isn't an integer or enum,
2481/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002482unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002483 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002484 if (EnumType* ET = dyn_cast<EnumType>(T))
2485 T = ET->getDecl()->getIntegerType().getTypePtr();
2486
Eli Friedmana3426752009-07-05 23:44:27 +00002487 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2488 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2489
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002490 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2491 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2492
2493 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2494 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2495
Eli Friedmanf98aba32009-02-13 02:31:07 +00002496 // There are two things which impact the integer rank: the width, and
2497 // the ordering of builtins. The builtin ordering is encoded in the
2498 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002499 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002500 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002501
Chris Lattnerf52ab252008-04-06 22:59:24 +00002502 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002503 default: assert(0 && "getIntegerRank(): not a built-in integer");
2504 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002505 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002506 case BuiltinType::Char_S:
2507 case BuiltinType::Char_U:
2508 case BuiltinType::SChar:
2509 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002510 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002511 case BuiltinType::Short:
2512 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002513 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002514 case BuiltinType::Int:
2515 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002516 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002517 case BuiltinType::Long:
2518 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002519 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002520 case BuiltinType::LongLong:
2521 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002522 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002523 case BuiltinType::Int128:
2524 case BuiltinType::UInt128:
2525 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002526 }
2527}
2528
Eli Friedman04e83572009-08-20 04:21:42 +00002529/// \brief Whether this is a promotable bitfield reference according
2530/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2531///
2532/// \returns the type this bit-field will promote to, or NULL if no
2533/// promotion occurs.
2534QualType ASTContext::isPromotableBitField(Expr *E) {
2535 FieldDecl *Field = E->getBitField();
2536 if (!Field)
2537 return QualType();
2538
2539 QualType FT = Field->getType();
2540
2541 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2542 uint64_t BitWidth = BitWidthAP.getZExtValue();
2543 uint64_t IntSize = getTypeSize(IntTy);
2544 // GCC extension compatibility: if the bit-field size is less than or equal
2545 // to the size of int, it gets promoted no matter what its type is.
2546 // For instance, unsigned long bf : 4 gets promoted to signed int.
2547 if (BitWidth < IntSize)
2548 return IntTy;
2549
2550 if (BitWidth == IntSize)
2551 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2552
2553 // Types bigger than int are not subject to promotions, and therefore act
2554 // like the base type.
2555 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2556 // is ridiculous.
2557 return QualType();
2558}
2559
Eli Friedmana95d7572009-08-19 07:44:53 +00002560/// getPromotedIntegerType - Returns the type that Promotable will
2561/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2562/// integer type.
2563QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2564 assert(!Promotable.isNull());
2565 assert(Promotable->isPromotableIntegerType());
2566 if (Promotable->isSignedIntegerType())
2567 return IntTy;
2568 uint64_t PromotableSize = getTypeSize(Promotable);
2569 uint64_t IntSize = getTypeSize(IntTy);
2570 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2571 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2572}
2573
Mike Stump1eb44332009-09-09 15:08:12 +00002574/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002575/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002576/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002577int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002578 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2579 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002580 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Chris Lattnerf52ab252008-04-06 22:59:24 +00002582 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2583 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Chris Lattner7cfeb082008-04-06 23:55:33 +00002585 unsigned LHSRank = getIntegerRank(LHSC);
2586 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Chris Lattner7cfeb082008-04-06 23:55:33 +00002588 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2589 if (LHSRank == RHSRank) return 0;
2590 return LHSRank > RHSRank ? 1 : -1;
2591 }
Mike Stump1eb44332009-09-09 15:08:12 +00002592
Chris Lattner7cfeb082008-04-06 23:55:33 +00002593 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2594 if (LHSUnsigned) {
2595 // If the unsigned [LHS] type is larger, return it.
2596 if (LHSRank >= RHSRank)
2597 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Chris Lattner7cfeb082008-04-06 23:55:33 +00002599 // If the signed type can represent all values of the unsigned type, it
2600 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002601 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002602 return -1;
2603 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002604
Chris Lattner7cfeb082008-04-06 23:55:33 +00002605 // If the unsigned [RHS] type is larger, return it.
2606 if (RHSRank >= LHSRank)
2607 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Chris Lattner7cfeb082008-04-06 23:55:33 +00002609 // If the signed type can represent all values of the unsigned type, it
2610 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002611 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002612 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002613}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002614
Mike Stump1eb44332009-09-09 15:08:12 +00002615// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002616QualType ASTContext::getCFConstantStringType() {
2617 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002618 CFConstantStringTypeDecl =
2619 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002620 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002621 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Anders Carlsson71993dd2007-08-17 05:31:46 +00002623 // const int *isa;
John McCall0953e762009-09-24 19:53:00 +00002624 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlssonf06273f2007-11-19 00:25:30 +00002625 // int flags;
2626 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002627 // const char *str;
John McCall0953e762009-09-24 19:53:00 +00002628 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson71993dd2007-08-17 05:31:46 +00002629 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002630 FieldTypes[3] = LongTy;
2631
Anders Carlsson71993dd2007-08-17 05:31:46 +00002632 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002633 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002634 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002635 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002636 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002637 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002638 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002639 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002640 }
2641
2642 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002643 }
Mike Stump1eb44332009-09-09 15:08:12 +00002644
Anders Carlsson71993dd2007-08-17 05:31:46 +00002645 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002646}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002647
Douglas Gregor319ac892009-04-23 22:29:11 +00002648void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002649 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002650 assert(Rec && "Invalid CFConstantStringType");
2651 CFConstantStringTypeDecl = Rec->getDecl();
2652}
2653
Mike Stump1eb44332009-09-09 15:08:12 +00002654QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002655 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002656 ObjCFastEnumerationStateTypeDecl =
2657 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2658 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002660 QualType FieldTypes[] = {
2661 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002662 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002663 getPointerType(UnsignedLongTy),
2664 getConstantArrayType(UnsignedLongTy,
2665 llvm::APInt(32, 5), ArrayType::Normal, 0)
2666 };
Mike Stump1eb44332009-09-09 15:08:12 +00002667
Douglas Gregor44b43212008-12-11 16:49:14 +00002668 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002669 FieldDecl *Field = FieldDecl::Create(*this,
2670 ObjCFastEnumerationStateTypeDecl,
2671 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002672 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002673 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002674 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002675 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002676 }
Mike Stump1eb44332009-09-09 15:08:12 +00002677
Douglas Gregor44b43212008-12-11 16:49:14 +00002678 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002679 }
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002681 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2682}
2683
Douglas Gregor319ac892009-04-23 22:29:11 +00002684void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002685 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002686 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2687 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2688}
2689
Anders Carlssone8c49532007-10-29 06:33:42 +00002690// This returns true if a type has been typedefed to BOOL:
2691// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002692static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002693 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002694 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2695 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002697 return false;
2698}
2699
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002700/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002701/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002702int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002703 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002705 // Make all integer and enum types at least as large as an int
2706 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002707 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002708 // Treat arrays as pointers, since that's how they're passed in.
2709 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002710 sz = getTypeSize(VoidPtrTy);
2711 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002712}
2713
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002714/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002715/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002716void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002717 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002718 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002719 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002720 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002721 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002722 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002723 // Compute size of all parameters.
2724 // Start with computing size of a pointer in number of bytes.
2725 // FIXME: There might(should) be a better way of doing this computation!
2726 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002727 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002728 // The first two arguments (self and _cmd) are pointers; account for
2729 // their size.
2730 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002731 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2732 E = Decl->param_end(); PI != E; ++PI) {
2733 QualType PType = (*PI)->getType();
2734 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002735 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002736 ParmOffset += sz;
2737 }
2738 S += llvm::utostr(ParmOffset);
2739 S += "@0:";
2740 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002741
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002742 // Argument types.
2743 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002744 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2745 E = Decl->param_end(); PI != E; ++PI) {
2746 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00002747 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002748 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002749 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2750 // Use array's original type only if it has known number of
2751 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002752 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002753 PType = PVDecl->getType();
2754 } else if (PType->isFunctionType())
2755 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002756 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002757 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002758 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002759 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002760 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002761 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002762 }
2763}
2764
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002765/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002766/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002767/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2768/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00002769/// Property attributes are stored as a comma-delimited C string. The simple
2770/// attributes readonly and bycopy are encoded as single characters. The
2771/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2772/// encoded as single characters, followed by an identifier. Property types
2773/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002774/// these attributes are defined by the following enumeration:
2775/// @code
2776/// enum PropertyAttributes {
2777/// kPropertyReadOnly = 'R', // property is read-only.
2778/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2779/// kPropertyByref = '&', // property is a reference to the value last assigned
2780/// kPropertyDynamic = 'D', // property is dynamic
2781/// kPropertyGetter = 'G', // followed by getter selector name
2782/// kPropertySetter = 'S', // followed by setter selector name
2783/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2784/// kPropertyType = 't' // followed by old-style type encoding.
2785/// kPropertyWeak = 'W' // 'weak' property
2786/// kPropertyStrong = 'P' // property GC'able
2787/// kPropertyNonAtomic = 'N' // property non-atomic
2788/// };
2789/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00002790void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002791 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002792 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002793 // Collect information from the property implementation decl(s).
2794 bool Dynamic = false;
2795 ObjCPropertyImplDecl *SynthesizePID = 0;
2796
2797 // FIXME: Duplicated code due to poor abstraction.
2798 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00002799 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002800 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2801 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002802 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002803 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002804 ObjCPropertyImplDecl *PID = *i;
2805 if (PID->getPropertyDecl() == PD) {
2806 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2807 Dynamic = true;
2808 } else {
2809 SynthesizePID = PID;
2810 }
2811 }
2812 }
2813 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002814 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002815 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002816 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002817 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002818 ObjCPropertyImplDecl *PID = *i;
2819 if (PID->getPropertyDecl() == PD) {
2820 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2821 Dynamic = true;
2822 } else {
2823 SynthesizePID = PID;
2824 }
2825 }
Mike Stump1eb44332009-09-09 15:08:12 +00002826 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002827 }
2828 }
2829
2830 // FIXME: This is not very efficient.
2831 S = "T";
2832
2833 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002834 // GCC has some special rules regarding encoding of properties which
2835 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00002836 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002837 true /* outermost type */,
2838 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002839
2840 if (PD->isReadOnly()) {
2841 S += ",R";
2842 } else {
2843 switch (PD->getSetterKind()) {
2844 case ObjCPropertyDecl::Assign: break;
2845 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002846 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002847 }
2848 }
2849
2850 // It really isn't clear at all what this means, since properties
2851 // are "dynamic by default".
2852 if (Dynamic)
2853 S += ",D";
2854
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002855 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2856 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00002857
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002858 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2859 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002860 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002861 }
2862
2863 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2864 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002865 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002866 }
2867
2868 if (SynthesizePID) {
2869 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2870 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002871 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002872 }
2873
2874 // FIXME: OBJCGC: weak & strong
2875}
2876
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002877/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00002878/// Another legacy compatibility encoding: 32-bit longs are encoded as
2879/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002880/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2881///
2882void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00002883 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00002884 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002885 if (BT->getKind() == BuiltinType::ULong &&
2886 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002887 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002888 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002889 if (BT->getKind() == BuiltinType::Long &&
2890 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002891 PointeeTy = IntTy;
2892 }
2893 }
2894}
2895
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002896void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002897 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002898 // We follow the behavior of gcc, expanding structures which are
2899 // directly pointed to, and expanding embedded structures. Note that
2900 // these rules are sufficient to prevent recursive encoding of the
2901 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00002902 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002903 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002904}
2905
Mike Stump1eb44332009-09-09 15:08:12 +00002906static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002907 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002908 const Expr *E = FD->getBitWidth();
2909 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2910 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002911 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002912 S += 'b';
2913 S += llvm::utostr(N);
2914}
2915
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002916void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2917 bool ExpandPointedToStructures,
2918 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002919 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002920 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002921 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00002922 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002923 if (FD && FD->isBitField())
2924 return EncodeBitField(this, S, FD);
2925 char encoding;
2926 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002927 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002928 case BuiltinType::Void: encoding = 'v'; break;
2929 case BuiltinType::Bool: encoding = 'B'; break;
2930 case BuiltinType::Char_U:
2931 case BuiltinType::UChar: encoding = 'C'; break;
2932 case BuiltinType::UShort: encoding = 'S'; break;
2933 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002934 case BuiltinType::ULong:
2935 encoding =
2936 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002937 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002938 case BuiltinType::UInt128: encoding = 'T'; break;
2939 case BuiltinType::ULongLong: encoding = 'Q'; break;
2940 case BuiltinType::Char_S:
2941 case BuiltinType::SChar: encoding = 'c'; break;
2942 case BuiltinType::Short: encoding = 's'; break;
2943 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002944 case BuiltinType::Long:
2945 encoding =
2946 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002947 break;
2948 case BuiltinType::LongLong: encoding = 'q'; break;
2949 case BuiltinType::Int128: encoding = 't'; break;
2950 case BuiltinType::Float: encoding = 'f'; break;
2951 case BuiltinType::Double: encoding = 'd'; break;
2952 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002953 }
Mike Stump1eb44332009-09-09 15:08:12 +00002954
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002955 S += encoding;
2956 return;
2957 }
Mike Stump1eb44332009-09-09 15:08:12 +00002958
John McCall183700f2009-09-21 23:43:11 +00002959 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002960 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00002961 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002962 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002963 return;
2964 }
Mike Stump1eb44332009-09-09 15:08:12 +00002965
Ted Kremenek6217b802009-07-29 21:53:49 +00002966 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002967 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002968 bool isReadOnly = false;
2969 // For historical/compatibility reasons, the read-only qualifier of the
2970 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2971 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00002972 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00002973 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002974 if (OutermostType && T.isConstQualified()) {
2975 isReadOnly = true;
2976 S += 'r';
2977 }
Mike Stump9fdbab32009-07-31 02:02:20 +00002978 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002979 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00002980 while (P->getAs<PointerType>())
2981 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002982 if (P.isConstQualified()) {
2983 isReadOnly = true;
2984 S += 'r';
2985 }
2986 }
2987 if (isReadOnly) {
2988 // Another legacy compatibility encoding. Some ObjC qualifier and type
2989 // combinations need to be rearranged.
2990 // Rewrite "in const" from "nr" to "rn"
2991 const char * s = S.c_str();
2992 int len = S.length();
2993 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2994 std::string replace = "rn";
2995 S.replace(S.end()-2, S.end(), replace);
2996 }
2997 }
Steve Naroff14108da2009-07-10 23:34:53 +00002998 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002999 S += ':';
3000 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00003001 }
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003003 if (PointeeTy->isCharType()) {
3004 // char pointer types should be encoded as '*' unless it is a
3005 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003006 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003007 S += '*';
3008 return;
3009 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003010 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003011 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3012 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3013 S += '#';
3014 return;
3015 }
3016 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3017 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3018 S += '@';
3019 return;
3020 }
3021 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003022 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003023 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003024 getLegacyIntegralTypeEncoding(PointeeTy);
3025
Mike Stump1eb44332009-09-09 15:08:12 +00003026 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003027 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003028 return;
3029 }
Mike Stump1eb44332009-09-09 15:08:12 +00003030
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003031 if (const ArrayType *AT =
3032 // Ignore type qualifiers etc.
3033 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003034 if (isa<IncompleteArrayType>(AT)) {
3035 // Incomplete arrays are encoded as a pointer to the array element.
3036 S += '^';
3037
Mike Stump1eb44332009-09-09 15:08:12 +00003038 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003039 false, ExpandStructures, FD);
3040 } else {
3041 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003042
Anders Carlsson559a8332009-02-22 01:38:57 +00003043 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3044 S += llvm::utostr(CAT->getSize().getZExtValue());
3045 else {
3046 //Variable length arrays are encoded as a regular array with 0 elements.
3047 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3048 S += '0';
3049 }
Mike Stump1eb44332009-09-09 15:08:12 +00003050
3051 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003052 false, ExpandStructures, FD);
3053 S += ']';
3054 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003055 return;
3056 }
Mike Stump1eb44332009-09-09 15:08:12 +00003057
John McCall183700f2009-09-21 23:43:11 +00003058 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003059 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003060 return;
3061 }
Mike Stump1eb44332009-09-09 15:08:12 +00003062
Ted Kremenek6217b802009-07-29 21:53:49 +00003063 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003064 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003065 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003066 // Anonymous structures print as '?'
3067 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3068 S += II->getName();
3069 } else {
3070 S += '?';
3071 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003072 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003073 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003074 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3075 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003076 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003077 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003078 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003079 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003080 S += '"';
3081 }
Mike Stump1eb44332009-09-09 15:08:12 +00003082
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003083 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003084 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003085 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003086 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003087 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003088 QualType qt = Field->getType();
3089 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003090 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003091 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003092 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003093 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003094 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003095 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003096 return;
3097 }
Mike Stump1eb44332009-09-09 15:08:12 +00003098
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003099 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003100 if (FD && FD->isBitField())
3101 EncodeBitField(this, S, FD);
3102 else
3103 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003104 return;
3105 }
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003107 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003108 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003109 return;
3110 }
Mike Stump1eb44332009-09-09 15:08:12 +00003111
John McCall0953e762009-09-24 19:53:00 +00003112 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003113 // @encode(class_name)
John McCall0953e762009-09-24 19:53:00 +00003114 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003115 S += '{';
3116 const IdentifierInfo *II = OI->getIdentifier();
3117 S += II->getName();
3118 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003119 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003120 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003121 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003122 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003123 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003124 RecFields[i]);
3125 else
Mike Stump1eb44332009-09-09 15:08:12 +00003126 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003127 FD);
3128 }
3129 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003130 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003131 }
Mike Stump1eb44332009-09-09 15:08:12 +00003132
John McCall183700f2009-09-21 23:43:11 +00003133 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003134 if (OPT->isObjCIdType()) {
3135 S += '@';
3136 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003137 }
Mike Stump1eb44332009-09-09 15:08:12 +00003138
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003139 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003140 S += '#';
3141 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003142 }
Mike Stump1eb44332009-09-09 15:08:12 +00003143
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003144 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003145 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003146 ExpandPointedToStructures,
3147 ExpandStructures, FD);
3148 if (FD || EncodingProperty) {
3149 // Note that we do extended encoding of protocol qualifer list
3150 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003151 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003152 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3153 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003154 S += '<';
3155 S += (*I)->getNameAsString();
3156 S += '>';
3157 }
3158 S += '"';
3159 }
3160 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003161 }
Mike Stump1eb44332009-09-09 15:08:12 +00003162
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003163 QualType PointeeTy = OPT->getPointeeType();
3164 if (!EncodingProperty &&
3165 isa<TypedefType>(PointeeTy.getTypePtr())) {
3166 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003167 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003168 // {...};
3169 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003170 getObjCEncodingForTypeImpl(PointeeTy, S,
3171 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003172 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003173 return;
3174 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003175
3176 S += '@';
3177 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003178 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003179 S += OPT->getInterfaceDecl()->getNameAsCString();
3180 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3181 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003182 S += '<';
3183 S += (*I)->getNameAsString();
3184 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003185 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003186 S += '"';
3187 }
3188 return;
3189 }
Mike Stump1eb44332009-09-09 15:08:12 +00003190
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003191 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003192}
3193
Mike Stump1eb44332009-09-09 15:08:12 +00003194void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003195 std::string& S) const {
3196 if (QT & Decl::OBJC_TQ_In)
3197 S += 'n';
3198 if (QT & Decl::OBJC_TQ_Inout)
3199 S += 'N';
3200 if (QT & Decl::OBJC_TQ_Out)
3201 S += 'o';
3202 if (QT & Decl::OBJC_TQ_Bycopy)
3203 S += 'O';
3204 if (QT & Decl::OBJC_TQ_Byref)
3205 S += 'R';
3206 if (QT & Decl::OBJC_TQ_Oneway)
3207 S += 'V';
3208}
3209
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003210void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003211 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003212
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003213 BuiltinVaListType = T;
3214}
3215
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003216void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003217 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003218}
3219
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003220void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003221 ObjCSelType = T;
3222
John McCall183700f2009-09-21 23:43:11 +00003223 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003224 if (!TT)
3225 return;
3226 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003227
3228 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003229 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003230 if (!ptr)
3231 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003232 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003233 if (!rec)
3234 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003235 SelStructType = rec;
3236}
3237
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003238void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003239 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003240}
3241
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003242void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003243 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003244}
3245
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003246void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003247 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003248 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003249
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003250 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003251}
3252
Douglas Gregor7532dc62009-03-30 22:58:21 +00003253/// \brief Retrieve the template name that represents a qualified
3254/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003255TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003256 bool TemplateKeyword,
3257 TemplateDecl *Template) {
3258 llvm::FoldingSetNodeID ID;
3259 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3260
3261 void *InsertPos = 0;
3262 QualifiedTemplateName *QTN =
3263 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3264 if (!QTN) {
3265 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3266 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3267 }
3268
3269 return TemplateName(QTN);
3270}
3271
Douglas Gregord99cbe62009-07-29 18:26:50 +00003272/// \brief Retrieve the template name that represents a qualified
3273/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003274TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003275 bool TemplateKeyword,
3276 OverloadedFunctionDecl *Template) {
3277 llvm::FoldingSetNodeID ID;
3278 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003279
Douglas Gregord99cbe62009-07-29 18:26:50 +00003280 void *InsertPos = 0;
3281 QualifiedTemplateName *QTN =
3282 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3283 if (!QTN) {
3284 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3285 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3286 }
Mike Stump1eb44332009-09-09 15:08:12 +00003287
Douglas Gregord99cbe62009-07-29 18:26:50 +00003288 return TemplateName(QTN);
3289}
3290
Douglas Gregor7532dc62009-03-30 22:58:21 +00003291/// \brief Retrieve the template name that represents a dependent
3292/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003293TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003294 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003295 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003296 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003297
3298 llvm::FoldingSetNodeID ID;
3299 DependentTemplateName::Profile(ID, NNS, Name);
3300
3301 void *InsertPos = 0;
3302 DependentTemplateName *QTN =
3303 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3304
3305 if (QTN)
3306 return TemplateName(QTN);
3307
3308 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3309 if (CanonNNS == NNS) {
3310 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3311 } else {
3312 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3313 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3314 }
3315
3316 DependentTemplateNames.InsertNode(QTN, InsertPos);
3317 return TemplateName(QTN);
3318}
3319
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003320/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003321/// TargetInfo, produce the corresponding type. The unsigned @p Type
3322/// is actually a value of type @c TargetInfo::IntType.
3323QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003324 switch (Type) {
Mike Stump1eb44332009-09-09 15:08:12 +00003325 case TargetInfo::NoInt: return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003326 case TargetInfo::SignedShort: return ShortTy;
3327 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3328 case TargetInfo::SignedInt: return IntTy;
3329 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3330 case TargetInfo::SignedLong: return LongTy;
3331 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3332 case TargetInfo::SignedLongLong: return LongLongTy;
3333 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3334 }
3335
3336 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003337 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003338}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003339
3340//===----------------------------------------------------------------------===//
3341// Type Predicates.
3342//===----------------------------------------------------------------------===//
3343
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003344/// isObjCNSObjectType - Return true if this is an NSObject object using
3345/// NSObject attribute on a c-style pointer type.
3346/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003347/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003348///
3349bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3350 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3351 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003352 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003353 return true;
3354 }
Mike Stump1eb44332009-09-09 15:08:12 +00003355 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003356}
3357
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003358/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3359/// garbage collection attribute.
3360///
John McCall0953e762009-09-24 19:53:00 +00003361Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3362 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003363 if (getLangOptions().ObjC1 &&
3364 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003365 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003366 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003367 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003368 // as __strong.
John McCall0953e762009-09-24 19:53:00 +00003369 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003370 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003371 GCAttrs = Qualifiers::Strong;
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003372 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003373 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003374 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003375 // Non-pointers have none gc'able attribute regardless of the attribute
3376 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003377 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003378 return Qualifiers::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003379 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003380 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003381}
3382
Chris Lattner6ac46a42008-04-07 06:51:04 +00003383//===----------------------------------------------------------------------===//
3384// Type Compatibility Testing
3385//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003386
Mike Stump1eb44332009-09-09 15:08:12 +00003387/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003388/// compatible.
3389static bool areCompatVectorTypes(const VectorType *LHS,
3390 const VectorType *RHS) {
3391 assert(LHS->isCanonical() && RHS->isCanonical());
3392 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003393 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003394}
3395
Steve Naroff4084c302009-07-23 01:01:38 +00003396//===----------------------------------------------------------------------===//
3397// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3398//===----------------------------------------------------------------------===//
3399
3400/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3401/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003402bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3403 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003404 if (lProto == rProto)
3405 return true;
3406 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3407 E = rProto->protocol_end(); PI != E; ++PI)
3408 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3409 return true;
3410 return false;
3411}
3412
Steve Naroff4084c302009-07-23 01:01:38 +00003413/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3414/// return true if lhs's protocols conform to rhs's protocol; false
3415/// otherwise.
3416bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3417 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3418 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3419 return false;
3420}
3421
3422/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3423/// ObjCQualifiedIDType.
3424bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3425 bool compare) {
3426 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003427 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003428 lhs->isObjCIdType() || lhs->isObjCClassType())
3429 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003430 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003431 rhs->isObjCIdType() || rhs->isObjCClassType())
3432 return true;
3433
3434 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003435 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003436
Steve Naroff4084c302009-07-23 01:01:38 +00003437 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003438
Steve Naroff4084c302009-07-23 01:01:38 +00003439 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003440 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003441 // make sure we check the class hierarchy.
3442 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3443 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3444 E = lhsQID->qual_end(); I != E; ++I) {
3445 // when comparing an id<P> on lhs with a static type on rhs,
3446 // see if static class implements all of id's protocols, directly or
3447 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003448 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003449 return false;
3450 }
3451 }
3452 // If there are no qualifiers and no interface, we have an 'id'.
3453 return true;
3454 }
Mike Stump1eb44332009-09-09 15:08:12 +00003455 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003456 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3457 E = lhsQID->qual_end(); I != E; ++I) {
3458 ObjCProtocolDecl *lhsProto = *I;
3459 bool match = false;
3460
3461 // when comparing an id<P> on lhs with a static type on rhs,
3462 // see if static class implements all of id's protocols, directly or
3463 // through its super class and categories.
3464 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3465 E = rhsOPT->qual_end(); J != E; ++J) {
3466 ObjCProtocolDecl *rhsProto = *J;
3467 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3468 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3469 match = true;
3470 break;
3471 }
3472 }
Mike Stump1eb44332009-09-09 15:08:12 +00003473 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003474 // make sure we check the class hierarchy.
3475 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3476 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3477 E = lhsQID->qual_end(); I != E; ++I) {
3478 // when comparing an id<P> on lhs with a static type on rhs,
3479 // see if static class implements all of id's protocols, directly or
3480 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003481 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003482 match = true;
3483 break;
3484 }
3485 }
3486 }
3487 if (!match)
3488 return false;
3489 }
Mike Stump1eb44332009-09-09 15:08:12 +00003490
Steve Naroff4084c302009-07-23 01:01:38 +00003491 return true;
3492 }
Mike Stump1eb44332009-09-09 15:08:12 +00003493
Steve Naroff4084c302009-07-23 01:01:38 +00003494 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3495 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3496
Mike Stump1eb44332009-09-09 15:08:12 +00003497 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003498 lhs->getAsObjCInterfacePointerType()) {
3499 if (lhsOPT->qual_empty()) {
3500 bool match = false;
3501 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3502 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3503 E = rhsQID->qual_end(); I != E; ++I) {
3504 // when comparing an id<P> on lhs with a static type on rhs,
3505 // see if static class implements all of id's protocols, directly or
3506 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003507 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003508 match = true;
3509 break;
3510 }
3511 }
3512 if (!match)
3513 return false;
3514 }
3515 return true;
3516 }
Mike Stump1eb44332009-09-09 15:08:12 +00003517 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003518 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3519 E = lhsOPT->qual_end(); I != E; ++I) {
3520 ObjCProtocolDecl *lhsProto = *I;
3521 bool match = false;
3522
3523 // when comparing an id<P> on lhs with a static type on rhs,
3524 // see if static class implements all of id's protocols, directly or
3525 // through its super class and categories.
3526 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3527 E = rhsQID->qual_end(); J != E; ++J) {
3528 ObjCProtocolDecl *rhsProto = *J;
3529 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3530 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3531 match = true;
3532 break;
3533 }
3534 }
3535 if (!match)
3536 return false;
3537 }
3538 return true;
3539 }
3540 return false;
3541}
3542
Eli Friedman3d815e72008-08-22 00:56:42 +00003543/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003544/// compatible for assignment from RHS to LHS. This handles validation of any
3545/// protocol qualifiers on the LHS or RHS.
3546///
Steve Naroff14108da2009-07-10 23:34:53 +00003547bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3548 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003549 // If either type represents the built-in 'id' or 'Class' types, return true.
3550 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003551 return true;
3552
Steve Naroff4084c302009-07-23 01:01:38 +00003553 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003554 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3555 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003556 false);
3557
Steve Naroff14108da2009-07-10 23:34:53 +00003558 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3559 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003560 if (LHS && RHS) // We have 2 user-defined types.
3561 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003562
Steve Naroff4084c302009-07-23 01:01:38 +00003563 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003564}
3565
Eli Friedman3d815e72008-08-22 00:56:42 +00003566bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3567 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003568 // Verify that the base decls are compatible: the RHS must be a subclass of
3569 // the LHS.
3570 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3571 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003572
Chris Lattner6ac46a42008-04-07 06:51:04 +00003573 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3574 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003575 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003576 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003577
Chris Lattner6ac46a42008-04-07 06:51:04 +00003578 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3579 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003580 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003581 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003582
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003583 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3584 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003585 LHSPI != LHSPE; LHSPI++) {
3586 bool RHSImplementsProtocol = false;
3587
3588 // If the RHS doesn't implement the protocol on the left, the types
3589 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003590 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003591 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003592 RHSPI != RHSPE; RHSPI++) {
3593 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003594 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003595 break;
3596 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003597 }
3598 // FIXME: For better diagnostics, consider passing back the protocol name.
3599 if (!RHSImplementsProtocol)
3600 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003601 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003602 // The RHS implements all protocols listed on the LHS.
3603 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003604}
3605
Steve Naroff389bf462009-02-12 17:52:19 +00003606bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3607 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00003608 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3609 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003610
Steve Naroff14108da2009-07-10 23:34:53 +00003611 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003612 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003613
3614 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3615 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003616}
3617
Mike Stump1eb44332009-09-09 15:08:12 +00003618/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003619/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003620/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003621/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003622bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3623 return !mergeTypes(LHS, RHS).isNull();
3624}
3625
3626QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00003627 const FunctionType *lbase = lhs->getAs<FunctionType>();
3628 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00003629 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3630 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003631 bool allLTypes = true;
3632 bool allRTypes = true;
3633
3634 // Check return type
3635 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3636 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003637 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3638 allLTypes = false;
3639 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3640 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003641 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003642 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3643 if (NoReturn != lbase->getNoReturnAttr())
3644 allLTypes = false;
3645 if (NoReturn != rbase->getNoReturnAttr())
3646 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003647
Eli Friedman3d815e72008-08-22 00:56:42 +00003648 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003649 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3650 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003651 unsigned lproto_nargs = lproto->getNumArgs();
3652 unsigned rproto_nargs = rproto->getNumArgs();
3653
3654 // Compatible functions must have the same number of arguments
3655 if (lproto_nargs != rproto_nargs)
3656 return QualType();
3657
3658 // Variadic and non-variadic functions aren't compatible
3659 if (lproto->isVariadic() != rproto->isVariadic())
3660 return QualType();
3661
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003662 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3663 return QualType();
3664
Eli Friedman3d815e72008-08-22 00:56:42 +00003665 // Check argument compatibility
3666 llvm::SmallVector<QualType, 10> types;
3667 for (unsigned i = 0; i < lproto_nargs; i++) {
3668 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3669 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3670 QualType argtype = mergeTypes(largtype, rargtype);
3671 if (argtype.isNull()) return QualType();
3672 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003673 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3674 allLTypes = false;
3675 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3676 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003677 }
3678 if (allLTypes) return lhs;
3679 if (allRTypes) return rhs;
3680 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003681 lproto->isVariadic(), lproto->getTypeQuals(),
3682 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003683 }
3684
3685 if (lproto) allRTypes = false;
3686 if (rproto) allLTypes = false;
3687
Douglas Gregor72564e72009-02-26 23:50:07 +00003688 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003689 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003690 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003691 if (proto->isVariadic()) return QualType();
3692 // Check that the types are compatible with the types that
3693 // would result from default argument promotions (C99 6.7.5.3p15).
3694 // The only types actually affected are promotable integer
3695 // types and floats, which would be passed as a different
3696 // type depending on whether the prototype is visible.
3697 unsigned proto_nargs = proto->getNumArgs();
3698 for (unsigned i = 0; i < proto_nargs; ++i) {
3699 QualType argTy = proto->getArgType(i);
3700 if (argTy->isPromotableIntegerType() ||
3701 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3702 return QualType();
3703 }
3704
3705 if (allLTypes) return lhs;
3706 if (allRTypes) return rhs;
3707 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003708 proto->getNumArgs(), proto->isVariadic(),
3709 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003710 }
3711
3712 if (allLTypes) return lhs;
3713 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003714 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003715}
3716
3717QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003718 // C++ [expr]: If an expression initially has the type "reference to T", the
3719 // type is adjusted to "T" prior to any further analysis, the expression
3720 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003721 // expression is an lvalue unless the reference is an rvalue reference and
3722 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003723 // FIXME: C++ shouldn't be going through here! The rules are different
3724 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003725 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3726 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00003727 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003728 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00003729 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003730 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003731
Eli Friedman3d815e72008-08-22 00:56:42 +00003732 QualType LHSCan = getCanonicalType(LHS),
3733 RHSCan = getCanonicalType(RHS);
3734
3735 // If two types are identical, they are compatible.
3736 if (LHSCan == RHSCan)
3737 return LHS;
3738
John McCall0953e762009-09-24 19:53:00 +00003739 // If the qualifiers are different, the types aren't compatible... mostly.
3740 Qualifiers LQuals = LHSCan.getQualifiers();
3741 Qualifiers RQuals = RHSCan.getQualifiers();
3742 if (LQuals != RQuals) {
3743 // If any of these qualifiers are different, we have a type
3744 // mismatch.
3745 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
3746 LQuals.getAddressSpace() != RQuals.getAddressSpace())
3747 return QualType();
3748
3749 // Exactly one GC qualifier difference is allowed: __strong is
3750 // okay if the other type has no GC qualifier but is an Objective
3751 // C object pointer (i.e. implicitly strong by default). We fix
3752 // this by pretending that the unqualified type was actually
3753 // qualified __strong.
3754 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
3755 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
3756 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
3757
3758 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
3759 return QualType();
3760
3761 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
3762 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
3763 }
3764 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
3765 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
3766 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003767 return QualType();
John McCall0953e762009-09-24 19:53:00 +00003768 }
3769
3770 // Okay, qualifiers are equal.
Eli Friedman3d815e72008-08-22 00:56:42 +00003771
Eli Friedman852d63b2009-06-01 01:22:52 +00003772 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3773 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003774
Chris Lattner1adb8832008-01-14 05:45:46 +00003775 // We want to consider the two function types to be the same for these
3776 // comparisons, just force one to the other.
3777 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3778 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003779
3780 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003781 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3782 LHSClass = Type::ConstantArray;
3783 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3784 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00003785
Nate Begeman213541a2008-04-18 23:10:10 +00003786 // Canonicalize ExtVector -> Vector.
3787 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3788 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00003789
Chris Lattnera36a61f2008-04-07 05:43:21 +00003790 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003791 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003792 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00003793 // a signed integer type, or an unsigned integer type.
John McCall183700f2009-09-21 23:43:11 +00003794 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003795 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3796 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003797 }
John McCall183700f2009-09-21 23:43:11 +00003798 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003799 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3800 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003801 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003802
Eli Friedman3d815e72008-08-22 00:56:42 +00003803 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003804 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003805
Steve Naroff4a746782008-01-09 22:43:08 +00003806 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003807 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003808#define TYPE(Class, Base)
3809#define ABSTRACT_TYPE(Class, Base)
3810#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3811#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3812#include "clang/AST/TypeNodes.def"
3813 assert(false && "Non-canonical and dependent types shouldn't get here");
3814 return QualType();
3815
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003816 case Type::LValueReference:
3817 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003818 case Type::MemberPointer:
3819 assert(false && "C++ should never be in mergeTypes");
3820 return QualType();
3821
3822 case Type::IncompleteArray:
3823 case Type::VariableArray:
3824 case Type::FunctionProto:
3825 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00003826 assert(false && "Types are eliminated above");
3827 return QualType();
3828
Chris Lattner1adb8832008-01-14 05:45:46 +00003829 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003830 {
3831 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003832 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
3833 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003834 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3835 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003836 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003837 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003838 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003839 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003840 return getPointerType(ResultType);
3841 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003842 case Type::BlockPointer:
3843 {
3844 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003845 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
3846 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00003847 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3848 if (ResultType.isNull()) return QualType();
3849 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3850 return LHS;
3851 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3852 return RHS;
3853 return getBlockPointerType(ResultType);
3854 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003855 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003856 {
3857 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3858 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3859 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3860 return QualType();
3861
3862 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3863 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3864 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3865 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003866 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3867 return LHS;
3868 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3869 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003870 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3871 ArrayType::ArraySizeModifier(), 0);
3872 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3873 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003874 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3875 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003876 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3877 return LHS;
3878 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3879 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003880 if (LVAT) {
3881 // FIXME: This isn't correct! But tricky to implement because
3882 // the array's size has to be the size of LHS, but the type
3883 // has to be different.
3884 return LHS;
3885 }
3886 if (RVAT) {
3887 // FIXME: This isn't correct! But tricky to implement because
3888 // the array's size has to be the size of RHS, but the type
3889 // has to be different.
3890 return RHS;
3891 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003892 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3893 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003894 return getIncompleteArrayType(ResultType,
3895 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003896 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003897 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003898 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003899 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003900 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003901 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003902 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003903 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003904 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003905 case Type::Complex:
3906 // Distinct complex types are incompatible.
3907 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003908 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003909 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00003910 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00003911 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003912 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003913 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003914 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003915 // FIXME: This should be type compatibility, e.g. whether
3916 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00003917 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
3918 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00003919 if (LHSIface && RHSIface &&
3920 canAssignObjCInterfaces(LHSIface, RHSIface))
3921 return LHS;
3922
Eli Friedman3d815e72008-08-22 00:56:42 +00003923 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003924 }
Steve Naroff14108da2009-07-10 23:34:53 +00003925 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00003926 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
3927 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00003928 return LHS;
3929
Steve Naroffbc76dd02008-12-10 22:14:21 +00003930 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00003931 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003932 case Type::FixedWidthInt:
3933 // Distinct fixed-width integers are not compatible.
3934 return QualType();
Douglas Gregor7532dc62009-03-30 22:58:21 +00003935 case Type::TemplateSpecialization:
3936 assert(false && "Dependent types have no size");
3937 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003938 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003939
3940 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003941}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003942
Chris Lattner5426bf62008-04-07 07:01:58 +00003943//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003944// Integer Predicates
3945//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003946
Eli Friedmanad74a752008-06-28 06:23:08 +00003947unsigned ASTContext::getIntWidth(QualType T) {
3948 if (T == BoolTy)
3949 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003950 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3951 return FWIT->getWidth();
3952 }
3953 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003954 return (unsigned)getTypeSize(T);
3955}
3956
3957QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3958 assert(T->isSignedIntegerType() && "Unexpected type");
John McCall183700f2009-09-21 23:43:11 +00003959 if (const EnumType* ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00003960 T = ETy->getDecl()->getIntegerType();
John McCall183700f2009-09-21 23:43:11 +00003961 const BuiltinType* BTy = T->getAs<BuiltinType>();
Eli Friedmanad74a752008-06-28 06:23:08 +00003962 assert (BTy && "Unexpected signed integer type");
3963 switch (BTy->getKind()) {
3964 case BuiltinType::Char_S:
3965 case BuiltinType::SChar:
3966 return UnsignedCharTy;
3967 case BuiltinType::Short:
3968 return UnsignedShortTy;
3969 case BuiltinType::Int:
3970 return UnsignedIntTy;
3971 case BuiltinType::Long:
3972 return UnsignedLongTy;
3973 case BuiltinType::LongLong:
3974 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003975 case BuiltinType::Int128:
3976 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003977 default:
3978 assert(0 && "Unexpected signed integer type");
3979 return QualType();
3980 }
3981}
3982
Douglas Gregor2cf26342009-04-09 22:27:44 +00003983ExternalASTSource::~ExternalASTSource() { }
3984
3985void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00003986
3987
3988//===----------------------------------------------------------------------===//
3989// Builtin Type Computation
3990//===----------------------------------------------------------------------===//
3991
3992/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
3993/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00003994static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00003995 ASTContext::GetBuiltinTypeError &Error,
3996 bool AllowTypeModifiers = true) {
3997 // Modifiers.
3998 int HowLong = 0;
3999 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004000
Chris Lattner86df27b2009-06-14 00:45:47 +00004001 // Read the modifiers first.
4002 bool Done = false;
4003 while (!Done) {
4004 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004005 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004006 case 'S':
4007 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4008 assert(!Signed && "Can't use 'S' modifier multiple times!");
4009 Signed = true;
4010 break;
4011 case 'U':
4012 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4013 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4014 Unsigned = true;
4015 break;
4016 case 'L':
4017 assert(HowLong <= 2 && "Can't have LLLL modifier");
4018 ++HowLong;
4019 break;
4020 }
4021 }
4022
4023 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004024
Chris Lattner86df27b2009-06-14 00:45:47 +00004025 // Read the base type.
4026 switch (*Str++) {
4027 default: assert(0 && "Unknown builtin type letter!");
4028 case 'v':
4029 assert(HowLong == 0 && !Signed && !Unsigned &&
4030 "Bad modifiers used with 'v'!");
4031 Type = Context.VoidTy;
4032 break;
4033 case 'f':
4034 assert(HowLong == 0 && !Signed && !Unsigned &&
4035 "Bad modifiers used with 'f'!");
4036 Type = Context.FloatTy;
4037 break;
4038 case 'd':
4039 assert(HowLong < 2 && !Signed && !Unsigned &&
4040 "Bad modifiers used with 'd'!");
4041 if (HowLong)
4042 Type = Context.LongDoubleTy;
4043 else
4044 Type = Context.DoubleTy;
4045 break;
4046 case 's':
4047 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4048 if (Unsigned)
4049 Type = Context.UnsignedShortTy;
4050 else
4051 Type = Context.ShortTy;
4052 break;
4053 case 'i':
4054 if (HowLong == 3)
4055 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4056 else if (HowLong == 2)
4057 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4058 else if (HowLong == 1)
4059 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4060 else
4061 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4062 break;
4063 case 'c':
4064 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4065 if (Signed)
4066 Type = Context.SignedCharTy;
4067 else if (Unsigned)
4068 Type = Context.UnsignedCharTy;
4069 else
4070 Type = Context.CharTy;
4071 break;
4072 case 'b': // boolean
4073 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4074 Type = Context.BoolTy;
4075 break;
4076 case 'z': // size_t.
4077 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4078 Type = Context.getSizeType();
4079 break;
4080 case 'F':
4081 Type = Context.getCFConstantStringType();
4082 break;
4083 case 'a':
4084 Type = Context.getBuiltinVaListType();
4085 assert(!Type.isNull() && "builtin va list type not initialized!");
4086 break;
4087 case 'A':
4088 // This is a "reference" to a va_list; however, what exactly
4089 // this means depends on how va_list is defined. There are two
4090 // different kinds of va_list: ones passed by value, and ones
4091 // passed by reference. An example of a by-value va_list is
4092 // x86, where va_list is a char*. An example of by-ref va_list
4093 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4094 // we want this argument to be a char*&; for x86-64, we want
4095 // it to be a __va_list_tag*.
4096 Type = Context.getBuiltinVaListType();
4097 assert(!Type.isNull() && "builtin va list type not initialized!");
4098 if (Type->isArrayType()) {
4099 Type = Context.getArrayDecayedType(Type);
4100 } else {
4101 Type = Context.getLValueReferenceType(Type);
4102 }
4103 break;
4104 case 'V': {
4105 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004106 unsigned NumElements = strtoul(Str, &End, 10);
4107 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004108
Chris Lattner86df27b2009-06-14 00:45:47 +00004109 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004110
Chris Lattner86df27b2009-06-14 00:45:47 +00004111 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4112 Type = Context.getVectorType(ElementType, NumElements);
4113 break;
4114 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004115 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004116 Type = Context.getFILEType();
4117 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004118 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004119 return QualType();
4120 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004121 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004122 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004123 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004124 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004125 else
4126 Type = Context.getjmp_bufType();
4127
Mike Stumpfd612db2009-07-28 23:47:15 +00004128 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004129 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004130 return QualType();
4131 }
4132 break;
Mike Stump782fa302009-07-28 02:25:19 +00004133 }
Mike Stump1eb44332009-09-09 15:08:12 +00004134
Chris Lattner86df27b2009-06-14 00:45:47 +00004135 if (!AllowTypeModifiers)
4136 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004137
Chris Lattner86df27b2009-06-14 00:45:47 +00004138 Done = false;
4139 while (!Done) {
4140 switch (*Str++) {
4141 default: Done = true; --Str; break;
4142 case '*':
4143 Type = Context.getPointerType(Type);
4144 break;
4145 case '&':
4146 Type = Context.getLValueReferenceType(Type);
4147 break;
4148 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4149 case 'C':
John McCall0953e762009-09-24 19:53:00 +00004150 Type = Type.withConst();
Chris Lattner86df27b2009-06-14 00:45:47 +00004151 break;
4152 }
4153 }
Mike Stump1eb44332009-09-09 15:08:12 +00004154
Chris Lattner86df27b2009-06-14 00:45:47 +00004155 return Type;
4156}
4157
4158/// GetBuiltinType - Return the type for the specified builtin.
4159QualType ASTContext::GetBuiltinType(unsigned id,
4160 GetBuiltinTypeError &Error) {
4161 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004162
Chris Lattner86df27b2009-06-14 00:45:47 +00004163 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004164
Chris Lattner86df27b2009-06-14 00:45:47 +00004165 Error = GE_None;
4166 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4167 if (Error != GE_None)
4168 return QualType();
4169 while (TypeStr[0] && TypeStr[0] != '.') {
4170 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4171 if (Error != GE_None)
4172 return QualType();
4173
4174 // Do array -> pointer decay. The builtin should use the decayed type.
4175 if (Ty->isArrayType())
4176 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004177
Chris Lattner86df27b2009-06-14 00:45:47 +00004178 ArgTypes.push_back(Ty);
4179 }
4180
4181 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4182 "'.' should only occur at end of builtin type list!");
4183
4184 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4185 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4186 return getFunctionNoProtoType(ResType);
4187 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4188 TypeStr[0] == '.', 0);
4189}
Eli Friedmana95d7572009-08-19 07:44:53 +00004190
4191QualType
4192ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4193 // Perform the usual unary conversions. We do this early so that
4194 // integral promotions to "int" can allow us to exit early, in the
4195 // lhs == rhs check. Also, for conversion purposes, we ignore any
4196 // qualifiers. For example, "const float" and "float" are
4197 // equivalent.
4198 if (lhs->isPromotableIntegerType())
4199 lhs = getPromotedIntegerType(lhs);
4200 else
4201 lhs = lhs.getUnqualifiedType();
4202 if (rhs->isPromotableIntegerType())
4203 rhs = getPromotedIntegerType(rhs);
4204 else
4205 rhs = rhs.getUnqualifiedType();
4206
4207 // If both types are identical, no conversion is needed.
4208 if (lhs == rhs)
4209 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004210
Eli Friedmana95d7572009-08-19 07:44:53 +00004211 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4212 // The caller can deal with this (e.g. pointer + int).
4213 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4214 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004215
4216 // At this point, we have two different arithmetic types.
4217
Eli Friedmana95d7572009-08-19 07:44:53 +00004218 // Handle complex types first (C99 6.3.1.8p1).
4219 if (lhs->isComplexType() || rhs->isComplexType()) {
4220 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004221 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004222 // convert the rhs to the lhs complex type.
4223 return lhs;
4224 }
Mike Stump1eb44332009-09-09 15:08:12 +00004225 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004226 // convert the lhs to the rhs complex type.
4227 return rhs;
4228 }
4229 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004230 // When both operands are complex, the shorter operand is converted to the
4231 // type of the longer, and that is the type of the result. This corresponds
4232 // to what is done when combining two real floating-point operands.
4233 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004234 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004235 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004236 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004237 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004238 // "double _Complex" is promoted to "long double _Complex".
4239 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004240
4241 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004242 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004243 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004244 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004245 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004246 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4247 // domains match. This is a requirement for our implementation, C99
4248 // does not require this promotion.
4249 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4250 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4251 return rhs;
4252 } else { // handle "_Complex double, double".
4253 return lhs;
4254 }
4255 }
4256 return lhs; // The domain/size match exactly.
4257 }
4258 // Now handle "real" floating types (i.e. float, double, long double).
4259 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4260 // if we have an integer operand, the result is the real floating type.
4261 if (rhs->isIntegerType()) {
4262 // convert rhs to the lhs floating point type.
4263 return lhs;
4264 }
4265 if (rhs->isComplexIntegerType()) {
4266 // convert rhs to the complex floating point type.
4267 return getComplexType(lhs);
4268 }
4269 if (lhs->isIntegerType()) {
4270 // convert lhs to the rhs floating point type.
4271 return rhs;
4272 }
Mike Stump1eb44332009-09-09 15:08:12 +00004273 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004274 // convert lhs to the complex floating point type.
4275 return getComplexType(rhs);
4276 }
4277 // We have two real floating types, float/complex combos were handled above.
4278 // Convert the smaller operand to the bigger result.
4279 int result = getFloatingTypeOrder(lhs, rhs);
4280 if (result > 0) // convert the rhs
4281 return lhs;
4282 assert(result < 0 && "illegal float comparison");
4283 return rhs; // convert the lhs
4284 }
4285 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4286 // Handle GCC complex int extension.
4287 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4288 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4289
4290 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004291 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004292 rhsComplexInt->getElementType()) >= 0)
4293 return lhs; // convert the rhs
4294 return rhs;
4295 } else if (lhsComplexInt && rhs->isIntegerType()) {
4296 // convert the rhs to the lhs complex type.
4297 return lhs;
4298 } else if (rhsComplexInt && lhs->isIntegerType()) {
4299 // convert the lhs to the rhs complex type.
4300 return rhs;
4301 }
4302 }
4303 // Finally, we have two differing integer types.
4304 // The rules for this case are in C99 6.3.1.8
4305 int compare = getIntegerTypeOrder(lhs, rhs);
4306 bool lhsSigned = lhs->isSignedIntegerType(),
4307 rhsSigned = rhs->isSignedIntegerType();
4308 QualType destType;
4309 if (lhsSigned == rhsSigned) {
4310 // Same signedness; use the higher-ranked type
4311 destType = compare >= 0 ? lhs : rhs;
4312 } else if (compare != (lhsSigned ? 1 : -1)) {
4313 // The unsigned type has greater than or equal rank to the
4314 // signed type, so use the unsigned type
4315 destType = lhsSigned ? rhs : lhs;
4316 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4317 // The two types are different widths; if we are here, that
4318 // means the signed type is larger than the unsigned type, so
4319 // use the signed type.
4320 destType = lhsSigned ? lhs : rhs;
4321 } else {
4322 // The signed type is higher-ranked than the unsigned type,
4323 // but isn't actually any bigger (like unsigned int and long
4324 // on most 32-bit systems). Use the unsigned type corresponding
4325 // to the signed type.
4326 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4327 }
4328 return destType;
4329}