blob: a12ac207d910cc68fd7eec71d47cd5802267c2c8 [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 {
62 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
63 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
64 while (I != E) {
65 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66 delete R;
67 }
68 }
69
70 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000071 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
72 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000073 while (I != E) {
74 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
75 delete R;
76 }
77 }
78
Douglas Gregorab452ba2009-03-26 23:50:42 +000079 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000080 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
81 NNS = NestedNameSpecifiers.begin(),
Mike Stump1eb44332009-09-09 15:08:12 +000082 NNSEnd = NestedNameSpecifiers.end();
83 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000084 /* Increment in loop */)
85 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000086
87 if (GlobalNestedNameSpecifier)
88 GlobalNestedNameSpecifier->Destroy(*this);
89
Eli Friedmanb26153c2008-05-27 03:08:09 +000090 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000091}
92
Mike Stump1eb44332009-09-09 15:08:12 +000093void
Douglas Gregor2cf26342009-04-09 22:27:44 +000094ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
95 ExternalSource.reset(Source.take());
96}
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098void ASTContext::PrintStats() const {
99 fprintf(stderr, "*** AST Context Stats:\n");
100 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000101
Douglas Gregordbe833d2009-05-26 14:40:08 +0000102 unsigned counts[] = {
Mike Stump1eb44332009-09-09 15:08:12 +0000103#define TYPE(Name, Parent) 0,
Douglas Gregordbe833d2009-05-26 14:40:08 +0000104#define ABSTRACT_TYPE(Name, Parent)
105#include "clang/AST/TypeNodes.def"
106 0 // Extra
107 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
110 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000111 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
113
Douglas Gregordbe833d2009-05-26 14:40:08 +0000114 unsigned Idx = 0;
115 unsigned TotalBytes = 0;
116#define TYPE(Name, Parent) \
117 if (counts[Idx]) \
118 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
119 TotalBytes += counts[Idx] * sizeof(Name##Type); \
120 ++Idx;
121#define ABSTRACT_TYPE(Name, Parent)
122#include "clang/AST/TypeNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Douglas Gregordbe833d2009-05-26 14:40:08 +0000124 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125
126 if (ExternalSource.get()) {
127 fprintf(stderr, "\n");
128 ExternalSource->PrintStats();
129 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000130}
131
132
133void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000134 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000135}
136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137void ASTContext::InitBuiltinTypes() {
138 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 // C99 6.2.5p19.
141 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 // C99 6.2.5p2.
144 InitBuiltinType(BoolTy, BuiltinType::Bool);
145 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000146 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 InitBuiltinType(CharTy, BuiltinType::Char_S);
148 else
149 InitBuiltinType(CharTy, BuiltinType::Char_U);
150 // C99 6.2.5p4.
151 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
152 InitBuiltinType(ShortTy, BuiltinType::Short);
153 InitBuiltinType(IntTy, BuiltinType::Int);
154 InitBuiltinType(LongTy, BuiltinType::Long);
155 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // C99 6.2.5p6.
158 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
159 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
160 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
161 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
162 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // C99 6.2.5p10.
165 InitBuiltinType(FloatTy, BuiltinType::Float);
166 InitBuiltinType(DoubleTy, BuiltinType::Double);
167 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000168
Chris Lattner2df9ced2009-04-30 02:43:43 +0000169 // GNU extension, 128-bit integers.
170 InitBuiltinType(Int128Ty, BuiltinType::Int128);
171 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
172
Chris Lattner3a250322009-02-26 23:43:47 +0000173 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
174 InitBuiltinType(WCharTy, BuiltinType::WChar);
175 else // C99
176 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000178 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
179 InitBuiltinType(Char16Ty, BuiltinType::Char16);
180 else // C99
181 Char16Ty = getFromTargetType(Target.getChar16Type());
182
183 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
184 InitBuiltinType(Char32Ty, BuiltinType::Char32);
185 else // C99
186 Char32Ty = getFromTargetType(Target.getChar32Type());
187
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000188 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000189 InitBuiltinType(OverloadTy, BuiltinType::Overload);
190
191 // Placeholder type for type-dependent expressions whose type is
192 // completely unknown. No code should ever check a type against
193 // DependentTy and users should never see it; however, it is here to
194 // help diagnose failures to properly check for type-dependent
195 // expressions.
196 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000197
Mike Stump1eb44332009-09-09 15:08:12 +0000198 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlssone89d1592009-06-26 18:41:36 +0000199 // not yet been deduced.
200 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 // C99 6.2.5p11.
203 FloatComplexTy = getComplexType(FloatTy);
204 DoubleComplexTy = getComplexType(DoubleTy);
205 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000206
Steve Naroff7e219e42007-10-15 14:41:52 +0000207 BuiltinVaListType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Steve Naroffde2e22d2009-07-15 18:40:39 +0000209 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
210 ObjCIdTypedefType = QualType();
211 ObjCClassTypedefType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Steve Naroffde2e22d2009-07-15 18:40:39 +0000213 // Builtin types for 'id' and 'Class'.
214 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
215 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff14108da2009-07-10 23:34:53 +0000216
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000217 ObjCConstantStringType = QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000219 // void * type
220 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000221
222 // nullptr type (C++0x 2.14.7)
223 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000224}
225
Douglas Gregor7caa6822009-07-24 20:34:43 +0000226VarDecl *ASTContext::getInstantiatedFromStaticDataMember(VarDecl *Var) {
227 assert(Var->isStaticDataMember() && "Not a static data member");
228 llvm::DenseMap<VarDecl *, VarDecl *>::iterator Pos
229 = InstantiatedFromStaticDataMember.find(Var);
230 if (Pos == InstantiatedFromStaticDataMember.end())
231 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Douglas Gregor7caa6822009-07-24 20:34:43 +0000233 return Pos->second;
234}
235
Mike Stump1eb44332009-09-09 15:08:12 +0000236void
Douglas Gregor7caa6822009-07-24 20:34:43 +0000237ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl) {
238 assert(Inst->isStaticDataMember() && "Not a static data member");
239 assert(Tmpl->isStaticDataMember() && "Not a static data member");
240 assert(!InstantiatedFromStaticDataMember[Inst] &&
241 "Already noted what static data member was instantiated from");
242 InstantiatedFromStaticDataMember[Inst] = Tmpl;
243}
244
Anders Carlsson0d8df782009-08-29 19:37:28 +0000245UnresolvedUsingDecl *
246ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000247 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson0d8df782009-08-29 19:37:28 +0000248 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
249 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
250 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Anders Carlsson0d8df782009-08-29 19:37:28 +0000252 return Pos->second;
253}
254
255void
256ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
257 UnresolvedUsingDecl *UUD) {
258 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
259 "Already noted what using decl what instantiated from");
260 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
261}
262
Anders Carlssond8b285f2009-09-01 04:26:58 +0000263FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
264 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
265 = InstantiatedFromUnnamedFieldDecl.find(Field);
266 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
267 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Anders Carlssond8b285f2009-09-01 04:26:58 +0000269 return Pos->second;
270}
271
272void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
273 FieldDecl *Tmpl) {
274 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
275 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
276 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
277 "Already noted what unnamed field was instantiated from");
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Anders Carlssond8b285f2009-09-01 04:26:58 +0000279 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
280}
281
Douglas Gregor2e222532009-07-02 17:08:52 +0000282namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000283 class BeforeInTranslationUnit
Douglas Gregor2e222532009-07-02 17:08:52 +0000284 : std::binary_function<SourceRange, SourceRange, bool> {
285 SourceManager *SourceMgr;
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Douglas Gregor2e222532009-07-02 17:08:52 +0000287 public:
288 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Douglas Gregor2e222532009-07-02 17:08:52 +0000290 bool operator()(SourceRange X, SourceRange Y) {
291 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
292 }
293 };
294}
295
296/// \brief Determine whether the given comment is a Doxygen-style comment.
297///
298/// \param Start the start of the comment text.
299///
300/// \param End the end of the comment text.
301///
302/// \param Member whether we want to check whether this is a member comment
303/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
304/// we only return true when we find a non-member comment.
Mike Stump1eb44332009-09-09 15:08:12 +0000305static bool
306isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregor2e222532009-07-02 17:08:52 +0000307 bool Member = false) {
Mike Stump1eb44332009-09-09 15:08:12 +0000308 const char *BufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000309 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
310 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
311 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Douglas Gregor2e222532009-07-02 17:08:52 +0000313 if (End - Start < 4)
314 return false;
315
316 assert(Start[0] == '/' && "Not a comment?");
317 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
318 return false;
319 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
320 return false;
321
322 return (Start[3] == '<') == Member;
323}
324
325/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump1eb44332009-09-09 15:08:12 +0000326/// it has one.
Douglas Gregor2e222532009-07-02 17:08:52 +0000327const char *ASTContext::getCommentForDecl(const Decl *D) {
328 if (!D)
329 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Douglas Gregor2e222532009-07-02 17:08:52 +0000331 // Check whether we have cached a comment string for this declaration
332 // already.
Mike Stump1eb44332009-09-09 15:08:12 +0000333 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregor2e222532009-07-02 17:08:52 +0000334 = DeclComments.find(D);
335 if (Pos != DeclComments.end())
336 return Pos->second.c_str();
337
Mike Stump1eb44332009-09-09 15:08:12 +0000338 // If we have an external AST source and have not yet loaded comments from
Douglas Gregor2e222532009-07-02 17:08:52 +0000339 // that source, do so now.
340 if (ExternalSource && !LoadedExternalComments) {
341 std::vector<SourceRange> LoadedComments;
342 ExternalSource->ReadComments(LoadedComments);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Douglas Gregor2e222532009-07-02 17:08:52 +0000344 if (!LoadedComments.empty())
345 Comments.insert(Comments.begin(), LoadedComments.begin(),
346 LoadedComments.end());
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Douglas Gregor2e222532009-07-02 17:08:52 +0000348 LoadedExternalComments = true;
349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
351 // If there are no comments anywhere, we won't find anything.
Douglas Gregor2e222532009-07-02 17:08:52 +0000352 if (Comments.empty())
353 return 0;
354
355 // If the declaration doesn't map directly to a location in a file, we
356 // can't find the comment.
357 SourceLocation DeclStartLoc = D->getLocStart();
358 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
359 return 0;
360
361 // Find the comment that occurs just before this declaration.
362 std::vector<SourceRange>::iterator LastComment
Mike Stump1eb44332009-09-09 15:08:12 +0000363 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregor2e222532009-07-02 17:08:52 +0000364 SourceRange(DeclStartLoc),
365 BeforeInTranslationUnit(&SourceMgr));
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Douglas Gregor2e222532009-07-02 17:08:52 +0000367 // Decompose the location for the start of the declaration and find the
368 // beginning of the file buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000369 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregor2e222532009-07-02 17:08:52 +0000370 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000371 const char *FileBufferStart
Douglas Gregor2e222532009-07-02 17:08:52 +0000372 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Douglas Gregor2e222532009-07-02 17:08:52 +0000374 // First check whether we have a comment for a member.
375 if (LastComment != Comments.end() &&
376 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
377 isDoxygenComment(SourceMgr, *LastComment, true)) {
378 std::pair<FileID, unsigned> LastCommentEndDecomp
379 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
380 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
381 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump1eb44332009-09-09 15:08:12 +0000382 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000383 LastCommentEndDecomp.second)) {
384 // The Doxygen member comment comes after the declaration starts and
385 // is on the same line and in the same file as the declaration. This
386 // is the comment we want.
387 std::string &Result = DeclComments[D];
Mike Stump1eb44332009-09-09 15:08:12 +0000388 Result.append(FileBufferStart +
389 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000390 FileBufferStart + LastCommentEndDecomp.second + 1);
391 return Result.c_str();
392 }
393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Douglas Gregor2e222532009-07-02 17:08:52 +0000395 if (LastComment == Comments.begin())
396 return 0;
397 --LastComment;
398
399 // Decompose the end of the comment.
400 std::pair<FileID, unsigned> LastCommentEndDecomp
401 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Douglas Gregor2e222532009-07-02 17:08:52 +0000403 // If the comment and the declaration aren't in the same file, then they
404 // aren't related.
405 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
406 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Douglas Gregor2e222532009-07-02 17:08:52 +0000408 // Check that we actually have a Doxygen comment.
409 if (!isDoxygenComment(SourceMgr, *LastComment))
410 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Douglas Gregor2e222532009-07-02 17:08:52 +0000412 // Compute the starting line for the declaration and for the end of the
413 // comment (this is expensive).
Mike Stump1eb44332009-09-09 15:08:12 +0000414 unsigned DeclStartLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000415 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
416 unsigned CommentEndLine
Mike Stump1eb44332009-09-09 15:08:12 +0000417 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregor2e222532009-07-02 17:08:52 +0000418 LastCommentEndDecomp.second);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Douglas Gregor2e222532009-07-02 17:08:52 +0000420 // If the comment does not end on the line prior to the declaration, then
421 // the comment is not associated with the declaration at all.
422 if (CommentEndLine + 1 != DeclStartLine)
423 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Douglas Gregor2e222532009-07-02 17:08:52 +0000425 // We have a comment, but there may be more comments on the previous lines.
426 // Keep looking so long as the comments are still Doxygen comments and are
427 // still adjacent.
Mike Stump1eb44332009-09-09 15:08:12 +0000428 unsigned ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000429 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
430 std::vector<SourceRange>::iterator FirstComment = LastComment;
431 while (FirstComment != Comments.begin()) {
432 // Look at the previous comment
433 --FirstComment;
434 std::pair<FileID, unsigned> Decomp
435 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregor2e222532009-07-02 17:08:52 +0000437 // If this previous comment is in a different file, we're done.
438 if (Decomp.first != DeclStartDecomp.first) {
439 ++FirstComment;
440 break;
441 }
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Douglas Gregor2e222532009-07-02 17:08:52 +0000443 // If this comment is not a Doxygen comment, we're done.
444 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
445 ++FirstComment;
446 break;
447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Douglas Gregor2e222532009-07-02 17:08:52 +0000449 // If the line number is not what we expected, we're done.
450 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
451 if (Line != ExpectedLine) {
452 ++FirstComment;
453 break;
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Douglas Gregor2e222532009-07-02 17:08:52 +0000456 // Set the next expected line number.
Mike Stump1eb44332009-09-09 15:08:12 +0000457 ExpectedLine
Douglas Gregor2e222532009-07-02 17:08:52 +0000458 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Douglas Gregor2e222532009-07-02 17:08:52 +0000461 // The iterator range [FirstComment, LastComment] contains all of the
462 // BCPL comments that, together, are associated with this declaration.
463 // Form a single comment block string for this declaration that concatenates
464 // all of these comments.
465 std::string &Result = DeclComments[D];
466 while (FirstComment != LastComment) {
467 std::pair<FileID, unsigned> DecompStart
468 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
469 std::pair<FileID, unsigned> DecompEnd
470 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
471 Result.append(FileBufferStart + DecompStart.second,
472 FileBufferStart + DecompEnd.second + 1);
473 ++FirstComment;
474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Douglas Gregor2e222532009-07-02 17:08:52 +0000476 // Append the last comment line.
Mike Stump1eb44332009-09-09 15:08:12 +0000477 Result.append(FileBufferStart +
478 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregor2e222532009-07-02 17:08:52 +0000479 FileBufferStart + LastCommentEndDecomp.second + 1);
480 return Result.c_str();
481}
482
Chris Lattner464175b2007-07-18 17:52:12 +0000483//===----------------------------------------------------------------------===//
484// Type Sizing and Analysis
485//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000486
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000487/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
488/// scalar floating point type.
489const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall183700f2009-09-21 23:43:11 +0000490 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000491 assert(BT && "Not a floating point type!");
492 switch (BT->getKind()) {
493 default: assert(0 && "Not a floating point type!");
494 case BuiltinType::Float: return Target.getFloatFormat();
495 case BuiltinType::Double: return Target.getDoubleFormat();
496 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
497 }
498}
499
Chris Lattneraf707ab2009-01-24 21:53:27 +0000500/// getDeclAlign - Return a conservative estimate of the alignment of the
501/// specified decl. Note that bitfields do not have a valid alignment, so
502/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000503unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000504 unsigned Align = Target.getCharWidth();
505
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000506 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedmandcdafb62009-02-22 02:56:25 +0000507 Align = std::max(Align, AA->getAlignment());
508
Chris Lattneraf707ab2009-01-24 21:53:27 +0000509 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
510 QualType T = VD->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000511 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000512 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000513 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000514 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
515 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000516 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
517 T = cast<ArrayType>(T)->getElementType();
518
519 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
520 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000521 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000522
523 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000524}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000525
Chris Lattnera7674d82007-07-13 22:13:22 +0000526/// getTypeSize - Return the size of the specified type, in bits. This method
527/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000528std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000529ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000530 uint64_t Width=0;
531 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000532 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000533#define TYPE(Class, Base)
534#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000535#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000536#define DEPENDENT_TYPE(Class, Base) case Type::Class:
537#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000538 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000539 break;
540
Chris Lattner692233e2007-07-13 22:27:08 +0000541 case Type::FunctionNoProto:
542 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000543 // GCC extension: alignof(function) = 32 bits
544 Width = 0;
545 Align = 32;
546 break;
547
Douglas Gregor72564e72009-02-26 23:50:07 +0000548 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000549 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000550 Width = 0;
551 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
552 break;
553
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000554 case Type::ConstantArrayWithExpr:
555 case Type::ConstantArrayWithoutExpr:
Steve Narofffb22d962007-08-30 01:06:46 +0000556 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000557 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner98be4942008-03-05 18:54:05 +0000559 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000560 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000561 Align = EltInfo.second;
562 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000563 }
Nate Begeman213541a2008-04-18 23:10:10 +0000564 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000565 case Type::Vector: {
Mike Stump1eb44332009-09-09 15:08:12 +0000566 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000567 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000568 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000569 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000570 // If the alignment is not a power of 2, round up to the next power of 2.
571 // This happens for non-power-of-2 length vectors.
572 // FIXME: this should probably be a target property.
573 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000574 break;
575 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000576
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000577 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000578 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000579 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000580 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000581 // GCC extension: alignof(void) = 8 bits.
582 Width = 0;
583 Align = 8;
584 break;
585
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000586 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000587 Width = Target.getBoolWidth();
588 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000589 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000590 case BuiltinType::Char_S:
591 case BuiltinType::Char_U:
592 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000593 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000594 Width = Target.getCharWidth();
595 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000596 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000597 case BuiltinType::WChar:
598 Width = Target.getWCharWidth();
599 Align = Target.getWCharAlign();
600 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000601 case BuiltinType::Char16:
602 Width = Target.getChar16Width();
603 Align = Target.getChar16Align();
604 break;
605 case BuiltinType::Char32:
606 Width = Target.getChar32Width();
607 Align = Target.getChar32Align();
608 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000609 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000610 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000611 Width = Target.getShortWidth();
612 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000613 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000614 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000615 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000616 Width = Target.getIntWidth();
617 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000618 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000619 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000620 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000621 Width = Target.getLongWidth();
622 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000623 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000624 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000625 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000626 Width = Target.getLongLongWidth();
627 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000628 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000629 case BuiltinType::Int128:
630 case BuiltinType::UInt128:
631 Width = 128;
632 Align = 128; // int128_t is 128-bit aligned on all targets.
633 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000634 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000635 Width = Target.getFloatWidth();
636 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000637 break;
638 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000639 Width = Target.getDoubleWidth();
640 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000641 break;
642 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000643 Width = Target.getLongDoubleWidth();
644 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000645 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000646 case BuiltinType::NullPtr:
647 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
648 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000649 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000650 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000651 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000652 case Type::FixedWidthInt:
653 // FIXME: This isn't precisely correct; the width/alignment should depend
654 // on the available types for the target
655 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000656 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000657 Align = Width;
658 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000659 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000660 // FIXME: Pointers into different addr spaces could have different sizes and
661 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000662 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000663 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000664 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000665 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000666 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000667 case Type::BlockPointer: {
668 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
669 Width = Target.getPointerWidth(AS);
670 Align = Target.getPointerAlign(AS);
671 break;
672 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000673 case Type::Pointer: {
674 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000675 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000676 Align = Target.getPointerAlign(AS);
677 break;
678 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000679 case Type::LValueReference:
680 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000681 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000682 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000683 // FIXME: This is wrong for struct layout: a reference in a struct has
684 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000685 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000686 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000687 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
688 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
689 // If we ever want to support other ABIs this needs to be abstracted.
690
Sebastian Redlf30208a2009-01-24 21:16:55 +0000691 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000692 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000693 getTypeInfo(getPointerDiffType());
694 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000695 if (Pointee->isFunctionType())
696 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000697 Align = PtrDiffInfo.second;
698 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000699 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000700 case Type::Complex: {
701 // Complex types have the same alignment as their elements, but twice the
702 // size.
Mike Stump1eb44332009-09-09 15:08:12 +0000703 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000704 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000705 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000706 Align = EltInfo.second;
707 break;
708 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000709 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000710 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000711 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
712 Width = Layout.getSize();
713 Align = Layout.getAlignment();
714 break;
715 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000716 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000717 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000718 const TagType *TT = cast<TagType>(T);
719
720 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000721 Width = 1;
722 Align = 1;
723 break;
724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Daniel Dunbar1d751182008-11-08 05:48:37 +0000726 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000727 return getTypeInfo(ET->getDecl()->getIntegerType());
728
Daniel Dunbar1d751182008-11-08 05:48:37 +0000729 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000730 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
731 Width = Layout.getSize();
732 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000733 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000734 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000735
John McCall7da24312009-09-05 00:15:47 +0000736 case Type::Elaborated: {
737 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
738 }
739
Douglas Gregor18857642009-04-30 17:32:17 +0000740 case Type::Typedef: {
741 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000742 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregor18857642009-04-30 17:32:17 +0000743 Align = Aligned->getAlignment();
744 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
745 } else
746 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000747 break;
Chris Lattner71763312008-04-06 22:05:18 +0000748 }
Douglas Gregor18857642009-04-30 17:32:17 +0000749
750 case Type::TypeOfExpr:
751 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
752 .getTypePtr());
753
754 case Type::TypeOf:
755 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
756
Anders Carlsson395b4752009-06-24 19:06:50 +0000757 case Type::Decltype:
758 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
759 .getTypePtr());
760
Douglas Gregor18857642009-04-30 17:32:17 +0000761 case Type::QualifiedName:
762 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Douglas Gregor18857642009-04-30 17:32:17 +0000764 case Type::TemplateSpecialization:
Mike Stump1eb44332009-09-09 15:08:12 +0000765 assert(getCanonicalType(T) != T &&
Douglas Gregor18857642009-04-30 17:32:17 +0000766 "Cannot request the size of a dependent type");
767 // FIXME: this is likely to be wrong once we support template
768 // aliases, since a template alias could refer to a typedef that
769 // has an __aligned__ attribute on it.
770 return getTypeInfo(getCanonicalType(T));
771 }
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Chris Lattner464175b2007-07-18 17:52:12 +0000773 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000774 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000775}
776
Chris Lattner34ebde42009-01-27 18:08:34 +0000777/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
778/// type for the current target in bits. This can be different than the ABI
779/// alignment in cases where it is beneficial for performance to overalign
780/// a data type.
781unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
782 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000783
784 // Double and long long should be naturally aligned if possible.
John McCall183700f2009-09-21 23:43:11 +0000785 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman1eed6022009-05-25 21:27:19 +0000786 T = CT->getElementType().getTypePtr();
787 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
788 T->isSpecificBuiltinType(BuiltinType::LongLong))
789 return std::max(ABIAlign, (unsigned)getTypeSize(T));
790
Chris Lattner34ebde42009-01-27 18:08:34 +0000791 return ABIAlign;
792}
793
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000794static void CollectLocalObjCIvars(ASTContext *Ctx,
795 const ObjCInterfaceDecl *OI,
796 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000797 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
798 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000799 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000800 if (!IVDecl->isInvalidDecl())
801 Fields.push_back(cast<FieldDecl>(IVDecl));
802 }
803}
804
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000805void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
806 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
807 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
808 CollectObjCIvars(SuperClass, Fields);
809 CollectLocalObjCIvars(this, OI, Fields);
810}
811
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000812/// ShallowCollectObjCIvars -
813/// Collect all ivars, including those synthesized, in the current class.
814///
815void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
816 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
817 bool CollectSynthesized) {
818 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
819 E = OI->ivar_end(); I != E; ++I) {
820 Ivars.push_back(*I);
821 }
822 if (CollectSynthesized)
823 CollectSynthesizedIvars(OI, Ivars);
824}
825
Fariborz Jahanian98200742009-05-12 18:14:29 +0000826void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
827 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000828 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
829 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000830 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
831 Ivars.push_back(Ivar);
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Fariborz Jahanian98200742009-05-12 18:14:29 +0000833 // Also look into nested protocols.
834 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
835 E = PD->protocol_end(); P != E; ++P)
836 CollectProtocolSynthesizedIvars(*P, Ivars);
837}
838
839/// CollectSynthesizedIvars -
840/// This routine collect synthesized ivars for the designated class.
841///
842void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
843 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000844 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
845 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000846 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
847 Ivars.push_back(Ivar);
848 }
849 // Also look into interface's protocol list for properties declared
850 // in the protocol and whose ivars are synthesized.
851 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
852 PE = OI->protocol_end(); P != PE; ++P) {
853 ObjCProtocolDecl *PD = (*P);
854 CollectProtocolSynthesizedIvars(PD, Ivars);
855 }
856}
857
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000858unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
859 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000860 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
861 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000862 if ((*I)->getPropertyIvarDecl())
863 ++count;
864
865 // Also look into nested protocols.
866 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
867 E = PD->protocol_end(); P != E; ++P)
868 count += CountProtocolSynthesizedIvars(*P);
869 return count;
870}
871
Mike Stump1eb44332009-09-09 15:08:12 +0000872unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000873 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000874 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
875 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000876 if ((*I)->getPropertyIvarDecl())
877 ++count;
878 }
879 // Also look into interface's protocol list for properties declared
880 // in the protocol and whose ivars are synthesized.
881 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
882 PE = OI->protocol_end(); P != PE; ++P) {
883 ObjCProtocolDecl *PD = (*P);
884 count += CountProtocolSynthesizedIvars(PD);
885 }
886 return count;
887}
888
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000889/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
890ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
891 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
892 I = ObjCImpls.find(D);
893 if (I != ObjCImpls.end())
894 return cast<ObjCImplementationDecl>(I->second);
895 return 0;
896}
897/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
898ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
899 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
900 I = ObjCImpls.find(D);
901 if (I != ObjCImpls.end())
902 return cast<ObjCCategoryImplDecl>(I->second);
903 return 0;
904}
905
906/// \brief Set the implementation of ObjCInterfaceDecl.
907void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
908 ObjCImplementationDecl *ImplD) {
909 assert(IFaceD && ImplD && "Passed null params");
910 ObjCImpls[IFaceD] = ImplD;
911}
912/// \brief Set the implementation of ObjCCategoryDecl.
913void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
914 ObjCCategoryImplDecl *ImplD) {
915 assert(CatD && ImplD && "Passed null params");
916 ObjCImpls[CatD] = ImplD;
917}
918
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +0000919/// \brief Allocate an uninitialized DeclaratorInfo.
920///
921/// The caller should initialize the memory held by DeclaratorInfo using
922/// the TypeLoc wrappers.
923///
924/// \param T the type that will be the basis for type source info. This type
925/// should refer to how the declarator was written in source code, not to
926/// what type semantic analysis resolved the declarator to.
927DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
928 unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
929 DeclaratorInfo *DInfo =
930 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
931 new (DInfo) DeclaratorInfo(T);
932 return DInfo;
933}
934
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000935/// getInterfaceLayoutImpl - Get or compute information about the
936/// layout of the given interface.
937///
938/// \param Impl - If given, also include the layout of the interface's
939/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000940const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000941ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
942 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000943 assert(!D->isForwardDecl() && "Invalid interface decl!");
944
Devang Patel44a3dde2008-06-04 21:54:36 +0000945 // Look up this layout, if already laid out, return what we have.
Mike Stump1eb44332009-09-09 15:08:12 +0000946 ObjCContainerDecl *Key =
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000947 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
948 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
949 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000950
Daniel Dunbar453addb2009-05-03 11:16:44 +0000951 // Add in synthesized ivar count if laying out an implementation.
952 if (Impl) {
Anders Carlsson29445a02009-07-18 21:19:52 +0000953 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000954 unsigned SynthCount = CountSynthesizedIvars(D);
955 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000956 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000957 // entry. Note we can't cache this because we simply free all
958 // entries later; however we shouldn't look up implementations
959 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000960 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000961 return getObjCLayout(D, 0);
962 }
963
Mike Stump1eb44332009-09-09 15:08:12 +0000964 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000965 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
966 ObjCLayouts[Key] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Devang Patel44a3dde2008-06-04 21:54:36 +0000968 return *NewEntry;
969}
970
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000971const ASTRecordLayout &
972ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
973 return getObjCLayout(D, 0);
974}
975
976const ASTRecordLayout &
977ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
978 return getObjCLayout(D->getClassInterface(), D);
979}
980
Devang Patel88a981b2007-11-01 19:11:01 +0000981/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000982/// specified record (struct/union/class), which indicates its size and field
983/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000984const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000985 D = D->getDefinition(*this);
986 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000987
Chris Lattner464175b2007-07-18 17:52:12 +0000988 // Look up this layout, if already laid out, return what we have.
Eli Friedmanab22c432009-07-22 20:29:16 +0000989 // Note that we can't save a reference to the entry because this function
990 // is recursive.
991 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000992 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000993
Mike Stump1eb44332009-09-09 15:08:12 +0000994 const ASTRecordLayout *NewEntry =
Anders Carlsson29445a02009-07-18 21:19:52 +0000995 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedmanab22c432009-07-22 20:29:16 +0000996 ASTRecordLayouts[D] = NewEntry;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner5d2a6302007-07-18 18:26:58 +0000998 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000999}
1000
Chris Lattnera7674d82007-07-13 22:13:22 +00001001//===----------------------------------------------------------------------===//
1002// Type creation/memoization methods
1003//===----------------------------------------------------------------------===//
1004
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001005QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001006 QualType CanT = getCanonicalType(T);
1007 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001008 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001009
1010 // If we are composing extended qualifiers together, merge together into one
1011 // ExtQualType node.
1012 unsigned CVRQuals = T.getCVRQualifiers();
1013 QualType::GCAttrTypes GCAttr = QualType::GCNone;
1014 Type *TypeNode = T.getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Chris Lattnerb7d25532009-02-18 22:53:11 +00001016 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
1017 // If this type already has an address space specified, it cannot get
1018 // another one.
1019 assert(EQT->getAddressSpace() == 0 &&
1020 "Type cannot be in multiple addr spaces!");
1021 GCAttr = EQT->getObjCGCAttr();
1022 TypeNode = EQT->getBaseType();
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattnerb7d25532009-02-18 22:53:11 +00001025 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +00001026 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001027 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +00001028 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001029 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +00001030 return QualType(EXTQy, CVRQuals);
1031
Christopher Lambebb97e92008-02-04 02:31:56 +00001032 // If the base type isn't canonical, this won't be a canonical type either,
1033 // so fill in the canonical type field.
1034 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001035 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001036 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattnerb7d25532009-02-18 22:53:11 +00001038 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001039 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001040 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +00001041 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00001042 ExtQualType *New =
1043 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001044 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +00001045 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001046 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001047}
1048
Chris Lattnerb7d25532009-02-18 22:53:11 +00001049QualType ASTContext::getObjCGCQualType(QualType T,
1050 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001051 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001052 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001053 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001055 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001056 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001057 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001058 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1059 return getPointerType(ResultType);
1060 }
1061 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00001062 // If we are composing extended qualifiers together, merge together into one
1063 // ExtQualType node.
1064 unsigned CVRQuals = T.getCVRQualifiers();
1065 Type *TypeNode = T.getTypePtr();
1066 unsigned AddressSpace = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Chris Lattnerb7d25532009-02-18 22:53:11 +00001068 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
Mike Stump24556362009-07-25 21:26:53 +00001069 // If this type already has an ObjCGC specified, it cannot get
Chris Lattnerb7d25532009-02-18 22:53:11 +00001070 // another one.
1071 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
Mike Stump24556362009-07-25 21:26:53 +00001072 "Type cannot have multiple ObjCGCs!");
Chris Lattnerb7d25532009-02-18 22:53:11 +00001073 AddressSpace = EQT->getAddressSpace();
1074 TypeNode = EQT->getBaseType();
1075 }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001077 // Check if we've already instantiated an gc qual'd type of this type.
1078 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001079 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001080 void *InsertPos = 0;
1081 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +00001082 return QualType(EXTQy, CVRQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001084 // If the base type isn't canonical, this won't be a canonical type either,
1085 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00001086 // FIXME: Isn't this also not canonical if the base type is a array
1087 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001088 QualType Canonical;
1089 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00001090 Canonical = getObjCGCQualType(CanT, GCAttr);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Chris Lattnerb7d25532009-02-18 22:53:11 +00001092 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001093 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
1094 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1095 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00001096 ExtQualType *New =
1097 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001098 ExtQualTypes.InsertNode(New, InsertPos);
1099 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001100 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001101}
Chris Lattnera7674d82007-07-13 22:13:22 +00001102
Mike Stump24556362009-07-25 21:26:53 +00001103QualType ASTContext::getNoReturnType(QualType T) {
Mike Stump6dcbc292009-07-25 23:24:03 +00001104 QualifierSet qs;
1105 qs.strip(T);
Mike Stump24556362009-07-25 21:26:53 +00001106 if (T->isPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001107 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Mike Stump24556362009-07-25 21:26:53 +00001108 QualType ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001109 ResultType = getPointerType(ResultType);
1110 ResultType.setCVRQualifiers(T.getCVRQualifiers());
1111 return qs.apply(ResultType, *this);
Mike Stump24556362009-07-25 21:26:53 +00001112 }
1113 if (T->isBlockPointerType()) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001114 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
Mike Stump24556362009-07-25 21:26:53 +00001115 QualType ResultType = getNoReturnType(Pointee);
Mike Stump6dcbc292009-07-25 23:24:03 +00001116 ResultType = getBlockPointerType(ResultType);
1117 ResultType.setCVRQualifiers(T.getCVRQualifiers());
1118 return qs.apply(ResultType, *this);
Mike Stump1eb44332009-09-09 15:08:12 +00001119 }
Mike Stump24556362009-07-25 21:26:53 +00001120 if (!T->isFunctionType())
1121 assert(0 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump1eb44332009-09-09 15:08:12 +00001122
John McCall183700f2009-09-21 23:43:11 +00001123 if (const FunctionNoProtoType *F = T->getAs<FunctionNoProtoType>()) {
Mike Stump24556362009-07-25 21:26:53 +00001124 return getFunctionNoProtoType(F->getResultType(), true);
1125 }
John McCall183700f2009-09-21 23:43:11 +00001126 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
Mike Stump24556362009-07-25 21:26:53 +00001127 return getFunctionType(F->getResultType(), F->arg_type_begin(),
1128 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1129 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1130 F->getNumExceptions(), F->exception_begin(), true);
1131}
1132
Reid Spencer5f016e22007-07-11 17:01:13 +00001133/// getComplexType - Return the uniqued reference to the type for a complex
1134/// number with the specified element type.
1135QualType ASTContext::getComplexType(QualType T) {
1136 // Unique pointers, to guarantee there is only one pointer of a particular
1137 // structure.
1138 llvm::FoldingSetNodeID ID;
1139 ComplexType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 void *InsertPos = 0;
1142 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1143 return QualType(CT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 // If the pointee type isn't canonical, this won't be a canonical type either,
1146 // so fill in the canonical type field.
1147 QualType Canonical;
1148 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001149 Canonical = getComplexType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 // Get the new insert position for the node we care about.
1152 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001153 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 }
Steve Narofff83820b2009-01-27 22:08:43 +00001155 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 Types.push_back(New);
1157 ComplexTypes.InsertNode(New, InsertPos);
1158 return QualType(New, 0);
1159}
1160
Eli Friedmanf98aba32009-02-13 02:31:07 +00001161QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1162 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1163 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1164 FixedWidthIntType *&Entry = Map[Width];
1165 if (!Entry)
1166 Entry = new FixedWidthIntType(Width, Signed);
1167 return QualType(Entry, 0);
1168}
Reid Spencer5f016e22007-07-11 17:01:13 +00001169
1170/// getPointerType - Return the uniqued reference to the type for a pointer to
1171/// the specified type.
1172QualType ASTContext::getPointerType(QualType T) {
1173 // Unique pointers, to guarantee there is only one pointer of a particular
1174 // structure.
1175 llvm::FoldingSetNodeID ID;
1176 PointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 void *InsertPos = 0;
1179 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1180 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 // If the pointee type isn't canonical, this won't be a canonical type either,
1183 // so fill in the canonical type field.
1184 QualType Canonical;
1185 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001186 Canonical = getPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 // Get the new insert position for the node we care about.
1189 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001190 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 }
Steve Narofff83820b2009-01-27 22:08:43 +00001192 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 Types.push_back(New);
1194 PointerTypes.InsertNode(New, InsertPos);
1195 return QualType(New, 0);
1196}
1197
Mike Stump1eb44332009-09-09 15:08:12 +00001198/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroff5618bd42008-08-27 16:04:49 +00001199/// a pointer to the specified block.
1200QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001201 assert(T->isFunctionType() && "block of function types only");
1202 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001203 // structure.
1204 llvm::FoldingSetNodeID ID;
1205 BlockPointerType::Profile(ID, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Steve Naroff5618bd42008-08-27 16:04:49 +00001207 void *InsertPos = 0;
1208 if (BlockPointerType *PT =
1209 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1210 return QualType(PT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001211
1212 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001213 // type either so fill in the canonical type field.
1214 QualType Canonical;
1215 if (!T->isCanonical()) {
1216 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Steve Naroff5618bd42008-08-27 16:04:49 +00001218 // Get the new insert position for the node we care about.
1219 BlockPointerType *NewIP =
1220 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001221 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001222 }
Steve Narofff83820b2009-01-27 22:08:43 +00001223 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001224 Types.push_back(New);
1225 BlockPointerTypes.InsertNode(New, InsertPos);
1226 return QualType(New, 0);
1227}
1228
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001229/// getLValueReferenceType - Return the uniqued reference to the type for an
1230/// lvalue reference to the specified type.
1231QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // Unique pointers, to guarantee there is only one pointer of a particular
1233 // structure.
1234 llvm::FoldingSetNodeID ID;
1235 ReferenceType::Profile(ID, T);
1236
1237 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001238 if (LValueReferenceType *RT =
1239 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001241
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 // If the referencee type isn't canonical, this won't be a canonical type
1243 // either, so fill in the canonical type field.
1244 QualType Canonical;
1245 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001246 Canonical = getLValueReferenceType(getCanonicalType(T));
1247
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001249 LValueReferenceType *NewIP =
1250 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001251 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 }
1253
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001254 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001256 LValueReferenceTypes.InsertNode(New, InsertPos);
1257 return QualType(New, 0);
1258}
1259
1260/// getRValueReferenceType - Return the uniqued reference to the type for an
1261/// rvalue reference to the specified type.
1262QualType ASTContext::getRValueReferenceType(QualType T) {
1263 // Unique pointers, to guarantee there is only one pointer of a particular
1264 // structure.
1265 llvm::FoldingSetNodeID ID;
1266 ReferenceType::Profile(ID, T);
1267
1268 void *InsertPos = 0;
1269 if (RValueReferenceType *RT =
1270 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1271 return QualType(RT, 0);
1272
1273 // If the referencee type isn't canonical, this won't be a canonical type
1274 // either, so fill in the canonical type field.
1275 QualType Canonical;
1276 if (!T->isCanonical()) {
1277 Canonical = getRValueReferenceType(getCanonicalType(T));
1278
1279 // Get the new insert position for the node we care about.
1280 RValueReferenceType *NewIP =
1281 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1282 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1283 }
1284
1285 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1286 Types.push_back(New);
1287 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 return QualType(New, 0);
1289}
1290
Sebastian Redlf30208a2009-01-24 21:16:55 +00001291/// getMemberPointerType - Return the uniqued reference to the type for a
1292/// member pointer to the specified type, in the specified class.
Mike Stump1eb44332009-09-09 15:08:12 +00001293QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001294 // Unique pointers, to guarantee there is only one pointer of a particular
1295 // structure.
1296 llvm::FoldingSetNodeID ID;
1297 MemberPointerType::Profile(ID, T, Cls);
1298
1299 void *InsertPos = 0;
1300 if (MemberPointerType *PT =
1301 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1302 return QualType(PT, 0);
1303
1304 // If the pointee or class type isn't canonical, this won't be a canonical
1305 // type either, so fill in the canonical type field.
1306 QualType Canonical;
1307 if (!T->isCanonical()) {
1308 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1309
1310 // Get the new insert position for the node we care about.
1311 MemberPointerType *NewIP =
1312 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1313 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1314 }
Steve Narofff83820b2009-01-27 22:08:43 +00001315 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001316 Types.push_back(New);
1317 MemberPointerTypes.InsertNode(New, InsertPos);
1318 return QualType(New, 0);
1319}
1320
Mike Stump1eb44332009-09-09 15:08:12 +00001321/// getConstantArrayType - Return the unique reference to the type for an
Steve Narofffb22d962007-08-30 01:06:46 +00001322/// array of the specified element type.
Mike Stump1eb44332009-09-09 15:08:12 +00001323QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001324 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001325 ArrayType::ArraySizeModifier ASM,
1326 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001327 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1328 "Constant array of VLAs is illegal!");
1329
Chris Lattner38aeec72009-05-13 04:12:56 +00001330 // Convert the array size into a canonical width matching the pointer size for
1331 // the target.
1332 llvm::APInt ArySize(ArySizeIn);
1333 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001336 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001339 if (ConstantArrayType *ATP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001340 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 return QualType(ATP, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 // If the element type isn't canonical, this won't be a canonical type either,
1344 // so fill in the canonical type field.
1345 QualType Canonical;
1346 if (!EltTy->isCanonical()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001347 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001348 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 // Get the new insert position for the node we care about.
Mike Stump1eb44332009-09-09 15:08:12 +00001350 ConstantArrayType *NewIP =
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001351 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001352 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 }
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001355 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001356 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001357 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 Types.push_back(New);
1359 return QualType(New, 0);
1360}
1361
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001362/// getConstantArrayWithExprType - Return a reference to the type for
1363/// an array of the specified element type.
1364QualType
1365ASTContext::getConstantArrayWithExprType(QualType EltTy,
1366 const llvm::APInt &ArySizeIn,
1367 Expr *ArySizeExpr,
1368 ArrayType::ArraySizeModifier ASM,
1369 unsigned EltTypeQuals,
1370 SourceRange Brackets) {
1371 // Convert the array size into a canonical width matching the pointer
1372 // size for the target.
1373 llvm::APInt ArySize(ArySizeIn);
1374 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1375
1376 // Compute the canonical ConstantArrayType.
1377 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1378 ArySize, ASM, EltTypeQuals);
1379 // Since we don't unique expressions, it isn't possible to unique VLA's
1380 // that have an expression provided for their size.
1381 ConstantArrayWithExprType *New =
1382 new(*this,8)ConstantArrayWithExprType(EltTy, Canonical,
1383 ArySize, ArySizeExpr,
1384 ASM, EltTypeQuals, Brackets);
1385 Types.push_back(New);
1386 return QualType(New, 0);
1387}
1388
1389/// getConstantArrayWithoutExprType - Return a reference to the type for
1390/// an array of the specified element type.
1391QualType
1392ASTContext::getConstantArrayWithoutExprType(QualType EltTy,
1393 const llvm::APInt &ArySizeIn,
1394 ArrayType::ArraySizeModifier ASM,
1395 unsigned EltTypeQuals) {
1396 // Convert the array size into a canonical width matching the pointer
1397 // size for the target.
1398 llvm::APInt ArySize(ArySizeIn);
1399 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1400
1401 // Compute the canonical ConstantArrayType.
1402 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1403 ArySize, ASM, EltTypeQuals);
1404 ConstantArrayWithoutExprType *New =
1405 new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical,
1406 ArySize, ASM, EltTypeQuals);
1407 Types.push_back(New);
1408 return QualType(New, 0);
1409}
1410
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001411/// getVariableArrayType - Returns a non-unique reference to the type for a
1412/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001413QualType ASTContext::getVariableArrayType(QualType EltTy,
1414 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001415 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001416 unsigned EltTypeQuals,
1417 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001418 // Since we don't unique expressions, it isn't possible to unique VLA's
1419 // that have an expression provided for their size.
1420
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001421 VariableArrayType *New =
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001422 new(*this,8)VariableArrayType(EltTy, QualType(),
1423 NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001424
1425 VariableArrayTypes.push_back(New);
1426 Types.push_back(New);
1427 return QualType(New, 0);
1428}
1429
Douglas Gregor898574e2008-12-05 23:32:09 +00001430/// getDependentSizedArrayType - Returns a non-unique reference to
1431/// the type for a dependently-sized array of the specified element
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001432/// type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001433QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1434 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001435 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001436 unsigned EltTypeQuals,
1437 SourceRange Brackets) {
Mike Stump1eb44332009-09-09 15:08:12 +00001438 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor898574e2008-12-05 23:32:09 +00001439 "Size must be type- or value-dependent!");
1440
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001441 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001442 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001443 EltTypeQuals, NumElts);
Douglas Gregor898574e2008-12-05 23:32:09 +00001444
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001445 void *InsertPos = 0;
1446 DependentSizedArrayType *Canon
1447 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1448 DependentSizedArrayType *New;
1449 if (Canon) {
1450 // We already have a canonical version of this array type; use it as
1451 // the canonical type for a newly-built type.
Mike Stump1eb44332009-09-09 15:08:12 +00001452 New = new (*this,8) DependentSizedArrayType(*this, EltTy,
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001453 QualType(Canon, 0),
1454 NumElts, ASM, EltTypeQuals,
1455 Brackets);
1456 } else {
1457 QualType CanonEltTy = getCanonicalType(EltTy);
1458 if (CanonEltTy == EltTy) {
1459 New = new (*this,8) DependentSizedArrayType(*this, EltTy, QualType(),
1460 NumElts, ASM, EltTypeQuals,
1461 Brackets);
1462 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1463 } else {
1464 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1465 ASM, EltTypeQuals,
1466 SourceRange());
1467 New = new (*this,8) DependentSizedArrayType(*this, EltTy, Canon,
1468 NumElts, ASM, EltTypeQuals,
Mike Stump1eb44332009-09-09 15:08:12 +00001469 Brackets);
Douglas Gregor04d4bee2009-07-31 00:23:35 +00001470 }
1471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Douglas Gregor898574e2008-12-05 23:32:09 +00001473 Types.push_back(New);
1474 return QualType(New, 0);
1475}
1476
Eli Friedmanc5773c42008-02-15 18:16:39 +00001477QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1478 ArrayType::ArraySizeModifier ASM,
1479 unsigned EltTypeQuals) {
1480 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001481 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001482
1483 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001484 if (IncompleteArrayType *ATP =
Eli Friedmanc5773c42008-02-15 18:16:39 +00001485 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1486 return QualType(ATP, 0);
1487
1488 // If the element type isn't canonical, this won't be a canonical type
1489 // either, so fill in the canonical type field.
1490 QualType Canonical;
1491
1492 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001493 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001494 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001495
1496 // Get the new insert position for the node we care about.
1497 IncompleteArrayType *NewIP =
1498 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001499 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001500 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001501
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001502 IncompleteArrayType *New
1503 = new (*this,8) IncompleteArrayType(EltTy, Canonical,
1504 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001505
1506 IncompleteArrayTypes.InsertNode(New, InsertPos);
1507 Types.push_back(New);
1508 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001509}
1510
Steve Naroff73322922007-07-18 18:00:27 +00001511/// getVectorType - Return the unique reference to a vector type of
1512/// the specified element type and size. VectorType must be a built-in type.
1513QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Chris Lattnerf52ab252008-04-06 22:59:24 +00001516 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001517 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 // Check if we've already instantiated a vector of this type.
1520 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001521 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001522 void *InsertPos = 0;
1523 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1524 return QualType(VTP, 0);
1525
1526 // If the element type isn't canonical, this won't be a canonical type either,
1527 // so fill in the canonical type field.
1528 QualType Canonical;
1529 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001530 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Reid Spencer5f016e22007-07-11 17:01:13 +00001532 // Get the new insert position for the node we care about.
1533 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001534 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001535 }
Steve Narofff83820b2009-01-27 22:08:43 +00001536 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001537 VectorTypes.InsertNode(New, InsertPos);
1538 Types.push_back(New);
1539 return QualType(New, 0);
1540}
1541
Nate Begeman213541a2008-04-18 23:10:10 +00001542/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001543/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001544QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001545 BuiltinType *baseType;
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Chris Lattnerf52ab252008-04-06 22:59:24 +00001547 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001548 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Steve Naroff73322922007-07-18 18:00:27 +00001550 // Check if we've already instantiated a vector of this type.
1551 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001552 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001553 void *InsertPos = 0;
1554 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1555 return QualType(VTP, 0);
1556
1557 // If the element type isn't canonical, this won't be a canonical type either,
1558 // so fill in the canonical type field.
1559 QualType Canonical;
1560 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001561 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Steve Naroff73322922007-07-18 18:00:27 +00001563 // Get the new insert position for the node we care about.
1564 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001565 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001566 }
Steve Narofff83820b2009-01-27 22:08:43 +00001567 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001568 VectorTypes.InsertNode(New, InsertPos);
1569 Types.push_back(New);
1570 return QualType(New, 0);
1571}
1572
Mike Stump1eb44332009-09-09 15:08:12 +00001573QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001574 Expr *SizeExpr,
1575 SourceLocation AttrLoc) {
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001576 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001577 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001578 SizeExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001580 void *InsertPos = 0;
1581 DependentSizedExtVectorType *Canon
1582 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1583 DependentSizedExtVectorType *New;
1584 if (Canon) {
1585 // We already have a canonical version of this array type; use it as
1586 // the canonical type for a newly-built type.
1587 New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
1588 QualType(Canon, 0),
1589 SizeExpr, AttrLoc);
1590 } else {
1591 QualType CanonVecTy = getCanonicalType(vecType);
1592 if (CanonVecTy == vecType) {
Mike Stump1eb44332009-09-09 15:08:12 +00001593 New = new (*this,8) DependentSizedExtVectorType(*this, vecType,
1594 QualType(), SizeExpr,
Douglas Gregor2ec09f12009-07-31 03:54:25 +00001595 AttrLoc);
1596 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1597 } else {
1598 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1599 SourceLocation());
1600 New = new (*this,8) DependentSizedExtVectorType(*this, vecType, Canon,
1601 SizeExpr, AttrLoc);
1602 }
1603 }
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001605 Types.push_back(New);
1606 return QualType(New, 0);
1607}
1608
Douglas Gregor72564e72009-02-26 23:50:07 +00001609/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001610///
Mike Stump24556362009-07-25 21:26:53 +00001611QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 // Unique functions, to guarantee there is only one function of a particular
1613 // structure.
1614 llvm::FoldingSetNodeID ID;
Mike Stump24556362009-07-25 21:26:53 +00001615 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Reid Spencer5f016e22007-07-11 17:01:13 +00001617 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001618 if (FunctionNoProtoType *FT =
Douglas Gregor72564e72009-02-26 23:50:07 +00001619 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 return QualType(FT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 QualType Canonical;
1623 if (!ResultTy->isCanonical()) {
Mike Stump24556362009-07-25 21:26:53 +00001624 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001627 FunctionNoProtoType *NewIP =
1628 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001629 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 }
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Mike Stump24556362009-07-25 21:26:53 +00001632 FunctionNoProtoType *New
1633 = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001635 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 return QualType(New, 0);
1637}
1638
1639/// getFunctionType - Return a normal function type with a typed argument
1640/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001641QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001642 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001643 unsigned TypeQuals, bool hasExceptionSpec,
1644 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump24556362009-07-25 21:26:53 +00001645 const QualType *ExArray, bool NoReturn) {
Anders Carlsson83913e32009-09-16 23:47:08 +00001646 if (LangOpts.CPlusPlus) {
1647 for (unsigned i = 0; i != NumArgs; ++i)
1648 assert(!ArgArray[i].getCVRQualifiers() &&
1649 "C++ arguments can't have toplevel CVR qualifiers!");
1650 }
1651
Reid Spencer5f016e22007-07-11 17:01:13 +00001652 // Unique functions, to guarantee there is only one function of a particular
1653 // structure.
1654 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001655 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001656 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001657 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001658
1659 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001660 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001661 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001663
1664 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001666 if (hasExceptionSpec)
1667 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1669 if (!ArgArray[i]->isCanonical())
1670 isCanonical = false;
1671
1672 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001673 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 QualType Canonical;
1675 if (!isCanonical) {
1676 llvm::SmallVector<QualType, 16> CanonicalArgs;
1677 CanonicalArgs.reserve(NumArgs);
1678 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001679 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001680
Chris Lattnerf52ab252008-04-06 22:59:24 +00001681 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001682 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001683 isVariadic, TypeQuals, false,
1684 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001685
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001687 FunctionProtoType *NewIP =
1688 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001689 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001690 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001691
Douglas Gregor72564e72009-02-26 23:50:07 +00001692 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001693 // for two variable size arrays (for parameter and exception types) at the
1694 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001695 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001696 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1697 NumArgs*sizeof(QualType) +
1698 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001699 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001700 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001701 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001703 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 return QualType(FTP, 0);
1705}
1706
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001707/// getTypeDeclType - Return the unique reference to the type for the
1708/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001709QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001710 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001711 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001712
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001713 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001714 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001715 else if (isa<TemplateTypeParmDecl>(Decl)) {
1716 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001717 } else if (ObjCInterfaceDecl *ObjCInterface
1718 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001719 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001720
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001721 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001722 if (PrevDecl)
1723 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001724 else
1725 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001726 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001727 if (PrevDecl)
1728 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001729 else
1730 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001731 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001732 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001733
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001734 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001735 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001736}
1737
Reid Spencer5f016e22007-07-11 17:01:13 +00001738/// getTypedefType - Return the unique reference to the type for the
1739/// specified typename decl.
1740QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1741 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Chris Lattnerf52ab252008-04-06 22:59:24 +00001743 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001744 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 Types.push_back(Decl->TypeForDecl);
1746 return QualType(Decl->TypeForDecl, 0);
1747}
1748
Douglas Gregorfab9d672009-02-05 23:33:38 +00001749/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001750/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001751/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001752QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001753 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001754 IdentifierInfo *Name) {
1755 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001756 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001757 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001758 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001759 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1760
1761 if (TypeParm)
1762 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001764 if (Name) {
1765 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
1766 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
1767 Name, Canon);
1768 } else
1769 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001770
1771 Types.push_back(TypeParm);
1772 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1773
1774 return QualType(TypeParm, 0);
1775}
1776
Mike Stump1eb44332009-09-09 15:08:12 +00001777QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001778ASTContext::getTemplateSpecializationType(TemplateName Template,
1779 const TemplateArgument *Args,
1780 unsigned NumArgs,
1781 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001782 if (!Canon.isNull())
1783 Canon = getCanonicalType(Canon);
1784 else {
1785 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001786 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1787 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1788 CanonArgs.reserve(NumArgs);
1789 for (unsigned I = 0; I != NumArgs; ++I)
1790 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1791
1792 // Determine whether this canonical template specialization type already
1793 // exists.
1794 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001795 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001796 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001797
1798 void *InsertPos = 0;
1799 TemplateSpecializationType *Spec
1800 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Douglas Gregor1275ae02009-07-28 23:00:59 +00001802 if (!Spec) {
1803 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001804 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001805 sizeof(TemplateArgument) * NumArgs),
1806 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001807 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001808 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001809 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001810 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001811 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001812 }
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Douglas Gregorb88e8882009-07-30 17:40:51 +00001814 if (Canon.isNull())
1815 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001816 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001817 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001818 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001819
Douglas Gregor1275ae02009-07-28 23:00:59 +00001820 // Allocate the (non-canonical) template specialization type, but don't
1821 // try to unique it: these types typically have location information that
1822 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001823 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001824 sizeof(TemplateArgument) * NumArgs),
1825 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001826 TemplateSpecializationType *Spec
1827 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001828 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Douglas Gregor55f6b142009-02-09 18:46:07 +00001830 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001831 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001832}
1833
Mike Stump1eb44332009-09-09 15:08:12 +00001834QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001835ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001836 QualType NamedType) {
1837 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001838 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001839
1840 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001841 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001842 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1843 if (T)
1844 return QualType(T, 0);
1845
Mike Stump1eb44332009-09-09 15:08:12 +00001846 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001847 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001848 Types.push_back(T);
1849 QualifiedNameTypes.InsertNode(T, InsertPos);
1850 return QualType(T, 0);
1851}
1852
Mike Stump1eb44332009-09-09 15:08:12 +00001853QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001854 const IdentifierInfo *Name,
1855 QualType Canon) {
1856 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1857
1858 if (Canon.isNull()) {
1859 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1860 if (CanonNNS != NNS)
1861 Canon = getTypenameType(CanonNNS, Name);
1862 }
1863
1864 llvm::FoldingSetNodeID ID;
1865 TypenameType::Profile(ID, NNS, Name);
1866
1867 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001868 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001869 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1870 if (T)
1871 return QualType(T, 0);
1872
1873 T = new (*this) TypenameType(NNS, Name, Canon);
1874 Types.push_back(T);
1875 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001876 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001877}
1878
Mike Stump1eb44332009-09-09 15:08:12 +00001879QualType
1880ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001881 const TemplateSpecializationType *TemplateId,
1882 QualType Canon) {
1883 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1884
1885 if (Canon.isNull()) {
1886 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1887 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1888 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1889 const TemplateSpecializationType *CanonTemplateId
John McCall183700f2009-09-21 23:43:11 +00001890 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregor17343172009-04-01 00:28:59 +00001891 assert(CanonTemplateId &&
1892 "Canonical type must also be a template specialization type");
1893 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1894 }
1895 }
1896
1897 llvm::FoldingSetNodeID ID;
1898 TypenameType::Profile(ID, NNS, TemplateId);
1899
1900 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001901 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001902 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1903 if (T)
1904 return QualType(T, 0);
1905
1906 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1907 Types.push_back(T);
1908 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001909 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001910}
1911
John McCall7da24312009-09-05 00:15:47 +00001912QualType
1913ASTContext::getElaboratedType(QualType UnderlyingType,
1914 ElaboratedType::TagKind Tag) {
1915 llvm::FoldingSetNodeID ID;
1916 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001917
John McCall7da24312009-09-05 00:15:47 +00001918 void *InsertPos = 0;
1919 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1920 if (T)
1921 return QualType(T, 0);
1922
1923 QualType Canon = getCanonicalType(UnderlyingType);
1924
1925 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1926 Types.push_back(T);
1927 ElaboratedTypes.InsertNode(T, InsertPos);
1928 return QualType(T, 0);
1929}
1930
Chris Lattner88cb27a2008-04-07 04:56:42 +00001931/// CmpProtocolNames - Comparison predicate for sorting protocols
1932/// alphabetically.
1933static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1934 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001935 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001936}
1937
1938static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1939 unsigned &NumProtocols) {
1940 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Chris Lattner88cb27a2008-04-07 04:56:42 +00001942 // Sort protocols, keyed by name.
1943 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1944
1945 // Remove duplicates.
1946 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1947 NumProtocols = ProtocolsEnd-Protocols;
1948}
1949
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001950/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1951/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001952QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001953 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001954 unsigned NumProtocols) {
1955 // Sort the protocol list alphabetically to canonicalize it.
1956 if (NumProtocols)
1957 SortAndUniqueProtocols(Protocols, NumProtocols);
1958
1959 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001960 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001961
1962 void *InsertPos = 0;
1963 if (ObjCObjectPointerType *QT =
1964 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1965 return QualType(QT, 0);
1966
1967 // No Match;
1968 ObjCObjectPointerType *QType =
Steve Naroff14108da2009-07-10 23:34:53 +00001969 new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001971 Types.push_back(QType);
1972 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1973 return QualType(QType, 0);
1974}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001975
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001976/// getObjCInterfaceType - Return the unique reference to the type for the
1977/// specified ObjC interface decl. The list of protocols is optional.
1978QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001979 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump1eb44332009-09-09 15:08:12 +00001980 if (NumProtocols)
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001981 // Sort the protocol list alphabetically to canonicalize it.
1982 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001984 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001985 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001987 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001988 if (ObjCInterfaceType *QT =
1989 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001990 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001992 // No Match;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001993 ObjCInterfaceType *QType =
Mike Stump1eb44332009-09-09 15:08:12 +00001994 new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001995 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001996 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001997 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001998 return QualType(QType, 0);
1999}
2000
Douglas Gregor72564e72009-02-26 23:50:07 +00002001/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
2002/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00002003/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00002004/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002005/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002006QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002007 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002008 if (tofExpr->isTypeDependent()) {
2009 llvm::FoldingSetNodeID ID;
2010 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Douglas Gregorb1975722009-07-30 23:18:24 +00002012 void *InsertPos = 0;
2013 DependentTypeOfExprType *Canon
2014 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2015 if (Canon) {
2016 // We already have a "canonical" version of an identical, dependent
2017 // typeof(expr) type. Use that as our canonical type.
Mike Stump1eb44332009-09-09 15:08:12 +00002018 toe = new (*this, 8) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002019 QualType((TypeOfExprType*)Canon, 0));
2020 }
2021 else {
2022 // Build a new, canonical typeof(expr) type.
2023 Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr);
2024 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2025 toe = Canon;
2026 }
2027 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002028 QualType Canonical = getCanonicalType(tofExpr->getType());
2029 toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
2030 }
Steve Naroff9752f252007-08-01 18:02:17 +00002031 Types.push_back(toe);
2032 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002033}
2034
Steve Naroff9752f252007-08-01 18:02:17 +00002035/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2036/// TypeOfType AST's. The only motivation to unique these nodes would be
2037/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002038/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002039/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002040QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002041 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00002042 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002043 Types.push_back(tot);
2044 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002045}
2046
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002047/// getDecltypeForExpr - Given an expr, will return the decltype for that
2048/// expression, according to the rules in C++0x [dcl.type.simple]p4
2049static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002050 if (e->isTypeDependent())
2051 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002053 // If e is an id expression or a class member access, decltype(e) is defined
2054 // as the type of the entity named by e.
2055 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2056 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2057 return VD->getType();
2058 }
2059 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2060 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2061 return FD->getType();
2062 }
2063 // If e is a function call or an invocation of an overloaded operator,
2064 // (parentheses around e are ignored), decltype(e) is defined as the
2065 // return type of that function.
2066 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2067 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002069 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002070
2071 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002072 // defined as T&, otherwise decltype(e) is defined as T.
2073 if (e->isLvalue(Context) == Expr::LV_Valid)
2074 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002075
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002076 return T;
2077}
2078
Anders Carlsson395b4752009-06-24 19:06:50 +00002079/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2080/// DecltypeType AST's. The only motivation to unique these nodes would be
2081/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002082/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002083/// on canonical type's (which are always unique).
2084QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002085 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002086 if (e->isTypeDependent()) {
2087 llvm::FoldingSetNodeID ID;
2088 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002090 void *InsertPos = 0;
2091 DependentDecltypeType *Canon
2092 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2093 if (Canon) {
2094 // We already have a "canonical" version of an equivalent, dependent
2095 // decltype type. Use that as our canonical type.
2096 dt = new (*this, 8) DecltypeType(e, DependentTy,
2097 QualType((DecltypeType*)Canon, 0));
2098 }
2099 else {
2100 // Build a new, canonical typeof(expr) type.
2101 Canon = new (*this, 8) DependentDecltypeType(*this, e);
2102 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2103 dt = Canon;
2104 }
2105 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002106 QualType T = getDecltypeForExpr(e, *this);
Mike Stump1eb44332009-09-09 15:08:12 +00002107 dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002108 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002109 Types.push_back(dt);
2110 return QualType(dt, 0);
2111}
2112
Reid Spencer5f016e22007-07-11 17:01:13 +00002113/// getTagDeclType - Return the unique reference to the type for the
2114/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002115QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002116 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002117 // FIXME: What is the design on getTagDeclType when it requires casting
2118 // away const? mutable?
2119 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002120}
2121
Mike Stump1eb44332009-09-09 15:08:12 +00002122/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2123/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2124/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002125QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002126 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002127}
2128
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002129/// getSignedWCharType - Return the type of "signed wchar_t".
2130/// Used when in C++, as a GCC extension.
2131QualType ASTContext::getSignedWCharType() const {
2132 // FIXME: derive from "Target" ?
2133 return WCharTy;
2134}
2135
2136/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2137/// Used when in C++, as a GCC extension.
2138QualType ASTContext::getUnsignedWCharType() const {
2139 // FIXME: derive from "Target" ?
2140 return UnsignedIntTy;
2141}
2142
Chris Lattner8b9023b2007-07-13 03:05:23 +00002143/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2144/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2145QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002146 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002147}
2148
Chris Lattnere6327742008-04-02 05:18:44 +00002149//===----------------------------------------------------------------------===//
2150// Type Operators
2151//===----------------------------------------------------------------------===//
2152
Chris Lattner77c96472008-04-06 22:41:35 +00002153/// getCanonicalType - Return the canonical (structural) type corresponding to
2154/// the specified potentially non-canonical type. The non-canonical version
2155/// of a type may have many "decorated" versions of types. Decorators can
2156/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2157/// to be free of any of these, allowing two canonical types to be compared
2158/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002159CanQualType ASTContext::getCanonicalType(QualType T) {
Chris Lattner77c96472008-04-06 22:41:35 +00002160 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002162 // If the result has type qualifiers, make sure to canonicalize them as well.
2163 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
Mike Stump1eb44332009-09-09 15:08:12 +00002164 if (TypeQuals == 0)
Douglas Gregor50d62d12009-08-05 05:36:45 +00002165 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002166
2167 // If the type qualifiers are on an array type, get the canonical type of the
2168 // array with the qualifiers applied to the element type.
2169 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2170 if (!AT)
Douglas Gregor50d62d12009-08-05 05:36:45 +00002171 return CanQualType::CreateUnsafe(CanType.getQualifiedType(TypeQuals));
Mike Stump1eb44332009-09-09 15:08:12 +00002172
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002173 // Get the canonical version of the element with the extra qualifiers on it.
2174 // This can recursively sink qualifiers through multiple levels of arrays.
2175 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
2176 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002177
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002178 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002179 return CanQualType::CreateUnsafe(
2180 getConstantArrayType(NewEltTy, CAT->getSize(),
2181 CAT->getSizeModifier(),
2182 CAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002183 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002184 return CanQualType::CreateUnsafe(
2185 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
2186 IAT->getIndexTypeQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002187
Douglas Gregor898574e2008-12-05 23:32:09 +00002188 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002189 return CanQualType::CreateUnsafe(
2190 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002191 DSAT->getSizeExpr() ?
2192 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002193 DSAT->getSizeModifier(),
2194 DSAT->getIndexTypeQualifier(),
2195 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002196
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002197 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002198 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002199 VAT->getSizeExpr() ?
2200 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002201 VAT->getSizeModifier(),
2202 VAT->getIndexTypeQualifier(),
2203 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002204}
2205
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002206TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2207 // If this template name refers to a template, the canonical
2208 // template name merely stores the template itself.
2209 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002210 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002211
Mike Stump1eb44332009-09-09 15:08:12 +00002212 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002213 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002214 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2215 OverloadedFunctionDecl *CanonOvl = 0;
2216 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2217 FEnd = Ovl->function_end();
2218 F != FEnd; ++F) {
2219 Decl *Canon = F->get()->getCanonicalDecl();
2220 if (CanonOvl || Canon != F->get()) {
2221 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002222 CanonOvl = OverloadedFunctionDecl::Create(*this,
2223 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002224 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002226 CanonOvl->addOverload(
2227 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2228 }
2229 }
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002231 return TemplateName(CanonOvl? CanonOvl : Ovl);
2232 }
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002234 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2235 assert(DTN && "Non-dependent template names must refer to template decls.");
2236 return DTN->CanonicalTemplateName;
2237}
2238
Mike Stump1eb44332009-09-09 15:08:12 +00002239TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002240ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2241 switch (Arg.getKind()) {
2242 case TemplateArgument::Null:
2243 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Douglas Gregor1275ae02009-07-28 23:00:59 +00002245 case TemplateArgument::Expression:
2246 // FIXME: Build canonical expression?
2247 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Douglas Gregor1275ae02009-07-28 23:00:59 +00002249 case TemplateArgument::Declaration:
2250 return TemplateArgument(SourceLocation(),
2251 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002252
Douglas Gregor1275ae02009-07-28 23:00:59 +00002253 case TemplateArgument::Integral:
2254 return TemplateArgument(SourceLocation(),
2255 *Arg.getAsIntegral(),
2256 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002257
Douglas Gregor1275ae02009-07-28 23:00:59 +00002258 case TemplateArgument::Type:
2259 return TemplateArgument(SourceLocation(),
2260 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Douglas Gregor1275ae02009-07-28 23:00:59 +00002262 case TemplateArgument::Pack: {
2263 // FIXME: Allocate in ASTContext
2264 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2265 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002266 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002267 AEnd = Arg.pack_end();
2268 A != AEnd; (void)++A, ++Idx)
2269 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Douglas Gregor1275ae02009-07-28 23:00:59 +00002271 TemplateArgument Result;
2272 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2273 return Result;
2274 }
2275 }
2276
2277 // Silence GCC warning
2278 assert(false && "Unhandled template argument kind");
2279 return TemplateArgument();
2280}
2281
Douglas Gregord57959a2009-03-27 23:10:48 +00002282NestedNameSpecifier *
2283ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002284 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002285 return 0;
2286
2287 switch (NNS->getKind()) {
2288 case NestedNameSpecifier::Identifier:
2289 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002290 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002291 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2292 NNS->getAsIdentifier());
2293
2294 case NestedNameSpecifier::Namespace:
2295 // A namespace is canonical; build a nested-name-specifier with
2296 // this namespace and no prefix.
2297 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2298
2299 case NestedNameSpecifier::TypeSpec:
2300 case NestedNameSpecifier::TypeSpecWithTemplate: {
2301 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002302 return NestedNameSpecifier::Create(*this, 0,
2303 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002304 T.getTypePtr());
2305 }
2306
2307 case NestedNameSpecifier::Global:
2308 // The global specifier is canonical and unique.
2309 return NNS;
2310 }
2311
2312 // Required to silence a GCC warning
2313 return 0;
2314}
2315
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002316
2317const ArrayType *ASTContext::getAsArrayType(QualType T) {
2318 // Handle the non-qualified case efficiently.
2319 if (T.getCVRQualifiers() == 0) {
2320 // Handle the common positive case fast.
2321 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2322 return AT;
2323 }
Mike Stump1eb44332009-09-09 15:08:12 +00002324
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002325 // Handle the common negative case fast, ignoring CVR qualifiers.
2326 QualType CType = T->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002327
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002328 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002329 // test.
2330 if (!isa<ArrayType>(CType) &&
2331 !isa<ArrayType>(CType.getUnqualifiedType()))
2332 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002334 // Apply any CVR qualifiers from the array type to the element type. This
2335 // implements C99 6.7.3p8: "If the specification of an array type includes
2336 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002337
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002338 // If we get here, we either have type qualifiers on the type, or we have
2339 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002340 // we must propagate them down into the element type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002341 unsigned CVRQuals = T.getCVRQualifiers();
2342 unsigned AddrSpace = 0;
2343 Type *Ty = T.getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002345 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002346 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002347 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
2348 AddrSpace = EXTQT->getAddressSpace();
2349 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002350 } else {
2351 T = Ty->getDesugaredType();
2352 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
2353 break;
2354 CVRQuals |= T.getCVRQualifiers();
2355 Ty = T.getTypePtr();
2356 }
2357 }
Mike Stump1eb44332009-09-09 15:08:12 +00002358
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002359 // If we have a simple case, just return now.
2360 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
2361 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
2362 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002364 // Otherwise, we have an array and we have qualifiers on it. Push the
2365 // qualifiers into the array element type and return a new array type.
2366 // Get the canonical version of the element with the extra qualifiers on it.
2367 // This can recursively sink qualifiers through multiple levels of arrays.
2368 QualType NewEltTy = ATy->getElementType();
2369 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002370 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002371 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002373 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2374 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2375 CAT->getSizeModifier(),
2376 CAT->getIndexTypeQualifier()));
2377 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2378 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2379 IAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002380 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002381
Mike Stump1eb44332009-09-09 15:08:12 +00002382 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002383 = dyn_cast<DependentSizedArrayType>(ATy))
2384 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002385 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002386 DSAT->getSizeExpr() ?
2387 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002388 DSAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002389 DSAT->getIndexTypeQualifier(),
2390 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002391
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002392 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002393 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002394 VAT->getSizeExpr() ?
2395 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002396 VAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002397 VAT->getIndexTypeQualifier(),
2398 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002399}
2400
2401
Chris Lattnere6327742008-04-02 05:18:44 +00002402/// getArrayDecayedType - Return the properly qualified result of decaying the
2403/// specified array type to a pointer. This operation is non-trivial when
2404/// handling typedefs etc. The canonical type of "T" must be an array type,
2405/// this returns a pointer to a properly qualified element of the array.
2406///
2407/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2408QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002409 // Get the element type with 'getAsArrayType' so that we don't lose any
2410 // typedefs in the element type of the array. This also handles propagation
2411 // of type qualifiers from the array type into the element type if present
2412 // (C99 6.7.3p8).
2413 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2414 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002416 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002417
2418 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002419 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00002420}
2421
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002422QualType ASTContext::getBaseElementType(QualType QT) {
2423 QualifierSet qualifiers;
2424 while (true) {
2425 const Type *UT = qualifiers.strip(QT);
2426 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2427 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002428 } else {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002429 return qualifiers.apply(QT, *this);
2430 }
2431 }
2432}
2433
Daniel Dunbard786f6a2009-01-05 22:14:37 +00002434QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00002435 QualType ElemTy = VAT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002436
Anders Carlsson6183a992008-12-21 03:44:36 +00002437 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
2438 return getBaseElementType(VAT);
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Anders Carlsson6183a992008-12-21 03:44:36 +00002440 return ElemTy;
2441}
2442
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002443/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002444uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002445ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2446 uint64_t ElementCount = 1;
2447 do {
2448 ElementCount *= CA->getSize().getZExtValue();
2449 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2450 } while (CA);
2451 return ElementCount;
2452}
2453
Reid Spencer5f016e22007-07-11 17:01:13 +00002454/// getFloatingRank - Return a relative rank for floating point types.
2455/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002456static FloatingRank getFloatingRank(QualType T) {
John McCall183700f2009-09-21 23:43:11 +00002457 if (const ComplexType *CT = T->getAs<ComplexType>())
Reid Spencer5f016e22007-07-11 17:01:13 +00002458 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002459
John McCall183700f2009-09-21 23:43:11 +00002460 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2461 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002462 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002463 case BuiltinType::Float: return FloatRank;
2464 case BuiltinType::Double: return DoubleRank;
2465 case BuiltinType::LongDouble: return LongDoubleRank;
2466 }
2467}
2468
Mike Stump1eb44332009-09-09 15:08:12 +00002469/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2470/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002471/// 'typeDomain' is a real floating point or complex type.
2472/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002473QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2474 QualType Domain) const {
2475 FloatingRank EltRank = getFloatingRank(Size);
2476 if (Domain->isComplexType()) {
2477 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002478 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002479 case FloatRank: return FloatComplexTy;
2480 case DoubleRank: return DoubleComplexTy;
2481 case LongDoubleRank: return LongDoubleComplexTy;
2482 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002483 }
Chris Lattner1361b112008-04-06 23:58:54 +00002484
2485 assert(Domain->isRealFloatingType() && "Unknown domain!");
2486 switch (EltRank) {
2487 default: assert(0 && "getFloatingRank(): illegal value for rank");
2488 case FloatRank: return FloatTy;
2489 case DoubleRank: return DoubleTy;
2490 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002491 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002492}
2493
Chris Lattner7cfeb082008-04-06 23:55:33 +00002494/// getFloatingTypeOrder - Compare the rank of the two specified floating
2495/// point types, ignoring the domain of the type (i.e. 'double' ==
2496/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002497/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002498int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2499 FloatingRank LHSR = getFloatingRank(LHS);
2500 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002501
Chris Lattnera75cea32008-04-06 23:38:49 +00002502 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002503 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002504 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002505 return 1;
2506 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002507}
2508
Chris Lattnerf52ab252008-04-06 22:59:24 +00002509/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2510/// routine will assert if passed a built-in type that isn't an integer or enum,
2511/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002512unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002513 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002514 if (EnumType* ET = dyn_cast<EnumType>(T))
2515 T = ET->getDecl()->getIntegerType().getTypePtr();
2516
Eli Friedmana3426752009-07-05 23:44:27 +00002517 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2518 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2519
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002520 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2521 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2522
2523 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2524 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2525
Eli Friedmanf98aba32009-02-13 02:31:07 +00002526 // There are two things which impact the integer rank: the width, and
2527 // the ordering of builtins. The builtin ordering is encoded in the
2528 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002529 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002530 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002531
Chris Lattnerf52ab252008-04-06 22:59:24 +00002532 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002533 default: assert(0 && "getIntegerRank(): not a built-in integer");
2534 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002535 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002536 case BuiltinType::Char_S:
2537 case BuiltinType::Char_U:
2538 case BuiltinType::SChar:
2539 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002540 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002541 case BuiltinType::Short:
2542 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002543 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002544 case BuiltinType::Int:
2545 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002546 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002547 case BuiltinType::Long:
2548 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002549 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002550 case BuiltinType::LongLong:
2551 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002552 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002553 case BuiltinType::Int128:
2554 case BuiltinType::UInt128:
2555 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002556 }
2557}
2558
Eli Friedman04e83572009-08-20 04:21:42 +00002559/// \brief Whether this is a promotable bitfield reference according
2560/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2561///
2562/// \returns the type this bit-field will promote to, or NULL if no
2563/// promotion occurs.
2564QualType ASTContext::isPromotableBitField(Expr *E) {
2565 FieldDecl *Field = E->getBitField();
2566 if (!Field)
2567 return QualType();
2568
2569 QualType FT = Field->getType();
2570
2571 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2572 uint64_t BitWidth = BitWidthAP.getZExtValue();
2573 uint64_t IntSize = getTypeSize(IntTy);
2574 // GCC extension compatibility: if the bit-field size is less than or equal
2575 // to the size of int, it gets promoted no matter what its type is.
2576 // For instance, unsigned long bf : 4 gets promoted to signed int.
2577 if (BitWidth < IntSize)
2578 return IntTy;
2579
2580 if (BitWidth == IntSize)
2581 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2582
2583 // Types bigger than int are not subject to promotions, and therefore act
2584 // like the base type.
2585 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2586 // is ridiculous.
2587 return QualType();
2588}
2589
Eli Friedmana95d7572009-08-19 07:44:53 +00002590/// getPromotedIntegerType - Returns the type that Promotable will
2591/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2592/// integer type.
2593QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2594 assert(!Promotable.isNull());
2595 assert(Promotable->isPromotableIntegerType());
2596 if (Promotable->isSignedIntegerType())
2597 return IntTy;
2598 uint64_t PromotableSize = getTypeSize(Promotable);
2599 uint64_t IntSize = getTypeSize(IntTy);
2600 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2601 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2602}
2603
Mike Stump1eb44332009-09-09 15:08:12 +00002604/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002605/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002606/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002607int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002608 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2609 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002610 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Chris Lattnerf52ab252008-04-06 22:59:24 +00002612 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2613 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Chris Lattner7cfeb082008-04-06 23:55:33 +00002615 unsigned LHSRank = getIntegerRank(LHSC);
2616 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002617
Chris Lattner7cfeb082008-04-06 23:55:33 +00002618 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2619 if (LHSRank == RHSRank) return 0;
2620 return LHSRank > RHSRank ? 1 : -1;
2621 }
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Chris Lattner7cfeb082008-04-06 23:55:33 +00002623 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2624 if (LHSUnsigned) {
2625 // If the unsigned [LHS] type is larger, return it.
2626 if (LHSRank >= RHSRank)
2627 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Chris Lattner7cfeb082008-04-06 23:55:33 +00002629 // If the signed type can represent all values of the unsigned type, it
2630 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002631 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002632 return -1;
2633 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002634
Chris Lattner7cfeb082008-04-06 23:55:33 +00002635 // If the unsigned [RHS] type is larger, return it.
2636 if (RHSRank >= LHSRank)
2637 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002638
Chris Lattner7cfeb082008-04-06 23:55:33 +00002639 // If the signed type can represent all values of the unsigned type, it
2640 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002641 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002642 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002643}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002644
Mike Stump1eb44332009-09-09 15:08:12 +00002645// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002646QualType ASTContext::getCFConstantStringType() {
2647 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002648 CFConstantStringTypeDecl =
2649 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002650 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002651 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Anders Carlsson71993dd2007-08-17 05:31:46 +00002653 // const int *isa;
Mike Stump1eb44332009-09-09 15:08:12 +00002654 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002655 // int flags;
2656 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002657 // const char *str;
Mike Stump1eb44332009-09-09 15:08:12 +00002658 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002659 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002660 FieldTypes[3] = LongTy;
2661
Anders Carlsson71993dd2007-08-17 05:31:46 +00002662 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002663 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002664 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002665 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002666 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002667 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002668 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002669 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002670 }
2671
2672 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002673 }
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Anders Carlsson71993dd2007-08-17 05:31:46 +00002675 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002676}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002677
Douglas Gregor319ac892009-04-23 22:29:11 +00002678void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002679 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002680 assert(Rec && "Invalid CFConstantStringType");
2681 CFConstantStringTypeDecl = Rec->getDecl();
2682}
2683
Mike Stump1eb44332009-09-09 15:08:12 +00002684QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002685 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002686 ObjCFastEnumerationStateTypeDecl =
2687 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2688 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002690 QualType FieldTypes[] = {
2691 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002692 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002693 getPointerType(UnsignedLongTy),
2694 getConstantArrayType(UnsignedLongTy,
2695 llvm::APInt(32, 5), ArrayType::Normal, 0)
2696 };
Mike Stump1eb44332009-09-09 15:08:12 +00002697
Douglas Gregor44b43212008-12-11 16:49:14 +00002698 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002699 FieldDecl *Field = FieldDecl::Create(*this,
2700 ObjCFastEnumerationStateTypeDecl,
2701 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002702 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002703 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002704 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002705 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002706 }
Mike Stump1eb44332009-09-09 15:08:12 +00002707
Douglas Gregor44b43212008-12-11 16:49:14 +00002708 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002709 }
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002711 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2712}
2713
Douglas Gregor319ac892009-04-23 22:29:11 +00002714void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002715 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002716 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2717 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2718}
2719
Anders Carlssone8c49532007-10-29 06:33:42 +00002720// This returns true if a type has been typedefed to BOOL:
2721// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002722static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002723 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002724 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2725 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002726
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002727 return false;
2728}
2729
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002730/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002731/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002732int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002733 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002734
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002735 // Make all integer and enum types at least as large as an int
2736 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002737 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002738 // Treat arrays as pointers, since that's how they're passed in.
2739 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002740 sz = getTypeSize(VoidPtrTy);
2741 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002742}
2743
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002744/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002745/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002746void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002747 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002748 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002749 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002750 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002751 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002752 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002753 // Compute size of all parameters.
2754 // Start with computing size of a pointer in number of bytes.
2755 // FIXME: There might(should) be a better way of doing this computation!
2756 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002757 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002758 // The first two arguments (self and _cmd) are pointers; account for
2759 // their size.
2760 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002761 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2762 E = Decl->param_end(); PI != E; ++PI) {
2763 QualType PType = (*PI)->getType();
2764 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002765 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002766 ParmOffset += sz;
2767 }
2768 S += llvm::utostr(ParmOffset);
2769 S += "@0:";
2770 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002771
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002772 // Argument types.
2773 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002774 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2775 E = Decl->param_end(); PI != E; ++PI) {
2776 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00002777 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002778 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002779 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2780 // Use array's original type only if it has known number of
2781 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002782 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002783 PType = PVDecl->getType();
2784 } else if (PType->isFunctionType())
2785 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002786 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002787 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002788 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002789 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002790 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002791 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002792 }
2793}
2794
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002795/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002796/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002797/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2798/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00002799/// Property attributes are stored as a comma-delimited C string. The simple
2800/// attributes readonly and bycopy are encoded as single characters. The
2801/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2802/// encoded as single characters, followed by an identifier. Property types
2803/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002804/// these attributes are defined by the following enumeration:
2805/// @code
2806/// enum PropertyAttributes {
2807/// kPropertyReadOnly = 'R', // property is read-only.
2808/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2809/// kPropertyByref = '&', // property is a reference to the value last assigned
2810/// kPropertyDynamic = 'D', // property is dynamic
2811/// kPropertyGetter = 'G', // followed by getter selector name
2812/// kPropertySetter = 'S', // followed by setter selector name
2813/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2814/// kPropertyType = 't' // followed by old-style type encoding.
2815/// kPropertyWeak = 'W' // 'weak' property
2816/// kPropertyStrong = 'P' // property GC'able
2817/// kPropertyNonAtomic = 'N' // property non-atomic
2818/// };
2819/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00002820void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002821 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002822 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002823 // Collect information from the property implementation decl(s).
2824 bool Dynamic = false;
2825 ObjCPropertyImplDecl *SynthesizePID = 0;
2826
2827 // FIXME: Duplicated code due to poor abstraction.
2828 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00002829 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002830 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2831 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002832 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002833 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002834 ObjCPropertyImplDecl *PID = *i;
2835 if (PID->getPropertyDecl() == PD) {
2836 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2837 Dynamic = true;
2838 } else {
2839 SynthesizePID = PID;
2840 }
2841 }
2842 }
2843 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002844 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002845 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002846 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002847 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002848 ObjCPropertyImplDecl *PID = *i;
2849 if (PID->getPropertyDecl() == PD) {
2850 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2851 Dynamic = true;
2852 } else {
2853 SynthesizePID = PID;
2854 }
2855 }
Mike Stump1eb44332009-09-09 15:08:12 +00002856 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002857 }
2858 }
2859
2860 // FIXME: This is not very efficient.
2861 S = "T";
2862
2863 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002864 // GCC has some special rules regarding encoding of properties which
2865 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00002866 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002867 true /* outermost type */,
2868 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002869
2870 if (PD->isReadOnly()) {
2871 S += ",R";
2872 } else {
2873 switch (PD->getSetterKind()) {
2874 case ObjCPropertyDecl::Assign: break;
2875 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002876 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002877 }
2878 }
2879
2880 // It really isn't clear at all what this means, since properties
2881 // are "dynamic by default".
2882 if (Dynamic)
2883 S += ",D";
2884
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002885 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2886 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002888 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2889 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002890 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002891 }
2892
2893 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2894 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002895 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002896 }
2897
2898 if (SynthesizePID) {
2899 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2900 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002901 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002902 }
2903
2904 // FIXME: OBJCGC: weak & strong
2905}
2906
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002907/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00002908/// Another legacy compatibility encoding: 32-bit longs are encoded as
2909/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002910/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2911///
2912void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00002913 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall183700f2009-09-21 23:43:11 +00002914 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002915 if (BT->getKind() == BuiltinType::ULong &&
2916 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002917 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002918 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002919 if (BT->getKind() == BuiltinType::Long &&
2920 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002921 PointeeTy = IntTy;
2922 }
2923 }
2924}
2925
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002926void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002927 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002928 // We follow the behavior of gcc, expanding structures which are
2929 // directly pointed to, and expanding embedded structures. Note that
2930 // these rules are sufficient to prevent recursive encoding of the
2931 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00002932 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002933 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002934}
2935
Mike Stump1eb44332009-09-09 15:08:12 +00002936static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002937 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002938 const Expr *E = FD->getBitWidth();
2939 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2940 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002941 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002942 S += 'b';
2943 S += llvm::utostr(N);
2944}
2945
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002946void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2947 bool ExpandPointedToStructures,
2948 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002949 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002950 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002951 bool EncodingProperty) {
John McCall183700f2009-09-21 23:43:11 +00002952 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002953 if (FD && FD->isBitField())
2954 return EncodeBitField(this, S, FD);
2955 char encoding;
2956 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002957 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002958 case BuiltinType::Void: encoding = 'v'; break;
2959 case BuiltinType::Bool: encoding = 'B'; break;
2960 case BuiltinType::Char_U:
2961 case BuiltinType::UChar: encoding = 'C'; break;
2962 case BuiltinType::UShort: encoding = 'S'; break;
2963 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002964 case BuiltinType::ULong:
2965 encoding =
2966 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002967 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002968 case BuiltinType::UInt128: encoding = 'T'; break;
2969 case BuiltinType::ULongLong: encoding = 'Q'; break;
2970 case BuiltinType::Char_S:
2971 case BuiltinType::SChar: encoding = 'c'; break;
2972 case BuiltinType::Short: encoding = 's'; break;
2973 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002974 case BuiltinType::Long:
2975 encoding =
2976 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002977 break;
2978 case BuiltinType::LongLong: encoding = 'q'; break;
2979 case BuiltinType::Int128: encoding = 't'; break;
2980 case BuiltinType::Float: encoding = 'f'; break;
2981 case BuiltinType::Double: encoding = 'd'; break;
2982 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002983 }
Mike Stump1eb44332009-09-09 15:08:12 +00002984
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002985 S += encoding;
2986 return;
2987 }
Mike Stump1eb44332009-09-09 15:08:12 +00002988
John McCall183700f2009-09-21 23:43:11 +00002989 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002990 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00002991 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002992 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002993 return;
2994 }
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Ted Kremenek6217b802009-07-29 21:53:49 +00002996 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002997 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002998 bool isReadOnly = false;
2999 // For historical/compatibility reasons, the read-only qualifier of the
3000 // pointee gets emitted _before_ the '^'. The read-only qualifier of
3001 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00003002 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00003003 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003004 if (OutermostType && T.isConstQualified()) {
3005 isReadOnly = true;
3006 S += 'r';
3007 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003008 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003009 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003010 while (P->getAs<PointerType>())
3011 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003012 if (P.isConstQualified()) {
3013 isReadOnly = true;
3014 S += 'r';
3015 }
3016 }
3017 if (isReadOnly) {
3018 // Another legacy compatibility encoding. Some ObjC qualifier and type
3019 // combinations need to be rearranged.
3020 // Rewrite "in const" from "nr" to "rn"
3021 const char * s = S.c_str();
3022 int len = S.length();
3023 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3024 std::string replace = "rn";
3025 S.replace(S.end()-2, S.end(), replace);
3026 }
3027 }
Steve Naroff14108da2009-07-10 23:34:53 +00003028 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00003029 S += ':';
3030 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00003031 }
Mike Stump1eb44332009-09-09 15:08:12 +00003032
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003033 if (PointeeTy->isCharType()) {
3034 // char pointer types should be encoded as '*' unless it is a
3035 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003036 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003037 S += '*';
3038 return;
3039 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003040 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003041 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3042 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3043 S += '#';
3044 return;
3045 }
3046 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3047 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3048 S += '@';
3049 return;
3050 }
3051 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003052 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003053 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003054 getLegacyIntegralTypeEncoding(PointeeTy);
3055
Mike Stump1eb44332009-09-09 15:08:12 +00003056 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003057 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003058 return;
3059 }
Mike Stump1eb44332009-09-09 15:08:12 +00003060
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003061 if (const ArrayType *AT =
3062 // Ignore type qualifiers etc.
3063 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003064 if (isa<IncompleteArrayType>(AT)) {
3065 // Incomplete arrays are encoded as a pointer to the array element.
3066 S += '^';
3067
Mike Stump1eb44332009-09-09 15:08:12 +00003068 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003069 false, ExpandStructures, FD);
3070 } else {
3071 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003072
Anders Carlsson559a8332009-02-22 01:38:57 +00003073 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3074 S += llvm::utostr(CAT->getSize().getZExtValue());
3075 else {
3076 //Variable length arrays are encoded as a regular array with 0 elements.
3077 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3078 S += '0';
3079 }
Mike Stump1eb44332009-09-09 15:08:12 +00003080
3081 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003082 false, ExpandStructures, FD);
3083 S += ']';
3084 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003085 return;
3086 }
Mike Stump1eb44332009-09-09 15:08:12 +00003087
John McCall183700f2009-09-21 23:43:11 +00003088 if (T->getAs<FunctionType>()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003089 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003090 return;
3091 }
Mike Stump1eb44332009-09-09 15:08:12 +00003092
Ted Kremenek6217b802009-07-29 21:53:49 +00003093 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003094 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003095 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003096 // Anonymous structures print as '?'
3097 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3098 S += II->getName();
3099 } else {
3100 S += '?';
3101 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003102 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003103 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003104 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3105 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003106 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003107 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003108 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003109 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003110 S += '"';
3111 }
Mike Stump1eb44332009-09-09 15:08:12 +00003112
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003113 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003114 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003115 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003116 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003117 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003118 QualType qt = Field->getType();
3119 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003120 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003121 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003122 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003123 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003124 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003125 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003126 return;
3127 }
Mike Stump1eb44332009-09-09 15:08:12 +00003128
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003129 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003130 if (FD && FD->isBitField())
3131 EncodeBitField(this, S, FD);
3132 else
3133 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003134 return;
3135 }
Mike Stump1eb44332009-09-09 15:08:12 +00003136
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003137 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003138 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003139 return;
3140 }
Mike Stump1eb44332009-09-09 15:08:12 +00003141
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003142 if (T->isObjCInterfaceType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003143 // @encode(class_name)
John McCall183700f2009-09-21 23:43:11 +00003144 ObjCInterfaceDecl *OI = T->getAs<ObjCInterfaceType>()->getDecl();
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003145 S += '{';
3146 const IdentifierInfo *II = OI->getIdentifier();
3147 S += II->getName();
3148 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003149 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003150 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003151 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003152 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003153 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003154 RecFields[i]);
3155 else
Mike Stump1eb44332009-09-09 15:08:12 +00003156 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003157 FD);
3158 }
3159 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003160 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003161 }
Mike Stump1eb44332009-09-09 15:08:12 +00003162
John McCall183700f2009-09-21 23:43:11 +00003163 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003164 if (OPT->isObjCIdType()) {
3165 S += '@';
3166 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003167 }
Mike Stump1eb44332009-09-09 15:08:12 +00003168
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003169 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003170 S += '#';
3171 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003172 }
Mike Stump1eb44332009-09-09 15:08:12 +00003173
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003174 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003175 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003176 ExpandPointedToStructures,
3177 ExpandStructures, FD);
3178 if (FD || EncodingProperty) {
3179 // Note that we do extended encoding of protocol qualifer list
3180 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003181 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003182 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3183 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003184 S += '<';
3185 S += (*I)->getNameAsString();
3186 S += '>';
3187 }
3188 S += '"';
3189 }
3190 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003191 }
Mike Stump1eb44332009-09-09 15:08:12 +00003192
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003193 QualType PointeeTy = OPT->getPointeeType();
3194 if (!EncodingProperty &&
3195 isa<TypedefType>(PointeeTy.getTypePtr())) {
3196 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003197 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003198 // {...};
3199 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003200 getObjCEncodingForTypeImpl(PointeeTy, S,
3201 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003202 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003203 return;
3204 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003205
3206 S += '@';
3207 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003208 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003209 S += OPT->getInterfaceDecl()->getNameAsCString();
3210 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3211 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003212 S += '<';
3213 S += (*I)->getNameAsString();
3214 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003215 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003216 S += '"';
3217 }
3218 return;
3219 }
Mike Stump1eb44332009-09-09 15:08:12 +00003220
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003221 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003222}
3223
Mike Stump1eb44332009-09-09 15:08:12 +00003224void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003225 std::string& S) const {
3226 if (QT & Decl::OBJC_TQ_In)
3227 S += 'n';
3228 if (QT & Decl::OBJC_TQ_Inout)
3229 S += 'N';
3230 if (QT & Decl::OBJC_TQ_Out)
3231 S += 'o';
3232 if (QT & Decl::OBJC_TQ_Bycopy)
3233 S += 'O';
3234 if (QT & Decl::OBJC_TQ_Byref)
3235 S += 'R';
3236 if (QT & Decl::OBJC_TQ_Oneway)
3237 S += 'V';
3238}
3239
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003240void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003241 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003242
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003243 BuiltinVaListType = T;
3244}
3245
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003246void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003247 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003248}
3249
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003250void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003251 ObjCSelType = T;
3252
John McCall183700f2009-09-21 23:43:11 +00003253 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00003254 if (!TT)
3255 return;
3256 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003257
3258 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003259 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003260 if (!ptr)
3261 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003262 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003263 if (!rec)
3264 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003265 SelStructType = rec;
3266}
3267
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003268void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003269 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003270}
3271
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003272void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003273 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003274}
3275
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003276void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003277 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003278 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003279
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003280 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003281}
3282
Douglas Gregor7532dc62009-03-30 22:58:21 +00003283/// \brief Retrieve the template name that represents a qualified
3284/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003285TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003286 bool TemplateKeyword,
3287 TemplateDecl *Template) {
3288 llvm::FoldingSetNodeID ID;
3289 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3290
3291 void *InsertPos = 0;
3292 QualifiedTemplateName *QTN =
3293 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3294 if (!QTN) {
3295 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3296 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3297 }
3298
3299 return TemplateName(QTN);
3300}
3301
Douglas Gregord99cbe62009-07-29 18:26:50 +00003302/// \brief Retrieve the template name that represents a qualified
3303/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003304TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003305 bool TemplateKeyword,
3306 OverloadedFunctionDecl *Template) {
3307 llvm::FoldingSetNodeID ID;
3308 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003309
Douglas Gregord99cbe62009-07-29 18:26:50 +00003310 void *InsertPos = 0;
3311 QualifiedTemplateName *QTN =
3312 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3313 if (!QTN) {
3314 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3315 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3316 }
Mike Stump1eb44332009-09-09 15:08:12 +00003317
Douglas Gregord99cbe62009-07-29 18:26:50 +00003318 return TemplateName(QTN);
3319}
3320
Douglas Gregor7532dc62009-03-30 22:58:21 +00003321/// \brief Retrieve the template name that represents a dependent
3322/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003323TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003324 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003325 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003326 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003327
3328 llvm::FoldingSetNodeID ID;
3329 DependentTemplateName::Profile(ID, NNS, Name);
3330
3331 void *InsertPos = 0;
3332 DependentTemplateName *QTN =
3333 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3334
3335 if (QTN)
3336 return TemplateName(QTN);
3337
3338 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3339 if (CanonNNS == NNS) {
3340 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3341 } else {
3342 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3343 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3344 }
3345
3346 DependentTemplateNames.InsertNode(QTN, InsertPos);
3347 return TemplateName(QTN);
3348}
3349
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003350/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003351/// TargetInfo, produce the corresponding type. The unsigned @p Type
3352/// is actually a value of type @c TargetInfo::IntType.
3353QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003354 switch (Type) {
Mike Stump1eb44332009-09-09 15:08:12 +00003355 case TargetInfo::NoInt: return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003356 case TargetInfo::SignedShort: return ShortTy;
3357 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3358 case TargetInfo::SignedInt: return IntTy;
3359 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3360 case TargetInfo::SignedLong: return LongTy;
3361 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3362 case TargetInfo::SignedLongLong: return LongLongTy;
3363 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3364 }
3365
3366 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003367 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003368}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003369
3370//===----------------------------------------------------------------------===//
3371// Type Predicates.
3372//===----------------------------------------------------------------------===//
3373
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003374/// isObjCNSObjectType - Return true if this is an NSObject object using
3375/// NSObject attribute on a c-style pointer type.
3376/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003377/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003378///
3379bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3380 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3381 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003382 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003383 return true;
3384 }
Mike Stump1eb44332009-09-09 15:08:12 +00003385 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003386}
3387
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003388/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3389/// garbage collection attribute.
3390///
3391QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003392 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003393 if (getLangOptions().ObjC1 &&
3394 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003395 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003396 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003397 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003398 // as __strong.
3399 if (GCAttrs == QualType::GCNone) {
Fariborz Jahanian75212ee2009-09-10 23:38:45 +00003400 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003401 GCAttrs = QualType::Strong;
3402 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003403 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003404 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003405 // Non-pointers have none gc'able attribute regardless of the attribute
3406 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003407 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003408 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003409 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003410 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003411}
3412
Chris Lattner6ac46a42008-04-07 06:51:04 +00003413//===----------------------------------------------------------------------===//
3414// Type Compatibility Testing
3415//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003416
Mike Stump1eb44332009-09-09 15:08:12 +00003417/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003418/// compatible.
3419static bool areCompatVectorTypes(const VectorType *LHS,
3420 const VectorType *RHS) {
3421 assert(LHS->isCanonical() && RHS->isCanonical());
3422 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003423 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003424}
3425
Steve Naroff4084c302009-07-23 01:01:38 +00003426//===----------------------------------------------------------------------===//
3427// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3428//===----------------------------------------------------------------------===//
3429
3430/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3431/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003432bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3433 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003434 if (lProto == rProto)
3435 return true;
3436 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3437 E = rProto->protocol_end(); PI != E; ++PI)
3438 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3439 return true;
3440 return false;
3441}
3442
Steve Naroff4084c302009-07-23 01:01:38 +00003443/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3444/// return true if lhs's protocols conform to rhs's protocol; false
3445/// otherwise.
3446bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3447 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3448 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3449 return false;
3450}
3451
3452/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3453/// ObjCQualifiedIDType.
3454bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3455 bool compare) {
3456 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003457 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003458 lhs->isObjCIdType() || lhs->isObjCClassType())
3459 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003460 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003461 rhs->isObjCIdType() || rhs->isObjCClassType())
3462 return true;
3463
3464 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall183700f2009-09-21 23:43:11 +00003465 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003466
Steve Naroff4084c302009-07-23 01:01:38 +00003467 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003468
Steve Naroff4084c302009-07-23 01:01:38 +00003469 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003470 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003471 // make sure we check the class hierarchy.
3472 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3473 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3474 E = lhsQID->qual_end(); I != E; ++I) {
3475 // when comparing an id<P> on lhs with a static type on rhs,
3476 // see if static class implements all of id's protocols, directly or
3477 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003478 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003479 return false;
3480 }
3481 }
3482 // If there are no qualifiers and no interface, we have an 'id'.
3483 return true;
3484 }
Mike Stump1eb44332009-09-09 15:08:12 +00003485 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003486 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3487 E = lhsQID->qual_end(); I != E; ++I) {
3488 ObjCProtocolDecl *lhsProto = *I;
3489 bool match = false;
3490
3491 // when comparing an id<P> on lhs with a static type on rhs,
3492 // see if static class implements all of id's protocols, directly or
3493 // through its super class and categories.
3494 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3495 E = rhsOPT->qual_end(); J != E; ++J) {
3496 ObjCProtocolDecl *rhsProto = *J;
3497 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3498 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3499 match = true;
3500 break;
3501 }
3502 }
Mike Stump1eb44332009-09-09 15:08:12 +00003503 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003504 // make sure we check the class hierarchy.
3505 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3506 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3507 E = lhsQID->qual_end(); I != E; ++I) {
3508 // when comparing an id<P> on lhs with a static type on rhs,
3509 // see if static class implements all of id's protocols, directly or
3510 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003511 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003512 match = true;
3513 break;
3514 }
3515 }
3516 }
3517 if (!match)
3518 return false;
3519 }
Mike Stump1eb44332009-09-09 15:08:12 +00003520
Steve Naroff4084c302009-07-23 01:01:38 +00003521 return true;
3522 }
Mike Stump1eb44332009-09-09 15:08:12 +00003523
Steve Naroff4084c302009-07-23 01:01:38 +00003524 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3525 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3526
Mike Stump1eb44332009-09-09 15:08:12 +00003527 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003528 lhs->getAsObjCInterfacePointerType()) {
3529 if (lhsOPT->qual_empty()) {
3530 bool match = false;
3531 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3532 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3533 E = rhsQID->qual_end(); I != E; ++I) {
3534 // when comparing an id<P> on lhs with a static type on rhs,
3535 // see if static class implements all of id's protocols, directly or
3536 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003537 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003538 match = true;
3539 break;
3540 }
3541 }
3542 if (!match)
3543 return false;
3544 }
3545 return true;
3546 }
Mike Stump1eb44332009-09-09 15:08:12 +00003547 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003548 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3549 E = lhsOPT->qual_end(); I != E; ++I) {
3550 ObjCProtocolDecl *lhsProto = *I;
3551 bool match = false;
3552
3553 // when comparing an id<P> on lhs with a static type on rhs,
3554 // see if static class implements all of id's protocols, directly or
3555 // through its super class and categories.
3556 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3557 E = rhsQID->qual_end(); J != E; ++J) {
3558 ObjCProtocolDecl *rhsProto = *J;
3559 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3560 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3561 match = true;
3562 break;
3563 }
3564 }
3565 if (!match)
3566 return false;
3567 }
3568 return true;
3569 }
3570 return false;
3571}
3572
Eli Friedman3d815e72008-08-22 00:56:42 +00003573/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003574/// compatible for assignment from RHS to LHS. This handles validation of any
3575/// protocol qualifiers on the LHS or RHS.
3576///
Steve Naroff14108da2009-07-10 23:34:53 +00003577bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3578 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003579 // If either type represents the built-in 'id' or 'Class' types, return true.
3580 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003581 return true;
3582
Steve Naroff4084c302009-07-23 01:01:38 +00003583 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003584 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3585 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003586 false);
3587
Steve Naroff14108da2009-07-10 23:34:53 +00003588 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3589 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003590 if (LHS && RHS) // We have 2 user-defined types.
3591 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003592
Steve Naroff4084c302009-07-23 01:01:38 +00003593 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003594}
3595
Eli Friedman3d815e72008-08-22 00:56:42 +00003596bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3597 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003598 // Verify that the base decls are compatible: the RHS must be a subclass of
3599 // the LHS.
3600 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3601 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003602
Chris Lattner6ac46a42008-04-07 06:51:04 +00003603 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3604 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003605 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003606 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003607
Chris Lattner6ac46a42008-04-07 06:51:04 +00003608 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3609 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003610 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003611 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003612
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003613 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3614 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003615 LHSPI != LHSPE; LHSPI++) {
3616 bool RHSImplementsProtocol = false;
3617
3618 // If the RHS doesn't implement the protocol on the left, the types
3619 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003620 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003621 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003622 RHSPI != RHSPE; RHSPI++) {
3623 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003624 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003625 break;
3626 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003627 }
3628 // FIXME: For better diagnostics, consider passing back the protocol name.
3629 if (!RHSImplementsProtocol)
3630 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003631 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003632 // The RHS implements all protocols listed on the LHS.
3633 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003634}
3635
Steve Naroff389bf462009-02-12 17:52:19 +00003636bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3637 // get the "pointed to" types
John McCall183700f2009-09-21 23:43:11 +00003638 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3639 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump1eb44332009-09-09 15:08:12 +00003640
Steve Naroff14108da2009-07-10 23:34:53 +00003641 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003642 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003643
3644 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3645 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003646}
3647
Mike Stump1eb44332009-09-09 15:08:12 +00003648/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003649/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003650/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003651/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003652bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3653 return !mergeTypes(LHS, RHS).isNull();
3654}
3655
3656QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall183700f2009-09-21 23:43:11 +00003657 const FunctionType *lbase = lhs->getAs<FunctionType>();
3658 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00003659 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3660 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003661 bool allLTypes = true;
3662 bool allRTypes = true;
3663
3664 // Check return type
3665 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3666 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003667 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3668 allLTypes = false;
3669 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3670 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003671 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003672 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3673 if (NoReturn != lbase->getNoReturnAttr())
3674 allLTypes = false;
3675 if (NoReturn != rbase->getNoReturnAttr())
3676 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003677
Eli Friedman3d815e72008-08-22 00:56:42 +00003678 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003679 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3680 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003681 unsigned lproto_nargs = lproto->getNumArgs();
3682 unsigned rproto_nargs = rproto->getNumArgs();
3683
3684 // Compatible functions must have the same number of arguments
3685 if (lproto_nargs != rproto_nargs)
3686 return QualType();
3687
3688 // Variadic and non-variadic functions aren't compatible
3689 if (lproto->isVariadic() != rproto->isVariadic())
3690 return QualType();
3691
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003692 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3693 return QualType();
3694
Eli Friedman3d815e72008-08-22 00:56:42 +00003695 // Check argument compatibility
3696 llvm::SmallVector<QualType, 10> types;
3697 for (unsigned i = 0; i < lproto_nargs; i++) {
3698 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3699 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3700 QualType argtype = mergeTypes(largtype, rargtype);
3701 if (argtype.isNull()) return QualType();
3702 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003703 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3704 allLTypes = false;
3705 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3706 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003707 }
3708 if (allLTypes) return lhs;
3709 if (allRTypes) return rhs;
3710 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003711 lproto->isVariadic(), lproto->getTypeQuals(),
3712 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003713 }
3714
3715 if (lproto) allRTypes = false;
3716 if (rproto) allLTypes = false;
3717
Douglas Gregor72564e72009-02-26 23:50:07 +00003718 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003719 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003720 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003721 if (proto->isVariadic()) return QualType();
3722 // Check that the types are compatible with the types that
3723 // would result from default argument promotions (C99 6.7.5.3p15).
3724 // The only types actually affected are promotable integer
3725 // types and floats, which would be passed as a different
3726 // type depending on whether the prototype is visible.
3727 unsigned proto_nargs = proto->getNumArgs();
3728 for (unsigned i = 0; i < proto_nargs; ++i) {
3729 QualType argTy = proto->getArgType(i);
3730 if (argTy->isPromotableIntegerType() ||
3731 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3732 return QualType();
3733 }
3734
3735 if (allLTypes) return lhs;
3736 if (allRTypes) return rhs;
3737 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003738 proto->getNumArgs(), proto->isVariadic(),
3739 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003740 }
3741
3742 if (allLTypes) return lhs;
3743 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003744 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003745}
3746
3747QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003748 // C++ [expr]: If an expression initially has the type "reference to T", the
3749 // type is adjusted to "T" prior to any further analysis, the expression
3750 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003751 // expression is an lvalue unless the reference is an rvalue reference and
3752 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003753 // FIXME: C++ shouldn't be going through here! The rules are different
3754 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003755 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3756 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00003757 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003758 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00003759 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003760 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003761
Eli Friedman3d815e72008-08-22 00:56:42 +00003762 QualType LHSCan = getCanonicalType(LHS),
3763 RHSCan = getCanonicalType(RHS);
3764
3765 // If two types are identical, they are compatible.
3766 if (LHSCan == RHSCan)
3767 return LHS;
3768
3769 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003770 // Note that we handle extended qualifiers later, in the
3771 // case for ExtQualType.
3772 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003773 return QualType();
3774
Eli Friedman852d63b2009-06-01 01:22:52 +00003775 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3776 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003777
Chris Lattner1adb8832008-01-14 05:45:46 +00003778 // We want to consider the two function types to be the same for these
3779 // comparisons, just force one to the other.
3780 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3781 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003782
Eli Friedman07d25872009-06-02 05:28:56 +00003783 // Strip off objc_gc attributes off the top level so they can be merged.
3784 // This is a complete mess, but the attribute itself doesn't make much sense.
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003785 if (RHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003786 QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
3787 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003788 QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
Mike Stump1eb44332009-09-09 15:08:12 +00003789 // __weak attribute must appear on both declarations.
3790 // __strong attribue is redundant if other decl is an objective-c
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003791 // object pointer (or decorated with __strong attribute); otherwise
3792 // issue error.
3793 if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
3794 (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
Steve Naroff14108da2009-07-10 23:34:53 +00003795 !LHSCan->isObjCObjectPointerType()))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003796 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003797
Eli Friedman07d25872009-06-02 05:28:56 +00003798 RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
3799 RHS.getCVRQualifiers());
3800 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003801 if (!Result.isNull()) {
3802 if (Result.getObjCGCAttr() == QualType::GCNone)
3803 Result = getObjCGCQualType(Result, GCAttr);
3804 else if (Result.getObjCGCAttr() != GCAttr)
3805 Result = QualType();
3806 }
Eli Friedman07d25872009-06-02 05:28:56 +00003807 return Result;
3808 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003809 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003810 if (LHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003811 QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
3812 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003813 QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
3814 // __weak attribute must appear on both declarations. __strong
Mike Stump1eb44332009-09-09 15:08:12 +00003815 // __strong attribue is redundant if other decl is an objective-c
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003816 // object pointer (or decorated with __strong attribute); otherwise
3817 // issue error.
3818 if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
3819 (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
Steve Naroff14108da2009-07-10 23:34:53 +00003820 !RHSCan->isObjCObjectPointerType()))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003821 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003822
Eli Friedman07d25872009-06-02 05:28:56 +00003823 LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
3824 LHS.getCVRQualifiers());
3825 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003826 if (!Result.isNull()) {
3827 if (Result.getObjCGCAttr() == QualType::GCNone)
3828 Result = getObjCGCQualType(Result, GCAttr);
3829 else if (Result.getObjCGCAttr() != GCAttr)
3830 Result = QualType();
3831 }
Eli Friedman354e53d2009-06-02 07:45:37 +00003832 return Result;
Eli Friedman07d25872009-06-02 05:28:56 +00003833 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003834 }
3835
Eli Friedman4c721d32008-02-12 08:23:06 +00003836 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003837 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3838 LHSClass = Type::ConstantArray;
3839 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3840 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00003841
Nate Begeman213541a2008-04-18 23:10:10 +00003842 // Canonicalize ExtVector -> Vector.
3843 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3844 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00003845
Chris Lattnera36a61f2008-04-07 05:43:21 +00003846 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003847 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003848 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00003849 // a signed integer type, or an unsigned integer type.
John McCall183700f2009-09-21 23:43:11 +00003850 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003851 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3852 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003853 }
John McCall183700f2009-09-21 23:43:11 +00003854 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman3d815e72008-08-22 00:56:42 +00003855 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3856 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003857 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003858
Eli Friedman3d815e72008-08-22 00:56:42 +00003859 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003860 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003861
Steve Naroff4a746782008-01-09 22:43:08 +00003862 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003863 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003864#define TYPE(Class, Base)
3865#define ABSTRACT_TYPE(Class, Base)
3866#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3867#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3868#include "clang/AST/TypeNodes.def"
3869 assert(false && "Non-canonical and dependent types shouldn't get here");
3870 return QualType();
3871
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003872 case Type::LValueReference:
3873 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003874 case Type::MemberPointer:
3875 assert(false && "C++ should never be in mergeTypes");
3876 return QualType();
3877
3878 case Type::IncompleteArray:
3879 case Type::VariableArray:
3880 case Type::FunctionProto:
3881 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00003882 assert(false && "Types are eliminated above");
3883 return QualType();
3884
Chris Lattner1adb8832008-01-14 05:45:46 +00003885 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003886 {
3887 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003888 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
3889 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003890 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3891 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003892 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003893 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003894 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003895 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003896 return getPointerType(ResultType);
3897 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003898 case Type::BlockPointer:
3899 {
3900 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003901 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
3902 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00003903 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3904 if (ResultType.isNull()) return QualType();
3905 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3906 return LHS;
3907 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3908 return RHS;
3909 return getBlockPointerType(ResultType);
3910 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003911 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003912 {
3913 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3914 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3915 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3916 return QualType();
3917
3918 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3919 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3920 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3921 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003922 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3923 return LHS;
3924 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3925 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003926 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3927 ArrayType::ArraySizeModifier(), 0);
3928 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3929 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003930 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3931 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003932 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3933 return LHS;
3934 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3935 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003936 if (LVAT) {
3937 // FIXME: This isn't correct! But tricky to implement because
3938 // the array's size has to be the size of LHS, but the type
3939 // has to be different.
3940 return LHS;
3941 }
3942 if (RVAT) {
3943 // FIXME: This isn't correct! But tricky to implement because
3944 // the array's size has to be the size of RHS, but the type
3945 // has to be different.
3946 return RHS;
3947 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003948 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3949 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003950 return getIncompleteArrayType(ResultType,
3951 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003952 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003953 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003954 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003955 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003956 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003957 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003958 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003959 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003960 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003961 case Type::Complex:
3962 // Distinct complex types are incompatible.
3963 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003964 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003965 // FIXME: The merged type should be an ExtVector!
John McCall183700f2009-09-21 23:43:11 +00003966 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman3d815e72008-08-22 00:56:42 +00003967 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003968 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003969 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003970 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003971 // FIXME: This should be type compatibility, e.g. whether
3972 // "LHS x; RHS x;" at global scope is legal.
John McCall183700f2009-09-21 23:43:11 +00003973 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
3974 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff5fd659d2009-02-21 16:18:07 +00003975 if (LHSIface && RHSIface &&
3976 canAssignObjCInterfaces(LHSIface, RHSIface))
3977 return LHS;
3978
Eli Friedman3d815e72008-08-22 00:56:42 +00003979 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003980 }
Steve Naroff14108da2009-07-10 23:34:53 +00003981 case Type::ObjCObjectPointer: {
John McCall183700f2009-09-21 23:43:11 +00003982 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
3983 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff14108da2009-07-10 23:34:53 +00003984 return LHS;
3985
Steve Naroffbc76dd02008-12-10 22:14:21 +00003986 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00003987 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003988 case Type::FixedWidthInt:
3989 // Distinct fixed-width integers are not compatible.
3990 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003991 case Type::ExtQual:
3992 // FIXME: ExtQual types can be compatible even if they're not
3993 // identical!
3994 return QualType();
3995 // First attempt at an implementation, but I'm not really sure it's
3996 // right...
3997#if 0
3998 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3999 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
4000 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
4001 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
4002 return QualType();
4003 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
4004 LHSBase = QualType(LQual->getBaseType(), 0);
4005 RHSBase = QualType(RQual->getBaseType(), 0);
4006 ResultType = mergeTypes(LHSBase, RHSBase);
4007 if (ResultType.isNull()) return QualType();
4008 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
4009 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
4010 return LHS;
4011 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
4012 return RHS;
4013 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
4014 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
4015 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
4016 return ResultType;
4017#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00004018
4019 case Type::TemplateSpecialization:
4020 assert(false && "Dependent types have no size");
4021 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00004022 }
Douglas Gregor72564e72009-02-26 23:50:07 +00004023
4024 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004025}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00004026
Chris Lattner5426bf62008-04-07 07:01:58 +00004027//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00004028// Integer Predicates
4029//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00004030
Eli Friedmanad74a752008-06-28 06:23:08 +00004031unsigned ASTContext::getIntWidth(QualType T) {
4032 if (T == BoolTy)
4033 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00004034 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
4035 return FWIT->getWidth();
4036 }
4037 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00004038 return (unsigned)getTypeSize(T);
4039}
4040
4041QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4042 assert(T->isSignedIntegerType() && "Unexpected type");
John McCall183700f2009-09-21 23:43:11 +00004043 if (const EnumType* ETy = T->getAs<EnumType>())
Eli Friedmanad74a752008-06-28 06:23:08 +00004044 T = ETy->getDecl()->getIntegerType();
John McCall183700f2009-09-21 23:43:11 +00004045 const BuiltinType* BTy = T->getAs<BuiltinType>();
Eli Friedmanad74a752008-06-28 06:23:08 +00004046 assert (BTy && "Unexpected signed integer type");
4047 switch (BTy->getKind()) {
4048 case BuiltinType::Char_S:
4049 case BuiltinType::SChar:
4050 return UnsignedCharTy;
4051 case BuiltinType::Short:
4052 return UnsignedShortTy;
4053 case BuiltinType::Int:
4054 return UnsignedIntTy;
4055 case BuiltinType::Long:
4056 return UnsignedLongTy;
4057 case BuiltinType::LongLong:
4058 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004059 case BuiltinType::Int128:
4060 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004061 default:
4062 assert(0 && "Unexpected signed integer type");
4063 return QualType();
4064 }
4065}
4066
Douglas Gregor2cf26342009-04-09 22:27:44 +00004067ExternalASTSource::~ExternalASTSource() { }
4068
4069void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004070
4071
4072//===----------------------------------------------------------------------===//
4073// Builtin Type Computation
4074//===----------------------------------------------------------------------===//
4075
4076/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4077/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004078static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004079 ASTContext::GetBuiltinTypeError &Error,
4080 bool AllowTypeModifiers = true) {
4081 // Modifiers.
4082 int HowLong = 0;
4083 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004084
Chris Lattner86df27b2009-06-14 00:45:47 +00004085 // Read the modifiers first.
4086 bool Done = false;
4087 while (!Done) {
4088 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004089 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004090 case 'S':
4091 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4092 assert(!Signed && "Can't use 'S' modifier multiple times!");
4093 Signed = true;
4094 break;
4095 case 'U':
4096 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4097 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4098 Unsigned = true;
4099 break;
4100 case 'L':
4101 assert(HowLong <= 2 && "Can't have LLLL modifier");
4102 ++HowLong;
4103 break;
4104 }
4105 }
4106
4107 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004108
Chris Lattner86df27b2009-06-14 00:45:47 +00004109 // Read the base type.
4110 switch (*Str++) {
4111 default: assert(0 && "Unknown builtin type letter!");
4112 case 'v':
4113 assert(HowLong == 0 && !Signed && !Unsigned &&
4114 "Bad modifiers used with 'v'!");
4115 Type = Context.VoidTy;
4116 break;
4117 case 'f':
4118 assert(HowLong == 0 && !Signed && !Unsigned &&
4119 "Bad modifiers used with 'f'!");
4120 Type = Context.FloatTy;
4121 break;
4122 case 'd':
4123 assert(HowLong < 2 && !Signed && !Unsigned &&
4124 "Bad modifiers used with 'd'!");
4125 if (HowLong)
4126 Type = Context.LongDoubleTy;
4127 else
4128 Type = Context.DoubleTy;
4129 break;
4130 case 's':
4131 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4132 if (Unsigned)
4133 Type = Context.UnsignedShortTy;
4134 else
4135 Type = Context.ShortTy;
4136 break;
4137 case 'i':
4138 if (HowLong == 3)
4139 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4140 else if (HowLong == 2)
4141 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4142 else if (HowLong == 1)
4143 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4144 else
4145 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4146 break;
4147 case 'c':
4148 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4149 if (Signed)
4150 Type = Context.SignedCharTy;
4151 else if (Unsigned)
4152 Type = Context.UnsignedCharTy;
4153 else
4154 Type = Context.CharTy;
4155 break;
4156 case 'b': // boolean
4157 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4158 Type = Context.BoolTy;
4159 break;
4160 case 'z': // size_t.
4161 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4162 Type = Context.getSizeType();
4163 break;
4164 case 'F':
4165 Type = Context.getCFConstantStringType();
4166 break;
4167 case 'a':
4168 Type = Context.getBuiltinVaListType();
4169 assert(!Type.isNull() && "builtin va list type not initialized!");
4170 break;
4171 case 'A':
4172 // This is a "reference" to a va_list; however, what exactly
4173 // this means depends on how va_list is defined. There are two
4174 // different kinds of va_list: ones passed by value, and ones
4175 // passed by reference. An example of a by-value va_list is
4176 // x86, where va_list is a char*. An example of by-ref va_list
4177 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4178 // we want this argument to be a char*&; for x86-64, we want
4179 // it to be a __va_list_tag*.
4180 Type = Context.getBuiltinVaListType();
4181 assert(!Type.isNull() && "builtin va list type not initialized!");
4182 if (Type->isArrayType()) {
4183 Type = Context.getArrayDecayedType(Type);
4184 } else {
4185 Type = Context.getLValueReferenceType(Type);
4186 }
4187 break;
4188 case 'V': {
4189 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004190 unsigned NumElements = strtoul(Str, &End, 10);
4191 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004192
Chris Lattner86df27b2009-06-14 00:45:47 +00004193 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004194
Chris Lattner86df27b2009-06-14 00:45:47 +00004195 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4196 Type = Context.getVectorType(ElementType, NumElements);
4197 break;
4198 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004199 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004200 Type = Context.getFILEType();
4201 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004202 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004203 return QualType();
4204 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004205 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004206 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004207 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004208 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004209 else
4210 Type = Context.getjmp_bufType();
4211
Mike Stumpfd612db2009-07-28 23:47:15 +00004212 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004213 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004214 return QualType();
4215 }
4216 break;
Mike Stump782fa302009-07-28 02:25:19 +00004217 }
Mike Stump1eb44332009-09-09 15:08:12 +00004218
Chris Lattner86df27b2009-06-14 00:45:47 +00004219 if (!AllowTypeModifiers)
4220 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004221
Chris Lattner86df27b2009-06-14 00:45:47 +00004222 Done = false;
4223 while (!Done) {
4224 switch (*Str++) {
4225 default: Done = true; --Str; break;
4226 case '*':
4227 Type = Context.getPointerType(Type);
4228 break;
4229 case '&':
4230 Type = Context.getLValueReferenceType(Type);
4231 break;
4232 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4233 case 'C':
4234 Type = Type.getQualifiedType(QualType::Const);
4235 break;
4236 }
4237 }
Mike Stump1eb44332009-09-09 15:08:12 +00004238
Chris Lattner86df27b2009-06-14 00:45:47 +00004239 return Type;
4240}
4241
4242/// GetBuiltinType - Return the type for the specified builtin.
4243QualType ASTContext::GetBuiltinType(unsigned id,
4244 GetBuiltinTypeError &Error) {
4245 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004246
Chris Lattner86df27b2009-06-14 00:45:47 +00004247 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004248
Chris Lattner86df27b2009-06-14 00:45:47 +00004249 Error = GE_None;
4250 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4251 if (Error != GE_None)
4252 return QualType();
4253 while (TypeStr[0] && TypeStr[0] != '.') {
4254 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4255 if (Error != GE_None)
4256 return QualType();
4257
4258 // Do array -> pointer decay. The builtin should use the decayed type.
4259 if (Ty->isArrayType())
4260 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004261
Chris Lattner86df27b2009-06-14 00:45:47 +00004262 ArgTypes.push_back(Ty);
4263 }
4264
4265 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4266 "'.' should only occur at end of builtin type list!");
4267
4268 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4269 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4270 return getFunctionNoProtoType(ResType);
4271 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4272 TypeStr[0] == '.', 0);
4273}
Eli Friedmana95d7572009-08-19 07:44:53 +00004274
4275QualType
4276ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4277 // Perform the usual unary conversions. We do this early so that
4278 // integral promotions to "int" can allow us to exit early, in the
4279 // lhs == rhs check. Also, for conversion purposes, we ignore any
4280 // qualifiers. For example, "const float" and "float" are
4281 // equivalent.
4282 if (lhs->isPromotableIntegerType())
4283 lhs = getPromotedIntegerType(lhs);
4284 else
4285 lhs = lhs.getUnqualifiedType();
4286 if (rhs->isPromotableIntegerType())
4287 rhs = getPromotedIntegerType(rhs);
4288 else
4289 rhs = rhs.getUnqualifiedType();
4290
4291 // If both types are identical, no conversion is needed.
4292 if (lhs == rhs)
4293 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004294
Eli Friedmana95d7572009-08-19 07:44:53 +00004295 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4296 // The caller can deal with this (e.g. pointer + int).
4297 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4298 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004299
4300 // At this point, we have two different arithmetic types.
4301
Eli Friedmana95d7572009-08-19 07:44:53 +00004302 // Handle complex types first (C99 6.3.1.8p1).
4303 if (lhs->isComplexType() || rhs->isComplexType()) {
4304 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004305 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004306 // convert the rhs to the lhs complex type.
4307 return lhs;
4308 }
Mike Stump1eb44332009-09-09 15:08:12 +00004309 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004310 // convert the lhs to the rhs complex type.
4311 return rhs;
4312 }
4313 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004314 // When both operands are complex, the shorter operand is converted to the
4315 // type of the longer, and that is the type of the result. This corresponds
4316 // to what is done when combining two real floating-point operands.
4317 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004318 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004319 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004320 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004321 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004322 // "double _Complex" is promoted to "long double _Complex".
4323 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004324
4325 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004326 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004327 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004328 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004329 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004330 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4331 // domains match. This is a requirement for our implementation, C99
4332 // does not require this promotion.
4333 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4334 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4335 return rhs;
4336 } else { // handle "_Complex double, double".
4337 return lhs;
4338 }
4339 }
4340 return lhs; // The domain/size match exactly.
4341 }
4342 // Now handle "real" floating types (i.e. float, double, long double).
4343 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4344 // if we have an integer operand, the result is the real floating type.
4345 if (rhs->isIntegerType()) {
4346 // convert rhs to the lhs floating point type.
4347 return lhs;
4348 }
4349 if (rhs->isComplexIntegerType()) {
4350 // convert rhs to the complex floating point type.
4351 return getComplexType(lhs);
4352 }
4353 if (lhs->isIntegerType()) {
4354 // convert lhs to the rhs floating point type.
4355 return rhs;
4356 }
Mike Stump1eb44332009-09-09 15:08:12 +00004357 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004358 // convert lhs to the complex floating point type.
4359 return getComplexType(rhs);
4360 }
4361 // We have two real floating types, float/complex combos were handled above.
4362 // Convert the smaller operand to the bigger result.
4363 int result = getFloatingTypeOrder(lhs, rhs);
4364 if (result > 0) // convert the rhs
4365 return lhs;
4366 assert(result < 0 && "illegal float comparison");
4367 return rhs; // convert the lhs
4368 }
4369 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4370 // Handle GCC complex int extension.
4371 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4372 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4373
4374 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004375 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004376 rhsComplexInt->getElementType()) >= 0)
4377 return lhs; // convert the rhs
4378 return rhs;
4379 } else if (lhsComplexInt && rhs->isIntegerType()) {
4380 // convert the rhs to the lhs complex type.
4381 return lhs;
4382 } else if (rhsComplexInt && lhs->isIntegerType()) {
4383 // convert the lhs to the rhs complex type.
4384 return rhs;
4385 }
4386 }
4387 // Finally, we have two differing integer types.
4388 // The rules for this case are in C99 6.3.1.8
4389 int compare = getIntegerTypeOrder(lhs, rhs);
4390 bool lhsSigned = lhs->isSignedIntegerType(),
4391 rhsSigned = rhs->isSignedIntegerType();
4392 QualType destType;
4393 if (lhsSigned == rhsSigned) {
4394 // Same signedness; use the higher-ranked type
4395 destType = compare >= 0 ? lhs : rhs;
4396 } else if (compare != (lhsSigned ? 1 : -1)) {
4397 // The unsigned type has greater than or equal rank to the
4398 // signed type, so use the unsigned type
4399 destType = lhsSigned ? rhs : lhs;
4400 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4401 // The two types are different widths; if we are here, that
4402 // means the signed type is larger than the unsigned type, so
4403 // use the signed type.
4404 destType = lhsSigned ? lhs : rhs;
4405 } else {
4406 // The signed type is higher-ranked than the unsigned type,
4407 // but isn't actually any bigger (like unsigned int and long
4408 // on most 32-bit systems). Use the unsigned type corresponding
4409 // to the signed type.
4410 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4411 }
4412 return destType;
4413}