blob: bf1fac9eec11a77a73f94b63fec44cb33d5278a5 [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 {
490 const BuiltinType *BT = T->getAsBuiltinType();
491 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.
785 if (const ComplexType* CT = T->getAsComplexType())
786 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
Mike Stumpe24aea22009-07-27 22:25:19 +00001123 if (const FunctionNoProtoType *F = T->getAsFunctionNoProtoType()) {
Mike Stump24556362009-07-25 21:26:53 +00001124 return getFunctionNoProtoType(F->getResultType(), true);
1125 }
Mike Stumpe24aea22009-07-27 22:25:19 +00001126 const FunctionProtoType *F = T->getAsFunctionProtoType();
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) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 // Unique functions, to guarantee there is only one function of a particular
1647 // structure.
1648 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001649 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001650 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001651 NumExs, ExArray, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001652
1653 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001654 if (FunctionProtoType *FTP =
Douglas Gregor72564e72009-02-26 23:50:07 +00001655 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001657
1658 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001660 if (hasExceptionSpec)
1661 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1663 if (!ArgArray[i]->isCanonical())
1664 isCanonical = false;
1665
1666 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001667 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 QualType Canonical;
1669 if (!isCanonical) {
1670 llvm::SmallVector<QualType, 16> CanonicalArgs;
1671 CanonicalArgs.reserve(NumArgs);
1672 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001673 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001674
Chris Lattnerf52ab252008-04-06 22:59:24 +00001675 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001676 CanonicalArgs.data(), NumArgs,
Douglas Gregor47259d92009-08-05 19:03:35 +00001677 isVariadic, TypeQuals, false,
1678 false, 0, 0, NoReturn);
Sebastian Redl465226e2009-05-27 22:11:52 +00001679
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001681 FunctionProtoType *NewIP =
1682 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001683 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001685
Douglas Gregor72564e72009-02-26 23:50:07 +00001686 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001687 // for two variable size arrays (for parameter and exception types) at the
1688 // end of them.
Mike Stump1eb44332009-09-09 15:08:12 +00001689 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001690 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1691 NumArgs*sizeof(QualType) +
1692 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001693 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001694 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump24556362009-07-25 21:26:53 +00001695 ExArray, NumExs, Canonical, NoReturn);
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001697 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 return QualType(FTP, 0);
1699}
1700
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001701/// getTypeDeclType - Return the unique reference to the type for the
1702/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001703QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001704 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001705 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001707 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001708 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001709 else if (isa<TemplateTypeParmDecl>(Decl)) {
1710 assert(false && "Template type parameter types are always available.");
Mike Stump9fdbab32009-07-31 02:02:20 +00001711 } else if (ObjCInterfaceDecl *ObjCInterface
1712 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001713 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001714
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001715 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001716 if (PrevDecl)
1717 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001718 else
1719 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Mike Stump9fdbab32009-07-31 02:02:20 +00001720 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001721 if (PrevDecl)
1722 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001723 else
1724 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Mike Stump9fdbab32009-07-31 02:02:20 +00001725 } else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001726 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001727
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001728 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001729 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001730}
1731
Reid Spencer5f016e22007-07-11 17:01:13 +00001732/// getTypedefType - Return the unique reference to the type for the
1733/// specified typename decl.
1734QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1735 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Chris Lattnerf52ab252008-04-06 22:59:24 +00001737 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001738 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 Types.push_back(Decl->TypeForDecl);
1740 return QualType(Decl->TypeForDecl, 0);
1741}
1742
Douglas Gregorfab9d672009-02-05 23:33:38 +00001743/// \brief Retrieve the template type parameter type for a template
Mike Stump1eb44332009-09-09 15:08:12 +00001744/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001745/// name.
Mike Stump1eb44332009-09-09 15:08:12 +00001746QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001747 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001748 IdentifierInfo *Name) {
1749 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001750 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001751 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001752 TemplateTypeParmType *TypeParm
Douglas Gregorfab9d672009-02-05 23:33:38 +00001753 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1754
1755 if (TypeParm)
1756 return QualType(TypeParm, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001758 if (Name) {
1759 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
1760 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
1761 Name, Canon);
1762 } else
1763 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001764
1765 Types.push_back(TypeParm);
1766 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1767
1768 return QualType(TypeParm, 0);
1769}
1770
Mike Stump1eb44332009-09-09 15:08:12 +00001771QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001772ASTContext::getTemplateSpecializationType(TemplateName Template,
1773 const TemplateArgument *Args,
1774 unsigned NumArgs,
1775 QualType Canon) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00001776 if (!Canon.isNull())
1777 Canon = getCanonicalType(Canon);
1778 else {
1779 // Build the canonical template specialization type.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001780 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1781 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1782 CanonArgs.reserve(NumArgs);
1783 for (unsigned I = 0; I != NumArgs; ++I)
1784 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1785
1786 // Determine whether this canonical template specialization type already
1787 // exists.
1788 llvm::FoldingSetNodeID ID;
Mike Stump1eb44332009-09-09 15:08:12 +00001789 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor828e2262009-07-29 16:09:57 +00001790 CanonArgs.data(), NumArgs, *this);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001791
1792 void *InsertPos = 0;
1793 TemplateSpecializationType *Spec
1794 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Douglas Gregor1275ae02009-07-28 23:00:59 +00001796 if (!Spec) {
1797 // Allocate a new canonical template specialization type.
Mike Stump1eb44332009-09-09 15:08:12 +00001798 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor1275ae02009-07-28 23:00:59 +00001799 sizeof(TemplateArgument) * NumArgs),
1800 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001801 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregor1275ae02009-07-28 23:00:59 +00001802 CanonArgs.data(), NumArgs,
Douglas Gregorb88e8882009-07-30 17:40:51 +00001803 Canon);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001804 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001805 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor1275ae02009-07-28 23:00:59 +00001806 }
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Douglas Gregorb88e8882009-07-30 17:40:51 +00001808 if (Canon.isNull())
1809 Canon = QualType(Spec, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001810 assert(Canon->isDependentType() &&
Douglas Gregor1275ae02009-07-28 23:00:59 +00001811 "Non-dependent template-id type must have a canonical type");
Douglas Gregorb88e8882009-07-30 17:40:51 +00001812 }
Douglas Gregorfc705b82009-02-26 22:19:44 +00001813
Douglas Gregor1275ae02009-07-28 23:00:59 +00001814 // Allocate the (non-canonical) template specialization type, but don't
1815 // try to unique it: these types typically have location information that
1816 // we don't unique and don't want to lose.
Mike Stump1eb44332009-09-09 15:08:12 +00001817 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001818 sizeof(TemplateArgument) * NumArgs),
1819 8);
Mike Stump1eb44332009-09-09 15:08:12 +00001820 TemplateSpecializationType *Spec
1821 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor828e2262009-07-29 16:09:57 +00001822 Canon);
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Douglas Gregor55f6b142009-02-09 18:46:07 +00001824 Types.push_back(Spec);
Mike Stump1eb44332009-09-09 15:08:12 +00001825 return QualType(Spec, 0);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001826}
1827
Mike Stump1eb44332009-09-09 15:08:12 +00001828QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001829ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001830 QualType NamedType) {
1831 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001832 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001833
1834 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001835 QualifiedNameType *T
Douglas Gregore4e5b052009-03-19 00:18:19 +00001836 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1837 if (T)
1838 return QualType(T, 0);
1839
Mike Stump1eb44332009-09-09 15:08:12 +00001840 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001841 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001842 Types.push_back(T);
1843 QualifiedNameTypes.InsertNode(T, InsertPos);
1844 return QualType(T, 0);
1845}
1846
Mike Stump1eb44332009-09-09 15:08:12 +00001847QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregord57959a2009-03-27 23:10:48 +00001848 const IdentifierInfo *Name,
1849 QualType Canon) {
1850 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1851
1852 if (Canon.isNull()) {
1853 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1854 if (CanonNNS != NNS)
1855 Canon = getTypenameType(CanonNNS, Name);
1856 }
1857
1858 llvm::FoldingSetNodeID ID;
1859 TypenameType::Profile(ID, NNS, Name);
1860
1861 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001862 TypenameType *T
Douglas Gregord57959a2009-03-27 23:10:48 +00001863 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1864 if (T)
1865 return QualType(T, 0);
1866
1867 T = new (*this) TypenameType(NNS, Name, Canon);
1868 Types.push_back(T);
1869 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001870 return QualType(T, 0);
Douglas Gregord57959a2009-03-27 23:10:48 +00001871}
1872
Mike Stump1eb44332009-09-09 15:08:12 +00001873QualType
1874ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor17343172009-04-01 00:28:59 +00001875 const TemplateSpecializationType *TemplateId,
1876 QualType Canon) {
1877 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1878
1879 if (Canon.isNull()) {
1880 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1881 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1882 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1883 const TemplateSpecializationType *CanonTemplateId
1884 = CanonType->getAsTemplateSpecializationType();
1885 assert(CanonTemplateId &&
1886 "Canonical type must also be a template specialization type");
1887 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1888 }
1889 }
1890
1891 llvm::FoldingSetNodeID ID;
1892 TypenameType::Profile(ID, NNS, TemplateId);
1893
1894 void *InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001895 TypenameType *T
Douglas Gregor17343172009-04-01 00:28:59 +00001896 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1897 if (T)
1898 return QualType(T, 0);
1899
1900 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1901 Types.push_back(T);
1902 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +00001903 return QualType(T, 0);
Douglas Gregor17343172009-04-01 00:28:59 +00001904}
1905
John McCall7da24312009-09-05 00:15:47 +00001906QualType
1907ASTContext::getElaboratedType(QualType UnderlyingType,
1908 ElaboratedType::TagKind Tag) {
1909 llvm::FoldingSetNodeID ID;
1910 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump1eb44332009-09-09 15:08:12 +00001911
John McCall7da24312009-09-05 00:15:47 +00001912 void *InsertPos = 0;
1913 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1914 if (T)
1915 return QualType(T, 0);
1916
1917 QualType Canon = getCanonicalType(UnderlyingType);
1918
1919 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1920 Types.push_back(T);
1921 ElaboratedTypes.InsertNode(T, InsertPos);
1922 return QualType(T, 0);
1923}
1924
Chris Lattner88cb27a2008-04-07 04:56:42 +00001925/// CmpProtocolNames - Comparison predicate for sorting protocols
1926/// alphabetically.
1927static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1928 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001929 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001930}
1931
1932static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1933 unsigned &NumProtocols) {
1934 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Chris Lattner88cb27a2008-04-07 04:56:42 +00001936 // Sort protocols, keyed by name.
1937 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1938
1939 // Remove duplicates.
1940 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1941 NumProtocols = ProtocolsEnd-Protocols;
1942}
1943
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001944/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1945/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001946QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump1eb44332009-09-09 15:08:12 +00001947 ObjCProtocolDecl **Protocols,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001948 unsigned NumProtocols) {
1949 // Sort the protocol list alphabetically to canonicalize it.
1950 if (NumProtocols)
1951 SortAndUniqueProtocols(Protocols, NumProtocols);
1952
1953 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001954 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001955
1956 void *InsertPos = 0;
1957 if (ObjCObjectPointerType *QT =
1958 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1959 return QualType(QT, 0);
1960
1961 // No Match;
1962 ObjCObjectPointerType *QType =
Steve Naroff14108da2009-07-10 23:34:53 +00001963 new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001965 Types.push_back(QType);
1966 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1967 return QualType(QType, 0);
1968}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001969
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001970/// getObjCInterfaceType - Return the unique reference to the type for the
1971/// specified ObjC interface decl. The list of protocols is optional.
1972QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001973 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump1eb44332009-09-09 15:08:12 +00001974 if (NumProtocols)
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001975 // Sort the protocol list alphabetically to canonicalize it.
1976 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001978 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001979 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001981 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001982 if (ObjCInterfaceType *QT =
1983 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001984 return QualType(QT, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001986 // No Match;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001987 ObjCInterfaceType *QType =
Mike Stump1eb44332009-09-09 15:08:12 +00001988 new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001989 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001990 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001991 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001992 return QualType(QType, 0);
1993}
1994
Douglas Gregor72564e72009-02-26 23:50:07 +00001995/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1996/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001997/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump1eb44332009-09-09 15:08:12 +00001998/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00001999/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00002000QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002001 TypeOfExprType *toe;
Douglas Gregorb1975722009-07-30 23:18:24 +00002002 if (tofExpr->isTypeDependent()) {
2003 llvm::FoldingSetNodeID ID;
2004 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump1eb44332009-09-09 15:08:12 +00002005
Douglas Gregorb1975722009-07-30 23:18:24 +00002006 void *InsertPos = 0;
2007 DependentTypeOfExprType *Canon
2008 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2009 if (Canon) {
2010 // We already have a "canonical" version of an identical, dependent
2011 // typeof(expr) type. Use that as our canonical type.
Mike Stump1eb44332009-09-09 15:08:12 +00002012 toe = new (*this, 8) TypeOfExprType(tofExpr,
Douglas Gregorb1975722009-07-30 23:18:24 +00002013 QualType((TypeOfExprType*)Canon, 0));
2014 }
2015 else {
2016 // Build a new, canonical typeof(expr) type.
2017 Canon = new (*this, 8) DependentTypeOfExprType(*this, tofExpr);
2018 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2019 toe = Canon;
2020 }
2021 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002022 QualType Canonical = getCanonicalType(tofExpr->getType());
2023 toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
2024 }
Steve Naroff9752f252007-08-01 18:02:17 +00002025 Types.push_back(toe);
2026 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002027}
2028
Steve Naroff9752f252007-08-01 18:02:17 +00002029/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2030/// TypeOfType AST's. The only motivation to unique these nodes would be
2031/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002032/// an issue. This doesn't effect the type checker, since it operates
Steve Naroff9752f252007-08-01 18:02:17 +00002033/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00002034QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002035 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00002036 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00002037 Types.push_back(tot);
2038 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002039}
2040
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002041/// getDecltypeForExpr - Given an expr, will return the decltype for that
2042/// expression, according to the rules in C++0x [dcl.type.simple]p4
2043static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00002044 if (e->isTypeDependent())
2045 return Context.DependentTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002047 // If e is an id expression or a class member access, decltype(e) is defined
2048 // as the type of the entity named by e.
2049 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2050 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2051 return VD->getType();
2052 }
2053 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2054 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2055 return FD->getType();
2056 }
2057 // If e is a function call or an invocation of an overloaded operator,
2058 // (parentheses around e are ignored), decltype(e) is defined as the
2059 // return type of that function.
2060 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2061 return CE->getCallReturnType();
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002063 QualType T = e->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002064
2065 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002066 // defined as T&, otherwise decltype(e) is defined as T.
2067 if (e->isLvalue(Context) == Expr::LV_Valid)
2068 T = Context.getLValueReferenceType(T);
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00002070 return T;
2071}
2072
Anders Carlsson395b4752009-06-24 19:06:50 +00002073/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2074/// DecltypeType AST's. The only motivation to unique these nodes would be
2075/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump1eb44332009-09-09 15:08:12 +00002076/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson395b4752009-06-24 19:06:50 +00002077/// on canonical type's (which are always unique).
2078QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002079 DecltypeType *dt;
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002080 if (e->isTypeDependent()) {
2081 llvm::FoldingSetNodeID ID;
2082 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump1eb44332009-09-09 15:08:12 +00002083
Douglas Gregor9d702ae2009-07-30 23:36:40 +00002084 void *InsertPos = 0;
2085 DependentDecltypeType *Canon
2086 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2087 if (Canon) {
2088 // We already have a "canonical" version of an equivalent, dependent
2089 // decltype type. Use that as our canonical type.
2090 dt = new (*this, 8) DecltypeType(e, DependentTy,
2091 QualType((DecltypeType*)Canon, 0));
2092 }
2093 else {
2094 // Build a new, canonical typeof(expr) type.
2095 Canon = new (*this, 8) DependentDecltypeType(*this, e);
2096 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2097 dt = Canon;
2098 }
2099 } else {
Douglas Gregordd0257c2009-07-08 00:03:05 +00002100 QualType T = getDecltypeForExpr(e, *this);
Mike Stump1eb44332009-09-09 15:08:12 +00002101 dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00002102 }
Anders Carlsson395b4752009-06-24 19:06:50 +00002103 Types.push_back(dt);
2104 return QualType(dt, 0);
2105}
2106
Reid Spencer5f016e22007-07-11 17:01:13 +00002107/// getTagDeclType - Return the unique reference to the type for the
2108/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpe607ed02009-08-07 18:05:12 +00002109QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00002110 assert (Decl);
Mike Stumpe607ed02009-08-07 18:05:12 +00002111 // FIXME: What is the design on getTagDeclType when it requires casting
2112 // away const? mutable?
2113 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Reid Spencer5f016e22007-07-11 17:01:13 +00002114}
2115
Mike Stump1eb44332009-09-09 15:08:12 +00002116/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2117/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2118/// needs to agree with the definition in <stddef.h>.
Reid Spencer5f016e22007-07-11 17:01:13 +00002119QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002120 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002121}
2122
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002123/// getSignedWCharType - Return the type of "signed wchar_t".
2124/// Used when in C++, as a GCC extension.
2125QualType ASTContext::getSignedWCharType() const {
2126 // FIXME: derive from "Target" ?
2127 return WCharTy;
2128}
2129
2130/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2131/// Used when in C++, as a GCC extension.
2132QualType ASTContext::getUnsignedWCharType() const {
2133 // FIXME: derive from "Target" ?
2134 return UnsignedIntTy;
2135}
2136
Chris Lattner8b9023b2007-07-13 03:05:23 +00002137/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2138/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2139QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002140 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002141}
2142
Chris Lattnere6327742008-04-02 05:18:44 +00002143//===----------------------------------------------------------------------===//
2144// Type Operators
2145//===----------------------------------------------------------------------===//
2146
Chris Lattner77c96472008-04-06 22:41:35 +00002147/// getCanonicalType - Return the canonical (structural) type corresponding to
2148/// the specified potentially non-canonical type. The non-canonical version
2149/// of a type may have many "decorated" versions of types. Decorators can
2150/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2151/// to be free of any of these, allowing two canonical types to be compared
2152/// for exact equality with a simple pointer comparison.
Douglas Gregor50d62d12009-08-05 05:36:45 +00002153CanQualType ASTContext::getCanonicalType(QualType T) {
Chris Lattner77c96472008-04-06 22:41:35 +00002154 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002156 // If the result has type qualifiers, make sure to canonicalize them as well.
2157 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
Mike Stump1eb44332009-09-09 15:08:12 +00002158 if (TypeQuals == 0)
Douglas Gregor50d62d12009-08-05 05:36:45 +00002159 return CanQualType::CreateUnsafe(CanType);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002160
2161 // If the type qualifiers are on an array type, get the canonical type of the
2162 // array with the qualifiers applied to the element type.
2163 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2164 if (!AT)
Douglas Gregor50d62d12009-08-05 05:36:45 +00002165 return CanQualType::CreateUnsafe(CanType.getQualifiedType(TypeQuals));
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002167 // Get the canonical version of the element with the extra qualifiers on it.
2168 // This can recursively sink qualifiers through multiple levels of arrays.
2169 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
2170 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002171
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002172 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002173 return CanQualType::CreateUnsafe(
2174 getConstantArrayType(NewEltTy, CAT->getSize(),
2175 CAT->getSizeModifier(),
2176 CAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002177 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002178 return CanQualType::CreateUnsafe(
2179 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
2180 IAT->getIndexTypeQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Douglas Gregor898574e2008-12-05 23:32:09 +00002182 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor50d62d12009-08-05 05:36:45 +00002183 return CanQualType::CreateUnsafe(
2184 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002185 DSAT->getSizeExpr() ?
2186 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002187 DSAT->getSizeModifier(),
2188 DSAT->getIndexTypeQualifier(),
2189 DSAT->getBracketsRange()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002190
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002191 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor50d62d12009-08-05 05:36:45 +00002192 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002193 VAT->getSizeExpr() ?
2194 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor50d62d12009-08-05 05:36:45 +00002195 VAT->getSizeModifier(),
2196 VAT->getIndexTypeQualifier(),
2197 VAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002198}
2199
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002200TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2201 // If this template name refers to a template, the canonical
2202 // template name merely stores the template itself.
2203 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002204 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002205
Mike Stump1eb44332009-09-09 15:08:12 +00002206 // If this template name refers to a set of overloaded function templates,
Douglas Gregord99cbe62009-07-29 18:26:50 +00002207 /// the canonical template name merely stores the set of function templates.
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002208 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2209 OverloadedFunctionDecl *CanonOvl = 0;
2210 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2211 FEnd = Ovl->function_end();
2212 F != FEnd; ++F) {
2213 Decl *Canon = F->get()->getCanonicalDecl();
2214 if (CanonOvl || Canon != F->get()) {
2215 if (!CanonOvl)
Mike Stump1eb44332009-09-09 15:08:12 +00002216 CanonOvl = OverloadedFunctionDecl::Create(*this,
2217 Ovl->getDeclContext(),
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002218 Ovl->getDeclName());
Mike Stump1eb44332009-09-09 15:08:12 +00002219
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002220 CanonOvl->addOverload(
2221 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2222 }
2223 }
Mike Stump1eb44332009-09-09 15:08:12 +00002224
Douglas Gregor6ebd15e2009-07-31 05:24:01 +00002225 return TemplateName(CanonOvl? CanonOvl : Ovl);
2226 }
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002228 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2229 assert(DTN && "Non-dependent template names must refer to template decls.");
2230 return DTN->CanonicalTemplateName;
2231}
2232
Mike Stump1eb44332009-09-09 15:08:12 +00002233TemplateArgument
Douglas Gregor1275ae02009-07-28 23:00:59 +00002234ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2235 switch (Arg.getKind()) {
2236 case TemplateArgument::Null:
2237 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002238
Douglas Gregor1275ae02009-07-28 23:00:59 +00002239 case TemplateArgument::Expression:
2240 // FIXME: Build canonical expression?
2241 return Arg;
Mike Stump1eb44332009-09-09 15:08:12 +00002242
Douglas Gregor1275ae02009-07-28 23:00:59 +00002243 case TemplateArgument::Declaration:
2244 return TemplateArgument(SourceLocation(),
2245 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Douglas Gregor1275ae02009-07-28 23:00:59 +00002247 case TemplateArgument::Integral:
2248 return TemplateArgument(SourceLocation(),
2249 *Arg.getAsIntegral(),
2250 getCanonicalType(Arg.getIntegralType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Douglas Gregor1275ae02009-07-28 23:00:59 +00002252 case TemplateArgument::Type:
2253 return TemplateArgument(SourceLocation(),
2254 getCanonicalType(Arg.getAsType()));
Mike Stump1eb44332009-09-09 15:08:12 +00002255
Douglas Gregor1275ae02009-07-28 23:00:59 +00002256 case TemplateArgument::Pack: {
2257 // FIXME: Allocate in ASTContext
2258 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2259 unsigned Idx = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002260 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregor1275ae02009-07-28 23:00:59 +00002261 AEnd = Arg.pack_end();
2262 A != AEnd; (void)++A, ++Idx)
2263 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Douglas Gregor1275ae02009-07-28 23:00:59 +00002265 TemplateArgument Result;
2266 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2267 return Result;
2268 }
2269 }
2270
2271 // Silence GCC warning
2272 assert(false && "Unhandled template argument kind");
2273 return TemplateArgument();
2274}
2275
Douglas Gregord57959a2009-03-27 23:10:48 +00002276NestedNameSpecifier *
2277ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump1eb44332009-09-09 15:08:12 +00002278 if (!NNS)
Douglas Gregord57959a2009-03-27 23:10:48 +00002279 return 0;
2280
2281 switch (NNS->getKind()) {
2282 case NestedNameSpecifier::Identifier:
2283 // Canonicalize the prefix but keep the identifier the same.
Mike Stump1eb44332009-09-09 15:08:12 +00002284 return NestedNameSpecifier::Create(*this,
Douglas Gregord57959a2009-03-27 23:10:48 +00002285 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2286 NNS->getAsIdentifier());
2287
2288 case NestedNameSpecifier::Namespace:
2289 // A namespace is canonical; build a nested-name-specifier with
2290 // this namespace and no prefix.
2291 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2292
2293 case NestedNameSpecifier::TypeSpec:
2294 case NestedNameSpecifier::TypeSpecWithTemplate: {
2295 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump1eb44332009-09-09 15:08:12 +00002296 return NestedNameSpecifier::Create(*this, 0,
2297 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00002298 T.getTypePtr());
2299 }
2300
2301 case NestedNameSpecifier::Global:
2302 // The global specifier is canonical and unique.
2303 return NNS;
2304 }
2305
2306 // Required to silence a GCC warning
2307 return 0;
2308}
2309
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002310
2311const ArrayType *ASTContext::getAsArrayType(QualType T) {
2312 // Handle the non-qualified case efficiently.
2313 if (T.getCVRQualifiers() == 0) {
2314 // Handle the common positive case fast.
2315 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2316 return AT;
2317 }
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002319 // Handle the common negative case fast, ignoring CVR qualifiers.
2320 QualType CType = T->getCanonicalTypeInternal();
Mike Stump1eb44332009-09-09 15:08:12 +00002321
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002322 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002323 // test.
2324 if (!isa<ArrayType>(CType) &&
2325 !isa<ArrayType>(CType.getUnqualifiedType()))
2326 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002327
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002328 // Apply any CVR qualifiers from the array type to the element type. This
2329 // implements C99 6.7.3p8: "If the specification of an array type includes
2330 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002332 // If we get here, we either have type qualifiers on the type, or we have
2333 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor50d62d12009-08-05 05:36:45 +00002334 // we must propagate them down into the element type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002335 unsigned CVRQuals = T.getCVRQualifiers();
2336 unsigned AddrSpace = 0;
2337 Type *Ty = T.getTypePtr();
Mike Stump1eb44332009-09-09 15:08:12 +00002338
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002339 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002340 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002341 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
2342 AddrSpace = EXTQT->getAddressSpace();
2343 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002344 } else {
2345 T = Ty->getDesugaredType();
2346 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
2347 break;
2348 CVRQuals |= T.getCVRQualifiers();
2349 Ty = T.getTypePtr();
2350 }
2351 }
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002353 // If we have a simple case, just return now.
2354 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
2355 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
2356 return ATy;
Mike Stump1eb44332009-09-09 15:08:12 +00002357
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002358 // Otherwise, we have an array and we have qualifiers on it. Push the
2359 // qualifiers into the array element type and return a new array type.
2360 // Get the canonical version of the element with the extra qualifiers on it.
2361 // This can recursively sink qualifiers through multiple levels of arrays.
2362 QualType NewEltTy = ATy->getElementType();
2363 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002364 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002365 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
Mike Stump1eb44332009-09-09 15:08:12 +00002366
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002367 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2368 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2369 CAT->getSizeModifier(),
2370 CAT->getIndexTypeQualifier()));
2371 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2372 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2373 IAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002374 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002375
Mike Stump1eb44332009-09-09 15:08:12 +00002376 if (const DependentSizedArrayType *DSAT
Douglas Gregor898574e2008-12-05 23:32:09 +00002377 = dyn_cast<DependentSizedArrayType>(ATy))
2378 return cast<ArrayType>(
Mike Stump1eb44332009-09-09 15:08:12 +00002379 getDependentSizedArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002380 DSAT->getSizeExpr() ?
2381 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor898574e2008-12-05 23:32:09 +00002382 DSAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002383 DSAT->getIndexTypeQualifier(),
2384 DSAT->getBracketsRange()));
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002386 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002387 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedmanbbed6b92009-08-15 02:50:32 +00002388 VAT->getSizeExpr() ?
2389 VAT->getSizeExpr()->Retain() : 0,
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002390 VAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002391 VAT->getIndexTypeQualifier(),
2392 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002393}
2394
2395
Chris Lattnere6327742008-04-02 05:18:44 +00002396/// getArrayDecayedType - Return the properly qualified result of decaying the
2397/// specified array type to a pointer. This operation is non-trivial when
2398/// handling typedefs etc. The canonical type of "T" must be an array type,
2399/// this returns a pointer to a properly qualified element of the array.
2400///
2401/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2402QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002403 // Get the element type with 'getAsArrayType' so that we don't lose any
2404 // typedefs in the element type of the array. This also handles propagation
2405 // of type qualifiers from the array type into the element type if present
2406 // (C99 6.7.3p8).
2407 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2408 assert(PrettyArrayType && "Not an array type!");
Mike Stump1eb44332009-09-09 15:08:12 +00002409
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002410 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002411
2412 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002413 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00002414}
2415
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002416QualType ASTContext::getBaseElementType(QualType QT) {
2417 QualifierSet qualifiers;
2418 while (true) {
2419 const Type *UT = qualifiers.strip(QT);
2420 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2421 QT = AT->getElementType();
Mike Stump6dcbc292009-07-25 23:24:03 +00002422 } else {
Douglas Gregor5e03f9e2009-07-23 23:49:00 +00002423 return qualifiers.apply(QT, *this);
2424 }
2425 }
2426}
2427
Daniel Dunbard786f6a2009-01-05 22:14:37 +00002428QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00002429 QualType ElemTy = VAT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002430
Anders Carlsson6183a992008-12-21 03:44:36 +00002431 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
2432 return getBaseElementType(VAT);
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Anders Carlsson6183a992008-12-21 03:44:36 +00002434 return ElemTy;
2435}
2436
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002437/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump1eb44332009-09-09 15:08:12 +00002438uint64_t
Fariborz Jahanian0de78992009-08-21 16:31:06 +00002439ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2440 uint64_t ElementCount = 1;
2441 do {
2442 ElementCount *= CA->getSize().getZExtValue();
2443 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2444 } while (CA);
2445 return ElementCount;
2446}
2447
Reid Spencer5f016e22007-07-11 17:01:13 +00002448/// getFloatingRank - Return a relative rank for floating point types.
2449/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002450static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00002451 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00002452 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002453
Daniel Dunbard786f6a2009-01-05 22:14:37 +00002454 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00002455 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002456 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002457 case BuiltinType::Float: return FloatRank;
2458 case BuiltinType::Double: return DoubleRank;
2459 case BuiltinType::LongDouble: return LongDoubleRank;
2460 }
2461}
2462
Mike Stump1eb44332009-09-09 15:08:12 +00002463/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2464/// point or a complex type (based on typeDomain/typeSize).
Steve Naroff716c7302007-08-27 01:41:48 +00002465/// 'typeDomain' is a real floating point or complex type.
2466/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002467QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2468 QualType Domain) const {
2469 FloatingRank EltRank = getFloatingRank(Size);
2470 if (Domain->isComplexType()) {
2471 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002472 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002473 case FloatRank: return FloatComplexTy;
2474 case DoubleRank: return DoubleComplexTy;
2475 case LongDoubleRank: return LongDoubleComplexTy;
2476 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002477 }
Chris Lattner1361b112008-04-06 23:58:54 +00002478
2479 assert(Domain->isRealFloatingType() && "Unknown domain!");
2480 switch (EltRank) {
2481 default: assert(0 && "getFloatingRank(): illegal value for rank");
2482 case FloatRank: return FloatTy;
2483 case DoubleRank: return DoubleTy;
2484 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002485 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002486}
2487
Chris Lattner7cfeb082008-04-06 23:55:33 +00002488/// getFloatingTypeOrder - Compare the rank of the two specified floating
2489/// point types, ignoring the domain of the type (i.e. 'double' ==
2490/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002491/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002492int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2493 FloatingRank LHSR = getFloatingRank(LHS);
2494 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Chris Lattnera75cea32008-04-06 23:38:49 +00002496 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002497 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002498 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002499 return 1;
2500 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002501}
2502
Chris Lattnerf52ab252008-04-06 22:59:24 +00002503/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2504/// routine will assert if passed a built-in type that isn't an integer or enum,
2505/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002506unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002507 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002508 if (EnumType* ET = dyn_cast<EnumType>(T))
2509 T = ET->getDecl()->getIntegerType().getTypePtr();
2510
Eli Friedmana3426752009-07-05 23:44:27 +00002511 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2512 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2513
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002514 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2515 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2516
2517 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2518 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2519
Eli Friedmanf98aba32009-02-13 02:31:07 +00002520 // There are two things which impact the integer rank: the width, and
2521 // the ordering of builtins. The builtin ordering is encoded in the
2522 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002523 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002524 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002525
Chris Lattnerf52ab252008-04-06 22:59:24 +00002526 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002527 default: assert(0 && "getIntegerRank(): not a built-in integer");
2528 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002529 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002530 case BuiltinType::Char_S:
2531 case BuiltinType::Char_U:
2532 case BuiltinType::SChar:
2533 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002534 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002535 case BuiltinType::Short:
2536 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002537 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002538 case BuiltinType::Int:
2539 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002540 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002541 case BuiltinType::Long:
2542 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002543 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002544 case BuiltinType::LongLong:
2545 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002546 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002547 case BuiltinType::Int128:
2548 case BuiltinType::UInt128:
2549 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002550 }
2551}
2552
Eli Friedman04e83572009-08-20 04:21:42 +00002553/// \brief Whether this is a promotable bitfield reference according
2554/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2555///
2556/// \returns the type this bit-field will promote to, or NULL if no
2557/// promotion occurs.
2558QualType ASTContext::isPromotableBitField(Expr *E) {
2559 FieldDecl *Field = E->getBitField();
2560 if (!Field)
2561 return QualType();
2562
2563 QualType FT = Field->getType();
2564
2565 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2566 uint64_t BitWidth = BitWidthAP.getZExtValue();
2567 uint64_t IntSize = getTypeSize(IntTy);
2568 // GCC extension compatibility: if the bit-field size is less than or equal
2569 // to the size of int, it gets promoted no matter what its type is.
2570 // For instance, unsigned long bf : 4 gets promoted to signed int.
2571 if (BitWidth < IntSize)
2572 return IntTy;
2573
2574 if (BitWidth == IntSize)
2575 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2576
2577 // Types bigger than int are not subject to promotions, and therefore act
2578 // like the base type.
2579 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2580 // is ridiculous.
2581 return QualType();
2582}
2583
Eli Friedmana95d7572009-08-19 07:44:53 +00002584/// getPromotedIntegerType - Returns the type that Promotable will
2585/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2586/// integer type.
2587QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2588 assert(!Promotable.isNull());
2589 assert(Promotable->isPromotableIntegerType());
2590 if (Promotable->isSignedIntegerType())
2591 return IntTy;
2592 uint64_t PromotableSize = getTypeSize(Promotable);
2593 uint64_t IntSize = getTypeSize(IntTy);
2594 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2595 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2596}
2597
Mike Stump1eb44332009-09-09 15:08:12 +00002598/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattner7cfeb082008-04-06 23:55:33 +00002599/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump1eb44332009-09-09 15:08:12 +00002600/// LHS < RHS, return -1.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002601int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002602 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2603 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002604 if (LHSC == RHSC) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Chris Lattnerf52ab252008-04-06 22:59:24 +00002606 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2607 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Chris Lattner7cfeb082008-04-06 23:55:33 +00002609 unsigned LHSRank = getIntegerRank(LHSC);
2610 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Chris Lattner7cfeb082008-04-06 23:55:33 +00002612 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2613 if (LHSRank == RHSRank) return 0;
2614 return LHSRank > RHSRank ? 1 : -1;
2615 }
Mike Stump1eb44332009-09-09 15:08:12 +00002616
Chris Lattner7cfeb082008-04-06 23:55:33 +00002617 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2618 if (LHSUnsigned) {
2619 // If the unsigned [LHS] type is larger, return it.
2620 if (LHSRank >= RHSRank)
2621 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Chris Lattner7cfeb082008-04-06 23:55:33 +00002623 // If the signed type can represent all values of the unsigned type, it
2624 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002625 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002626 return -1;
2627 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002628
Chris Lattner7cfeb082008-04-06 23:55:33 +00002629 // If the unsigned [RHS] type is larger, return it.
2630 if (RHSRank >= LHSRank)
2631 return -1;
Mike Stump1eb44332009-09-09 15:08:12 +00002632
Chris Lattner7cfeb082008-04-06 23:55:33 +00002633 // If the signed type can represent all values of the unsigned type, it
2634 // wins. Because we are dealing with 2's complement and types that are
Mike Stump1eb44332009-09-09 15:08:12 +00002635 // powers of two larger than each other, this is always safe.
Chris Lattner7cfeb082008-04-06 23:55:33 +00002636 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002637}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002638
Mike Stump1eb44332009-09-09 15:08:12 +00002639// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson71993dd2007-08-17 05:31:46 +00002640QualType ASTContext::getCFConstantStringType() {
2641 if (!CFConstantStringTypeDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002642 CFConstantStringTypeDecl =
2643 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002644 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002645 QualType FieldTypes[4];
Mike Stump1eb44332009-09-09 15:08:12 +00002646
Anders Carlsson71993dd2007-08-17 05:31:46 +00002647 // const int *isa;
Mike Stump1eb44332009-09-09 15:08:12 +00002648 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002649 // int flags;
2650 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002651 // const char *str;
Mike Stump1eb44332009-09-09 15:08:12 +00002652 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002653 // long length;
Mike Stump1eb44332009-09-09 15:08:12 +00002654 FieldTypes[3] = LongTy;
2655
Anders Carlsson71993dd2007-08-17 05:31:46 +00002656 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002657 for (unsigned i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002658 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor44b43212008-12-11 16:49:14 +00002659 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002660 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002661 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002662 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002663 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002664 }
2665
2666 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002667 }
Mike Stump1eb44332009-09-09 15:08:12 +00002668
Anders Carlsson71993dd2007-08-17 05:31:46 +00002669 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002670}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002671
Douglas Gregor319ac892009-04-23 22:29:11 +00002672void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002673 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002674 assert(Rec && "Invalid CFConstantStringType");
2675 CFConstantStringTypeDecl = Rec->getDecl();
2676}
2677
Mike Stump1eb44332009-09-09 15:08:12 +00002678QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002679 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002680 ObjCFastEnumerationStateTypeDecl =
2681 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2682 &Idents.get("__objcFastEnumerationState"));
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002684 QualType FieldTypes[] = {
2685 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002686 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002687 getPointerType(UnsignedLongTy),
2688 getConstantArrayType(UnsignedLongTy,
2689 llvm::APInt(32, 5), ArrayType::Normal, 0)
2690 };
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Douglas Gregor44b43212008-12-11 16:49:14 +00002692 for (size_t i = 0; i < 4; ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +00002693 FieldDecl *Field = FieldDecl::Create(*this,
2694 ObjCFastEnumerationStateTypeDecl,
2695 SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00002696 FieldTypes[i], /*DInfo=*/0,
Mike Stump1eb44332009-09-09 15:08:12 +00002697 /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002698 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002699 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002700 }
Mike Stump1eb44332009-09-09 15:08:12 +00002701
Douglas Gregor44b43212008-12-11 16:49:14 +00002702 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002703 }
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002705 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2706}
2707
Douglas Gregor319ac892009-04-23 22:29:11 +00002708void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00002709 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor319ac892009-04-23 22:29:11 +00002710 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2711 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2712}
2713
Anders Carlssone8c49532007-10-29 06:33:42 +00002714// This returns true if a type has been typedefed to BOOL:
2715// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002716static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002717 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002718 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2719 return II->isStr("BOOL");
Mike Stump1eb44332009-09-09 15:08:12 +00002720
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002721 return false;
2722}
2723
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002724/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002725/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002726int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002727 uint64_t sz = getTypeSize(type);
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002729 // Make all integer and enum types at least as large as an int
2730 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002731 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002732 // Treat arrays as pointers, since that's how they're passed in.
2733 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002734 sz = getTypeSize(VoidPtrTy);
2735 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002736}
2737
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002738/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002739/// declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002740void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002741 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002742 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002743 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002744 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002745 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002746 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002747 // Compute size of all parameters.
2748 // Start with computing size of a pointer in number of bytes.
2749 // FIXME: There might(should) be a better way of doing this computation!
2750 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002751 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002752 // The first two arguments (self and _cmd) are pointers; account for
2753 // their size.
2754 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002755 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2756 E = Decl->param_end(); PI != E; ++PI) {
2757 QualType PType = (*PI)->getType();
2758 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002759 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002760 ParmOffset += sz;
2761 }
2762 S += llvm::utostr(ParmOffset);
2763 S += "@0:";
2764 S += llvm::utostr(PtrSize);
Mike Stump1eb44332009-09-09 15:08:12 +00002765
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002766 // Argument types.
2767 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002768 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2769 E = Decl->param_end(); PI != E; ++PI) {
2770 ParmVarDecl *PVDecl = *PI;
Mike Stump1eb44332009-09-09 15:08:12 +00002771 QualType PType = PVDecl->getOriginalType();
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002772 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002773 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2774 // Use array's original type only if it has known number of
2775 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002776 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002777 PType = PVDecl->getType();
2778 } else if (PType->isFunctionType())
2779 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002780 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002781 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002782 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002783 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002784 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002785 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002786 }
2787}
2788
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002789/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002790/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002791/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2792/// NULL when getting encodings for protocol properties.
Mike Stump1eb44332009-09-09 15:08:12 +00002793/// Property attributes are stored as a comma-delimited C string. The simple
2794/// attributes readonly and bycopy are encoded as single characters. The
2795/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2796/// encoded as single characters, followed by an identifier. Property types
2797/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002798/// these attributes are defined by the following enumeration:
2799/// @code
2800/// enum PropertyAttributes {
2801/// kPropertyReadOnly = 'R', // property is read-only.
2802/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2803/// kPropertyByref = '&', // property is a reference to the value last assigned
2804/// kPropertyDynamic = 'D', // property is dynamic
2805/// kPropertyGetter = 'G', // followed by getter selector name
2806/// kPropertySetter = 'S', // followed by setter selector name
2807/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2808/// kPropertyType = 't' // followed by old-style type encoding.
2809/// kPropertyWeak = 'W' // 'weak' property
2810/// kPropertyStrong = 'P' // property GC'able
2811/// kPropertyNonAtomic = 'N' // property non-atomic
2812/// };
2813/// @endcode
Mike Stump1eb44332009-09-09 15:08:12 +00002814void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002815 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002816 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002817 // Collect information from the property implementation decl(s).
2818 bool Dynamic = false;
2819 ObjCPropertyImplDecl *SynthesizePID = 0;
2820
2821 // FIXME: Duplicated code due to poor abstraction.
2822 if (Container) {
Mike Stump1eb44332009-09-09 15:08:12 +00002823 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002824 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2825 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002826 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002827 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002828 ObjCPropertyImplDecl *PID = *i;
2829 if (PID->getPropertyDecl() == PD) {
2830 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2831 Dynamic = true;
2832 } else {
2833 SynthesizePID = PID;
2834 }
2835 }
2836 }
2837 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002838 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002839 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002840 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002841 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002842 ObjCPropertyImplDecl *PID = *i;
2843 if (PID->getPropertyDecl() == PD) {
2844 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2845 Dynamic = true;
2846 } else {
2847 SynthesizePID = PID;
2848 }
2849 }
Mike Stump1eb44332009-09-09 15:08:12 +00002850 }
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002851 }
2852 }
2853
2854 // FIXME: This is not very efficient.
2855 S = "T";
2856
2857 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002858 // GCC has some special rules regarding encoding of properties which
2859 // closely resembles encoding of ivars.
Mike Stump1eb44332009-09-09 15:08:12 +00002860 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002861 true /* outermost type */,
2862 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002863
2864 if (PD->isReadOnly()) {
2865 S += ",R";
2866 } else {
2867 switch (PD->getSetterKind()) {
2868 case ObjCPropertyDecl::Assign: break;
2869 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002870 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002871 }
2872 }
2873
2874 // It really isn't clear at all what this means, since properties
2875 // are "dynamic by default".
2876 if (Dynamic)
2877 S += ",D";
2878
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002879 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2880 S += ",N";
Mike Stump1eb44332009-09-09 15:08:12 +00002881
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002882 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2883 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002884 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002885 }
2886
2887 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2888 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002889 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002890 }
2891
2892 if (SynthesizePID) {
2893 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2894 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002895 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002896 }
2897
2898 // FIXME: OBJCGC: weak & strong
2899}
2900
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002901/// getLegacyIntegralTypeEncoding -
Mike Stump1eb44332009-09-09 15:08:12 +00002902/// Another legacy compatibility encoding: 32-bit longs are encoded as
2903/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002904/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2905///
2906void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump8e1fab22009-07-22 18:58:19 +00002907 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002908 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002909 if (BT->getKind() == BuiltinType::ULong &&
2910 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002911 PointeeTy = UnsignedIntTy;
Mike Stump1eb44332009-09-09 15:08:12 +00002912 else
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002913 if (BT->getKind() == BuiltinType::Long &&
2914 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002915 PointeeTy = IntTy;
2916 }
2917 }
2918}
2919
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002920void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002921 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002922 // We follow the behavior of gcc, expanding structures which are
2923 // directly pointed to, and expanding embedded structures. Note that
2924 // these rules are sufficient to prevent recursive encoding of the
2925 // same type.
Mike Stump1eb44332009-09-09 15:08:12 +00002926 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002927 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002928}
2929
Mike Stump1eb44332009-09-09 15:08:12 +00002930static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002931 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002932 const Expr *E = FD->getBitWidth();
2933 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2934 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002935 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002936 S += 'b';
2937 S += llvm::utostr(N);
2938}
2939
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002940void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2941 bool ExpandPointedToStructures,
2942 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002943 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002944 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002945 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002946 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002947 if (FD && FD->isBitField())
2948 return EncodeBitField(this, S, FD);
2949 char encoding;
2950 switch (BT->getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002951 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002952 case BuiltinType::Void: encoding = 'v'; break;
2953 case BuiltinType::Bool: encoding = 'B'; break;
2954 case BuiltinType::Char_U:
2955 case BuiltinType::UChar: encoding = 'C'; break;
2956 case BuiltinType::UShort: encoding = 'S'; break;
2957 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002958 case BuiltinType::ULong:
2959 encoding =
2960 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002961 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002962 case BuiltinType::UInt128: encoding = 'T'; break;
2963 case BuiltinType::ULongLong: encoding = 'Q'; break;
2964 case BuiltinType::Char_S:
2965 case BuiltinType::SChar: encoding = 'c'; break;
2966 case BuiltinType::Short: encoding = 's'; break;
2967 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump1eb44332009-09-09 15:08:12 +00002968 case BuiltinType::Long:
2969 encoding =
2970 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002971 break;
2972 case BuiltinType::LongLong: encoding = 'q'; break;
2973 case BuiltinType::Int128: encoding = 't'; break;
2974 case BuiltinType::Float: encoding = 'f'; break;
2975 case BuiltinType::Double: encoding = 'd'; break;
2976 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002977 }
Mike Stump1eb44332009-09-09 15:08:12 +00002978
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002979 S += encoding;
2980 return;
2981 }
Mike Stump1eb44332009-09-09 15:08:12 +00002982
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002983 if (const ComplexType *CT = T->getAsComplexType()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002984 S += 'j';
Mike Stump1eb44332009-09-09 15:08:12 +00002985 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002986 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002987 return;
2988 }
Mike Stump1eb44332009-09-09 15:08:12 +00002989
Ted Kremenek6217b802009-07-29 21:53:49 +00002990 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002991 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002992 bool isReadOnly = false;
2993 // For historical/compatibility reasons, the read-only qualifier of the
2994 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2995 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump1eb44332009-09-09 15:08:12 +00002996 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump8e1fab22009-07-22 18:58:19 +00002997 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002998 if (OutermostType && T.isConstQualified()) {
2999 isReadOnly = true;
3000 S += 'r';
3001 }
Mike Stump9fdbab32009-07-31 02:02:20 +00003002 } else if (OutermostType) {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003003 QualType P = PointeeTy;
Ted Kremenek6217b802009-07-29 21:53:49 +00003004 while (P->getAs<PointerType>())
3005 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003006 if (P.isConstQualified()) {
3007 isReadOnly = true;
3008 S += 'r';
3009 }
3010 }
3011 if (isReadOnly) {
3012 // Another legacy compatibility encoding. Some ObjC qualifier and type
3013 // combinations need to be rearranged.
3014 // Rewrite "in const" from "nr" to "rn"
3015 const char * s = S.c_str();
3016 int len = S.length();
3017 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3018 std::string replace = "rn";
3019 S.replace(S.end()-2, S.end(), replace);
3020 }
3021 }
Steve Naroff14108da2009-07-10 23:34:53 +00003022 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00003023 S += ':';
3024 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00003025 }
Mike Stump1eb44332009-09-09 15:08:12 +00003026
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003027 if (PointeeTy->isCharType()) {
3028 // char pointer types should be encoded as '*' unless it is a
3029 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00003030 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003031 S += '*';
3032 return;
3033 }
Ted Kremenek6217b802009-07-29 21:53:49 +00003034 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff9533a7f2009-07-22 17:14:51 +00003035 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3036 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3037 S += '#';
3038 return;
3039 }
3040 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3041 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3042 S += '@';
3043 return;
3044 }
3045 // fall through...
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003046 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003047 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003048 getLegacyIntegralTypeEncoding(PointeeTy);
3049
Mike Stump1eb44332009-09-09 15:08:12 +00003050 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003051 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003052 return;
3053 }
Mike Stump1eb44332009-09-09 15:08:12 +00003054
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003055 if (const ArrayType *AT =
3056 // Ignore type qualifiers etc.
3057 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00003058 if (isa<IncompleteArrayType>(AT)) {
3059 // Incomplete arrays are encoded as a pointer to the array element.
3060 S += '^';
3061
Mike Stump1eb44332009-09-09 15:08:12 +00003062 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003063 false, ExpandStructures, FD);
3064 } else {
3065 S += '[';
Mike Stump1eb44332009-09-09 15:08:12 +00003066
Anders Carlsson559a8332009-02-22 01:38:57 +00003067 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3068 S += llvm::utostr(CAT->getSize().getZExtValue());
3069 else {
3070 //Variable length arrays are encoded as a regular array with 0 elements.
3071 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3072 S += '0';
3073 }
Mike Stump1eb44332009-09-09 15:08:12 +00003074
3075 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlsson559a8332009-02-22 01:38:57 +00003076 false, ExpandStructures, FD);
3077 S += ']';
3078 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003079 return;
3080 }
Mike Stump1eb44332009-09-09 15:08:12 +00003081
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003082 if (T->getAsFunctionType()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00003083 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003084 return;
3085 }
Mike Stump1eb44332009-09-09 15:08:12 +00003086
Ted Kremenek6217b802009-07-29 21:53:49 +00003087 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00003088 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003089 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00003090 // Anonymous structures print as '?'
3091 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3092 S += II->getName();
3093 } else {
3094 S += '?';
3095 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00003096 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003097 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003098 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3099 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00003100 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003101 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003102 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00003103 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003104 S += '"';
3105 }
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003107 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003108 if (Field->isBitField()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003109 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003110 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003111 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00003112 QualType qt = Field->getType();
3113 getLegacyIntegralTypeEncoding(qt);
Mike Stump1eb44332009-09-09 15:08:12 +00003114 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003115 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003116 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00003117 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00003118 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00003119 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003120 return;
3121 }
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003123 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00003124 if (FD && FD->isBitField())
3125 EncodeBitField(this, S, FD);
3126 else
3127 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003128 return;
3129 }
Mike Stump1eb44332009-09-09 15:08:12 +00003130
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003131 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00003132 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003133 return;
3134 }
Mike Stump1eb44332009-09-09 15:08:12 +00003135
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003136 if (T->isObjCInterfaceType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003137 // @encode(class_name)
3138 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
3139 S += '{';
3140 const IdentifierInfo *II = OI->getIdentifier();
3141 S += II->getName();
3142 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00003143 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003144 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00003145 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003146 if (RecFields[i]->isBitField())
Mike Stump1eb44332009-09-09 15:08:12 +00003147 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003148 RecFields[i]);
3149 else
Mike Stump1eb44332009-09-09 15:08:12 +00003150 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003151 FD);
3152 }
3153 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003154 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00003155 }
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003157 if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003158 if (OPT->isObjCIdType()) {
3159 S += '@';
3160 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003161 }
Mike Stump1eb44332009-09-09 15:08:12 +00003162
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003163 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00003164 S += '#';
3165 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003166 }
Mike Stump1eb44332009-09-09 15:08:12 +00003167
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003168 if (OPT->isObjCQualifiedIdType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003169 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff14108da2009-07-10 23:34:53 +00003170 ExpandPointedToStructures,
3171 ExpandStructures, FD);
3172 if (FD || EncodingProperty) {
3173 // Note that we do extended encoding of protocol qualifer list
3174 // Only when doing ivar or property encoding.
Steve Naroff14108da2009-07-10 23:34:53 +00003175 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003176 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3177 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff14108da2009-07-10 23:34:53 +00003178 S += '<';
3179 S += (*I)->getNameAsString();
3180 S += '>';
3181 }
3182 S += '"';
3183 }
3184 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003185 }
Mike Stump1eb44332009-09-09 15:08:12 +00003186
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003187 QualType PointeeTy = OPT->getPointeeType();
3188 if (!EncodingProperty &&
3189 isa<TypedefType>(PointeeTy.getTypePtr())) {
3190 // Another historical/compatibility reason.
Mike Stump1eb44332009-09-09 15:08:12 +00003191 // We encode the underlying type which comes out as
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003192 // {...};
3193 S += '^';
Mike Stump1eb44332009-09-09 15:08:12 +00003194 getObjCEncodingForTypeImpl(PointeeTy, S,
3195 false, ExpandPointedToStructures,
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003196 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00003197 return;
3198 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003199
3200 S += '@';
3201 if (FD || EncodingProperty) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003202 S += '"';
Steve Naroff67ef8ea2009-07-20 17:56:53 +00003203 S += OPT->getInterfaceDecl()->getNameAsCString();
3204 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3205 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003206 S += '<';
3207 S += (*I)->getNameAsString();
3208 S += '>';
Mike Stump1eb44332009-09-09 15:08:12 +00003209 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003210 S += '"';
3211 }
3212 return;
3213 }
Mike Stump1eb44332009-09-09 15:08:12 +00003214
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003215 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00003216}
3217
Mike Stump1eb44332009-09-09 15:08:12 +00003218void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00003219 std::string& S) const {
3220 if (QT & Decl::OBJC_TQ_In)
3221 S += 'n';
3222 if (QT & Decl::OBJC_TQ_Inout)
3223 S += 'N';
3224 if (QT & Decl::OBJC_TQ_Out)
3225 S += 'o';
3226 if (QT & Decl::OBJC_TQ_Bycopy)
3227 S += 'O';
3228 if (QT & Decl::OBJC_TQ_Byref)
3229 S += 'R';
3230 if (QT & Decl::OBJC_TQ_Oneway)
3231 S += 'V';
3232}
3233
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003234void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003235 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003236
Anders Carlssonb2cf3572007-10-11 01:00:40 +00003237 BuiltinVaListType = T;
3238}
3239
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003240void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003241 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00003242}
3243
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003244void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00003245 ObjCSelType = T;
3246
3247 const TypedefType *TT = T->getAsTypedefType();
3248 if (!TT)
3249 return;
3250 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003251
3252 // typedef struct objc_selector *SEL;
Ted Kremenek6217b802009-07-29 21:53:49 +00003253 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003254 if (!ptr)
3255 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003256 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00003257 if (!rec)
3258 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00003259 SelStructType = rec;
3260}
3261
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003262void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003263 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003264}
3265
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003266void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003267 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003268}
3269
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003270void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump1eb44332009-09-09 15:08:12 +00003271 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003272 "'NSConstantString' type already set!");
Mike Stump1eb44332009-09-09 15:08:12 +00003273
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003274 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003275}
3276
Douglas Gregor7532dc62009-03-30 22:58:21 +00003277/// \brief Retrieve the template name that represents a qualified
3278/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003279TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003280 bool TemplateKeyword,
3281 TemplateDecl *Template) {
3282 llvm::FoldingSetNodeID ID;
3283 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3284
3285 void *InsertPos = 0;
3286 QualifiedTemplateName *QTN =
3287 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3288 if (!QTN) {
3289 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3290 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3291 }
3292
3293 return TemplateName(QTN);
3294}
3295
Douglas Gregord99cbe62009-07-29 18:26:50 +00003296/// \brief Retrieve the template name that represents a qualified
3297/// template name such as \c std::vector.
Mike Stump1eb44332009-09-09 15:08:12 +00003298TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregord99cbe62009-07-29 18:26:50 +00003299 bool TemplateKeyword,
3300 OverloadedFunctionDecl *Template) {
3301 llvm::FoldingSetNodeID ID;
3302 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Douglas Gregord99cbe62009-07-29 18:26:50 +00003304 void *InsertPos = 0;
3305 QualifiedTemplateName *QTN =
3306 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3307 if (!QTN) {
3308 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3309 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3310 }
Mike Stump1eb44332009-09-09 15:08:12 +00003311
Douglas Gregord99cbe62009-07-29 18:26:50 +00003312 return TemplateName(QTN);
3313}
3314
Douglas Gregor7532dc62009-03-30 22:58:21 +00003315/// \brief Retrieve the template name that represents a dependent
3316/// template name such as \c MetaFun::template apply.
Mike Stump1eb44332009-09-09 15:08:12 +00003317TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00003318 const IdentifierInfo *Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00003319 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00003320 "Nested name specifier must be dependent");
Douglas Gregor7532dc62009-03-30 22:58:21 +00003321
3322 llvm::FoldingSetNodeID ID;
3323 DependentTemplateName::Profile(ID, NNS, Name);
3324
3325 void *InsertPos = 0;
3326 DependentTemplateName *QTN =
3327 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3328
3329 if (QTN)
3330 return TemplateName(QTN);
3331
3332 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3333 if (CanonNNS == NNS) {
3334 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3335 } else {
3336 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3337 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3338 }
3339
3340 DependentTemplateNames.InsertNode(QTN, InsertPos);
3341 return TemplateName(QTN);
3342}
3343
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003344/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003345/// TargetInfo, produce the corresponding type. The unsigned @p Type
3346/// is actually a value of type @c TargetInfo::IntType.
3347QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003348 switch (Type) {
Mike Stump1eb44332009-09-09 15:08:12 +00003349 case TargetInfo::NoInt: return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003350 case TargetInfo::SignedShort: return ShortTy;
3351 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3352 case TargetInfo::SignedInt: return IntTy;
3353 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3354 case TargetInfo::SignedLong: return LongTy;
3355 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3356 case TargetInfo::SignedLongLong: return LongLongTy;
3357 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3358 }
3359
3360 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003361 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003362}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003363
3364//===----------------------------------------------------------------------===//
3365// Type Predicates.
3366//===----------------------------------------------------------------------===//
3367
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003368/// isObjCNSObjectType - Return true if this is an NSObject object using
3369/// NSObject attribute on a c-style pointer type.
3370/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003371/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003372///
3373bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3374 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3375 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003376 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003377 return true;
3378 }
Mike Stump1eb44332009-09-09 15:08:12 +00003379 return false;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003380}
3381
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003382/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3383/// garbage collection attribute.
3384///
3385QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003386 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003387 if (getLangOptions().ObjC1 &&
3388 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003389 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003390 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump1eb44332009-09-09 15:08:12 +00003391 // (or pointers to them) be treated as though they were declared
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003392 // as __strong.
3393 if (GCAttrs == QualType::GCNone) {
Steve Narofff4954562009-07-16 15:41:00 +00003394 if (Ty->isObjCObjectPointerType())
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003395 GCAttrs = QualType::Strong;
3396 else if (Ty->isPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +00003397 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003398 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003399 // Non-pointers have none gc'able attribute regardless of the attribute
3400 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003401 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003402 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003403 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003404 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003405}
3406
Chris Lattner6ac46a42008-04-07 06:51:04 +00003407//===----------------------------------------------------------------------===//
3408// Type Compatibility Testing
3409//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003410
Mike Stump1eb44332009-09-09 15:08:12 +00003411/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003412/// compatible.
3413static bool areCompatVectorTypes(const VectorType *LHS,
3414 const VectorType *RHS) {
3415 assert(LHS->isCanonical() && RHS->isCanonical());
3416 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003417 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003418}
3419
Steve Naroff4084c302009-07-23 01:01:38 +00003420//===----------------------------------------------------------------------===//
3421// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3422//===----------------------------------------------------------------------===//
3423
3424/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3425/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003426bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3427 ObjCProtocolDecl *rProto) {
Steve Naroff4084c302009-07-23 01:01:38 +00003428 if (lProto == rProto)
3429 return true;
3430 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3431 E = rProto->protocol_end(); PI != E; ++PI)
3432 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3433 return true;
3434 return false;
3435}
3436
Steve Naroff4084c302009-07-23 01:01:38 +00003437/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3438/// return true if lhs's protocols conform to rhs's protocol; false
3439/// otherwise.
3440bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3441 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3442 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3443 return false;
3444}
3445
3446/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3447/// ObjCQualifiedIDType.
3448bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3449 bool compare) {
3450 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump1eb44332009-09-09 15:08:12 +00003451 if (lhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003452 lhs->isObjCIdType() || lhs->isObjCClassType())
3453 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003454 else if (rhs->isVoidPointerType() ||
Steve Naroff4084c302009-07-23 01:01:38 +00003455 rhs->isObjCIdType() || rhs->isObjCClassType())
3456 return true;
3457
3458 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
3459 const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType();
Mike Stump1eb44332009-09-09 15:08:12 +00003460
Steve Naroff4084c302009-07-23 01:01:38 +00003461 if (!rhsOPT) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003462
Steve Naroff4084c302009-07-23 01:01:38 +00003463 if (rhsOPT->qual_empty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003464 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff4084c302009-07-23 01:01:38 +00003465 // make sure we check the class hierarchy.
3466 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3467 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3468 E = lhsQID->qual_end(); I != E; ++I) {
3469 // when comparing an id<P> on lhs with a static type on rhs,
3470 // see if static class implements all of id's protocols, directly or
3471 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003472 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff4084c302009-07-23 01:01:38 +00003473 return false;
3474 }
3475 }
3476 // If there are no qualifiers and no interface, we have an 'id'.
3477 return true;
3478 }
Mike Stump1eb44332009-09-09 15:08:12 +00003479 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003480 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3481 E = lhsQID->qual_end(); I != E; ++I) {
3482 ObjCProtocolDecl *lhsProto = *I;
3483 bool match = false;
3484
3485 // when comparing an id<P> on lhs with a static type on rhs,
3486 // see if static class implements all of id's protocols, directly or
3487 // through its super class and categories.
3488 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3489 E = rhsOPT->qual_end(); J != E; ++J) {
3490 ObjCProtocolDecl *rhsProto = *J;
3491 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3492 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3493 match = true;
3494 break;
3495 }
3496 }
Mike Stump1eb44332009-09-09 15:08:12 +00003497 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff4084c302009-07-23 01:01:38 +00003498 // make sure we check the class hierarchy.
3499 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3500 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3501 E = lhsQID->qual_end(); I != E; ++I) {
3502 // when comparing an id<P> on lhs with a static type on rhs,
3503 // see if static class implements all of id's protocols, directly or
3504 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003505 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003506 match = true;
3507 break;
3508 }
3509 }
3510 }
3511 if (!match)
3512 return false;
3513 }
Mike Stump1eb44332009-09-09 15:08:12 +00003514
Steve Naroff4084c302009-07-23 01:01:38 +00003515 return true;
3516 }
Mike Stump1eb44332009-09-09 15:08:12 +00003517
Steve Naroff4084c302009-07-23 01:01:38 +00003518 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3519 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3520
Mike Stump1eb44332009-09-09 15:08:12 +00003521 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff4084c302009-07-23 01:01:38 +00003522 lhs->getAsObjCInterfacePointerType()) {
3523 if (lhsOPT->qual_empty()) {
3524 bool match = false;
3525 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3526 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3527 E = rhsQID->qual_end(); I != E; ++I) {
3528 // when comparing an id<P> on lhs with a static type on rhs,
3529 // see if static class implements all of id's protocols, directly or
3530 // through its super class and categories.
Fariborz Jahanian0fd89042009-08-11 22:02:25 +00003531 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff4084c302009-07-23 01:01:38 +00003532 match = true;
3533 break;
3534 }
3535 }
3536 if (!match)
3537 return false;
3538 }
3539 return true;
3540 }
Mike Stump1eb44332009-09-09 15:08:12 +00003541 // Both the right and left sides have qualifiers.
Steve Naroff4084c302009-07-23 01:01:38 +00003542 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3543 E = lhsOPT->qual_end(); I != E; ++I) {
3544 ObjCProtocolDecl *lhsProto = *I;
3545 bool match = false;
3546
3547 // when comparing an id<P> on lhs with a static type on rhs,
3548 // see if static class implements all of id's protocols, directly or
3549 // through its super class and categories.
3550 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3551 E = rhsQID->qual_end(); J != E; ++J) {
3552 ObjCProtocolDecl *rhsProto = *J;
3553 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3554 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3555 match = true;
3556 break;
3557 }
3558 }
3559 if (!match)
3560 return false;
3561 }
3562 return true;
3563 }
3564 return false;
3565}
3566
Eli Friedman3d815e72008-08-22 00:56:42 +00003567/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003568/// compatible for assignment from RHS to LHS. This handles validation of any
3569/// protocol qualifiers on the LHS or RHS.
3570///
Steve Naroff14108da2009-07-10 23:34:53 +00003571bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3572 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003573 // If either type represents the built-in 'id' or 'Class' types, return true.
3574 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003575 return true;
3576
Steve Naroff4084c302009-07-23 01:01:38 +00003577 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump1eb44332009-09-09 15:08:12 +00003578 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3579 QualType(RHSOPT,0),
Steve Naroff4084c302009-07-23 01:01:38 +00003580 false);
3581
Steve Naroff14108da2009-07-10 23:34:53 +00003582 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3583 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff4084c302009-07-23 01:01:38 +00003584 if (LHS && RHS) // We have 2 user-defined types.
3585 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump1eb44332009-09-09 15:08:12 +00003586
Steve Naroff4084c302009-07-23 01:01:38 +00003587 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003588}
3589
Eli Friedman3d815e72008-08-22 00:56:42 +00003590bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3591 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003592 // Verify that the base decls are compatible: the RHS must be a subclass of
3593 // the LHS.
3594 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3595 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003596
Chris Lattner6ac46a42008-04-07 06:51:04 +00003597 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3598 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003599 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003600 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003601
Chris Lattner6ac46a42008-04-07 06:51:04 +00003602 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3603 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003604 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003605 return true; // FIXME: should return false!
Mike Stump1eb44332009-09-09 15:08:12 +00003606
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003607 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3608 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003609 LHSPI != LHSPE; LHSPI++) {
3610 bool RHSImplementsProtocol = false;
3611
3612 // If the RHS doesn't implement the protocol on the left, the types
3613 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003614 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff4084c302009-07-23 01:01:38 +00003615 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003616 RHSPI != RHSPE; RHSPI++) {
3617 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003618 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003619 break;
3620 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003621 }
3622 // FIXME: For better diagnostics, consider passing back the protocol name.
3623 if (!RHSImplementsProtocol)
3624 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003625 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003626 // The RHS implements all protocols listed on the LHS.
3627 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003628}
3629
Steve Naroff389bf462009-02-12 17:52:19 +00003630bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3631 // get the "pointed to" types
Steve Naroff14108da2009-07-10 23:34:53 +00003632 const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType();
3633 const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType();
Mike Stump1eb44332009-09-09 15:08:12 +00003634
Steve Naroff14108da2009-07-10 23:34:53 +00003635 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003636 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003637
3638 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3639 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003640}
3641
Mike Stump1eb44332009-09-09 15:08:12 +00003642/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroffec0550f2007-10-15 20:41:53 +00003643/// both shall have the identically qualified version of a compatible type.
Mike Stump1eb44332009-09-09 15:08:12 +00003644/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroffec0550f2007-10-15 20:41:53 +00003645/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003646bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3647 return !mergeTypes(LHS, RHS).isNull();
3648}
3649
3650QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
3651 const FunctionType *lbase = lhs->getAsFunctionType();
3652 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00003653 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3654 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003655 bool allLTypes = true;
3656 bool allRTypes = true;
3657
3658 // Check return type
3659 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3660 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003661 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3662 allLTypes = false;
3663 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3664 allRTypes = false;
Mike Stump6dcbc292009-07-25 23:24:03 +00003665 // FIXME: double check this
Mike Stump24556362009-07-25 21:26:53 +00003666 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3667 if (NoReturn != lbase->getNoReturnAttr())
3668 allLTypes = false;
3669 if (NoReturn != rbase->getNoReturnAttr())
3670 allRTypes = false;
Mike Stump1eb44332009-09-09 15:08:12 +00003671
Eli Friedman3d815e72008-08-22 00:56:42 +00003672 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003673 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3674 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003675 unsigned lproto_nargs = lproto->getNumArgs();
3676 unsigned rproto_nargs = rproto->getNumArgs();
3677
3678 // Compatible functions must have the same number of arguments
3679 if (lproto_nargs != rproto_nargs)
3680 return QualType();
3681
3682 // Variadic and non-variadic functions aren't compatible
3683 if (lproto->isVariadic() != rproto->isVariadic())
3684 return QualType();
3685
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003686 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3687 return QualType();
3688
Eli Friedman3d815e72008-08-22 00:56:42 +00003689 // Check argument compatibility
3690 llvm::SmallVector<QualType, 10> types;
3691 for (unsigned i = 0; i < lproto_nargs; i++) {
3692 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3693 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3694 QualType argtype = mergeTypes(largtype, rargtype);
3695 if (argtype.isNull()) return QualType();
3696 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003697 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3698 allLTypes = false;
3699 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3700 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003701 }
3702 if (allLTypes) return lhs;
3703 if (allRTypes) return rhs;
3704 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump24556362009-07-25 21:26:53 +00003705 lproto->isVariadic(), lproto->getTypeQuals(),
3706 NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003707 }
3708
3709 if (lproto) allRTypes = false;
3710 if (rproto) allLTypes = false;
3711
Douglas Gregor72564e72009-02-26 23:50:07 +00003712 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003713 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003714 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003715 if (proto->isVariadic()) return QualType();
3716 // Check that the types are compatible with the types that
3717 // would result from default argument promotions (C99 6.7.5.3p15).
3718 // The only types actually affected are promotable integer
3719 // types and floats, which would be passed as a different
3720 // type depending on whether the prototype is visible.
3721 unsigned proto_nargs = proto->getNumArgs();
3722 for (unsigned i = 0; i < proto_nargs; ++i) {
3723 QualType argTy = proto->getArgType(i);
3724 if (argTy->isPromotableIntegerType() ||
3725 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3726 return QualType();
3727 }
3728
3729 if (allLTypes) return lhs;
3730 if (allRTypes) return rhs;
3731 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump2d3c1912009-07-27 00:44:23 +00003732 proto->getNumArgs(), proto->isVariadic(),
3733 proto->getTypeQuals(), NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003734 }
3735
3736 if (allLTypes) return lhs;
3737 if (allRTypes) return rhs;
Mike Stump24556362009-07-25 21:26:53 +00003738 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman3d815e72008-08-22 00:56:42 +00003739}
3740
3741QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003742 // C++ [expr]: If an expression initially has the type "reference to T", the
3743 // type is adjusted to "T" prior to any further analysis, the expression
3744 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003745 // expression is an lvalue unless the reference is an rvalue reference and
3746 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003747 // FIXME: C++ shouldn't be going through here! The rules are different
3748 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003749 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3750 // shouldn't be going through here!
Ted Kremenek6217b802009-07-29 21:53:49 +00003751 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003752 LHS = RT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00003753 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003754 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003755
Eli Friedman3d815e72008-08-22 00:56:42 +00003756 QualType LHSCan = getCanonicalType(LHS),
3757 RHSCan = getCanonicalType(RHS);
3758
3759 // If two types are identical, they are compatible.
3760 if (LHSCan == RHSCan)
3761 return LHS;
3762
3763 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003764 // Note that we handle extended qualifiers later, in the
3765 // case for ExtQualType.
3766 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003767 return QualType();
3768
Eli Friedman852d63b2009-06-01 01:22:52 +00003769 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3770 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003771
Chris Lattner1adb8832008-01-14 05:45:46 +00003772 // We want to consider the two function types to be the same for these
3773 // comparisons, just force one to the other.
3774 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3775 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003776
Eli Friedman07d25872009-06-02 05:28:56 +00003777 // Strip off objc_gc attributes off the top level so they can be merged.
3778 // This is a complete mess, but the attribute itself doesn't make much sense.
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003779 if (RHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003780 QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
3781 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003782 QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
Mike Stump1eb44332009-09-09 15:08:12 +00003783 // __weak attribute must appear on both declarations.
3784 // __strong attribue is redundant if other decl is an objective-c
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003785 // object pointer (or decorated with __strong attribute); otherwise
3786 // issue error.
3787 if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
3788 (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
Steve Naroff14108da2009-07-10 23:34:53 +00003789 !LHSCan->isObjCObjectPointerType()))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003790 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003791
Eli Friedman07d25872009-06-02 05:28:56 +00003792 RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
3793 RHS.getCVRQualifiers());
3794 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003795 if (!Result.isNull()) {
3796 if (Result.getObjCGCAttr() == QualType::GCNone)
3797 Result = getObjCGCQualType(Result, GCAttr);
3798 else if (Result.getObjCGCAttr() != GCAttr)
3799 Result = QualType();
3800 }
Eli Friedman07d25872009-06-02 05:28:56 +00003801 return Result;
3802 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003803 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003804 if (LHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003805 QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
3806 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003807 QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
3808 // __weak attribute must appear on both declarations. __strong
Mike Stump1eb44332009-09-09 15:08:12 +00003809 // __strong attribue is redundant if other decl is an objective-c
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003810 // object pointer (or decorated with __strong attribute); otherwise
3811 // issue error.
3812 if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
3813 (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
Steve Naroff14108da2009-07-10 23:34:53 +00003814 !RHSCan->isObjCObjectPointerType()))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003815 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +00003816
Eli Friedman07d25872009-06-02 05:28:56 +00003817 LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
3818 LHS.getCVRQualifiers());
3819 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003820 if (!Result.isNull()) {
3821 if (Result.getObjCGCAttr() == QualType::GCNone)
3822 Result = getObjCGCQualType(Result, GCAttr);
3823 else if (Result.getObjCGCAttr() != GCAttr)
3824 Result = QualType();
3825 }
Eli Friedman354e53d2009-06-02 07:45:37 +00003826 return Result;
Eli Friedman07d25872009-06-02 05:28:56 +00003827 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003828 }
3829
Eli Friedman4c721d32008-02-12 08:23:06 +00003830 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003831 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3832 LHSClass = Type::ConstantArray;
3833 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3834 RHSClass = Type::ConstantArray;
Mike Stump1eb44332009-09-09 15:08:12 +00003835
Nate Begeman213541a2008-04-18 23:10:10 +00003836 // Canonicalize ExtVector -> Vector.
3837 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3838 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump1eb44332009-09-09 15:08:12 +00003839
Chris Lattnera36a61f2008-04-07 05:43:21 +00003840 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003841 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003842 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump1eb44332009-09-09 15:08:12 +00003843 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003844 if (const EnumType* ETy = LHS->getAsEnumType()) {
3845 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3846 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003847 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003848 if (const EnumType* ETy = RHS->getAsEnumType()) {
3849 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3850 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003851 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003852
Eli Friedman3d815e72008-08-22 00:56:42 +00003853 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003854 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003855
Steve Naroff4a746782008-01-09 22:43:08 +00003856 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003857 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003858#define TYPE(Class, Base)
3859#define ABSTRACT_TYPE(Class, Base)
3860#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3861#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3862#include "clang/AST/TypeNodes.def"
3863 assert(false && "Non-canonical and dependent types shouldn't get here");
3864 return QualType();
3865
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003866 case Type::LValueReference:
3867 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003868 case Type::MemberPointer:
3869 assert(false && "C++ should never be in mergeTypes");
3870 return QualType();
3871
3872 case Type::IncompleteArray:
3873 case Type::VariableArray:
3874 case Type::FunctionProto:
3875 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00003876 assert(false && "Types are eliminated above");
3877 return QualType();
3878
Chris Lattner1adb8832008-01-14 05:45:46 +00003879 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003880 {
3881 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003882 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
3883 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003884 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3885 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003886 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003887 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003888 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003889 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003890 return getPointerType(ResultType);
3891 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003892 case Type::BlockPointer:
3893 {
3894 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek6217b802009-07-29 21:53:49 +00003895 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
3896 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00003897 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3898 if (ResultType.isNull()) return QualType();
3899 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3900 return LHS;
3901 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3902 return RHS;
3903 return getBlockPointerType(ResultType);
3904 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003905 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003906 {
3907 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3908 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3909 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3910 return QualType();
3911
3912 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3913 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3914 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3915 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003916 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3917 return LHS;
3918 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3919 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003920 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3921 ArrayType::ArraySizeModifier(), 0);
3922 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3923 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003924 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3925 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003926 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3927 return LHS;
3928 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3929 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003930 if (LVAT) {
3931 // FIXME: This isn't correct! But tricky to implement because
3932 // the array's size has to be the size of LHS, but the type
3933 // has to be different.
3934 return LHS;
3935 }
3936 if (RVAT) {
3937 // FIXME: This isn't correct! But tricky to implement because
3938 // the array's size has to be the size of RHS, but the type
3939 // has to be different.
3940 return RHS;
3941 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003942 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3943 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003944 return getIncompleteArrayType(ResultType,
3945 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003946 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003947 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003948 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003949 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003950 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003951 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003952 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003953 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003954 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003955 case Type::Complex:
3956 // Distinct complex types are incompatible.
3957 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003958 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003959 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003960 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3961 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003962 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003963 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003964 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003965 // FIXME: This should be type compatibility, e.g. whether
3966 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003967 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3968 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3969 if (LHSIface && RHSIface &&
3970 canAssignObjCInterfaces(LHSIface, RHSIface))
3971 return LHS;
3972
Eli Friedman3d815e72008-08-22 00:56:42 +00003973 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003974 }
Steve Naroff14108da2009-07-10 23:34:53 +00003975 case Type::ObjCObjectPointer: {
Mike Stump1eb44332009-09-09 15:08:12 +00003976 if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(),
Steve Naroff14108da2009-07-10 23:34:53 +00003977 RHS->getAsObjCObjectPointerType()))
3978 return LHS;
3979
Steve Naroffbc76dd02008-12-10 22:14:21 +00003980 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00003981 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003982 case Type::FixedWidthInt:
3983 // Distinct fixed-width integers are not compatible.
3984 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003985 case Type::ExtQual:
3986 // FIXME: ExtQual types can be compatible even if they're not
3987 // identical!
3988 return QualType();
3989 // First attempt at an implementation, but I'm not really sure it's
3990 // right...
3991#if 0
3992 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3993 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3994 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3995 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3996 return QualType();
3997 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3998 LHSBase = QualType(LQual->getBaseType(), 0);
3999 RHSBase = QualType(RQual->getBaseType(), 0);
4000 ResultType = mergeTypes(LHSBase, RHSBase);
4001 if (ResultType.isNull()) return QualType();
4002 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
4003 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
4004 return LHS;
4005 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
4006 return RHS;
4007 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
4008 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
4009 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
4010 return ResultType;
4011#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00004012
4013 case Type::TemplateSpecialization:
4014 assert(false && "Dependent types have no size");
4015 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00004016 }
Douglas Gregor72564e72009-02-26 23:50:07 +00004017
4018 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00004019}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00004020
Chris Lattner5426bf62008-04-07 07:01:58 +00004021//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00004022// Integer Predicates
4023//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00004024
Eli Friedmanad74a752008-06-28 06:23:08 +00004025unsigned ASTContext::getIntWidth(QualType T) {
4026 if (T == BoolTy)
4027 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00004028 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
4029 return FWIT->getWidth();
4030 }
4031 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00004032 return (unsigned)getTypeSize(T);
4033}
4034
4035QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
4036 assert(T->isSignedIntegerType() && "Unexpected type");
4037 if (const EnumType* ETy = T->getAsEnumType())
4038 T = ETy->getDecl()->getIntegerType();
4039 const BuiltinType* BTy = T->getAsBuiltinType();
4040 assert (BTy && "Unexpected signed integer type");
4041 switch (BTy->getKind()) {
4042 case BuiltinType::Char_S:
4043 case BuiltinType::SChar:
4044 return UnsignedCharTy;
4045 case BuiltinType::Short:
4046 return UnsignedShortTy;
4047 case BuiltinType::Int:
4048 return UnsignedIntTy;
4049 case BuiltinType::Long:
4050 return UnsignedLongTy;
4051 case BuiltinType::LongLong:
4052 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00004053 case BuiltinType::Int128:
4054 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00004055 default:
4056 assert(0 && "Unexpected signed integer type");
4057 return QualType();
4058 }
4059}
4060
Douglas Gregor2cf26342009-04-09 22:27:44 +00004061ExternalASTSource::~ExternalASTSource() { }
4062
4063void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00004064
4065
4066//===----------------------------------------------------------------------===//
4067// Builtin Type Computation
4068//===----------------------------------------------------------------------===//
4069
4070/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4071/// pointer over the consumed characters. This returns the resultant type.
Mike Stump1eb44332009-09-09 15:08:12 +00004072static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattner86df27b2009-06-14 00:45:47 +00004073 ASTContext::GetBuiltinTypeError &Error,
4074 bool AllowTypeModifiers = true) {
4075 // Modifiers.
4076 int HowLong = 0;
4077 bool Signed = false, Unsigned = false;
Mike Stump1eb44332009-09-09 15:08:12 +00004078
Chris Lattner86df27b2009-06-14 00:45:47 +00004079 // Read the modifiers first.
4080 bool Done = false;
4081 while (!Done) {
4082 switch (*Str++) {
Mike Stump1eb44332009-09-09 15:08:12 +00004083 default: Done = true; --Str; break;
Chris Lattner86df27b2009-06-14 00:45:47 +00004084 case 'S':
4085 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4086 assert(!Signed && "Can't use 'S' modifier multiple times!");
4087 Signed = true;
4088 break;
4089 case 'U':
4090 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4091 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4092 Unsigned = true;
4093 break;
4094 case 'L':
4095 assert(HowLong <= 2 && "Can't have LLLL modifier");
4096 ++HowLong;
4097 break;
4098 }
4099 }
4100
4101 QualType Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004102
Chris Lattner86df27b2009-06-14 00:45:47 +00004103 // Read the base type.
4104 switch (*Str++) {
4105 default: assert(0 && "Unknown builtin type letter!");
4106 case 'v':
4107 assert(HowLong == 0 && !Signed && !Unsigned &&
4108 "Bad modifiers used with 'v'!");
4109 Type = Context.VoidTy;
4110 break;
4111 case 'f':
4112 assert(HowLong == 0 && !Signed && !Unsigned &&
4113 "Bad modifiers used with 'f'!");
4114 Type = Context.FloatTy;
4115 break;
4116 case 'd':
4117 assert(HowLong < 2 && !Signed && !Unsigned &&
4118 "Bad modifiers used with 'd'!");
4119 if (HowLong)
4120 Type = Context.LongDoubleTy;
4121 else
4122 Type = Context.DoubleTy;
4123 break;
4124 case 's':
4125 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4126 if (Unsigned)
4127 Type = Context.UnsignedShortTy;
4128 else
4129 Type = Context.ShortTy;
4130 break;
4131 case 'i':
4132 if (HowLong == 3)
4133 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4134 else if (HowLong == 2)
4135 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4136 else if (HowLong == 1)
4137 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4138 else
4139 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4140 break;
4141 case 'c':
4142 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4143 if (Signed)
4144 Type = Context.SignedCharTy;
4145 else if (Unsigned)
4146 Type = Context.UnsignedCharTy;
4147 else
4148 Type = Context.CharTy;
4149 break;
4150 case 'b': // boolean
4151 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4152 Type = Context.BoolTy;
4153 break;
4154 case 'z': // size_t.
4155 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4156 Type = Context.getSizeType();
4157 break;
4158 case 'F':
4159 Type = Context.getCFConstantStringType();
4160 break;
4161 case 'a':
4162 Type = Context.getBuiltinVaListType();
4163 assert(!Type.isNull() && "builtin va list type not initialized!");
4164 break;
4165 case 'A':
4166 // This is a "reference" to a va_list; however, what exactly
4167 // this means depends on how va_list is defined. There are two
4168 // different kinds of va_list: ones passed by value, and ones
4169 // passed by reference. An example of a by-value va_list is
4170 // x86, where va_list is a char*. An example of by-ref va_list
4171 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4172 // we want this argument to be a char*&; for x86-64, we want
4173 // it to be a __va_list_tag*.
4174 Type = Context.getBuiltinVaListType();
4175 assert(!Type.isNull() && "builtin va list type not initialized!");
4176 if (Type->isArrayType()) {
4177 Type = Context.getArrayDecayedType(Type);
4178 } else {
4179 Type = Context.getLValueReferenceType(Type);
4180 }
4181 break;
4182 case 'V': {
4183 char *End;
Chris Lattner86df27b2009-06-14 00:45:47 +00004184 unsigned NumElements = strtoul(Str, &End, 10);
4185 assert(End != Str && "Missing vector size");
Mike Stump1eb44332009-09-09 15:08:12 +00004186
Chris Lattner86df27b2009-06-14 00:45:47 +00004187 Str = End;
Mike Stump1eb44332009-09-09 15:08:12 +00004188
Chris Lattner86df27b2009-06-14 00:45:47 +00004189 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4190 Type = Context.getVectorType(ElementType, NumElements);
4191 break;
4192 }
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004193 case 'P':
Douglas Gregorc29f77b2009-07-07 16:35:42 +00004194 Type = Context.getFILEType();
4195 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004196 Error = ASTContext::GE_Missing_stdio;
Chris Lattner86df27b2009-06-14 00:45:47 +00004197 return QualType();
4198 }
Mike Stumpfd612db2009-07-28 23:47:15 +00004199 break;
Chris Lattner9a5a7e72009-07-28 22:49:34 +00004200 case 'J':
Mike Stumpf711c412009-07-28 23:57:15 +00004201 if (Signed)
Mike Stump782fa302009-07-28 02:25:19 +00004202 Type = Context.getsigjmp_bufType();
Mike Stumpf711c412009-07-28 23:57:15 +00004203 else
4204 Type = Context.getjmp_bufType();
4205
Mike Stumpfd612db2009-07-28 23:47:15 +00004206 if (Type.isNull()) {
Mike Stumpf711c412009-07-28 23:57:15 +00004207 Error = ASTContext::GE_Missing_setjmp;
Mike Stumpfd612db2009-07-28 23:47:15 +00004208 return QualType();
4209 }
4210 break;
Mike Stump782fa302009-07-28 02:25:19 +00004211 }
Mike Stump1eb44332009-09-09 15:08:12 +00004212
Chris Lattner86df27b2009-06-14 00:45:47 +00004213 if (!AllowTypeModifiers)
4214 return Type;
Mike Stump1eb44332009-09-09 15:08:12 +00004215
Chris Lattner86df27b2009-06-14 00:45:47 +00004216 Done = false;
4217 while (!Done) {
4218 switch (*Str++) {
4219 default: Done = true; --Str; break;
4220 case '*':
4221 Type = Context.getPointerType(Type);
4222 break;
4223 case '&':
4224 Type = Context.getLValueReferenceType(Type);
4225 break;
4226 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4227 case 'C':
4228 Type = Type.getQualifiedType(QualType::Const);
4229 break;
4230 }
4231 }
Mike Stump1eb44332009-09-09 15:08:12 +00004232
Chris Lattner86df27b2009-06-14 00:45:47 +00004233 return Type;
4234}
4235
4236/// GetBuiltinType - Return the type for the specified builtin.
4237QualType ASTContext::GetBuiltinType(unsigned id,
4238 GetBuiltinTypeError &Error) {
4239 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump1eb44332009-09-09 15:08:12 +00004240
Chris Lattner86df27b2009-06-14 00:45:47 +00004241 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00004242
Chris Lattner86df27b2009-06-14 00:45:47 +00004243 Error = GE_None;
4244 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4245 if (Error != GE_None)
4246 return QualType();
4247 while (TypeStr[0] && TypeStr[0] != '.') {
4248 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4249 if (Error != GE_None)
4250 return QualType();
4251
4252 // Do array -> pointer decay. The builtin should use the decayed type.
4253 if (Ty->isArrayType())
4254 Ty = getArrayDecayedType(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00004255
Chris Lattner86df27b2009-06-14 00:45:47 +00004256 ArgTypes.push_back(Ty);
4257 }
4258
4259 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4260 "'.' should only occur at end of builtin type list!");
4261
4262 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4263 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4264 return getFunctionNoProtoType(ResType);
4265 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4266 TypeStr[0] == '.', 0);
4267}
Eli Friedmana95d7572009-08-19 07:44:53 +00004268
4269QualType
4270ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4271 // Perform the usual unary conversions. We do this early so that
4272 // integral promotions to "int" can allow us to exit early, in the
4273 // lhs == rhs check. Also, for conversion purposes, we ignore any
4274 // qualifiers. For example, "const float" and "float" are
4275 // equivalent.
4276 if (lhs->isPromotableIntegerType())
4277 lhs = getPromotedIntegerType(lhs);
4278 else
4279 lhs = lhs.getUnqualifiedType();
4280 if (rhs->isPromotableIntegerType())
4281 rhs = getPromotedIntegerType(rhs);
4282 else
4283 rhs = rhs.getUnqualifiedType();
4284
4285 // If both types are identical, no conversion is needed.
4286 if (lhs == rhs)
4287 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004288
Eli Friedmana95d7572009-08-19 07:44:53 +00004289 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4290 // The caller can deal with this (e.g. pointer + int).
4291 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4292 return lhs;
Mike Stump1eb44332009-09-09 15:08:12 +00004293
4294 // At this point, we have two different arithmetic types.
4295
Eli Friedmana95d7572009-08-19 07:44:53 +00004296 // Handle complex types first (C99 6.3.1.8p1).
4297 if (lhs->isComplexType() || rhs->isComplexType()) {
4298 // if we have an integer operand, the result is the complex type.
Mike Stump1eb44332009-09-09 15:08:12 +00004299 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004300 // convert the rhs to the lhs complex type.
4301 return lhs;
4302 }
Mike Stump1eb44332009-09-09 15:08:12 +00004303 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004304 // convert the lhs to the rhs complex type.
4305 return rhs;
4306 }
4307 // This handles complex/complex, complex/float, or float/complex.
Mike Stump1eb44332009-09-09 15:08:12 +00004308 // When both operands are complex, the shorter operand is converted to the
4309 // type of the longer, and that is the type of the result. This corresponds
4310 // to what is done when combining two real floating-point operands.
4311 // The fun begins when size promotion occur across type domains.
Eli Friedmana95d7572009-08-19 07:44:53 +00004312 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump1eb44332009-09-09 15:08:12 +00004313 // floating-point type, the less precise type is converted, within it's
Eli Friedmana95d7572009-08-19 07:44:53 +00004314 // real or complex domain, to the precision of the other type. For example,
Mike Stump1eb44332009-09-09 15:08:12 +00004315 // when combining a "long double" with a "double _Complex", the
Eli Friedmana95d7572009-08-19 07:44:53 +00004316 // "double _Complex" is promoted to "long double _Complex".
4317 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004318
4319 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004320 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004321 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedmana95d7572009-08-19 07:44:53 +00004322 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump1eb44332009-09-09 15:08:12 +00004323 }
Eli Friedmana95d7572009-08-19 07:44:53 +00004324 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4325 // domains match. This is a requirement for our implementation, C99
4326 // does not require this promotion.
4327 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4328 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4329 return rhs;
4330 } else { // handle "_Complex double, double".
4331 return lhs;
4332 }
4333 }
4334 return lhs; // The domain/size match exactly.
4335 }
4336 // Now handle "real" floating types (i.e. float, double, long double).
4337 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4338 // if we have an integer operand, the result is the real floating type.
4339 if (rhs->isIntegerType()) {
4340 // convert rhs to the lhs floating point type.
4341 return lhs;
4342 }
4343 if (rhs->isComplexIntegerType()) {
4344 // convert rhs to the complex floating point type.
4345 return getComplexType(lhs);
4346 }
4347 if (lhs->isIntegerType()) {
4348 // convert lhs to the rhs floating point type.
4349 return rhs;
4350 }
Mike Stump1eb44332009-09-09 15:08:12 +00004351 if (lhs->isComplexIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00004352 // convert lhs to the complex floating point type.
4353 return getComplexType(rhs);
4354 }
4355 // We have two real floating types, float/complex combos were handled above.
4356 // Convert the smaller operand to the bigger result.
4357 int result = getFloatingTypeOrder(lhs, rhs);
4358 if (result > 0) // convert the rhs
4359 return lhs;
4360 assert(result < 0 && "illegal float comparison");
4361 return rhs; // convert the lhs
4362 }
4363 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4364 // Handle GCC complex int extension.
4365 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4366 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4367
4368 if (lhsComplexInt && rhsComplexInt) {
Mike Stump1eb44332009-09-09 15:08:12 +00004369 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedmana95d7572009-08-19 07:44:53 +00004370 rhsComplexInt->getElementType()) >= 0)
4371 return lhs; // convert the rhs
4372 return rhs;
4373 } else if (lhsComplexInt && rhs->isIntegerType()) {
4374 // convert the rhs to the lhs complex type.
4375 return lhs;
4376 } else if (rhsComplexInt && lhs->isIntegerType()) {
4377 // convert the lhs to the rhs complex type.
4378 return rhs;
4379 }
4380 }
4381 // Finally, we have two differing integer types.
4382 // The rules for this case are in C99 6.3.1.8
4383 int compare = getIntegerTypeOrder(lhs, rhs);
4384 bool lhsSigned = lhs->isSignedIntegerType(),
4385 rhsSigned = rhs->isSignedIntegerType();
4386 QualType destType;
4387 if (lhsSigned == rhsSigned) {
4388 // Same signedness; use the higher-ranked type
4389 destType = compare >= 0 ? lhs : rhs;
4390 } else if (compare != (lhsSigned ? 1 : -1)) {
4391 // The unsigned type has greater than or equal rank to the
4392 // signed type, so use the unsigned type
4393 destType = lhsSigned ? rhs : lhs;
4394 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4395 // The two types are different widths; if we are here, that
4396 // means the signed type is larger than the unsigned type, so
4397 // use the signed type.
4398 destType = lhsSigned ? lhs : rhs;
4399 } else {
4400 // The signed type is higher-ranked than the unsigned type,
4401 // but isn't actually any bigger (like unsigned int and long
4402 // on most 32-bit systems). Use the unsigned type corresponding
4403 // to the signed type.
4404 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4405 }
4406 return destType;
4407}