blob: 5572b7a3a0948bc8f40e92c4ca504612d4d6c441 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Anders Carlsson63f1ad92009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000022#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000024#include "llvm/ADT/StringExtras.h"
Nate Begeman7903d052009-01-18 06:42:49 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000026#include "llvm/Support/MemoryBuffer.h"
Anders Carlsson5d382582009-07-18 21:19:52 +000027#include "RecordLayoutBuilder.h"
28
Chris Lattner4b009652007-07-25 00:24:17 +000029using namespace clang;
30
31enum FloatingRank {
32 FloatRank, DoubleRank, LongDoubleRank
33};
34
Chris Lattner2fda0ed2008-10-05 17:34:18 +000035ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
36 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000037 IdentifierTable &idents, SelectorTable &sels,
Chris Lattnerc46fcdd2009-06-14 01:54:56 +000038 Builtin::Context &builtins,
39 bool FreeMem, unsigned size_reserve) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000040 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Douglas Gregor151fac72009-07-07 16:35:42 +000041 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0),
42 SourceMgr(SM), LangOpts(LOpts),
Douglas Gregora252b232009-07-02 17:08:52 +000043 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
44 Idents(idents), Selectors(sels),
Chris Lattner7099c782009-06-30 01:26:17 +000045 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
Daniel Dunbarde300732008-08-11 04:54:23 +000046 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbarde300732008-08-11 04:54:23 +000047 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff329ec222009-07-10 23:34:53 +000048 InitBuiltinTypes();
Daniel Dunbarde300732008-08-11 04:54:23 +000049}
50
Chris Lattner4b009652007-07-25 00:24:17 +000051ASTContext::~ASTContext() {
52 // Deallocate all the types.
53 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000054 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000055 Types.pop_back();
56 }
Eli Friedman65489b72008-05-27 03:08:09 +000057
Nuno Lopes355a8682008-12-17 22:30:25 +000058 {
59 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
60 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
61 while (I != E) {
62 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
63 delete R;
64 }
65 }
66
67 {
Daniel Dunbar1fbaef12009-05-03 10:38:35 +000068 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
69 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopes355a8682008-12-17 22:30:25 +000070 while (I != E) {
71 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
72 delete R;
73 }
74 }
75
Douglas Gregor1e589cc2009-03-26 23:50:42 +000076 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000077 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
78 NNS = NestedNameSpecifiers.begin(),
79 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000080 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000081 /* Increment in loop */)
82 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000083
84 if (GlobalNestedNameSpecifier)
85 GlobalNestedNameSpecifier->Destroy(*this);
86
Eli Friedman65489b72008-05-27 03:08:09 +000087 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000088}
89
Douglas Gregorc34897d2009-04-09 22:27:44 +000090void
91ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
92 ExternalSource.reset(Source.take());
93}
94
Chris Lattner4b009652007-07-25 00:24:17 +000095void ASTContext::PrintStats() const {
96 fprintf(stderr, "*** AST Context Stats:\n");
97 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redlce6fff02009-03-16 23:22:08 +000098
Douglas Gregore6609442009-05-26 14:40:08 +000099 unsigned counts[] = {
100#define TYPE(Name, Parent) 0,
101#define ABSTRACT_TYPE(Name, Parent)
102#include "clang/AST/TypeNodes.def"
103 0 // Extra
104 };
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000105
Chris Lattner4b009652007-07-25 00:24:17 +0000106 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
107 Type *T = Types[i];
Douglas Gregore6609442009-05-26 14:40:08 +0000108 counts[(unsigned)T->getTypeClass()]++;
Chris Lattner4b009652007-07-25 00:24:17 +0000109 }
110
Douglas Gregore6609442009-05-26 14:40:08 +0000111 unsigned Idx = 0;
112 unsigned TotalBytes = 0;
113#define TYPE(Name, Parent) \
114 if (counts[Idx]) \
115 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
116 TotalBytes += counts[Idx] * sizeof(Name##Type); \
117 ++Idx;
118#define ABSTRACT_TYPE(Name, Parent)
119#include "clang/AST/TypeNodes.def"
120
121 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000122
123 if (ExternalSource.get()) {
124 fprintf(stderr, "\n");
125 ExternalSource->PrintStats();
126 }
Chris Lattner4b009652007-07-25 00:24:17 +0000127}
128
129
130void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000131 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000132}
133
Chris Lattner4b009652007-07-25 00:24:17 +0000134void ASTContext::InitBuiltinTypes() {
135 assert(VoidTy.isNull() && "Context reinitialized?");
136
137 // C99 6.2.5p19.
138 InitBuiltinType(VoidTy, BuiltinType::Void);
139
140 // C99 6.2.5p2.
141 InitBuiltinType(BoolTy, BuiltinType::Bool);
142 // C99 6.2.5p3.
Eli Friedmand9389be2009-06-05 07:05:05 +0000143 if (LangOpts.CharIsSigned)
Chris Lattner4b009652007-07-25 00:24:17 +0000144 InitBuiltinType(CharTy, BuiltinType::Char_S);
145 else
146 InitBuiltinType(CharTy, BuiltinType::Char_U);
147 // C99 6.2.5p4.
148 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
149 InitBuiltinType(ShortTy, BuiltinType::Short);
150 InitBuiltinType(IntTy, BuiltinType::Int);
151 InitBuiltinType(LongTy, BuiltinType::Long);
152 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
153
154 // C99 6.2.5p6.
155 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
156 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
157 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
158 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
159 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
160
161 // C99 6.2.5p10.
162 InitBuiltinType(FloatTy, BuiltinType::Float);
163 InitBuiltinType(DoubleTy, BuiltinType::Double);
164 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000165
Chris Lattner6cc7e412009-04-30 02:43:43 +0000166 // GNU extension, 128-bit integers.
167 InitBuiltinType(Int128Ty, BuiltinType::Int128);
168 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
169
Chris Lattnere1dafe72009-02-26 23:43:47 +0000170 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
171 InitBuiltinType(WCharTy, BuiltinType::WChar);
172 else // C99
173 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000174
Alisdair Meredith2bcacb62009-07-14 06:30:34 +0000175 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
176 InitBuiltinType(Char16Ty, BuiltinType::Char16);
177 else // C99
178 Char16Ty = getFromTargetType(Target.getChar16Type());
179
180 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
181 InitBuiltinType(Char32Ty, BuiltinType::Char32);
182 else // C99
183 Char32Ty = getFromTargetType(Target.getChar32Type());
184
Douglas Gregord2baafd2008-10-21 16:13:35 +0000185 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000186 InitBuiltinType(OverloadTy, BuiltinType::Overload);
187
188 // Placeholder type for type-dependent expressions whose type is
189 // completely unknown. No code should ever check a type against
190 // DependentTy and users should never see it; however, it is here to
191 // help diagnose failures to properly check for type-dependent
192 // expressions.
193 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000194
Anders Carlsson4a8498c2009-06-26 18:41:36 +0000195 // Placeholder type for C++0x auto declarations whose real type has
196 // not yet been deduced.
197 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
198
Chris Lattner4b009652007-07-25 00:24:17 +0000199 // C99 6.2.5p11.
200 FloatComplexTy = getComplexType(FloatTy);
201 DoubleComplexTy = getComplexType(DoubleTy);
202 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000203
Steve Naroff9d12c902007-10-15 14:41:52 +0000204 BuiltinVaListType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000205
Steve Naroff7bffd372009-07-15 18:40:39 +0000206 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
207 ObjCIdTypedefType = QualType();
208 ObjCClassTypedefType = QualType();
209
210 // Builtin types for 'id' and 'Class'.
211 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
212 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff329ec222009-07-10 23:34:53 +0000213
Ted Kremenek42730c52008-01-07 19:49:32 +0000214 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000215
216 // void * type
217 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl5d0ead72009-05-10 18:38:11 +0000218
219 // nullptr type (C++0x 2.14.7)
220 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Chris Lattner4b009652007-07-25 00:24:17 +0000221}
222
Douglas Gregora252b232009-07-02 17:08:52 +0000223namespace {
224 class BeforeInTranslationUnit
225 : std::binary_function<SourceRange, SourceRange, bool> {
226 SourceManager *SourceMgr;
227
228 public:
229 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
230
231 bool operator()(SourceRange X, SourceRange Y) {
232 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
233 }
234 };
235}
236
237/// \brief Determine whether the given comment is a Doxygen-style comment.
238///
239/// \param Start the start of the comment text.
240///
241/// \param End the end of the comment text.
242///
243/// \param Member whether we want to check whether this is a member comment
244/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
245/// we only return true when we find a non-member comment.
246static bool
247isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
248 bool Member = false) {
249 const char *BufferStart
250 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
251 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
252 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
253
254 if (End - Start < 4)
255 return false;
256
257 assert(Start[0] == '/' && "Not a comment?");
258 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
259 return false;
260 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
261 return false;
262
263 return (Start[3] == '<') == Member;
264}
265
266/// \brief Retrieve the comment associated with the given declaration, if
267/// it has one.
268const char *ASTContext::getCommentForDecl(const Decl *D) {
269 if (!D)
270 return 0;
271
272 // Check whether we have cached a comment string for this declaration
273 // already.
274 llvm::DenseMap<const Decl *, std::string>::iterator Pos
275 = DeclComments.find(D);
276 if (Pos != DeclComments.end())
277 return Pos->second.c_str();
278
279 // If we have an external AST source and have not yet loaded comments from
280 // that source, do so now.
281 if (ExternalSource && !LoadedExternalComments) {
282 std::vector<SourceRange> LoadedComments;
283 ExternalSource->ReadComments(LoadedComments);
284
285 if (!LoadedComments.empty())
286 Comments.insert(Comments.begin(), LoadedComments.begin(),
287 LoadedComments.end());
288
289 LoadedExternalComments = true;
290 }
291
292 // If there are no comments anywhere, we won't find anything.
293 if (Comments.empty())
294 return 0;
295
296 // If the declaration doesn't map directly to a location in a file, we
297 // can't find the comment.
298 SourceLocation DeclStartLoc = D->getLocStart();
299 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
300 return 0;
301
302 // Find the comment that occurs just before this declaration.
303 std::vector<SourceRange>::iterator LastComment
304 = std::lower_bound(Comments.begin(), Comments.end(),
305 SourceRange(DeclStartLoc),
306 BeforeInTranslationUnit(&SourceMgr));
307
308 // Decompose the location for the start of the declaration and find the
309 // beginning of the file buffer.
310 std::pair<FileID, unsigned> DeclStartDecomp
311 = SourceMgr.getDecomposedLoc(DeclStartLoc);
312 const char *FileBufferStart
313 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
314
315 // First check whether we have a comment for a member.
316 if (LastComment != Comments.end() &&
317 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
318 isDoxygenComment(SourceMgr, *LastComment, true)) {
319 std::pair<FileID, unsigned> LastCommentEndDecomp
320 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
321 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
322 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
323 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
324 LastCommentEndDecomp.second)) {
325 // The Doxygen member comment comes after the declaration starts and
326 // is on the same line and in the same file as the declaration. This
327 // is the comment we want.
328 std::string &Result = DeclComments[D];
329 Result.append(FileBufferStart +
330 SourceMgr.getFileOffset(LastComment->getBegin()),
331 FileBufferStart + LastCommentEndDecomp.second + 1);
332 return Result.c_str();
333 }
334 }
335
336 if (LastComment == Comments.begin())
337 return 0;
338 --LastComment;
339
340 // Decompose the end of the comment.
341 std::pair<FileID, unsigned> LastCommentEndDecomp
342 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
343
344 // If the comment and the declaration aren't in the same file, then they
345 // aren't related.
346 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
347 return 0;
348
349 // Check that we actually have a Doxygen comment.
350 if (!isDoxygenComment(SourceMgr, *LastComment))
351 return 0;
352
353 // Compute the starting line for the declaration and for the end of the
354 // comment (this is expensive).
355 unsigned DeclStartLine
356 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
357 unsigned CommentEndLine
358 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
359 LastCommentEndDecomp.second);
360
361 // If the comment does not end on the line prior to the declaration, then
362 // the comment is not associated with the declaration at all.
363 if (CommentEndLine + 1 != DeclStartLine)
364 return 0;
365
366 // We have a comment, but there may be more comments on the previous lines.
367 // Keep looking so long as the comments are still Doxygen comments and are
368 // still adjacent.
369 unsigned ExpectedLine
370 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
371 std::vector<SourceRange>::iterator FirstComment = LastComment;
372 while (FirstComment != Comments.begin()) {
373 // Look at the previous comment
374 --FirstComment;
375 std::pair<FileID, unsigned> Decomp
376 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
377
378 // If this previous comment is in a different file, we're done.
379 if (Decomp.first != DeclStartDecomp.first) {
380 ++FirstComment;
381 break;
382 }
383
384 // If this comment is not a Doxygen comment, we're done.
385 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
386 ++FirstComment;
387 break;
388 }
389
390 // If the line number is not what we expected, we're done.
391 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
392 if (Line != ExpectedLine) {
393 ++FirstComment;
394 break;
395 }
396
397 // Set the next expected line number.
398 ExpectedLine
399 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
400 }
401
402 // The iterator range [FirstComment, LastComment] contains all of the
403 // BCPL comments that, together, are associated with this declaration.
404 // Form a single comment block string for this declaration that concatenates
405 // all of these comments.
406 std::string &Result = DeclComments[D];
407 while (FirstComment != LastComment) {
408 std::pair<FileID, unsigned> DecompStart
409 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
410 std::pair<FileID, unsigned> DecompEnd
411 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
412 Result.append(FileBufferStart + DecompStart.second,
413 FileBufferStart + DecompEnd.second + 1);
414 ++FirstComment;
415 }
416
417 // Append the last comment line.
418 Result.append(FileBufferStart +
419 SourceMgr.getFileOffset(LastComment->getBegin()),
420 FileBufferStart + LastCommentEndDecomp.second + 1);
421 return Result.c_str();
422}
423
Chris Lattner4b009652007-07-25 00:24:17 +0000424//===----------------------------------------------------------------------===//
425// Type Sizing and Analysis
426//===----------------------------------------------------------------------===//
427
Chris Lattner2a674dc2008-06-30 18:32:54 +0000428/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
429/// scalar floating point type.
430const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
431 const BuiltinType *BT = T->getAsBuiltinType();
432 assert(BT && "Not a floating point type!");
433 switch (BT->getKind()) {
434 default: assert(0 && "Not a floating point type!");
435 case BuiltinType::Float: return Target.getFloatFormat();
436 case BuiltinType::Double: return Target.getDoubleFormat();
437 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
438 }
439}
440
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000441/// getDeclAlign - Return a conservative estimate of the alignment of the
442/// specified decl. Note that bitfields do not have a valid alignment, so
443/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000444unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000445 unsigned Align = Target.getCharWidth();
446
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000447 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedman0ee57322009-02-22 02:56:25 +0000448 Align = std::max(Align, AA->getAlignment());
449
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000450 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
451 QualType T = VD->getType();
Ted Kremenekd9b39bf2009-07-17 17:50:17 +0000452 if (const ReferenceType* RT = T->getAsReferenceType()) {
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000453 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000454 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000455 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
456 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000457 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
458 T = cast<ArrayType>(T)->getElementType();
459
460 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
461 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000462 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000463
464 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000465}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000466
Chris Lattner4b009652007-07-25 00:24:17 +0000467/// getTypeSize - Return the size of the specified type, in bits. This method
468/// does not work on incomplete types.
469std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000470ASTContext::getTypeInfo(const Type *T) {
Mike Stump44d1f402009-02-27 18:32:39 +0000471 uint64_t Width=0;
472 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000473 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000474#define TYPE(Class, Base)
475#define ABSTRACT_TYPE(Class, Base)
Douglas Gregorab380272009-04-30 17:32:17 +0000476#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor4fa58902009-02-26 23:50:07 +0000477#define DEPENDENT_TYPE(Class, Base) case Type::Class:
478#include "clang/AST/TypeNodes.def"
Douglas Gregorab380272009-04-30 17:32:17 +0000479 assert(false && "Should not see dependent types");
Douglas Gregor4fa58902009-02-26 23:50:07 +0000480 break;
481
Chris Lattner4b009652007-07-25 00:24:17 +0000482 case Type::FunctionNoProto:
483 case Type::FunctionProto:
Douglas Gregorab380272009-04-30 17:32:17 +0000484 // GCC extension: alignof(function) = 32 bits
485 Width = 0;
486 Align = 32;
487 break;
488
Douglas Gregor4fa58902009-02-26 23:50:07 +0000489 case Type::IncompleteArray:
Steve Naroff83c13012007-08-30 01:06:46 +0000490 case Type::VariableArray:
Douglas Gregorab380272009-04-30 17:32:17 +0000491 Width = 0;
492 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
493 break;
494
Douglas Gregor1d381132009-07-06 15:59:29 +0000495 case Type::ConstantArrayWithExpr:
496 case Type::ConstantArrayWithoutExpr:
Steve Naroff83c13012007-08-30 01:06:46 +0000497 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000498 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000499
Chris Lattner8cd0e932008-03-05 18:54:05 +0000500 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000501 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000502 Align = EltInfo.second;
503 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000504 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000505 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000506 case Type::Vector: {
507 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000508 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000509 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000510 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000511 // If the alignment is not a power of 2, round up to the next power of 2.
512 // This happens for non-power-of-2 length vectors.
513 // FIXME: this should probably be a target property.
514 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000515 break;
516 }
517
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000518 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000519 switch (cast<BuiltinType>(T)->getKind()) {
520 default: assert(0 && "Unknown builtin type!");
521 case BuiltinType::Void:
Douglas Gregorab380272009-04-30 17:32:17 +0000522 // GCC extension: alignof(void) = 8 bits.
523 Width = 0;
524 Align = 8;
525 break;
526
Chris Lattnerb66237b2007-12-19 19:23:28 +0000527 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000528 Width = Target.getBoolWidth();
529 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000530 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000531 case BuiltinType::Char_S:
532 case BuiltinType::Char_U:
533 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000534 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000535 Width = Target.getCharWidth();
536 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000537 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000538 case BuiltinType::WChar:
539 Width = Target.getWCharWidth();
540 Align = Target.getWCharAlign();
541 break;
Alisdair Meredith2bcacb62009-07-14 06:30:34 +0000542 case BuiltinType::Char16:
543 Width = Target.getChar16Width();
544 Align = Target.getChar16Align();
545 break;
546 case BuiltinType::Char32:
547 Width = Target.getChar32Width();
548 Align = Target.getChar32Align();
549 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000550 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000551 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000552 Width = Target.getShortWidth();
553 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000554 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000555 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000556 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000557 Width = Target.getIntWidth();
558 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000559 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000560 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000561 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000562 Width = Target.getLongWidth();
563 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000564 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000565 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000566 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000567 Width = Target.getLongLongWidth();
568 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000569 break;
Chris Lattner4b11cc22009-04-30 02:55:13 +0000570 case BuiltinType::Int128:
571 case BuiltinType::UInt128:
572 Width = 128;
573 Align = 128; // int128_t is 128-bit aligned on all targets.
574 break;
Chris Lattnerb66237b2007-12-19 19:23:28 +0000575 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000576 Width = Target.getFloatWidth();
577 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000578 break;
579 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000580 Width = Target.getDoubleWidth();
581 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000582 break;
583 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000584 Width = Target.getLongDoubleWidth();
585 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000586 break;
Sebastian Redl5d0ead72009-05-10 18:38:11 +0000587 case BuiltinType::NullPtr:
588 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
589 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redlc4cce782009-05-27 19:34:06 +0000590 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000591 }
592 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000593 case Type::FixedWidthInt:
594 // FIXME: This isn't precisely correct; the width/alignment should depend
595 // on the available types for the target
596 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000597 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000598 Align = Width;
599 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000600 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000601 // FIXME: Pointers into different addr spaces could have different sizes and
602 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000603 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Steve Naroffc75c1a82009-06-17 22:40:22 +0000604 case Type::ObjCObjectPointer:
Chris Lattner1d78a862008-04-07 07:01:58 +0000605 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000606 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000607 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000608 case Type::BlockPointer: {
609 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
610 Width = Target.getPointerWidth(AS);
611 Align = Target.getPointerAlign(AS);
612 break;
613 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000614 case Type::Pointer: {
615 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000616 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000617 Align = Target.getPointerAlign(AS);
618 break;
619 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000620 case Type::LValueReference:
621 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000622 // "When applied to a reference or a reference type, the result is the size
623 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000624 // FIXME: This is wrong for struct layout: a reference in a struct has
625 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000626 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000627 case Type::MemberPointer: {
Anders Carlsson86cf4ac2009-05-17 02:06:04 +0000628 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
629 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
630 // If we ever want to support other ABIs this needs to be abstracted.
631
Sebastian Redl75555032009-01-24 21:16:55 +0000632 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson86cf4ac2009-05-17 02:06:04 +0000633 std::pair<uint64_t, unsigned> PtrDiffInfo =
634 getTypeInfo(getPointerDiffType());
635 Width = PtrDiffInfo.first;
Sebastian Redl75555032009-01-24 21:16:55 +0000636 if (Pointee->isFunctionType())
637 Width *= 2;
Anders Carlsson86cf4ac2009-05-17 02:06:04 +0000638 Align = PtrDiffInfo.second;
639 break;
Sebastian Redl75555032009-01-24 21:16:55 +0000640 }
Chris Lattner4b009652007-07-25 00:24:17 +0000641 case Type::Complex: {
642 // Complex types have the same alignment as their elements, but twice the
643 // size.
644 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000645 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000646 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000647 Align = EltInfo.second;
648 break;
649 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000650 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000651 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000652 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
653 Width = Layout.getSize();
654 Align = Layout.getAlignment();
655 break;
656 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000657 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000658 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000659 const TagType *TT = cast<TagType>(T);
660
661 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000662 Width = 1;
663 Align = 1;
664 break;
665 }
666
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000667 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000668 return getTypeInfo(ET->getDecl()->getIntegerType());
669
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000670 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000671 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
672 Width = Layout.getSize();
673 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000674 break;
675 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000676
Douglas Gregorab380272009-04-30 17:32:17 +0000677 case Type::Typedef: {
678 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000679 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregorab380272009-04-30 17:32:17 +0000680 Align = Aligned->getAlignment();
681 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
682 } else
683 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordd13e842009-03-30 22:58:21 +0000684 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000685 }
Douglas Gregorab380272009-04-30 17:32:17 +0000686
687 case Type::TypeOfExpr:
688 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
689 .getTypePtr());
690
691 case Type::TypeOf:
692 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
693
Anders Carlsson93ab5332009-06-24 19:06:50 +0000694 case Type::Decltype:
695 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
696 .getTypePtr());
697
Douglas Gregorab380272009-04-30 17:32:17 +0000698 case Type::QualifiedName:
699 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
700
701 case Type::TemplateSpecialization:
702 assert(getCanonicalType(T) != T &&
703 "Cannot request the size of a dependent type");
704 // FIXME: this is likely to be wrong once we support template
705 // aliases, since a template alias could refer to a typedef that
706 // has an __aligned__ attribute on it.
707 return getTypeInfo(getCanonicalType(T));
708 }
Chris Lattner4b009652007-07-25 00:24:17 +0000709
710 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000711 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000712}
713
Chris Lattner83165b52009-01-27 18:08:34 +0000714/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
715/// type for the current target in bits. This can be different than the ABI
716/// alignment in cases where it is beneficial for performance to overalign
717/// a data type.
718unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
719 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman66c9edf2009-05-25 21:27:19 +0000720
721 // Double and long long should be naturally aligned if possible.
722 if (const ComplexType* CT = T->getAsComplexType())
723 T = CT->getElementType().getTypePtr();
724 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
725 T->isSpecificBuiltinType(BuiltinType::LongLong))
726 return std::max(ABIAlign, (unsigned)getTypeSize(T));
727
Chris Lattner83165b52009-01-27 18:08:34 +0000728 return ABIAlign;
729}
730
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000731static void CollectLocalObjCIvars(ASTContext *Ctx,
732 const ObjCInterfaceDecl *OI,
733 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000734 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
735 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000736 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000737 if (!IVDecl->isInvalidDecl())
738 Fields.push_back(cast<FieldDecl>(IVDecl));
739 }
740}
741
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000742void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
743 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
744 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
745 CollectObjCIvars(SuperClass, Fields);
746 CollectLocalObjCIvars(this, OI, Fields);
747}
748
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000749/// ShallowCollectObjCIvars -
750/// Collect all ivars, including those synthesized, in the current class.
751///
752void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
753 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
754 bool CollectSynthesized) {
755 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
756 E = OI->ivar_end(); I != E; ++I) {
757 Ivars.push_back(*I);
758 }
759 if (CollectSynthesized)
760 CollectSynthesizedIvars(OI, Ivars);
761}
762
Fariborz Jahanian02ebfa82009-05-12 18:14:29 +0000763void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
764 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000765 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
766 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian02ebfa82009-05-12 18:14:29 +0000767 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
768 Ivars.push_back(Ivar);
769
770 // Also look into nested protocols.
771 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
772 E = PD->protocol_end(); P != E; ++P)
773 CollectProtocolSynthesizedIvars(*P, Ivars);
774}
775
776/// CollectSynthesizedIvars -
777/// This routine collect synthesized ivars for the designated class.
778///
779void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
780 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000781 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
782 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian02ebfa82009-05-12 18:14:29 +0000783 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
784 Ivars.push_back(Ivar);
785 }
786 // Also look into interface's protocol list for properties declared
787 // in the protocol and whose ivars are synthesized.
788 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
789 PE = OI->protocol_end(); P != PE; ++P) {
790 ObjCProtocolDecl *PD = (*P);
791 CollectProtocolSynthesizedIvars(PD, Ivars);
792 }
793}
794
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000795unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
796 unsigned count = 0;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000797 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
798 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000799 if ((*I)->getPropertyIvarDecl())
800 ++count;
801
802 // Also look into nested protocols.
803 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
804 E = PD->protocol_end(); P != E; ++P)
805 count += CountProtocolSynthesizedIvars(*P);
806 return count;
807}
808
809unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI)
810{
811 unsigned count = 0;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000812 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
813 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000814 if ((*I)->getPropertyIvarDecl())
815 ++count;
816 }
817 // Also look into interface's protocol list for properties declared
818 // in the protocol and whose ivars are synthesized.
819 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
820 PE = OI->protocol_end(); P != PE; ++P) {
821 ObjCProtocolDecl *PD = (*P);
822 count += CountProtocolSynthesizedIvars(PD);
823 }
824 return count;
825}
826
Argiris Kirtzidis3a4d9832009-07-21 00:05:53 +0000827/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
828ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
829 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
830 I = ObjCImpls.find(D);
831 if (I != ObjCImpls.end())
832 return cast<ObjCImplementationDecl>(I->second);
833 return 0;
834}
835/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
836ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
837 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
838 I = ObjCImpls.find(D);
839 if (I != ObjCImpls.end())
840 return cast<ObjCCategoryImplDecl>(I->second);
841 return 0;
842}
843
844/// \brief Set the implementation of ObjCInterfaceDecl.
845void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
846 ObjCImplementationDecl *ImplD) {
847 assert(IFaceD && ImplD && "Passed null params");
848 ObjCImpls[IFaceD] = ImplD;
849}
850/// \brief Set the implementation of ObjCCategoryDecl.
851void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
852 ObjCCategoryImplDecl *ImplD) {
853 assert(CatD && ImplD && "Passed null params");
854 ObjCImpls[CatD] = ImplD;
855}
856
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000857/// getInterfaceLayoutImpl - Get or compute information about the
858/// layout of the given interface.
859///
860/// \param Impl - If given, also include the layout of the interface's
861/// implementation. This may differ by including synthesized ivars.
Devang Patel4b6bf702008-06-04 21:54:36 +0000862const ASTRecordLayout &
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000863ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
864 const ObjCImplementationDecl *Impl) {
Daniel Dunbar94d2ede2009-05-03 13:15:50 +0000865 assert(!D->isForwardDecl() && "Invalid interface decl!");
866
Devang Patel4b6bf702008-06-04 21:54:36 +0000867 // Look up this layout, if already laid out, return what we have.
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000868 ObjCContainerDecl *Key =
869 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
870 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
871 return *Entry;
Devang Patel4b6bf702008-06-04 21:54:36 +0000872
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000873 // Add in synthesized ivar count if laying out an implementation.
874 if (Impl) {
Anders Carlsson5d382582009-07-18 21:19:52 +0000875 unsigned FieldCount = D->ivar_size();
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000876 unsigned SynthCount = CountSynthesizedIvars(D);
877 FieldCount += SynthCount;
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000878 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000879 // entry. Note we can't cache this because we simply free all
880 // entries later; however we shouldn't look up implementations
881 // frequently.
Fariborz Jahanianb290be02009-06-04 01:19:09 +0000882 if (SynthCount == 0)
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000883 return getObjCLayout(D, 0);
884 }
885
Anders Carlsson5d382582009-07-18 21:19:52 +0000886 const ASTRecordLayout *NewEntry =
887 ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl);
888 ObjCLayouts[Key] = NewEntry;
889
Devang Patel4b6bf702008-06-04 21:54:36 +0000890 return *NewEntry;
891}
892
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000893const ASTRecordLayout &
894ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
895 return getObjCLayout(D, 0);
896}
897
898const ASTRecordLayout &
899ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
900 return getObjCLayout(D->getClassInterface(), D);
901}
902
Devang Patel7a78e432007-11-01 19:11:01 +0000903/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000904/// specified record (struct/union/class), which indicates its size and field
905/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000906const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000907 D = D->getDefinition(*this);
908 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000909
Chris Lattner4b009652007-07-25 00:24:17 +0000910 // Look up this layout, if already laid out, return what we have.
Eli Friedman774cb992009-07-22 20:29:16 +0000911 // Note that we can't save a reference to the entry because this function
912 // is recursive.
913 const ASTRecordLayout *Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000914 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000915
Anders Carlsson5d382582009-07-18 21:19:52 +0000916 const ASTRecordLayout *NewEntry =
917 ASTRecordLayoutBuilder::ComputeLayout(*this, D);
Eli Friedman774cb992009-07-22 20:29:16 +0000918 ASTRecordLayouts[D] = NewEntry;
Anders Carlsson5d382582009-07-18 21:19:52 +0000919
Chris Lattner4b009652007-07-25 00:24:17 +0000920 return *NewEntry;
921}
922
Chris Lattner4b009652007-07-25 00:24:17 +0000923//===----------------------------------------------------------------------===//
924// Type creation/memoization methods
925//===----------------------------------------------------------------------===//
926
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000927QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000928 QualType CanT = getCanonicalType(T);
929 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000930 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000931
932 // If we are composing extended qualifiers together, merge together into one
933 // ExtQualType node.
934 unsigned CVRQuals = T.getCVRQualifiers();
935 QualType::GCAttrTypes GCAttr = QualType::GCNone;
936 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000937
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000938 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
939 // If this type already has an address space specified, it cannot get
940 // another one.
941 assert(EQT->getAddressSpace() == 0 &&
942 "Type cannot be in multiple addr spaces!");
943 GCAttr = EQT->getObjCGCAttr();
944 TypeNode = EQT->getBaseType();
945 }
Chris Lattner35fef522008-02-20 20:55:12 +0000946
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000947 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000948 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000949 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000950 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000951 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000952 return QualType(EXTQy, CVRQuals);
953
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000954 // If the base type isn't canonical, this won't be a canonical type either,
955 // so fill in the canonical type field.
956 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000957 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000958 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000959
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000960 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000961 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000962 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000963 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000964 ExtQualType *New =
965 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000966 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000967 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000968 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000969}
970
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000971QualType ASTContext::getObjCGCQualType(QualType T,
972 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000973 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000974 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000975 return T;
976
Fariborz Jahanian143b0082009-06-03 17:15:17 +0000977 if (T->isPointerType()) {
Ted Kremenekd9b39bf2009-07-17 17:50:17 +0000978 QualType Pointee = T->getAsPointerType()->getPointeeType();
Steve Naroff79ae19a2009-07-14 18:25:06 +0000979 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian143b0082009-06-03 17:15:17 +0000980 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
981 return getPointerType(ResultType);
982 }
983 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000984 // If we are composing extended qualifiers together, merge together into one
985 // ExtQualType node.
986 unsigned CVRQuals = T.getCVRQualifiers();
987 Type *TypeNode = T.getTypePtr();
988 unsigned AddressSpace = 0;
989
990 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
991 // If this type already has an address space specified, it cannot get
992 // another one.
993 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
994 "Type cannot be in multiple addr spaces!");
995 AddressSpace = EQT->getAddressSpace();
996 TypeNode = EQT->getBaseType();
997 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000998
999 // Check if we've already instantiated an gc qual'd type of this type.
1000 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +00001001 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +00001002 void *InsertPos = 0;
1003 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +00001004 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +00001005
1006 // If the base type isn't canonical, this won't be a canonical type either,
1007 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00001008 // FIXME: Isn't this also not canonical if the base type is a array
1009 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +00001010 QualType Canonical;
1011 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00001012 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +00001013
Chris Lattner18b5a9a2009-02-18 22:53:11 +00001014 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +00001015 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
1016 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1017 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00001018 ExtQualType *New =
1019 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +00001020 ExtQualTypes.InsertNode(New, InsertPos);
1021 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +00001022 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +00001023}
Chris Lattner4b009652007-07-25 00:24:17 +00001024
1025/// getComplexType - Return the uniqued reference to the type for a complex
1026/// number with the specified element type.
1027QualType ASTContext::getComplexType(QualType T) {
1028 // Unique pointers, to guarantee there is only one pointer of a particular
1029 // structure.
1030 llvm::FoldingSetNodeID ID;
1031 ComplexType::Profile(ID, T);
1032
1033 void *InsertPos = 0;
1034 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1035 return QualType(CT, 0);
1036
1037 // If the pointee type isn't canonical, this won't be a canonical type either,
1038 // so fill in the canonical type field.
1039 QualType Canonical;
1040 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001041 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +00001042
1043 // Get the new insert position for the node we care about.
1044 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001045 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001046 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001047 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001048 Types.push_back(New);
1049 ComplexTypes.InsertNode(New, InsertPos);
1050 return QualType(New, 0);
1051}
1052
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001053QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1054 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1055 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1056 FixedWidthIntType *&Entry = Map[Width];
1057 if (!Entry)
1058 Entry = new FixedWidthIntType(Width, Signed);
1059 return QualType(Entry, 0);
1060}
Chris Lattner4b009652007-07-25 00:24:17 +00001061
1062/// getPointerType - Return the uniqued reference to the type for a pointer to
1063/// the specified type.
1064QualType ASTContext::getPointerType(QualType T) {
1065 // Unique pointers, to guarantee there is only one pointer of a particular
1066 // structure.
1067 llvm::FoldingSetNodeID ID;
1068 PointerType::Profile(ID, T);
1069
1070 void *InsertPos = 0;
1071 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1072 return QualType(PT, 0);
1073
1074 // If the pointee type isn't canonical, this won't be a canonical type either,
1075 // so fill in the canonical type field.
1076 QualType Canonical;
1077 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001078 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +00001079
1080 // Get the new insert position for the node we care about.
1081 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001082 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001083 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001084 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001085 Types.push_back(New);
1086 PointerTypes.InsertNode(New, InsertPos);
1087 return QualType(New, 0);
1088}
1089
Steve Naroff7aa54752008-08-27 16:04:49 +00001090/// getBlockPointerType - Return the uniqued reference to the type for
1091/// a pointer to the specified block.
1092QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +00001093 assert(T->isFunctionType() && "block of function types only");
1094 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +00001095 // structure.
1096 llvm::FoldingSetNodeID ID;
1097 BlockPointerType::Profile(ID, T);
1098
1099 void *InsertPos = 0;
1100 if (BlockPointerType *PT =
1101 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1102 return QualType(PT, 0);
1103
Steve Narofffd5b19d2008-08-28 19:20:44 +00001104 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +00001105 // type either so fill in the canonical type field.
1106 QualType Canonical;
1107 if (!T->isCanonical()) {
1108 Canonical = getBlockPointerType(getCanonicalType(T));
1109
1110 // Get the new insert position for the node we care about.
1111 BlockPointerType *NewIP =
1112 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001113 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +00001114 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001115 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +00001116 Types.push_back(New);
1117 BlockPointerTypes.InsertNode(New, InsertPos);
1118 return QualType(New, 0);
1119}
1120
Sebastian Redlce6fff02009-03-16 23:22:08 +00001121/// getLValueReferenceType - Return the uniqued reference to the type for an
1122/// lvalue reference to the specified type.
1123QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +00001124 // Unique pointers, to guarantee there is only one pointer of a particular
1125 // structure.
1126 llvm::FoldingSetNodeID ID;
1127 ReferenceType::Profile(ID, T);
1128
1129 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +00001130 if (LValueReferenceType *RT =
1131 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001132 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001133
Chris Lattner4b009652007-07-25 00:24:17 +00001134 // If the referencee type isn't canonical, this won't be a canonical type
1135 // either, so fill in the canonical type field.
1136 QualType Canonical;
1137 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +00001138 Canonical = getLValueReferenceType(getCanonicalType(T));
1139
Chris Lattner4b009652007-07-25 00:24:17 +00001140 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +00001141 LValueReferenceType *NewIP =
1142 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001143 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001144 }
1145
Sebastian Redlce6fff02009-03-16 23:22:08 +00001146 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001147 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001148 LValueReferenceTypes.InsertNode(New, InsertPos);
1149 return QualType(New, 0);
1150}
1151
1152/// getRValueReferenceType - Return the uniqued reference to the type for an
1153/// rvalue reference to the specified type.
1154QualType ASTContext::getRValueReferenceType(QualType T) {
1155 // Unique pointers, to guarantee there is only one pointer of a particular
1156 // structure.
1157 llvm::FoldingSetNodeID ID;
1158 ReferenceType::Profile(ID, T);
1159
1160 void *InsertPos = 0;
1161 if (RValueReferenceType *RT =
1162 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1163 return QualType(RT, 0);
1164
1165 // If the referencee type isn't canonical, this won't be a canonical type
1166 // either, so fill in the canonical type field.
1167 QualType Canonical;
1168 if (!T->isCanonical()) {
1169 Canonical = getRValueReferenceType(getCanonicalType(T));
1170
1171 // Get the new insert position for the node we care about.
1172 RValueReferenceType *NewIP =
1173 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1174 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1175 }
1176
1177 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1178 Types.push_back(New);
1179 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001180 return QualType(New, 0);
1181}
1182
Sebastian Redl75555032009-01-24 21:16:55 +00001183/// getMemberPointerType - Return the uniqued reference to the type for a
1184/// member pointer to the specified type, in the specified class.
1185QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1186{
1187 // Unique pointers, to guarantee there is only one pointer of a particular
1188 // structure.
1189 llvm::FoldingSetNodeID ID;
1190 MemberPointerType::Profile(ID, T, Cls);
1191
1192 void *InsertPos = 0;
1193 if (MemberPointerType *PT =
1194 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1195 return QualType(PT, 0);
1196
1197 // If the pointee or class type isn't canonical, this won't be a canonical
1198 // type either, so fill in the canonical type field.
1199 QualType Canonical;
1200 if (!T->isCanonical()) {
1201 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1202
1203 // Get the new insert position for the node we care about.
1204 MemberPointerType *NewIP =
1205 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1206 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1207 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001208 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001209 Types.push_back(New);
1210 MemberPointerTypes.InsertNode(New, InsertPos);
1211 return QualType(New, 0);
1212}
1213
Steve Naroff83c13012007-08-30 01:06:46 +00001214/// getConstantArrayType - Return the unique reference to the type for an
1215/// array of the specified element type.
1216QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner08bea472009-05-13 04:12:56 +00001217 const llvm::APInt &ArySizeIn,
Steve Naroff24c9b982007-08-30 18:10:14 +00001218 ArrayType::ArraySizeModifier ASM,
1219 unsigned EltTypeQuals) {
Eli Friedmanb4c71b32009-05-29 20:17:55 +00001220 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1221 "Constant array of VLAs is illegal!");
1222
Chris Lattner08bea472009-05-13 04:12:56 +00001223 // Convert the array size into a canonical width matching the pointer size for
1224 // the target.
1225 llvm::APInt ArySize(ArySizeIn);
1226 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1227
Chris Lattner4b009652007-07-25 00:24:17 +00001228 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001229 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001230
1231 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001232 if (ConstantArrayType *ATP =
1233 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001234 return QualType(ATP, 0);
1235
1236 // If the element type isn't canonical, this won't be a canonical type either,
1237 // so fill in the canonical type field.
1238 QualType Canonical;
1239 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001240 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001241 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001242 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001243 ConstantArrayType *NewIP =
1244 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001245 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001246 }
1247
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001248 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001249 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001250 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001251 Types.push_back(New);
1252 return QualType(New, 0);
1253}
1254
Douglas Gregor1d381132009-07-06 15:59:29 +00001255/// getConstantArrayWithExprType - Return a reference to the type for
1256/// an array of the specified element type.
1257QualType
1258ASTContext::getConstantArrayWithExprType(QualType EltTy,
1259 const llvm::APInt &ArySizeIn,
1260 Expr *ArySizeExpr,
1261 ArrayType::ArraySizeModifier ASM,
1262 unsigned EltTypeQuals,
1263 SourceRange Brackets) {
1264 // Convert the array size into a canonical width matching the pointer
1265 // size for the target.
1266 llvm::APInt ArySize(ArySizeIn);
1267 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1268
1269 // Compute the canonical ConstantArrayType.
1270 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1271 ArySize, ASM, EltTypeQuals);
1272 // Since we don't unique expressions, it isn't possible to unique VLA's
1273 // that have an expression provided for their size.
1274 ConstantArrayWithExprType *New =
1275 new(*this,8)ConstantArrayWithExprType(EltTy, Canonical,
1276 ArySize, ArySizeExpr,
1277 ASM, EltTypeQuals, Brackets);
1278 Types.push_back(New);
1279 return QualType(New, 0);
1280}
1281
1282/// getConstantArrayWithoutExprType - Return a reference to the type for
1283/// an array of the specified element type.
1284QualType
1285ASTContext::getConstantArrayWithoutExprType(QualType EltTy,
1286 const llvm::APInt &ArySizeIn,
1287 ArrayType::ArraySizeModifier ASM,
1288 unsigned EltTypeQuals) {
1289 // Convert the array size into a canonical width matching the pointer
1290 // size for the target.
1291 llvm::APInt ArySize(ArySizeIn);
1292 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1293
1294 // Compute the canonical ConstantArrayType.
1295 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1296 ArySize, ASM, EltTypeQuals);
1297 ConstantArrayWithoutExprType *New =
1298 new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical,
1299 ArySize, ASM, EltTypeQuals);
1300 Types.push_back(New);
1301 return QualType(New, 0);
1302}
1303
Steve Naroffe2579e32007-08-30 18:14:25 +00001304/// getVariableArrayType - Returns a non-unique reference to the type for a
1305/// variable array of the specified element type.
Douglas Gregor1d381132009-07-06 15:59:29 +00001306QualType ASTContext::getVariableArrayType(QualType EltTy,
1307 Expr *NumElts,
Steve Naroff24c9b982007-08-30 18:10:14 +00001308 ArrayType::ArraySizeModifier ASM,
Douglas Gregor1d381132009-07-06 15:59:29 +00001309 unsigned EltTypeQuals,
1310 SourceRange Brackets) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001311 // Since we don't unique expressions, it isn't possible to unique VLA's
1312 // that have an expression provided for their size.
1313
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001314 VariableArrayType *New =
Douglas Gregor1d381132009-07-06 15:59:29 +00001315 new(*this,8)VariableArrayType(EltTy, QualType(),
1316 NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedman8ff07782008-02-15 18:16:39 +00001317
1318 VariableArrayTypes.push_back(New);
1319 Types.push_back(New);
1320 return QualType(New, 0);
1321}
1322
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001323/// getDependentSizedArrayType - Returns a non-unique reference to
1324/// the type for a dependently-sized array of the specified element
1325/// type. FIXME: We will need these to be uniqued, or at least
1326/// comparable, at some point.
Douglas Gregor1d381132009-07-06 15:59:29 +00001327QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1328 Expr *NumElts,
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001329 ArrayType::ArraySizeModifier ASM,
Douglas Gregor1d381132009-07-06 15:59:29 +00001330 unsigned EltTypeQuals,
1331 SourceRange Brackets) {
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001332 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1333 "Size must be type- or value-dependent!");
1334
1335 // Since we don't unique expressions, it isn't possible to unique
1336 // dependently-sized array types.
1337
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001338 DependentSizedArrayType *New =
Douglas Gregor1d381132009-07-06 15:59:29 +00001339 new (*this,8) DependentSizedArrayType(EltTy, QualType(),
1340 NumElts, ASM, EltTypeQuals,
1341 Brackets);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001342
1343 DependentSizedArrayTypes.push_back(New);
1344 Types.push_back(New);
1345 return QualType(New, 0);
1346}
1347
Eli Friedman8ff07782008-02-15 18:16:39 +00001348QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1349 ArrayType::ArraySizeModifier ASM,
1350 unsigned EltTypeQuals) {
1351 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001352 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001353
1354 void *InsertPos = 0;
1355 if (IncompleteArrayType *ATP =
1356 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1357 return QualType(ATP, 0);
1358
1359 // If the element type isn't canonical, this won't be a canonical type
1360 // either, so fill in the canonical type field.
1361 QualType Canonical;
1362
1363 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001364 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001365 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001366
1367 // Get the new insert position for the node we care about.
1368 IncompleteArrayType *NewIP =
1369 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001370 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001371 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001372
Douglas Gregor1d381132009-07-06 15:59:29 +00001373 IncompleteArrayType *New
1374 = new (*this,8) IncompleteArrayType(EltTy, Canonical,
1375 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001376
1377 IncompleteArrayTypes.InsertNode(New, InsertPos);
1378 Types.push_back(New);
1379 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001380}
1381
Chris Lattner4b009652007-07-25 00:24:17 +00001382/// getVectorType - Return the unique reference to a vector type of
1383/// the specified element type and size. VectorType must be a built-in type.
1384QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1385 BuiltinType *baseType;
1386
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001387 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001388 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1389
1390 // Check if we've already instantiated a vector of this type.
1391 llvm::FoldingSetNodeID ID;
1392 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1393 void *InsertPos = 0;
1394 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1395 return QualType(VTP, 0);
1396
1397 // If the element type isn't canonical, this won't be a canonical type either,
1398 // so fill in the canonical type field.
1399 QualType Canonical;
1400 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001401 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001402
1403 // Get the new insert position for the node we care about.
1404 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001405 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001406 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001407 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001408 VectorTypes.InsertNode(New, InsertPos);
1409 Types.push_back(New);
1410 return QualType(New, 0);
1411}
1412
Nate Begemanaf6ed502008-04-18 23:10:10 +00001413/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001414/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001415QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001416 BuiltinType *baseType;
1417
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001418 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001419 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001420
1421 // Check if we've already instantiated a vector of this type.
1422 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001423 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001424 void *InsertPos = 0;
1425 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1426 return QualType(VTP, 0);
1427
1428 // If the element type isn't canonical, this won't be a canonical type either,
1429 // so fill in the canonical type field.
1430 QualType Canonical;
1431 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001432 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001433
1434 // Get the new insert position for the node we care about.
1435 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001436 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001437 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001438 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001439 VectorTypes.InsertNode(New, InsertPos);
1440 Types.push_back(New);
1441 return QualType(New, 0);
1442}
1443
Douglas Gregor2a2e0402009-06-17 21:51:59 +00001444QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
1445 Expr *SizeExpr,
1446 SourceLocation AttrLoc) {
1447 DependentSizedExtVectorType *New =
1448 new (*this,8) DependentSizedExtVectorType(vecType, QualType(),
1449 SizeExpr, AttrLoc);
1450
1451 DependentSizedExtVectorTypes.push_back(New);
1452 Types.push_back(New);
1453 return QualType(New, 0);
1454}
1455
Douglas Gregor4fa58902009-02-26 23:50:07 +00001456/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001457///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001458QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001459 // Unique functions, to guarantee there is only one function of a particular
1460 // structure.
1461 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001462 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001463
1464 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001465 if (FunctionNoProtoType *FT =
1466 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001467 return QualType(FT, 0);
1468
1469 QualType Canonical;
1470 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001471 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001472
1473 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001474 FunctionNoProtoType *NewIP =
1475 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001476 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001477 }
1478
Douglas Gregor4fa58902009-02-26 23:50:07 +00001479 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001480 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001481 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001482 return QualType(New, 0);
1483}
1484
1485/// getFunctionType - Return a normal function type with a typed argument
1486/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001487QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001488 unsigned NumArgs, bool isVariadic,
Sebastian Redl2767d882009-05-27 22:11:52 +00001489 unsigned TypeQuals, bool hasExceptionSpec,
1490 bool hasAnyExceptionSpec, unsigned NumExs,
1491 const QualType *ExArray) {
Chris Lattner4b009652007-07-25 00:24:17 +00001492 // Unique functions, to guarantee there is only one function of a particular
1493 // structure.
1494 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001495 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl2767d882009-05-27 22:11:52 +00001496 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1497 NumExs, ExArray);
Chris Lattner4b009652007-07-25 00:24:17 +00001498
1499 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001500 if (FunctionProtoType *FTP =
1501 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001502 return QualType(FTP, 0);
Sebastian Redl2767d882009-05-27 22:11:52 +00001503
1504 // Determine whether the type being created is already canonical or not.
Chris Lattner4b009652007-07-25 00:24:17 +00001505 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl2767d882009-05-27 22:11:52 +00001506 if (hasExceptionSpec)
1507 isCanonical = false;
Chris Lattner4b009652007-07-25 00:24:17 +00001508 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1509 if (!ArgArray[i]->isCanonical())
1510 isCanonical = false;
1511
1512 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl2767d882009-05-27 22:11:52 +00001513 // The exception spec is not part of the canonical type.
Chris Lattner4b009652007-07-25 00:24:17 +00001514 QualType Canonical;
1515 if (!isCanonical) {
1516 llvm::SmallVector<QualType, 16> CanonicalArgs;
1517 CanonicalArgs.reserve(NumArgs);
1518 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001519 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl2767d882009-05-27 22:11:52 +00001520
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001521 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foad9e6bef42009-05-21 09:52:38 +00001522 CanonicalArgs.data(), NumArgs,
Sebastian Redlba9a3712009-05-06 23:27:55 +00001523 isVariadic, TypeQuals);
Sebastian Redl2767d882009-05-27 22:11:52 +00001524
Chris Lattner4b009652007-07-25 00:24:17 +00001525 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001526 FunctionProtoType *NewIP =
1527 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001528 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001529 }
Sebastian Redl2767d882009-05-27 22:11:52 +00001530
Douglas Gregor4fa58902009-02-26 23:50:07 +00001531 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl2767d882009-05-27 22:11:52 +00001532 // for two variable size arrays (for parameter and exception types) at the
1533 // end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001534 FunctionProtoType *FTP =
Sebastian Redl2767d882009-05-27 22:11:52 +00001535 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1536 NumArgs*sizeof(QualType) +
1537 NumExs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001538 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl2767d882009-05-27 22:11:52 +00001539 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1540 ExArray, NumExs, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001541 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001542 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001543 return QualType(FTP, 0);
1544}
1545
Douglas Gregor1d661552008-04-13 21:07:44 +00001546/// getTypeDeclType - Return the unique reference to the type for the
1547/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001548QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001549 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001550 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1551
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001552 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001553 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001554 else if (isa<TemplateTypeParmDecl>(Decl)) {
1555 assert(false && "Template type parameter types are always available.");
1556 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001557 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001558
Douglas Gregor2e047592009-02-28 01:32:25 +00001559 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001560 if (PrevDecl)
1561 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001562 else
1563 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001564 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001565 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1566 if (PrevDecl)
1567 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001568 else
1569 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001570 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001571 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001572 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001573
Ted Kremenek46a837c2008-09-05 17:16:31 +00001574 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001575 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001576}
1577
Chris Lattner4b009652007-07-25 00:24:17 +00001578/// getTypedefType - Return the unique reference to the type for the
1579/// specified typename decl.
1580QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1581 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1582
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001583 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001584 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001585 Types.push_back(Decl->TypeForDecl);
1586 return QualType(Decl->TypeForDecl, 0);
1587}
1588
Douglas Gregora4918772009-02-05 23:33:38 +00001589/// \brief Retrieve the template type parameter type for a template
Anders Carlsson4e3d3552009-06-16 00:30:48 +00001590/// parameter or parameter pack with the given depth, index, and (optionally)
1591/// name.
Douglas Gregora4918772009-02-05 23:33:38 +00001592QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson4e3d3552009-06-16 00:30:48 +00001593 bool ParameterPack,
Douglas Gregora4918772009-02-05 23:33:38 +00001594 IdentifierInfo *Name) {
1595 llvm::FoldingSetNodeID ID;
Anders Carlsson4e3d3552009-06-16 00:30:48 +00001596 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregora4918772009-02-05 23:33:38 +00001597 void *InsertPos = 0;
1598 TemplateTypeParmType *TypeParm
1599 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1600
1601 if (TypeParm)
1602 return QualType(TypeParm, 0);
1603
Anders Carlsson4e3d3552009-06-16 00:30:48 +00001604 if (Name) {
1605 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
1606 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
1607 Name, Canon);
1608 } else
1609 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregora4918772009-02-05 23:33:38 +00001610
1611 Types.push_back(TypeParm);
1612 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1613
1614 return QualType(TypeParm, 0);
1615}
1616
Douglas Gregor8e458f42009-02-09 18:46:07 +00001617QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001618ASTContext::getTemplateSpecializationType(TemplateName Template,
1619 const TemplateArgument *Args,
1620 unsigned NumArgs,
1621 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001622 if (!Canon.isNull())
1623 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001624
Douglas Gregor8e458f42009-02-09 18:46:07 +00001625 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001626 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001627
Douglas Gregor8e458f42009-02-09 18:46:07 +00001628 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001629 TemplateSpecializationType *Spec
1630 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001631
1632 if (Spec)
1633 return QualType(Spec, 0);
1634
Douglas Gregordd13e842009-03-30 22:58:21 +00001635 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001636 sizeof(TemplateArgument) * NumArgs),
1637 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001638 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001639 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001640 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001641
1642 return QualType(Spec, 0);
1643}
1644
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001645QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001646ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001647 QualType NamedType) {
1648 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001649 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001650
1651 void *InsertPos = 0;
1652 QualifiedNameType *T
1653 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1654 if (T)
1655 return QualType(T, 0);
1656
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001657 T = new (*this) QualifiedNameType(NNS, NamedType,
1658 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001659 Types.push_back(T);
1660 QualifiedNameTypes.InsertNode(T, InsertPos);
1661 return QualType(T, 0);
1662}
1663
Douglas Gregord3022602009-03-27 23:10:48 +00001664QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1665 const IdentifierInfo *Name,
1666 QualType Canon) {
1667 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1668
1669 if (Canon.isNull()) {
1670 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1671 if (CanonNNS != NNS)
1672 Canon = getTypenameType(CanonNNS, Name);
1673 }
1674
1675 llvm::FoldingSetNodeID ID;
1676 TypenameType::Profile(ID, NNS, Name);
1677
1678 void *InsertPos = 0;
1679 TypenameType *T
1680 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1681 if (T)
1682 return QualType(T, 0);
1683
1684 T = new (*this) TypenameType(NNS, Name, Canon);
1685 Types.push_back(T);
1686 TypenameTypes.InsertNode(T, InsertPos);
1687 return QualType(T, 0);
1688}
1689
Douglas Gregor77da5802009-04-01 00:28:59 +00001690QualType
1691ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1692 const TemplateSpecializationType *TemplateId,
1693 QualType Canon) {
1694 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1695
1696 if (Canon.isNull()) {
1697 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1698 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1699 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1700 const TemplateSpecializationType *CanonTemplateId
1701 = CanonType->getAsTemplateSpecializationType();
1702 assert(CanonTemplateId &&
1703 "Canonical type must also be a template specialization type");
1704 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1705 }
1706 }
1707
1708 llvm::FoldingSetNodeID ID;
1709 TypenameType::Profile(ID, NNS, TemplateId);
1710
1711 void *InsertPos = 0;
1712 TypenameType *T
1713 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1714 if (T)
1715 return QualType(T, 0);
1716
1717 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1718 Types.push_back(T);
1719 TypenameTypes.InsertNode(T, InsertPos);
1720 return QualType(T, 0);
1721}
1722
Chris Lattnere1352302008-04-07 04:56:42 +00001723/// CmpProtocolNames - Comparison predicate for sorting protocols
1724/// alphabetically.
1725static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1726 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001727 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001728}
1729
1730static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1731 unsigned &NumProtocols) {
1732 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1733
1734 // Sort protocols, keyed by name.
1735 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1736
1737 // Remove duplicates.
1738 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1739 NumProtocols = ProtocolsEnd-Protocols;
1740}
1741
Steve Naroffc75c1a82009-06-17 22:40:22 +00001742/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1743/// the given interface decl and the conforming protocol list.
Steve Naroff329ec222009-07-10 23:34:53 +00001744QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Steve Naroffc75c1a82009-06-17 22:40:22 +00001745 ObjCProtocolDecl **Protocols,
1746 unsigned NumProtocols) {
1747 // Sort the protocol list alphabetically to canonicalize it.
1748 if (NumProtocols)
1749 SortAndUniqueProtocols(Protocols, NumProtocols);
1750
1751 llvm::FoldingSetNodeID ID;
Steve Naroff329ec222009-07-10 23:34:53 +00001752 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffc75c1a82009-06-17 22:40:22 +00001753
1754 void *InsertPos = 0;
1755 if (ObjCObjectPointerType *QT =
1756 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1757 return QualType(QT, 0);
1758
1759 // No Match;
1760 ObjCObjectPointerType *QType =
Steve Naroff329ec222009-07-10 23:34:53 +00001761 new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Steve Naroffc75c1a82009-06-17 22:40:22 +00001762
1763 Types.push_back(QType);
1764 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1765 return QualType(QType, 0);
1766}
Chris Lattnere1352302008-04-07 04:56:42 +00001767
Steve Naroff77763c52009-07-18 15:33:26 +00001768/// getObjCInterfaceType - Return the unique reference to the type for the
1769/// specified ObjC interface decl. The list of protocols is optional.
1770QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremenek42730c52008-01-07 19:49:32 +00001771 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Steve Naroff77763c52009-07-18 15:33:26 +00001772 if (NumProtocols)
1773 // Sort the protocol list alphabetically to canonicalize it.
1774 SortAndUniqueProtocols(Protocols, NumProtocols);
Chris Lattnere1352302008-04-07 04:56:42 +00001775
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001776 llvm::FoldingSetNodeID ID;
Steve Naroff77763c52009-07-18 15:33:26 +00001777 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001778
1779 void *InsertPos = 0;
Steve Naroff77763c52009-07-18 15:33:26 +00001780 if (ObjCInterfaceType *QT =
1781 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001782 return QualType(QT, 0);
1783
1784 // No Match;
Steve Naroff77763c52009-07-18 15:33:26 +00001785 ObjCInterfaceType *QType =
1786 new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1787 Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001788 Types.push_back(QType);
Steve Naroff77763c52009-07-18 15:33:26 +00001789 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001790 return QualType(QType, 0);
1791}
1792
Douglas Gregor4fa58902009-02-26 23:50:07 +00001793/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1794/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001795/// multiple declarations that refer to "typeof(x)" all contain different
1796/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1797/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001798QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregord1c0b682009-07-08 00:03:05 +00001799 TypeOfExprType *toe;
1800 if (tofExpr->isTypeDependent())
1801 toe = new (*this, 8) TypeOfExprType(tofExpr);
1802 else {
1803 QualType Canonical = getCanonicalType(tofExpr->getType());
1804 toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
1805 }
Steve Naroff0604dd92007-08-01 18:02:17 +00001806 Types.push_back(toe);
1807 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001808}
1809
Steve Naroff0604dd92007-08-01 18:02:17 +00001810/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1811/// TypeOfType AST's. The only motivation to unique these nodes would be
1812/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1813/// an issue. This doesn't effect the type checker, since it operates
1814/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001815QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001816 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001817 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001818 Types.push_back(tot);
1819 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001820}
1821
Anders Carlsson09b88962009-06-24 21:24:56 +00001822/// getDecltypeForExpr - Given an expr, will return the decltype for that
1823/// expression, according to the rules in C++0x [dcl.type.simple]p4
1824static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlsson26dcdbb2009-06-25 15:00:34 +00001825 if (e->isTypeDependent())
1826 return Context.DependentTy;
1827
Anders Carlsson09b88962009-06-24 21:24:56 +00001828 // If e is an id expression or a class member access, decltype(e) is defined
1829 // as the type of the entity named by e.
1830 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
1831 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
1832 return VD->getType();
1833 }
1834 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
1835 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
1836 return FD->getType();
1837 }
1838 // If e is a function call or an invocation of an overloaded operator,
1839 // (parentheses around e are ignored), decltype(e) is defined as the
1840 // return type of that function.
1841 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
1842 return CE->getCallReturnType();
1843
1844 QualType T = e->getType();
1845
1846 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
1847 // defined as T&, otherwise decltype(e) is defined as T.
1848 if (e->isLvalue(Context) == Expr::LV_Valid)
1849 T = Context.getLValueReferenceType(T);
1850
1851 return T;
1852}
1853
Anders Carlsson93ab5332009-06-24 19:06:50 +00001854/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
1855/// DecltypeType AST's. The only motivation to unique these nodes would be
1856/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
1857/// an issue. This doesn't effect the type checker, since it operates
1858/// on canonical type's (which are always unique).
1859QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregord1c0b682009-07-08 00:03:05 +00001860 DecltypeType *dt;
1861 if (e->isTypeDependent()) // FIXME: canonicalize the expression
Anders Carlsson42f394e2009-07-10 19:20:26 +00001862 dt = new (*this, 8) DecltypeType(e, DependentTy);
Douglas Gregord1c0b682009-07-08 00:03:05 +00001863 else {
1864 QualType T = getDecltypeForExpr(e, *this);
Anders Carlsson42f394e2009-07-10 19:20:26 +00001865 dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregord1c0b682009-07-08 00:03:05 +00001866 }
Anders Carlsson93ab5332009-06-24 19:06:50 +00001867 Types.push_back(dt);
1868 return QualType(dt, 0);
1869}
1870
Chris Lattner4b009652007-07-25 00:24:17 +00001871/// getTagDeclType - Return the unique reference to the type for the
1872/// specified TagDecl (struct/union/class/enum) decl.
1873QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001874 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001875 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001876}
1877
1878/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1879/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1880/// needs to agree with the definition in <stddef.h>.
1881QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001882 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001883}
1884
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001885/// getSignedWCharType - Return the type of "signed wchar_t".
1886/// Used when in C++, as a GCC extension.
1887QualType ASTContext::getSignedWCharType() const {
1888 // FIXME: derive from "Target" ?
1889 return WCharTy;
1890}
1891
1892/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1893/// Used when in C++, as a GCC extension.
1894QualType ASTContext::getUnsignedWCharType() const {
1895 // FIXME: derive from "Target" ?
1896 return UnsignedIntTy;
1897}
1898
Chris Lattner4b009652007-07-25 00:24:17 +00001899/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1900/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1901QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001902 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001903}
1904
Chris Lattner19eb97e2008-04-02 05:18:44 +00001905//===----------------------------------------------------------------------===//
1906// Type Operators
1907//===----------------------------------------------------------------------===//
1908
Chris Lattner3dae6f42008-04-06 22:41:35 +00001909/// getCanonicalType - Return the canonical (structural) type corresponding to
1910/// the specified potentially non-canonical type. The non-canonical version
1911/// of a type may have many "decorated" versions of types. Decorators can
1912/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1913/// to be free of any of these, allowing two canonical types to be compared
1914/// for exact equality with a simple pointer comparison.
1915QualType ASTContext::getCanonicalType(QualType T) {
1916 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001917
1918 // If the result has type qualifiers, make sure to canonicalize them as well.
1919 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1920 if (TypeQuals == 0) return CanType;
1921
1922 // If the type qualifiers are on an array type, get the canonical type of the
1923 // array with the qualifiers applied to the element type.
1924 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1925 if (!AT)
1926 return CanType.getQualifiedType(TypeQuals);
1927
1928 // Get the canonical version of the element with the extra qualifiers on it.
1929 // This can recursively sink qualifiers through multiple levels of arrays.
1930 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1931 NewEltTy = getCanonicalType(NewEltTy);
1932
1933 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1934 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1935 CAT->getIndexTypeQualifier());
1936 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1937 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1938 IAT->getIndexTypeQualifier());
1939
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001940 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor1d381132009-07-06 15:59:29 +00001941 return getDependentSizedArrayType(NewEltTy,
1942 DSAT->getSizeExpr(),
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001943 DSAT->getSizeModifier(),
Douglas Gregor1d381132009-07-06 15:59:29 +00001944 DSAT->getIndexTypeQualifier(),
1945 DSAT->getBracketsRange());
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001946
Chris Lattnera1923f62008-08-04 07:31:14 +00001947 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor1d381132009-07-06 15:59:29 +00001948 return getVariableArrayType(NewEltTy,
1949 VAT->getSizeExpr(),
Chris Lattnera1923f62008-08-04 07:31:14 +00001950 VAT->getSizeModifier(),
Douglas Gregor1d381132009-07-06 15:59:29 +00001951 VAT->getIndexTypeQualifier(),
1952 VAT->getBracketsRange());
Chris Lattnera1923f62008-08-04 07:31:14 +00001953}
1954
Douglas Gregorb88ba412009-05-07 06:41:52 +00001955TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1956 // If this template name refers to a template, the canonical
1957 // template name merely stores the template itself.
1958 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +00001959 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregorb88ba412009-05-07 06:41:52 +00001960
1961 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1962 assert(DTN && "Non-dependent template names must refer to template decls.");
1963 return DTN->CanonicalTemplateName;
1964}
1965
Douglas Gregord3022602009-03-27 23:10:48 +00001966NestedNameSpecifier *
1967ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1968 if (!NNS)
1969 return 0;
1970
1971 switch (NNS->getKind()) {
1972 case NestedNameSpecifier::Identifier:
1973 // Canonicalize the prefix but keep the identifier the same.
1974 return NestedNameSpecifier::Create(*this,
1975 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1976 NNS->getAsIdentifier());
1977
1978 case NestedNameSpecifier::Namespace:
1979 // A namespace is canonical; build a nested-name-specifier with
1980 // this namespace and no prefix.
1981 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1982
1983 case NestedNameSpecifier::TypeSpec:
1984 case NestedNameSpecifier::TypeSpecWithTemplate: {
1985 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1986 NestedNameSpecifier *Prefix = 0;
1987
1988 // FIXME: This isn't the right check!
1989 if (T->isDependentType())
1990 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1991
1992 return NestedNameSpecifier::Create(*this, Prefix,
1993 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1994 T.getTypePtr());
1995 }
1996
1997 case NestedNameSpecifier::Global:
1998 // The global specifier is canonical and unique.
1999 return NNS;
2000 }
2001
2002 // Required to silence a GCC warning
2003 return 0;
2004}
2005
Chris Lattnera1923f62008-08-04 07:31:14 +00002006
2007const ArrayType *ASTContext::getAsArrayType(QualType T) {
2008 // Handle the non-qualified case efficiently.
2009 if (T.getCVRQualifiers() == 0) {
2010 // Handle the common positive case fast.
2011 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2012 return AT;
2013 }
2014
2015 // Handle the common negative case fast, ignoring CVR qualifiers.
2016 QualType CType = T->getCanonicalTypeInternal();
2017
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00002018 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00002019 // test.
2020 if (!isa<ArrayType>(CType) &&
2021 !isa<ArrayType>(CType.getUnqualifiedType()))
2022 return 0;
2023
2024 // Apply any CVR qualifiers from the array type to the element type. This
2025 // implements C99 6.7.3p8: "If the specification of an array type includes
2026 // any type qualifiers, the element type is so qualified, not the array type."
2027
2028 // If we get here, we either have type qualifiers on the type, or we have
2029 // sugar such as a typedef in the way. If we have type qualifiers on the type
2030 // we must propagate them down into the elemeng type.
2031 unsigned CVRQuals = T.getCVRQualifiers();
2032 unsigned AddrSpace = 0;
2033 Type *Ty = T.getTypePtr();
2034
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00002035 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00002036 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00002037 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
2038 AddrSpace = EXTQT->getAddressSpace();
2039 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00002040 } else {
2041 T = Ty->getDesugaredType();
2042 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
2043 break;
2044 CVRQuals |= T.getCVRQualifiers();
2045 Ty = T.getTypePtr();
2046 }
2047 }
2048
2049 // If we have a simple case, just return now.
2050 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
2051 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
2052 return ATy;
2053
2054 // Otherwise, we have an array and we have qualifiers on it. Push the
2055 // qualifiers into the array element type and return a new array type.
2056 // Get the canonical version of the element with the extra qualifiers on it.
2057 // This can recursively sink qualifiers through multiple levels of arrays.
2058 QualType NewEltTy = ATy->getElementType();
2059 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00002060 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00002061 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
2062
2063 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2064 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2065 CAT->getSizeModifier(),
2066 CAT->getIndexTypeQualifier()));
2067 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2068 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2069 IAT->getSizeModifier(),
Douglas Gregor1d381132009-07-06 15:59:29 +00002070 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00002071
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00002072 if (const DependentSizedArrayType *DSAT
2073 = dyn_cast<DependentSizedArrayType>(ATy))
2074 return cast<ArrayType>(
2075 getDependentSizedArrayType(NewEltTy,
2076 DSAT->getSizeExpr(),
2077 DSAT->getSizeModifier(),
Douglas Gregor1d381132009-07-06 15:59:29 +00002078 DSAT->getIndexTypeQualifier(),
2079 DSAT->getBracketsRange()));
Chris Lattnera1923f62008-08-04 07:31:14 +00002080
Chris Lattnera1923f62008-08-04 07:31:14 +00002081 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor1d381132009-07-06 15:59:29 +00002082 return cast<ArrayType>(getVariableArrayType(NewEltTy,
2083 VAT->getSizeExpr(),
Chris Lattnera1923f62008-08-04 07:31:14 +00002084 VAT->getSizeModifier(),
Douglas Gregor1d381132009-07-06 15:59:29 +00002085 VAT->getIndexTypeQualifier(),
2086 VAT->getBracketsRange()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00002087}
2088
2089
Chris Lattner19eb97e2008-04-02 05:18:44 +00002090/// getArrayDecayedType - Return the properly qualified result of decaying the
2091/// specified array type to a pointer. This operation is non-trivial when
2092/// handling typedefs etc. The canonical type of "T" must be an array type,
2093/// this returns a pointer to a properly qualified element of the array.
2094///
2095/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2096QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00002097 // Get the element type with 'getAsArrayType' so that we don't lose any
2098 // typedefs in the element type of the array. This also handles propagation
2099 // of type qualifiers from the array type into the element type if present
2100 // (C99 6.7.3p8).
2101 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2102 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00002103
Chris Lattnera1923f62008-08-04 07:31:14 +00002104 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00002105
2106 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00002107 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00002108}
2109
Douglas Gregor4af05232009-07-23 23:49:00 +00002110QualType ASTContext::getBaseElementType(QualType QT) {
2111 QualifierSet qualifiers;
2112 while (true) {
2113 const Type *UT = qualifiers.strip(QT);
2114 if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) {
2115 QT = AT->getElementType();
2116 }else {
2117 return qualifiers.apply(QT, *this);
2118 }
2119 }
2120}
2121
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00002122QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00002123 QualType ElemTy = VAT->getElementType();
2124
2125 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
2126 return getBaseElementType(VAT);
2127
2128 return ElemTy;
2129}
2130
Chris Lattner4b009652007-07-25 00:24:17 +00002131/// getFloatingRank - Return a relative rank for floating point types.
2132/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00002133static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00002134 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00002135 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00002136
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00002137 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00002138 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00002139 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00002140 case BuiltinType::Float: return FloatRank;
2141 case BuiltinType::Double: return DoubleRank;
2142 case BuiltinType::LongDouble: return LongDoubleRank;
2143 }
2144}
2145
Steve Narofffa0c4532007-08-27 01:41:48 +00002146/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2147/// point or a complex type (based on typeDomain/typeSize).
2148/// 'typeDomain' is a real floating point or complex type.
2149/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00002150QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2151 QualType Domain) const {
2152 FloatingRank EltRank = getFloatingRank(Size);
2153 if (Domain->isComplexType()) {
2154 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00002155 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00002156 case FloatRank: return FloatComplexTy;
2157 case DoubleRank: return DoubleComplexTy;
2158 case LongDoubleRank: return LongDoubleComplexTy;
2159 }
Chris Lattner4b009652007-07-25 00:24:17 +00002160 }
Chris Lattner7794ae22008-04-06 23:58:54 +00002161
2162 assert(Domain->isRealFloatingType() && "Unknown domain!");
2163 switch (EltRank) {
2164 default: assert(0 && "getFloatingRank(): illegal value for rank");
2165 case FloatRank: return FloatTy;
2166 case DoubleRank: return DoubleTy;
2167 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00002168 }
Chris Lattner4b009652007-07-25 00:24:17 +00002169}
2170
Chris Lattner51285d82008-04-06 23:55:33 +00002171/// getFloatingTypeOrder - Compare the rank of the two specified floating
2172/// point types, ignoring the domain of the type (i.e. 'double' ==
2173/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
2174/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00002175int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2176 FloatingRank LHSR = getFloatingRank(LHS);
2177 FloatingRank RHSR = getFloatingRank(RHS);
2178
2179 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00002180 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00002181 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00002182 return 1;
2183 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00002184}
2185
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002186/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2187/// routine will assert if passed a built-in type that isn't an integer or enum,
2188/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002189unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002190 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002191 if (EnumType* ET = dyn_cast<EnumType>(T))
2192 T = ET->getDecl()->getIntegerType().getTypePtr();
2193
Eli Friedman78c50f12009-07-05 23:44:27 +00002194 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2195 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2196
Alisdair Meredith2bcacb62009-07-14 06:30:34 +00002197 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2198 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2199
2200 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2201 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2202
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002203 // There are two things which impact the integer rank: the width, and
2204 // the ordering of builtins. The builtin ordering is encoded in the
2205 // bottom three bits; the width is encoded in the bits above that.
Chris Lattnerc46fcdd2009-06-14 01:54:56 +00002206 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002207 return FWIT->getWidth() << 3;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002208
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002209 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00002210 default: assert(0 && "getIntegerRank(): not a built-in integer");
2211 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002212 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002213 case BuiltinType::Char_S:
2214 case BuiltinType::Char_U:
2215 case BuiltinType::SChar:
2216 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002217 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002218 case BuiltinType::Short:
2219 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002220 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002221 case BuiltinType::Int:
2222 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002223 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002224 case BuiltinType::Long:
2225 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002226 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00002227 case BuiltinType::LongLong:
2228 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00002229 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner6cc7e412009-04-30 02:43:43 +00002230 case BuiltinType::Int128:
2231 case BuiltinType::UInt128:
2232 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002233 }
2234}
2235
Chris Lattner51285d82008-04-06 23:55:33 +00002236/// getIntegerTypeOrder - Returns the highest ranked integer type:
2237/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2238/// LHS < RHS, return -1.
2239int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002240 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2241 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00002242 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00002243
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002244 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2245 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00002246
Chris Lattner51285d82008-04-06 23:55:33 +00002247 unsigned LHSRank = getIntegerRank(LHSC);
2248 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00002249
Chris Lattner51285d82008-04-06 23:55:33 +00002250 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2251 if (LHSRank == RHSRank) return 0;
2252 return LHSRank > RHSRank ? 1 : -1;
2253 }
Chris Lattner4b009652007-07-25 00:24:17 +00002254
Chris Lattner51285d82008-04-06 23:55:33 +00002255 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2256 if (LHSUnsigned) {
2257 // If the unsigned [LHS] type is larger, return it.
2258 if (LHSRank >= RHSRank)
2259 return 1;
2260
2261 // If the signed type can represent all values of the unsigned type, it
2262 // wins. Because we are dealing with 2's complement and types that are
2263 // powers of two larger than each other, this is always safe.
2264 return -1;
2265 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00002266
Chris Lattner51285d82008-04-06 23:55:33 +00002267 // If the unsigned [RHS] type is larger, return it.
2268 if (RHSRank >= LHSRank)
2269 return -1;
2270
2271 // If the signed type can represent all values of the unsigned type, it
2272 // wins. Because we are dealing with 2's complement and types that are
2273 // powers of two larger than each other, this is always safe.
2274 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00002275}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002276
2277// getCFConstantStringType - Return the type used for constant CFStrings.
2278QualType ASTContext::getCFConstantStringType() {
2279 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00002280 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00002281 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00002282 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002283 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002284
2285 // const int *isa;
2286 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002287 // int flags;
2288 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002289 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002290 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002291 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002292 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00002293
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002294 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00002295 for (unsigned i = 0; i < 4; ++i) {
2296 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2297 SourceLocation(), 0,
2298 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002299 /*Mutable=*/false);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002300 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002301 }
2302
2303 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002304 }
2305
2306 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00002307}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002308
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002309void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00002310 const RecordType *Rec = T->getAsRecordType();
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002311 assert(Rec && "Invalid CFConstantStringType");
2312 CFConstantStringTypeDecl = Rec->getDecl();
2313}
2314
Anders Carlssonf58cac72008-08-30 19:34:46 +00002315QualType ASTContext::getObjCFastEnumerationStateType()
2316{
2317 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002318 ObjCFastEnumerationStateTypeDecl =
2319 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2320 &Idents.get("__objcFastEnumerationState"));
2321
Anders Carlssonf58cac72008-08-30 19:34:46 +00002322 QualType FieldTypes[] = {
2323 UnsignedLongTy,
Steve Naroff7bffd372009-07-15 18:40:39 +00002324 getPointerType(ObjCIdTypedefType),
Anders Carlssonf58cac72008-08-30 19:34:46 +00002325 getPointerType(UnsignedLongTy),
2326 getConstantArrayType(UnsignedLongTy,
2327 llvm::APInt(32, 5), ArrayType::Normal, 0)
2328 };
2329
Douglas Gregor8acb7272008-12-11 16:49:14 +00002330 for (size_t i = 0; i < 4; ++i) {
2331 FieldDecl *Field = FieldDecl::Create(*this,
2332 ObjCFastEnumerationStateTypeDecl,
2333 SourceLocation(), 0,
2334 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002335 /*Mutable=*/false);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002336 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002337 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002338
Douglas Gregor8acb7272008-12-11 16:49:14 +00002339 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002340 }
2341
2342 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2343}
2344
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002345void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00002346 const RecordType *Rec = T->getAsRecordType();
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002347 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2348 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2349}
2350
Anders Carlssone3f02572007-10-29 06:33:42 +00002351// This returns true if a type has been typedefed to BOOL:
2352// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002353static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002354 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002355 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2356 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002357
2358 return false;
2359}
2360
Ted Kremenek42730c52008-01-07 19:49:32 +00002361/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002362/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002363int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002364 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002365
2366 // Make all integer and enum types at least as large as an int
2367 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002368 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002369 // Treat arrays as pointers, since that's how they're passed in.
2370 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002371 sz = getTypeSize(VoidPtrTy);
2372 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002373}
2374
Ted Kremenek42730c52008-01-07 19:49:32 +00002375/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002376/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002377void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002378 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002379 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002380 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002381 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002382 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002383 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002384 // Compute size of all parameters.
2385 // Start with computing size of a pointer in number of bytes.
2386 // FIXME: There might(should) be a better way of doing this computation!
2387 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002388 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002389 // The first two arguments (self and _cmd) are pointers; account for
2390 // their size.
2391 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002392 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2393 E = Decl->param_end(); PI != E; ++PI) {
2394 QualType PType = (*PI)->getType();
2395 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002396 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002397 ParmOffset += sz;
2398 }
2399 S += llvm::utostr(ParmOffset);
2400 S += "@0:";
2401 S += llvm::utostr(PtrSize);
2402
2403 // Argument types.
2404 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002405 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2406 E = Decl->param_end(); PI != E; ++PI) {
2407 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002408 QualType PType = PVDecl->getOriginalType();
2409 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002410 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2411 // Use array's original type only if it has known number of
2412 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002413 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002414 PType = PVDecl->getType();
2415 } else if (PType->isFunctionType())
2416 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002417 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002418 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002419 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002420 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002421 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002422 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002423 }
2424}
2425
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002426/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002427/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002428/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2429/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002430/// Property attributes are stored as a comma-delimited C string. The simple
2431/// attributes readonly and bycopy are encoded as single characters. The
2432/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2433/// encoded as single characters, followed by an identifier. Property types
2434/// are also encoded as a parametrized attribute. The characters used to encode
2435/// these attributes are defined by the following enumeration:
2436/// @code
2437/// enum PropertyAttributes {
2438/// kPropertyReadOnly = 'R', // property is read-only.
2439/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2440/// kPropertyByref = '&', // property is a reference to the value last assigned
2441/// kPropertyDynamic = 'D', // property is dynamic
2442/// kPropertyGetter = 'G', // followed by getter selector name
2443/// kPropertySetter = 'S', // followed by setter selector name
2444/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2445/// kPropertyType = 't' // followed by old-style type encoding.
2446/// kPropertyWeak = 'W' // 'weak' property
2447/// kPropertyStrong = 'P' // property GC'able
2448/// kPropertyNonAtomic = 'N' // property non-atomic
2449/// };
2450/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002451void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2452 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002453 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002454 // Collect information from the property implementation decl(s).
2455 bool Dynamic = false;
2456 ObjCPropertyImplDecl *SynthesizePID = 0;
2457
2458 // FIXME: Duplicated code due to poor abstraction.
2459 if (Container) {
2460 if (const ObjCCategoryImplDecl *CID =
2461 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2462 for (ObjCCategoryImplDecl::propimpl_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002463 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregorcd19b572009-04-23 01:02:12 +00002464 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002465 ObjCPropertyImplDecl *PID = *i;
2466 if (PID->getPropertyDecl() == PD) {
2467 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2468 Dynamic = true;
2469 } else {
2470 SynthesizePID = PID;
2471 }
2472 }
2473 }
2474 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002475 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002476 for (ObjCCategoryImplDecl::propimpl_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002477 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregorcd19b572009-04-23 01:02:12 +00002478 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002479 ObjCPropertyImplDecl *PID = *i;
2480 if (PID->getPropertyDecl() == PD) {
2481 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2482 Dynamic = true;
2483 } else {
2484 SynthesizePID = PID;
2485 }
2486 }
2487 }
2488 }
2489 }
2490
2491 // FIXME: This is not very efficient.
2492 S = "T";
2493
2494 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002495 // GCC has some special rules regarding encoding of properties which
2496 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002497 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002498 true /* outermost type */,
2499 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002500
2501 if (PD->isReadOnly()) {
2502 S += ",R";
2503 } else {
2504 switch (PD->getSetterKind()) {
2505 case ObjCPropertyDecl::Assign: break;
2506 case ObjCPropertyDecl::Copy: S += ",C"; break;
2507 case ObjCPropertyDecl::Retain: S += ",&"; break;
2508 }
2509 }
2510
2511 // It really isn't clear at all what this means, since properties
2512 // are "dynamic by default".
2513 if (Dynamic)
2514 S += ",D";
2515
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002516 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2517 S += ",N";
2518
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002519 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2520 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002521 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002522 }
2523
2524 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2525 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002526 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002527 }
2528
2529 if (SynthesizePID) {
2530 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2531 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002532 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002533 }
2534
2535 // FIXME: OBJCGC: weak & strong
2536}
2537
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002538/// getLegacyIntegralTypeEncoding -
2539/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002540/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002541/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2542///
2543void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
Mike Stump6eeaa782009-07-22 18:58:19 +00002544 if (isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002545 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002546 if (BT->getKind() == BuiltinType::ULong &&
2547 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002548 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002549 else
2550 if (BT->getKind() == BuiltinType::Long &&
2551 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002552 PointeeTy = IntTy;
2553 }
2554 }
2555}
2556
Fariborz Jahanian248db262008-01-22 22:44:46 +00002557void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002558 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002559 // We follow the behavior of gcc, expanding structures which are
2560 // directly pointed to, and expanding embedded structures. Note that
2561 // these rules are sufficient to prevent recursive encoding of the
2562 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002563 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2564 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002565}
2566
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002567static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002568 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002569 const Expr *E = FD->getBitWidth();
2570 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2571 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman5255e7a2009-04-26 19:19:15 +00002572 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002573 S += 'b';
2574 S += llvm::utostr(N);
2575}
2576
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002577void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2578 bool ExpandPointedToStructures,
2579 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002580 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002581 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002582 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002583 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Chris Lattner26e73852009-07-13 00:10:46 +00002584 if (FD && FD->isBitField())
2585 return EncodeBitField(this, S, FD);
2586 char encoding;
2587 switch (BT->getKind()) {
2588 default: assert(0 && "Unhandled builtin type kind");
2589 case BuiltinType::Void: encoding = 'v'; break;
2590 case BuiltinType::Bool: encoding = 'B'; break;
2591 case BuiltinType::Char_U:
2592 case BuiltinType::UChar: encoding = 'C'; break;
2593 case BuiltinType::UShort: encoding = 'S'; break;
2594 case BuiltinType::UInt: encoding = 'I'; break;
2595 case BuiltinType::ULong:
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002596 encoding =
Chris Lattner26e73852009-07-13 00:10:46 +00002597 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002598 break;
Chris Lattner26e73852009-07-13 00:10:46 +00002599 case BuiltinType::UInt128: encoding = 'T'; break;
2600 case BuiltinType::ULongLong: encoding = 'Q'; break;
2601 case BuiltinType::Char_S:
2602 case BuiltinType::SChar: encoding = 'c'; break;
2603 case BuiltinType::Short: encoding = 's'; break;
2604 case BuiltinType::Int: encoding = 'i'; break;
2605 case BuiltinType::Long:
2606 encoding =
2607 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2608 break;
2609 case BuiltinType::LongLong: encoding = 'q'; break;
2610 case BuiltinType::Int128: encoding = 't'; break;
2611 case BuiltinType::Float: encoding = 'f'; break;
2612 case BuiltinType::Double: encoding = 'd'; break;
2613 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002614 }
Chris Lattner26e73852009-07-13 00:10:46 +00002615
2616 S += encoding;
2617 return;
2618 }
2619
2620 if (const ComplexType *CT = T->getAsComplexType()) {
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002621 S += 'j';
2622 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2623 false);
Chris Lattner26e73852009-07-13 00:10:46 +00002624 return;
2625 }
2626
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00002627 if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002628 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002629 bool isReadOnly = false;
2630 // For historical/compatibility reasons, the read-only qualifier of the
2631 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2632 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2633 // Also, do not emit the 'r' for anything but the outermost type!
Mike Stump6eeaa782009-07-22 18:58:19 +00002634 if (isa<TypedefType>(T.getTypePtr())) {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002635 if (OutermostType && T.isConstQualified()) {
2636 isReadOnly = true;
2637 S += 'r';
2638 }
2639 }
2640 else if (OutermostType) {
2641 QualType P = PointeeTy;
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00002642 while (P->getAsPointerType())
2643 P = P->getAsPointerType()->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002644 if (P.isConstQualified()) {
2645 isReadOnly = true;
2646 S += 'r';
2647 }
2648 }
2649 if (isReadOnly) {
2650 // Another legacy compatibility encoding. Some ObjC qualifier and type
2651 // combinations need to be rearranged.
2652 // Rewrite "in const" from "nr" to "rn"
2653 const char * s = S.c_str();
2654 int len = S.length();
2655 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2656 std::string replace = "rn";
2657 S.replace(S.end()-2, S.end(), replace);
2658 }
2659 }
Steve Naroff329ec222009-07-10 23:34:53 +00002660 if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002661 S += ':';
2662 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002663 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002664
2665 if (PointeeTy->isCharType()) {
2666 // char pointer types should be encoded as '*' unless it is a
2667 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002668 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002669 S += '*';
2670 return;
2671 }
Steve Naroff0b60cf82009-07-22 17:14:51 +00002672 } else if (const RecordType *RTy = PointeeTy->getAsRecordType()) {
2673 // GCC binary compat: Need to convert "struct objc_class *" to "#".
2674 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
2675 S += '#';
2676 return;
2677 }
2678 // GCC binary compat: Need to convert "struct objc_object *" to "@".
2679 if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
2680 S += '@';
2681 return;
2682 }
2683 // fall through...
Anders Carlsson36f07d82007-10-29 05:01:08 +00002684 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002685 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002686 getLegacyIntegralTypeEncoding(PointeeTy);
2687
Chris Lattner26e73852009-07-13 00:10:46 +00002688 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002689 NULL);
Chris Lattner26e73852009-07-13 00:10:46 +00002690 return;
2691 }
2692
2693 if (const ArrayType *AT =
2694 // Ignore type qualifiers etc.
2695 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002696 if (isa<IncompleteArrayType>(AT)) {
2697 // Incomplete arrays are encoded as a pointer to the array element.
2698 S += '^';
2699
2700 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2701 false, ExpandStructures, FD);
2702 } else {
2703 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002704
Anders Carlsson858c64d2009-02-22 01:38:57 +00002705 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2706 S += llvm::utostr(CAT->getSize().getZExtValue());
2707 else {
2708 //Variable length arrays are encoded as a regular array with 0 elements.
2709 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2710 S += '0';
2711 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002712
Anders Carlsson858c64d2009-02-22 01:38:57 +00002713 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2714 false, ExpandStructures, FD);
2715 S += ']';
2716 }
Chris Lattner26e73852009-07-13 00:10:46 +00002717 return;
2718 }
2719
2720 if (T->getAsFunctionType()) {
Anders Carlsson5695bb72007-10-30 00:06:20 +00002721 S += '?';
Chris Lattner26e73852009-07-13 00:10:46 +00002722 return;
2723 }
2724
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00002725 if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002726 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002727 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002728 // Anonymous structures print as '?'
2729 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2730 S += II->getName();
2731 } else {
2732 S += '?';
2733 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002734 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002735 S += '=';
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00002736 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2737 FieldEnd = RDecl->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +00002738 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002739 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002740 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002741 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002742 S += '"';
2743 }
2744
2745 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002746 if (Field->isBitField()) {
2747 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2748 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002749 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002750 QualType qt = Field->getType();
2751 getLegacyIntegralTypeEncoding(qt);
2752 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002753 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002754 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002755 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002756 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002757 S += RDecl->isUnion() ? ')' : '}';
Chris Lattner26e73852009-07-13 00:10:46 +00002758 return;
2759 }
2760
2761 if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002762 if (FD && FD->isBitField())
2763 EncodeBitField(this, S, FD);
2764 else
2765 S += 'i';
Chris Lattner26e73852009-07-13 00:10:46 +00002766 return;
2767 }
2768
2769 if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002770 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattner26e73852009-07-13 00:10:46 +00002771 return;
2772 }
2773
2774 if (T->isObjCInterfaceType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002775 // @encode(class_name)
2776 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2777 S += '{';
2778 const IdentifierInfo *II = OI->getIdentifier();
2779 S += II->getName();
2780 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002781 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002782 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002783 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002784 if (RecFields[i]->isBitField())
2785 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2786 RecFields[i]);
2787 else
2788 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2789 FD);
2790 }
2791 S += '}';
Chris Lattner26e73852009-07-13 00:10:46 +00002792 return;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002793 }
Chris Lattner26e73852009-07-13 00:10:46 +00002794
2795 if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) {
Steve Naroff329ec222009-07-10 23:34:53 +00002796 if (OPT->isObjCIdType()) {
2797 S += '@';
2798 return;
Chris Lattner26e73852009-07-13 00:10:46 +00002799 }
2800
2801 if (OPT->isObjCClassType()) {
Steve Naroff329ec222009-07-10 23:34:53 +00002802 S += '#';
2803 return;
Chris Lattner26e73852009-07-13 00:10:46 +00002804 }
2805
2806 if (OPT->isObjCQualifiedIdType()) {
Steve Naroff329ec222009-07-10 23:34:53 +00002807 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2808 ExpandPointedToStructures,
2809 ExpandStructures, FD);
2810 if (FD || EncodingProperty) {
2811 // Note that we do extended encoding of protocol qualifer list
2812 // Only when doing ivar or property encoding.
Steve Naroff329ec222009-07-10 23:34:53 +00002813 S += '"';
Steve Naroff8194a542009-07-20 17:56:53 +00002814 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
2815 E = OPT->qual_end(); I != E; ++I) {
Steve Naroff329ec222009-07-10 23:34:53 +00002816 S += '<';
2817 S += (*I)->getNameAsString();
2818 S += '>';
2819 }
2820 S += '"';
2821 }
2822 return;
Chris Lattner26e73852009-07-13 00:10:46 +00002823 }
2824
2825 QualType PointeeTy = OPT->getPointeeType();
2826 if (!EncodingProperty &&
2827 isa<TypedefType>(PointeeTy.getTypePtr())) {
2828 // Another historical/compatibility reason.
2829 // We encode the underlying type which comes out as
2830 // {...};
2831 S += '^';
2832 getObjCEncodingForTypeImpl(PointeeTy, S,
2833 false, ExpandPointedToStructures,
2834 NULL);
Steve Naroff329ec222009-07-10 23:34:53 +00002835 return;
2836 }
Chris Lattner26e73852009-07-13 00:10:46 +00002837
2838 S += '@';
2839 if (FD || EncodingProperty) {
Chris Lattner26e73852009-07-13 00:10:46 +00002840 S += '"';
Steve Naroff8194a542009-07-20 17:56:53 +00002841 S += OPT->getInterfaceDecl()->getNameAsCString();
2842 for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
2843 E = OPT->qual_end(); I != E; ++I) {
Chris Lattner26e73852009-07-13 00:10:46 +00002844 S += '<';
2845 S += (*I)->getNameAsString();
2846 S += '>';
2847 }
2848 S += '"';
2849 }
2850 return;
2851 }
2852
2853 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002854}
2855
Ted Kremenek42730c52008-01-07 19:49:32 +00002856void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002857 std::string& S) const {
2858 if (QT & Decl::OBJC_TQ_In)
2859 S += 'n';
2860 if (QT & Decl::OBJC_TQ_Inout)
2861 S += 'N';
2862 if (QT & Decl::OBJC_TQ_Out)
2863 S += 'o';
2864 if (QT & Decl::OBJC_TQ_Bycopy)
2865 S += 'O';
2866 if (QT & Decl::OBJC_TQ_Byref)
2867 S += 'R';
2868 if (QT & Decl::OBJC_TQ_Oneway)
2869 S += 'V';
2870}
2871
Chris Lattner26e73852009-07-13 00:10:46 +00002872void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002873 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2874
2875 BuiltinVaListType = T;
2876}
2877
Chris Lattner26e73852009-07-13 00:10:46 +00002878void ASTContext::setObjCIdType(QualType T) {
Steve Naroff7bffd372009-07-15 18:40:39 +00002879 ObjCIdTypedefType = T;
Steve Naroff9d12c902007-10-15 14:41:52 +00002880}
2881
Chris Lattner26e73852009-07-13 00:10:46 +00002882void ASTContext::setObjCSelType(QualType T) {
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002883 ObjCSelType = T;
2884
2885 const TypedefType *TT = T->getAsTypedefType();
2886 if (!TT)
2887 return;
2888 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002889
2890 // typedef struct objc_selector *SEL;
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00002891 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002892 if (!ptr)
2893 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002894 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002895 if (!rec)
2896 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002897 SelStructType = rec;
2898}
2899
Chris Lattner26e73852009-07-13 00:10:46 +00002900void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremenek42730c52008-01-07 19:49:32 +00002901 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002902}
2903
Chris Lattner26e73852009-07-13 00:10:46 +00002904void ASTContext::setObjCClassType(QualType T) {
Steve Naroff7bffd372009-07-15 18:40:39 +00002905 ObjCClassTypedefType = T;
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002906}
2907
Ted Kremenek42730c52008-01-07 19:49:32 +00002908void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2909 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002910 "'NSConstantString' type already set!");
2911
Ted Kremenek42730c52008-01-07 19:49:32 +00002912 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002913}
2914
Douglas Gregordd13e842009-03-30 22:58:21 +00002915/// \brief Retrieve the template name that represents a qualified
2916/// template name such as \c std::vector.
2917TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2918 bool TemplateKeyword,
2919 TemplateDecl *Template) {
2920 llvm::FoldingSetNodeID ID;
2921 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2922
2923 void *InsertPos = 0;
2924 QualifiedTemplateName *QTN =
2925 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2926 if (!QTN) {
2927 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2928 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2929 }
2930
2931 return TemplateName(QTN);
2932}
2933
2934/// \brief Retrieve the template name that represents a dependent
2935/// template name such as \c MetaFun::template apply.
2936TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2937 const IdentifierInfo *Name) {
2938 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2939
2940 llvm::FoldingSetNodeID ID;
2941 DependentTemplateName::Profile(ID, NNS, Name);
2942
2943 void *InsertPos = 0;
2944 DependentTemplateName *QTN =
2945 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2946
2947 if (QTN)
2948 return TemplateName(QTN);
2949
2950 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2951 if (CanonNNS == NNS) {
2952 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2953 } else {
2954 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2955 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2956 }
2957
2958 DependentTemplateNames.InsertNode(QTN, InsertPos);
2959 return TemplateName(QTN);
2960}
2961
Douglas Gregorc6507e42008-11-03 14:12:49 +00002962/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002963/// TargetInfo, produce the corresponding type. The unsigned @p Type
2964/// is actually a value of type @c TargetInfo::IntType.
2965QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002966 switch (Type) {
2967 case TargetInfo::NoInt: return QualType();
2968 case TargetInfo::SignedShort: return ShortTy;
2969 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2970 case TargetInfo::SignedInt: return IntTy;
2971 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2972 case TargetInfo::SignedLong: return LongTy;
2973 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2974 case TargetInfo::SignedLongLong: return LongLongTy;
2975 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2976 }
2977
2978 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002979 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002980}
Ted Kremenek118930e2008-07-24 23:58:27 +00002981
2982//===----------------------------------------------------------------------===//
2983// Type Predicates.
2984//===----------------------------------------------------------------------===//
2985
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002986/// isObjCNSObjectType - Return true if this is an NSObject object using
2987/// NSObject attribute on a c-style pointer type.
2988/// FIXME - Make it work directly on types.
Steve Naroffad75bd22009-07-16 15:41:00 +00002989/// FIXME: Move to Type.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002990///
2991bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2992 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2993 if (TypedefDecl *TD = TDT->getDecl())
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +00002994 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002995 return true;
2996 }
2997 return false;
2998}
2999
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00003000/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3001/// garbage collection attribute.
3002///
3003QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00003004 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00003005 if (getLangOptions().ObjC1 &&
3006 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00003007 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00003008 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00003009 // (or pointers to them) be treated as though they were declared
3010 // as __strong.
3011 if (GCAttrs == QualType::GCNone) {
Steve Naroffad75bd22009-07-16 15:41:00 +00003012 if (Ty->isObjCObjectPointerType())
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00003013 GCAttrs = QualType::Strong;
3014 else if (Ty->isPointerType())
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00003015 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00003016 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00003017 // Non-pointers have none gc'able attribute regardless of the attribute
3018 // set on them.
Steve Naroffad75bd22009-07-16 15:41:00 +00003019 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00003020 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00003021 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00003022 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00003023}
3024
Chris Lattner6ff358b2008-04-07 06:51:04 +00003025//===----------------------------------------------------------------------===//
3026// Type Compatibility Testing
3027//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00003028
Chris Lattner6ff358b2008-04-07 06:51:04 +00003029/// areCompatVectorTypes - Return true if the two specified vector types are
3030/// compatible.
3031static bool areCompatVectorTypes(const VectorType *LHS,
3032 const VectorType *RHS) {
3033 assert(LHS->isCanonical() && RHS->isCanonical());
3034 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003035 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00003036}
3037
Steve Naroff99eb86b2009-07-23 01:01:38 +00003038//===----------------------------------------------------------------------===//
3039// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
3040//===----------------------------------------------------------------------===//
3041
3042/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
3043/// inheritance hierarchy of 'rProto'.
3044static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
3045 ObjCProtocolDecl *rProto) {
3046 if (lProto == rProto)
3047 return true;
3048 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
3049 E = rProto->protocol_end(); PI != E; ++PI)
3050 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3051 return true;
3052 return false;
3053}
3054
3055/// ClassImplementsProtocol - Checks that 'lProto' protocol
3056/// has been implemented in IDecl class, its super class or categories (if
3057/// lookupCategory is true).
3058static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
3059 ObjCInterfaceDecl *IDecl,
3060 bool lookupCategory,
3061 bool RHSIsQualifiedID = false) {
3062
3063 // 1st, look up the class.
3064 const ObjCList<ObjCProtocolDecl> &Protocols =
3065 IDecl->getReferencedProtocols();
3066
3067 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
3068 E = Protocols.end(); PI != E; ++PI) {
3069 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3070 return true;
3071 // This is dubious and is added to be compatible with gcc. In gcc, it is
3072 // also allowed assigning a protocol-qualified 'id' type to a LHS object
3073 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
3074 // object. This IMO, should be a bug.
3075 // FIXME: Treat this as an extension, and flag this as an error when GCC
3076 // extensions are not enabled.
3077 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
3078 return true;
3079 }
3080
3081 // 2nd, look up the category.
3082 if (lookupCategory)
3083 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
3084 CDecl = CDecl->getNextClassCategory()) {
3085 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
3086 E = CDecl->protocol_end(); PI != E; ++PI)
3087 if (ProtocolCompatibleWithProtocol(lProto, *PI))
3088 return true;
3089 }
3090
3091 // 3rd, look up the super class(s)
3092 if (IDecl->getSuperClass())
3093 return
3094 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
3095 RHSIsQualifiedID);
3096
3097 return false;
3098}
3099
3100/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
3101/// return true if lhs's protocols conform to rhs's protocol; false
3102/// otherwise.
3103bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
3104 if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
3105 return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
3106 return false;
3107}
3108
3109/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
3110/// ObjCQualifiedIDType.
3111bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
3112 bool compare) {
3113 // Allow id<P..> and an 'id' or void* type in all cases.
3114 if (lhs->isVoidPointerType() ||
3115 lhs->isObjCIdType() || lhs->isObjCClassType())
3116 return true;
3117 else if (rhs->isVoidPointerType() ||
3118 rhs->isObjCIdType() || rhs->isObjCClassType())
3119 return true;
3120
3121 if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
3122 const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType();
3123
3124 if (!rhsOPT) return false;
3125
3126 if (rhsOPT->qual_empty()) {
3127 // If the RHS is a unqualified interface pointer "NSString*",
3128 // make sure we check the class hierarchy.
3129 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3130 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3131 E = lhsQID->qual_end(); I != E; ++I) {
3132 // when comparing an id<P> on lhs with a static type on rhs,
3133 // see if static class implements all of id's protocols, directly or
3134 // through its super class and categories.
3135 if (!ClassImplementsProtocol(*I, rhsID, true))
3136 return false;
3137 }
3138 }
3139 // If there are no qualifiers and no interface, we have an 'id'.
3140 return true;
3141 }
3142 // Both the right and left sides have qualifiers.
3143 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3144 E = lhsQID->qual_end(); I != E; ++I) {
3145 ObjCProtocolDecl *lhsProto = *I;
3146 bool match = false;
3147
3148 // when comparing an id<P> on lhs with a static type on rhs,
3149 // see if static class implements all of id's protocols, directly or
3150 // through its super class and categories.
3151 for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
3152 E = rhsOPT->qual_end(); J != E; ++J) {
3153 ObjCProtocolDecl *rhsProto = *J;
3154 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3155 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3156 match = true;
3157 break;
3158 }
3159 }
3160 // If the RHS is a qualified interface pointer "NSString<P>*",
3161 // make sure we check the class hierarchy.
3162 if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
3163 for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
3164 E = lhsQID->qual_end(); I != E; ++I) {
3165 // when comparing an id<P> on lhs with a static type on rhs,
3166 // see if static class implements all of id's protocols, directly or
3167 // through its super class and categories.
3168 if (ClassImplementsProtocol(*I, rhsID, true)) {
3169 match = true;
3170 break;
3171 }
3172 }
3173 }
3174 if (!match)
3175 return false;
3176 }
3177
3178 return true;
3179 }
3180
3181 const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
3182 assert(rhsQID && "One of the LHS/RHS should be id<x>");
3183
3184 if (const ObjCObjectPointerType *lhsOPT =
3185 lhs->getAsObjCInterfacePointerType()) {
3186 if (lhsOPT->qual_empty()) {
3187 bool match = false;
3188 if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
3189 for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(),
3190 E = rhsQID->qual_end(); I != E; ++I) {
3191 // when comparing an id<P> on lhs with a static type on rhs,
3192 // see if static class implements all of id's protocols, directly or
3193 // through its super class and categories.
3194 if (ClassImplementsProtocol(*I, lhsID, true)) {
3195 match = true;
3196 break;
3197 }
3198 }
3199 if (!match)
3200 return false;
3201 }
3202 return true;
3203 }
3204 // Both the right and left sides have qualifiers.
3205 for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
3206 E = lhsOPT->qual_end(); I != E; ++I) {
3207 ObjCProtocolDecl *lhsProto = *I;
3208 bool match = false;
3209
3210 // when comparing an id<P> on lhs with a static type on rhs,
3211 // see if static class implements all of id's protocols, directly or
3212 // through its super class and categories.
3213 for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
3214 E = rhsQID->qual_end(); J != E; ++J) {
3215 ObjCProtocolDecl *rhsProto = *J;
3216 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
3217 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
3218 match = true;
3219 break;
3220 }
3221 }
3222 if (!match)
3223 return false;
3224 }
3225 return true;
3226 }
3227 return false;
3228}
3229
Eli Friedman0d9549b2008-08-22 00:56:42 +00003230/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00003231/// compatible for assignment from RHS to LHS. This handles validation of any
3232/// protocol qualifiers on the LHS or RHS.
3233///
Steve Naroff329ec222009-07-10 23:34:53 +00003234bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3235 const ObjCObjectPointerType *RHSOPT) {
Steve Naroff7bffd372009-07-15 18:40:39 +00003236 // If either type represents the built-in 'id' or 'Class' types, return true.
3237 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff329ec222009-07-10 23:34:53 +00003238 return true;
3239
Steve Naroff99eb86b2009-07-23 01:01:38 +00003240 if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
3241 return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
3242 QualType(RHSOPT,0),
3243 false);
3244
Steve Naroff329ec222009-07-10 23:34:53 +00003245 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3246 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroff99eb86b2009-07-23 01:01:38 +00003247 if (LHS && RHS) // We have 2 user-defined types.
3248 return canAssignObjCInterfaces(LHS, RHS);
3249
3250 return false;
Steve Naroff329ec222009-07-10 23:34:53 +00003251}
3252
Eli Friedman0d9549b2008-08-22 00:56:42 +00003253bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3254 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00003255 // Verify that the base decls are compatible: the RHS must be a subclass of
3256 // the LHS.
3257 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3258 return false;
3259
3260 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3261 // protocol qualified at all, then we are good.
Steve Naroff77763c52009-07-18 15:33:26 +00003262 if (LHS->getNumProtocols() == 0)
Chris Lattner6ff358b2008-04-07 06:51:04 +00003263 return true;
3264
3265 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3266 // isn't a superset.
Steve Naroff77763c52009-07-18 15:33:26 +00003267 if (RHS->getNumProtocols() == 0)
Chris Lattner6ff358b2008-04-07 06:51:04 +00003268 return true; // FIXME: should return false!
3269
Steve Naroff77763c52009-07-18 15:33:26 +00003270 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3271 LHSPE = LHS->qual_end();
Steve Naroff98e71b82009-03-01 16:12:44 +00003272 LHSPI != LHSPE; LHSPI++) {
3273 bool RHSImplementsProtocol = false;
3274
3275 // If the RHS doesn't implement the protocol on the left, the types
3276 // are incompatible.
Steve Naroff77763c52009-07-18 15:33:26 +00003277 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
Steve Naroff99eb86b2009-07-23 01:01:38 +00003278 RHSPE = RHS->qual_end();
Steve Naroffa9604792009-07-16 16:21:02 +00003279 RHSPI != RHSPE; RHSPI++) {
3280 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff98e71b82009-03-01 16:12:44 +00003281 RHSImplementsProtocol = true;
Steve Naroffa9604792009-07-16 16:21:02 +00003282 break;
3283 }
Steve Naroff98e71b82009-03-01 16:12:44 +00003284 }
3285 // FIXME: For better diagnostics, consider passing back the protocol name.
3286 if (!RHSImplementsProtocol)
3287 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00003288 }
Steve Naroff98e71b82009-03-01 16:12:44 +00003289 // The RHS implements all protocols listed on the LHS.
3290 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00003291}
3292
Steve Naroff17c03822009-02-12 17:52:19 +00003293bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3294 // get the "pointed to" types
Steve Naroff329ec222009-07-10 23:34:53 +00003295 const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType();
3296 const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType();
Steve Naroff17c03822009-02-12 17:52:19 +00003297
Steve Naroff329ec222009-07-10 23:34:53 +00003298 if (!LHSOPT || !RHSOPT)
Steve Naroff17c03822009-02-12 17:52:19 +00003299 return false;
Steve Naroff329ec222009-07-10 23:34:53 +00003300
3301 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3302 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff17c03822009-02-12 17:52:19 +00003303}
3304
Steve Naroff85f0dc52007-10-15 20:41:53 +00003305/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
3306/// both shall have the identically qualified version of a compatible type.
3307/// C99 6.2.7p1: Two types have compatible types if their types are the
3308/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003309bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3310 return !mergeTypes(LHS, RHS).isNull();
3311}
3312
3313QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
3314 const FunctionType *lbase = lhs->getAsFunctionType();
3315 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00003316 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3317 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003318 bool allLTypes = true;
3319 bool allRTypes = true;
3320
3321 // Check return type
3322 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3323 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003324 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3325 allLTypes = false;
3326 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3327 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003328
3329 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl2767d882009-05-27 22:11:52 +00003330 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3331 "C++ shouldn't be here");
Eli Friedman0d9549b2008-08-22 00:56:42 +00003332 unsigned lproto_nargs = lproto->getNumArgs();
3333 unsigned rproto_nargs = rproto->getNumArgs();
3334
3335 // Compatible functions must have the same number of arguments
3336 if (lproto_nargs != rproto_nargs)
3337 return QualType();
3338
3339 // Variadic and non-variadic functions aren't compatible
3340 if (lproto->isVariadic() != rproto->isVariadic())
3341 return QualType();
3342
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00003343 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3344 return QualType();
3345
Eli Friedman0d9549b2008-08-22 00:56:42 +00003346 // Check argument compatibility
3347 llvm::SmallVector<QualType, 10> types;
3348 for (unsigned i = 0; i < lproto_nargs; i++) {
3349 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3350 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3351 QualType argtype = mergeTypes(largtype, rargtype);
3352 if (argtype.isNull()) return QualType();
3353 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003354 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3355 allLTypes = false;
3356 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3357 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003358 }
3359 if (allLTypes) return lhs;
3360 if (allRTypes) return rhs;
3361 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00003362 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00003363 }
3364
3365 if (lproto) allRTypes = false;
3366 if (rproto) allLTypes = false;
3367
Douglas Gregor4fa58902009-02-26 23:50:07 +00003368 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003369 if (proto) {
Sebastian Redl2767d882009-05-27 22:11:52 +00003370 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman0d9549b2008-08-22 00:56:42 +00003371 if (proto->isVariadic()) return QualType();
3372 // Check that the types are compatible with the types that
3373 // would result from default argument promotions (C99 6.7.5.3p15).
3374 // The only types actually affected are promotable integer
3375 // types and floats, which would be passed as a different
3376 // type depending on whether the prototype is visible.
3377 unsigned proto_nargs = proto->getNumArgs();
3378 for (unsigned i = 0; i < proto_nargs; ++i) {
3379 QualType argTy = proto->getArgType(i);
3380 if (argTy->isPromotableIntegerType() ||
3381 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3382 return QualType();
3383 }
3384
3385 if (allLTypes) return lhs;
3386 if (allRTypes) return rhs;
3387 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00003388 proto->getNumArgs(), lproto->isVariadic(),
3389 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00003390 }
3391
3392 if (allLTypes) return lhs;
3393 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00003394 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003395}
3396
3397QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00003398 // C++ [expr]: If an expression initially has the type "reference to T", the
3399 // type is adjusted to "T" prior to any further analysis, the expression
3400 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00003401 // expression is an lvalue unless the reference is an rvalue reference and
3402 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00003403 // FIXME: C++ shouldn't be going through here! The rules are different
3404 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00003405 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3406 // shouldn't be going through here!
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00003407 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003408 LHS = RT->getPointeeType();
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00003409 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003410 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00003411
Eli Friedman0d9549b2008-08-22 00:56:42 +00003412 QualType LHSCan = getCanonicalType(LHS),
3413 RHSCan = getCanonicalType(RHS);
3414
3415 // If two types are identical, they are compatible.
3416 if (LHSCan == RHSCan)
3417 return LHS;
3418
3419 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003420 // Note that we handle extended qualifiers later, in the
3421 // case for ExtQualType.
3422 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00003423 return QualType();
3424
Eli Friedmanaeae1ce2009-06-01 01:22:52 +00003425 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3426 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003427
Chris Lattnerc38d4522008-01-14 05:45:46 +00003428 // We want to consider the two function types to be the same for these
3429 // comparisons, just force one to the other.
3430 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3431 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00003432
Eli Friedmande43bf62009-06-02 05:28:56 +00003433 // Strip off objc_gc attributes off the top level so they can be merged.
3434 // This is a complete mess, but the attribute itself doesn't make much sense.
Fariborz Jahanian85d91262009-06-02 01:40:22 +00003435 if (RHSClass == Type::ExtQual) {
Eli Friedmande43bf62009-06-02 05:28:56 +00003436 QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
3437 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian562d0ee2009-06-02 20:58:58 +00003438 QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003439 // __weak attribute must appear on both declarations.
Fariborz Jahanian562d0ee2009-06-02 20:58:58 +00003440 // __strong attribue is redundant if other decl is an objective-c
3441 // object pointer (or decorated with __strong attribute); otherwise
3442 // issue error.
3443 if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
3444 (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
Steve Naroff329ec222009-07-10 23:34:53 +00003445 !LHSCan->isObjCObjectPointerType()))
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003446 return QualType();
3447
Eli Friedmande43bf62009-06-02 05:28:56 +00003448 RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
3449 RHS.getCVRQualifiers());
3450 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003451 if (!Result.isNull()) {
3452 if (Result.getObjCGCAttr() == QualType::GCNone)
3453 Result = getObjCGCQualType(Result, GCAttr);
3454 else if (Result.getObjCGCAttr() != GCAttr)
3455 Result = QualType();
3456 }
Eli Friedmande43bf62009-06-02 05:28:56 +00003457 return Result;
3458 }
Fariborz Jahanian85d91262009-06-02 01:40:22 +00003459 }
Fariborz Jahanian85d91262009-06-02 01:40:22 +00003460 if (LHSClass == Type::ExtQual) {
Eli Friedmande43bf62009-06-02 05:28:56 +00003461 QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
3462 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003463 QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
3464 // __weak attribute must appear on both declarations. __strong
Fariborz Jahanian562d0ee2009-06-02 20:58:58 +00003465 // __strong attribue is redundant if other decl is an objective-c
3466 // object pointer (or decorated with __strong attribute); otherwise
3467 // issue error.
3468 if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
3469 (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
Steve Naroff329ec222009-07-10 23:34:53 +00003470 !RHSCan->isObjCObjectPointerType()))
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003471 return QualType();
Fariborz Jahanian562d0ee2009-06-02 20:58:58 +00003472
Eli Friedmande43bf62009-06-02 05:28:56 +00003473 LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
3474 LHS.getCVRQualifiers());
3475 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian717e4fa2009-06-02 18:32:00 +00003476 if (!Result.isNull()) {
3477 if (Result.getObjCGCAttr() == QualType::GCNone)
3478 Result = getObjCGCQualType(Result, GCAttr);
3479 else if (Result.getObjCGCAttr() != GCAttr)
3480 Result = QualType();
3481 }
Eli Friedman430d9f12009-06-02 07:45:37 +00003482 return Result;
Eli Friedmande43bf62009-06-02 05:28:56 +00003483 }
Fariborz Jahanian85d91262009-06-02 01:40:22 +00003484 }
3485
Eli Friedman398837e2008-02-12 08:23:06 +00003486 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00003487 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3488 LHSClass = Type::ConstantArray;
3489 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3490 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003491
Nate Begemanaf6ed502008-04-18 23:10:10 +00003492 // Canonicalize ExtVector -> Vector.
3493 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3494 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00003495
3496 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003497 if (LHSClass != RHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00003498 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3499 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003500 if (const EnumType* ETy = LHS->getAsEnumType()) {
3501 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3502 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003503 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003504 if (const EnumType* ETy = RHS->getAsEnumType()) {
3505 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3506 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003507 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003508
Eli Friedman0d9549b2008-08-22 00:56:42 +00003509 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003510 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003511
Steve Naroffc88babe2008-01-09 22:43:08 +00003512 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003513 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003514#define TYPE(Class, Base)
3515#define ABSTRACT_TYPE(Class, Base)
3516#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3517#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3518#include "clang/AST/TypeNodes.def"
3519 assert(false && "Non-canonical and dependent types shouldn't get here");
3520 return QualType();
3521
Sebastian Redlce6fff02009-03-16 23:22:08 +00003522 case Type::LValueReference:
3523 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003524 case Type::MemberPointer:
3525 assert(false && "C++ should never be in mergeTypes");
3526 return QualType();
3527
3528 case Type::IncompleteArray:
3529 case Type::VariableArray:
3530 case Type::FunctionProto:
3531 case Type::ExtVector:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003532 assert(false && "Types are eliminated above");
3533 return QualType();
3534
Chris Lattnerc38d4522008-01-14 05:45:46 +00003535 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003536 {
3537 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00003538 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3539 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003540 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3541 if (ResultType.isNull()) return QualType();
Eli Friedmande43bf62009-06-02 05:28:56 +00003542 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003543 return LHS;
Eli Friedmande43bf62009-06-02 05:28:56 +00003544 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003545 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003546 return getPointerType(ResultType);
3547 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003548 case Type::BlockPointer:
3549 {
3550 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenekd9b39bf2009-07-17 17:50:17 +00003551 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3552 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003553 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3554 if (ResultType.isNull()) return QualType();
3555 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3556 return LHS;
3557 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3558 return RHS;
3559 return getBlockPointerType(ResultType);
3560 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003561 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003562 {
3563 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3564 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3565 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3566 return QualType();
3567
3568 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3569 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3570 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3571 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003572 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3573 return LHS;
3574 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3575 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003576 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3577 ArrayType::ArraySizeModifier(), 0);
3578 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3579 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003580 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3581 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003582 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3583 return LHS;
3584 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3585 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003586 if (LVAT) {
3587 // FIXME: This isn't correct! But tricky to implement because
3588 // the array's size has to be the size of LHS, but the type
3589 // has to be different.
3590 return LHS;
3591 }
3592 if (RVAT) {
3593 // FIXME: This isn't correct! But tricky to implement because
3594 // the array's size has to be the size of RHS, but the type
3595 // has to be different.
3596 return RHS;
3597 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003598 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3599 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor1d381132009-07-06 15:59:29 +00003600 return getIncompleteArrayType(ResultType,
3601 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003602 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003603 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003604 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003605 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003606 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003607 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003608 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003609 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003610 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003611 case Type::Complex:
3612 // Distinct complex types are incompatible.
3613 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003614 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003615 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003616 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3617 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003618 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003619 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003620 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003621 // FIXME: This should be type compatibility, e.g. whether
3622 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003623 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3624 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3625 if (LHSIface && RHSIface &&
3626 canAssignObjCInterfaces(LHSIface, RHSIface))
3627 return LHS;
3628
Eli Friedman0d9549b2008-08-22 00:56:42 +00003629 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003630 }
Steve Naroff329ec222009-07-10 23:34:53 +00003631 case Type::ObjCObjectPointer: {
Steve Naroff329ec222009-07-10 23:34:53 +00003632 if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(),
3633 RHS->getAsObjCObjectPointerType()))
3634 return LHS;
3635
Steve Naroff28ceff72008-12-10 22:14:21 +00003636 return QualType();
Steve Naroff329ec222009-07-10 23:34:53 +00003637 }
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003638 case Type::FixedWidthInt:
3639 // Distinct fixed-width integers are not compatible.
3640 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003641 case Type::ExtQual:
3642 // FIXME: ExtQual types can be compatible even if they're not
3643 // identical!
3644 return QualType();
3645 // First attempt at an implementation, but I'm not really sure it's
3646 // right...
3647#if 0
3648 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3649 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3650 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3651 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3652 return QualType();
3653 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3654 LHSBase = QualType(LQual->getBaseType(), 0);
3655 RHSBase = QualType(RQual->getBaseType(), 0);
3656 ResultType = mergeTypes(LHSBase, RHSBase);
3657 if (ResultType.isNull()) return QualType();
3658 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3659 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3660 return LHS;
3661 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3662 return RHS;
3663 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3664 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3665 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3666 return ResultType;
3667#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003668
3669 case Type::TemplateSpecialization:
3670 assert(false && "Dependent types have no size");
3671 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003672 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003673
3674 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003675}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003676
Chris Lattner1d78a862008-04-07 07:01:58 +00003677//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003678// Integer Predicates
3679//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003680
Eli Friedman0832dbc2008-06-28 06:23:08 +00003681unsigned ASTContext::getIntWidth(QualType T) {
3682 if (T == BoolTy)
3683 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003684 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3685 return FWIT->getWidth();
3686 }
3687 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003688 return (unsigned)getTypeSize(T);
3689}
3690
3691QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3692 assert(T->isSignedIntegerType() && "Unexpected type");
3693 if (const EnumType* ETy = T->getAsEnumType())
3694 T = ETy->getDecl()->getIntegerType();
3695 const BuiltinType* BTy = T->getAsBuiltinType();
3696 assert (BTy && "Unexpected signed integer type");
3697 switch (BTy->getKind()) {
3698 case BuiltinType::Char_S:
3699 case BuiltinType::SChar:
3700 return UnsignedCharTy;
3701 case BuiltinType::Short:
3702 return UnsignedShortTy;
3703 case BuiltinType::Int:
3704 return UnsignedIntTy;
3705 case BuiltinType::Long:
3706 return UnsignedLongTy;
3707 case BuiltinType::LongLong:
3708 return UnsignedLongLongTy;
Chris Lattner6cc7e412009-04-30 02:43:43 +00003709 case BuiltinType::Int128:
3710 return UnsignedInt128Ty;
Eli Friedman0832dbc2008-06-28 06:23:08 +00003711 default:
3712 assert(0 && "Unexpected signed integer type");
3713 return QualType();
3714 }
3715}
3716
Douglas Gregorc34897d2009-04-09 22:27:44 +00003717ExternalASTSource::~ExternalASTSource() { }
3718
3719void ExternalASTSource::PrintStats() { }
Chris Lattner260ad502009-06-14 00:45:47 +00003720
3721
3722//===----------------------------------------------------------------------===//
3723// Builtin Type Computation
3724//===----------------------------------------------------------------------===//
3725
3726/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
3727/// pointer over the consumed characters. This returns the resultant type.
3728static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
3729 ASTContext::GetBuiltinTypeError &Error,
3730 bool AllowTypeModifiers = true) {
3731 // Modifiers.
3732 int HowLong = 0;
3733 bool Signed = false, Unsigned = false;
3734
3735 // Read the modifiers first.
3736 bool Done = false;
3737 while (!Done) {
3738 switch (*Str++) {
3739 default: Done = true; --Str; break;
3740 case 'S':
3741 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
3742 assert(!Signed && "Can't use 'S' modifier multiple times!");
3743 Signed = true;
3744 break;
3745 case 'U':
3746 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
3747 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
3748 Unsigned = true;
3749 break;
3750 case 'L':
3751 assert(HowLong <= 2 && "Can't have LLLL modifier");
3752 ++HowLong;
3753 break;
3754 }
3755 }
3756
3757 QualType Type;
3758
3759 // Read the base type.
3760 switch (*Str++) {
3761 default: assert(0 && "Unknown builtin type letter!");
3762 case 'v':
3763 assert(HowLong == 0 && !Signed && !Unsigned &&
3764 "Bad modifiers used with 'v'!");
3765 Type = Context.VoidTy;
3766 break;
3767 case 'f':
3768 assert(HowLong == 0 && !Signed && !Unsigned &&
3769 "Bad modifiers used with 'f'!");
3770 Type = Context.FloatTy;
3771 break;
3772 case 'd':
3773 assert(HowLong < 2 && !Signed && !Unsigned &&
3774 "Bad modifiers used with 'd'!");
3775 if (HowLong)
3776 Type = Context.LongDoubleTy;
3777 else
3778 Type = Context.DoubleTy;
3779 break;
3780 case 's':
3781 assert(HowLong == 0 && "Bad modifiers used with 's'!");
3782 if (Unsigned)
3783 Type = Context.UnsignedShortTy;
3784 else
3785 Type = Context.ShortTy;
3786 break;
3787 case 'i':
3788 if (HowLong == 3)
3789 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
3790 else if (HowLong == 2)
3791 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
3792 else if (HowLong == 1)
3793 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
3794 else
3795 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
3796 break;
3797 case 'c':
3798 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
3799 if (Signed)
3800 Type = Context.SignedCharTy;
3801 else if (Unsigned)
3802 Type = Context.UnsignedCharTy;
3803 else
3804 Type = Context.CharTy;
3805 break;
3806 case 'b': // boolean
3807 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
3808 Type = Context.BoolTy;
3809 break;
3810 case 'z': // size_t.
3811 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
3812 Type = Context.getSizeType();
3813 break;
3814 case 'F':
3815 Type = Context.getCFConstantStringType();
3816 break;
3817 case 'a':
3818 Type = Context.getBuiltinVaListType();
3819 assert(!Type.isNull() && "builtin va list type not initialized!");
3820 break;
3821 case 'A':
3822 // This is a "reference" to a va_list; however, what exactly
3823 // this means depends on how va_list is defined. There are two
3824 // different kinds of va_list: ones passed by value, and ones
3825 // passed by reference. An example of a by-value va_list is
3826 // x86, where va_list is a char*. An example of by-ref va_list
3827 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
3828 // we want this argument to be a char*&; for x86-64, we want
3829 // it to be a __va_list_tag*.
3830 Type = Context.getBuiltinVaListType();
3831 assert(!Type.isNull() && "builtin va list type not initialized!");
3832 if (Type->isArrayType()) {
3833 Type = Context.getArrayDecayedType(Type);
3834 } else {
3835 Type = Context.getLValueReferenceType(Type);
3836 }
3837 break;
3838 case 'V': {
3839 char *End;
3840
3841 unsigned NumElements = strtoul(Str, &End, 10);
3842 assert(End != Str && "Missing vector size");
3843
3844 Str = End;
3845
3846 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
3847 Type = Context.getVectorType(ElementType, NumElements);
3848 break;
3849 }
3850 case 'P': {
Douglas Gregor151fac72009-07-07 16:35:42 +00003851 Type = Context.getFILEType();
3852 if (Type.isNull()) {
Chris Lattner260ad502009-06-14 00:45:47 +00003853 Error = ASTContext::GE_Missing_FILE;
3854 return QualType();
Douglas Gregor151fac72009-07-07 16:35:42 +00003855 } else {
3856 break;
Chris Lattner260ad502009-06-14 00:45:47 +00003857 }
3858 }
3859 }
3860
3861 if (!AllowTypeModifiers)
3862 return Type;
3863
3864 Done = false;
3865 while (!Done) {
3866 switch (*Str++) {
3867 default: Done = true; --Str; break;
3868 case '*':
3869 Type = Context.getPointerType(Type);
3870 break;
3871 case '&':
3872 Type = Context.getLValueReferenceType(Type);
3873 break;
3874 // FIXME: There's no way to have a built-in with an rvalue ref arg.
3875 case 'C':
3876 Type = Type.getQualifiedType(QualType::Const);
3877 break;
3878 }
3879 }
3880
3881 return Type;
3882}
3883
3884/// GetBuiltinType - Return the type for the specified builtin.
3885QualType ASTContext::GetBuiltinType(unsigned id,
3886 GetBuiltinTypeError &Error) {
3887 const char *TypeStr = BuiltinInfo.GetTypeString(id);
3888
3889 llvm::SmallVector<QualType, 8> ArgTypes;
3890
3891 Error = GE_None;
3892 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
3893 if (Error != GE_None)
3894 return QualType();
3895 while (TypeStr[0] && TypeStr[0] != '.') {
3896 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
3897 if (Error != GE_None)
3898 return QualType();
3899
3900 // Do array -> pointer decay. The builtin should use the decayed type.
3901 if (Ty->isArrayType())
3902 Ty = getArrayDecayedType(Ty);
3903
3904 ArgTypes.push_back(Ty);
3905 }
3906
3907 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
3908 "'.' should only occur at end of builtin type list!");
3909
3910 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
3911 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
3912 return getFunctionNoProtoType(ResType);
3913 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
3914 TypeStr[0] == '.', 0);
3915}