blob: c7e5752ec3c03ac67efb408df6b7aad0f1334e22 [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerddc135e2006-11-10 06:34:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff67391b82007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000018#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000019#include "clang/AST/Expr.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000022#include "clang/Basic/Builtins.h"
Chris Lattnerd2868512009-03-28 03:45:20 +000023#include "clang/Basic/SourceManager.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000024#include "clang/Basic/TargetInfo.h"
Anders Carlssond8499822007-10-29 05:01:08 +000025#include "llvm/ADT/StringExtras.h"
Nate Begemanb699c9b2009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattnera5adead2009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Anders Carlssona4267a62009-07-18 21:19:52 +000028#include "RecordLayoutBuilder.h"
29
Chris Lattnerddc135e2006-11-10 06:34:16 +000030using namespace clang;
31
Steve Naroff0af91202007-04-27 21:51:21 +000032enum FloatingRank {
33 FloatRank, DoubleRank, LongDoubleRank
34};
35
Chris Lattner465fa322008-10-05 17:34:18 +000036ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
37 TargetInfo &t,
Daniel Dunbar221fa942008-08-11 04:54:23 +000038 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner15ba9492009-06-14 01:54:56 +000039 Builtin::Context &builtins,
Mike Stump11289f42009-09-09 15:08:12 +000040 bool FreeMem, unsigned size_reserve) :
41 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Mike Stumpa4de80b2009-07-28 02:25:19 +000042 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
Mike Stump11289f42009-09-09 15:08:12 +000043 sigjmp_bufDecl(0), SourceMgr(SM), LangOpts(LOpts),
44 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +000045 Idents(idents), Selectors(sels),
Mike Stump11289f42009-09-09 15:08:12 +000046 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
David Chisnall9f57c292009-08-17 16:35:33 +000047 ObjCIdRedefinitionType = QualType();
48 ObjCClassRedefinitionType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +000049 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbar221fa942008-08-11 04:54:23 +000050 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff7cae42b2009-07-10 23:34:53 +000051 InitBuiltinTypes();
Daniel Dunbar221fa942008-08-11 04:54:23 +000052}
53
Chris Lattnerd5973eb2006-11-12 00:53:46 +000054ASTContext::~ASTContext() {
55 // Deallocate all the types.
56 while (!Types.empty()) {
Ted Kremeneka2157712008-05-21 16:38:54 +000057 Types.back()->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +000058 Types.pop_back();
59 }
Eli Friedmane2bbfe22008-05-27 03:08:09 +000060
Nuno Lopese013c7f2008-12-17 22:30:25 +000061 {
John McCall8ccfcb52009-09-24 19:53:00 +000062 llvm::FoldingSet<ExtQuals>::iterator
63 I = ExtQualNodes.begin(), E = ExtQualNodes.end();
64 while (I != E)
65 Deallocate(&*I++);
66 }
67
68 {
Nuno Lopese013c7f2008-12-17 22:30:25 +000069 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
70 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
71 while (I != E) {
72 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
73 delete R;
74 }
75 }
76
77 {
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +000078 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
79 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopese013c7f2008-12-17 22:30:25 +000080 while (I != E) {
81 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
82 delete R;
83 }
84 }
85
Douglas Gregorf21eb492009-03-26 23:50:42 +000086 // Destroy nested-name-specifiers.
Douglas Gregorc741fb12009-03-27 23:54:10 +000087 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
88 NNS = NestedNameSpecifiers.begin(),
Mike Stump11289f42009-09-09 15:08:12 +000089 NNSEnd = NestedNameSpecifiers.end();
90 NNS != NNSEnd;
Douglas Gregorc741fb12009-03-27 23:54:10 +000091 /* Increment in loop */)
92 (*NNS++).Destroy(*this);
Douglas Gregorf21eb492009-03-26 23:50:42 +000093
94 if (GlobalNestedNameSpecifier)
95 GlobalNestedNameSpecifier->Destroy(*this);
96
Eli Friedmane2bbfe22008-05-27 03:08:09 +000097 TUDecl->Destroy(*this);
Chris Lattnerd5973eb2006-11-12 00:53:46 +000098}
99
Mike Stump11289f42009-09-09 15:08:12 +0000100void
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
102 ExternalSource.reset(Source.take());
103}
104
Chris Lattner4eb445d2007-01-26 01:27:23 +0000105void ASTContext::PrintStats() const {
106 fprintf(stderr, "*** AST Context Stats:\n");
107 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000108
Douglas Gregora30d0462009-05-26 14:40:08 +0000109 unsigned counts[] = {
Mike Stump11289f42009-09-09 15:08:12 +0000110#define TYPE(Name, Parent) 0,
Douglas Gregora30d0462009-05-26 14:40:08 +0000111#define ABSTRACT_TYPE(Name, Parent)
112#include "clang/AST/TypeNodes.def"
113 0 // Extra
114 };
Douglas Gregorb1fe2c92009-04-07 17:20:56 +0000115
Chris Lattner4eb445d2007-01-26 01:27:23 +0000116 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
117 Type *T = Types[i];
Douglas Gregora30d0462009-05-26 14:40:08 +0000118 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4eb445d2007-01-26 01:27:23 +0000119 }
120
Douglas Gregora30d0462009-05-26 14:40:08 +0000121 unsigned Idx = 0;
122 unsigned TotalBytes = 0;
123#define TYPE(Name, Parent) \
124 if (counts[Idx]) \
125 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
126 TotalBytes += counts[Idx] * sizeof(Name##Type); \
127 ++Idx;
128#define ABSTRACT_TYPE(Name, Parent)
129#include "clang/AST/TypeNodes.def"
Mike Stump11289f42009-09-09 15:08:12 +0000130
Douglas Gregora30d0462009-05-26 14:40:08 +0000131 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132
133 if (ExternalSource.get()) {
134 fprintf(stderr, "\n");
135 ExternalSource->PrintStats();
136 }
Chris Lattner4eb445d2007-01-26 01:27:23 +0000137}
138
139
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000140void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
John McCall90d1c2d2009-09-24 23:30:46 +0000141 BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
142 R = QualType(Ty, 0);
143 Types.push_back(Ty);
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000144}
145
Chris Lattner970e54e2006-11-12 00:37:36 +0000146void ASTContext::InitBuiltinTypes() {
147 assert(VoidTy.isNull() && "Context reinitialized?");
Mike Stump11289f42009-09-09 15:08:12 +0000148
Chris Lattner970e54e2006-11-12 00:37:36 +0000149 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000150 InitBuiltinType(VoidTy, BuiltinType::Void);
Mike Stump11289f42009-09-09 15:08:12 +0000151
Chris Lattner970e54e2006-11-12 00:37:36 +0000152 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000153 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000154 // C99 6.2.5p3.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000155 if (LangOpts.CharIsSigned)
Chris Lattnerb16f4552007-06-03 07:25:34 +0000156 InitBuiltinType(CharTy, BuiltinType::Char_S);
157 else
158 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000159 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000160 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
161 InitBuiltinType(ShortTy, BuiltinType::Short);
162 InitBuiltinType(IntTy, BuiltinType::Int);
163 InitBuiltinType(LongTy, BuiltinType::Long);
164 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000165
Chris Lattner970e54e2006-11-12 00:37:36 +0000166 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000167 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
168 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
169 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
170 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
171 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Mike Stump11289f42009-09-09 15:08:12 +0000172
Chris Lattner970e54e2006-11-12 00:37:36 +0000173 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000174 InitBuiltinType(FloatTy, BuiltinType::Float);
175 InitBuiltinType(DoubleTy, BuiltinType::Double);
176 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000177
Chris Lattnerf122cef2009-04-30 02:43:43 +0000178 // GNU extension, 128-bit integers.
179 InitBuiltinType(Int128Ty, BuiltinType::Int128);
180 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
181
Chris Lattner007cb022009-02-26 23:43:47 +0000182 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
183 InitBuiltinType(WCharTy, BuiltinType::WChar);
184 else // C99
185 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000186
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000187 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
188 InitBuiltinType(Char16Ty, BuiltinType::Char16);
189 else // C99
190 Char16Ty = getFromTargetType(Target.getChar16Type());
191
192 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
193 InitBuiltinType(Char32Ty, BuiltinType::Char32);
194 else // C99
195 Char32Ty = getFromTargetType(Target.getChar32Type());
196
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000197 // Placeholder type for functions.
Douglas Gregor4619e432008-12-05 23:32:09 +0000198 InitBuiltinType(OverloadTy, BuiltinType::Overload);
199
200 // Placeholder type for type-dependent expressions whose type is
201 // completely unknown. No code should ever check a type against
202 // DependentTy and users should never see it; however, it is here to
203 // help diagnose failures to properly check for type-dependent
204 // expressions.
205 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000206
Mike Stump11289f42009-09-09 15:08:12 +0000207 // Placeholder type for C++0x auto declarations whose real type has
Anders Carlsson082acde2009-06-26 18:41:36 +0000208 // not yet been deduced.
209 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
Mike Stump11289f42009-09-09 15:08:12 +0000210
Chris Lattner970e54e2006-11-12 00:37:36 +0000211 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000212 FloatComplexTy = getComplexType(FloatTy);
213 DoubleComplexTy = getComplexType(DoubleTy);
214 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor5251f1b2008-10-21 16:13:35 +0000215
Steve Naroff66e9f332007-10-15 14:41:52 +0000216 BuiltinVaListType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000217
Steve Naroff1329fa02009-07-15 18:40:39 +0000218 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
219 ObjCIdTypedefType = QualType();
220 ObjCClassTypedefType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000221
Steve Naroff1329fa02009-07-15 18:40:39 +0000222 // Builtin types for 'id' and 'Class'.
223 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
224 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff7cae42b2009-07-10 23:34:53 +0000225
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000226 ObjCConstantStringType = QualType();
Mike Stump11289f42009-09-09 15:08:12 +0000227
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000228 // void * type
229 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl576fd422009-05-10 18:38:11 +0000230
231 // nullptr type (C++0x 2.14.7)
232 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner970e54e2006-11-12 00:37:36 +0000233}
234
Douglas Gregor86d142a2009-10-08 07:24:58 +0000235MemberSpecializationInfo *
Douglas Gregor3c74d412009-10-14 20:14:33 +0000236ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000237 assert(Var->isStaticDataMember() && "Not a static data member");
Douglas Gregor3c74d412009-10-14 20:14:33 +0000238 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000239 = InstantiatedFromStaticDataMember.find(Var);
240 if (Pos == InstantiatedFromStaticDataMember.end())
241 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000242
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000243 return Pos->second;
244}
245
Mike Stump11289f42009-09-09 15:08:12 +0000246void
Douglas Gregor86d142a2009-10-08 07:24:58 +0000247ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
248 TemplateSpecializationKind TSK) {
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000249 assert(Inst->isStaticDataMember() && "Not a static data member");
250 assert(Tmpl->isStaticDataMember() && "Not a static data member");
251 assert(!InstantiatedFromStaticDataMember[Inst] &&
252 "Already noted what static data member was instantiated from");
Douglas Gregor86d142a2009-10-08 07:24:58 +0000253 InstantiatedFromStaticDataMember[Inst]
254 = new (*this) MemberSpecializationInfo(Tmpl, TSK);
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000255}
256
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000257UnresolvedUsingDecl *
258ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) {
Mike Stump11289f42009-09-09 15:08:12 +0000259 llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000260 = InstantiatedFromUnresolvedUsingDecl.find(UUD);
261 if (Pos == InstantiatedFromUnresolvedUsingDecl.end())
262 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000264 return Pos->second;
265}
266
267void
268ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD,
269 UnresolvedUsingDecl *UUD) {
270 assert(!InstantiatedFromUnresolvedUsingDecl[UD] &&
271 "Already noted what using decl what instantiated from");
272 InstantiatedFromUnresolvedUsingDecl[UD] = UUD;
273}
274
Anders Carlsson5da84842009-09-01 04:26:58 +0000275FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
276 llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
277 = InstantiatedFromUnnamedFieldDecl.find(Field);
278 if (Pos == InstantiatedFromUnnamedFieldDecl.end())
279 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000280
Anders Carlsson5da84842009-09-01 04:26:58 +0000281 return Pos->second;
282}
283
284void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
285 FieldDecl *Tmpl) {
286 assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
287 assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
288 assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
289 "Already noted what unnamed field was instantiated from");
Mike Stump11289f42009-09-09 15:08:12 +0000290
Anders Carlsson5da84842009-09-01 04:26:58 +0000291 InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
292}
293
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000294namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000295 class BeforeInTranslationUnit
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000296 : std::binary_function<SourceRange, SourceRange, bool> {
297 SourceManager *SourceMgr;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000299 public:
300 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
Mike Stump11289f42009-09-09 15:08:12 +0000301
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000302 bool operator()(SourceRange X, SourceRange Y) {
303 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
304 }
305 };
306}
307
308/// \brief Determine whether the given comment is a Doxygen-style comment.
309///
310/// \param Start the start of the comment text.
311///
312/// \param End the end of the comment text.
313///
314/// \param Member whether we want to check whether this is a member comment
315/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
316/// we only return true when we find a non-member comment.
Mike Stump11289f42009-09-09 15:08:12 +0000317static bool
318isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000319 bool Member = false) {
Mike Stump11289f42009-09-09 15:08:12 +0000320 const char *BufferStart
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000321 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
322 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
323 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000325 if (End - Start < 4)
326 return false;
327
328 assert(Start[0] == '/' && "Not a comment?");
329 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
330 return false;
331 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
332 return false;
333
334 return (Start[3] == '<') == Member;
335}
336
337/// \brief Retrieve the comment associated with the given declaration, if
Mike Stump11289f42009-09-09 15:08:12 +0000338/// it has one.
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000339const char *ASTContext::getCommentForDecl(const Decl *D) {
340 if (!D)
341 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000342
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000343 // Check whether we have cached a comment string for this declaration
344 // already.
Mike Stump11289f42009-09-09 15:08:12 +0000345 llvm::DenseMap<const Decl *, std::string>::iterator Pos
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000346 = DeclComments.find(D);
347 if (Pos != DeclComments.end())
348 return Pos->second.c_str();
349
Mike Stump11289f42009-09-09 15:08:12 +0000350 // If we have an external AST source and have not yet loaded comments from
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000351 // that source, do so now.
352 if (ExternalSource && !LoadedExternalComments) {
353 std::vector<SourceRange> LoadedComments;
354 ExternalSource->ReadComments(LoadedComments);
Mike Stump11289f42009-09-09 15:08:12 +0000355
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000356 if (!LoadedComments.empty())
357 Comments.insert(Comments.begin(), LoadedComments.begin(),
358 LoadedComments.end());
Mike Stump11289f42009-09-09 15:08:12 +0000359
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000360 LoadedExternalComments = true;
361 }
Mike Stump11289f42009-09-09 15:08:12 +0000362
363 // If there are no comments anywhere, we won't find anything.
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000364 if (Comments.empty())
365 return 0;
366
367 // If the declaration doesn't map directly to a location in a file, we
368 // can't find the comment.
369 SourceLocation DeclStartLoc = D->getLocStart();
370 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
371 return 0;
372
373 // Find the comment that occurs just before this declaration.
374 std::vector<SourceRange>::iterator LastComment
Mike Stump11289f42009-09-09 15:08:12 +0000375 = std::lower_bound(Comments.begin(), Comments.end(),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000376 SourceRange(DeclStartLoc),
377 BeforeInTranslationUnit(&SourceMgr));
Mike Stump11289f42009-09-09 15:08:12 +0000378
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000379 // Decompose the location for the start of the declaration and find the
380 // beginning of the file buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000381 std::pair<FileID, unsigned> DeclStartDecomp
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000382 = SourceMgr.getDecomposedLoc(DeclStartLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000383 const char *FileBufferStart
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000384 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
Mike Stump11289f42009-09-09 15:08:12 +0000385
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000386 // First check whether we have a comment for a member.
387 if (LastComment != Comments.end() &&
388 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
389 isDoxygenComment(SourceMgr, *LastComment, true)) {
390 std::pair<FileID, unsigned> LastCommentEndDecomp
391 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
392 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
393 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
Mike Stump11289f42009-09-09 15:08:12 +0000394 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000395 LastCommentEndDecomp.second)) {
396 // The Doxygen member comment comes after the declaration starts and
397 // is on the same line and in the same file as the declaration. This
398 // is the comment we want.
399 std::string &Result = DeclComments[D];
Mike Stump11289f42009-09-09 15:08:12 +0000400 Result.append(FileBufferStart +
401 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000402 FileBufferStart + LastCommentEndDecomp.second + 1);
403 return Result.c_str();
404 }
405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000407 if (LastComment == Comments.begin())
408 return 0;
409 --LastComment;
410
411 // Decompose the end of the comment.
412 std::pair<FileID, unsigned> LastCommentEndDecomp
413 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000414
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000415 // If the comment and the declaration aren't in the same file, then they
416 // aren't related.
417 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
418 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000419
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000420 // Check that we actually have a Doxygen comment.
421 if (!isDoxygenComment(SourceMgr, *LastComment))
422 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000423
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000424 // Compute the starting line for the declaration and for the end of the
425 // comment (this is expensive).
Mike Stump11289f42009-09-09 15:08:12 +0000426 unsigned DeclStartLine
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000427 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
428 unsigned CommentEndLine
Mike Stump11289f42009-09-09 15:08:12 +0000429 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000430 LastCommentEndDecomp.second);
Mike Stump11289f42009-09-09 15:08:12 +0000431
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000432 // If the comment does not end on the line prior to the declaration, then
433 // the comment is not associated with the declaration at all.
434 if (CommentEndLine + 1 != DeclStartLine)
435 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000436
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000437 // We have a comment, but there may be more comments on the previous lines.
438 // Keep looking so long as the comments are still Doxygen comments and are
439 // still adjacent.
Mike Stump11289f42009-09-09 15:08:12 +0000440 unsigned ExpectedLine
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000441 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
442 std::vector<SourceRange>::iterator FirstComment = LastComment;
443 while (FirstComment != Comments.begin()) {
444 // Look at the previous comment
445 --FirstComment;
446 std::pair<FileID, unsigned> Decomp
447 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000448
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000449 // If this previous comment is in a different file, we're done.
450 if (Decomp.first != DeclStartDecomp.first) {
451 ++FirstComment;
452 break;
453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000455 // If this comment is not a Doxygen comment, we're done.
456 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
457 ++FirstComment;
458 break;
459 }
Mike Stump11289f42009-09-09 15:08:12 +0000460
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000461 // If the line number is not what we expected, we're done.
462 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
463 if (Line != ExpectedLine) {
464 ++FirstComment;
465 break;
466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000468 // Set the next expected line number.
Mike Stump11289f42009-09-09 15:08:12 +0000469 ExpectedLine
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000470 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
471 }
Mike Stump11289f42009-09-09 15:08:12 +0000472
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000473 // The iterator range [FirstComment, LastComment] contains all of the
474 // BCPL comments that, together, are associated with this declaration.
475 // Form a single comment block string for this declaration that concatenates
476 // all of these comments.
477 std::string &Result = DeclComments[D];
478 while (FirstComment != LastComment) {
479 std::pair<FileID, unsigned> DecompStart
480 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
481 std::pair<FileID, unsigned> DecompEnd
482 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
483 Result.append(FileBufferStart + DecompStart.second,
484 FileBufferStart + DecompEnd.second + 1);
485 ++FirstComment;
486 }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000488 // Append the last comment line.
Mike Stump11289f42009-09-09 15:08:12 +0000489 Result.append(FileBufferStart +
490 SourceMgr.getFileOffset(LastComment->getBegin()),
Douglas Gregorc6d5edd2009-07-02 17:08:52 +0000491 FileBufferStart + LastCommentEndDecomp.second + 1);
492 return Result.c_str();
493}
494
Chris Lattner53cfe802007-07-18 17:52:12 +0000495//===----------------------------------------------------------------------===//
496// Type Sizing and Analysis
497//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000498
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000499/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
500/// scalar floating point type.
501const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
John McCall9dd450b2009-09-21 23:43:11 +0000502 const BuiltinType *BT = T->getAs<BuiltinType>();
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000503 assert(BT && "Not a floating point type!");
504 switch (BT->getKind()) {
505 default: assert(0 && "Not a floating point type!");
506 case BuiltinType::Float: return Target.getFloatFormat();
507 case BuiltinType::Double: return Target.getDoubleFormat();
508 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
509 }
510}
511
Mike Stump8ce0ea12009-09-22 02:43:44 +0000512/// getDeclAlignInBytes - Return a conservative estimate of the alignment of the
Chris Lattner68061312009-01-24 21:53:27 +0000513/// specified decl. Note that bitfields do not have a valid alignment, so
514/// this method will assert on them.
Daniel Dunbar43a5d9e2009-02-17 22:16:19 +0000515unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman19a546c2009-02-22 02:56:25 +0000516 unsigned Align = Target.getCharWidth();
517
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000518 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedman19a546c2009-02-22 02:56:25 +0000519 Align = std::max(Align, AA->getAlignment());
520
Chris Lattner68061312009-01-24 21:53:27 +0000521 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
522 QualType T = VD->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000523 if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000524 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlsson0e6d2b32009-04-10 04:52:36 +0000525 Align = Target.getPointerAlign(AS);
Anders Carlsson9b5038e2009-04-10 04:47:03 +0000526 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
527 // Incomplete or function types default to 1.
Eli Friedman19a546c2009-02-22 02:56:25 +0000528 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
529 T = cast<ArrayType>(T)->getElementType();
530
531 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
532 }
Chris Lattner68061312009-01-24 21:53:27 +0000533 }
Eli Friedman19a546c2009-02-22 02:56:25 +0000534
535 return Align / Target.getCharWidth();
Chris Lattner68061312009-01-24 21:53:27 +0000536}
Chris Lattner9a8d1d92008-06-30 18:32:54 +0000537
Chris Lattner983a8bb2007-07-13 22:13:22 +0000538/// getTypeSize - Return the size of the specified type, in bits. This method
539/// does not work on incomplete types.
John McCall8ccfcb52009-09-24 19:53:00 +0000540///
541/// FIXME: Pointers into different addr spaces could have different sizes and
542/// alignment requirements: getPointerInfo should take an AddrSpace, this
543/// should take a QualType, &c.
Chris Lattner4481b422007-07-14 01:29:45 +0000544std::pair<uint64_t, unsigned>
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000545ASTContext::getTypeInfo(const Type *T) {
Mike Stump5b9a3d52009-02-27 18:32:39 +0000546 uint64_t Width=0;
547 unsigned Align=8;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000548 switch (T->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000549#define TYPE(Class, Base)
550#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref462e62009-04-30 17:32:17 +0000551#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000552#define DEPENDENT_TYPE(Class, Base) case Type::Class:
553#include "clang/AST/TypeNodes.def"
Douglas Gregoref462e62009-04-30 17:32:17 +0000554 assert(false && "Should not see dependent types");
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000555 break;
556
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +0000557 case Type::ObjCProtocolList:
558 assert(false && "Should not see protocol list types");
559 break;
560
Chris Lattner355332d2007-07-13 22:27:08 +0000561 case Type::FunctionNoProto:
562 case Type::FunctionProto:
Douglas Gregoref462e62009-04-30 17:32:17 +0000563 // GCC extension: alignof(function) = 32 bits
564 Width = 0;
565 Align = 32;
566 break;
567
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000568 case Type::IncompleteArray:
Steve Naroff5c131802007-08-30 01:06:46 +0000569 case Type::VariableArray:
Douglas Gregoref462e62009-04-30 17:32:17 +0000570 Width = 0;
571 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
572 break;
573
Steve Naroff5c131802007-08-30 01:06:46 +0000574 case Type::ConstantArray: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000575 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Mike Stump11289f42009-09-09 15:08:12 +0000576
Chris Lattner37e05872008-03-05 18:54:05 +0000577 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000578 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000579 Align = EltInfo.second;
580 break;
Christopher Lambc5fafa22007-12-29 05:10:55 +0000581 }
Nate Begemance4d7fc2008-04-18 23:10:10 +0000582 case Type::ExtVector:
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000583 case Type::Vector: {
Mike Stump11289f42009-09-09 15:08:12 +0000584 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000585 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000586 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman3df5efe2008-05-30 09:31:38 +0000587 Align = Width;
Nate Begemanb699c9b2009-01-18 06:42:49 +0000588 // If the alignment is not a power of 2, round up to the next power of 2.
589 // This happens for non-power-of-2 length vectors.
590 // FIXME: this should probably be a target property.
591 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000592 break;
593 }
Chris Lattner647fb222007-07-18 18:26:58 +0000594
Chris Lattner7570e9c2008-03-08 08:52:55 +0000595 case Type::Builtin:
Chris Lattner983a8bb2007-07-13 22:13:22 +0000596 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000597 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000598 case BuiltinType::Void:
Douglas Gregoref462e62009-04-30 17:32:17 +0000599 // GCC extension: alignof(void) = 8 bits.
600 Width = 0;
601 Align = 8;
602 break;
603
Chris Lattner6a4f7452007-12-19 19:23:28 +0000604 case BuiltinType::Bool:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000605 Width = Target.getBoolWidth();
606 Align = Target.getBoolAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000607 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000608 case BuiltinType::Char_S:
609 case BuiltinType::Char_U:
610 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000611 case BuiltinType::SChar:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000612 Width = Target.getCharWidth();
613 Align = Target.getCharAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000614 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000615 case BuiltinType::WChar:
616 Width = Target.getWCharWidth();
617 Align = Target.getWCharAlign();
618 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000619 case BuiltinType::Char16:
620 Width = Target.getChar16Width();
621 Align = Target.getChar16Align();
622 break;
623 case BuiltinType::Char32:
624 Width = Target.getChar32Width();
625 Align = Target.getChar32Align();
626 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000627 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000628 case BuiltinType::Short:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000629 Width = Target.getShortWidth();
630 Align = Target.getShortAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000631 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000632 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000633 case BuiltinType::Int:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000634 Width = Target.getIntWidth();
635 Align = Target.getIntAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000636 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000637 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000638 case BuiltinType::Long:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000639 Width = Target.getLongWidth();
640 Align = Target.getLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000641 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000642 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000643 case BuiltinType::LongLong:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000644 Width = Target.getLongLongWidth();
645 Align = Target.getLongLongAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000646 break;
Chris Lattner0a415ec2009-04-30 02:55:13 +0000647 case BuiltinType::Int128:
648 case BuiltinType::UInt128:
649 Width = 128;
650 Align = 128; // int128_t is 128-bit aligned on all targets.
651 break;
Chris Lattner6a4f7452007-12-19 19:23:28 +0000652 case BuiltinType::Float:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000653 Width = Target.getFloatWidth();
654 Align = Target.getFloatAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000655 break;
656 case BuiltinType::Double:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000657 Width = Target.getDoubleWidth();
658 Align = Target.getDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000659 break;
660 case BuiltinType::LongDouble:
Chris Lattner7570e9c2008-03-08 08:52:55 +0000661 Width = Target.getLongDoubleWidth();
662 Align = Target.getLongDoubleAlign();
Chris Lattner6a4f7452007-12-19 19:23:28 +0000663 break;
Sebastian Redl576fd422009-05-10 18:38:11 +0000664 case BuiltinType::NullPtr:
665 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
666 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redla81b0b72009-05-27 19:34:06 +0000667 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000668 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000669 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +0000670 case Type::FixedWidthInt:
671 // FIXME: This isn't precisely correct; the width/alignment should depend
672 // on the available types for the target
673 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner641f1ea2009-02-15 21:20:13 +0000674 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedman1efaaea2009-02-13 02:31:07 +0000675 Align = Width;
676 break;
Steve Narofffb4330f2009-06-17 22:40:22 +0000677 case Type::ObjCObjectPointer:
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000678 Width = Target.getPointerWidth(0);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000679 Align = Target.getPointerAlign(0);
Chris Lattner6a4f7452007-12-19 19:23:28 +0000680 break;
Steve Naroff921a45c2008-09-24 15:05:44 +0000681 case Type::BlockPointer: {
682 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
683 Width = Target.getPointerWidth(AS);
684 Align = Target.getPointerAlign(AS);
685 break;
686 }
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000687 case Type::Pointer: {
688 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner4ba0cef2008-04-07 07:01:58 +0000689 Width = Target.getPointerWidth(AS);
Chris Lattner2dca6ff2008-03-08 08:34:58 +0000690 Align = Target.getPointerAlign(AS);
691 break;
692 }
Sebastian Redl0f8b23f2009-03-16 23:22:08 +0000693 case Type::LValueReference:
694 case Type::RValueReference:
Chris Lattner0523dc92007-07-13 22:16:13 +0000695 // "When applied to a reference or a reference type, the result is the size
Chris Lattner647fb222007-07-18 18:26:58 +0000696 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6a4f7452007-12-19 19:23:28 +0000697 // FIXME: This is wrong for struct layout: a reference in a struct has
698 // pointer size.
Chris Lattnerdb5f1fa2008-04-02 17:35:06 +0000699 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000700 case Type::MemberPointer: {
Anders Carlsson32440a02009-05-17 02:06:04 +0000701 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
702 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
703 // If we ever want to support other ABIs this needs to be abstracted.
704
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000705 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000706 std::pair<uint64_t, unsigned> PtrDiffInfo =
Anders Carlsson32440a02009-05-17 02:06:04 +0000707 getTypeInfo(getPointerDiffType());
708 Width = PtrDiffInfo.first;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000709 if (Pointee->isFunctionType())
710 Width *= 2;
Anders Carlsson32440a02009-05-17 02:06:04 +0000711 Align = PtrDiffInfo.second;
712 break;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +0000713 }
Chris Lattner647fb222007-07-18 18:26:58 +0000714 case Type::Complex: {
715 // Complex types have the same alignment as their elements, but twice the
716 // size.
Mike Stump11289f42009-09-09 15:08:12 +0000717 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner37e05872008-03-05 18:54:05 +0000718 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner7570e9c2008-03-08 08:52:55 +0000719 Width = EltInfo.first*2;
Chris Lattner647fb222007-07-18 18:26:58 +0000720 Align = EltInfo.second;
721 break;
722 }
Devang Pateldbb72632008-06-04 21:54:36 +0000723 case Type::ObjCInterface: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000724 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Pateldbb72632008-06-04 21:54:36 +0000725 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
726 Width = Layout.getSize();
727 Align = Layout.getAlignment();
728 break;
729 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000730 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000731 case Type::Enum: {
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000732 const TagType *TT = cast<TagType>(T);
733
734 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner572100b2008-08-09 21:35:13 +0000735 Width = 1;
736 Align = 1;
737 break;
738 }
Mike Stump11289f42009-09-09 15:08:12 +0000739
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000740 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner8b23c252008-04-06 22:05:18 +0000741 return getTypeInfo(ET->getDecl()->getIntegerType());
742
Daniel Dunbarbbc0af72008-11-08 05:48:37 +0000743 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner8b23c252008-04-06 22:05:18 +0000744 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
745 Width = Layout.getSize();
746 Align = Layout.getAlignment();
Chris Lattner49a953a2007-07-23 22:46:22 +0000747 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000748 }
Douglas Gregordc572a32009-03-30 22:58:21 +0000749
John McCallcebee162009-10-18 09:09:24 +0000750 case Type::SubstTemplateTypeParm: {
751 return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
752 getReplacementType().getTypePtr());
753 }
754
John McCallfcc33b02009-09-05 00:15:47 +0000755 case Type::Elaborated: {
756 return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType().getTypePtr());
757 }
758
Douglas Gregoref462e62009-04-30 17:32:17 +0000759 case Type::Typedef: {
760 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000761 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregoref462e62009-04-30 17:32:17 +0000762 Align = Aligned->getAlignment();
763 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
764 } else
765 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordc572a32009-03-30 22:58:21 +0000766 break;
Chris Lattner8b23c252008-04-06 22:05:18 +0000767 }
Douglas Gregoref462e62009-04-30 17:32:17 +0000768
769 case Type::TypeOfExpr:
770 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
771 .getTypePtr());
772
773 case Type::TypeOf:
774 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
775
Anders Carlsson81df7b82009-06-24 19:06:50 +0000776 case Type::Decltype:
777 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
778 .getTypePtr());
779
Douglas Gregoref462e62009-04-30 17:32:17 +0000780 case Type::QualifiedName:
781 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
Mike Stump11289f42009-09-09 15:08:12 +0000782
Douglas Gregoref462e62009-04-30 17:32:17 +0000783 case Type::TemplateSpecialization:
Mike Stump11289f42009-09-09 15:08:12 +0000784 assert(getCanonicalType(T) != T &&
Douglas Gregoref462e62009-04-30 17:32:17 +0000785 "Cannot request the size of a dependent type");
786 // FIXME: this is likely to be wrong once we support template
787 // aliases, since a template alias could refer to a typedef that
788 // has an __aligned__ attribute on it.
789 return getTypeInfo(getCanonicalType(T));
790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791
Chris Lattner53cfe802007-07-18 17:52:12 +0000792 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner7570e9c2008-03-08 08:52:55 +0000793 return std::make_pair(Width, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000794}
795
Chris Lattnera3402cd2009-01-27 18:08:34 +0000796/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
797/// type for the current target in bits. This can be different than the ABI
798/// alignment in cases where it is beneficial for performance to overalign
799/// a data type.
800unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
801 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman7ab09572009-05-25 21:27:19 +0000802
803 // Double and long long should be naturally aligned if possible.
John McCall9dd450b2009-09-21 23:43:11 +0000804 if (const ComplexType* CT = T->getAs<ComplexType>())
Eli Friedman7ab09572009-05-25 21:27:19 +0000805 T = CT->getElementType().getTypePtr();
806 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
807 T->isSpecificBuiltinType(BuiltinType::LongLong))
808 return std::max(ABIAlign, (unsigned)getTypeSize(T));
809
Chris Lattnera3402cd2009-01-27 18:08:34 +0000810 return ABIAlign;
811}
812
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000813static void CollectLocalObjCIvars(ASTContext *Ctx,
814 const ObjCInterfaceDecl *OI,
815 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000816 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
817 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000818 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanianf327e892008-12-17 21:40:49 +0000819 if (!IVDecl->isInvalidDecl())
820 Fields.push_back(cast<FieldDecl>(IVDecl));
821 }
822}
823
Daniel Dunbare4f25b72009-04-22 17:43:55 +0000824void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
825 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
826 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
827 CollectObjCIvars(SuperClass, Fields);
828 CollectLocalObjCIvars(this, OI, Fields);
829}
830
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000831/// ShallowCollectObjCIvars -
832/// Collect all ivars, including those synthesized, in the current class.
833///
834void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
835 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
836 bool CollectSynthesized) {
837 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
838 E = OI->ivar_end(); I != E; ++I) {
839 Ivars.push_back(*I);
840 }
841 if (CollectSynthesized)
842 CollectSynthesizedIvars(OI, Ivars);
843}
844
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000845void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
846 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000847 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
848 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000849 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
850 Ivars.push_back(Ivar);
Mike Stump11289f42009-09-09 15:08:12 +0000851
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000852 // Also look into nested protocols.
853 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
854 E = PD->protocol_end(); P != E; ++P)
855 CollectProtocolSynthesizedIvars(*P, Ivars);
856}
857
858/// CollectSynthesizedIvars -
859/// This routine collect synthesized ivars for the designated class.
860///
861void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
862 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000863 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
864 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian0f44d812009-05-12 18:14:29 +0000865 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
866 Ivars.push_back(Ivar);
867 }
868 // Also look into interface's protocol list for properties declared
869 // in the protocol and whose ivars are synthesized.
870 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
871 PE = OI->protocol_end(); P != PE; ++P) {
872 ObjCProtocolDecl *PD = (*P);
873 CollectProtocolSynthesizedIvars(PD, Ivars);
874 }
875}
876
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000877unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
878 unsigned count = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000879 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
880 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000881 if ((*I)->getPropertyIvarDecl())
882 ++count;
883
884 // Also look into nested protocols.
885 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
886 E = PD->protocol_end(); P != E; ++P)
887 count += CountProtocolSynthesizedIvars(*P);
888 return count;
889}
890
Mike Stump11289f42009-09-09 15:08:12 +0000891unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000892 unsigned count = 0;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000893 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
894 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000895 if ((*I)->getPropertyIvarDecl())
896 ++count;
897 }
898 // Also look into interface's protocol list for properties declared
899 // in the protocol and whose ivars are synthesized.
900 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
901 PE = OI->protocol_end(); P != PE; ++P) {
902 ObjCProtocolDecl *PD = (*P);
903 count += CountProtocolSynthesizedIvars(PD);
904 }
905 return count;
906}
907
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000908/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
909ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
910 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
911 I = ObjCImpls.find(D);
912 if (I != ObjCImpls.end())
913 return cast<ObjCImplementationDecl>(I->second);
914 return 0;
915}
916/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
917ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
918 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
919 I = ObjCImpls.find(D);
920 if (I != ObjCImpls.end())
921 return cast<ObjCCategoryImplDecl>(I->second);
922 return 0;
923}
924
925/// \brief Set the implementation of ObjCInterfaceDecl.
926void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
927 ObjCImplementationDecl *ImplD) {
928 assert(IFaceD && ImplD && "Passed null params");
929 ObjCImpls[IFaceD] = ImplD;
930}
931/// \brief Set the implementation of ObjCCategoryDecl.
932void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
933 ObjCCategoryImplDecl *ImplD) {
934 assert(CatD && ImplD && "Passed null params");
935 ObjCImpls[CatD] = ImplD;
936}
937
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +0000938/// \brief Allocate an uninitialized DeclaratorInfo.
939///
940/// The caller should initialize the memory held by DeclaratorInfo using
941/// the TypeLoc wrappers.
942///
943/// \param T the type that will be the basis for type source info. This type
944/// should refer to how the declarator was written in source code, not to
945/// what type semantic analysis resolved the declarator to.
946DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T) {
947 unsigned DataSize = TypeLoc::getFullDataSizeForType(T);
948 DeclaratorInfo *DInfo =
949 (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8);
950 new (DInfo) DeclaratorInfo(T);
951 return DInfo;
952}
953
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000954/// getInterfaceLayoutImpl - Get or compute information about the
955/// layout of the given interface.
956///
957/// \param Impl - If given, also include the layout of the interface's
958/// implementation. This may differ by including synthesized ivars.
Devang Pateldbb72632008-06-04 21:54:36 +0000959const ASTRecordLayout &
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000960ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
961 const ObjCImplementationDecl *Impl) {
Daniel Dunbar80b4eef2009-05-03 13:15:50 +0000962 assert(!D->isForwardDecl() && "Invalid interface decl!");
963
Devang Pateldbb72632008-06-04 21:54:36 +0000964 // Look up this layout, if already laid out, return what we have.
Mike Stump11289f42009-09-09 15:08:12 +0000965 ObjCContainerDecl *Key =
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000966 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
967 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
968 return *Entry;
Devang Pateldbb72632008-06-04 21:54:36 +0000969
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000970 // Add in synthesized ivar count if laying out an implementation.
971 if (Impl) {
Anders Carlssona4267a62009-07-18 21:19:52 +0000972 unsigned FieldCount = D->ivar_size();
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000973 unsigned SynthCount = CountSynthesizedIvars(D);
974 FieldCount += SynthCount;
Daniel Dunbar7bee4152009-05-03 11:41:43 +0000975 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000976 // entry. Note we can't cache this because we simply free all
977 // entries later; however we shouldn't look up implementations
978 // frequently.
Fariborz Jahanian7c809592009-06-04 01:19:09 +0000979 if (SynthCount == 0)
Daniel Dunbar2b65fe32009-05-03 11:16:44 +0000980 return getObjCLayout(D, 0);
981 }
982
Mike Stump11289f42009-09-09 15:08:12 +0000983 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +0000984 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
985 ObjCLayouts[Key] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +0000986
Devang Pateldbb72632008-06-04 21:54:36 +0000987 return *NewEntry;
988}
989
Daniel Dunbar02f7f5f2009-05-03 10:38:35 +0000990const ASTRecordLayout &
991ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
992 return getObjCLayout(D, 0);
993}
994
995const ASTRecordLayout &
996ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
997 return getObjCLayout(D->getClassInterface(), D);
998}
999
Devang Patele11664a2007-11-01 19:11:01 +00001000/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +00001001/// specified record (struct/union/class), which indicates its size and field
1002/// position information.
Chris Lattner37e05872008-03-05 18:54:05 +00001003const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek21475702008-09-05 17:16:31 +00001004 D = D->getDefinition(*this);
1005 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman3df5efe2008-05-30 09:31:38 +00001006
Chris Lattner53cfe802007-07-18 17:52:12 +00001007 // Look up this layout, if already laid out, return what we have.
Eli Friedman27291322009-07-22 20:29:16 +00001008 // Note that we can't save a reference to the entry because this function
1009 // is recursive.
1010 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +00001011 if (Entry) return *Entry;
Eli Friedman3df5efe2008-05-30 09:31:38 +00001012
Mike Stump11289f42009-09-09 15:08:12 +00001013 const ASTRecordLayout *NewEntry =
Anders Carlssona4267a62009-07-18 21:19:52 +00001014 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman27291322009-07-22 20:29:16 +00001015 ASTRecordLayouts[D] = NewEntry;
Mike Stump11289f42009-09-09 15:08:12 +00001016
Chris Lattner647fb222007-07-18 18:26:58 +00001017 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +00001018}
1019
Chris Lattner983a8bb2007-07-13 22:13:22 +00001020//===----------------------------------------------------------------------===//
1021// Type creation/memoization methods
1022//===----------------------------------------------------------------------===//
1023
John McCall8ccfcb52009-09-24 19:53:00 +00001024QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) {
1025 unsigned Fast = Quals.getFastQualifiers();
1026 Quals.removeFastQualifiers();
1027
1028 // Check if we've already instantiated this type.
1029 llvm::FoldingSetNodeID ID;
1030 ExtQuals::Profile(ID, TypeNode, Quals);
1031 void *InsertPos = 0;
1032 if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) {
1033 assert(EQ->getQualifiers() == Quals);
1034 QualType T = QualType(EQ, Fast);
1035 return T;
1036 }
1037
John McCall90d1c2d2009-09-24 23:30:46 +00001038 ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001039 ExtQualNodes.InsertNode(New, InsertPos);
1040 QualType T = QualType(New, Fast);
1041 return T;
1042}
1043
1044QualType ASTContext::getVolatileType(QualType T) {
1045 QualType CanT = getCanonicalType(T);
1046 if (CanT.isVolatileQualified()) return T;
1047
1048 QualifierCollector Quals;
1049 const Type *TypeNode = Quals.strip(T);
1050 Quals.addVolatile();
1051
1052 return getExtQualType(TypeNode, Quals);
1053}
1054
Fariborz Jahanianece85822009-02-17 18:27:45 +00001055QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001056 QualType CanT = getCanonicalType(T);
1057 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner445fcab2008-02-20 20:55:12 +00001058 return T;
Chris Lattnerd60183d2009-02-18 22:53:11 +00001059
John McCall8ccfcb52009-09-24 19:53:00 +00001060 // If we are composing extended qualifiers together, merge together
1061 // into one ExtQuals node.
1062 QualifierCollector Quals;
1063 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001064
John McCall8ccfcb52009-09-24 19:53:00 +00001065 // If this type already has an address space specified, it cannot get
1066 // another one.
1067 assert(!Quals.hasAddressSpace() &&
1068 "Type cannot be in multiple addr spaces!");
1069 Quals.addAddressSpace(AddressSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001070
John McCall8ccfcb52009-09-24 19:53:00 +00001071 return getExtQualType(TypeNode, Quals);
Christopher Lamb025b5fb2008-02-04 02:31:56 +00001072}
1073
Chris Lattnerd60183d2009-02-18 22:53:11 +00001074QualType ASTContext::getObjCGCQualType(QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +00001075 Qualifiers::GC GCAttr) {
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001076 QualType CanT = getCanonicalType(T);
Chris Lattnerd60183d2009-02-18 22:53:11 +00001077 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001078 return T;
Mike Stump11289f42009-09-09 15:08:12 +00001079
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001080 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001081 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
Steve Naroff6b712a72009-07-14 18:25:06 +00001082 if (Pointee->isAnyPointerType()) {
Fariborz Jahanianb68215c2009-06-03 17:15:17 +00001083 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1084 return getPointerType(ResultType);
1085 }
1086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
John McCall8ccfcb52009-09-24 19:53:00 +00001088 // If we are composing extended qualifiers together, merge together
1089 // into one ExtQuals node.
1090 QualifierCollector Quals;
1091 const Type *TypeNode = Quals.strip(T);
Mike Stump11289f42009-09-09 15:08:12 +00001092
John McCall8ccfcb52009-09-24 19:53:00 +00001093 // If this type already has an ObjCGC specified, it cannot get
1094 // another one.
1095 assert(!Quals.hasObjCGCAttr() &&
1096 "Type cannot have multiple ObjCGCs!");
1097 Quals.addObjCGCAttr(GCAttr);
Mike Stump11289f42009-09-09 15:08:12 +00001098
John McCall8ccfcb52009-09-24 19:53:00 +00001099 return getExtQualType(TypeNode, Quals);
Fariborz Jahaniane27e9342009-02-18 05:09:49 +00001100}
Chris Lattner983a8bb2007-07-13 22:13:22 +00001101
Mike Stump8c5d7992009-07-25 21:26:53 +00001102QualType ASTContext::getNoReturnType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00001103 QualType ResultType;
Mike Stump8c5d7992009-07-25 21:26:53 +00001104 if (T->isPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001105 QualType Pointee = T->getAs<PointerType>()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001106 ResultType = getNoReturnType(Pointee);
Mike Stumpea086c72009-07-25 23:24:03 +00001107 ResultType = getPointerType(ResultType);
John McCall8ccfcb52009-09-24 19:53:00 +00001108 } else if (T->isBlockPointerType()) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001109 QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001110 ResultType = getNoReturnType(Pointee);
Mike Stumpea086c72009-07-25 23:24:03 +00001111 ResultType = getBlockPointerType(ResultType);
John McCall8ccfcb52009-09-24 19:53:00 +00001112 } else {
1113 assert (T->isFunctionType()
1114 && "can't noreturn qualify non-pointer to function or block type");
Mike Stump11289f42009-09-09 15:08:12 +00001115
Benjamin Kramer07fec3b2009-09-25 11:47:22 +00001116 if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) {
1117 ResultType = getFunctionNoProtoType(FNPT->getResultType(), true);
John McCall8ccfcb52009-09-24 19:53:00 +00001118 } else {
1119 const FunctionProtoType *F = T->getAs<FunctionProtoType>();
1120 ResultType
1121 = getFunctionType(F->getResultType(), F->arg_type_begin(),
1122 F->getNumArgs(), F->isVariadic(), F->getTypeQuals(),
1123 F->hasExceptionSpec(), F->hasAnyExceptionSpec(),
1124 F->getNumExceptions(), F->exception_begin(), true);
1125 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001126 }
John McCall8ccfcb52009-09-24 19:53:00 +00001127
1128 return getQualifiedType(ResultType, T.getQualifiers());
Mike Stump8c5d7992009-07-25 21:26:53 +00001129}
1130
Chris Lattnerc6395932007-06-22 20:56:16 +00001131/// getComplexType - Return the uniqued reference to the type for a complex
1132/// number with the specified element type.
1133QualType ASTContext::getComplexType(QualType T) {
1134 // Unique pointers, to guarantee there is only one pointer of a particular
1135 // structure.
1136 llvm::FoldingSetNodeID ID;
1137 ComplexType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001138
Chris Lattnerc6395932007-06-22 20:56:16 +00001139 void *InsertPos = 0;
1140 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1141 return QualType(CT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001142
Chris Lattnerc6395932007-06-22 20:56:16 +00001143 // If the pointee type isn't canonical, this won't be a canonical type either,
1144 // so fill in the canonical type field.
1145 QualType Canonical;
1146 if (!T->isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001147 Canonical = getComplexType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001148
Chris Lattnerc6395932007-06-22 20:56:16 +00001149 // Get the new insert position for the node we care about.
1150 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001151 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6395932007-06-22 20:56:16 +00001152 }
John McCall90d1c2d2009-09-24 23:30:46 +00001153 ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
Chris Lattnerc6395932007-06-22 20:56:16 +00001154 Types.push_back(New);
1155 ComplexTypes.InsertNode(New, InsertPos);
1156 return QualType(New, 0);
1157}
1158
Eli Friedman1efaaea2009-02-13 02:31:07 +00001159QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1160 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1161 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1162 FixedWidthIntType *&Entry = Map[Width];
1163 if (!Entry)
1164 Entry = new FixedWidthIntType(Width, Signed);
1165 return QualType(Entry, 0);
1166}
Chris Lattnerc6395932007-06-22 20:56:16 +00001167
Chris Lattner970e54e2006-11-12 00:37:36 +00001168/// getPointerType - Return the uniqued reference to the type for a pointer to
1169/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001170QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +00001171 // Unique pointers, to guarantee there is only one pointer of a particular
1172 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001173 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +00001174 PointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001175
Chris Lattner67521df2007-01-27 01:29:36 +00001176 void *InsertPos = 0;
1177 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001178 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001179
Chris Lattner7ccecb92006-11-12 08:50:50 +00001180 // If the pointee type isn't canonical, this won't be a canonical type either,
1181 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001182 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +00001183 if (!T->isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001184 Canonical = getPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattner67521df2007-01-27 01:29:36 +00001186 // Get the new insert position for the node we care about.
1187 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001188 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner67521df2007-01-27 01:29:36 +00001189 }
John McCall90d1c2d2009-09-24 23:30:46 +00001190 PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
Chris Lattner67521df2007-01-27 01:29:36 +00001191 Types.push_back(New);
1192 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001193 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +00001194}
1195
Mike Stump11289f42009-09-09 15:08:12 +00001196/// getBlockPointerType - Return the uniqued reference to the type for
Steve Naroffec33ed92008-08-27 16:04:49 +00001197/// a pointer to the specified block.
1198QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff0ac012832008-08-28 19:20:44 +00001199 assert(T->isFunctionType() && "block of function types only");
1200 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroffec33ed92008-08-27 16:04:49 +00001201 // structure.
1202 llvm::FoldingSetNodeID ID;
1203 BlockPointerType::Profile(ID, T);
Mike Stump11289f42009-09-09 15:08:12 +00001204
Steve Naroffec33ed92008-08-27 16:04:49 +00001205 void *InsertPos = 0;
1206 if (BlockPointerType *PT =
1207 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1208 return QualType(PT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001209
1210 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroffec33ed92008-08-27 16:04:49 +00001211 // type either so fill in the canonical type field.
1212 QualType Canonical;
1213 if (!T->isCanonical()) {
1214 Canonical = getBlockPointerType(getCanonicalType(T));
Mike Stump11289f42009-09-09 15:08:12 +00001215
Steve Naroffec33ed92008-08-27 16:04:49 +00001216 // Get the new insert position for the node we care about.
1217 BlockPointerType *NewIP =
1218 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001219 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroffec33ed92008-08-27 16:04:49 +00001220 }
John McCall90d1c2d2009-09-24 23:30:46 +00001221 BlockPointerType *New
1222 = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
Steve Naroffec33ed92008-08-27 16:04:49 +00001223 Types.push_back(New);
1224 BlockPointerTypes.InsertNode(New, InsertPos);
1225 return QualType(New, 0);
1226}
1227
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001228/// getLValueReferenceType - Return the uniqued reference to the type for an
1229/// lvalue reference to the specified type.
1230QualType ASTContext::getLValueReferenceType(QualType T) {
Bill Wendling3708c182007-05-27 10:15:43 +00001231 // Unique pointers, to guarantee there is only one pointer of a particular
1232 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001233 llvm::FoldingSetNodeID ID;
Bill Wendling3708c182007-05-27 10:15:43 +00001234 ReferenceType::Profile(ID, T);
1235
1236 void *InsertPos = 0;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001237 if (LValueReferenceType *RT =
1238 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Bill Wendling3708c182007-05-27 10:15:43 +00001239 return QualType(RT, 0);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001240
Bill Wendling3708c182007-05-27 10:15:43 +00001241 // If the referencee type isn't canonical, this won't be a canonical type
1242 // either, so fill in the canonical type field.
1243 QualType Canonical;
1244 if (!T->isCanonical()) {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001245 Canonical = getLValueReferenceType(getCanonicalType(T));
1246
Bill Wendling3708c182007-05-27 10:15:43 +00001247 // Get the new insert position for the node we care about.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001248 LValueReferenceType *NewIP =
1249 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001250 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Bill Wendling3708c182007-05-27 10:15:43 +00001251 }
1252
John McCall90d1c2d2009-09-24 23:30:46 +00001253 LValueReferenceType *New
1254 = new (*this, TypeAlignment) LValueReferenceType(T, Canonical);
Bill Wendling3708c182007-05-27 10:15:43 +00001255 Types.push_back(New);
Sebastian Redl0f8b23f2009-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
John McCall90d1c2d2009-09-24 23:30:46 +00001285 RValueReferenceType *New
1286 = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001287 Types.push_back(New);
1288 RValueReferenceTypes.InsertNode(New, InsertPos);
Bill Wendling3708c182007-05-27 10:15:43 +00001289 return QualType(New, 0);
1290}
1291
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001292/// getMemberPointerType - Return the uniqued reference to the type for a
1293/// member pointer to the specified type, in the specified class.
Mike Stump11289f42009-09-09 15:08:12 +00001294QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001295 // Unique pointers, to guarantee there is only one pointer of a particular
1296 // structure.
1297 llvm::FoldingSetNodeID ID;
1298 MemberPointerType::Profile(ID, T, Cls);
1299
1300 void *InsertPos = 0;
1301 if (MemberPointerType *PT =
1302 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1303 return QualType(PT, 0);
1304
1305 // If the pointee or class type isn't canonical, this won't be a canonical
1306 // type either, so fill in the canonical type field.
1307 QualType Canonical;
1308 if (!T->isCanonical()) {
1309 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1310
1311 // Get the new insert position for the node we care about.
1312 MemberPointerType *NewIP =
1313 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1314 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1315 }
John McCall90d1c2d2009-09-24 23:30:46 +00001316 MemberPointerType *New
1317 = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001318 Types.push_back(New);
1319 MemberPointerTypes.InsertNode(New, InsertPos);
1320 return QualType(New, 0);
1321}
1322
Mike Stump11289f42009-09-09 15:08:12 +00001323/// getConstantArrayType - Return the unique reference to the type for an
Steve Naroff5c131802007-08-30 01:06:46 +00001324/// array of the specified element type.
Mike Stump11289f42009-09-09 15:08:12 +00001325QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattnere2df3f92009-05-13 04:12:56 +00001326 const llvm::APInt &ArySizeIn,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001327 ArrayType::ArraySizeModifier ASM,
1328 unsigned EltTypeQuals) {
Eli Friedmanbe7e42b2009-05-29 20:17:55 +00001329 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1330 "Constant array of VLAs is illegal!");
1331
Chris Lattnere2df3f92009-05-13 04:12:56 +00001332 // Convert the array size into a canonical width matching the pointer size for
1333 // the target.
1334 llvm::APInt ArySize(ArySizeIn);
1335 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
Mike Stump11289f42009-09-09 15:08:12 +00001336
Chris Lattner23b7eb62007-06-15 23:05:46 +00001337 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001338 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Mike Stump11289f42009-09-09 15:08:12 +00001339
Chris Lattner36f8e652007-01-27 08:31:04 +00001340 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001341 if (ConstantArrayType *ATP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001342 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001343 return QualType(ATP, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001344
Chris Lattner7ccecb92006-11-12 08:50:50 +00001345 // If the element type isn't canonical, this won't be a canonical type either,
1346 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001347 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +00001348 if (!EltTy->isCanonical()) {
Mike Stump11289f42009-09-09 15:08:12 +00001349 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001350 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +00001351 // Get the new insert position for the node we care about.
Mike Stump11289f42009-09-09 15:08:12 +00001352 ConstantArrayType *NewIP =
Ted Kremenekfc581a92007-10-31 17:10:13 +00001353 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001354 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner36f8e652007-01-27 08:31:04 +00001355 }
Mike Stump11289f42009-09-09 15:08:12 +00001356
John McCall90d1c2d2009-09-24 23:30:46 +00001357 ConstantArrayType *New = new(*this,TypeAlignment)
1358 ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001359 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +00001360 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001361 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +00001362}
1363
Steve Naroffcadebd02007-08-30 18:14:25 +00001364/// getVariableArrayType - Returns a non-unique reference to the type for a
1365/// variable array of the specified element type.
Douglas Gregor04318252009-07-06 15:59:29 +00001366QualType ASTContext::getVariableArrayType(QualType EltTy,
1367 Expr *NumElts,
Steve Naroff90dfdd52007-08-30 18:10:14 +00001368 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001369 unsigned EltTypeQuals,
1370 SourceRange Brackets) {
Eli Friedmanbd258282008-02-15 18:16:39 +00001371 // Since we don't unique expressions, it isn't possible to unique VLA's
1372 // that have an expression provided for their size.
1373
John McCall90d1c2d2009-09-24 23:30:46 +00001374 VariableArrayType *New = new(*this, TypeAlignment)
1375 VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanbd258282008-02-15 18:16:39 +00001376
1377 VariableArrayTypes.push_back(New);
1378 Types.push_back(New);
1379 return QualType(New, 0);
1380}
1381
Douglas Gregor4619e432008-12-05 23:32:09 +00001382/// getDependentSizedArrayType - Returns a non-unique reference to
1383/// the type for a dependently-sized array of the specified element
Douglas Gregorf3f95522009-07-31 00:23:35 +00001384/// type.
Douglas Gregor04318252009-07-06 15:59:29 +00001385QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1386 Expr *NumElts,
Douglas Gregor4619e432008-12-05 23:32:09 +00001387 ArrayType::ArraySizeModifier ASM,
Douglas Gregor04318252009-07-06 15:59:29 +00001388 unsigned EltTypeQuals,
1389 SourceRange Brackets) {
Mike Stump11289f42009-09-09 15:08:12 +00001390 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
Douglas Gregor4619e432008-12-05 23:32:09 +00001391 "Size must be type- or value-dependent!");
1392
Douglas Gregorf3f95522009-07-31 00:23:35 +00001393 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001394 DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM,
Douglas Gregorf3f95522009-07-31 00:23:35 +00001395 EltTypeQuals, NumElts);
Douglas Gregor4619e432008-12-05 23:32:09 +00001396
Douglas Gregorf3f95522009-07-31 00:23:35 +00001397 void *InsertPos = 0;
1398 DependentSizedArrayType *Canon
1399 = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1400 DependentSizedArrayType *New;
1401 if (Canon) {
1402 // We already have a canonical version of this array type; use it as
1403 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001404 New = new (*this, TypeAlignment)
1405 DependentSizedArrayType(*this, EltTy, QualType(Canon, 0),
1406 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001407 } else {
1408 QualType CanonEltTy = getCanonicalType(EltTy);
1409 if (CanonEltTy == EltTy) {
John McCall90d1c2d2009-09-24 23:30:46 +00001410 New = new (*this, TypeAlignment)
1411 DependentSizedArrayType(*this, EltTy, QualType(),
1412 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001413 DependentSizedArrayTypes.InsertNode(New, InsertPos);
1414 } else {
1415 QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts,
1416 ASM, EltTypeQuals,
1417 SourceRange());
John McCall90d1c2d2009-09-24 23:30:46 +00001418 New = new (*this, TypeAlignment)
1419 DependentSizedArrayType(*this, EltTy, Canon,
1420 NumElts, ASM, EltTypeQuals, Brackets);
Douglas Gregorf3f95522009-07-31 00:23:35 +00001421 }
1422 }
Mike Stump11289f42009-09-09 15:08:12 +00001423
Douglas Gregor4619e432008-12-05 23:32:09 +00001424 Types.push_back(New);
1425 return QualType(New, 0);
1426}
1427
Eli Friedmanbd258282008-02-15 18:16:39 +00001428QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1429 ArrayType::ArraySizeModifier ASM,
1430 unsigned EltTypeQuals) {
1431 llvm::FoldingSetNodeID ID;
Chris Lattner780b46f2009-02-19 17:31:02 +00001432 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001433
1434 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001435 if (IncompleteArrayType *ATP =
Eli Friedmanbd258282008-02-15 18:16:39 +00001436 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1437 return QualType(ATP, 0);
1438
1439 // If the element type isn't canonical, this won't be a canonical type
1440 // either, so fill in the canonical type field.
1441 QualType Canonical;
1442
1443 if (!EltTy->isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001444 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001445 ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001446
1447 // Get the new insert position for the node we care about.
1448 IncompleteArrayType *NewIP =
1449 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001450 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek843ebedd2007-10-29 23:37:31 +00001451 }
Eli Friedmanbd258282008-02-15 18:16:39 +00001452
John McCall90d1c2d2009-09-24 23:30:46 +00001453 IncompleteArrayType *New = new (*this, TypeAlignment)
1454 IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals);
Eli Friedmanbd258282008-02-15 18:16:39 +00001455
1456 IncompleteArrayTypes.InsertNode(New, InsertPos);
1457 Types.push_back(New);
1458 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +00001459}
1460
Steve Naroff91fcddb2007-07-18 18:00:27 +00001461/// getVectorType - Return the unique reference to a vector type of
1462/// the specified element type and size. VectorType must be a built-in type.
1463QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001464 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001465
Chris Lattner76a00cf2008-04-06 22:59:24 +00001466 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +00001467 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001468
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001469 // Check if we've already instantiated a vector of this type.
1470 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001471 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001472 void *InsertPos = 0;
1473 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1474 return QualType(VTP, 0);
1475
1476 // If the element type isn't canonical, this won't be a canonical type either,
1477 // so fill in the canonical type field.
1478 QualType Canonical;
1479 if (!vecType->isCanonical()) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00001480 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001481
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001482 // Get the new insert position for the node we care about.
1483 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001484 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001485 }
John McCall90d1c2d2009-09-24 23:30:46 +00001486 VectorType *New = new (*this, TypeAlignment)
1487 VectorType(vecType, NumElts, Canonical);
Steve Naroff4ae0ac62007-07-06 23:09:18 +00001488 VectorTypes.InsertNode(New, InsertPos);
1489 Types.push_back(New);
1490 return QualType(New, 0);
1491}
1492
Nate Begemance4d7fc2008-04-18 23:10:10 +00001493/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff91fcddb2007-07-18 18:00:27 +00001494/// the specified element type and size. VectorType must be a built-in type.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001495QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff91fcddb2007-07-18 18:00:27 +00001496 BuiltinType *baseType;
Mike Stump11289f42009-09-09 15:08:12 +00001497
Chris Lattner76a00cf2008-04-06 22:59:24 +00001498 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemance4d7fc2008-04-18 23:10:10 +00001499 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Mike Stump11289f42009-09-09 15:08:12 +00001500
Steve Naroff91fcddb2007-07-18 18:00:27 +00001501 // Check if we've already instantiated a vector of this type.
1502 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001503 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001504 void *InsertPos = 0;
1505 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1506 return QualType(VTP, 0);
1507
1508 // If the element type isn't canonical, this won't be a canonical type either,
1509 // so fill in the canonical type field.
1510 QualType Canonical;
1511 if (!vecType->isCanonical()) {
Nate Begemance4d7fc2008-04-18 23:10:10 +00001512 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Mike Stump11289f42009-09-09 15:08:12 +00001513
Steve Naroff91fcddb2007-07-18 18:00:27 +00001514 // Get the new insert position for the node we care about.
1515 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001516 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff91fcddb2007-07-18 18:00:27 +00001517 }
John McCall90d1c2d2009-09-24 23:30:46 +00001518 ExtVectorType *New = new (*this, TypeAlignment)
1519 ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff91fcddb2007-07-18 18:00:27 +00001520 VectorTypes.InsertNode(New, InsertPos);
1521 Types.push_back(New);
1522 return QualType(New, 0);
1523}
1524
Mike Stump11289f42009-09-09 15:08:12 +00001525QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
Douglas Gregor758a8692009-06-17 21:51:59 +00001526 Expr *SizeExpr,
1527 SourceLocation AttrLoc) {
Douglas Gregor352169a2009-07-31 03:54:25 +00001528 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001529 DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
Douglas Gregor352169a2009-07-31 03:54:25 +00001530 SizeExpr);
Mike Stump11289f42009-09-09 15:08:12 +00001531
Douglas Gregor352169a2009-07-31 03:54:25 +00001532 void *InsertPos = 0;
1533 DependentSizedExtVectorType *Canon
1534 = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1535 DependentSizedExtVectorType *New;
1536 if (Canon) {
1537 // We already have a canonical version of this array type; use it as
1538 // the canonical type for a newly-built type.
John McCall90d1c2d2009-09-24 23:30:46 +00001539 New = new (*this, TypeAlignment)
1540 DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
1541 SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001542 } else {
1543 QualType CanonVecTy = getCanonicalType(vecType);
1544 if (CanonVecTy == vecType) {
John McCall90d1c2d2009-09-24 23:30:46 +00001545 New = new (*this, TypeAlignment)
1546 DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
1547 AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001548 DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
1549 } else {
1550 QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
1551 SourceLocation());
John McCall90d1c2d2009-09-24 23:30:46 +00001552 New = new (*this, TypeAlignment)
1553 DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
Douglas Gregor352169a2009-07-31 03:54:25 +00001554 }
1555 }
Mike Stump11289f42009-09-09 15:08:12 +00001556
Douglas Gregor758a8692009-06-17 21:51:59 +00001557 Types.push_back(New);
1558 return QualType(New, 0);
1559}
1560
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001561/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001562///
Mike Stump8c5d7992009-07-25 21:26:53 +00001563QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001564 // Unique functions, to guarantee there is only one function of a particular
1565 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001566 llvm::FoldingSetNodeID ID;
Mike Stump8c5d7992009-07-25 21:26:53 +00001567 FunctionNoProtoType::Profile(ID, ResultTy, NoReturn);
Mike Stump11289f42009-09-09 15:08:12 +00001568
Chris Lattner47955de2007-01-27 08:37:20 +00001569 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001570 if (FunctionNoProtoType *FT =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001571 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001572 return QualType(FT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001573
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001574 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +00001575 if (!ResultTy->isCanonical()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001576 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn);
Mike Stump11289f42009-09-09 15:08:12 +00001577
Chris Lattner47955de2007-01-27 08:37:20 +00001578 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001579 FunctionNoProtoType *NewIP =
1580 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001581 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner47955de2007-01-27 08:37:20 +00001582 }
Mike Stump11289f42009-09-09 15:08:12 +00001583
John McCall90d1c2d2009-09-24 23:30:46 +00001584 FunctionNoProtoType *New = new (*this, TypeAlignment)
1585 FunctionNoProtoType(ResultTy, Canonical, NoReturn);
Chris Lattner47955de2007-01-27 08:37:20 +00001586 Types.push_back(New);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001587 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001588 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001589}
1590
1591/// getFunctionType - Return a normal function type with a typed argument
1592/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner465fa322008-10-05 17:34:18 +00001593QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001594 unsigned NumArgs, bool isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001595 unsigned TypeQuals, bool hasExceptionSpec,
1596 bool hasAnyExceptionSpec, unsigned NumExs,
Mike Stump8c5d7992009-07-25 21:26:53 +00001597 const QualType *ExArray, bool NoReturn) {
Anders Carlssone7e163c2009-09-16 23:47:08 +00001598 if (LangOpts.CPlusPlus) {
1599 for (unsigned i = 0; i != NumArgs; ++i)
John McCall8ccfcb52009-09-24 19:53:00 +00001600 assert(!ArgArray[i].hasQualifiers() &&
1601 "C++ arguments can't have toplevel qualifiers!");
Anders Carlssone7e163c2009-09-16 23:47:08 +00001602 }
1603
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001604 // Unique functions, to guarantee there is only one function of a particular
1605 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001606 llvm::FoldingSetNodeID ID;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001607 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001608 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump8c5d7992009-07-25 21:26:53 +00001609 NumExs, ExArray, NoReturn);
Chris Lattnerfd4de792007-01-27 01:15:32 +00001610
1611 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001612 if (FunctionProtoType *FTP =
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001613 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001614 return QualType(FTP, 0);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001615
1616 // Determine whether the type being created is already canonical or not.
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001617 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001618 if (hasExceptionSpec)
1619 isCanonical = false;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001620 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1621 if (!ArgArray[i]->isCanonical())
1622 isCanonical = false;
1623
1624 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001625 // The exception spec is not part of the canonical type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001626 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001627 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +00001628 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001629 CanonicalArgs.reserve(NumArgs);
1630 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner76a00cf2008-04-06 22:59:24 +00001631 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001632
Chris Lattner76a00cf2008-04-06 22:59:24 +00001633 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad7d0479f2009-05-21 09:52:38 +00001634 CanonicalArgs.data(), NumArgs,
Douglas Gregorf9bd4ec2009-08-05 19:03:35 +00001635 isVariadic, TypeQuals, false,
1636 false, 0, 0, NoReturn);
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001637
Chris Lattnerfd4de792007-01-27 01:15:32 +00001638 // Get the new insert position for the node we care about.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001639 FunctionProtoType *NewIP =
1640 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnere05f5342008-10-12 00:26:57 +00001641 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001642 }
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001643
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001644 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001645 // for two variable size arrays (for parameter and exception types) at the
1646 // end of them.
Mike Stump11289f42009-09-09 15:08:12 +00001647 FunctionProtoType *FTP =
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001648 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1649 NumArgs*sizeof(QualType) +
John McCall90d1c2d2009-09-24 23:30:46 +00001650 NumExs*sizeof(QualType), TypeAlignment);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001651 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00001652 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
Mike Stump8c5d7992009-07-25 21:26:53 +00001653 ExArray, NumExs, Canonical, NoReturn);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001654 Types.push_back(FTP);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001655 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001656 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +00001657}
Chris Lattneref51c202006-11-10 07:17:23 +00001658
Douglas Gregor83a586e2008-04-13 21:07:44 +00001659/// getTypeDeclType - Return the unique reference to the type for the
1660/// specified type declaration.
Ted Kremenek21475702008-09-05 17:16:31 +00001661QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001662 assert(Decl && "Passed null for Decl param");
Douglas Gregor83a586e2008-04-13 21:07:44 +00001663 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001664
Argyrios Kyrtzidis89656d22008-10-16 16:50:47 +00001665 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001666 return getTypedefType(Typedef);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001667 else if (isa<TemplateTypeParmDecl>(Decl)) {
1668 assert(false && "Template type parameter types are always available.");
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001669 } else if (ObjCInterfaceDecl *ObjCInterface
1670 = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor83a586e2008-04-13 21:07:44 +00001671 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001672
Douglas Gregor89ee6822009-02-28 01:32:25 +00001673 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek721e2392009-01-19 21:31:22 +00001674 if (PrevDecl)
1675 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff096bab72009-01-27 22:08:43 +00001676 else
John McCall90d1c2d2009-09-24 23:30:46 +00001677 Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001678 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
Ted Kremenek721e2392009-01-19 21:31:22 +00001679 if (PrevDecl)
1680 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff096bab72009-01-27 22:08:43 +00001681 else
John McCall90d1c2d2009-09-24 23:30:46 +00001682 Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum);
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00001683 } else
Douglas Gregor83a586e2008-04-13 21:07:44 +00001684 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001685
Ted Kremenek21475702008-09-05 17:16:31 +00001686 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidisfaf08762008-08-07 20:55:28 +00001687 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor83a586e2008-04-13 21:07:44 +00001688}
1689
Chris Lattner32d920b2007-01-26 02:01:53 +00001690/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +00001691/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001692QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1693 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001694
Chris Lattner76a00cf2008-04-06 22:59:24 +00001695 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
John McCall90d1c2d2009-09-24 23:30:46 +00001696 Decl->TypeForDecl = new(*this, TypeAlignment)
1697 TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +00001698 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +00001699 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +00001700}
1701
John McCallcebee162009-10-18 09:09:24 +00001702/// \brief Retrieve a substitution-result type.
1703QualType
1704ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
1705 QualType Replacement) {
1706 assert(Replacement->isCanonical()
1707 && "replacement types must always be canonical");
1708
1709 llvm::FoldingSetNodeID ID;
1710 SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
1711 void *InsertPos = 0;
1712 SubstTemplateTypeParmType *SubstParm
1713 = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1714
1715 if (!SubstParm) {
1716 SubstParm = new (*this, TypeAlignment)
1717 SubstTemplateTypeParmType(Parm, Replacement);
1718 Types.push_back(SubstParm);
1719 SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
1720 }
1721
1722 return QualType(SubstParm, 0);
1723}
1724
Douglas Gregoreff93e02009-02-05 23:33:38 +00001725/// \brief Retrieve the template type parameter type for a template
Mike Stump11289f42009-09-09 15:08:12 +00001726/// parameter or parameter pack with the given depth, index, and (optionally)
Anders Carlsson90036dc2009-06-16 00:30:48 +00001727/// name.
Mike Stump11289f42009-09-09 15:08:12 +00001728QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson90036dc2009-06-16 00:30:48 +00001729 bool ParameterPack,
Douglas Gregoreff93e02009-02-05 23:33:38 +00001730 IdentifierInfo *Name) {
1731 llvm::FoldingSetNodeID ID;
Anders Carlsson90036dc2009-06-16 00:30:48 +00001732 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001733 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001734 TemplateTypeParmType *TypeParm
Douglas Gregoreff93e02009-02-05 23:33:38 +00001735 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1736
1737 if (TypeParm)
1738 return QualType(TypeParm, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001739
Anders Carlsson90036dc2009-06-16 00:30:48 +00001740 if (Name) {
1741 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
John McCall90d1c2d2009-09-24 23:30:46 +00001742 TypeParm = new (*this, TypeAlignment)
1743 TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon);
Anders Carlsson90036dc2009-06-16 00:30:48 +00001744 } else
John McCall90d1c2d2009-09-24 23:30:46 +00001745 TypeParm = new (*this, TypeAlignment)
1746 TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregoreff93e02009-02-05 23:33:38 +00001747
1748 Types.push_back(TypeParm);
1749 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1750
1751 return QualType(TypeParm, 0);
1752}
1753
Mike Stump11289f42009-09-09 15:08:12 +00001754QualType
Douglas Gregordc572a32009-03-30 22:58:21 +00001755ASTContext::getTemplateSpecializationType(TemplateName Template,
1756 const TemplateArgument *Args,
1757 unsigned NumArgs,
1758 QualType Canon) {
Douglas Gregor15301382009-07-30 17:40:51 +00001759 if (!Canon.isNull())
1760 Canon = getCanonicalType(Canon);
1761 else {
1762 // Build the canonical template specialization type.
Douglas Gregora8e02e72009-07-28 23:00:59 +00001763 TemplateName CanonTemplate = getCanonicalTemplateName(Template);
1764 llvm::SmallVector<TemplateArgument, 4> CanonArgs;
1765 CanonArgs.reserve(NumArgs);
1766 for (unsigned I = 0; I != NumArgs; ++I)
1767 CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
1768
1769 // Determine whether this canonical template specialization type already
1770 // exists.
1771 llvm::FoldingSetNodeID ID;
Mike Stump11289f42009-09-09 15:08:12 +00001772 TemplateSpecializationType::Profile(ID, CanonTemplate,
Douglas Gregor00044172009-07-29 16:09:57 +00001773 CanonArgs.data(), NumArgs, *this);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001774
1775 void *InsertPos = 0;
1776 TemplateSpecializationType *Spec
1777 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001778
Douglas Gregora8e02e72009-07-28 23:00:59 +00001779 if (!Spec) {
1780 // Allocate a new canonical template specialization type.
Mike Stump11289f42009-09-09 15:08:12 +00001781 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregora8e02e72009-07-28 23:00:59 +00001782 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001783 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001784 Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate,
Douglas Gregora8e02e72009-07-28 23:00:59 +00001785 CanonArgs.data(), NumArgs,
Douglas Gregor15301382009-07-30 17:40:51 +00001786 Canon);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001787 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001788 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregora8e02e72009-07-28 23:00:59 +00001789 }
Mike Stump11289f42009-09-09 15:08:12 +00001790
Douglas Gregor15301382009-07-30 17:40:51 +00001791 if (Canon.isNull())
1792 Canon = QualType(Spec, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001793 assert(Canon->isDependentType() &&
Douglas Gregora8e02e72009-07-28 23:00:59 +00001794 "Non-dependent template-id type must have a canonical type");
Douglas Gregor15301382009-07-30 17:40:51 +00001795 }
Douglas Gregord56a91e2009-02-26 22:19:44 +00001796
Douglas Gregora8e02e72009-07-28 23:00:59 +00001797 // Allocate the (non-canonical) template specialization type, but don't
1798 // try to unique it: these types typically have location information that
1799 // we don't unique and don't want to lose.
Mike Stump11289f42009-09-09 15:08:12 +00001800 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorc40290e2009-03-09 23:48:35 +00001801 sizeof(TemplateArgument) * NumArgs),
John McCall90d1c2d2009-09-24 23:30:46 +00001802 TypeAlignment);
Mike Stump11289f42009-09-09 15:08:12 +00001803 TemplateSpecializationType *Spec
1804 = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs,
Douglas Gregor00044172009-07-29 16:09:57 +00001805 Canon);
Mike Stump11289f42009-09-09 15:08:12 +00001806
Douglas Gregor8bf42052009-02-09 18:46:07 +00001807 Types.push_back(Spec);
Mike Stump11289f42009-09-09 15:08:12 +00001808 return QualType(Spec, 0);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001809}
1810
Mike Stump11289f42009-09-09 15:08:12 +00001811QualType
Douglas Gregorf21eb492009-03-26 23:50:42 +00001812ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor52537682009-03-19 00:18:19 +00001813 QualType NamedType) {
1814 llvm::FoldingSetNodeID ID;
Douglas Gregorf21eb492009-03-26 23:50:42 +00001815 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor52537682009-03-19 00:18:19 +00001816
1817 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001818 QualifiedNameType *T
Douglas Gregor52537682009-03-19 00:18:19 +00001819 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1820 if (T)
1821 return QualType(T, 0);
1822
Mike Stump11289f42009-09-09 15:08:12 +00001823 T = new (*this) QualifiedNameType(NNS, NamedType,
Douglas Gregorf21eb492009-03-26 23:50:42 +00001824 getCanonicalType(NamedType));
Douglas Gregor52537682009-03-19 00:18:19 +00001825 Types.push_back(T);
1826 QualifiedNameTypes.InsertNode(T, InsertPos);
1827 return QualType(T, 0);
1828}
1829
Mike Stump11289f42009-09-09 15:08:12 +00001830QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregor333489b2009-03-27 23:10:48 +00001831 const IdentifierInfo *Name,
1832 QualType Canon) {
1833 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1834
1835 if (Canon.isNull()) {
1836 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1837 if (CanonNNS != NNS)
1838 Canon = getTypenameType(CanonNNS, Name);
1839 }
1840
1841 llvm::FoldingSetNodeID ID;
1842 TypenameType::Profile(ID, NNS, Name);
1843
1844 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001845 TypenameType *T
Douglas Gregor333489b2009-03-27 23:10:48 +00001846 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1847 if (T)
1848 return QualType(T, 0);
1849
1850 T = new (*this) TypenameType(NNS, Name, Canon);
1851 Types.push_back(T);
1852 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001853 return QualType(T, 0);
Douglas Gregor333489b2009-03-27 23:10:48 +00001854}
1855
Mike Stump11289f42009-09-09 15:08:12 +00001856QualType
1857ASTContext::getTypenameType(NestedNameSpecifier *NNS,
Douglas Gregordce2b622009-04-01 00:28:59 +00001858 const TemplateSpecializationType *TemplateId,
1859 QualType Canon) {
1860 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1861
1862 if (Canon.isNull()) {
1863 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1864 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1865 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1866 const TemplateSpecializationType *CanonTemplateId
John McCall9dd450b2009-09-21 23:43:11 +00001867 = CanonType->getAs<TemplateSpecializationType>();
Douglas Gregordce2b622009-04-01 00:28:59 +00001868 assert(CanonTemplateId &&
1869 "Canonical type must also be a template specialization type");
1870 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1871 }
1872 }
1873
1874 llvm::FoldingSetNodeID ID;
1875 TypenameType::Profile(ID, NNS, TemplateId);
1876
1877 void *InsertPos = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001878 TypenameType *T
Douglas Gregordce2b622009-04-01 00:28:59 +00001879 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1880 if (T)
1881 return QualType(T, 0);
1882
1883 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1884 Types.push_back(T);
1885 TypenameTypes.InsertNode(T, InsertPos);
Mike Stump11289f42009-09-09 15:08:12 +00001886 return QualType(T, 0);
Douglas Gregordce2b622009-04-01 00:28:59 +00001887}
1888
John McCallfcc33b02009-09-05 00:15:47 +00001889QualType
1890ASTContext::getElaboratedType(QualType UnderlyingType,
1891 ElaboratedType::TagKind Tag) {
1892 llvm::FoldingSetNodeID ID;
1893 ElaboratedType::Profile(ID, UnderlyingType, Tag);
Mike Stump11289f42009-09-09 15:08:12 +00001894
John McCallfcc33b02009-09-05 00:15:47 +00001895 void *InsertPos = 0;
1896 ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
1897 if (T)
1898 return QualType(T, 0);
1899
1900 QualType Canon = getCanonicalType(UnderlyingType);
1901
1902 T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon);
1903 Types.push_back(T);
1904 ElaboratedTypes.InsertNode(T, InsertPos);
1905 return QualType(T, 0);
1906}
1907
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001908/// CmpProtocolNames - Comparison predicate for sorting protocols
1909/// alphabetically.
1910static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1911 const ObjCProtocolDecl *RHS) {
Douglas Gregor77324f32008-11-17 14:58:09 +00001912 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001913}
1914
1915static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1916 unsigned &NumProtocols) {
1917 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
Mike Stump11289f42009-09-09 15:08:12 +00001918
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001919 // Sort protocols, keyed by name.
1920 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1921
1922 // Remove duplicates.
1923 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1924 NumProtocols = ProtocolsEnd-Protocols;
1925}
1926
Steve Narofffb4330f2009-06-17 22:40:22 +00001927/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1928/// the given interface decl and the conforming protocol list.
Steve Naroff7cae42b2009-07-10 23:34:53 +00001929QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Mike Stump11289f42009-09-09 15:08:12 +00001930 ObjCProtocolDecl **Protocols,
Steve Narofffb4330f2009-06-17 22:40:22 +00001931 unsigned NumProtocols) {
1932 // Sort the protocol list alphabetically to canonicalize it.
1933 if (NumProtocols)
1934 SortAndUniqueProtocols(Protocols, NumProtocols);
1935
1936 llvm::FoldingSetNodeID ID;
Steve Naroff7cae42b2009-07-10 23:34:53 +00001937 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Narofffb4330f2009-06-17 22:40:22 +00001938
1939 void *InsertPos = 0;
1940 if (ObjCObjectPointerType *QT =
1941 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1942 return QualType(QT, 0);
1943
1944 // No Match;
John McCall90d1c2d2009-09-24 23:30:46 +00001945 ObjCObjectPointerType *QType = new (*this, TypeAlignment)
1946 ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00001947
Steve Narofffb4330f2009-06-17 22:40:22 +00001948 Types.push_back(QType);
1949 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1950 return QualType(QType, 0);
1951}
Chris Lattnere0ea37a2008-04-07 04:56:42 +00001952
Steve Naroffc277ad12009-07-18 15:33:26 +00001953/// getObjCInterfaceType - Return the unique reference to the type for the
1954/// specified ObjC interface decl. The list of protocols is optional.
1955QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001956 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Mike Stump11289f42009-09-09 15:08:12 +00001957 if (NumProtocols)
Steve Naroffc277ad12009-07-18 15:33:26 +00001958 // Sort the protocol list alphabetically to canonicalize it.
1959 SortAndUniqueProtocols(Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00001960
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001961 llvm::FoldingSetNodeID ID;
Steve Naroffc277ad12009-07-18 15:33:26 +00001962 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Mike Stump11289f42009-09-09 15:08:12 +00001963
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001964 void *InsertPos = 0;
Steve Naroffc277ad12009-07-18 15:33:26 +00001965 if (ObjCInterfaceType *QT =
1966 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001967 return QualType(QT, 0);
Mike Stump11289f42009-09-09 15:08:12 +00001968
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001969 // No Match;
John McCall90d1c2d2009-09-24 23:30:46 +00001970 ObjCInterfaceType *QType = new (*this, TypeAlignment)
1971 ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1972 Protocols, NumProtocols);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001973 Types.push_back(QType);
Steve Naroffc277ad12009-07-18 15:33:26 +00001974 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001975 return QualType(QType, 0);
1976}
1977
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00001978QualType ASTContext::getObjCProtocolListType(QualType T,
1979 ObjCProtocolDecl **Protocols,
1980 unsigned NumProtocols) {
1981 llvm::FoldingSetNodeID ID;
1982 ObjCProtocolListType::Profile(ID, T, Protocols, NumProtocols);
1983
1984 void *InsertPos = 0;
1985 if (ObjCProtocolListType *QT =
1986 ObjCProtocolListTypes.FindNodeOrInsertPos(ID, InsertPos))
1987 return QualType(QT, 0);
1988
1989 // No Match;
1990 ObjCProtocolListType *QType = new (*this, TypeAlignment)
1991 ObjCProtocolListType(T, Protocols, NumProtocols);
1992 Types.push_back(QType);
1993 ObjCProtocolListTypes.InsertNode(QType, InsertPos);
1994 return QualType(QType, 0);
1995}
1996
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001997/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1998/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroffa773cd52007-08-01 18:02:17 +00001999/// multiple declarations that refer to "typeof(x)" all contain different
Mike Stump11289f42009-09-09 15:08:12 +00002000/// DeclRefExpr's. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002001/// on canonical type's (which are always unique).
Douglas Gregordeaad8c2009-02-26 23:50:07 +00002002QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002003 TypeOfExprType *toe;
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002004 if (tofExpr->isTypeDependent()) {
2005 llvm::FoldingSetNodeID ID;
2006 DependentTypeOfExprType::Profile(ID, *this, tofExpr);
Mike Stump11289f42009-09-09 15:08:12 +00002007
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002008 void *InsertPos = 0;
2009 DependentTypeOfExprType *Canon
2010 = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2011 if (Canon) {
2012 // We already have a "canonical" version of an identical, dependent
2013 // typeof(expr) type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002014 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002015 QualType((TypeOfExprType*)Canon, 0));
2016 }
2017 else {
2018 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002019 Canon
2020 = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
Douglas Gregora5dd9f82009-07-30 23:18:24 +00002021 DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2022 toe = Canon;
2023 }
2024 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002025 QualType Canonical = getCanonicalType(tofExpr->getType());
John McCall90d1c2d2009-09-24 23:30:46 +00002026 toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
Douglas Gregorabd68132009-07-08 00:03:05 +00002027 }
Steve Naroffa773cd52007-08-01 18:02:17 +00002028 Types.push_back(toe);
2029 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002030}
2031
Steve Naroffa773cd52007-08-01 18:02:17 +00002032/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
2033/// TypeOfType AST's. The only motivation to unique these nodes would be
2034/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002035/// an issue. This doesn't effect the type checker, since it operates
Steve Naroffa773cd52007-08-01 18:02:17 +00002036/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +00002037QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002038 QualType Canonical = getCanonicalType(tofType);
John McCall90d1c2d2009-09-24 23:30:46 +00002039 TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
Steve Naroffa773cd52007-08-01 18:02:17 +00002040 Types.push_back(tot);
2041 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +00002042}
2043
Anders Carlssonad6bd352009-06-24 21:24:56 +00002044/// getDecltypeForExpr - Given an expr, will return the decltype for that
2045/// expression, according to the rules in C++0x [dcl.type.simple]p4
2046static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson7d209572009-06-25 15:00:34 +00002047 if (e->isTypeDependent())
2048 return Context.DependentTy;
Mike Stump11289f42009-09-09 15:08:12 +00002049
Anders Carlssonad6bd352009-06-24 21:24:56 +00002050 // If e is an id expression or a class member access, decltype(e) is defined
2051 // as the type of the entity named by e.
2052 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
2053 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
2054 return VD->getType();
2055 }
2056 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
2057 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
2058 return FD->getType();
2059 }
2060 // If e is a function call or an invocation of an overloaded operator,
2061 // (parentheses around e are ignored), decltype(e) is defined as the
2062 // return type of that function.
2063 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
2064 return CE->getCallReturnType();
Mike Stump11289f42009-09-09 15:08:12 +00002065
Anders Carlssonad6bd352009-06-24 21:24:56 +00002066 QualType T = e->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002067
2068 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
Anders Carlssonad6bd352009-06-24 21:24:56 +00002069 // defined as T&, otherwise decltype(e) is defined as T.
2070 if (e->isLvalue(Context) == Expr::LV_Valid)
2071 T = Context.getLValueReferenceType(T);
Mike Stump11289f42009-09-09 15:08:12 +00002072
Anders Carlssonad6bd352009-06-24 21:24:56 +00002073 return T;
2074}
2075
Anders Carlsson81df7b82009-06-24 19:06:50 +00002076/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
2077/// DecltypeType AST's. The only motivation to unique these nodes would be
2078/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
Mike Stump11289f42009-09-09 15:08:12 +00002079/// an issue. This doesn't effect the type checker, since it operates
Anders Carlsson81df7b82009-06-24 19:06:50 +00002080/// on canonical type's (which are always unique).
2081QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregorabd68132009-07-08 00:03:05 +00002082 DecltypeType *dt;
Douglas Gregora21f6c32009-07-30 23:36:40 +00002083 if (e->isTypeDependent()) {
2084 llvm::FoldingSetNodeID ID;
2085 DependentDecltypeType::Profile(ID, *this, e);
Mike Stump11289f42009-09-09 15:08:12 +00002086
Douglas Gregora21f6c32009-07-30 23:36:40 +00002087 void *InsertPos = 0;
2088 DependentDecltypeType *Canon
2089 = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
2090 if (Canon) {
2091 // We already have a "canonical" version of an equivalent, dependent
2092 // decltype type. Use that as our canonical type.
John McCall90d1c2d2009-09-24 23:30:46 +00002093 dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
Douglas Gregora21f6c32009-07-30 23:36:40 +00002094 QualType((DecltypeType*)Canon, 0));
2095 }
2096 else {
2097 // Build a new, canonical typeof(expr) type.
John McCall90d1c2d2009-09-24 23:30:46 +00002098 Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
Douglas Gregora21f6c32009-07-30 23:36:40 +00002099 DependentDecltypeTypes.InsertNode(Canon, InsertPos);
2100 dt = Canon;
2101 }
2102 } else {
Douglas Gregorabd68132009-07-08 00:03:05 +00002103 QualType T = getDecltypeForExpr(e, *this);
John McCall90d1c2d2009-09-24 23:30:46 +00002104 dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregorabd68132009-07-08 00:03:05 +00002105 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00002106 Types.push_back(dt);
2107 return QualType(dt, 0);
2108}
2109
Chris Lattnerfb072462007-01-23 05:45:31 +00002110/// getTagDeclType - Return the unique reference to the type for the
2111/// specified TagDecl (struct/union/class/enum) decl.
Mike Stumpb93185d2009-08-07 18:05:12 +00002112QualType ASTContext::getTagDeclType(const TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +00002113 assert (Decl);
Mike Stumpb93185d2009-08-07 18:05:12 +00002114 // FIXME: What is the design on getTagDeclType when it requires casting
2115 // away const? mutable?
2116 return getTypeDeclType(const_cast<TagDecl*>(Decl));
Chris Lattnerfb072462007-01-23 05:45:31 +00002117}
2118
Mike Stump11289f42009-09-09 15:08:12 +00002119/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
2120/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
2121/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +00002122QualType ASTContext::getSizeType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002123 return getFromTargetType(Target.getSizeType());
Steve Naroff92e30f82007-04-02 22:35:25 +00002124}
Chris Lattnerfb072462007-01-23 05:45:31 +00002125
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002126/// getSignedWCharType - Return the type of "signed wchar_t".
2127/// Used when in C++, as a GCC extension.
2128QualType ASTContext::getSignedWCharType() const {
2129 // FIXME: derive from "Target" ?
2130 return WCharTy;
2131}
2132
2133/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2134/// Used when in C++, as a GCC extension.
2135QualType ASTContext::getUnsignedWCharType() const {
2136 // FIXME: derive from "Target" ?
2137 return UnsignedIntTy;
2138}
2139
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002140/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2141/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2142QualType ASTContext::getPointerDiffType() const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00002143 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattnerd2b88ab2007-07-13 03:05:23 +00002144}
2145
Chris Lattnera21ad802008-04-02 05:18:44 +00002146//===----------------------------------------------------------------------===//
2147// Type Operators
2148//===----------------------------------------------------------------------===//
2149
Chris Lattnered0d0792008-04-06 22:41:35 +00002150/// getCanonicalType - Return the canonical (structural) type corresponding to
2151/// the specified potentially non-canonical type. The non-canonical version
2152/// of a type may have many "decorated" versions of types. Decorators can
2153/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2154/// to be free of any of these, allowing two canonical types to be compared
2155/// for exact equality with a simple pointer comparison.
Douglas Gregor2211d342009-08-05 05:36:45 +00002156CanQualType ASTContext::getCanonicalType(QualType T) {
John McCall8ccfcb52009-09-24 19:53:00 +00002157 QualifierCollector Quals;
2158 const Type *Ptr = Quals.strip(T);
2159 QualType CanType = Ptr->getCanonicalTypeInternal();
Mike Stump11289f42009-09-09 15:08:12 +00002160
John McCall8ccfcb52009-09-24 19:53:00 +00002161 // The canonical internal type will be the canonical type *except*
2162 // that we push type qualifiers down through array types.
2163
2164 // If there are no new qualifiers to push down, stop here.
2165 if (!Quals.hasQualifiers())
Douglas Gregor2211d342009-08-05 05:36:45 +00002166 return CanQualType::CreateUnsafe(CanType);
Chris Lattner7adf0762008-08-04 07:31:14 +00002167
John McCall8ccfcb52009-09-24 19:53:00 +00002168 // If the type qualifiers are on an array type, get the canonical
2169 // type of the array with the qualifiers applied to the element
2170 // type.
Chris Lattner7adf0762008-08-04 07:31:14 +00002171 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2172 if (!AT)
John McCall8ccfcb52009-09-24 19:53:00 +00002173 return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals));
Mike Stump11289f42009-09-09 15:08:12 +00002174
Chris Lattner7adf0762008-08-04 07:31:14 +00002175 // Get the canonical version of the element with the extra qualifiers on it.
2176 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002177 QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals);
Chris Lattner7adf0762008-08-04 07:31:14 +00002178 NewEltTy = getCanonicalType(NewEltTy);
Mike Stump11289f42009-09-09 15:08:12 +00002179
Chris Lattner7adf0762008-08-04 07:31:14 +00002180 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002181 return CanQualType::CreateUnsafe(
2182 getConstantArrayType(NewEltTy, CAT->getSize(),
2183 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002184 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002185 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002186 return CanQualType::CreateUnsafe(
2187 getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002188 IAT->getIndexTypeCVRQualifiers()));
Mike Stump11289f42009-09-09 15:08:12 +00002189
Douglas Gregor4619e432008-12-05 23:32:09 +00002190 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor2211d342009-08-05 05:36:45 +00002191 return CanQualType::CreateUnsafe(
2192 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002193 DSAT->getSizeExpr() ?
2194 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002195 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002196 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002197 DSAT->getBracketsRange()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002198
Chris Lattner7adf0762008-08-04 07:31:14 +00002199 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor2211d342009-08-05 05:36:45 +00002200 return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002201 VAT->getSizeExpr() ?
2202 VAT->getSizeExpr()->Retain() : 0,
Douglas Gregor2211d342009-08-05 05:36:45 +00002203 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002204 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor2211d342009-08-05 05:36:45 +00002205 VAT->getBracketsRange()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002206}
2207
Douglas Gregor6bc50582009-05-07 06:41:52 +00002208TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2209 // If this template name refers to a template, the canonical
2210 // template name merely stores the template itself.
2211 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002212 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor6bc50582009-05-07 06:41:52 +00002213
Mike Stump11289f42009-09-09 15:08:12 +00002214 // If this template name refers to a set of overloaded function templates,
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00002215 /// the canonical template name merely stores the set of function templates.
Douglas Gregor802a0302009-07-31 05:24:01 +00002216 if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) {
2217 OverloadedFunctionDecl *CanonOvl = 0;
2218 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
2219 FEnd = Ovl->function_end();
2220 F != FEnd; ++F) {
2221 Decl *Canon = F->get()->getCanonicalDecl();
2222 if (CanonOvl || Canon != F->get()) {
2223 if (!CanonOvl)
Mike Stump11289f42009-09-09 15:08:12 +00002224 CanonOvl = OverloadedFunctionDecl::Create(*this,
2225 Ovl->getDeclContext(),
Douglas Gregor802a0302009-07-31 05:24:01 +00002226 Ovl->getDeclName());
Mike Stump11289f42009-09-09 15:08:12 +00002227
Douglas Gregor802a0302009-07-31 05:24:01 +00002228 CanonOvl->addOverload(
2229 AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon)));
2230 }
2231 }
Mike Stump11289f42009-09-09 15:08:12 +00002232
Douglas Gregor802a0302009-07-31 05:24:01 +00002233 return TemplateName(CanonOvl? CanonOvl : Ovl);
2234 }
Mike Stump11289f42009-09-09 15:08:12 +00002235
Douglas Gregor6bc50582009-05-07 06:41:52 +00002236 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2237 assert(DTN && "Non-dependent template names must refer to template decls.");
2238 return DTN->CanonicalTemplateName;
2239}
2240
Mike Stump11289f42009-09-09 15:08:12 +00002241TemplateArgument
Douglas Gregora8e02e72009-07-28 23:00:59 +00002242ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) {
2243 switch (Arg.getKind()) {
2244 case TemplateArgument::Null:
2245 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002246
Douglas Gregora8e02e72009-07-28 23:00:59 +00002247 case TemplateArgument::Expression:
2248 // FIXME: Build canonical expression?
2249 return Arg;
Mike Stump11289f42009-09-09 15:08:12 +00002250
Douglas Gregora8e02e72009-07-28 23:00:59 +00002251 case TemplateArgument::Declaration:
2252 return TemplateArgument(SourceLocation(),
2253 Arg.getAsDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +00002254
Douglas Gregora8e02e72009-07-28 23:00:59 +00002255 case TemplateArgument::Integral:
2256 return TemplateArgument(SourceLocation(),
2257 *Arg.getAsIntegral(),
2258 getCanonicalType(Arg.getIntegralType()));
Mike Stump11289f42009-09-09 15:08:12 +00002259
Douglas Gregora8e02e72009-07-28 23:00:59 +00002260 case TemplateArgument::Type:
2261 return TemplateArgument(SourceLocation(),
2262 getCanonicalType(Arg.getAsType()));
Mike Stump11289f42009-09-09 15:08:12 +00002263
Douglas Gregora8e02e72009-07-28 23:00:59 +00002264 case TemplateArgument::Pack: {
2265 // FIXME: Allocate in ASTContext
2266 TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()];
2267 unsigned Idx = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002268 for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
Douglas Gregora8e02e72009-07-28 23:00:59 +00002269 AEnd = Arg.pack_end();
2270 A != AEnd; (void)++A, ++Idx)
2271 CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
Mike Stump11289f42009-09-09 15:08:12 +00002272
Douglas Gregora8e02e72009-07-28 23:00:59 +00002273 TemplateArgument Result;
2274 Result.setArgumentPack(CanonArgs, Arg.pack_size(), false);
2275 return Result;
2276 }
2277 }
2278
2279 // Silence GCC warning
2280 assert(false && "Unhandled template argument kind");
2281 return TemplateArgument();
2282}
2283
Douglas Gregor333489b2009-03-27 23:10:48 +00002284NestedNameSpecifier *
2285ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
Mike Stump11289f42009-09-09 15:08:12 +00002286 if (!NNS)
Douglas Gregor333489b2009-03-27 23:10:48 +00002287 return 0;
2288
2289 switch (NNS->getKind()) {
2290 case NestedNameSpecifier::Identifier:
2291 // Canonicalize the prefix but keep the identifier the same.
Mike Stump11289f42009-09-09 15:08:12 +00002292 return NestedNameSpecifier::Create(*this,
Douglas Gregor333489b2009-03-27 23:10:48 +00002293 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2294 NNS->getAsIdentifier());
2295
2296 case NestedNameSpecifier::Namespace:
2297 // A namespace is canonical; build a nested-name-specifier with
2298 // this namespace and no prefix.
2299 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2300
2301 case NestedNameSpecifier::TypeSpec:
2302 case NestedNameSpecifier::TypeSpecWithTemplate: {
2303 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
Mike Stump11289f42009-09-09 15:08:12 +00002304 return NestedNameSpecifier::Create(*this, 0,
2305 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregor333489b2009-03-27 23:10:48 +00002306 T.getTypePtr());
2307 }
2308
2309 case NestedNameSpecifier::Global:
2310 // The global specifier is canonical and unique.
2311 return NNS;
2312 }
2313
2314 // Required to silence a GCC warning
2315 return 0;
2316}
2317
Chris Lattner7adf0762008-08-04 07:31:14 +00002318
2319const ArrayType *ASTContext::getAsArrayType(QualType T) {
2320 // Handle the non-qualified case efficiently.
John McCall8ccfcb52009-09-24 19:53:00 +00002321 if (!T.hasQualifiers()) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002322 // Handle the common positive case fast.
2323 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2324 return AT;
2325 }
Mike Stump11289f42009-09-09 15:08:12 +00002326
John McCall8ccfcb52009-09-24 19:53:00 +00002327 // Handle the common negative case fast.
Chris Lattner7adf0762008-08-04 07:31:14 +00002328 QualType CType = T->getCanonicalTypeInternal();
John McCall8ccfcb52009-09-24 19:53:00 +00002329 if (!isa<ArrayType>(CType))
Chris Lattner7adf0762008-08-04 07:31:14 +00002330 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002331
John McCall8ccfcb52009-09-24 19:53:00 +00002332 // Apply any qualifiers from the array type to the element type. This
Chris Lattner7adf0762008-08-04 07:31:14 +00002333 // implements C99 6.7.3p8: "If the specification of an array type includes
2334 // any type qualifiers, the element type is so qualified, not the array type."
Mike Stump11289f42009-09-09 15:08:12 +00002335
Chris Lattner7adf0762008-08-04 07:31:14 +00002336 // If we get here, we either have type qualifiers on the type, or we have
2337 // sugar such as a typedef in the way. If we have type qualifiers on the type
Douglas Gregor2211d342009-08-05 05:36:45 +00002338 // we must propagate them down into the element type.
Mike Stump11289f42009-09-09 15:08:12 +00002339
John McCall8ccfcb52009-09-24 19:53:00 +00002340 QualifierCollector Qs;
2341 const Type *Ty = Qs.strip(T.getDesugaredType());
Mike Stump11289f42009-09-09 15:08:12 +00002342
Chris Lattner7adf0762008-08-04 07:31:14 +00002343 // If we have a simple case, just return now.
2344 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
John McCall8ccfcb52009-09-24 19:53:00 +00002345 if (ATy == 0 || Qs.empty())
Chris Lattner7adf0762008-08-04 07:31:14 +00002346 return ATy;
Mike Stump11289f42009-09-09 15:08:12 +00002347
Chris Lattner7adf0762008-08-04 07:31:14 +00002348 // Otherwise, we have an array and we have qualifiers on it. Push the
2349 // qualifiers into the array element type and return a new array type.
2350 // Get the canonical version of the element with the extra qualifiers on it.
2351 // This can recursively sink qualifiers through multiple levels of arrays.
John McCall8ccfcb52009-09-24 19:53:00 +00002352 QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs);
Mike Stump11289f42009-09-09 15:08:12 +00002353
Chris Lattner7adf0762008-08-04 07:31:14 +00002354 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2355 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2356 CAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002357 CAT->getIndexTypeCVRQualifiers()));
Chris Lattner7adf0762008-08-04 07:31:14 +00002358 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2359 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2360 IAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002361 IAT->getIndexTypeCVRQualifiers()));
Douglas Gregor4619e432008-12-05 23:32:09 +00002362
Mike Stump11289f42009-09-09 15:08:12 +00002363 if (const DependentSizedArrayType *DSAT
Douglas Gregor4619e432008-12-05 23:32:09 +00002364 = dyn_cast<DependentSizedArrayType>(ATy))
2365 return cast<ArrayType>(
Mike Stump11289f42009-09-09 15:08:12 +00002366 getDependentSizedArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002367 DSAT->getSizeExpr() ?
2368 DSAT->getSizeExpr()->Retain() : 0,
Douglas Gregor4619e432008-12-05 23:32:09 +00002369 DSAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002370 DSAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002371 DSAT->getBracketsRange()));
Mike Stump11289f42009-09-09 15:08:12 +00002372
Chris Lattner7adf0762008-08-04 07:31:14 +00002373 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor04318252009-07-06 15:59:29 +00002374 return cast<ArrayType>(getVariableArrayType(NewEltTy,
Eli Friedman04fddf02009-08-15 02:50:32 +00002375 VAT->getSizeExpr() ?
John McCall8ccfcb52009-09-24 19:53:00 +00002376 VAT->getSizeExpr()->Retain() : 0,
Chris Lattner7adf0762008-08-04 07:31:14 +00002377 VAT->getSizeModifier(),
John McCall8ccfcb52009-09-24 19:53:00 +00002378 VAT->getIndexTypeCVRQualifiers(),
Douglas Gregor04318252009-07-06 15:59:29 +00002379 VAT->getBracketsRange()));
Chris Lattnered0d0792008-04-06 22:41:35 +00002380}
2381
2382
Chris Lattnera21ad802008-04-02 05:18:44 +00002383/// getArrayDecayedType - Return the properly qualified result of decaying the
2384/// specified array type to a pointer. This operation is non-trivial when
2385/// handling typedefs etc. The canonical type of "T" must be an array type,
2386/// this returns a pointer to a properly qualified element of the array.
2387///
2388/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2389QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattner7adf0762008-08-04 07:31:14 +00002390 // Get the element type with 'getAsArrayType' so that we don't lose any
2391 // typedefs in the element type of the array. This also handles propagation
2392 // of type qualifiers from the array type into the element type if present
2393 // (C99 6.7.3p8).
2394 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2395 assert(PrettyArrayType && "Not an array type!");
Mike Stump11289f42009-09-09 15:08:12 +00002396
Chris Lattner7adf0762008-08-04 07:31:14 +00002397 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnera21ad802008-04-02 05:18:44 +00002398
2399 // int x[restrict 4] -> int *restrict
John McCall8ccfcb52009-09-24 19:53:00 +00002400 return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
Chris Lattnera21ad802008-04-02 05:18:44 +00002401}
2402
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002403QualType ASTContext::getBaseElementType(QualType QT) {
John McCall8ccfcb52009-09-24 19:53:00 +00002404 QualifierCollector Qs;
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002405 while (true) {
John McCall8ccfcb52009-09-24 19:53:00 +00002406 const Type *UT = Qs.strip(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002407 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2408 QT = AT->getElementType();
Mike Stumpea086c72009-07-25 23:24:03 +00002409 } else {
John McCall8ccfcb52009-09-24 19:53:00 +00002410 return Qs.apply(QT);
Douglas Gregor79f83ed2009-07-23 23:49:00 +00002411 }
2412 }
2413}
2414
Anders Carlsson4bf82142009-09-25 01:23:32 +00002415QualType ASTContext::getBaseElementType(const ArrayType *AT) {
2416 QualType ElemTy = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002417
Anders Carlsson4bf82142009-09-25 01:23:32 +00002418 if (const ArrayType *AT = getAsArrayType(ElemTy))
2419 return getBaseElementType(AT);
Mike Stump11289f42009-09-09 15:08:12 +00002420
Anders Carlssone0808df2008-12-21 03:44:36 +00002421 return ElemTy;
2422}
2423
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002424/// getConstantArrayElementCount - Returns number of constant array elements.
Mike Stump11289f42009-09-09 15:08:12 +00002425uint64_t
Fariborz Jahanian6c9e5a22009-08-21 16:31:06 +00002426ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const {
2427 uint64_t ElementCount = 1;
2428 do {
2429 ElementCount *= CA->getSize().getZExtValue();
2430 CA = dyn_cast<ConstantArrayType>(CA->getElementType());
2431 } while (CA);
2432 return ElementCount;
2433}
2434
Steve Naroff0af91202007-04-27 21:51:21 +00002435/// getFloatingRank - Return a relative rank for floating point types.
2436/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002437static FloatingRank getFloatingRank(QualType T) {
John McCall9dd450b2009-09-21 23:43:11 +00002438 if (const ComplexType *CT = T->getAs<ComplexType>())
Chris Lattnerc6395932007-06-22 20:56:16 +00002439 return getFloatingRank(CT->getElementType());
Chris Lattnerb90739d2008-04-06 23:38:49 +00002440
John McCall9dd450b2009-09-21 23:43:11 +00002441 assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
2442 switch (T->getAs<BuiltinType>()->getKind()) {
Chris Lattnerb90739d2008-04-06 23:38:49 +00002443 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +00002444 case BuiltinType::Float: return FloatRank;
2445 case BuiltinType::Double: return DoubleRank;
2446 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +00002447 }
2448}
2449
Mike Stump11289f42009-09-09 15:08:12 +00002450/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2451/// point or a complex type (based on typeDomain/typeSize).
Steve Narofffc6ffa22007-08-27 01:41:48 +00002452/// 'typeDomain' is a real floating point or complex type.
2453/// 'typeSize' is a real floating point or complex type.
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002454QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2455 QualType Domain) const {
2456 FloatingRank EltRank = getFloatingRank(Size);
2457 if (Domain->isComplexType()) {
2458 switch (EltRank) {
Steve Narofffc6ffa22007-08-27 01:41:48 +00002459 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +00002460 case FloatRank: return FloatComplexTy;
2461 case DoubleRank: return DoubleComplexTy;
2462 case LongDoubleRank: return LongDoubleComplexTy;
2463 }
Steve Naroff0af91202007-04-27 21:51:21 +00002464 }
Chris Lattnerb9dfb032008-04-06 23:58:54 +00002465
2466 assert(Domain->isRealFloatingType() && "Unknown domain!");
2467 switch (EltRank) {
2468 default: assert(0 && "getFloatingRank(): illegal value for rank");
2469 case FloatRank: return FloatTy;
2470 case DoubleRank: return DoubleTy;
2471 case LongDoubleRank: return LongDoubleTy;
Steve Naroff9091ef72007-08-27 01:27:54 +00002472 }
Steve Naroffe4718892007-04-27 18:30:00 +00002473}
2474
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002475/// getFloatingTypeOrder - Compare the rank of the two specified floating
2476/// point types, ignoring the domain of the type (i.e. 'double' ==
2477/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002478/// LHS < RHS, return -1.
Chris Lattnerb90739d2008-04-06 23:38:49 +00002479int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2480 FloatingRank LHSR = getFloatingRank(LHS);
2481 FloatingRank RHSR = getFloatingRank(RHS);
Mike Stump11289f42009-09-09 15:08:12 +00002482
Chris Lattnerb90739d2008-04-06 23:38:49 +00002483 if (LHSR == RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002484 return 0;
Chris Lattnerb90739d2008-04-06 23:38:49 +00002485 if (LHSR > RHSR)
Steve Naroff7af82d42007-08-27 15:30:22 +00002486 return 1;
2487 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +00002488}
2489
Chris Lattner76a00cf2008-04-06 22:59:24 +00002490/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2491/// routine will assert if passed a built-in type that isn't an integer or enum,
2492/// or if it is not canonicalized.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002493unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002494 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedman1efaaea2009-02-13 02:31:07 +00002495 if (EnumType* ET = dyn_cast<EnumType>(T))
2496 T = ET->getDecl()->getIntegerType().getTypePtr();
2497
Eli Friedmanc131d3b2009-07-05 23:44:27 +00002498 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2499 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2500
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002501 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2502 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2503
2504 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2505 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2506
Eli Friedman1efaaea2009-02-13 02:31:07 +00002507 // There are two things which impact the integer rank: the width, and
2508 // the ordering of builtins. The builtin ordering is encoded in the
2509 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner15ba9492009-06-14 01:54:56 +00002510 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedman1efaaea2009-02-13 02:31:07 +00002511 return FWIT->getWidth() << 3;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002512
Chris Lattner76a00cf2008-04-06 22:59:24 +00002513 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002514 default: assert(0 && "getIntegerRank(): not a built-in integer");
2515 case BuiltinType::Bool:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002516 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002517 case BuiltinType::Char_S:
2518 case BuiltinType::Char_U:
2519 case BuiltinType::SChar:
2520 case BuiltinType::UChar:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002521 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002522 case BuiltinType::Short:
2523 case BuiltinType::UShort:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002524 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002525 case BuiltinType::Int:
2526 case BuiltinType::UInt:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002527 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002528 case BuiltinType::Long:
2529 case BuiltinType::ULong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002530 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002531 case BuiltinType::LongLong:
2532 case BuiltinType::ULongLong:
Eli Friedman1efaaea2009-02-13 02:31:07 +00002533 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf122cef2009-04-30 02:43:43 +00002534 case BuiltinType::Int128:
2535 case BuiltinType::UInt128:
2536 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattner76a00cf2008-04-06 22:59:24 +00002537 }
2538}
2539
Eli Friedman629ffb92009-08-20 04:21:42 +00002540/// \brief Whether this is a promotable bitfield reference according
2541/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
2542///
2543/// \returns the type this bit-field will promote to, or NULL if no
2544/// promotion occurs.
2545QualType ASTContext::isPromotableBitField(Expr *E) {
2546 FieldDecl *Field = E->getBitField();
2547 if (!Field)
2548 return QualType();
2549
2550 QualType FT = Field->getType();
2551
2552 llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
2553 uint64_t BitWidth = BitWidthAP.getZExtValue();
2554 uint64_t IntSize = getTypeSize(IntTy);
2555 // GCC extension compatibility: if the bit-field size is less than or equal
2556 // to the size of int, it gets promoted no matter what its type is.
2557 // For instance, unsigned long bf : 4 gets promoted to signed int.
2558 if (BitWidth < IntSize)
2559 return IntTy;
2560
2561 if (BitWidth == IntSize)
2562 return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
2563
2564 // Types bigger than int are not subject to promotions, and therefore act
2565 // like the base type.
2566 // FIXME: This doesn't quite match what gcc does, but what gcc does here
2567 // is ridiculous.
2568 return QualType();
2569}
2570
Eli Friedman5ae98ee2009-08-19 07:44:53 +00002571/// getPromotedIntegerType - Returns the type that Promotable will
2572/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
2573/// integer type.
2574QualType ASTContext::getPromotedIntegerType(QualType Promotable) {
2575 assert(!Promotable.isNull());
2576 assert(Promotable->isPromotableIntegerType());
2577 if (Promotable->isSignedIntegerType())
2578 return IntTy;
2579 uint64_t PromotableSize = getTypeSize(Promotable);
2580 uint64_t IntSize = getTypeSize(IntTy);
2581 assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
2582 return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
2583}
2584
Mike Stump11289f42009-09-09 15:08:12 +00002585/// getIntegerTypeOrder - Returns the highest ranked integer type:
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002586/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
Mike Stump11289f42009-09-09 15:08:12 +00002587/// LHS < RHS, return -1.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002588int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattner76a00cf2008-04-06 22:59:24 +00002589 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2590 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002591 if (LHSC == RHSC) return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002592
Chris Lattner76a00cf2008-04-06 22:59:24 +00002593 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2594 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Mike Stump11289f42009-09-09 15:08:12 +00002595
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002596 unsigned LHSRank = getIntegerRank(LHSC);
2597 unsigned RHSRank = getIntegerRank(RHSC);
Mike Stump11289f42009-09-09 15:08:12 +00002598
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002599 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2600 if (LHSRank == RHSRank) return 0;
2601 return LHSRank > RHSRank ? 1 : -1;
2602 }
Mike Stump11289f42009-09-09 15:08:12 +00002603
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002604 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2605 if (LHSUnsigned) {
2606 // If the unsigned [LHS] type is larger, return it.
2607 if (LHSRank >= RHSRank)
2608 return 1;
Mike Stump11289f42009-09-09 15:08:12 +00002609
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002610 // If the signed type can represent all values of the unsigned type, it
2611 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002612 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002613 return -1;
2614 }
Chris Lattner76a00cf2008-04-06 22:59:24 +00002615
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002616 // If the unsigned [RHS] type is larger, return it.
2617 if (RHSRank >= LHSRank)
2618 return -1;
Mike Stump11289f42009-09-09 15:08:12 +00002619
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002620 // If the signed type can represent all values of the unsigned type, it
2621 // wins. Because we are dealing with 2's complement and types that are
Mike Stump11289f42009-09-09 15:08:12 +00002622 // powers of two larger than each other, this is always safe.
Chris Lattnerd4bacd62008-04-06 23:55:33 +00002623 return 1;
Steve Naroffe4718892007-04-27 18:30:00 +00002624}
Anders Carlsson98f07902007-08-17 05:31:46 +00002625
Mike Stump11289f42009-09-09 15:08:12 +00002626// getCFConstantStringType - Return the type used for constant CFStrings.
Anders Carlsson98f07902007-08-17 05:31:46 +00002627QualType ASTContext::getCFConstantStringType() {
2628 if (!CFConstantStringTypeDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002629 CFConstantStringTypeDecl =
2630 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek47923c72008-09-05 01:34:33 +00002631 &Idents.get("NSConstantString"));
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002632 QualType FieldTypes[4];
Mike Stump11289f42009-09-09 15:08:12 +00002633
Anders Carlsson98f07902007-08-17 05:31:46 +00002634 // const int *isa;
John McCall8ccfcb52009-09-24 19:53:00 +00002635 FieldTypes[0] = getPointerType(IntTy.withConst());
Anders Carlsson9c1011c2007-11-19 00:25:30 +00002636 // int flags;
2637 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +00002638 // const char *str;
John McCall8ccfcb52009-09-24 19:53:00 +00002639 FieldTypes[2] = getPointerType(CharTy.withConst());
Anders Carlsson98f07902007-08-17 05:31:46 +00002640 // long length;
Mike Stump11289f42009-09-09 15:08:12 +00002641 FieldTypes[3] = LongTy;
2642
Anders Carlsson98f07902007-08-17 05:31:46 +00002643 // Create fields
Douglas Gregor91f84212008-12-11 16:49:14 +00002644 for (unsigned i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002645 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
Douglas Gregor91f84212008-12-11 16:49:14 +00002646 SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002647 FieldTypes[i], /*DInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002648 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002649 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002650 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002651 }
2652
2653 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson98f07902007-08-17 05:31:46 +00002654 }
Mike Stump11289f42009-09-09 15:08:12 +00002655
Anders Carlsson98f07902007-08-17 05:31:46 +00002656 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +00002657}
Anders Carlsson87c149b2007-10-11 01:00:40 +00002658
Douglas Gregor512b0772009-04-23 22:29:11 +00002659void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002660 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002661 assert(Rec && "Invalid CFConstantStringType");
2662 CFConstantStringTypeDecl = Rec->getDecl();
2663}
2664
Mike Stump11289f42009-09-09 15:08:12 +00002665QualType ASTContext::getObjCFastEnumerationStateType() {
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002666 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor91f84212008-12-11 16:49:14 +00002667 ObjCFastEnumerationStateTypeDecl =
2668 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2669 &Idents.get("__objcFastEnumerationState"));
Mike Stump11289f42009-09-09 15:08:12 +00002670
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002671 QualType FieldTypes[] = {
2672 UnsignedLongTy,
Steve Naroff1329fa02009-07-15 18:40:39 +00002673 getPointerType(ObjCIdTypedefType),
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002674 getPointerType(UnsignedLongTy),
2675 getConstantArrayType(UnsignedLongTy,
2676 llvm::APInt(32, 5), ArrayType::Normal, 0)
2677 };
Mike Stump11289f42009-09-09 15:08:12 +00002678
Douglas Gregor91f84212008-12-11 16:49:14 +00002679 for (size_t i = 0; i < 4; ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002680 FieldDecl *Field = FieldDecl::Create(*this,
2681 ObjCFastEnumerationStateTypeDecl,
2682 SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00002683 FieldTypes[i], /*DInfo=*/0,
Mike Stump11289f42009-09-09 15:08:12 +00002684 /*BitWidth=*/0,
Douglas Gregor6e6ad602009-01-20 01:17:11 +00002685 /*Mutable=*/false);
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002686 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor91f84212008-12-11 16:49:14 +00002687 }
Mike Stump11289f42009-09-09 15:08:12 +00002688
Douglas Gregor91f84212008-12-11 16:49:14 +00002689 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002690 }
Mike Stump11289f42009-09-09 15:08:12 +00002691
Anders Carlssond89ba7d2008-08-30 19:34:46 +00002692 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2693}
2694
Douglas Gregor512b0772009-04-23 22:29:11 +00002695void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002696 const RecordType *Rec = T->getAs<RecordType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00002697 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2698 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2699}
2700
Anders Carlsson18acd442007-10-29 06:33:42 +00002701// This returns true if a type has been typedefed to BOOL:
2702// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +00002703static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +00002704 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner9b1f2792008-11-24 03:52:59 +00002705 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2706 return II->isStr("BOOL");
Mike Stump11289f42009-09-09 15:08:12 +00002707
Anders Carlssond8499822007-10-29 05:01:08 +00002708 return false;
2709}
2710
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002711/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002712/// purpose.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002713int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner37e05872008-03-05 18:54:05 +00002714 uint64_t sz = getTypeSize(type);
Mike Stump11289f42009-09-09 15:08:12 +00002715
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002716 // Make all integer and enum types at least as large as an int
2717 if (sz > 0 && type->isIntegralType())
Chris Lattner37e05872008-03-05 18:54:05 +00002718 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002719 // Treat arrays as pointers, since that's how they're passed in.
2720 else if (type->isArrayType())
Chris Lattner37e05872008-03-05 18:54:05 +00002721 sz = getTypeSize(VoidPtrTy);
2722 return sz / getTypeSize(CharTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002723}
2724
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002725/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002726/// declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002727void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattner230fc3d2008-11-19 07:24:05 +00002728 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00002729 // FIXME: This is not very efficient.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00002730 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002731 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002732 // Encode result type.
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00002733 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002734 // Compute size of all parameters.
2735 // Start with computing size of a pointer in number of bytes.
2736 // FIXME: There might(should) be a better way of doing this computation!
2737 SourceLocation Loc;
Chris Lattner37e05872008-03-05 18:54:05 +00002738 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002739 // The first two arguments (self and _cmd) are pointers; account for
2740 // their size.
2741 int ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00002742 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2743 E = Decl->param_end(); PI != E; ++PI) {
2744 QualType PType = (*PI)->getType();
2745 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002746 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002747 ParmOffset += sz;
2748 }
2749 S += llvm::utostr(ParmOffset);
2750 S += "@0:";
2751 S += llvm::utostr(PtrSize);
Mike Stump11289f42009-09-09 15:08:12 +00002752
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002753 // Argument types.
2754 ParmOffset = 2 * PtrSize;
Chris Lattnera4997152009-02-20 18:43:26 +00002755 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2756 E = Decl->param_end(); PI != E; ++PI) {
2757 ParmVarDecl *PVDecl = *PI;
Mike Stump11289f42009-09-09 15:08:12 +00002758 QualType PType = PVDecl->getOriginalType();
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00002759 if (const ArrayType *AT =
Steve Naroffe4e55d22009-04-14 00:03:58 +00002760 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2761 // Use array's original type only if it has known number of
2762 // elements.
Steve Naroff323827e2009-04-14 00:40:09 +00002763 if (!isa<ConstantArrayType>(AT))
Steve Naroffe4e55d22009-04-14 00:03:58 +00002764 PType = PVDecl->getType();
2765 } else if (PType->isFunctionType())
2766 PType = PVDecl->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00002767 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002768 // 'in', 'inout', etc.
Fariborz Jahaniana0befc02008-12-20 23:29:59 +00002769 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00002770 getObjCEncodingForType(PType, S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002771 S += llvm::utostr(ParmOffset);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002772 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00002773 }
2774}
2775
Daniel Dunbar4932b362008-08-28 04:38:10 +00002776/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00002777/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar4932b362008-08-28 04:38:10 +00002778/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2779/// NULL when getting encodings for protocol properties.
Mike Stump11289f42009-09-09 15:08:12 +00002780/// Property attributes are stored as a comma-delimited C string. The simple
2781/// attributes readonly and bycopy are encoded as single characters. The
2782/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2783/// encoded as single characters, followed by an identifier. Property types
2784/// are also encoded as a parametrized attribute. The characters used to encode
Fariborz Jahanian2f85a642009-01-20 20:04:12 +00002785/// these attributes are defined by the following enumeration:
2786/// @code
2787/// enum PropertyAttributes {
2788/// kPropertyReadOnly = 'R', // property is read-only.
2789/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2790/// kPropertyByref = '&', // property is a reference to the value last assigned
2791/// kPropertyDynamic = 'D', // property is dynamic
2792/// kPropertyGetter = 'G', // followed by getter selector name
2793/// kPropertySetter = 'S', // followed by setter selector name
2794/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2795/// kPropertyType = 't' // followed by old-style type encoding.
2796/// kPropertyWeak = 'W' // 'weak' property
2797/// kPropertyStrong = 'P' // property GC'able
2798/// kPropertyNonAtomic = 'N' // property non-atomic
2799/// };
2800/// @endcode
Mike Stump11289f42009-09-09 15:08:12 +00002801void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
Daniel Dunbar4932b362008-08-28 04:38:10 +00002802 const Decl *Container,
Chris Lattner230fc3d2008-11-19 07:24:05 +00002803 std::string& S) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00002804 // Collect information from the property implementation decl(s).
2805 bool Dynamic = false;
2806 ObjCPropertyImplDecl *SynthesizePID = 0;
2807
2808 // FIXME: Duplicated code due to poor abstraction.
2809 if (Container) {
Mike Stump11289f42009-09-09 15:08:12 +00002810 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbar4932b362008-08-28 04:38:10 +00002811 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2812 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002813 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002814 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00002815 ObjCPropertyImplDecl *PID = *i;
2816 if (PID->getPropertyDecl() == PD) {
2817 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2818 Dynamic = true;
2819 } else {
2820 SynthesizePID = PID;
2821 }
2822 }
2823 }
2824 } else {
Chris Lattner465fa322008-10-05 17:34:18 +00002825 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar4932b362008-08-28 04:38:10 +00002826 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002827 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor29bd76f2009-04-23 01:02:12 +00002828 i != e; ++i) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00002829 ObjCPropertyImplDecl *PID = *i;
2830 if (PID->getPropertyDecl() == PD) {
2831 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2832 Dynamic = true;
2833 } else {
2834 SynthesizePID = PID;
2835 }
2836 }
Mike Stump11289f42009-09-09 15:08:12 +00002837 }
Daniel Dunbar4932b362008-08-28 04:38:10 +00002838 }
2839 }
2840
2841 // FIXME: This is not very efficient.
2842 S = "T";
2843
2844 // Encode result type.
Fariborz Jahanian218c6302009-01-20 19:14:18 +00002845 // GCC has some special rules regarding encoding of properties which
2846 // closely resembles encoding of ivars.
Mike Stump11289f42009-09-09 15:08:12 +00002847 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00002848 true /* outermost type */,
2849 true /* encoding for property */);
Daniel Dunbar4932b362008-08-28 04:38:10 +00002850
2851 if (PD->isReadOnly()) {
2852 S += ",R";
2853 } else {
2854 switch (PD->getSetterKind()) {
2855 case ObjCPropertyDecl::Assign: break;
2856 case ObjCPropertyDecl::Copy: S += ",C"; break;
Mike Stump11289f42009-09-09 15:08:12 +00002857 case ObjCPropertyDecl::Retain: S += ",&"; break;
Daniel Dunbar4932b362008-08-28 04:38:10 +00002858 }
2859 }
2860
2861 // It really isn't clear at all what this means, since properties
2862 // are "dynamic by default".
2863 if (Dynamic)
2864 S += ",D";
2865
Fariborz Jahanian218c6302009-01-20 19:14:18 +00002866 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2867 S += ",N";
Mike Stump11289f42009-09-09 15:08:12 +00002868
Daniel Dunbar4932b362008-08-28 04:38:10 +00002869 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2870 S += ",G";
Chris Lattnere4b95692008-11-24 03:33:13 +00002871 S += PD->getGetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00002872 }
2873
2874 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2875 S += ",S";
Chris Lattnere4b95692008-11-24 03:33:13 +00002876 S += PD->getSetterName().getAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00002877 }
2878
2879 if (SynthesizePID) {
2880 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2881 S += ",V";
Chris Lattner1cbaacc2008-11-24 04:00:27 +00002882 S += OID->getNameAsString();
Daniel Dunbar4932b362008-08-28 04:38:10 +00002883 }
2884
2885 // FIXME: OBJCGC: weak & strong
2886}
2887
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00002888/// getLegacyIntegralTypeEncoding -
Mike Stump11289f42009-09-09 15:08:12 +00002889/// Another legacy compatibility encoding: 32-bit longs are encoded as
2890/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00002891/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2892///
2893void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump212005c2009-07-22 18:58:19 +00002894 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
John McCall9dd450b2009-09-21 23:43:11 +00002895 if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00002896 if (BT->getKind() == BuiltinType::ULong &&
2897 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00002898 PointeeTy = UnsignedIntTy;
Mike Stump11289f42009-09-09 15:08:12 +00002899 else
Fariborz Jahanian77b6b5d2009-02-11 23:59:18 +00002900 if (BT->getKind() == BuiltinType::Long &&
2901 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00002902 PointeeTy = IntTy;
2903 }
2904 }
2905}
2906
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00002907void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00002908 const FieldDecl *Field) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00002909 // We follow the behavior of gcc, expanding structures which are
2910 // directly pointed to, and expanding embedded structures. Note that
2911 // these rules are sufficient to prevent recursive encoding of the
2912 // same type.
Mike Stump11289f42009-09-09 15:08:12 +00002913 getObjCEncodingForTypeImpl(T, S, true, true, Field,
Fariborz Jahaniandaef00b2008-12-22 23:22:27 +00002914 true /* outermost type */);
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00002915}
2916
Mike Stump11289f42009-09-09 15:08:12 +00002917static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00002918 const FieldDecl *FD) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00002919 const Expr *E = FD->getBitWidth();
2920 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2921 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman1c4a1752009-04-26 19:19:15 +00002922 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian5c767722009-01-13 01:18:13 +00002923 S += 'b';
2924 S += llvm::utostr(N);
2925}
2926
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00002927void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2928 bool ExpandPointedToStructures,
2929 bool ExpandStructures,
Daniel Dunbarc040ce42009-04-20 06:37:24 +00002930 const FieldDecl *FD,
Fariborz Jahanian218c6302009-01-20 19:14:18 +00002931 bool OutermostType,
Douglas Gregorbcced4e2009-04-09 21:40:53 +00002932 bool EncodingProperty) {
John McCall9dd450b2009-09-21 23:43:11 +00002933 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00002934 if (FD && FD->isBitField())
2935 return EncodeBitField(this, S, FD);
2936 char encoding;
2937 switch (BT->getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002938 default: assert(0 && "Unhandled builtin type kind");
Chris Lattnere7cabb92009-07-13 00:10:46 +00002939 case BuiltinType::Void: encoding = 'v'; break;
2940 case BuiltinType::Bool: encoding = 'B'; break;
2941 case BuiltinType::Char_U:
2942 case BuiltinType::UChar: encoding = 'C'; break;
2943 case BuiltinType::UShort: encoding = 'S'; break;
2944 case BuiltinType::UInt: encoding = 'I'; break;
Mike Stump11289f42009-09-09 15:08:12 +00002945 case BuiltinType::ULong:
2946 encoding =
2947 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian1f0a9eb2009-02-11 22:31:45 +00002948 break;
Chris Lattnere7cabb92009-07-13 00:10:46 +00002949 case BuiltinType::UInt128: encoding = 'T'; break;
2950 case BuiltinType::ULongLong: encoding = 'Q'; break;
2951 case BuiltinType::Char_S:
2952 case BuiltinType::SChar: encoding = 'c'; break;
2953 case BuiltinType::Short: encoding = 's'; break;
2954 case BuiltinType::Int: encoding = 'i'; break;
Mike Stump11289f42009-09-09 15:08:12 +00002955 case BuiltinType::Long:
2956 encoding =
2957 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
Chris Lattnere7cabb92009-07-13 00:10:46 +00002958 break;
2959 case BuiltinType::LongLong: encoding = 'q'; break;
2960 case BuiltinType::Int128: encoding = 't'; break;
2961 case BuiltinType::Float: encoding = 'f'; break;
2962 case BuiltinType::Double: encoding = 'd'; break;
2963 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00002964 }
Mike Stump11289f42009-09-09 15:08:12 +00002965
Chris Lattnere7cabb92009-07-13 00:10:46 +00002966 S += encoding;
2967 return;
2968 }
Mike Stump11289f42009-09-09 15:08:12 +00002969
John McCall9dd450b2009-09-21 23:43:11 +00002970 if (const ComplexType *CT = T->getAs<ComplexType>()) {
Anders Carlsson39b2e132009-04-09 21:55:45 +00002971 S += 'j';
Mike Stump11289f42009-09-09 15:08:12 +00002972 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
Anders Carlsson39b2e132009-04-09 21:55:45 +00002973 false);
Chris Lattnere7cabb92009-07-13 00:10:46 +00002974 return;
2975 }
Mike Stump11289f42009-09-09 15:08:12 +00002976
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002977 if (const PointerType *PT = T->getAs<PointerType>()) {
Anders Carlssond8499822007-10-29 05:01:08 +00002978 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00002979 bool isReadOnly = false;
2980 // For historical/compatibility reasons, the read-only qualifier of the
2981 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2982 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
Mike Stump11289f42009-09-09 15:08:12 +00002983 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump212005c2009-07-22 18:58:19 +00002984 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00002985 if (OutermostType && T.isConstQualified()) {
2986 isReadOnly = true;
2987 S += 'r';
2988 }
Mike Stumpe9c6ffc2009-07-31 02:02:20 +00002989 } else if (OutermostType) {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00002990 QualType P = PointeeTy;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002991 while (P->getAs<PointerType>())
2992 P = P->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00002993 if (P.isConstQualified()) {
2994 isReadOnly = true;
2995 S += 'r';
2996 }
2997 }
2998 if (isReadOnly) {
2999 // Another legacy compatibility encoding. Some ObjC qualifier and type
3000 // combinations need to be rearranged.
3001 // Rewrite "in const" from "nr" to "rn"
3002 const char * s = S.c_str();
3003 int len = S.length();
3004 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
3005 std::string replace = "rn";
3006 S.replace(S.end()-2, S.end(), replace);
3007 }
3008 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00003009 if (isObjCSelType(PointeeTy)) {
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003010 S += ':';
3011 return;
Fariborz Jahanian509d8d62007-10-30 17:06:23 +00003012 }
Mike Stump11289f42009-09-09 15:08:12 +00003013
Anders Carlssond8499822007-10-29 05:01:08 +00003014 if (PointeeTy->isCharType()) {
3015 // char pointer types should be encoded as '*' unless it is a
3016 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00003017 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00003018 S += '*';
3019 return;
3020 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003021 } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
Steve Naroff3de6b702009-07-22 17:14:51 +00003022 // GCC binary compat: Need to convert "struct objc_class *" to "#".
3023 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
3024 S += '#';
3025 return;
3026 }
3027 // GCC binary compat: Need to convert "struct objc_object *" to "@".
3028 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
3029 S += '@';
3030 return;
3031 }
3032 // fall through...
Anders Carlssond8499822007-10-29 05:01:08 +00003033 }
Anders Carlssond8499822007-10-29 05:01:08 +00003034 S += '^';
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003035 getLegacyIntegralTypeEncoding(PointeeTy);
3036
Mike Stump11289f42009-09-09 15:08:12 +00003037 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003038 NULL);
Chris Lattnere7cabb92009-07-13 00:10:46 +00003039 return;
3040 }
Mike Stump11289f42009-09-09 15:08:12 +00003041
Chris Lattnere7cabb92009-07-13 00:10:46 +00003042 if (const ArrayType *AT =
3043 // Ignore type qualifiers etc.
3044 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlssond05f44b2009-02-22 01:38:57 +00003045 if (isa<IncompleteArrayType>(AT)) {
3046 // Incomplete arrays are encoded as a pointer to the array element.
3047 S += '^';
3048
Mike Stump11289f42009-09-09 15:08:12 +00003049 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003050 false, ExpandStructures, FD);
3051 } else {
3052 S += '[';
Mike Stump11289f42009-09-09 15:08:12 +00003053
Anders Carlssond05f44b2009-02-22 01:38:57 +00003054 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
3055 S += llvm::utostr(CAT->getSize().getZExtValue());
3056 else {
3057 //Variable length arrays are encoded as a regular array with 0 elements.
3058 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
3059 S += '0';
3060 }
Mike Stump11289f42009-09-09 15:08:12 +00003061
3062 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Anders Carlssond05f44b2009-02-22 01:38:57 +00003063 false, ExpandStructures, FD);
3064 S += ']';
3065 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003066 return;
3067 }
Mike Stump11289f42009-09-09 15:08:12 +00003068
John McCall9dd450b2009-09-21 23:43:11 +00003069 if (T->getAs<FunctionType>()) {
Anders Carlssondf4cc612007-10-30 00:06:20 +00003070 S += '?';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003071 return;
3072 }
Mike Stump11289f42009-09-09 15:08:12 +00003073
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003074 if (const RecordType *RTy = T->getAs<RecordType>()) {
Daniel Dunbar3cd9a292008-10-17 07:30:50 +00003075 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003076 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar40cac772008-10-17 06:22:57 +00003077 // Anonymous structures print as '?'
3078 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
3079 S += II->getName();
3080 } else {
3081 S += '?';
3082 }
Daniel Dunbarfc1066d2008-10-17 20:21:44 +00003083 if (ExpandStructures) {
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003084 S += '=';
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003085 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
3086 FieldEnd = RDecl->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +00003087 Field != FieldEnd; ++Field) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003088 if (FD) {
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003089 S += '"';
Douglas Gregor91f84212008-12-11 16:49:14 +00003090 S += Field->getNameAsString();
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003091 S += '"';
3092 }
Mike Stump11289f42009-09-09 15:08:12 +00003093
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003094 // Special case bit-fields.
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003095 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +00003096 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003097 (*Field));
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003098 } else {
Fariborz Jahanian0f66a6c2008-12-23 19:56:47 +00003099 QualType qt = Field->getType();
3100 getLegacyIntegralTypeEncoding(qt);
Mike Stump11289f42009-09-09 15:08:12 +00003101 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003102 FD);
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003103 }
Fariborz Jahanian0a71ad22008-01-22 22:44:46 +00003104 }
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00003105 }
Daniel Dunbarff3c6742008-10-17 16:17:37 +00003106 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003107 return;
3108 }
Mike Stump11289f42009-09-09 15:08:12 +00003109
Chris Lattnere7cabb92009-07-13 00:10:46 +00003110 if (T->isEnumeralType()) {
Fariborz Jahanian5c767722009-01-13 01:18:13 +00003111 if (FD && FD->isBitField())
3112 EncodeBitField(this, S, FD);
3113 else
3114 S += 'i';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003115 return;
3116 }
Mike Stump11289f42009-09-09 15:08:12 +00003117
Chris Lattnere7cabb92009-07-13 00:10:46 +00003118 if (T->isBlockPointerType()) {
Steve Naroff49140cb2009-02-02 18:24:29 +00003119 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnere7cabb92009-07-13 00:10:46 +00003120 return;
3121 }
Mike Stump11289f42009-09-09 15:08:12 +00003122
John McCall8ccfcb52009-09-24 19:53:00 +00003123 if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003124 // @encode(class_name)
John McCall8ccfcb52009-09-24 19:53:00 +00003125 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003126 S += '{';
3127 const IdentifierInfo *II = OI->getIdentifier();
3128 S += II->getName();
3129 S += '=';
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003130 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003131 CollectObjCIvars(OI, RecFields);
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003132 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003133 if (RecFields[i]->isBitField())
Mike Stump11289f42009-09-09 15:08:12 +00003134 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003135 RecFields[i]);
3136 else
Mike Stump11289f42009-09-09 15:08:12 +00003137 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003138 FD);
3139 }
3140 S += '}';
Chris Lattnere7cabb92009-07-13 00:10:46 +00003141 return;
Fariborz Jahanian1d35f122008-12-19 23:34:38 +00003142 }
Mike Stump11289f42009-09-09 15:08:12 +00003143
John McCall9dd450b2009-09-21 23:43:11 +00003144 if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003145 if (OPT->isObjCIdType()) {
3146 S += '@';
3147 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003148 }
Mike Stump11289f42009-09-09 15:08:12 +00003149
Chris Lattnere7cabb92009-07-13 00:10:46 +00003150 if (OPT->isObjCClassType()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003151 S += '#';
3152 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003153 }
Mike Stump11289f42009-09-09 15:08:12 +00003154
Chris Lattnere7cabb92009-07-13 00:10:46 +00003155 if (OPT->isObjCQualifiedIdType()) {
Mike Stump11289f42009-09-09 15:08:12 +00003156 getObjCEncodingForTypeImpl(getObjCIdType(), S,
Steve Naroff7cae42b2009-07-10 23:34:53 +00003157 ExpandPointedToStructures,
3158 ExpandStructures, FD);
3159 if (FD || EncodingProperty) {
3160 // Note that we do extended encoding of protocol qualifer list
3161 // Only when doing ivar or property encoding.
Steve Naroff7cae42b2009-07-10 23:34:53 +00003162 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003163 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3164 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003165 S += '<';
3166 S += (*I)->getNameAsString();
3167 S += '>';
3168 }
3169 S += '"';
3170 }
3171 return;
Chris Lattnere7cabb92009-07-13 00:10:46 +00003172 }
Mike Stump11289f42009-09-09 15:08:12 +00003173
Chris Lattnere7cabb92009-07-13 00:10:46 +00003174 QualType PointeeTy = OPT->getPointeeType();
3175 if (!EncodingProperty &&
3176 isa<TypedefType>(PointeeTy.getTypePtr())) {
3177 // Another historical/compatibility reason.
Mike Stump11289f42009-09-09 15:08:12 +00003178 // We encode the underlying type which comes out as
Chris Lattnere7cabb92009-07-13 00:10:46 +00003179 // {...};
3180 S += '^';
Mike Stump11289f42009-09-09 15:08:12 +00003181 getObjCEncodingForTypeImpl(PointeeTy, S,
3182 false, ExpandPointedToStructures,
Chris Lattnere7cabb92009-07-13 00:10:46 +00003183 NULL);
Steve Naroff7cae42b2009-07-10 23:34:53 +00003184 return;
3185 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003186
3187 S += '@';
3188 if (FD || EncodingProperty) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003189 S += '"';
Steve Naroffaccc4882009-07-20 17:56:53 +00003190 S += OPT->getInterfaceDecl()->getNameAsCString();
3191 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
3192 E = OPT->qual_end(); I != E; ++I) {
Chris Lattnere7cabb92009-07-13 00:10:46 +00003193 S += '<';
3194 S += (*I)->getNameAsString();
3195 S += '>';
Mike Stump11289f42009-09-09 15:08:12 +00003196 }
Chris Lattnere7cabb92009-07-13 00:10:46 +00003197 S += '"';
3198 }
3199 return;
3200 }
Mike Stump11289f42009-09-09 15:08:12 +00003201
Chris Lattnere7cabb92009-07-13 00:10:46 +00003202 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00003203}
3204
Mike Stump11289f42009-09-09 15:08:12 +00003205void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00003206 std::string& S) const {
3207 if (QT & Decl::OBJC_TQ_In)
3208 S += 'n';
3209 if (QT & Decl::OBJC_TQ_Inout)
3210 S += 'N';
3211 if (QT & Decl::OBJC_TQ_Out)
3212 S += 'o';
3213 if (QT & Decl::OBJC_TQ_Bycopy)
3214 S += 'O';
3215 if (QT & Decl::OBJC_TQ_Byref)
3216 S += 'R';
3217 if (QT & Decl::OBJC_TQ_Oneway)
3218 S += 'V';
3219}
3220
Chris Lattnere7cabb92009-07-13 00:10:46 +00003221void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlsson87c149b2007-10-11 01:00:40 +00003222 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003223
Anders Carlsson87c149b2007-10-11 01:00:40 +00003224 BuiltinVaListType = T;
3225}
3226
Chris Lattnere7cabb92009-07-13 00:10:46 +00003227void ASTContext::setObjCIdType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003228 ObjCIdTypedefType = T;
Steve Naroff66e9f332007-10-15 14:41:52 +00003229}
3230
Chris Lattnere7cabb92009-07-13 00:10:46 +00003231void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor512b0772009-04-23 22:29:11 +00003232 ObjCSelType = T;
3233
John McCall9dd450b2009-09-21 23:43:11 +00003234 const TypedefType *TT = T->getAs<TypedefType>();
Douglas Gregor512b0772009-04-23 22:29:11 +00003235 if (!TT)
3236 return;
3237 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003238
3239 // typedef struct objc_selector *SEL;
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003240 const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>();
Fariborz Jahanian1778f4b2009-01-16 19:58:32 +00003241 if (!ptr)
3242 return;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003243 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanian1778f4b2009-01-16 19:58:32 +00003244 if (!rec)
3245 return;
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00003246 SelStructType = rec;
3247}
3248
Chris Lattnere7cabb92009-07-13 00:10:46 +00003249void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003250 ObjCProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003251}
3252
Chris Lattnere7cabb92009-07-13 00:10:46 +00003253void ASTContext::setObjCClassType(QualType T) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003254 ObjCClassTypedefType = T;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00003255}
3256
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003257void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
Mike Stump11289f42009-09-09 15:08:12 +00003258 assert(ObjCConstantStringType.isNull() &&
Steve Narofff73b7842007-10-15 23:35:17 +00003259 "'NSConstantString' type already set!");
Mike Stump11289f42009-09-09 15:08:12 +00003260
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003261 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff73b7842007-10-15 23:35:17 +00003262}
3263
Douglas Gregordc572a32009-03-30 22:58:21 +00003264/// \brief Retrieve the template name that represents a qualified
3265/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003266TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003267 bool TemplateKeyword,
3268 TemplateDecl *Template) {
3269 llvm::FoldingSetNodeID ID;
3270 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3271
3272 void *InsertPos = 0;
3273 QualifiedTemplateName *QTN =
3274 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3275 if (!QTN) {
3276 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3277 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3278 }
3279
3280 return TemplateName(QTN);
3281}
3282
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00003283/// \brief Retrieve the template name that represents a qualified
3284/// template name such as \c std::vector.
Mike Stump11289f42009-09-09 15:08:12 +00003285TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00003286 bool TemplateKeyword,
3287 OverloadedFunctionDecl *Template) {
3288 llvm::FoldingSetNodeID ID;
3289 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
Mike Stump11289f42009-09-09 15:08:12 +00003290
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00003291 void *InsertPos = 0;
3292 QualifiedTemplateName *QTN =
3293 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3294 if (!QTN) {
3295 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3296 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3297 }
Mike Stump11289f42009-09-09 15:08:12 +00003298
Douglas Gregoraa87ebc2009-07-29 18:26:50 +00003299 return TemplateName(QTN);
3300}
3301
Douglas Gregordc572a32009-03-30 22:58:21 +00003302/// \brief Retrieve the template name that represents a dependent
3303/// template name such as \c MetaFun::template apply.
Mike Stump11289f42009-09-09 15:08:12 +00003304TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
Douglas Gregordc572a32009-03-30 22:58:21 +00003305 const IdentifierInfo *Name) {
Mike Stump11289f42009-09-09 15:08:12 +00003306 assert((!NNS || NNS->isDependent()) &&
Douglas Gregor308047d2009-09-09 00:23:06 +00003307 "Nested name specifier must be dependent");
Douglas Gregordc572a32009-03-30 22:58:21 +00003308
3309 llvm::FoldingSetNodeID ID;
3310 DependentTemplateName::Profile(ID, NNS, Name);
3311
3312 void *InsertPos = 0;
3313 DependentTemplateName *QTN =
3314 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3315
3316 if (QTN)
3317 return TemplateName(QTN);
3318
3319 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3320 if (CanonNNS == NNS) {
3321 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3322 } else {
3323 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3324 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3325 }
3326
3327 DependentTemplateNames.InsertNode(QTN, InsertPos);
3328 return TemplateName(QTN);
3329}
3330
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003331/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorab138572008-11-03 15:57:00 +00003332/// TargetInfo, produce the corresponding type. The unsigned @p Type
3333/// is actually a value of type @c TargetInfo::IntType.
3334QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003335 switch (Type) {
Mike Stump11289f42009-09-09 15:08:12 +00003336 case TargetInfo::NoInt: return QualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003337 case TargetInfo::SignedShort: return ShortTy;
3338 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3339 case TargetInfo::SignedInt: return IntTy;
3340 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3341 case TargetInfo::SignedLong: return LongTy;
3342 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3343 case TargetInfo::SignedLongLong: return LongLongTy;
3344 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3345 }
3346
3347 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbare27d7b52008-11-11 01:16:00 +00003348 return QualType();
Douglas Gregor8af6e6d2008-11-03 14:12:49 +00003349}
Ted Kremenek77c51b22008-07-24 23:58:27 +00003350
3351//===----------------------------------------------------------------------===//
3352// Type Predicates.
3353//===----------------------------------------------------------------------===//
3354
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003355/// isObjCNSObjectType - Return true if this is an NSObject object using
3356/// NSObject attribute on a c-style pointer type.
3357/// FIXME - Make it work directly on types.
Steve Naroff79d12152009-07-16 15:41:00 +00003358/// FIXME: Move to Type.
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003359///
3360bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3361 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3362 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00003363 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003364 return true;
3365 }
Mike Stump11289f42009-09-09 15:08:12 +00003366 return false;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00003367}
3368
Fariborz Jahanian96207692009-02-18 21:49:28 +00003369/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3370/// garbage collection attribute.
3371///
John McCall8ccfcb52009-09-24 19:53:00 +00003372Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
3373 Qualifiers::GC GCAttrs = Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003374 if (getLangOptions().ObjC1 &&
3375 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerd60183d2009-02-18 22:53:11 +00003376 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian96207692009-02-18 21:49:28 +00003377 // Default behavious under objective-c's gc is for objective-c pointers
Mike Stump11289f42009-09-09 15:08:12 +00003378 // (or pointers to them) be treated as though they were declared
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003379 // as __strong.
John McCall8ccfcb52009-09-24 19:53:00 +00003380 if (GCAttrs == Qualifiers::GCNone) {
Fariborz Jahanian8d6298b2009-09-10 23:38:45 +00003381 if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003382 GCAttrs = Qualifiers::Strong;
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003383 else if (Ty->isPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003384 return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
Fariborz Jahanian0f466c72009-02-19 23:36:06 +00003385 }
Fariborz Jahaniand381cde2009-04-11 00:00:54 +00003386 // Non-pointers have none gc'able attribute regardless of the attribute
3387 // set on them.
Steve Naroff79d12152009-07-16 15:41:00 +00003388 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003389 return Qualifiers::GCNone;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003390 }
Chris Lattnerd60183d2009-02-18 22:53:11 +00003391 return GCAttrs;
Fariborz Jahanian96207692009-02-18 21:49:28 +00003392}
3393
Chris Lattner49af6a42008-04-07 06:51:04 +00003394//===----------------------------------------------------------------------===//
3395// Type Compatibility Testing
3396//===----------------------------------------------------------------------===//
Chris Lattnerb338a6b2007-11-01 05:03:41 +00003397
Mike Stump11289f42009-09-09 15:08:12 +00003398/// areCompatVectorTypes - Return true if the two specified vector types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003399/// compatible.
3400static bool areCompatVectorTypes(const VectorType *LHS,
3401 const VectorType *RHS) {
3402 assert(LHS->isCanonical() && RHS->isCanonical());
3403 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner465fa322008-10-05 17:34:18 +00003404 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner49af6a42008-04-07 06:51:04 +00003405}
3406
Steve Naroff8e6aee52009-07-23 01:01:38 +00003407//===----------------------------------------------------------------------===//
3408// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3409//===----------------------------------------------------------------------===//
3410
3411/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3412/// inheritance hierarchy of 'rProto'.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003413bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3414 ObjCProtocolDecl *rProto) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003415 if (lProto == rProto)
3416 return true;
3417 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3418 E = rProto->protocol_end(); PI != E; ++PI)
3419 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3420 return true;
3421 return false;
3422}
3423
Steve Naroff8e6aee52009-07-23 01:01:38 +00003424/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3425/// return true if lhs's protocols conform to rhs's protocol; false
3426/// otherwise.
3427bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3428 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3429 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3430 return false;
3431}
3432
3433/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3434/// ObjCQualifiedIDType.
3435bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3436 bool compare) {
3437 // Allow id<P..> and an 'id' or void* type in all cases.
Mike Stump11289f42009-09-09 15:08:12 +00003438 if (lhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003439 lhs->isObjCIdType() || lhs->isObjCClassType())
3440 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003441 else if (rhs->isVoidPointerType() ||
Steve Naroff8e6aee52009-07-23 01:01:38 +00003442 rhs->isObjCIdType() || rhs->isObjCClassType())
3443 return true;
3444
3445 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
John McCall9dd450b2009-09-21 23:43:11 +00003446 const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00003447
Steve Naroff8e6aee52009-07-23 01:01:38 +00003448 if (!rhsOPT) return false;
Mike Stump11289f42009-09-09 15:08:12 +00003449
Steve Naroff8e6aee52009-07-23 01:01:38 +00003450 if (rhsOPT->qual_empty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003451 // If the RHS is a unqualified interface pointer "NSString*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00003452 // make sure we check the class hierarchy.
3453 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3454 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3455 E = lhsQID->qual_end(); I != E; ++I) {
3456 // when comparing an id<P> on lhs with a static type on rhs,
3457 // see if static class implements all of id's protocols, directly or
3458 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003459 if (!rhsID->ClassImplementsProtocol(*I, true))
Steve Naroff8e6aee52009-07-23 01:01:38 +00003460 return false;
3461 }
3462 }
3463 // If there are no qualifiers and no interface, we have an 'id'.
3464 return true;
3465 }
Mike Stump11289f42009-09-09 15:08:12 +00003466 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00003467 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3468 E = lhsQID->qual_end(); I != E; ++I) {
3469 ObjCProtocolDecl *lhsProto = *I;
3470 bool match = false;
3471
3472 // when comparing an id<P> on lhs with a static type on rhs,
3473 // see if static class implements all of id's protocols, directly or
3474 // through its super class and categories.
3475 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3476 E = rhsOPT->qual_end(); J != E; ++J) {
3477 ObjCProtocolDecl *rhsProto = *J;
3478 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3479 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3480 match = true;
3481 break;
3482 }
3483 }
Mike Stump11289f42009-09-09 15:08:12 +00003484 // If the RHS is a qualified interface pointer "NSString<P>*",
Steve Naroff8e6aee52009-07-23 01:01:38 +00003485 // make sure we check the class hierarchy.
3486 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3487 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3488 E = lhsQID->qual_end(); I != E; ++I) {
3489 // when comparing an id<P> on lhs with a static type on rhs,
3490 // see if static class implements all of id's protocols, directly or
3491 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003492 if (rhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003493 match = true;
3494 break;
3495 }
3496 }
3497 }
3498 if (!match)
3499 return false;
3500 }
Mike Stump11289f42009-09-09 15:08:12 +00003501
Steve Naroff8e6aee52009-07-23 01:01:38 +00003502 return true;
3503 }
Mike Stump11289f42009-09-09 15:08:12 +00003504
Steve Naroff8e6aee52009-07-23 01:01:38 +00003505 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3506 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3507
Mike Stump11289f42009-09-09 15:08:12 +00003508 if (const ObjCObjectPointerType *lhsOPT =
Steve Naroff8e6aee52009-07-23 01:01:38 +00003509 lhs->getAsObjCInterfacePointerType()) {
3510 if (lhsOPT->qual_empty()) {
3511 bool match = false;
3512 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3513 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3514 E = rhsQID->qual_end(); I != E; ++I) {
3515 // when comparing an id<P> on lhs with a static type on rhs,
3516 // see if static class implements all of id's protocols, directly or
3517 // through its super class and categories.
Fariborz Jahanian3f8917a2009-08-11 22:02:25 +00003518 if (lhsID->ClassImplementsProtocol(*I, true)) {
Steve Naroff8e6aee52009-07-23 01:01:38 +00003519 match = true;
3520 break;
3521 }
3522 }
3523 if (!match)
3524 return false;
3525 }
3526 return true;
3527 }
Mike Stump11289f42009-09-09 15:08:12 +00003528 // Both the right and left sides have qualifiers.
Steve Naroff8e6aee52009-07-23 01:01:38 +00003529 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3530 E = lhsOPT->qual_end(); I != E; ++I) {
3531 ObjCProtocolDecl *lhsProto = *I;
3532 bool match = false;
3533
3534 // when comparing an id<P> on lhs with a static type on rhs,
3535 // see if static class implements all of id's protocols, directly or
3536 // through its super class and categories.
3537 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3538 E = rhsQID->qual_end(); J != E; ++J) {
3539 ObjCProtocolDecl *rhsProto = *J;
3540 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3541 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3542 match = true;
3543 break;
3544 }
3545 }
3546 if (!match)
3547 return false;
3548 }
3549 return true;
3550 }
3551 return false;
3552}
3553
Eli Friedman47f77112008-08-22 00:56:42 +00003554/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner49af6a42008-04-07 06:51:04 +00003555/// compatible for assignment from RHS to LHS. This handles validation of any
3556/// protocol qualifiers on the LHS or RHS.
3557///
Steve Naroff7cae42b2009-07-10 23:34:53 +00003558bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3559 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff1329fa02009-07-15 18:40:39 +00003560 // If either type represents the built-in 'id' or 'Class' types, return true.
3561 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff7cae42b2009-07-10 23:34:53 +00003562 return true;
3563
Steve Naroff8e6aee52009-07-23 01:01:38 +00003564 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
Mike Stump11289f42009-09-09 15:08:12 +00003565 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3566 QualType(RHSOPT,0),
Steve Naroff8e6aee52009-07-23 01:01:38 +00003567 false);
3568
Steve Naroff7cae42b2009-07-10 23:34:53 +00003569 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3570 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff8e6aee52009-07-23 01:01:38 +00003571 if (LHS && RHS) // We have 2 user-defined types.
3572 return canAssignObjCInterfaces(LHS, RHS);
Mike Stump11289f42009-09-09 15:08:12 +00003573
Steve Naroff8e6aee52009-07-23 01:01:38 +00003574 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00003575}
3576
Eli Friedman47f77112008-08-22 00:56:42 +00003577bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3578 const ObjCInterfaceType *RHS) {
Chris Lattner49af6a42008-04-07 06:51:04 +00003579 // Verify that the base decls are compatible: the RHS must be a subclass of
3580 // the LHS.
3581 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3582 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003583
Chris Lattner49af6a42008-04-07 06:51:04 +00003584 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3585 // protocol qualified at all, then we are good.
Steve Naroffc277ad12009-07-18 15:33:26 +00003586 if (LHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00003587 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003588
Chris Lattner49af6a42008-04-07 06:51:04 +00003589 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3590 // isn't a superset.
Steve Naroffc277ad12009-07-18 15:33:26 +00003591 if (RHS->getNumProtocols() == 0)
Chris Lattner49af6a42008-04-07 06:51:04 +00003592 return true; // FIXME: should return false!
Mike Stump11289f42009-09-09 15:08:12 +00003593
Steve Naroffc277ad12009-07-18 15:33:26 +00003594 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3595 LHSPE = LHS->qual_end();
Steve Naroff114aecb2009-03-01 16:12:44 +00003596 LHSPI != LHSPE; LHSPI++) {
3597 bool RHSImplementsProtocol = false;
3598
3599 // If the RHS doesn't implement the protocol on the left, the types
3600 // are incompatible.
Steve Naroffc277ad12009-07-18 15:33:26 +00003601 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff8e6aee52009-07-23 01:01:38 +00003602 RHSPE = RHS->qual_end();
Steve Narofffac5bc92009-07-16 16:21:02 +00003603 RHSPI != RHSPE; RHSPI++) {
3604 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff114aecb2009-03-01 16:12:44 +00003605 RHSImplementsProtocol = true;
Steve Narofffac5bc92009-07-16 16:21:02 +00003606 break;
3607 }
Steve Naroff114aecb2009-03-01 16:12:44 +00003608 }
3609 // FIXME: For better diagnostics, consider passing back the protocol name.
3610 if (!RHSImplementsProtocol)
3611 return false;
Chris Lattner49af6a42008-04-07 06:51:04 +00003612 }
Steve Naroff114aecb2009-03-01 16:12:44 +00003613 // The RHS implements all protocols listed on the LHS.
3614 return true;
Chris Lattner49af6a42008-04-07 06:51:04 +00003615}
3616
Steve Naroffb7605152009-02-12 17:52:19 +00003617bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3618 // get the "pointed to" types
John McCall9dd450b2009-09-21 23:43:11 +00003619 const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
3620 const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00003621
Steve Naroff7cae42b2009-07-10 23:34:53 +00003622 if (!LHSOPT || !RHSOPT)
Steve Naroffb7605152009-02-12 17:52:19 +00003623 return false;
Steve Naroff7cae42b2009-07-10 23:34:53 +00003624
3625 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3626 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroffb7605152009-02-12 17:52:19 +00003627}
3628
Mike Stump11289f42009-09-09 15:08:12 +00003629/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
Steve Naroff32e44c02007-10-15 20:41:53 +00003630/// both shall have the identically qualified version of a compatible type.
Mike Stump11289f42009-09-09 15:08:12 +00003631/// C99 6.2.7p1: Two types have compatible types if their types are the
Steve Naroff32e44c02007-10-15 20:41:53 +00003632/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman47f77112008-08-22 00:56:42 +00003633bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3634 return !mergeTypes(LHS, RHS).isNull();
3635}
3636
3637QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
John McCall9dd450b2009-09-21 23:43:11 +00003638 const FunctionType *lbase = lhs->getAs<FunctionType>();
3639 const FunctionType *rbase = rhs->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003640 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3641 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman47f77112008-08-22 00:56:42 +00003642 bool allLTypes = true;
3643 bool allRTypes = true;
3644
3645 // Check return type
3646 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3647 if (retType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00003648 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3649 allLTypes = false;
3650 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3651 allRTypes = false;
Mike Stumpea086c72009-07-25 23:24:03 +00003652 // FIXME: double check this
Mike Stump8c5d7992009-07-25 21:26:53 +00003653 bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr();
3654 if (NoReturn != lbase->getNoReturnAttr())
3655 allLTypes = false;
3656 if (NoReturn != rbase->getNoReturnAttr())
3657 allRTypes = false;
Mike Stump11289f42009-09-09 15:08:12 +00003658
Eli Friedman47f77112008-08-22 00:56:42 +00003659 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00003660 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3661 "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00003662 unsigned lproto_nargs = lproto->getNumArgs();
3663 unsigned rproto_nargs = rproto->getNumArgs();
3664
3665 // Compatible functions must have the same number of arguments
3666 if (lproto_nargs != rproto_nargs)
3667 return QualType();
3668
3669 // Variadic and non-variadic functions aren't compatible
3670 if (lproto->isVariadic() != rproto->isVariadic())
3671 return QualType();
3672
Argyrios Kyrtzidis22a37352008-10-26 16:43:14 +00003673 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3674 return QualType();
3675
Eli Friedman47f77112008-08-22 00:56:42 +00003676 // Check argument compatibility
3677 llvm::SmallVector<QualType, 10> types;
3678 for (unsigned i = 0; i < lproto_nargs; i++) {
3679 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3680 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3681 QualType argtype = mergeTypes(largtype, rargtype);
3682 if (argtype.isNull()) return QualType();
3683 types.push_back(argtype);
Chris Lattner465fa322008-10-05 17:34:18 +00003684 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3685 allLTypes = false;
3686 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3687 allRTypes = false;
Eli Friedman47f77112008-08-22 00:56:42 +00003688 }
3689 if (allLTypes) return lhs;
3690 if (allRTypes) return rhs;
3691 return getFunctionType(retType, types.begin(), types.size(),
Mike Stump8c5d7992009-07-25 21:26:53 +00003692 lproto->isVariadic(), lproto->getTypeQuals(),
3693 NoReturn);
Eli Friedman47f77112008-08-22 00:56:42 +00003694 }
3695
3696 if (lproto) allRTypes = false;
3697 if (rproto) allLTypes = false;
3698
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003699 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman47f77112008-08-22 00:56:42 +00003700 if (proto) {
Sebastian Redl5068f77ac2009-05-27 22:11:52 +00003701 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman47f77112008-08-22 00:56:42 +00003702 if (proto->isVariadic()) return QualType();
3703 // Check that the types are compatible with the types that
3704 // would result from default argument promotions (C99 6.7.5.3p15).
3705 // The only types actually affected are promotable integer
3706 // types and floats, which would be passed as a different
3707 // type depending on whether the prototype is visible.
3708 unsigned proto_nargs = proto->getNumArgs();
3709 for (unsigned i = 0; i < proto_nargs; ++i) {
3710 QualType argTy = proto->getArgType(i);
3711 if (argTy->isPromotableIntegerType() ||
3712 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3713 return QualType();
3714 }
3715
3716 if (allLTypes) return lhs;
3717 if (allRTypes) return rhs;
3718 return getFunctionType(retType, proto->arg_type_begin(),
Mike Stump21e0f892009-07-27 00:44:23 +00003719 proto->getNumArgs(), proto->isVariadic(),
3720 proto->getTypeQuals(), NoReturn);
Eli Friedman47f77112008-08-22 00:56:42 +00003721 }
3722
3723 if (allLTypes) return lhs;
3724 if (allRTypes) return rhs;
Mike Stump8c5d7992009-07-25 21:26:53 +00003725 return getFunctionNoProtoType(retType, NoReturn);
Eli Friedman47f77112008-08-22 00:56:42 +00003726}
3727
3728QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendlingdb4e3492007-12-03 07:33:35 +00003729 // C++ [expr]: If an expression initially has the type "reference to T", the
3730 // type is adjusted to "T" prior to any further analysis, the expression
3731 // designates the object or function denoted by the reference, and the
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00003732 // expression is an lvalue unless the reference is an rvalue reference and
3733 // the expression is a function call (possibly inside parentheses).
Eli Friedman47f77112008-08-22 00:56:42 +00003734 // FIXME: C++ shouldn't be going through here! The rules are different
3735 // enough that they should be handled separately.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00003736 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3737 // shouldn't be going through here!
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003738 if (const ReferenceType *RT = LHS->getAs<ReferenceType>())
Chris Lattnerddfdaf92008-04-07 04:07:56 +00003739 LHS = RT->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003740 if (const ReferenceType *RT = RHS->getAs<ReferenceType>())
Chris Lattnerddfdaf92008-04-07 04:07:56 +00003741 RHS = RT->getPointeeType();
Chris Lattneraee96c32008-04-07 05:37:56 +00003742
Eli Friedman47f77112008-08-22 00:56:42 +00003743 QualType LHSCan = getCanonicalType(LHS),
3744 RHSCan = getCanonicalType(RHS);
3745
3746 // If two types are identical, they are compatible.
3747 if (LHSCan == RHSCan)
3748 return LHS;
3749
John McCall8ccfcb52009-09-24 19:53:00 +00003750 // If the qualifiers are different, the types aren't compatible... mostly.
3751 Qualifiers LQuals = LHSCan.getQualifiers();
3752 Qualifiers RQuals = RHSCan.getQualifiers();
3753 if (LQuals != RQuals) {
3754 // If any of these qualifiers are different, we have a type
3755 // mismatch.
3756 if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
3757 LQuals.getAddressSpace() != RQuals.getAddressSpace())
3758 return QualType();
3759
3760 // Exactly one GC qualifier difference is allowed: __strong is
3761 // okay if the other type has no GC qualifier but is an Objective
3762 // C object pointer (i.e. implicitly strong by default). We fix
3763 // this by pretending that the unqualified type was actually
3764 // qualified __strong.
3765 Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
3766 Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
3767 assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
3768
3769 if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
3770 return QualType();
3771
3772 if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
3773 return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
3774 }
3775 if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
3776 return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
3777 }
Eli Friedman47f77112008-08-22 00:56:42 +00003778 return QualType();
John McCall8ccfcb52009-09-24 19:53:00 +00003779 }
3780
3781 // Okay, qualifiers are equal.
Eli Friedman47f77112008-08-22 00:56:42 +00003782
Eli Friedmandcca6332009-06-01 01:22:52 +00003783 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3784 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman47f77112008-08-22 00:56:42 +00003785
Chris Lattnerfd652912008-01-14 05:45:46 +00003786 // We want to consider the two function types to be the same for these
3787 // comparisons, just force one to the other.
3788 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3789 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman16f90962008-02-12 08:23:06 +00003790
3791 // Same as above for arrays
Chris Lattner95554662008-04-07 05:43:21 +00003792 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3793 LHSClass = Type::ConstantArray;
3794 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3795 RHSClass = Type::ConstantArray;
Mike Stump11289f42009-09-09 15:08:12 +00003796
Nate Begemance4d7fc2008-04-18 23:10:10 +00003797 // Canonicalize ExtVector -> Vector.
3798 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3799 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Mike Stump11289f42009-09-09 15:08:12 +00003800
Chris Lattner95554662008-04-07 05:43:21 +00003801 // If the canonical type classes don't match.
Chris Lattnerfd652912008-01-14 05:45:46 +00003802 if (LHSClass != RHSClass) {
Chris Lattnerfd652912008-01-14 05:45:46 +00003803 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
Mike Stump11289f42009-09-09 15:08:12 +00003804 // a signed integer type, or an unsigned integer type.
John McCall9dd450b2009-09-21 23:43:11 +00003805 if (const EnumType* ETy = LHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00003806 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3807 return RHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00003808 }
John McCall9dd450b2009-09-21 23:43:11 +00003809 if (const EnumType* ETy = RHS->getAs<EnumType>()) {
Eli Friedman47f77112008-08-22 00:56:42 +00003810 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3811 return LHS;
Eli Friedmana7bf7ed2008-02-12 08:46:17 +00003812 }
Chris Lattnerfd652912008-01-14 05:45:46 +00003813
Eli Friedman47f77112008-08-22 00:56:42 +00003814 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00003815 }
Eli Friedman47f77112008-08-22 00:56:42 +00003816
Steve Naroffc6edcbd2008-01-09 22:43:08 +00003817 // The canonical type classes match.
Chris Lattnerfd652912008-01-14 05:45:46 +00003818 switch (LHSClass) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003819#define TYPE(Class, Base)
3820#define ABSTRACT_TYPE(Class, Base)
3821#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3822#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3823#include "clang/AST/TypeNodes.def"
3824 assert(false && "Non-canonical and dependent types shouldn't get here");
3825 return QualType();
3826
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00003827 case Type::LValueReference:
3828 case Type::RValueReference:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003829 case Type::MemberPointer:
3830 assert(false && "C++ should never be in mergeTypes");
3831 return QualType();
3832
3833 case Type::IncompleteArray:
3834 case Type::VariableArray:
3835 case Type::FunctionProto:
3836 case Type::ExtVector:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003837 assert(false && "Types are eliminated above");
3838 return QualType();
3839
Chris Lattnerfd652912008-01-14 05:45:46 +00003840 case Type::Pointer:
Eli Friedman47f77112008-08-22 00:56:42 +00003841 {
3842 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003843 QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
3844 QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
Eli Friedman47f77112008-08-22 00:56:42 +00003845 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3846 if (ResultType.isNull()) return QualType();
Eli Friedman091a9ac2009-06-02 05:28:56 +00003847 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00003848 return LHS;
Eli Friedman091a9ac2009-06-02 05:28:56 +00003849 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner465fa322008-10-05 17:34:18 +00003850 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00003851 return getPointerType(ResultType);
3852 }
Steve Naroff68e167d2008-12-10 17:49:55 +00003853 case Type::BlockPointer:
3854 {
3855 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003856 QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
3857 QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
Steve Naroff68e167d2008-12-10 17:49:55 +00003858 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3859 if (ResultType.isNull()) return QualType();
3860 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3861 return LHS;
3862 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3863 return RHS;
3864 return getBlockPointerType(ResultType);
3865 }
Chris Lattnerfd652912008-01-14 05:45:46 +00003866 case Type::ConstantArray:
Eli Friedman47f77112008-08-22 00:56:42 +00003867 {
3868 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3869 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3870 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3871 return QualType();
3872
3873 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3874 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3875 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3876 if (ResultType.isNull()) return QualType();
Chris Lattner465fa322008-10-05 17:34:18 +00003877 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3878 return LHS;
3879 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3880 return RHS;
Eli Friedman3e62c212008-08-22 01:48:21 +00003881 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3882 ArrayType::ArraySizeModifier(), 0);
3883 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3884 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00003885 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3886 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner465fa322008-10-05 17:34:18 +00003887 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3888 return LHS;
3889 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3890 return RHS;
Eli Friedman47f77112008-08-22 00:56:42 +00003891 if (LVAT) {
3892 // FIXME: This isn't correct! But tricky to implement because
3893 // the array's size has to be the size of LHS, but the type
3894 // has to be different.
3895 return LHS;
3896 }
3897 if (RVAT) {
3898 // FIXME: This isn't correct! But tricky to implement because
3899 // the array's size has to be the size of RHS, but the type
3900 // has to be different.
3901 return RHS;
3902 }
Eli Friedman3e62c212008-08-22 01:48:21 +00003903 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3904 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor04318252009-07-06 15:59:29 +00003905 return getIncompleteArrayType(ResultType,
3906 ArrayType::ArraySizeModifier(), 0);
Eli Friedman47f77112008-08-22 00:56:42 +00003907 }
Chris Lattnerfd652912008-01-14 05:45:46 +00003908 case Type::FunctionNoProto:
Eli Friedman47f77112008-08-22 00:56:42 +00003909 return mergeFunctionTypes(LHS, RHS);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003910 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003911 case Type::Enum:
Eli Friedman47f77112008-08-22 00:56:42 +00003912 return QualType();
Chris Lattnerfd652912008-01-14 05:45:46 +00003913 case Type::Builtin:
Chris Lattner7bbd3d72008-04-07 05:55:38 +00003914 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman47f77112008-08-22 00:56:42 +00003915 return QualType();
Daniel Dunbar804c0442009-01-28 21:22:12 +00003916 case Type::Complex:
3917 // Distinct complex types are incompatible.
3918 return QualType();
Chris Lattner7bbd3d72008-04-07 05:55:38 +00003919 case Type::Vector:
Eli Friedmancad96382009-02-27 23:04:43 +00003920 // FIXME: The merged type should be an ExtVector!
John McCall9dd450b2009-09-21 23:43:11 +00003921 if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>()))
Eli Friedman47f77112008-08-22 00:56:42 +00003922 return LHS;
Chris Lattner465fa322008-10-05 17:34:18 +00003923 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00003924 case Type::ObjCInterface: {
Steve Naroff7a7814c2009-02-21 16:18:07 +00003925 // Check if the interfaces are assignment compatible.
Eli Friedmancad96382009-02-27 23:04:43 +00003926 // FIXME: This should be type compatibility, e.g. whether
3927 // "LHS x; RHS x;" at global scope is legal.
John McCall9dd450b2009-09-21 23:43:11 +00003928 const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>();
3929 const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>();
Steve Naroff7a7814c2009-02-21 16:18:07 +00003930 if (LHSIface && RHSIface &&
3931 canAssignObjCInterfaces(LHSIface, RHSIface))
3932 return LHS;
3933
Eli Friedman47f77112008-08-22 00:56:42 +00003934 return QualType();
Cedric Venet4fc88b72009-02-21 17:14:49 +00003935 }
Steve Naroff7cae42b2009-07-10 23:34:53 +00003936 case Type::ObjCObjectPointer: {
John McCall9dd450b2009-09-21 23:43:11 +00003937 if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
3938 RHS->getAs<ObjCObjectPointerType>()))
Steve Naroff7cae42b2009-07-10 23:34:53 +00003939 return LHS;
3940
Steve Naroffc68cfcf2008-12-10 22:14:21 +00003941 return QualType();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003942 }
Eli Friedmancad96382009-02-27 23:04:43 +00003943 case Type::FixedWidthInt:
3944 // Distinct fixed-width integers are not compatible.
3945 return QualType();
Douglas Gregordc572a32009-03-30 22:58:21 +00003946 case Type::TemplateSpecialization:
3947 assert(false && "Dependent types have no size");
3948 break;
Steve Naroff32e44c02007-10-15 20:41:53 +00003949 }
Douglas Gregordeaad8c2009-02-26 23:50:07 +00003950
3951 return QualType();
Steve Naroff32e44c02007-10-15 20:41:53 +00003952}
Ted Kremenekfc581a92007-10-31 17:10:13 +00003953
Chris Lattner4ba0cef2008-04-07 07:01:58 +00003954//===----------------------------------------------------------------------===//
Eli Friedman4f89ccb2008-06-28 06:23:08 +00003955// Integer Predicates
3956//===----------------------------------------------------------------------===//
Chris Lattner3c919712009-01-16 07:15:35 +00003957
Eli Friedman4f89ccb2008-06-28 06:23:08 +00003958unsigned ASTContext::getIntWidth(QualType T) {
3959 if (T == BoolTy)
3960 return 1;
Chris Lattnerec3a1562009-10-17 20:33:28 +00003961 if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) {
Eli Friedman1efaaea2009-02-13 02:31:07 +00003962 return FWIT->getWidth();
3963 }
3964 // For builtin types, just use the standard type sizing method
Eli Friedman4f89ccb2008-06-28 06:23:08 +00003965 return (unsigned)getTypeSize(T);
3966}
3967
3968QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3969 assert(T->isSignedIntegerType() && "Unexpected type");
Chris Lattnerec3a1562009-10-17 20:33:28 +00003970
3971 // Turn <4 x signed int> -> <4 x unsigned int>
3972 if (const VectorType *VTy = T->getAs<VectorType>())
3973 return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
3974 VTy->getNumElements());
3975
3976 // For enums, we return the unsigned version of the base type.
3977 if (const EnumType *ETy = T->getAs<EnumType>())
Eli Friedman4f89ccb2008-06-28 06:23:08 +00003978 T = ETy->getDecl()->getIntegerType();
Chris Lattnerec3a1562009-10-17 20:33:28 +00003979
3980 const BuiltinType *BTy = T->getAs<BuiltinType>();
3981 assert(BTy && "Unexpected signed integer type");
Eli Friedman4f89ccb2008-06-28 06:23:08 +00003982 switch (BTy->getKind()) {
3983 case BuiltinType::Char_S:
3984 case BuiltinType::SChar:
3985 return UnsignedCharTy;
3986 case BuiltinType::Short:
3987 return UnsignedShortTy;
3988 case BuiltinType::Int:
3989 return UnsignedIntTy;
3990 case BuiltinType::Long:
3991 return UnsignedLongTy;
3992 case BuiltinType::LongLong:
3993 return UnsignedLongLongTy;
Chris Lattnerf122cef2009-04-30 02:43:43 +00003994 case BuiltinType::Int128:
3995 return UnsignedInt128Ty;
Eli Friedman4f89ccb2008-06-28 06:23:08 +00003996 default:
3997 assert(0 && "Unexpected signed integer type");
3998 return QualType();
3999 }
4000}
4001
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004002ExternalASTSource::~ExternalASTSource() { }
4003
4004void ExternalASTSource::PrintStats() { }
Chris Lattnerecd79c62009-06-14 00:45:47 +00004005
4006
4007//===----------------------------------------------------------------------===//
4008// Builtin Type Computation
4009//===----------------------------------------------------------------------===//
4010
4011/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
4012/// pointer over the consumed characters. This returns the resultant type.
Mike Stump11289f42009-09-09 15:08:12 +00004013static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
Chris Lattnerecd79c62009-06-14 00:45:47 +00004014 ASTContext::GetBuiltinTypeError &Error,
4015 bool AllowTypeModifiers = true) {
4016 // Modifiers.
4017 int HowLong = 0;
4018 bool Signed = false, Unsigned = false;
Mike Stump11289f42009-09-09 15:08:12 +00004019
Chris Lattnerecd79c62009-06-14 00:45:47 +00004020 // Read the modifiers first.
4021 bool Done = false;
4022 while (!Done) {
4023 switch (*Str++) {
Mike Stump11289f42009-09-09 15:08:12 +00004024 default: Done = true; --Str; break;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004025 case 'S':
4026 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
4027 assert(!Signed && "Can't use 'S' modifier multiple times!");
4028 Signed = true;
4029 break;
4030 case 'U':
4031 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
4032 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
4033 Unsigned = true;
4034 break;
4035 case 'L':
4036 assert(HowLong <= 2 && "Can't have LLLL modifier");
4037 ++HowLong;
4038 break;
4039 }
4040 }
4041
4042 QualType Type;
Mike Stump11289f42009-09-09 15:08:12 +00004043
Chris Lattnerecd79c62009-06-14 00:45:47 +00004044 // Read the base type.
4045 switch (*Str++) {
4046 default: assert(0 && "Unknown builtin type letter!");
4047 case 'v':
4048 assert(HowLong == 0 && !Signed && !Unsigned &&
4049 "Bad modifiers used with 'v'!");
4050 Type = Context.VoidTy;
4051 break;
4052 case 'f':
4053 assert(HowLong == 0 && !Signed && !Unsigned &&
4054 "Bad modifiers used with 'f'!");
4055 Type = Context.FloatTy;
4056 break;
4057 case 'd':
4058 assert(HowLong < 2 && !Signed && !Unsigned &&
4059 "Bad modifiers used with 'd'!");
4060 if (HowLong)
4061 Type = Context.LongDoubleTy;
4062 else
4063 Type = Context.DoubleTy;
4064 break;
4065 case 's':
4066 assert(HowLong == 0 && "Bad modifiers used with 's'!");
4067 if (Unsigned)
4068 Type = Context.UnsignedShortTy;
4069 else
4070 Type = Context.ShortTy;
4071 break;
4072 case 'i':
4073 if (HowLong == 3)
4074 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
4075 else if (HowLong == 2)
4076 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
4077 else if (HowLong == 1)
4078 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
4079 else
4080 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
4081 break;
4082 case 'c':
4083 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
4084 if (Signed)
4085 Type = Context.SignedCharTy;
4086 else if (Unsigned)
4087 Type = Context.UnsignedCharTy;
4088 else
4089 Type = Context.CharTy;
4090 break;
4091 case 'b': // boolean
4092 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
4093 Type = Context.BoolTy;
4094 break;
4095 case 'z': // size_t.
4096 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
4097 Type = Context.getSizeType();
4098 break;
4099 case 'F':
4100 Type = Context.getCFConstantStringType();
4101 break;
4102 case 'a':
4103 Type = Context.getBuiltinVaListType();
4104 assert(!Type.isNull() && "builtin va list type not initialized!");
4105 break;
4106 case 'A':
4107 // This is a "reference" to a va_list; however, what exactly
4108 // this means depends on how va_list is defined. There are two
4109 // different kinds of va_list: ones passed by value, and ones
4110 // passed by reference. An example of a by-value va_list is
4111 // x86, where va_list is a char*. An example of by-ref va_list
4112 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
4113 // we want this argument to be a char*&; for x86-64, we want
4114 // it to be a __va_list_tag*.
4115 Type = Context.getBuiltinVaListType();
4116 assert(!Type.isNull() && "builtin va list type not initialized!");
4117 if (Type->isArrayType()) {
4118 Type = Context.getArrayDecayedType(Type);
4119 } else {
4120 Type = Context.getLValueReferenceType(Type);
4121 }
4122 break;
4123 case 'V': {
4124 char *End;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004125 unsigned NumElements = strtoul(Str, &End, 10);
4126 assert(End != Str && "Missing vector size");
Mike Stump11289f42009-09-09 15:08:12 +00004127
Chris Lattnerecd79c62009-06-14 00:45:47 +00004128 Str = End;
Mike Stump11289f42009-09-09 15:08:12 +00004129
Chris Lattnerecd79c62009-06-14 00:45:47 +00004130 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4131 Type = Context.getVectorType(ElementType, NumElements);
4132 break;
4133 }
Douglas Gregor40ef7c52009-09-28 21:45:01 +00004134 case 'X': {
4135 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
4136 Type = Context.getComplexType(ElementType);
4137 break;
4138 }
Chris Lattnera58b3af2009-07-28 22:49:34 +00004139 case 'P':
Douglas Gregor27821ce2009-07-07 16:35:42 +00004140 Type = Context.getFILEType();
4141 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004142 Error = ASTContext::GE_Missing_stdio;
Chris Lattnerecd79c62009-06-14 00:45:47 +00004143 return QualType();
4144 }
Mike Stump2adb4da2009-07-28 23:47:15 +00004145 break;
Chris Lattnera58b3af2009-07-28 22:49:34 +00004146 case 'J':
Mike Stump93246cc2009-07-28 23:57:15 +00004147 if (Signed)
Mike Stumpa4de80b2009-07-28 02:25:19 +00004148 Type = Context.getsigjmp_bufType();
Mike Stump93246cc2009-07-28 23:57:15 +00004149 else
4150 Type = Context.getjmp_bufType();
4151
Mike Stump2adb4da2009-07-28 23:47:15 +00004152 if (Type.isNull()) {
Mike Stump93246cc2009-07-28 23:57:15 +00004153 Error = ASTContext::GE_Missing_setjmp;
Mike Stump2adb4da2009-07-28 23:47:15 +00004154 return QualType();
4155 }
4156 break;
Mike Stumpa4de80b2009-07-28 02:25:19 +00004157 }
Mike Stump11289f42009-09-09 15:08:12 +00004158
Chris Lattnerecd79c62009-06-14 00:45:47 +00004159 if (!AllowTypeModifiers)
4160 return Type;
Mike Stump11289f42009-09-09 15:08:12 +00004161
Chris Lattnerecd79c62009-06-14 00:45:47 +00004162 Done = false;
4163 while (!Done) {
4164 switch (*Str++) {
4165 default: Done = true; --Str; break;
4166 case '*':
4167 Type = Context.getPointerType(Type);
4168 break;
4169 case '&':
4170 Type = Context.getLValueReferenceType(Type);
4171 break;
4172 // FIXME: There's no way to have a built-in with an rvalue ref arg.
4173 case 'C':
John McCall8ccfcb52009-09-24 19:53:00 +00004174 Type = Type.withConst();
Chris Lattnerecd79c62009-06-14 00:45:47 +00004175 break;
4176 }
4177 }
Mike Stump11289f42009-09-09 15:08:12 +00004178
Chris Lattnerecd79c62009-06-14 00:45:47 +00004179 return Type;
4180}
4181
4182/// GetBuiltinType - Return the type for the specified builtin.
4183QualType ASTContext::GetBuiltinType(unsigned id,
4184 GetBuiltinTypeError &Error) {
4185 const char *TypeStr = BuiltinInfo.GetTypeString(id);
Mike Stump11289f42009-09-09 15:08:12 +00004186
Chris Lattnerecd79c62009-06-14 00:45:47 +00004187 llvm::SmallVector<QualType, 8> ArgTypes;
Mike Stump11289f42009-09-09 15:08:12 +00004188
Chris Lattnerecd79c62009-06-14 00:45:47 +00004189 Error = GE_None;
4190 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
4191 if (Error != GE_None)
4192 return QualType();
4193 while (TypeStr[0] && TypeStr[0] != '.') {
4194 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
4195 if (Error != GE_None)
4196 return QualType();
4197
4198 // Do array -> pointer decay. The builtin should use the decayed type.
4199 if (Ty->isArrayType())
4200 Ty = getArrayDecayedType(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00004201
Chris Lattnerecd79c62009-06-14 00:45:47 +00004202 ArgTypes.push_back(Ty);
4203 }
4204
4205 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
4206 "'.' should only occur at end of builtin type list!");
4207
4208 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
4209 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
4210 return getFunctionNoProtoType(ResType);
4211 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
4212 TypeStr[0] == '.', 0);
4213}
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004214
4215QualType
4216ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) {
4217 // Perform the usual unary conversions. We do this early so that
4218 // integral promotions to "int" can allow us to exit early, in the
4219 // lhs == rhs check. Also, for conversion purposes, we ignore any
4220 // qualifiers. For example, "const float" and "float" are
4221 // equivalent.
4222 if (lhs->isPromotableIntegerType())
4223 lhs = getPromotedIntegerType(lhs);
4224 else
4225 lhs = lhs.getUnqualifiedType();
4226 if (rhs->isPromotableIntegerType())
4227 rhs = getPromotedIntegerType(rhs);
4228 else
4229 rhs = rhs.getUnqualifiedType();
4230
4231 // If both types are identical, no conversion is needed.
4232 if (lhs == rhs)
4233 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004234
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004235 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
4236 // The caller can deal with this (e.g. pointer + int).
4237 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
4238 return lhs;
Mike Stump11289f42009-09-09 15:08:12 +00004239
4240 // At this point, we have two different arithmetic types.
4241
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004242 // Handle complex types first (C99 6.3.1.8p1).
4243 if (lhs->isComplexType() || rhs->isComplexType()) {
4244 // if we have an integer operand, the result is the complex type.
Mike Stump11289f42009-09-09 15:08:12 +00004245 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004246 // convert the rhs to the lhs complex type.
4247 return lhs;
4248 }
Mike Stump11289f42009-09-09 15:08:12 +00004249 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004250 // convert the lhs to the rhs complex type.
4251 return rhs;
4252 }
4253 // This handles complex/complex, complex/float, or float/complex.
Mike Stump11289f42009-09-09 15:08:12 +00004254 // When both operands are complex, the shorter operand is converted to the
4255 // type of the longer, and that is the type of the result. This corresponds
4256 // to what is done when combining two real floating-point operands.
4257 // The fun begins when size promotion occur across type domains.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004258 // From H&S 6.3.4: When one operand is complex and the other is a real
Mike Stump11289f42009-09-09 15:08:12 +00004259 // floating-point type, the less precise type is converted, within it's
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004260 // real or complex domain, to the precision of the other type. For example,
Mike Stump11289f42009-09-09 15:08:12 +00004261 // when combining a "long double" with a "double _Complex", the
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004262 // "double _Complex" is promoted to "long double _Complex".
4263 int result = getFloatingTypeOrder(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004264
4265 if (result > 0) { // The left side is bigger, convert rhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004266 rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs);
Mike Stump11289f42009-09-09 15:08:12 +00004267 } else if (result < 0) { // The right side is bigger, convert lhs.
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004268 lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs);
Mike Stump11289f42009-09-09 15:08:12 +00004269 }
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004270 // At this point, lhs and rhs have the same rank/size. Now, make sure the
4271 // domains match. This is a requirement for our implementation, C99
4272 // does not require this promotion.
4273 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
4274 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
4275 return rhs;
4276 } else { // handle "_Complex double, double".
4277 return lhs;
4278 }
4279 }
4280 return lhs; // The domain/size match exactly.
4281 }
4282 // Now handle "real" floating types (i.e. float, double, long double).
4283 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
4284 // if we have an integer operand, the result is the real floating type.
4285 if (rhs->isIntegerType()) {
4286 // convert rhs to the lhs floating point type.
4287 return lhs;
4288 }
4289 if (rhs->isComplexIntegerType()) {
4290 // convert rhs to the complex floating point type.
4291 return getComplexType(lhs);
4292 }
4293 if (lhs->isIntegerType()) {
4294 // convert lhs to the rhs floating point type.
4295 return rhs;
4296 }
Mike Stump11289f42009-09-09 15:08:12 +00004297 if (lhs->isComplexIntegerType()) {
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004298 // convert lhs to the complex floating point type.
4299 return getComplexType(rhs);
4300 }
4301 // We have two real floating types, float/complex combos were handled above.
4302 // Convert the smaller operand to the bigger result.
4303 int result = getFloatingTypeOrder(lhs, rhs);
4304 if (result > 0) // convert the rhs
4305 return lhs;
4306 assert(result < 0 && "illegal float comparison");
4307 return rhs; // convert the lhs
4308 }
4309 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
4310 // Handle GCC complex int extension.
4311 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
4312 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
4313
4314 if (lhsComplexInt && rhsComplexInt) {
Mike Stump11289f42009-09-09 15:08:12 +00004315 if (getIntegerTypeOrder(lhsComplexInt->getElementType(),
Eli Friedman5ae98ee2009-08-19 07:44:53 +00004316 rhsComplexInt->getElementType()) >= 0)
4317 return lhs; // convert the rhs
4318 return rhs;
4319 } else if (lhsComplexInt && rhs->isIntegerType()) {
4320 // convert the rhs to the lhs complex type.
4321 return lhs;
4322 } else if (rhsComplexInt && lhs->isIntegerType()) {
4323 // convert the lhs to the rhs complex type.
4324 return rhs;
4325 }
4326 }
4327 // Finally, we have two differing integer types.
4328 // The rules for this case are in C99 6.3.1.8
4329 int compare = getIntegerTypeOrder(lhs, rhs);
4330 bool lhsSigned = lhs->isSignedIntegerType(),
4331 rhsSigned = rhs->isSignedIntegerType();
4332 QualType destType;
4333 if (lhsSigned == rhsSigned) {
4334 // Same signedness; use the higher-ranked type
4335 destType = compare >= 0 ? lhs : rhs;
4336 } else if (compare != (lhsSigned ? 1 : -1)) {
4337 // The unsigned type has greater than or equal rank to the
4338 // signed type, so use the unsigned type
4339 destType = lhsSigned ? rhs : lhs;
4340 } else if (getIntWidth(lhs) != getIntWidth(rhs)) {
4341 // The two types are different widths; if we are here, that
4342 // means the signed type is larger than the unsigned type, so
4343 // use the signed type.
4344 destType = lhsSigned ? lhs : rhs;
4345 } else {
4346 // The signed type is higher-ranked than the unsigned type,
4347 // but isn't actually any bigger (like unsigned int and long
4348 // on most 32-bit systems). Use the unsigned type corresponding
4349 // to the signed type.
4350 destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs);
4351 }
4352 return destType;
4353}